📄 unformat.cpp
字号:
/*
// Program: Free Unformat
// Version: 0.8
// Written By: Brian E. Reifsnyder
// Copyright 1999 under the terms of the GNU GPL by Brian E. Reifsnyder
//
// Note: This program is free and is without a warranty of any kind.
//
*/
/*
/////////////////////////////////////////////////////////////////////////////
// INCLUDES
/////////////////////////////////////////////////////////////////////////////
*/
#include <bios.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
/////////////////////////////////////////////////////////////////////////////
// DEFINES
/////////////////////////////////////////////////////////////////////////////
*/
#define FOUND 1
#define LIST 1
#define TRUE 1
#define FALSE 0
#define NULL 0
#define PERCENTAGE 1
#define PRIMARY 0
#define EXTENDED 1
#define HARD 0
#define FLOPPY 1
#define FAT12 0
#define FAT16 1
#define FAT32 2
/*
/////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
/////////////////////////////////////////////////////////////////////////////
*/
int list_file_and_directory_names = 0;
int print_output = 0;
int test = 0;
int unformat_without_mirror_file = 0;
long sectors_per_fat;
/* Physical Drive Parameters */
int physical_drive;
int logical_drive;
unsigned long logical_sector_offset;
long total_cylinders;
long total_heads;
long total_sectors;
long floppy_drive_type;
int drive_type;
int media_descriptor;
/* Logical drive information */
long sectors_in_root_dir;
int type_of_fat;
unsigned long number_of_logical_sectors_on_drive;
long number_of_root_directory_entries;
/* Extracted cylinder & sector from partition table */
unsigned long g_cylinder;
unsigned long g_sector;
/* Partition Information */
int numeric_partition_type[24];
unsigned long starting_cylinder[24];
unsigned long starting_head[24];
unsigned long starting_sector[24];
unsigned long ending_cylinder[24];
unsigned long ending_head[24];
unsigned long ending_sector[24];
int relative_sectors_1[24];
int relative_sectors_2[24];
int relative_sectors_3[24];
int relative_sectors_4[24];
int partition_size_1[24];
int partition_size_2[24];
int partition_size_3[24];
int partition_size_4[24];
unsigned long partition_size[24];
int number_of_partitions;
/* "Logical Drive to Format" translation information */
int physical_drive_number;
unsigned long partition_starting_cylinder;
unsigned long partition_starting_head;
unsigned long partition_starting_sector;
unsigned long partition_ending_cylinder;
unsigned long partition_ending_head;
unsigned long partition_ending_sector;
unsigned long drive_maximum_cylinders;
unsigned long drive_maximum_heads;
unsigned long drive_maximum_sectors;
unsigned long total_logical_sectors;
unsigned long total_physical_sectors;
int number_of_sectors_per_cluster;
int partition_on_hard_disk;
/* Translated values from "void Convert_Logical_To_Physical" */
unsigned long translated_head;
unsigned long translated_cylinder;
unsigned long translated_sector;
/* Misc variables */
unsigned long integer1;
unsigned long integer2;
unsigned long integer3;
unsigned long integer4;
/* Buffers */
unsigned char mirror_map[5120];
unsigned char control_buffer[512];
unsigned char sector_buffer[512];
/*
/////////////////////////////////////////////////////////////////////////////
// PROTOTYPES
/////////////////////////////////////////////////////////////////////////////
*/
int Compare_Sector_Buffers();
unsigned long Decimal_Number(unsigned long hex1, unsigned long hex2, unsigned long hex3, unsigned long hex4);
void Copy_Sector_Into_Control_Buffer();
void Display_Help_Screen();
void List_Names();
void Read_Mirror_Map();
void Restore_Partition_Tables(int list);
void Unformat_Drive();
void Verify_Drive_Mirror();
//////////////
void Clear_Sector_Buffer();
void Convert_Huge_To_Integers(unsigned long number);
void Convert_Logical_To_Physical(unsigned long sector);
void Extract_Cylinder_and_Sector(unsigned long hex1, unsigned long hex2);
void Get_Drive_Parameters();
void Get_Physical_Floppy_Drive_Parameters();
void Get_Physical_Hard_Drive_Parameters();
void Read_Partition_Table();
void Read_Physical_Sector(int drive, int head, long cyl, int sector);
void Read_Sector(unsigned long sector);
void Reset_Drive();
void Write_Physical_Sector(int drive, int head, long cyl, int sector);
void Write_Sector(unsigned long sector);
/*
/////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
/////////////////////////////////////////////////////////////////////////////
*/
/* Clear Sector Buffer */
void Clear_Sector_Buffer()
{
long loop=0;
do
{
sector_buffer[loop]=0;
loop++;
}while(loop<512);
}
/* Convert a logical sector to a physical drive location */
void Convert_Logical_To_Physical(unsigned long sector)
{
unsigned long remaining;
if(drive_type==HARD) sector=sector+logical_sector_offset;
translated_cylinder=sector/(total_sectors*(total_heads+1));
remaining=sector % (total_sectors*(total_heads+1));
translated_head=remaining/(total_sectors);
translated_sector=remaining % (total_sectors);
translated_sector++;
}
/* Compare Sector Buffers ///////////// */
int Compare_Sector_Buffers()
{
int answer=0;
int compare=0;
int loop = 0;
do
{
compare=control_buffer[loop]-sector_buffer[loop];
if(compare!=0) answer = 1;
loop++;
} while(loop<512);
return(answer);
}
/* Copy sector into control buffer //// */
void Copy_Sector_Into_Control_Buffer()
{
int loop=0;
do
{
control_buffer[loop]=sector_buffer[loop];
loop++;
} while(loop<=512);
}
/* Convert Hexidecimal Number to a Decimal Number */
unsigned long Decimal_Number(unsigned long hex1, unsigned long hex2, unsigned long hex3, unsigned long hex4)
{
return((hex1) + (hex2*256) + (hex3*65536) + (hex4*16777216));
}
/* Extract Cylinder & Sector */
void Extract_Cylinder_and_Sector(unsigned long hex1, unsigned long hex2)
{
unsigned long cylinder_and_sector = ( (256*hex2) + hex1 );
g_sector = cylinder_and_sector % 64;
g_cylinder = ( ( (cylinder_and_sector*4) & 768) + (cylinder_and_sector /256) );
}
/* Get Drive Parameters */
/* The physical drive parameters are necessary to allow this program */
/* to bypass the DOS interrupts. */
void Get_Drive_Parameters()
{
int drives_found_flag;
long hard_disk_installed[8];
int increment_heads=FALSE;
int logical_drive_found=FALSE;
int partition_number_of_logical_drive;
int pointer=0;
int possible_logical_drive;
int possible_physical_hard_disk;
int sub_pointer=0;
int result;
/* Brief partition table variables */
long partition_type_table[8][26];
long maximum_partition_assigned[8];
if(drive_type==FLOPPY)
{
physical_drive=logical_drive;
Get_Physical_Floppy_Drive_Parameters();
partition_starting_cylinder=0;
partition_starting_head=0;
partition_starting_sector=1;
partition_ending_cylinder=total_cylinders;
partition_ending_head=total_heads;
partition_ending_sector=total_sectors;
drive_maximum_cylinders=total_cylinders;
drive_maximum_heads=1;
drive_maximum_sectors=total_sectors;
total_logical_sectors=(total_cylinders+1)*(total_heads+1)*total_sectors;
total_physical_sectors=total_logical_sectors;
number_of_sectors_per_cluster=1;
}
else
{
/* Get availability of physical hard drives and */
/* assemble a brief partition table. */
/* Initialize partition_type_table */
pointer=0;
do
{
partition_type_table[0][pointer]=0;
partition_type_table[1][pointer]=0;
partition_type_table[2][pointer]=0;
partition_type_table[3][pointer]=0;
partition_type_table[4][pointer]=0;
partition_type_table[5][pointer]=0;
partition_type_table[6][pointer]=0;
partition_type_table[7][pointer]=0;
pointer++;
}while(pointer<=26);
/* Initialize maximum_partition_assigned */
maximum_partition_assigned[0]=0;
maximum_partition_assigned[1]=0;
maximum_partition_assigned[2]=0;
maximum_partition_assigned[3]=0;
maximum_partition_assigned[4]=0;
maximum_partition_assigned[5]=0;
maximum_partition_assigned[6]=0;
maximum_partition_assigned[7]=0;
pointer=128;
drives_found_flag=FALSE;
do
{
result=biosdisk(2, pointer, 0, 0, 1, 1, sector_buffer);
if(result==0)
{
drives_found_flag=TRUE;
hard_disk_installed[pointer-128]=TRUE;
physical_drive=pointer;
Read_Partition_Table();
sub_pointer=0;
do
{
partition_type_table[pointer-128][sub_pointer]=numeric_partition_type[sub_pointer];
sub_pointer++;
}while(sub_pointer<=number_of_partitions);
maximum_partition_assigned[pointer-128]=number_of_partitions;
}
else hard_disk_installed[pointer-128]=FALSE;
pointer++;
}while(pointer<136);
/* If there aren't any physical hard drives available, exit program */
if(drives_found_flag==FALSE)
{
printf("\nNo hard disks found...operation terminated.\n");
exit(4);
}
/* Determine on which physical hard drive the logical drive is */
/* and on which partition it is located. */
/* Check on primary partitions */
possible_logical_drive=1;
possible_physical_hard_disk=128;
do
{
if(hard_disk_installed[possible_physical_hard_disk-128]==TRUE)
{
pointer=0;
do
{
if( (partition_type_table[possible_physical_hard_disk-128][pointer]==0x01) || (partition_type_table[possible_physical_hard_disk-128][pointer]==0x04) || (partition_type_table[possible_physical_hard_disk-128][pointer]==0x06) )
{
possible_logical_drive++;
if(logical_drive==possible_logical_drive)
{
physical_drive=possible_physical_hard_disk;
partition_number_of_logical_drive=pointer;
logical_drive_found=TRUE;
}
}
pointer++;
}while(pointer<4);
}
possible_physical_hard_disk++;
}while(possible_physical_hard_disk<136);
/* Check on extended partitions */
possible_physical_hard_disk=128;
if(logical_drive_found==FALSE)
{
do
{
if( (hard_disk_installed[possible_physical_hard_disk-128]==TRUE) && (maximum_partition_assigned[possible_physical_hard_disk-128]>=4) )
{
pointer=4;
do
{
if( (partition_type_table[possible_physical_hard_disk-128][pointer]==0x01) || (partition_type_table[possible_physical_hard_disk-128][pointer]==0x04) || (partition_type_table[possible_physical_hard_disk-128][pointer]==0x06) )
{
possible_logical_drive++;
if(logical_drive==possible_logical_drive)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -