home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / bcpp / file7 / genboot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  3.9 KB  |  189 lines

  1. /*
  2.    Program to read the boot sector from a floppy disk; stores the data
  3.    in a binary file named boot.bin for use with cformat.exe and also creates
  4.    a human-readable file named boot.img.  This is an alternative to the
  5.    boot sector code generated by boot.asm.
  6.  
  7.    Tested with BC++ 3.1 (compilable as C or as C++)
  8.  
  9.    Compile: bcc -ms genboot.c
  10.  
  11.    Use:  genboot [A | B]
  12. */
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <conio.h>
  18. #include <bios.h>
  19. #include <ctype.h>
  20.  
  21. #define  STDERR   stderr
  22.  
  23. const int bytes_per_sector = 512;
  24. const int retries          =   3;
  25.  
  26. const char* bootname_machine = "boot.bin";
  27. const char* bootname_human   = "boot.img";
  28.  
  29. int reset(int drive);
  30. int read_boot(int drive, unsigned char* buf);
  31. int write_files(unsigned char* buf);
  32.  
  33. int main(int argc, char* argv[])
  34. {
  35.    int   drive;
  36.  
  37.    unsigned char* buf;
  38.  
  39.    if(argc != 2)
  40.       {
  41.       fputs("Need to specify drive letter on command line\n", STDERR);
  42.       return 1;
  43.       }
  44.  
  45.    drive = toupper(argv[1][0]) - 'A';
  46.    if(drive < 0  ||  drive > 1)
  47.       {
  48.       fputs("Please specify floppy drive A: or B: on command line\n", STDERR);
  49.       return 1;
  50.       }
  51.  
  52.    /* allocate memory for sector buffer */
  53.    buf = (unsigned char*) malloc( bytes_per_sector );
  54.    if(!buf)
  55.       {
  56.       fputs("Unable to allocate memory for boot sector image\n", STDERR);
  57.       return 1;
  58.       }
  59.  
  60.    /* reset the disk controller */
  61.    if( reset(drive) )
  62.       {
  63.       fputs("Unable to reset floppy controller\n", STDERR);
  64.       free(buf);
  65.       return 1;
  66.       }
  67.  
  68.    /* read the boot sector */
  69.    if( read_boot(drive, buf) )
  70.       {
  71.       fputs("Unable to read boot sector from floppy\n", STDERR);
  72.       free(buf);
  73.       return 1;
  74.       }
  75.  
  76.    /* write the files */
  77.    write_files(buf);
  78.  
  79.    free(buf);
  80.    return 0;
  81. }
  82.  
  83. int reset(int drive)
  84. {
  85.    int status;
  86.    int retry;
  87.  
  88.    for(retry=0; retry<retries; retry++)
  89.       {
  90.       if( (status = biosdisk(0, drive, 0, 0, 0, 0, 0)) == 0 )
  91.          break;
  92.       }
  93.  
  94.    if(retry == retries)
  95.       return status;
  96.  
  97.    return 0;
  98. }
  99.  
  100. int read_boot(int drive, unsigned char* buf)
  101. {
  102.    int status;
  103.    int retry;
  104.  
  105.    for(retry=0; retry<retries; retry++)
  106.       {
  107.       if( (status = biosdisk(2, drive, 0, 0, 1, 1, buf)) == 0 )
  108.          break;
  109.       }
  110.  
  111.    if(retry == retries)
  112.       return status;
  113.  
  114.    return 0;
  115. }
  116.  
  117. int write_files(unsigned char* buf)
  118. {
  119.    int result;
  120.    int i, j;
  121.  
  122.    FILE* stream = fopen(bootname_machine, "wb");
  123.    if(!stream)
  124.       {
  125.       fprintf(STDERR, "Unable to open file '%s' for boot sector image\n", bootname_machine);
  126.       return 1;
  127.       }
  128.  
  129.    result = fwrite(buf, bytes_per_sector, 1, stream);
  130.    if(result != 1)
  131.       {
  132.       fprintf(STDERR, "Unable to write data to '%s'\n", bootname_machine);
  133.       return 1;
  134.       }
  135.  
  136.    if( fclose(stream) )
  137.       {
  138.       fprintf(STDERR, "Unable to close file '%s' properly\n", bootname_machine);
  139.       return 1;
  140.       }
  141.  
  142.    stream = fopen(bootname_human, "wt");
  143.    if(!stream)
  144.       {
  145.       fprintf(STDERR, "Unable to open file '%s' for human-readable boot sector image\n", bootname_human);
  146.       return 1;
  147.       }
  148.  
  149.    for(i=0; i<32; i++)
  150.       {
  151.       fprintf(stream, "%04X:  ", i*16);
  152.  
  153.       for(j=0; j<8; j++)
  154.          fprintf(stream, "%02X ", buf[i*16+j]);
  155.  
  156.       fputc(' ', stream);
  157.  
  158.       for(; j<16; j++)
  159.          fprintf(stream, "%02X ", buf[i*16+j]);
  160.  
  161.       fprintf(stream, "  ");
  162.  
  163.       for(j=0; j<16; j++)
  164.          {
  165.          int c = buf[i*16+j];
  166.          if( isprint(c) )
  167.             fputc(c, stream);
  168.          else 
  169.             fputc('.', stream);
  170.          }
  171.  
  172.       fputc('\n', stream);
  173.       }
  174.  
  175.    if( ferror(stream) )
  176.       {
  177.       fprintf(STDERR, "Error writing file '%s'", bootname_human);
  178.       return 1;
  179.       }
  180.  
  181.    if( fclose(stream) )
  182.       {
  183.       fprintf(STDERR, "Unable to close file '%s' properly\n", bootname_human);
  184.       return 1;
  185.       }
  186.  
  187.    return 0;
  188. }
  189.