读写硬盘扇区的C语言程序

*******************************************
 * Master Boot Record Programme:           *
 *   Backup the Master Boot Sector         *
 *   Fix the Master Boot Sector            *
 *   Change the Partition Table            *
 *       -- by Crystal.Mouse 04/30/99 --   *
 * Turboc 2.0                              *
 *******************************************/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <bios.h>
#include <io.h>
#include <dos.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <conio.h>


/* define the           */


char buff[512];  /*buffer for master boot sector */
/* buffer1 backup for master boot sector */
char buff1[512];
/* This is the dos boot programme data */
char sector2[512]={0xEB,0x3C,0x90,0x4D,0x53,0x57,0x49,0x4E,0x34,0x2E,0x31,0x00,
 0x02,0x20,0x01,0x00,0x02,0x00,0x02,0x00,0x00,0xF8,0xC9,0x00,
 0x3F,0x00,0x40,0x00,0x3F,0x00,0x00,0x00,0x01,0x0A,0x19,0x00,
 0x80,0x00,0x29,0xDC,0x12,0x6F,0x04,0x30,0x34,0x32,0x36,0x39,
 0x39,0x20,0x20,0x20,0x20,0x20,0x46,0x41,0x54,0x31,0x36,0x20,
 0x20,0x20,0xFA,0x33,0xC9,0x8E,0xD1,0xBC,0xFC,0x7B,0x16,0x07,
 0xBD,0x78,0x00,0xC5,0x76,0x00,0x1E,0x56,0x16,0x55,0xBF,0x22,
 0x05,0x89,0x7E,0x00,0x89,0x4E,0x02,0xB1,0x0B,0xFC,0xF3,0xA4,
 0x06,0x1F,0xBD,0x00,0x7C,0xC6,0x45,0xFE,0x0F,0x8B,0x46,0x18,
 0x88,0x45,0xF9,0x38,0x4E,0x24,0x7D,0x22,0x8B,0xC1,0x99,0xE8,
 0x77,0x01,0x72,0x1A,0x83,0xEB,0x3A,0x66,0xA1,0x1C,0x7C,0x66,
 0x3B,0x07,0x8A,0x57,0xFC,0x75,0x06,0x80,0xCA,0x02,0x88,0x56,
 0x02,0x80,0xC3,0x10,0x73,0xED,0x33,0xC9,0x8A,0x46,0x10,0x98,
 0xF7,0x66,0x16,0x03,0x46,0x1C,0x13,0x56,0x1E,0x03,0x46,0x0E,
 0x13,0xD1,0x8B,0x76,0x11,0x60,0x89,0x46,0xFC,0x89,0x56,0xFE,
 0xB8,0x20,0x00,0xF7,0xE6,0x8B,0x5E,0x0B,0x03,0xC3,0x48,0xF7,
 0xF3,0x01,0x46,0xFC,0x11,0x4E,0xFE,0x61,0xBF,0x00,0x07,0xE8,
 0x23,0x01,0x72,0x39,0x38,0x2D,0x74,0x17,0x60,0xB1,0x0B,0xBE,
 0xD8,0x7D,0xF3,0xA6,0x61,0x74,0x39,0x4E,0x74,0x09,0x83,0xC7,
 0x20,0x3B,0xFB,0x72,0xE7,0xEB,0xDD,0xBE,0x7F,0x7D,0xAC,0x98,
 0x03,0xF0,0xAC,0x84,0xC0,0x74,0x17,0x3C,0xFF,0x74,0x09,0xB4,
 0x0E,0xBB,0x07,0x00,0xCD,0x10,0xEB,0xEE,0xBE,0x82,0x7D,0xEB,
 0xE5,0xBE,0x80,0x7D,0xEB,0xE0,0x98,0xCD,0x16,0x5E,0x1F,0x66,
 0x8F,0x04,0xCD,0x19,0xBE,0x81,0x7D,0x8B,0x7D,0x1A,0x8D,0x45,
 0xFE,0x8A,0x4E,0x0D,0xF7,0xE1,0x03,0x46,0xFC,0x13,0x56,0xFE,
 0xB1,0x04,0xE8,0xC1,0x00,0x72,0xD6,0xEA,0x00,0x02,0x70,0x00,
 0xB4,0x42,0xEB,0x2D,0x60,0x66,0x6A,0x00,0x52,0x50,0x06,0x53,
 0x6A,0x01,0x6A,0x10,0x8B,0xF4,0x74,0xEC,0x91,0x92,0x33,0xD2,
 0xF7,0x76,0x18,0x91,0xF7,0x76,0x18,0x42,0x87,0xCA,0xF7,0x76,
 0x1A,0x8A,0xF2,0x8A,0xE8,0xC0,0xCC,0x02,0x0A,0xCC,0xB8,0x01,
 0x02,0x8A,0x56,0x24,0xCD,0x13,0x8D,0x64,0x10,0x61,0x72,0x0A,
 0x40,0x75,0x01,0x42,0x03,0x5E,0x0B,0x49,0x75,0x77,0xC3,0x03,
 0x18,0x01,0x27,0x0D,0x0A,0x49,0x6E,0x76,0x61,0x6C,0x69,0x64,
 0x20,0x73,0x79,0x73,0x74,0x65,0x6D,0x20,0x64,0x69,0x73,0x6B,
 0xFF,0x0D,0x0A,0x44,0x69,0x73,0x6B,0x20,0x49,0x2F,0x4F,0x20,
 0x65,0x72,0x72,0x6F,0x72,0xFF,0x0D,0x0A,0x52,0x65,0x70,0x6C,
 0x61,0x63,0x65,0x20,0x74,0x68,0x65,0x20,0x64,0x69,0x73,0x6B,
 0x2C,0x20,0x61,0x6E,0x64,0x20,0x74,0x68,0x65,0x6E,0x20,0x70,
 0x72,0x65,0x73,0x73,0x20,0x61,0x6E,0x79,0x20,0x6B,0x65,0x79,
 0x0D,0x0A,0x00,0x00,0x49,0x4F,0x20,0x20,0x20,0x20,0x20,0x20,
 0x53,0x59,0x53,0x4D,0x53,0x44,0x4F,0x53,0x20,0x20,0x20,0x53,
 0x59,0x53,0x7F,0x01,0x00,0x41,0xBB,0x00,0x07,0x80,0x7E,0x02,
 0x0E,0xE9,0x40,0xFF,0x00,0x00,0x55,0xAA};


/* sector1 is the Master boot program data */
char 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,0xB8,0x01,0x02,0xBB,0x00,0x08,0xB9,
 0x01,0x00,0xBA,0x80,0x00,0xCD,0x13,0xBF,0x00,0x06,0xBE,0x00,
 0x08,0xB9,0x00,0x01,0xFC,0xF3,0xA7,0x74,0x14,0xBE,0x17,0x07,
 0xE8,0x2F,0x01,0xB8,0x01,0x03,0xBB,0x00,0x06,0xB9,0x01,0x00,
 0xBA,0x80,0x00,0xCD,0x13,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,0x0D,0x80,0x3C,0x00,0x74,0xF4,
 0xBE,0xAE,0x06,0xE8,0xF0,0x00,0xEB,0xFE,0xBF,0x05,0x00,0xBB,
 0x00,0x7C,0xB8,0x01,0x02,0x57,0xCD,0x13,0x5F,0x73,0x0C,0x33,
 0xC0,0xCD,0x13,0x4F,0x75,0xED,0xBE,0xD2,0x06,0xEB,0xE0,0xBE,
 0xF7,0x06,0xBF,0xFE,0x7D,0x81,0x3D,0x55,0xAA,0x75,0xD4,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,0x2E,0x20,0x4F,0x68,0x2C,0x6D,0x79,
 0x20,0x67,0x6F,0x64,0x21,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,
 0x2E,0x20,0x57,0x68,0x79,0x3F,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,0x2E,0x20,0x53,0x68,0x69,
 0x74,0x21,0x00,0x4D,0x61,0x73,0x74,0x65,0x72,0x20,0x42,0x6F,
 0x6F,0x74,0x20,0x53,0x65,0x63,0x74,0x6F,0x72,0x20,0x63,0x68,
 0x61,0x6E,0x67,0x65,0x64,0x2E,0x0A,0x0D,0x44,0x6F,0x6E,0x27,
 0x74,0x20,0x77,0x6F,0x72,0x72,0x79,0x2C,0x49,0x20,0x68,0x61,
 0x76,0x65,0x20,0x72,0x65,0x63,0x6F,0x76,0x65,0x72,0x65,0x64,
 0x20,0x69,0x74,0x21,0x20,0x43,0x6C,0x65,0x76,0x65,0x72,0x21,
 0x20,0x45,0x54,0x2D,0x4D,0x6F,0x75,0x73,0x65,0x20,0x59,0x65,
 0x61,0x68,0x21,0x0A,0x0D,0x00,0xAC,0x3C,0x00,0x74,0x0B,0x56,
 0xBB,0x07,0x00,0xB4,0x0E,0xCD,0x10,0x5E,0xEB,0xF0,0xC3,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,0x55,0xAA};


char fnwrite[20],fnread[20];     /* filename */
FILE *fp;                        /* file pointer */
int head,cylinder,sector,drive;  /* harddisk head,cylinder sector drive */



/* define the key code */


#define ESC 0x011b
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define UP 0x4800
#define DOWN 0x5000
#define HOME 0x4700
#define END 0x4f00
#define PGUP 0x4900
#define PGDN 0x5100
#define F2 0x3c00
#define F1 0x3b00



#define W 1
#define R 0        /* flag for                 */
#define D 0        /* flag for Dos Boot Sector */
#define M 1        /* flag for Master Boot Sector */


/* declare the function */


int  fileopen(int f,char *file); /*Read or Creat the backupfile */
int  writembr(char *file);   /* write the buffer to master boot sector */
int  writedbr(char *file);   /* write the buffer to dos boot sector */
int  backupmbr(char *file);      /* backup the Master Boot record data */
int  backupdbr(char *file);        /* backup the dos boot sector data */
int  printmbr();      /* print the master boot sector */
int  init(int f);         /* init the operating */
void help();       /*help*/
void title();      /* display the infomation */
int  copypart(char *src,char *dest);
void infina(char *file);     /* open or creat the backup file */
void closecur(); /* close the cursor */
void opencur();  /* open the cursor */
int  display();  /* display the data */
void interrupt(*reset)();   /* interrupt reset the machine */
void resetwin();            /* reset the screen */
void search();              /* search the dos boot sector */
int  detect(int num,int driver,int f);              /* detect the drive */


int detect(int num,int driver,int f)
/* detect the disk */
{


 union REGS r;
 r.h.ah=num;
 r.h.dl=driver;
 int86(0x13,&r,&r);
 if(f)
  return r.h.al;
 else
  return r.x.cflag;
}


/* backup the dos boot sector */
int backupdbr(char *file)
{
 int flag=1; /* flag for success or not */
 if(!fileopen(W,file))
   {printf("/nBackup file write error!");
    flag=0;
   }
 return flag;


}


/* rewrite the dos boot record */
int writedbr(char *file)
{
 int flag;  /* flag for success or not */
 char key;
 search();  /* search the the dos boot sector: head , cylinder and sector */
 init(D);   /* initialize the dos boot sector data to the buffer */
 printf("/nWarning...");
 printf("/nIt might destory your harddisk!");
 printf("/nDo you want to a backup?(y/n)");
 key=getch();
 switch(key)
 {
  case 'y':
  case 'Y':
  infina(fnwrite);      /* input the backup filename */
  if(!backupdbr(fnwrite))
     {
      printf("/nBackup error!");
      flag=0;
      break;
     }
  case 'n':
  case 'N':
           if(!fileopen(R,file))  /* open the existed backup file data to buffer */
    {
     printf("/nBackup file %s open error!",file);
     flag=0;
     return flag;
    }
  }
 /* rewrite the dos boot sector with the buffer data */
  if(biosdisk(0x03,0x80,head,cylinder,sector,1,buff1))
      {
       printf("/nWrite Master Boot Sector error!");
       flag=0;
      }
    else
      {
       printf("/nWrite Dos Boot Sector OK!");
       flag=1;
      }
}


