home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 8 / IOPROG_8.ISO / install / fips / restorrb / restorrb.c next >
Encoding:
C/C++ Source or Header  |  1997-08-25  |  6.9 KB  |  240 lines

  1. /*
  2.  
  3.     FIPS - the First nondestructive Interactive Partition Splitting program
  4.  
  5.     Module restorrb.c
  6.  
  7.  
  8.  
  9.     Copyright (C) 1993 Arno Schaefer
  10.  
  11.  
  12.  
  13.     This program is free software; you can redistribute it and/or modify
  14.  
  15.     it under the terms of the GNU General Public License as published by
  16.  
  17.     the Free Software Foundation; either version 1, or (at your option)
  18.  
  19.     any later version.
  20.  
  21.  
  22.  
  23.     This program is distributed in the hope that it will be useful,
  24.  
  25.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.  
  27.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28.  
  29.     GNU General Public License for more details.
  30.  
  31.  
  32.  
  33.     You should have received a copy of the GNU General Public License
  34.  
  35.     along with this program; if not, write to the Free Software
  36.  
  37.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  38.  
  39. */
  40.  
  41.  
  42.  
  43. #include <stdio.h>
  44.  
  45. #include <io.h>
  46.  
  47. #include <stdlib.h>
  48.  
  49. #include <dos.h>
  50.  
  51. #include <bios.h>
  52.  
  53. #include <alloc.h>
  54.  
  55. #include <conio.h>
  56.  
  57. #include <ctype.h>
  58.  
  59.  
  60.  
  61. #include "rtypes.h"
  62.  
  63. #include "rversion.h"
  64.  
  65.  
  66.  
  67. #define DISK_INT 0x13
  68.  
  69.  
  70.  
  71. #define RESET_DISK 0
  72.  
  73. #define WRITE_SECTOR 3
  74.  
  75. #define VERIFY_SECTOR 4
  76.  
  77.  
  78.  
  79. #define DISK1 0x80
  80.  
  81.  
  82.  
  83. /* ----------------------------------------------------------------------- */
  84.  
  85. /* Copyright notice and version number                                     */
  86.  
  87. /* ----------------------------------------------------------------------- */
  88.  
  89.  
  90.  
  91. void notice (void)
  92.  
  93. {
  94.  
  95.     printf ("\nFIPS version " FIPS_VERSION ", Copyright (C) 1993/94 Arno Schaefer\n");
  96.  
  97.     printf ("Module RESTORRB.EXE - Please read the file README.1ST\n");
  98.  
  99.     printf ("FIPS comes with ABSOLUTELY NO WARRANTY, see file COPYING for details\n");
  100.  
  101.     printf ("This is free software, and you are welcome to redistribute it\n");
  102.  
  103.     printf ("under certain conditions; again see file COPYING for details.\n\n");
  104.  
  105. }
  106.  
  107.  
  108.  
  109. /* ----------------------------------------------------------------------- */
  110.  
  111. /* Error Handling                                                          */
  112.  
  113. /* ----------------------------------------------------------------------- */
  114.  
  115.  
  116.  
  117. int getx (void)
  118.  
  119. {
  120.  
  121.     int character = getch();
  122.  
  123.  
  124.  
  125.     if (character == 3)
  126.  
  127.     {
  128.  
  129.         printf ("\n");
  130.  
  131.         exit (0);
  132.  
  133.     }
  134.  
  135.     return (character);
  136.  
  137. }
  138.  
  139.  
  140.  
  141. void error (char *message)
  142.  
  143. {
  144.  
  145.     fprintf (stderr,"\nError: %s!\n",message);
  146.  
  147.     exit (-1);
  148.  
  149. }
  150.  
  151.  
  152.  
  153. /* ----------------------------------------------------------------------- */
  154.  
  155. /* BIOS calls                                                              */
  156.  
  157. /* ----------------------------------------------------------------------- */
  158.  
  159.  
  160.  
  161. int reset_drives (void)
  162.  
  163. {
  164.  
  165.     union REGS regs;
  166.  
  167.  
  168.  
  169.     regs.h.ah = RESET_DISK;
  170.  
  171.     regs.h.dl = DISK1;
  172.  
  173.     int86 (DISK_INT,®s,®s);
  174.  
  175.     if (regs.x.cflag) return (-1);
  176.  
  177.     return 0;
  178.  
  179. }
  180.  
  181.  
  182.  
  183. /* ----------------------------------------------------------------------- */
  184.  
  185. /* read / write sectors                                                    */
  186.  
  187. /* ----------------------------------------------------------------------- */
  188.  
  189.  
  190.  
  191. int verify_sector (int drive_number,dword head,dword cylinder,dword sector,byte *buffer)
  192.  
  193. {
  194.  
  195.     if (biosdisk (VERIFY_SECTOR,drive_number,head,cylinder,sector,1,buffer)) return (-1);
  196.  
  197.     return 0;
  198.  
  199. }
  200.  
  201.  
  202.  
  203. int write_sector (int drive_number,dword head,dword cylinder,dword sector,byte *buffer)
  204.  
  205. {
  206.  
  207.     int i;
  208.  
  209.     boolean done=false;
  210.  
  211.     for (i=0;i<3;i++)
  212.  
  213.     {
  214.  
  215.         if (!biosdisk (WRITE_SECTOR,drive_number,head,cylinder,sector,1,buffer))
  216.  
  217.         {
  218.  
  219.             done=true;
  220.  
  221.             break;
  222.  
  223.         }
  224.  
  225.         reset_drives();
  226.  
  227.     }
  228.  
  229.     if (!done) return (-1);
  230.  
  231.     return (verify_sector (drive_number,head,cylinder,sector,buffer));
  232.  
  233. }
  234.  
  235.  
  236.  
  237. int write_root_sector (int drive_number,byte *buffer)
  238.  
  239. {
  240.  
  241.     return (write_sector (drive_number,0,0,1,buffer));
  242.  
  243. }
  244.  
  245.  
  246.  
  247. /* ----------------------------------------------------------------------- */
  248.  
  249. /* User Input                                                              */
  250.  
  251. /* ----------------------------------------------------------------------- */
  252.  
  253.  
  254.  
  255. void ask_for_write_permission (char *filename)
  256.  
  257. {
  258.  
  259.     int character = 'x';
  260.  
  261.  
  262.  
  263.     printf ("\nReady to write old root- and bootsector from file %s to disk\n", filename);
  264.  
  265.     printf ("Do you want to proceed (y/n): ");
  266.  
  267.  
  268.  
  269.     while ((character != 'y') && (character != 'n')) character = getx();
  270.  
  271.     printf ("%c\n",character);
  272.  
  273.     if (character == 'n') exit (0);
  274.  
  275. }
  276.  
  277.  
  278.  
  279. /* ----------------------------------------------------------------------- */
  280.  
  281. /* Main                                                                    */
  282.  
  283. /* ----------------------------------------------------------------------- */
  284.  
  285.  
  286.  
  287. void main (void)
  288.  
  289. {
  290.  
  291.     byte rootsector[512];
  292.  
  293.     byte bootsector[512];
  294.  
  295.     int drive_number,partition_number,i;
  296.  
  297.     FILE *handle;
  298.  
  299.     dword head,cylinder,sector;
  300.  
  301.     char *filename = "a:\\rootboot.000";
  302.  
  303.     int no_of_savefiles = 0;
  304.  
  305.     char first = 'x';
  306.  
  307.     char list[10];
  308.  
  309.  
  310.  
  311.     notice();
  312.  
  313.  
  314.  
  315.     if (reset_drives ()) error ("Drive Initialization Failure");
  316.  
  317.  
  318.  
  319.     for (i='0';i<='9';i++)
  320.  
  321.     {
  322.  
  323.         filename[14] = i;
  324.  
  325.         if (access (filename,0) == 0)
  326.  
  327.         {
  328.  
  329.             if (first == 'x') first = i;
  330.  
  331.             list[no_of_savefiles++] = i;
  332.  
  333.             printf ("Found save file %s\n",filename);
  334.  
  335.         }
  336.  
  337.     }
  338.  
  339.  
  340.  
  341.     if (no_of_savefiles == 0) error ("No savefile ROOTBOOT.00? found on disk A:");
  342.  
  343.  
  344.  
  345.     if (no_of_savefiles > 1)
  346.  
  347.     {
  348.  
  349.         printf ("\nWhich file do you want to restore (");
  350.  
  351.         for (i = 0; i < no_of_savefiles; i++)
  352.  
  353.         {
  354.  
  355.             printf ("%c/", list[i]);
  356.  
  357.         }
  358.  
  359.         printf ("\b)? ");
  360.  
  361.  
  362.  
  363.         while (true)
  364.  
  365.         {
  366.  
  367.             int c;
  368.  
  369.             if (isdigit (c = getx()))
  370.  
  371.             {
  372.  
  373.                 boolean found = false;
  374.  
  375.  
  376.  
  377.                 for (i = 0; i < no_of_savefiles; i++)
  378.  
  379.                 {
  380.  
  381.                     if (c == list[i]) found = true;
  382.  
  383.                 }
  384.  
  385.  
  386.  
  387.                 if (found)
  388.  
  389.                 {
  390.  
  391.                     printf ("%c\n", c);
  392.  
  393.                     filename[14] = c;
  394.  
  395.                     break;
  396.  
  397.                 }
  398.  
  399.             }
  400.  
  401.         }
  402.  
  403.     }
  404.  
  405.     else
  406.  
  407.     {
  408.  
  409.         filename[14] = first;
  410.  
  411.     }
  412.  
  413.  
  414.  
  415.     if ((handle = fopen (filename,"rb")) == NULL)
  416.  
  417.         error ("Can't open file");
  418.  
  419.  
  420.  
  421.     for (i=0;i<512;i++)
  422.  
  423.     {
  424.  
  425.         int character = fgetc (handle);
  426.  
  427.         if (character == EOF) error ("Error reading file from disk");
  428.  
  429.         *(rootsector + i) = character;
  430.  
  431.     }
  432.  
  433.     for (i=0;i<512;i++)
  434.  
  435.     {
  436.  
  437.         int character = fgetc (handle);
  438.  
  439.         if (character == EOF) error ("Error reading file from disk");
  440.  
  441.         *(bootsector + i) = character;
  442.  
  443.     }
  444.  
  445.     if ((drive_number = fgetc (handle)) == EOF) error ("Error reading file from disk");
  446.  
  447.     if ((partition_number = fgetc (handle)) == EOF) error ("Error reading file from disk");
  448.  
  449.     if (fclose (handle)) error ("Error closing file");
  450.  
  451.  
  452.  
  453.     head = (dword) rootsector[0x1be+16*partition_number+1];
  454.  
  455.     cylinder = (((dword) rootsector[0x1be+16*partition_number+2] << 2) & 0x300)
  456.  
  457.         | (dword) rootsector[0x1be+16*partition_number+3];
  458.  
  459.     sector = (dword) rootsector[0x1be+16*partition_number+2] & 0x3f;
  460.  
  461.  
  462.  
  463.     ask_for_write_permission(filename);
  464.  
  465.  
  466.  
  467.     if (write_root_sector (drive_number,rootsector))
  468.  
  469.         error ("Error writing rootsector");
  470.  
  471.  
  472.  
  473.     if (write_sector (drive_number,head,cylinder,sector,bootsector))
  474.  
  475.         error ("Error writing bootsector");
  476.  
  477. }
  478.  
  479.