资源描述
备份硬盘主引导扇区程序 [首页][目录]
用FDISK对硬盘进行分区时,它在硬盘的0面0道1扇区生成一个包含分区信息表、主引导程序的主引导记录,
其作用是当系统加电或复位时,若从硬盘自举,ROMBIOS就会把硬盘该扇区的内容读到内存的0000:
7C00处,并执行主引导程序,把活动分区的操作系统引导到内存。
目前,计算机病毒已逾千种,它们以各种方式威胁着计算机用户,其中有一类计算机病毒专门攻击计算机硬盘
的主引导记录。如果在计算机无病毒污染时,把主引导记录读出来,写入一个数据文件,保存起来,一旦计算
机受到这类病毒的侵袭,就可以用以前保存的主引导记录覆盖0面0道1扇区,消除病毒,这不失为一种有效的
防病毒手段。
二、程序设计
TURBO C可以方便地调用汇编子程序,也可以用inline语句直接嵌入机器码,其函数及过程完备,代码质量高
。作者用TURBO C做为编程语言,编写了备份硬盘主引导扇区程序,它既可以将硬盘的主引导记录写到文件中
,又可以将文件的主引导记录备份写到硬盘的0面0道1扇区。
该程序设计的关键点是:如何读写硬盘的主引导记录,我们可以使用BIOS磁盘读写中断13H来完成这项功能,
在该程序中由ProcessPhysicalSector()来完成,它利用了C语言的inline汇编功能,直接将汇编语句嵌入C程
序,它可以根据所给参数的不同,执行读/写磁盘指定扇区的功能,这是一个通用函数,可以移植到其它程序
中使用。
三、程序的使用方法
在DOS系统提示符下执行:
HMR </R|/W>
当取“/R”开关时,HMR执行读硬盘主引导记录的功能,可把硬盘上的主引导记录写到MRECORD.SAV的文件中
;当取“/W”开关时,HMR执行写硬盘主引导记录的功能,可以根据提示把文件MRECORD.SAV中存储的内容写
到硬盘的0面0柱1扇区。
四、源程序清单
/********************************************************/
/* 程序名称: HMR.C 1.50 */
/* 作 者: 董占山 */
/* 完成日期: 1990,1995 */
/* 用 途: 读/写硬盘的主引导记录 */
/* 编译方法: 用下列命令编译连接可以得到HMR.COM: */
/* tcc -mt hmr */
/* tlink c:\tc\lib\c0t+hmr,hmr,,c:\tc\lib\cs\lib /t */
/********************************************************/
#include <stdio.h>
#include <string.h>
char *MBRF = "C:\\MRECORD.SAV";
char mp[512];
int i;
FILE *f1;
/* 执行读写指定磁盘物理扇区的信息 */
void ProcessPhysicalSector(OperateType,DriveType,HeadNo,StartCyl,StartSec,SectorNumber,p)
unsigned char OperateType,DriveType,HeadNo,StartCyl,StartSec,SectorNumber;
char *p;
{
asm push es
asm push ds
asm pop es
asm mov bx,p /* 缓冲区地址 */
asm mov ch,StartCyl /* 开始柱体数 */
asm mov cl,StartSec /* 开始扇区数 */
asm mov dh,HeadNo /* 头数 */
asm mov dl,DriveType /* 驱动器号,0=A,1=B,80=C,81=D */
asm mov ah,OperateType /* 操作类型 */
asm mov al,SectorNumber /* 扇区数 */
asm int 0x13
asm pop es
};
/* 回答是否的函数 */
int YesNo(s)
char *s;
{
char c;
printf("%s (Y/N)?",s);
do {
c = getchar();
c = toupper(c);
} while ((c!='Y') && (c!='N'));
if (c=='Y') return 1;
else return 0;
}
/* 显示程序的使用方法 */
void help()
{
printf("\n%s\n%s\n%s\n%s\n",
"Usage : Read/Write data the main boot record of the hard disk",
"Syntex : HMR </switch>",
" /R --- read the main boot record and write to file 'C:\MRECORD.SAV'",
" /W --- read file 'C:\MRECORD.SAV'and write the main boot record to hard disk");
exit(0);
}
/* 读取硬盘的主引导记录,并将它写入文件 */
void readhmr()
{
if (YesNo("Read themain boot record of the hard disk ")==1) {
ProcessPhysicalSector(2,0x80,0,0,1,1,mp); /* 读硬盘主引导记录 */
if (YesNo("Save themain boot record of the hard disk ")==1) {
if ((f1=fopen(MBRF,"wb+"))!=NULL) {
fwrite(mp,512,1,f1);
fclose(f1);
}
else {
printf("Error : File <C:\\MRECORD.SAV> can not be created !\n");
help();
}
}
}
}
/* 从文件读取主引导记录信息,并写入硬盘的0面0道1扇区 */
void writehmr()
{
if (YesNo("Write the main boot record of the hard disk")==1) {
if (YesNo("Are you sure")==1) {
if ((f1 = fopen(MBRF,"rb"))==NULL) {
printf("Error : File <C:\\MRECORD.SAV> not found !\n");
help();
}
else {
fread(mp,512,1,f1);
fclose(f1);
ProcessPhysicalSector(3,0x80,0,0,1,1,mp);
/* 写硬盘主引导记录 */
}
}
}
}
/* 主程序 */
main(argc,argv)
int argc;
char *argv[];
{
printf("HMR Version 1.2 Copyright (c) 1990,95 Dong Zhanshan\n");
switch (argc) {
case 1 : help();
break;
case 2 : if (strcmp(strupr(argv[1]),"/R")==0) readhmr();
else if (strcmp(strupr(argv[1]),"/W")==0) writehmr();
else help();
}
}
展开阅读全文