/* backup the Master Boot Sector data to file */
int backupmbr(char *file)
{
 int flag=1;
 init(M);   /* initialize the master boot sector to the buffer */
 if(!fileopen(W,file))
   {
    printf("/nBackup file write error!");
    flag=0;
   }
 return flag;
}


int fileopen(int f,char *file)
/* f for read or creat file */
{
 int flag=1;
 if(f==0)
   {
    if((fp=fopen(file,"rb"))!=NULL)
      {
       fread(buff1,512,1,fp);
       fclose(fp);
      }
    else flag=0;
   }
 if(f==1)
   {
    if((fp=fopen(file,"wb"))!=NULL)
      {
       fwrite(buff,512,1,fp);
       fclose(fp);
      }
    else flag=0;
   }
 return flag;
}


 


int writembr(char *file)
{
 int flag=1;
 char key;
 printf("/nWarning...");
 printf("/nIt might destory your harddisk!");
 printf("/nDo you want to a backup?(y/n)");
 key=getch();
 switch(key)
 {
  case 'y':
  case 'Y':
  infina(fnwrite);
         init(M);         /* initialize the MBR to buffer */
  if(!backupmbr(fnwrite)) /* backup the MBR */
     {
      printf("/nBackup error!");
      flag=0;


     }


      printf("/nFile %s backup ok!",fnwrite);
      if(!biosdisk(0x03,0x80,0,0,3,1,buff))
                 printf("/nThe old MBR write to head:0 , Cylinder:0 ,Sector:1 error!");
      if(!biosdisk(0x03,0x80,0,0,5,1,buff))
                 printf("/nThe old MBR write to head:0 , Cylinder:0 ,Sector:1 error!");
             printf("/nThe old MBR also backup to the harddisk side 0 cylinder 0 sector 3 and 5.");
             flag=1;



  case 'n':
  case 'N':
  if(!fileopen(R,file)) /* open the existed backup file */
    {
     printf("/nBackup file %s open error!",file);
     flag=0;
     return flag;
    }
  }
 if(biosdisk(0x03,0x80,0,0,1,1,buff1)) /* Rewrite the MBR with buffer1 */
   {
    printf("/nWrite Master Boot Sector error!");
    flag=0;
   }
 else
   {
    printf("/nWrite Master Boot Sector OK!");
    flag=1;
   }



 return flag;
}


int init(int f)
{
 int i;
 int result=1;


 for(i=0;i<512;i++)  /* initialize the buffer */
   buff[i]=0;
 if(f)
  {
   if(biosdisk(0x02,0x80,0,0,1,1,buff)) /* read the Mbr to buffer */
    {
     printf("/nMaster boot record read error!");
     result=0;
    }
  }
 else if(biosdisk(0x02,0x80,head,cylinder,sector,1,buff))
  {
   printf("/nDos Boot Sector read error!");
   result=0;
  }
 return result;
}


