home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / misc / check.cpp < prev    next >
C/C++ Source or Header  |  1993-11-17  |  8KB  |  213 lines

  1. /*
  2.     FIPS - the First nondestructive Interactive Partition Splitting program
  3.  
  4.     Module check.cpp
  5.  
  6.     RCS - Header:
  7.     $Header: c:/daten/c/fips/source/cpp/RCS/check.cpp 0.9.1.1 1993/11/17 17:51:02 schaefer Exp schaefer $
  8.  
  9.     Copyright (C) 1993 Arno Schaefer
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25.  
  26.     Report problems and direct all questions to:
  27.  
  28.     schaefer@rbg.informatik.th-darmstadt.de
  29. */
  30.  
  31. #include "hdstruct.h"
  32. #include "global.h"
  33. #include "fipsspec.h"
  34.  
  35. void fips_harddrive::check (void)
  36. {
  37.     int i,j,k;
  38.     boolean bootable = false;
  39.  
  40.     byte *rootsector = harddrive::rootsector->data;
  41.     partition_info parts[4] = partition_table().partition_info;
  42.  
  43.     int order[4] = {-1,-1,-1,-1};
  44.  
  45.     if (!global.quiet_mode) printx ("\nChecking Rootsector ... ");
  46.  
  47.     if ((*(rootsector+510) != 0x55) || (*(rootsector+511) != 0xaa))
  48.         error ("Invalid Rootsector Signature: %02X %02X",*(rootsector+510),*(rootsector+511));
  49.  
  50.     for (i=0;i<4;i++)
  51.     {
  52.         if (parts[i].bootable == 0x80)
  53.         {
  54.             if (bootable)
  55.             {
  56.                 if (!global.override_multiple_boot)
  57.                     error ("More than one bootable Partition");
  58.             }
  59.             else bootable = true;
  60.         }
  61.         else if (parts[i].bootable != 0)
  62.             if (!global.override_bootable_flag)
  63.                 error ("Invalid bootable-flag: partition %u: %02Xh",i+1,parts[i].bootable);
  64.             // must be 0 or 80h
  65.  
  66.         if (parts[i].system)
  67.         {
  68.             if ((parts[i].start_sector == 0) || (parts[i].start_sector > geometry.sectors))
  69.                 error ("Invalid Start Sector: partition %u: %u",i+1,parts[i].start_sector);
  70.             if ((parts[i].end_sector == 0) || (parts[i].end_sector > geometry.sectors))
  71.                 error ("Invalid End Sector: partition %u: %u",i+1,parts[i].end_sector);
  72.             if (parts[i].start_head > (geometry.heads - 1))
  73.                 error ("Invalid Start Head: partition %u: %u",i+1,parts[i].start_head);
  74.             if (parts[i].end_head > (geometry.heads - 1))
  75.                 error ("Invalid End Head: partition %u: %u",i+1,parts[i].end_head);
  76.  
  77.             if (parts[i].start_sector_abs != (parts[i].start_cylinder * geometry.heads * geometry.sectors +
  78.                 parts[i].start_head * geometry.sectors + parts[i].start_sector - 1))
  79.                 error ("Partition Table Corrupt - start: partition %u",i+1);
  80.                 // physical start sector does not match logical start sector
  81.  
  82.             if ((parts[i].start_sector_abs + parts[i].no_of_sectors_abs - 1) !=
  83.                 (parts[i].end_cylinder * geometry.heads * geometry.sectors +
  84.                 parts[i].end_head * geometry.sectors + parts[i].end_sector - 1))
  85.                 error ("Partition Table Corrupt - end: partition %u",i+1);
  86.                 // physical end sector does not match logical end sector
  87.  
  88.             for (j=0;j<4;j++)       // insert partition in ordered table
  89.             {
  90.                 if (order[j] == -1)
  91.                 {
  92.                     order[j] = i;
  93.                     break;
  94.                 }
  95.                 else if (parts[i].start_sector_abs < parts[order[j]].start_sector_abs)
  96.                 {
  97.                     for (k=3;k>j;k--) order[k] = order[k-1];
  98.                     order[j] = i;
  99.                     break;
  100.                 }
  101.             }
  102.         }
  103.         else            // system = 0
  104.         {
  105.             for (j=0;j<16;j++)
  106.             {
  107.                 if (*(rootsector + 0x1be + 16*i + j))
  108.                 {
  109.                     warning ("Invalid Partition entry: partition %u",i+1);
  110.                     break;
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     for (i=0;i<4;i++)
  116.     {
  117.         if ((k=order[i]) != -1)         // valid partition
  118.         {
  119.             if ((parts[k].end_sector != geometry.sectors) || (parts[k].end_head != (geometry.heads - 1)))
  120.                 warning ("Partition does not end on Cylinder boundary: partition %u",k+1);
  121.             if (i != 0) if ((parts[k].start_sector != 1) || (parts[k].start_head != 0))
  122.                 warning ("Partition does not begin on Cylinder boundary: partition %u",k+1);
  123.  
  124.             if (i<3) if ((j=order[i+1]) != -1)       // following valid partition
  125.             {
  126.                 if ((parts[k].start_sector_abs + parts[k].no_of_sectors_abs) > parts[j].start_sector_abs)
  127.                     error ("Overlapping Partitions: %u and %u",k+1,j+1);
  128.                 if ((parts[k].start_sector_abs + parts[k].no_of_sectors_abs) < parts[j].start_sector_abs)
  129.                     warning ("Free Space between Partitions: %u and %u",k+1,j+1);
  130.             }
  131.         }
  132.     }
  133.     if (!global.quiet_mode) printx ("OK\n");
  134. }
  135.  
  136. void fips_partition::check (void)
  137. {
  138.     if (!global.quiet_mode) printx ("Checking Bootsector ... ");
  139.  
  140.     byte *bootsector = partition::bootsector->data;
  141.  
  142.     if (*(bootsector) == 0xeb)
  143.     {
  144.         if (*(bootsector + 2) != 0x90)
  145.             error ("Invalid Jump Instruction in Bootsector: %02X %02X %02X",*(bootsector),*(bootsector+1),*(bootsector+2));
  146.     }
  147.     else if (*(bootsector) != 0xe9)
  148.         error ("Invalid Jump Instruction in Bootsector: %02X %02X %02X",*(bootsector),*(bootsector+1),*(bootsector+2));
  149.  
  150.     if ((*(bootsector+510) != 0x55) || (*(bootsector+511) != 0xaa))
  151.         error ("Invalid Bootsector: %02X %02X",*(bootsector+510),*(bootsector+511));
  152.     if (bpb().bytes_per_sector != 512)
  153.         error ("Can't handle number of Bytes per Sector: %u",bpb().bytes_per_sector);
  154.     switch (bpb().sectors_per_cluster)
  155.     {
  156.         case 1:case 2:case 4:case 8:case 16:case 32:case 64:case 128: break;
  157.         default:
  158.             error ("Number of Sectors per Cluster must be a power of 2: actually it is %u",bpb().sectors_per_cluster);
  159.     }
  160.     if (bpb().reserved_sectors != 1)
  161.         warning ("Number of reserved sectors should be 1: actually it is %u",bpb().reserved_sectors);
  162.     if (bpb().no_of_fats != 2)
  163.         error ("Partition must have 2 FATs: actually it is %u",bpb().no_of_fats);
  164.     if (bpb().no_of_rootdir_entries % 16)
  165.         if (!global.override_rootdir_entries)
  166.             error ("Number of Rootdir entries must be multiple of 16: actually it is %u",bpb().no_of_rootdir_entries);
  167.     if (bpb().no_of_rootdir_entries == 0)
  168.         error ("Number of Rootdir entries must not be zero");
  169.     if (bpb().media_descriptor != 0xf8)
  170.         if (!global.override_media_descriptor)
  171.             error ("Wrong Media Descriptor Byte in Bootsector: %02X",bpb().media_descriptor);
  172.     if (bpb().sectors_per_fat > 256)
  173.         if (!global.override_large_fat)
  174.             error ("FAT too large: %u sectors",bpb().sectors_per_fat);
  175.     if (bpb().sectors_per_fat < (info().no_of_clusters + 1) / 256 + 1)
  176.         if (!global.override_small_fat)
  177.             error ("FAT too small: %u sectors (should be %u)",bpb().sectors_per_fat,(int) (info().no_of_clusters + 1) / 256 + 1);
  178.     if (bpb().sectors_per_track != drive->geometry.sectors)
  179.         warning ("Sectors per track incorrect: %u instead of %u",bpb().sectors_per_track,(int) drive->geometry.sectors);
  180.     if (bpb().drive_heads != drive->geometry.heads)
  181.         warning ("Number of drive heads incorrect: %u instead of %u",bpb().drive_heads,(int) drive->geometry.heads);
  182.     if (bpb().hidden_sectors != partition_info->start_sector_abs)
  183.         error ("Number of hidden sectors incorrect: %lu instead of %lu",bpb().hidden_sectors,partition_info->start_sector_abs);
  184.  
  185.     if (info().no_of_clusters <= 4084)
  186.         error ("12-bit FAT not supported: number of clusters is %u",(int) info().no_of_clusters);
  187.  
  188.     if (bpb().no_of_sectors)
  189.     {
  190.         if (partition_info->no_of_sectors_abs > 0xffff)
  191.             error ("Number of sectors (short) must be zero");
  192.         if (bpb().no_of_sectors != partition_info->no_of_sectors_abs)
  193.             error ("Number of sectors (short) does not match Partition Info:\n%u instead of %lu",bpb().no_of_sectors,partition_info->no_of_sectors_abs);
  194.         if (partition_info->system != 4)
  195.             warning ("Wrong System Indicator Byte: %u instead of 4",partition_info->system);
  196.     }
  197.     else
  198.     {
  199.         if (bpb().no_of_sectors_long != partition_info->no_of_sectors_abs)
  200.             error ("Number of Sectors (long) does not match Partition Info:\n%lu instead of %lu",bpb().no_of_sectors_long,partition_info->no_of_sectors_abs);
  201.         if (bpb().signature != 0x29)
  202.             warning ("Wrong Signature: %02Xh",bpb().signature);
  203.         if (bpb().phys_drive_no != drive->number)
  204.             warning ("Drive number in bootsector does not match actual drivenumber:\n%02Xh instead of %02Xh"
  205.             ,bpb().phys_drive_no,drive->number);
  206.         if (partition_info->system != 6)
  207.             warning ("Wrong System Indicator Byte: %u instead of 6",partition_info->system);
  208.     }
  209.  
  210.     if (!global.quiet_mode) printx ("OK\n");
  211. }
  212.  
  213.