HDFIXER.C
上传用户:yugezi
上传日期:2007-01-06
资源大小:18k
文件大小:8k
源码类别:

磁盘编程

开发平台:

Others

  1. #define Us unsigned
  2. #define Uc Us char
  3. #define Ul Us long
  4. #include<dos.h>
  5. #include<bios.h>
  6. #include<conio.h>
  7. #include<stdlib.h>
  8. #include<stdio.h>
  9. #include<string.h>
  10. Uc Sector1[512]=
  11.       { 0xFA,0x33,0xC0,0x8E,0xD0,0xBC,0x00,0x7C,0x8B,0xF4,0x50,0x07,
  12. 0x50,0x1F,0xFB,0xFC,0xBF,0x00,0x06,0xB9,0x00,0x01,0xF2,0xA5,
  13. 0xEA,0x1D,0x06,0x00,0x00,0xBE,0xBE,0x07,0xB3,0x04,0x80,0x3C,
  14. 0x80,0x74,0x0E,0x80,0x3C,0x00,0x75,0x1C,0x83,0xC6,0x10,0xFE,
  15. 0xCB,0x75,0xEF,0xCD,0x18,0x8B,0x14,0x8B,0x4C,0x02,0x8B,0xEE,
  16. 0x83,0xC6,0x10,0xFE,0xCB,0x74,0x1A,0x80,0x3C,0x00,0x74,0xF4,
  17. 0xBE,0x8B,0x06,0xAC,0x3C,0x00,0x74,0x0B,0x56,0xBB,0x07,0x00,
  18. 0xB4,0x0E,0xCD,0x10,0x5E,0xEB,0xF0,0xEB,0xFE,0xBF,0x05,0x00,
  19. 0xBB,0x00,0x7C,0xB8,0x01,0x02,0x57,0xCD,0x13,0x5F,0x75,0x0C,
  20. 0x33,0xC0,0xCD,0x13,0x4F,0x75,0xED,0xBE,0xA3,0x06,0xEB,0xD3,
  21. 0xBE,0xC2,0x06,0xBF,0xFE,0x7D,0x81,0x3D,0x55,0xAA,0x75,0xC7,
  22. 0x8B,0xF5,0xEA,0x00,0x7C,0x00,0x00,0x49,0x6E,0x76,0x61,0x6C,
  23. 0x69,0x64,0x20,0x70,0x61,0x72,0x74,0x69,0x74,0x69,0x6F,0x6E,
  24. 0x20,0x74,0x61,0x62,0x6C,0x65,0x00,0x45,0x72,0x72,0x6F,0x72,
  25. 0x20,0x6C,0x6F,0x61,0x64,0x69,0x6E,0x67,0x20,0x6F,0x70,0x65,
  26. 0x72,0x61,0x74,0x69,0x6E,0x67,0x20,0x73,0x79,0x73,0x74,0x65,
  27. 0x6D,0x00,0x4D,0x69,0x73,0x73,0x69,0x6E,0x67,0x20,0x6F,0x70,
  28. 0x65,0x72,0x61,0x74,0x69,0x6E,0x67,0x20,0x73,0x79,0x73,0x74,
  29. 0x65,0x6D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  32. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  33. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  34. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  35. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  36. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48. 0x00,0x00,0x80,0x01,0x01,0x00,0x04,0x04,0xD1,0x02,0x11,0x00,
  49. 0x00,0x00,0xEE,0xFF,0x00,0x00,0x00,0x00,0xC1,0x03,0x05,0x04,
  50. 0xD1,0xFE,0xFF,0xFF,0x00,0x00,0xAC,0x53,0x00,0x00,0x00,0x00,
  51. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53. 0x00,0x00,0x00,0x00,0x00,0x00,0x55,0xAA};
  54. Us Cyl,Head,Sct,drv=0x80,N;
  55. FILE *fp;
  56. struct HSC
  57. {Uc hd;
  58.  Uc sc;
  59.  Uc cy;
  60. }last;
  61. struct PartitionTable
  62. {
  63.  Uc Flg;
  64.  struct HSC bgn;
  65.  Uc Tp;
  66.  struct HSC end;
  67.  Ul id,sz;
  68. }pt[16][2];
  69. struct DiskBPB
  70. {
  71.  Us bytespersect;
  72.  Uc sectperclust;
  73.  Us reservedsect;
  74.  Uc numberofFAT;
  75.  Us itemsofroot;
  76.  Us totalsect;
  77.  Uc media;
  78.  Us sectperFAT;
  79.  Us sectperCyl;
  80.  Us Head;
  81.  Us hiddensect;
  82. }db[16];
  83. long HSCtold(Us,Us,Us);
  84. long hsctoid(struct HSC *s);
  85. struct HSC ldtoHSC(long);
  86. void getenddata();
  87. void searchBOOT(int);
  88. void getbiosHSC(int);
  89. void showPTable();
  90. void showBPB(int);
  91. void copyright();
  92. void main()
  93. {
  94.  int i;
  95.  char ft;
  96.  Us *mb,*cp;
  97.  clrscr();
  98.  copyright();
  99.  getbiosHSC(0x80);
  100.  searchBOOT(0x80);
  101.  showPTable();
  102.  printf("n");
  103.  getbiosHSC(0x81);
  104.  searchBOOT(0x81);
  105.  getch();
  106.  showPTable();
  107. }
  108. void getbiosHSC(int drv)
  109. {
  110.  Uc bff[8];
  111.  if(biosdisk(8,drv,1,1,1,1,bff)){printf("an The No.%d Physical Hard Disk doesn't exist or Read error!n",(drv-127));exit(1);}
  112.  Head=bff[3]+1;
  113.  Sct=bff[0]%64;
  114.  Cyl=bff[0]/64*256+bff[1]+2;
  115. }
  116. void searchBOOT(int drv)
  117. {
  118.  Us cyl,i=0;
  119.  char bff[512],dos[9];
  120.  if(biosdisk(2,drv,1,0,1,1,bff)){printf("an Searching BOOT error!n");exit(1);}
  121.  printf("n Fixing the No.%d Physical Hard Disk... n",(drv-127));
  122.  printf("n CylNum head sector cylinder n");
  123.  for(cyl=0;cyl<Cyl;cyl++)
  124.  {
  125.   biosdisk(2,drv,1,cyl,1,1,bff);
  126.   if((Us)bff[510]==0x55&&(Uc)bff[511]==0xaa)
  127.   {
  128.    db[i]=*(struct DiskBPB *)(bff+11);
  129.    printf("%7d",cyl);
  130.    pt[i][0].bgn=ldtoHSC(HSCtold(1,1,cyl));
  131.    printf("  %3x %6x %8xn",pt[i][0].bgn.hd,pt[i][0].bgn.sc,pt[i][0].bgn.cy);
  132.    i++;
  133.   }
  134.  }
  135.  N=i;
  136.  showBPB(N);
  137. }
  138. void showPTable()
  139. {
  140.  int i,j,k;
  141.  char ch;
  142.  getenddata();
  143.  printf("n Cyl=%d  Head=%d  Sect=%d  Partiton=%dn",Cyl,Head,Sct,N);
  144.  printf("n Partition Table Data:n");
  145.  for(i=0;i<N;i++)
  146.  {
  147.   for(j=0;j<2;j++)
  148.      if(j==0||i<N-1)
  149.      {
  150.       printf(" ");
  151.       for(k=0;k<16;k++)printf("%2x ",((Uc *)&pt[i][j])[k]);
  152.       if(j==0)printf(" Sector=%6lu Size=%3u MB",(pt[i][j].sz+1),(pt[i][j].sz+1024)/2048);
  153.       printf("n");
  154.      }
  155.      if(i<N-1)printf("n");
  156.  }
  157.  printf("n Are you sure this is the right Partition Info?(Y/N)");
  158.  ch=getche();
  159.  if(ch=='Y'||ch=='y')
  160.  {
  161.   for(i=0;i<32;i++)
  162.   {
  163.    Sector1[0x1BE+i]=((Uc *)&pt[0][0])[i];
  164.   }
  165.   if(biosdisk(0x03,0x80,0,0,1,1,Sector1))
  166.   {printf("na Fixed Hard Disk Partition error!n");exit(0);}
  167.   else printf("n Fixed Hard Disk Partition ok.");
  168.  }
  169. }
  170. void getenddata()
  171. {
  172.  struct HSC hsc;
  173.  long id1,id0,ext;
  174.  int i,j;
  175.  last=ldtoHSC(HSCtold(Head-1,Sct,Cyl-2));
  176.  for(i=0;i<N;i++)
  177.  {
  178.   id0=HSCtold(pt[i][0].bgn.hd-1,pt[i][0].bgn.sc,pt[i][0].bgn.cy);
  179.   if(i<N-1)
  180.   {
  181.    id1=HSCtold(pt[i+1][0].bgn.hd-1,pt[i+1][0].bgn.sc,pt[i+1][0].bgn.cy);
  182.    pt[i][0].end=ldtoHSC(id1-1);
  183.   }
  184.   else{
  185.        id1=HSCtold(Head-1,Sct,Cyl-2)+1;
  186.        pt[i][0].end=last;
  187.       }
  188.   if(i==0)pt[i][0].Flg=0x80;else pt[i][0].Flg=0;
  189.   pt[i][0].id=Sct;pt[i][0].sz=id1-id0-Sct;
  190.   if(pt[i][0].sz>65535)pt[i][0].Tp=6;
  191.   else if((Ul)db[i].bytespersect*db[i].sectperFAT*db[i].sectperclust>=2*((Ul)pt[i][0].sz-1-(Ul)db[i].numberofFAT*db[i].sectperFAT-(Ul)db[i].itemsofroot*32/db[i].bytespersect)+4)
  192.   pt[i][0].Tp=4;
  193.        else pt[i][0].Tp=1;
  194.  }
  195.  ext=pt[0][0].sz+pt[0][0].id;
  196.  for(i=0;i<N-1;i++)
  197.  {
  198.   pt[i][1].Tp=5;
  199.   pt[i][1].bgn=pt[i+1][0].bgn;
  200.   pt[i][1].bgn.hd=pt[i+1][0].bgn.hd-1;
  201.   if(i==0)pt[i][1].end=last;
  202.   else pt[i][1].end=pt[i+1][0].end;
  203.   if(i==0)pt[i][1].id=ext;
  204.   else pt[i][1].id=hsctoid((struct HSC *)&(pt[i+1][0].bgn))-ext-Sct;
  205.   if(i==0)pt[i][1].sz=hsctoid(&last)-hsctoid((struct HSC *)&(pt[0][0].end));
  206.   else pt[i][1].sz=hsctoid((struct HSC *)&(pt[i][1].end))-hsctoid((struct HSC *)&(pt[i][0].end));
  207.  }
  208. }
  209. long hsctoid(struct HSC *s)
  210. {
  211.  return HSCtold(s->hd,s->sc,s->cy);
  212. }
  213. long HSCtold(Us hd,Us sct,Us cyl)
  214. {
  215.  long id;
  216.  Ul cylinder,sector;
  217.  cylinder=cyl+sct/64*256;sector=sct%64-1;
  218.  id=(long)cylinder*Head*Sct+hd*Sct+sector;
  219.  return id;
  220. }
  221. struct HSC ldtoHSC(long id)
  222. {
  223.  struct HSC hsc;
  224.  Us r,cyl;
  225.  cyl=id/((long)Head*Sct);hsc.cy=cyl%256;
  226.  r=id%((long)Head*Sct);hsc.hd=r/Sct;
  227.  hsc.sc=r%Sct+1+cyl/256*64;
  228.  return hsc;
  229. }
  230. void showBPB(int n)
  231. {
  232.  int i;
  233.  printf("n Partition number :  ");
  234.  for(i=0;i<n;i++)printf("%4d",i+1);
  235.  printf("n Sectors per cluster :");
  236.  for(i=0;i<n;i++)printf("%4u",db[i].sectperclust);
  237.  printf("n Sectors per FAT :     ");
  238.  for(i=0;i<n;i++)printf("%4u",db[i].sectperFAT);
  239.  printf("n Number of FAT :     ");
  240.  for(i=0;i<n;i++)printf("%4u",db[i].numberofFAT);
  241.  printf("n Items of root Dir :   ");
  242.  for(i=0;i<n;i++)printf("%4u",db[i].itemsofroot);
  243.  printf("n Hidden sectors :     ");
  244.  for(i=0;i<n;i++)printf("%4u",db[i].hiddensect);
  245.  printf("n Reserved sectors :  ");
  246.  for(i=0;i<n;i++)printf("%4u",db[i].reservedsect);
  247.  printf("n");
  248. }
  249. void copyright()
  250. {
  251.   printf("n Babysoft  (R) Hard Disk Partition Fixer version 1.0");
  252.   printf("n Copyright (C) 1997.12 LiuYaDing,Babysoft Corp.n");
  253. }