📄 minitow.c
字号:
unsigned long oldDiff=0;
/*unsigned long offsetAllRecord=0; */
char done=0;
unsigned long offsetAllRecord=0;
short lastSpare=0;
unsigned long sectorToRestore=0;
unsigned long sectorDone=0;
/*if(fopenFile(&f, 1, file, NULL)==-1) goto end;*/
char checkFile[32];
if(fopenFile(&f, 1, file, NULL)==-1) goto end;
memset(checkFile,0,32);
fread(checkFile,strlen(miniTOtag),1,f);
if(strnicmp(miniTOtag,checkFile,strlen(miniTOtag))!=0)
{
printf("Invalid file");
return -1;
}
driveNum=(drive ? drive : 1);
all=filelength(fileno(f));
buf=(char *)malloc(65535);
if(mode==COMPARE) diskBuf=(char *)malloc(65535);
printf("Working...\n");
while(1)
{
readf=ftell(f);
/*printf("read:%ld,all:%ld\n",readf,all); */
if(readf>=all) break;
currentDrive=driveNum;
if(drive)
{
while(1)
{
/* printf("2 driveNum=%d,start=%lu\n",driveNum,start); */
/*printf("pos=%lu ",ftell(f)); */
if(/*feof(f)*/all==ftell(f)) {goto end;}
fread(&driveNum,2,1,f);
/*printf("driveNum=%d,drive=%d\n",driveNum,drive); */
if(driveNum!=drive)
{
/*printf("if(driveNum!=drive)\ndriveNum=%d,drive=%d\n",driveNum,drive); */
if(driveNum<drive)
{
/*fseek(f,516L,SEEK_CUR);*/
/*printf("after fseek,%ld\n",ftell(f));*/
fseek(f,4,SEEK_CUR);
fread(§orToRestore,2,1,f);
fseek(f,512*sectorToRestore,SEEK_CUR);
}
else goto end;
}
else break;
}
}
else fread(&driveNum,2,1,f);
/*fread(&start,4,1,f);
fread(buf,512,1,f);*/
fread(&start,4,1,f);
fread(§orToRestore,2,1,f);
fread(buf,512*sectorToRestore,1,f);
/* printf("driveNum=%d,start=%lu\n",driveNum,start); */
done=1;
if(mode==RESTORE || mode==COMPARE)
{
/*
if(driveNum!=currentDrive || drive)
{
supportExtInt13=1;
}
/* if(!supportExtInt13) continue;*/
if(!yes)
{
printf("%s drive_%d sector_%-10lu %-3lu sector ",(mode==RESTORE ? "Restore" : "Compare"),driveNum,start,sectorToRestore);
if(YesNo("Continue","")!=1) continue;
}
int13Return=extInt13((mode==RESTORE ? 0X43 : 0x42),
driveNum+127,
start,
sectorToRestore,
(mode==RESTORE ? buf : diskBuf));
if(int13Return!=0)
{
printf("Drive(%d) sector(%lu)\n",driveNum,start);
int13Error((mode==RESTORE ? 0X43 : 0x42),driveNum+127,start,int13Return);
continue;
}
}
l++;
if(mode==COMPARE)
{
if(compareByte(diskBuf,buf,512*sectorToRestore,pause,&diff,&offsetAllRecord)==-1)
break;
}
if(mode==PRINT)
{
/*
if(printByte(buf,512*sectorToRestore,0,&offsetAllRecord,pause,&lastSpare)==-1)
goto end;*/
}
if(mode!=PRINT)
{
printf("%s drive_%d sector_%-10lu %-3lu sector\n",
(mode==RESTORE ? "Restored" : "Compared"),
driveNum,
start,
sectorToRestore);
/* done=1;*/
}
sectorDone+=sectorToRestore;
}
end :
if(mode!=PRINT)
{
if(done)
{
printf("%s %lu sectors in all\n",
(mode==RESTORE ? "Restored" : "Compared"),
sectorDone);
if(mode==COMPARE) printf("%lu differences found\n",diff);
}
else printf("No found\n");
}
if(f) fclose(f);
if(buf) free(buf);
if(diskBuf) free(diskBuf);
}
short getFATinfo(Pinfo **partitionTable,
/*short driveNum,
unsigned long startSector, */
commonArg *arg,
unsigned short *reservedSectorBeforeFAT,
unsigned long *FATsector,
char *FATcount)
{
unsigned char buffer[512]; /* 存放 DBR 的缓冲 */
char fat12TRUE=0;
char fat16TRUE=0;
char fat32TRUE=0;
unsigned short fat1216=0;
unsigned long fat32=0;
/*char fat32TRUE=1; */
short int13Return;
short drive=0;
unsigned long start=0;
drive=arg->drive;
start=partitionTable[arg->index]->start;
/* } */
/*获取DBR,以便 获取FAT参数 */
/*biosdisk(2,hardDisk,1,0,1,1,buffer); */
if(drive==-127 || drive==-126)
{
if (absread(drive+127, 1, start, buffer) != 0)
{
printf("Failed to read %c: sector:%lu\n",(drive==-127 ? 'A' : 'B'),start);
return;
}
}
else
{
int13Return=extInt13(0x42,
drive+127,
start,
1,
buffer);
if(int13Return!=0)
{
int13Error(0x42,drive+127,start,int13Return);
return;
}
} /* 获取FAT前距离DBR的扇区数 */
*reservedSectorBeforeFAT=*(unsigned short *)(buffer+0xE);
*FATcount=buffer[0x10];
/* 获取FAT所占的扇区数 */
fat1216=*(unsigned short *)(buffer+0x16);
fat32=*(unsigned long *)(buffer+0x24);
if(fat1216)
{
if(strnicmp(buffer+0x36,"FAT12",5)==0) fat12TRUE=1;
if(strnicmp(buffer+0x36,"FAT16",5)==0) fat16TRUE=1;
}
else if(strnicmp(buffer+0x52,"FAT32",5)==0 && fat32) fat32TRUE=1;
else
{
printf("Invalid FAT.\n");
return 0;
}
if(fat32TRUE) /* 如果是 FAT32 */*FATsector=fat32;
/* printf("This is FAT32. Sector number per FAT is %lu.\n",*FATsector);
*/
/* 如果是 FAT16 */
else *FATsector=fat1216;
/* printf("This is FAT16. Sector number per FAT is %lu.\n",*FATsector);
*/
if(fat12TRUE) return 1;
else if(fat16TRUE) return 2;
else if(fat32TRUE) return 3;
}
void doDPTDBRFAT(Pinfo **partitionTable, commonArg *arg, char mode, char target, char *file, char pause, char compress, char level, char yes)
{
unsigned char zlibKey[256];
unsigned long fileSize=0;
unsigned long doLen=0;
unsigned long dstLen=MAX_BUF_BYTE;
/*unsigned char *dst=NULL; */
unsigned long allCompressed=0;
char compressKeyTRUE=1;
char compressKeyEveryAskTRUE=1;
unsigned long fileRealSize;
char noUse=1;
char mute=0;
unsigned char fat[]={0x04,0x06,0x0B,0x0C,0x01,0x0E,
0x14,0x16,0x1B,0x1C,0x11,0x1E,0x00};
unsigned char fat32[]={0x0B,0x0C,0x1B,0x1C,0x00};
unsigned char ntfs[]={0x07,0x17,0x00};
short i,j=0;
short drive=0;
unsigned long startToDo=0,startToDo2=0;
unsigned char *sharedBuf=NULL,*oneSide=NULL;
unsigned long sectorToDo=0;
FILE *f=NULL;
/*char read=1,write=0; */
unsigned short reservedSectorBeforeFAT=0;
unsigned long sectorDone=0;
unsigned short doSector=120;
unsigned long diff=0;
unsigned long offsetAllRecord=0;
short int13Return;
char FS[16];
char hidden=0;
char compressVar=0;
char result=0;
char done=0;
unsigned long freeSpaceKB=0;
unsigned long writtenSpace=0;
short x,y;
char gif[]={'-','\\','|','/','-','\\','|','/','\0'};
char FATcount=0;
short DBRbackupPos=0;
char DBRwarning[128];
char fat32TRUE=0;
/*unsigned long offsetAllRecord=0; */
short lastSpare=0;
if(mode==SAVE || mode==RESTORE || mode==COMPARE || (mode==PRINT && target==PRINTFILE))
{
if(fopenFile(&f, (mode==SAVE ? 0 : 1), file, NULL)==-1) goto end;
if(mode==SAVE) fwrite(miniTOtag,strlen(miniTOtag),1,f);
}
sharedBuf=(unsigned char *)malloc(MAX_BUF_BYTE);
if(mode==COMPARE/* || compress || compressVar*/) oneSide=(unsigned char *)malloc(MAX_BUF_BYTE);
if(mode==SAVE || mode==REPLACE || (mode==PRINT && (target==DPT || target==DBR || target==FAT)))
{
if(mode==SAVE)
{
/*if(getDiskFree(partitionTable, getDOSletterFromFileName(file), &freeSpaceKB)==-1)
goto end;*/
}
drive=arg->drive;
/*printf("%d\n",arg->drive); */
if(target==DPT)
{
if(arg->argMode==ARGUMENT_DISK) startToDo=0;
else
{
if(!partitionTable[arg->index]->logicDrive) startToDo=0;
else startToDo=partitionTable[arg->index]->start-63;
}
}
if(target==DBR)
{
for(i=0;fat[i];i++)
{
if(partitionTable[arg->index]->ID==fat[i]) goto out;
}
for(j=0;ntfs[j];j++)
{
if(partitionTable[arg->index]->ID==ntfs[j]) goto out;
}
out :
if(!fat[i] && !ntfs[j])
{
printf("Not FAT/NTFS, no DBR\n");
goto end;
}
if(mode==REPLACE)
{
for(i=0;fat32[i];i++)
if(partitionTable[arg->index]->ID==fat32[i]) fat32TRUE=1;
if(!fat32TRUE)
{
printf("Not FAT32, no DBR backup\n");
goto end;
}
}
i=0;
j=0;
startToDo=partitionTable[arg->index]->start;
}
if(target==DPT || target==DBR) sectorToDo=1;
if(target==FAT)
{
for(i=0;fat[i];i++)
{
if(partitionTable[arg->index]->ID==fat[i]) break;
else if(!fat[i+1])
{
printf("Not FAT\n");
goto end;
}
}
if(!getFATinfo(partitionTable,arg,&reservedSectorBeforeFAT,§orToDo,&FATcount))
goto end;
if(mode==REPLACE && FATcount!=2)
{
printf("FAT count %d is not normal 2\n",FATcount);
goto end;
}
printf("Sector number per FAT is %lu\n",sectorToDo);
startToDo=partitionTable[arg->index]->start+reservedSectorBeforeFAT;
if(startToDo==0 || sectorToDo==0)
{
goto end;
}
}
/* printf("2\n"); */
if(mode==SAVE)
{
/*
if(freeSpaceKB<(sectorToDo*512+8)/1024UL)
{
printf("Space needed : %lu bytes\n Free space : %lu bytes\nNo enough space\n",
(sectorToDo*512+8)/1024UL,freeSpaceKB);
goto end;
}*/
/*fwrite(&drive,2,1,f);
fwrite(&startToDo,4,1,f);
fwrite(§orToDo,2,1,f);*/
/*} */
}
}
else
{
/*fread(&drive,2,1,f);
fread(&startToDo,4,1,f);
fread(§orToDo,2,1,f);*/
/*} */
}
/*x=wherex();
y=wherey();*/
/*
if(!yes)
{
switch(mode)
{
case SAVE : printf("Save "); break;
case RESTORE : printf("Restore "); break;
case COMPARE : printf("Compare "); break;
case REPLACE : printf("Replace "); break;
}
printf("drive_%d sector_%lu %lu sectors ",drive,startToDo,sectorToDo);
if(YesNo("Continue","")!=1) goto end;
}
startToDo2=startToDo;
/*printf("Press ESC to exit...\n");*/
printf("Working...\n");
while(1)
{
dstLen=MAX_BUF_BYTE;
/*if(sectorToDo-sectorDone<doSector) doSector=sectorToDo-sectorDone;*/
if(mode==SAVE || mode==REPLACE || (mode==PRINT && target!=PRINTFILE))
{
if(sectorDone>=sectorToDo) break;
if(sectorToDo-sectorDone<doSector) doSector=sectorToDo-sectorDone;
if(!yes)
{
switch(mode)
{
case SAVE : printf("Save "); break;
case REPLACE : printf("Replace "); break;
}
printf("drive_%d sector_%lu %lu sectors ",drive,startToDo,doSector);
if(YesNo("Continue","")!=1)
{
sectorToDo-=doSector;
continue;
}
}
int13Return=extInt13(0x42,
drive+127,
startToDo+((mode==REPLACE && target==FAT) ? sectorToDo : 0),
doSector,
sharedBuf);
/* printf("%d %lu\n",drive,startToDo); */
if(int13Return!=0)
{
int13Error(0x42,drive+127,startToDo,int13Return);
goto end;
}
if(mode==SAVE)
{
fwrite(&drive,2,1,f);
fwrite(&startToDo,4,1,f);
fwrite(&doSector,2,1,f);
fwrite(sharedBuf,doSector*512,1,f);
}
if(mode==PRINT)
{
/*if(printByte(sharedBuf,doSector*512,0,&offsetAllRecord,pause,&lastSpare)==-1)
goto end;*/
}
if(mode==REPLACE && target==DBR)
{
DBRbackupPos=*(unsigned short *)(sharedBuf+0x32);
if(DBRbackupPos!=6)
{
memset(DBRwarning,0,128);
if(DBRbackupPos==0)
{
DBRbackupPos=6;
sprintf(DBRwarning,
"Invalid DBR backup sector position 0, not normal 6\nDo it as 6");
}
else
sprintf(DBRwarning,
"DBR backup sector position %d is not normal 6\nDo it as %d",
DBRbackupPos,DBRbackupPos);
if(YesNo(DBRwarning,"")!=1) goto end;
}
int13Return=extInt13(0x42,
drive+127,
startToDo+DBRbackupPos,
doSector,
sharedBuf);
/* printf("%d %lu\n",drive,startToDo); */
if(int13Return!=0)
{
int13Error(0x42,drive+127,startToDo,int13Return);
goto end;
}
int13Return=extInt13(0x43,
drive+127,
startToDo,
doSector,
/*((mode==RESTORE && compressVar) ? oneSide : */sharedBuf/*)*/);
if(int13Return!=0)
{
int13Error(0x43,drive+127,startToDo,int13Return);
goto end;
}
}
}
if(mode==RESTORE || mode==COMPARE |
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -