home *** CD-ROM | disk | FTP | other *** search
/ Zodiac Super OZ / MEDIADEPOT.ISO / FILES / 16 / FREEDOS.ZIP / FD_A4PRE.ZIP / SOURCE / FIPS11.ZIP / RESTORRB.C < prev    next >
C/C++ Source or Header  |  1994-05-26  |  7KB  |  226 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.  
  33. #define DISK_INT 0x13
  34.  
  35. #define RESET_DISK 0
  36. #define WRITE_SECTOR 3
  37. #define VERIFY_SECTOR 4
  38.  
  39. #define DISK1 0x80
  40.  
  41. /* ----------------------------------------------------------------------- */
  42. /* Copyright notice and version number                                     */
  43. /* ----------------------------------------------------------------------- */
  44.  
  45. void notice (void)
  46. {
  47.     printf ("\nFIPS version 1.1, Copyright (C) 1993/94 Arno Schaefer\n");
  48.     printf ("Module RESTORRB.EXE - Please read the file README.1ST\n");
  49.     printf ("FIPS comes with ABSOLUTELY NO WARRANTY, see file COPYING for details\n");
  50.     printf ("This is free software, and you are welcome to redistribute it\n");
  51.     printf ("under certain conditions; again see file COPYING for details.\n");
  52. }
  53.  
  54. /* ----------------------------------------------------------------------- */
  55. /* Error Handling                                                          */
  56. /* ----------------------------------------------------------------------- */
  57.  
  58. int getx (void)
  59. {
  60.     int character = getch();
  61.  
  62.     if (character == 3)
  63.     {
  64.         printf ("\n");
  65.         exit (0);
  66.     }
  67.     return (character);
  68. }
  69.  
  70. void error (char *message)
  71. {
  72.     fprintf (stderr,"\nError: %s!\n",message);
  73.     exit (-1);
  74. }
  75.  
  76. /* ----------------------------------------------------------------------- */
  77. /* BIOS calls                                                              */
  78. /* ----------------------------------------------------------------------- */
  79.  
  80. int reset_drives (void)
  81. {
  82.     union REGS regs;
  83.  
  84.     regs.h.ah = RESET_DISK;
  85.     regs.h.dl = DISK1;
  86.     int86 (DISK_INT,®s,®s);
  87.     if (regs.x.cflag) return (-1);
  88.     return 0;
  89. }
  90.  
  91. /* ----------------------------------------------------------------------- */
  92. /* read / write sectors                                                    */
  93. /* ----------------------------------------------------------------------- */
  94.  
  95. int verify_sector (int drive_number,dword head,dword cylinder,dword sector,byte *buffer)
  96. {
  97.     if (biosdisk (VERIFY_SECTOR,drive_number,head,cylinder,sector,1,buffer)) return (-1);
  98.     return 0;
  99. }
  100.  
  101. int write_sector (int drive_number,dword head,dword cylinder,dword sector,byte *buffer)
  102. {
  103.     int i;
  104.     boolean done=false;
  105.     for (i=0;i<3;i++)
  106.     {
  107.         if (!biosdisk (WRITE_SECTOR,drive_number,head,cylinder,sector,1,buffer))
  108.         {
  109.             done=true;
  110.             break;
  111.         }
  112.         reset_drives();
  113.     }
  114.     if (!done) return (-1);
  115.     return (verify_sector (drive_number,head,cylinder,sector,buffer));
  116. }
  117.  
  118. int write_root_sector (int drive_number,byte *buffer)
  119. {
  120.     return (write_sector (drive_number,0,0,1,buffer));
  121. }
  122.  
  123. /* ----------------------------------------------------------------------- */
  124. /* User Input                                                              */
  125. /* ----------------------------------------------------------------------- */
  126.  
  127. void ask_for_write_permission (void)
  128. {
  129.     int character = 'x';
  130.  
  131.     printf ("\nReady to write old root- and bootsector to disk\n");
  132.     printf ("Do you want to proceed (y/n): ");
  133.  
  134.     while ((character != 'y') && (character != 'n')) character = getx();
  135.     printf ("%c\n",character);
  136.     if (character == 'n') exit (0);
  137. }
  138.  
  139. /* ----------------------------------------------------------------------- */
  140. /* Main                                                                    */
  141. /* ----------------------------------------------------------------------- */
  142.  
  143. void main (void)
  144. {
  145.     byte rootsector[512];
  146.     byte bootsector[512];
  147.     int drive_number,partition_number,i;
  148.     FILE *handle;
  149.     dword head,cylinder,sector;
  150.     char *filename = "a:\\rootboot.000";
  151.     int no_of_savefiles = 0;
  152.     char first = 'x';
  153.  
  154.     notice();
  155.  
  156.     if (reset_drives ()) error ("Drive Initialization Failure");
  157.  
  158.     for (i='0';i<='9';i++)
  159.     {
  160.         filename[14] = i;
  161.         if (access (filename,0) == 0)
  162.         {
  163.             if (first == 'x') first = i;
  164.             no_of_savefiles++;
  165.             printf ("found savefile %s\n",filename);
  166.         }
  167.     }
  168.  
  169.     if (no_of_savefiles == 0) error ("No savefile ROOTBOOT.00? found on disk A:");
  170.  
  171.     if (no_of_savefiles > 1)
  172.     {
  173.         printf ("\nWhich file do you want to restore? ");
  174.         while (true)
  175.         {
  176.             int c;
  177.             if (isdigit (c = getx()))
  178.             {
  179.                 filename[14] = c;
  180.                 if (access (filename,0) == 0) break;
  181.             }
  182.         }
  183.     }
  184.     else
  185.     {
  186.         filename[14] = first;
  187.     }
  188.  
  189.     if (no_of_savefiles > 1)
  190.     {
  191.         printf ("%c\n", filename[14]);
  192.     }
  193.  
  194.     if ((handle = fopen (filename,"rb")) == NULL)
  195.         error ("Can't open file");
  196.  
  197.     for (i=0;i<512;i++)
  198.     {
  199.         int character = fgetc (handle);
  200.         if (character == EOF) error ("Error reading file from disk");
  201.         *(rootsector + i) = character;
  202.     }
  203.     for (i=0;i<512;i++)
  204.     {
  205.         int character = fgetc (handle);
  206.         if (character == EOF) error ("Error reading file from disk");
  207.         *(bootsector + i) = character;
  208.     }
  209.     if ((drive_number = fgetc (handle)) == EOF) error ("Error reading file from disk");
  210.     if ((partition_number = fgetc (handle)) == EOF) error ("Error reading file from disk");
  211.     if (fclose (handle)) error ("Error closing file");
  212.  
  213.     head = (dword) rootsector[0x1be+16*partition_number+1];
  214.     cylinder = (((dword) rootsector[0x1be+16*partition_number+2] << 2) & 0x300)
  215.         | (dword) rootsector[0x1be+16*partition_number+3];
  216.     sector = (dword) rootsector[0x1be+16*partition_number+2] & 0x3f;
  217.  
  218.     ask_for_write_permission();
  219.  
  220.     if (write_root_sector (drive_number,rootsector))
  221.         error ("Error writing rootsector");
  222.  
  223.     if (write_sector (drive_number,head,cylinder,sector,bootsector))
  224.         error ("Error writing bootsector");
  225. }
  226.