time 
设为首页】【收藏本站
当前位置: 主页 > 程序设计 > C\C++\VC > C语言 > 读写硬盘扇区的C语言程序

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

时间:2009-09-20 23:31 点击:2463次 字体:[ ]




我用C写的读写硬盘的MBR和DBR扇区的程序。

/*******************************************
 * 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;
}

 



本文地址 : http://www.fengfly.com/plus/view-77272-1.html
标签: 读写 硬盘
------分隔线----------------------------
相关文章
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:
本栏分类