home *** CD-ROM | disk | FTP | other *** search
- /*
- Program to read the boot sector from a floppy disk; stores the data
- in a binary file named boot.bin for use with cformat.exe and also creates
- a human-readable file named boot.img. This is an alternative to the
- boot sector code generated by boot.asm.
-
- Tested with BC++ 3.1 (compilable as C or as C++)
-
- Compile: bcc -ms genboot.c
-
- Use: genboot [A | B]
- */
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <bios.h>
- #include <ctype.h>
-
- #define STDERR stderr
-
- const int bytes_per_sector = 512;
- const int retries = 3;
-
- const char* bootname_machine = "boot.bin";
- const char* bootname_human = "boot.img";
-
- int reset(int drive);
- int read_boot(int drive, unsigned char* buf);
- int write_files(unsigned char* buf);
-
- int main(int argc, char* argv[])
- {
- int drive;
-
- unsigned char* buf;
-
- if(argc != 2)
- {
- fputs("Need to specify drive letter on command line\n", STDERR);
- return 1;
- }
-
- drive = toupper(argv[1][0]) - 'A';
- if(drive < 0 || drive > 1)
- {
- fputs("Please specify floppy drive A: or B: on command line\n", STDERR);
- return 1;
- }
-
- /* allocate memory for sector buffer */
- buf = (unsigned char*) malloc( bytes_per_sector );
- if(!buf)
- {
- fputs("Unable to allocate memory for boot sector image\n", STDERR);
- return 1;
- }
-
- /* reset the disk controller */
- if( reset(drive) )
- {
- fputs("Unable to reset floppy controller\n", STDERR);
- free(buf);
- return 1;
- }
-
- /* read the boot sector */
- if( read_boot(drive, buf) )
- {
- fputs("Unable to read boot sector from floppy\n", STDERR);
- free(buf);
- return 1;
- }
-
- /* write the files */
- write_files(buf);
-
- free(buf);
- return 0;
- }
-
- int reset(int drive)
- {
- int status;
- int retry;
-
- for(retry=0; retry<retries; retry++)
- {
- if( (status = biosdisk(0, drive, 0, 0, 0, 0, 0)) == 0 )
- break;
- }
-
- if(retry == retries)
- return status;
-
- return 0;
- }
-
- int read_boot(int drive, unsigned char* buf)
- {
- int status;
- int retry;
-
- for(retry=0; retry<retries; retry++)
- {
- if( (status = biosdisk(2, drive, 0, 0, 1, 1, buf)) == 0 )
- break;
- }
-
- if(retry == retries)
- return status;
-
- return 0;
- }
-
- int write_files(unsigned char* buf)
- {
- int result;
- int i, j;
-
- FILE* stream = fopen(bootname_machine, "wb");
- if(!stream)
- {
- fprintf(STDERR, "Unable to open file '%s' for boot sector image\n", bootname_machine);
- return 1;
- }
-
- result = fwrite(buf, bytes_per_sector, 1, stream);
- if(result != 1)
- {
- fprintf(STDERR, "Unable to write data to '%s'\n", bootname_machine);
- return 1;
- }
-
- if( fclose(stream) )
- {
- fprintf(STDERR, "Unable to close file '%s' properly\n", bootname_machine);
- return 1;
- }
-
- stream = fopen(bootname_human, "wt");
- if(!stream)
- {
- fprintf(STDERR, "Unable to open file '%s' for human-readable boot sector image\n", bootname_human);
- return 1;
- }
-
- for(i=0; i<32; i++)
- {
- fprintf(stream, "%04X: ", i*16);
-
- for(j=0; j<8; j++)
- fprintf(stream, "%02X ", buf[i*16+j]);
-
- fputc(' ', stream);
-
- for(; j<16; j++)
- fprintf(stream, "%02X ", buf[i*16+j]);
-
- fprintf(stream, " ");
-
- for(j=0; j<16; j++)
- {
- int c = buf[i*16+j];
- if( isprint(c) )
- fputc(c, stream);
- else
- fputc('.', stream);
- }
-
- fputc('\n', stream);
- }
-
- if( ferror(stream) )
- {
- fprintf(STDERR, "Error writing file '%s'", bootname_human);
- return 1;
- }
-
- if( fclose(stream) )
- {
- fprintf(STDERR, "Unable to close file '%s' properly\n", bootname_human);
- return 1;
- }
-
- return 0;
- }
-