main()
{
 int far *res=(int far *)0x472; /* the data for reset */
 int i,j,k;
 char key;
 reset=MK_FP(0xffff,0x0000); /* reset interrupt address */


  clrscr();
  while(1){
  printf("/nSetMbr programme...('h' for help)");
  printf("/n.");
  key=getch();
  printf("%c",key);
  switch(key)
  {
   case 'H':
   case 'h':
                  help();
    break;
/* rewrite the MBR sector */
   case 'w':
   case 'W':
     infina(fnread); /* input the existed file name */
     if(!writembr(fnread))
       {
        printf("/nWrite HardDisk MBR error!");
        printf("/nPress 'v' to check it.");
        printf("/nPress 'm' to fix it.");
       }
     else
        printf("/nWrite HardDisk MBR OK!");
     break;


/* backup the MBR sector */
   case 'b':
   case 'B':
    infina(fnwrite);



    if(!backupmbr(fnwrite))
       printf("/nBackup MBR error!");
    else
       printf("/nBackup MBR OK!");
    break;
/* rewrite the dos boot sector */
   case 'd':
   case 'D':
   infina(fnread);
   if(!writedbr(fnread))
      {
       printf("/nWrite Dos Boot Sector error!");
       printf("/nPress 'v' to view the Dos Boot Sector!");
       break;
      }
   break;
/* backup the dos boot sector */
   case 'o':
   case 'O':
    infina(fnwrite);


                  search();
                  init(D);
    if(!backupdbr(fnwrite))
       printf("/nBackup Dos Boot Sector error!");
    else
       printf("/nBackup Dos Boot Sector OK!");
    break;
/* rewrite the dos boot sector with default data */
   case 'i':
   case 'I':
    search();
    init(D);
    printf("/nIt would modify the Dos Boot secotr!");
    printf("/nDo you want to backup?(Y/N)");
    key=getch();
                  if(key=='y' || key=='Y')
      {
       infina(fnwrite);
       if(!backupmbr(fnwrite))


         printf("/nBackup Error!");


                     else
                       printf("/nBackup OK!");
      }
    else
       if(key=='n' || key=='N')
   printf("/nWarning:it would modify the Dos Boot Sector!");
/* write the sector2 to the dos boot sector */
    if(biosdisk(0x03,0x80,head,cylinder,sector,1,sector2)!=0)
       printf("/nDos Boot Sector Write error!");
    else
       printf("/nDos Boot Sector Write OK!");


    break;
/* reset the machine */
   case 'r':
   case 'R':
    *res=0x1234;
    (*reset)();
/* view the harddisk sector data */
   case 'v':
   case 'V':
                  search();
     printmbr();
    break;
/* fixed the flappy boot sector */
   case 'f':
   case 'F':
                   printf("/nWhich dekette? -> (A,B,...):");
     key=getch();
     printf("%1c:",key);
                   if(key>=97)


                      key=key-97;
                   else
                      key=key-65;
/*     if(detect(0x10,key,0))
       {
        printf("/nDriver %1c: not ready!",key+65);
        break;
       }
     else
       if(detect(0x13,key,1)==0x03)
         {
   printf("/nDriver %1c: write protect!",key+65);
   break;
         }
       else  */
         if(biosdisk(0x03,key,0,0,1,1,sector2))
     printf("/nDriver %c: write error!",key+65);
                       else
                          printf("/nDrive %1c: write OK!",key+65);


     break;
/* fixed the MBR with default data */


   case 'm':
   case 'M':
    printf("/nIt would modify the Master Boot Record.");
    printf("/nDo you want to backup?(Y/N)");
    key=getch();
    printf("%1c",key);
    if(key=='y' || key=='Y')
      {
       infina(fnwrite);
       if(!backupmbr(fnwrite))
        {
         printf("/nBackup Error!");
         break;
        }
      }
    else
       if(key=='n' || key=='N')
   printf("/nWarning:it would modify the Master Boot Sector!");
    init(M);
    if((buff[0x1be]&0x00ff)==0x80 || (buff[0x1be+16]&0x00ff)==0x80
       || (buff[0x1be+32]&0x00ff)==0x80 || (buff[0x1be+48]&0x00ff)==0x80)
    copypart(buff+0x1be,sector1+0x1be);
    if(biosdisk(0x03,0x80,0,0,1,1,sector1))
        {
         printf("/nHardDisk Write error!");
         break;
        }
    else
       {
        printf("/nHardDisk Write OK!");
        printf("/nPress 'v' to check it.");
       }
       break;
/* dos shell */


   case 's':
   case 'S':
    printf("/nPress 'Exit' to return....");
    system("");
    break;
/* exit */
   case 'q':
   case 'Q':
    title();
    return 1;
/* print the undefine command */
   default:
   printf("%cBad command!",27);


      }
   }
}
/* display the information */
void title()
{
 printf("/nThank you for using the programme...");
 printf("/nProgrammer:Crystal.Mouse");
 printf("/nDate:05/01/1999");
}


/* change the destination partition table data with the source data */
int copypart(char *src,char *dest)
{
 char len=0x40;
 while(len>=0)
 {
  *(dest+len)=*(src+len);
  len--;
 }
}
/* input the filename */


void infina(char *file)
{
 printf("/nPlease input the file name:");
 scanf("%s",file);
}


void search()
{
 int i,addr;
 init(M);
 for(i=0x1be;i<0x1fe;i++)
   if((buff[i]&0x00ff)==0x80)
    {
     addr=i;
     break;
    }
 head=buff[addr+1];
 sector=(buff[addr+2]<<2)>>2;
 cy

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值