📄 unformat.cpp
字号:
.
.
.
*/
long index=0;
FILE *file_pointer;
unsigned char partition_table_map[1024];
int drive_number;
unsigned long destination_cylinder;
unsigned long destination_head;
unsigned long destination_sector;
/* Clear partition_table_map[] */
index=0;
do
{
partition_table_map[index]=0;
index++;
}while(index<=1023);
/* Load the map of the PARTNSAV.FIL file */
index=0;
file_pointer=fopen("A:\\PARTNSAV.FIL","rb");
if(file_pointer==NULL)
{
printf("\nError opening \"A:\\PARTNSAV.FIL\"...Operation terminated.\n");
exit(1);
}
/* Read the map from the PARTNSAV.FIL file */
test=fread(&partition_table_map,1024,1,file_pointer);
if(test==0)
{
printf("\nError reading \"A:\\PARTNSAV.FIL\"...Operation terminated.\n");
exit(1);
}
/* Write the partitions to the hard disk(s) */
index=128;
do
{
/* Read the drive number */
drive_number=partition_table_map[index+0];
/* Get the destination cylinder */
destination_cylinder=Decimal_Number(partition_table_map[index+3],partition_table_map[index+4],0,0);
/* Get the destination head */
destination_head=Decimal_Number(partition_table_map[index+5],partition_table_map[index+6],0,0);
/* Get the destination sector */
destination_sector=partition_table_map[index+7];
/* Read the partition sector from the partnsav.fil */
fread(sector_buffer,512,1,file_pointer);
/* Write the partition sector to the hard disk */
Write_Physical_Sector(drive_number,destination_head,destination_cylinder,destination_sector);
index=index+8;
}while(0!=partition_table_map[index]);
fclose(file_pointer);
}
else
{
/* List all of the partition tables */
printf("\nThis feature is not implemented at this time.\n");
}
exit(0);
}
/* Unformat Drive ///////////////////// */
void Unformat_Drive()
{
int loop;
long mirror_map_pointer;
long index;
long current_sector;
long mirror_map_start_sector;
long source_sector;
long destination_sector;
Read_Mirror_Map();
printf("\n\nUnformatting drive...\n");
/*Re-create the boot sector */
destination_sector=Decimal_Number(mirror_map[84],mirror_map[85],mirror_map[86],mirror_map[87]);
source_sector=Decimal_Number(mirror_map[88],mirror_map[89],mirror_map[90],mirror_map[91]);
Read_Sector(source_sector);
Write_Sector(destination_sector);
/* Get the size of the FAT tables from the boot sector */
Read_Sector(0);
sectors_per_fat=Decimal_Number(sector_buffer[22],sector_buffer[23],0,0);
/*Re-create the FAT tables */
mirror_map_pointer = 92;
index=1;
do
{
destination_sector=Decimal_Number(mirror_map[mirror_map_pointer],mirror_map[(mirror_map_pointer+1)],mirror_map[(mirror_map_pointer+2)],mirror_map[(mirror_map_pointer+3)]);
source_sector=Decimal_Number(mirror_map[(mirror_map_pointer+4)],mirror_map[(mirror_map_pointer+5)],mirror_map[(mirror_map_pointer+6)],mirror_map[(mirror_map_pointer+7)]);
Read_Sector(source_sector);
Write_Sector(destination_sector);
Write_Sector(destination_sector+sectors_per_fat);
mirror_map_pointer=mirror_map_pointer + 8;
index++;
} while(index<=sectors_per_fat);
/*Re-create the Root Directory */
loop=1;
/*If drive is a floppy drive, change the loop for a smaller root dir*/
if(drive_type==FLOPPY) loop=18;
do
{
destination_sector=Decimal_Number(mirror_map[mirror_map_pointer],mirror_map[(mirror_map_pointer+1)],mirror_map[(mirror_map_pointer+2)],mirror_map[(mirror_map_pointer+3)]);
source_sector=Decimal_Number(mirror_map[(mirror_map_pointer+4)],mirror_map[(mirror_map_pointer+5)],mirror_map[(mirror_map_pointer+6)],mirror_map[(mirror_map_pointer+7)]);
Read_Sector(source_sector);
/* If user wants the file and directory names listed, list them. */
if(1==list_file_and_directory_names) List_Names();
Write_Sector(destination_sector);
mirror_map_pointer=mirror_map_pointer + 8;
loop++;
} while(loop<=31);
printf("\n\nUnformat operation complete!\n");
}
/* Verify Drive Mirror //////////////// */
void Verify_Drive_Mirror()
{
long mirrored_sector;
long system_sector;
int loop;
long mirror_map_pointer;
long index;
long current_sector;
long mirror_map_start_sector;
Read_Mirror_Map();
printf("\n\nComparing drive mirror with file system...\n");
/*Compare the boot sector */
system_sector=Decimal_Number(mirror_map[84],mirror_map[85],mirror_map[86],mirror_map[87]);
mirrored_sector=Decimal_Number(mirror_map[88],mirror_map[89],mirror_map[90],mirror_map[91]);
Read_Sector(system_sector);
Copy_Sector_Into_Control_Buffer();
Read_Sector(mirrored_sector);
if(Compare_Sector_Buffers()!=0)
{
printf("\n\nVerification failed at boot sector...operation terminated.\n\n");
exit(1);
}
/*Compare the FAT tables */
mirror_map_pointer = 92;
index=1;
do
{
system_sector=Decimal_Number(mirror_map[mirror_map_pointer],mirror_map[(mirror_map_pointer+1)],mirror_map[(mirror_map_pointer+2)],mirror_map[(mirror_map_pointer+3)]);
mirrored_sector=Decimal_Number(mirror_map[(mirror_map_pointer+4)],mirror_map[(mirror_map_pointer+5)],mirror_map[(mirror_map_pointer+6)],mirror_map[(mirror_map_pointer+7)]);
Read_Sector(system_sector);
Copy_Sector_Into_Control_Buffer();
Read_Sector(mirrored_sector);
if(Compare_Sector_Buffers()!=0)
{
printf("\n\nVerification failed on 1st FAT table...operation terminated.\n\n");
exit(1);
}
Read_Sector(system_sector+sectors_per_fat);
if(Compare_Sector_Buffers()!=0)
{
printf("\n\nVerification failed on 2nd FAT table...operation terminated.\n\n");
exit(1);
}
mirror_map_pointer=mirror_map_pointer + 8;
index++;
} while(index<=sectors_per_fat);
/*Compare the Root Directory */
loop=1;
/*If drive is a floppy drive, change the loop for a smaller root dir*/
if( (0==logical_drive) || (1==logical_drive) ) loop=18;
do
{
system_sector=Decimal_Number(mirror_map[mirror_map_pointer],mirror_map[(mirror_map_pointer+1)],mirror_map[(mirror_map_pointer+2)],mirror_map[(mirror_map_pointer+3)]);
mirrored_sector=Decimal_Number(mirror_map[(mirror_map_pointer+4)],mirror_map[(mirror_map_pointer+5)],mirror_map[(mirror_map_pointer+6)],mirror_map[(mirror_map_pointer+7)]);
Read_Sector(system_sector);
Copy_Sector_Into_Control_Buffer();
Read_Sector(mirrored_sector);
if(Compare_Sector_Buffers()!=0)
{
printf("\n\nVerification failed at root directory...operation terminated.\n\n");
exit(1);
}
mirror_map_pointer=mirror_map_pointer + 8;
loop++;
} while(loop<=31);
printf("\n\nMirror image has been verified.\n\n");
exit(0);
}
/* Write Physical Sector */
void Write_Physical_Sector(int drive, int head, long cyl, int sector)
{
int result;
result=biosdisk(3, drive, head, cyl, sector, 1, sector_buffer);
if (result!=0)
{
printf("\nDisk write error...operation terminated.\n");
exit(4);
}
}
/* Write Sector To Disk */
void Write_Sector(unsigned long sector)
{
Convert_Logical_To_Physical(sector);
printf("Cylinder: %5i ",translated_cylinder);
printf("Head: %2i \n",translated_head);
/* printf("Sector: %i \n",translated_sector); */
/* Re-position cursor back to the beginning of the line */
asm{
/* Get current video display mode */
mov ah,0x0f
int 0x10
/* Get cursor position */
mov ah,0x03
int 0x10
/* Set cursor position to beginning of line */
mov ah,0x02
sub dh,1
int 0x10
}
Write_Physical_Sector(physical_drive,translated_head,translated_cylinder,translated_sector);
}
/*
/////////////////////////////////////////////////////////////////////////////
// MAIN ROUTINE
/////////////////////////////////////////////////////////////////////////////
*/
void main(int argc, char *argv[])
{
int true=1;
/* if UNFORMAT is typed without any options */
if(argc==1)
{
Display_Help_Screen();
exit(1);
}
/* if UNFORMAT is typed with more than one option and
// the first option is "/?" or "/PARTN" or "/partn" */
if(argc>=2)
{
/* if "UNFORMAT /?" is entered */
true=strcmp("/?",argv[1]);
if(0==true)
{
Display_Help_Screen();
exit(0);
}
/* if "UNFORMAT /PARTN" or "UNFORMAT /partn" is entered */
if(0==stricmp("/PARTN",argv[1]))
{
if(argc==3)
{
/*if "UNFORMAT /PARTN /L" is entered */
if(0==stricmp("/L",argv[2]))
{
Restore_Partition_Tables(LIST);
exit(0);
}
printf("\nSyntax error, type \"UNFORMAT /?\" for help.\n");
exit(1);
}
else
{
Restore_Partition_Tables(FALSE);
exit(0);
}
}
}
/*if UNFORMAT is typed with a drive letter */
if(argc>=2)
{
char drive_letter[1];
char *input;
logical_drive=argv[1] [0];
if(logical_drive>=97) logical_drive = logical_drive - 97;
if(logical_drive>=65) logical_drive = logical_drive - 65;
if( (logical_drive<0) || (logical_drive> 25) )
{
printf("\nSyntax error, type \"UNFORMAT /?\" for help.\n");
exit(1);
}
drive_letter[0] = logical_drive+65;
drive_letter[1] = 58;
if(logical_drive>=2) drive_type=HARD;
else drive_type=FLOPPY;
Get_Drive_Parameters();
true=1;
true=stricmp("/j",argv[2]);
if(0==true) Verify_Drive_Mirror();
true=0;
if(0==true)
{
if(argc>=3)
{
int switch_to_test = (argc-1);
do
{
true=1;
true=stricmp("/u",argv[switch_to_test]);
if(0==true) unformat_without_mirror_file = 1;
true=1;
true=stricmp("/l",argv[switch_to_test]);
if(0==true) list_file_and_directory_names = 1;
true=1;
true=stricmp("/test",argv[switch_to_test]);
if(0==true) test = 1;
true=1;
true=stricmp("/p",argv[switch_to_test]);
if(0==true) print_output = 1;
switch_to_test--;
} while (switch_to_test>=2);
}
Unformat_Drive();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -