HDFIXER.C
资源名称:schdgurd.zip [点击查看]
上传用户:yugezi
上传日期:2007-01-06
资源大小:18k
文件大小:8k
源码类别:
磁盘编程
开发平台:
Others
- #define Us unsigned
- #define Uc Us char
- #define Ul Us long
- #include<dos.h>
- #include<bios.h>
- #include<conio.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- Uc Sector1[512]=
- { 0xFA,0x33,0xC0,0x8E,0xD0,0xBC,0x00,0x7C,0x8B,0xF4,0x50,0x07,
- 0x50,0x1F,0xFB,0xFC,0xBF,0x00,0x06,0xB9,0x00,0x01,0xF2,0xA5,
- 0xEA,0x1D,0x06,0x00,0x00,0xBE,0xBE,0x07,0xB3,0x04,0x80,0x3C,
- 0x80,0x74,0x0E,0x80,0x3C,0x00,0x75,0x1C,0x83,0xC6,0x10,0xFE,
- 0xCB,0x75,0xEF,0xCD,0x18,0x8B,0x14,0x8B,0x4C,0x02,0x8B,0xEE,
- 0x83,0xC6,0x10,0xFE,0xCB,0x74,0x1A,0x80,0x3C,0x00,0x74,0xF4,
- 0xBE,0x8B,0x06,0xAC,0x3C,0x00,0x74,0x0B,0x56,0xBB,0x07,0x00,
- 0xB4,0x0E,0xCD,0x10,0x5E,0xEB,0xF0,0xEB,0xFE,0xBF,0x05,0x00,
- 0xBB,0x00,0x7C,0xB8,0x01,0x02,0x57,0xCD,0x13,0x5F,0x75,0x0C,
- 0x33,0xC0,0xCD,0x13,0x4F,0x75,0xED,0xBE,0xA3,0x06,0xEB,0xD3,
- 0xBE,0xC2,0x06,0xBF,0xFE,0x7D,0x81,0x3D,0x55,0xAA,0x75,0xC7,
- 0x8B,0xF5,0xEA,0x00,0x7C,0x00,0x00,0x49,0x6E,0x76,0x61,0x6C,
- 0x69,0x64,0x20,0x70,0x61,0x72,0x74,0x69,0x74,0x69,0x6F,0x6E,
- 0x20,0x74,0x61,0x62,0x6C,0x65,0x00,0x45,0x72,0x72,0x6F,0x72,
- 0x20,0x6C,0x6F,0x61,0x64,0x69,0x6E,0x67,0x20,0x6F,0x70,0x65,
- 0x72,0x61,0x74,0x69,0x6E,0x67,0x20,0x73,0x79,0x73,0x74,0x65,
- 0x6D,0x00,0x4D,0x69,0x73,0x73,0x69,0x6E,0x67,0x20,0x6F,0x70,
- 0x65,0x72,0x61,0x74,0x69,0x6E,0x67,0x20,0x73,0x79,0x73,0x74,
- 0x65,0x6D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x80,0x01,0x01,0x00,0x04,0x04,0xD1,0x02,0x11,0x00,
- 0x00,0x00,0xEE,0xFF,0x00,0x00,0x00,0x00,0xC1,0x03,0x05,0x04,
- 0xD1,0xFE,0xFF,0xFF,0x00,0x00,0xAC,0x53,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x55,0xAA};
- Us Cyl,Head,Sct,drv=0x80,N;
- FILE *fp;
- struct HSC
- {Uc hd;
- Uc sc;
- Uc cy;
- }last;
- struct PartitionTable
- {
- Uc Flg;
- struct HSC bgn;
- Uc Tp;
- struct HSC end;
- Ul id,sz;
- }pt[16][2];
- struct DiskBPB
- {
- Us bytespersect;
- Uc sectperclust;
- Us reservedsect;
- Uc numberofFAT;
- Us itemsofroot;
- Us totalsect;
- Uc media;
- Us sectperFAT;
- Us sectperCyl;
- Us Head;
- Us hiddensect;
- }db[16];
- long HSCtold(Us,Us,Us);
- long hsctoid(struct HSC *s);
- struct HSC ldtoHSC(long);
- void getenddata();
- void searchBOOT(int);
- void getbiosHSC(int);
- void showPTable();
- void showBPB(int);
- void copyright();
- void main()
- {
- int i;
- char ft;
- Us *mb,*cp;
- clrscr();
- copyright();
- getbiosHSC(0x80);
- searchBOOT(0x80);
- showPTable();
- printf("n");
- getbiosHSC(0x81);
- searchBOOT(0x81);
- getch();
- showPTable();
- }
- void getbiosHSC(int drv)
- {
- Uc bff[8];
- 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);}
- Head=bff[3]+1;
- Sct=bff[0]%64;
- Cyl=bff[0]/64*256+bff[1]+2;
- }
- void searchBOOT(int drv)
- {
- Us cyl,i=0;
- char bff[512],dos[9];
- if(biosdisk(2,drv,1,0,1,1,bff)){printf("an Searching BOOT error!n");exit(1);}
- printf("n Fixing the No.%d Physical Hard Disk... n",(drv-127));
- printf("n CylNum head sector cylinder n");
- for(cyl=0;cyl<Cyl;cyl++)
- {
- biosdisk(2,drv,1,cyl,1,1,bff);
- if((Us)bff[510]==0x55&&(Uc)bff[511]==0xaa)
- {
- db[i]=*(struct DiskBPB *)(bff+11);
- printf("%7d",cyl);
- pt[i][0].bgn=ldtoHSC(HSCtold(1,1,cyl));
- printf(" %3x %6x %8xn",pt[i][0].bgn.hd,pt[i][0].bgn.sc,pt[i][0].bgn.cy);
- i++;
- }
- }
- N=i;
- showBPB(N);
- }
- void showPTable()
- {
- int i,j,k;
- char ch;
- getenddata();
- printf("n Cyl=%d Head=%d Sect=%d Partiton=%dn",Cyl,Head,Sct,N);
- printf("n Partition Table Data:n");
- for(i=0;i<N;i++)
- {
- for(j=0;j<2;j++)
- if(j==0||i<N-1)
- {
- printf(" ");
- for(k=0;k<16;k++)printf("%2x ",((Uc *)&pt[i][j])[k]);
- if(j==0)printf(" Sector=%6lu Size=%3u MB",(pt[i][j].sz+1),(pt[i][j].sz+1024)/2048);
- printf("n");
- }
- if(i<N-1)printf("n");
- }
- printf("n Are you sure this is the right Partition Info?(Y/N)");
- ch=getche();
- if(ch=='Y'||ch=='y')
- {
- for(i=0;i<32;i++)
- {
- Sector1[0x1BE+i]=((Uc *)&pt[0][0])[i];
- }
- if(biosdisk(0x03,0x80,0,0,1,1,Sector1))
- {printf("na Fixed Hard Disk Partition error!n");exit(0);}
- else printf("n Fixed Hard Disk Partition ok.");
- }
- }
- void getenddata()
- {
- struct HSC hsc;
- long id1,id0,ext;
- int i,j;
- last=ldtoHSC(HSCtold(Head-1,Sct,Cyl-2));
- for(i=0;i<N;i++)
- {
- id0=HSCtold(pt[i][0].bgn.hd-1,pt[i][0].bgn.sc,pt[i][0].bgn.cy);
- if(i<N-1)
- {
- id1=HSCtold(pt[i+1][0].bgn.hd-1,pt[i+1][0].bgn.sc,pt[i+1][0].bgn.cy);
- pt[i][0].end=ldtoHSC(id1-1);
- }
- else{
- id1=HSCtold(Head-1,Sct,Cyl-2)+1;
- pt[i][0].end=last;
- }
- if(i==0)pt[i][0].Flg=0x80;else pt[i][0].Flg=0;
- pt[i][0].id=Sct;pt[i][0].sz=id1-id0-Sct;
- if(pt[i][0].sz>65535)pt[i][0].Tp=6;
- 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)
- pt[i][0].Tp=4;
- else pt[i][0].Tp=1;
- }
- ext=pt[0][0].sz+pt[0][0].id;
- for(i=0;i<N-1;i++)
- {
- pt[i][1].Tp=5;
- pt[i][1].bgn=pt[i+1][0].bgn;
- pt[i][1].bgn.hd=pt[i+1][0].bgn.hd-1;
- if(i==0)pt[i][1].end=last;
- else pt[i][1].end=pt[i+1][0].end;
- if(i==0)pt[i][1].id=ext;
- else pt[i][1].id=hsctoid((struct HSC *)&(pt[i+1][0].bgn))-ext-Sct;
- if(i==0)pt[i][1].sz=hsctoid(&last)-hsctoid((struct HSC *)&(pt[0][0].end));
- else pt[i][1].sz=hsctoid((struct HSC *)&(pt[i][1].end))-hsctoid((struct HSC *)&(pt[i][0].end));
- }
- }
- long hsctoid(struct HSC *s)
- {
- return HSCtold(s->hd,s->sc,s->cy);
- }
- long HSCtold(Us hd,Us sct,Us cyl)
- {
- long id;
- Ul cylinder,sector;
- cylinder=cyl+sct/64*256;sector=sct%64-1;
- id=(long)cylinder*Head*Sct+hd*Sct+sector;
- return id;
- }
- struct HSC ldtoHSC(long id)
- {
- struct HSC hsc;
- Us r,cyl;
- cyl=id/((long)Head*Sct);hsc.cy=cyl%256;
- r=id%((long)Head*Sct);hsc.hd=r/Sct;
- hsc.sc=r%Sct+1+cyl/256*64;
- return hsc;
- }
- void showBPB(int n)
- {
- int i;
- printf("n Partition number : ");
- for(i=0;i<n;i++)printf("%4d",i+1);
- printf("n Sectors per cluster :");
- for(i=0;i<n;i++)printf("%4u",db[i].sectperclust);
- printf("n Sectors per FAT : ");
- for(i=0;i<n;i++)printf("%4u",db[i].sectperFAT);
- printf("n Number of FAT : ");
- for(i=0;i<n;i++)printf("%4u",db[i].numberofFAT);
- printf("n Items of root Dir : ");
- for(i=0;i<n;i++)printf("%4u",db[i].itemsofroot);
- printf("n Hidden sectors : ");
- for(i=0;i<n;i++)printf("%4u",db[i].hiddensect);
- printf("n Reserved sectors : ");
- for(i=0;i<n;i++)printf("%4u",db[i].reservedsect);
- printf("n");
- }
- void copyright()
- {
- printf("n Babysoft (R) Hard Disk Partition Fixer version 1.0");
- printf("n Copyright (C) 1997.12 LiuYaDing,Babysoft Corp.n");
- }