home *** CD-ROM | disk | FTP | other *** search
/ James Briskly's Game Magazine 2 / JBGM002S.ZIP / RESTORRB / RESTORRB.C next >
C/C++ Source or Header  |  1995-08-22  |  7KB  |  240 lines

  1. /*
  2.     FIPS - the First nondestructive Interactive Partition Splitting program
  3.     Module restorrb.c
  4.  
  5.     Copyright (C) 1993 Arno Schaefer
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 1, or (at your option)
  10.     any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <io.h>
  24. #include <stdlib.h>
  25. #include <dos.h>
  26. #include <bios.h>
  27. #include <alloc.h>
  28. #include <conio.h>
  29. #include <ctype.h>
  30.  
  31. #include "rtypes.h"
  32. #include "rversion.h"
  33.  
  34. #define DISK_INT 0x13
  35.  
  36. #define RESET_DISK 0
  37. #define WRITE_SECTOR 3
  38. #define VERIFY_SECTOR 4
  39.  
  40. #define DISK1 0x80
  41.  
  42. /* ----------------------------------------------------------------------- */
  43. /* Copyright notice and version number                                     */
  44. /* ----------------------------------------------------------------------- */
  45.  
  46. void notice (void)
  47. {
  48.     printf ("\nFIPS version " FIPS_VERSION ", Copyright (C) 1993/94 Arno Schaefer\n");
  49.     printf ("Module RESTORRB.EXE - Please read the file README.1ST\n");
  50.     printf ("FIPS comes with ABSOLUTELY NO WARRANTY, see file COPYING for details\n");
  51.     printf ("This is free software, and you are welcome to redistribute it\n");
  52.     printf ("under certain conditions; again see file COPYING for details.\n\n");
  53. }
  54.  
  55. /* ----------------------------------------------------------------------- */
  56. /* Error Handling                                                          */
  57. /* ----------------------------------------------------------------------- */
  58.  
  59. int getx (void)
  60. {
  61.     int character = getch();
  62.  
  63.     if (character == 3)
  64.     {
  65.         printf ("\n");
  66.         exit (0);
  67.     }
  68.     return (character);
  69. }
  70.  
  71. void error (char *message)
  72. {
  73.     fprintf (stderr,"\nError: %s!\n",message);
  74.     exit (-1);
  75. }
  76.  
  77. /* ----------------------------------------------------------------------- */
  78. /* BIOS calls                                                              */
  79. /* ----------------------------------------------------------------------- */
  80.  
  81. int reset_drives (void)
  82. {
  83.     union REGS regs;
  84.  
  85.     regs.h.ah = RESET_DISK;
  86.     regs.h.dl = DISK1;
  87.     int86 (DISK_INT,®s,®s);
  88.     if (regs.x.cflag) return (-1);
  89.     return 0;
  90. }
  91.  
  92. /* ----------------------------------------------------------------------- */
  93. /* read / write sectors                                                    */
  94. /* ----------------------------------------------------------------------- */
  95.  
  96. int verify_sector (int drive_number,dword head,dword cylinder,dword sector,byte *buffer)
  97. {
  98.     if (biosdisk (VERIFY_SECTOR,drive_number,head,cylinder,sector,1,buffer)) return (-1);
  99.     return 0;
  100. }
  101.  
  102. int write_sector (int drive_number,dword head,dword cylinder,dword sector,byte *buffer)
  103. {
  104.     int i;
  105.     boolean done=false;
  106.     for (i=0;i<3;i++)
  107.     {
  108.         if (!biosdisk (WRITE_SECTOR,drive_number,head,cylinder,sector,1,buffer))
  109.         {
  110.             done=true;
  111.             break;
  112.         }
  113.         reset_drives();
  114.     }
  115.     if (!done) return (-1);
  116.     return (verify_sector (drive_number,head,cylinder,sector,buffer));
  117. }
  118.  
  119. int write_root_sector (int drive_number,byte *buffer)
  120. {
  121.     return (write_sector (drive_number,0,0,1,buffer));
  122. }
  123.  
  124. /* ----------------------------------------------------------------------- */
  125. /* User Input                                                              */
  126. /* ----------------------------------------------------------------------- */
  127.  
  128. void ask_for_write_permission (char *filename)
  129. {
  130.     int character = 'x';
  131.  
  132.     printf ("\nReady to write old root- and bootsector from file %s to disk\n", filename);
  133.     printf ("Do you want to proceed (y/n): ");
  134.  
  135.     while ((character != 'y') && (character != 'n')) character = getx();
  136.     printf ("%c\n",character);
  137.     if (character == 'n') exit (0);
  138. }
  139.  
  140. /* ----------------------------------------------------------------------- */
  141. /* Main                                                                    */
  142. /* ----------------------------------------------------------------------- */
  143.  
  144. void main (void)
  145. {
  146.     byte rootsector[512];
  147.     byte bootsector[512];
  148.     int drive_number,partition_number,i;
  149.     FILE *handle;
  150.     dword head,cylinder,sector;
  151.     char *filename = "a:\\rootboot.000";
  152.     int no_of_savefiles = 0;
  153.     char first = 'x';
  154.     char list[10];
  155.  
  156.     notice();
  157.  
  158.     if (reset_drives ()) error ("Drive Initialization Failure");
  159.  
  160.     for (i='0';i<='9';i++)
  161.     {
  162.         filename[14] = i;
  163.         if (access (filename,0) == 0)
  164.         {
  165.             if (first == 'x') first = i;
  166.             list[no_of_savefiles++] = i;
  167.             printf ("Found save file %s\n",filename);
  168.         }
  169.     }
  170.  
  171.     if (no_of_savefiles == 0) error ("No savefile ROOTBOOT.00? found on disk A:");
  172.  
  173.     if (no_of_savefiles > 1)
  174.     {
  175.         printf ("\nWhich file do you want to restore (");
  176.         for (i = 0; i < no_of_savefiles; i++)
  177.         {
  178.             printf ("%c/", list[i]);
  179.         }
  180.         printf ("\b)? ");
  181.  
  182.         while (true)
  183.         {
  184.             int c;
  185.             if (isdigit (c = getx()))
  186.             {
  187.                 boolean found = false;
  188.  
  189.                 for (i = 0; i < no_of_savefiles; i++)
  190.                 {
  191.                     if (c == list[i]) found = true;
  192.                 }
  193.  
  194.                 if (found)
  195.                 {
  196.                     printf ("%c\n", c);
  197.                     filename[14] = c;
  198.                     break;
  199.                 }
  200.             }
  201.         }
  202.     }
  203.     else
  204.     {
  205.         filename[14] = first;
  206.     }
  207.  
  208.     if ((handle = fopen (filename,"rb")) == NULL)
  209.         error ("Can't open file");
  210.  
  211.     for (i=0;i<512;i++)
  212.     {
  213.         int character = fgetc (handle);
  214.         if (character == EOF) error ("Error reading file from disk");
  215.         *(rootsector + i) = character;
  216.     }
  217.     for (i=0;i<512;i++)
  218.     {
  219.         int character = fgetc (handle);
  220.         if (character == EOF) error ("Error reading file from disk");
  221.         *(bootsector + i) = character;
  222.     }
  223.     if ((drive_number = fgetc (handle)) == EOF) error ("Error reading file from disk");
  224.     if ((partition_number = fgetc (handle)) == EOF) error ("Error reading file from disk");
  225.     if (fclose (handle)) error ("Error closing file");
  226.  
  227.     head = (dword) rootsector[0x1be+16*partition_number+1];
  228.     cylinder = (((dword) rootsector[0x1be+16*partition_number+2] << 2) & 0x300)
  229.         | (dword) rootsector[0x1be+16*partition_number+3];
  230.     sector = (dword) rootsector[0x1be+16*partition_number+2] & 0x3f;
  231.  
  232.     ask_for_write_permission(filename);
  233.  
  234.     if (write_root_sector (drive_number,rootsector))
  235.         error ("Error writing rootsector");
  236.  
  237.     if (write_sector (drive_number,head,cylinder,sector,bootsector))
  238.         error ("Error writing bootsector");
  239. }
  240.