『第 9 楼』:
使用 LLM 解释/回答一下
刚刚把 写的 功能也加进去了, 合并在一起。
(已经过测试,改了几个BUG,目前还没发现新BUG,
谁发现了,贴出来)
读写硬盘(和软盘)绝对扇区的函数:
“dolba.h”
#include <bios.h>
#include <io.h>
#include <conio.h>
#include <stdio.h>
void doFile(unsigned int cmd, char *doMode, unsigned char *buffer, unsigned int sector, char *filename, unsigned long sectorDone)
{
FILE *f;
long move=sectorDone*512;
if((f=fopen(filename,doMode))==NULL)
{
printf("File Error\n");
return;
}
if(cmd==2)
fwrite(buffer,512,sector,f);
else if(cmd==3)
{
fseek(f,move,0);
fread(buffer,512,sector,f);
}
fclose(f);
}
void doLBA(unsigned int cmd,
unsigned int hardDisk,
unsigned long startSector,
unsigned long nSectorVar,
char *filename)
{
unsigned int head=0;
unsigned int cylinder=0;
unsigned int sector=0;
unsigned char pBuffer;
FILE *f;
unsigned int allowednSector=0;
unsigned int doReadSector=0;
unsigned long sectorWrite=0;
unsigned int x=0,y=0;
unsigned long nSector=nSectorVar;
long filesize;
FILE *checkFile;
x=wherex();
y=wherey();
//printf("%lu,%lu\n",startSector,nSectorVar);
cylinder=((startSector+nSector-1)/(255*63));
if(cylinder>1022)
{
printf("\n%u out of hard disk's cylinder range(0-1022)\n",cylinder);
return;
}
if(cmd==2)
{
if((f=fopen(filename,"wb+"))==NULL)
{
printf("File Error\n");
return;
}
fclose(f);
}
else if(cmd==3)
{
checkFile=fopen(filename,"rb");
filesize=filelength(fileno(checkFile));
fclose(checkFile);
if((filesize==0) || (filesize % 512)!=0)
{printf("the size of the file is 0 or not an integer in sectors!\n");
return;
}
else nSector=filesize/512;
}
cylinder=startSector/(255*63);
head=(startSector/63)%255;
sector=startSector%63+1;
allowednSector=63-sector+1;
if(nSector<=allowednSector)
doReadSector=nSector;
else doReadSector= allowednSector;
while(sectorWrite<nSector)
{
if(bioskey(1)!=0)
{
if(bioskey(0)==0x11B)
{
printf("\nStopped.\n");
return;
}
}
if(cylinder>1022)
{printf("\nout of hard disk's cylinder range(0-1022)\n");
return;}
if(cmd==2)
{
biosdisk(cmd,hardDisk,head,cylinder,sector,doReadSector,pBuffer);
doFile(2,"ab+",pBuffer,doReadSector,filename,0);
}
else if(cmd==3)
{
doFile(3,"rb",pBuffer,doReadSector,filename,sectorWrite);
biosdisk(cmd,hardDisk,head,cylinder,sector,doReadSector,pBuffer);
}
sectorWrite+= doReadSector;
gotoxy(x,y);
cprintf("%lu%c sectors done.",sectorWrite*100/nSector,'%');
head++;sector=1;
if (head>254) {cylinder++; head=0; sector=1;}
//if (cylinder>1022)
//{printf("\nout of hard disk's cylinder range(0-1022)\n");
//return;}
doReadSector=63;
if(nSector-sectorWrite<63) doReadSector=nSector-sectorWrite;
}
printf("\nDone.");
}
调用示例:
dolba.c
×××××××××××××××××××××××××××
#include ”dolba.h“
#include <stdlib.h>
void main(int argc, char *argv)
{
unsigned int cmd;
unsigned int hardDisk;
unsigned long startLBASector;
unsigned long sectorToDo;
char *filename,*endptr1,*endptr2;
if(argc!=6)
{
printf("SYNTAX: \nEXE cmd hardDisk startLBASector sectorToDo filename\n");
return;
}
cmd=atoi(argv);
hardDisk=atoi(argv);
//reservedSector=atol(argv);
// sectorPerFAT=atol(argv);
startLBASector=strtoul(argv,NULL,10);
sectorToDo=strtoul(argv,NULL,10);
filename=argv;
//printf("%s,%lu,%c\n%s,%lu,%c\n",argv,startLBASector,*endptr1,argv,sectorToDo,*endptr2);
/* getFATinfo(0x80,&reservedSector,§orPerFAT); */
doLBA(cmd,hardDisk,startLBASector,sectorToDo,filename);
}
运行:
dolba 2 128 100 200 file //从硬盘1 的100扇区处读200扇区到文件
dolba 3 128 100 200 file //把文件写到硬盘1 的100扇区处200扇区
Last edited by GOTOmsdos on 2006-6-29 at 22:19 ]
|