📄 fdiskio.c
字号:
/*
// Program: Free FDISK
// Module: FDISKIO.C
// Module Description: Disk input/output code module for functions specific
// to Free FDISK.
// Functions that access the hard disk and are specific
// to Free FDISK are in this module.
// Written By: Brian E. Reifsnyder
// Version: 1.2.1
// Copyright: 1998-2002 under the terms of the GNU GPL, Version 2
*/
/*
CATS message store for fdiskio.c:
$set 3
1
2
*/
/*
/////////////////////////////////////////////////////////////////////////////
// DEFINES
/////////////////////////////////////////////////////////////////////////////
*/
#define FDISKIO
/*
/////////////////////////////////////////////////////////////////////////////
// INCLUDES
/////////////////////////////////////////////////////////////////////////////
*/
#include <conio.h>
#include <dir.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "bootcode.h"
#include "main.h"
#include "fdiskio.h"
#include "pcompute.h"
#include "pdiskio.h"
/*
/////////////////////////////////////////////////////////////////////////////
// BOOTLOADER POINTERS
/////////////////////////////////////////////////////////////////////////////
*/
extern char booteasy_code[];
extern char bootnormal_code[];
/*
/////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
/////////////////////////////////////////////////////////////////////////////
*/
/* Automatically partition the selected hard drive */
void Automatically_Partition_Hard_Drive()
{
int index=0;
/* unsigned long maximum_partition_size_in_MB; */
Partition_Table *pDrive = &part_table[(flags.drive_number-128)];
Determine_Drive_Letters();
/* First, make sure no primary or extended partitions exist. */
/* Compaq diagnostic partitions are ok, though. */
do
{
if( (brief_partition_table[(flags.drive_number-128)] [index]>0) &&
(brief_partition_table[(flags.drive_number-128)][index]!=18) )
{
printf("\nThe hard drive has already been partitioned...Program Terminated.\n");
exit(7);
}
index++;
}while(index<4);
/* Create a primary partition...if the size or type is incorrect, */
/* int Create_Primary_Partition(...) will adjust it automatically. */
Determine_Free_Space();
Set_Active_Partition(Create_Primary_Partition(6,2048));
/* Create an extended partition, if space allows. */
Determine_Free_Space();
if( pDrive->pri_part_largest_free_space > 0)
{
Create_Primary_Partition(5,999999);
/* Fill the extended partition with logical drives. */
Determine_Free_Space();
do
{
Create_Logical_Drive(6,2048);
Determine_Free_Space();
/* maximum_partition_size_in_MB
= (pDrive->ext_part_largest_free_space+1)*
(pDrive->total_head+1)*
pDrive->total_sect / 2048; */
}while( (pDrive->ext_part_largest_free_space > 0)
&& (Determine_Drive_Letters()<'Z') );
}
}
/* Clear the first sector on the hard disk...removes the partitions and MBR. */
void Clear_Entire_Sector_Zero()
{
//Qprintf("Clearing boot sector of drive %x\n", flags.drive_number);
Clear_Sector_Buffer();
Write_Physical_Sectors(flags.drive_number, 0, 0, 1, 1);
}
/* Clear the Flag */
void Clear_Flag(int flag_number)
{
if(flags.flag_sector!=0)
{
Read_Physical_Sectors((flags.drive_number),0,0,(flags.flag_sector),1);
sector_buffer[(446+(flag_number-1))]=0;
Write_Physical_Sectors((flags.drive_number),0,0,(flags.flag_sector),1);
}
else
{
printf("\nSector flagging functions have been disabled...Program Terminated.\n");
exit(9);
}
}
/* Clear Partition Table */
void Clear_Partition_Table()
{
//Qprintf("Clearing partitiontable for drive %02x..",flags.drive_number);
Read_Physical_Sectors(flags.drive_number, 0, 0, 1, 1);
memset(sector_buffer+0x1be,0,4*16);
Write_Physical_Sectors(flags.drive_number, 0, 0, 1, 1);
//Qprintf("done\n");
}
/* Create Alternate Master Boot Code */
void Create_Alternate_MBR(void)
{
char home_path[255];
int index=0;
FILE *file_pointer;
//Qprintf("Create_Alternate_MBR()\n");
Read_Physical_Sectors(flags.drive_number,0,0,1,1);
/* Clear old MBR, if any */
memset(sector_buffer,0x00,0x1be);
strcpy(home_path,path);
strcat(home_path,"boot.mbr");
/* Search the directory Free FDISK resides in before searching the PATH */
/* in the environment for the boot.mbr file. */
file_pointer=fopen(home_path,"rb");
if(!file_pointer) file_pointer=fopen(searchpath("boot.mbr"),"rb");
if(!file_pointer)
{
printf("\nThe \"boot.mbr\" file has not been found...Program Terminated.\n");
exit(8);
}
index=0;
do
{
sector_buffer[index]=fgetc(file_pointer);
index++;
}while(index<0x1be);
fclose(file_pointer);
sector_buffer[0x1fe]=0x55;
sector_buffer[0x1ff]=0xaa;
Write_Physical_Sectors(flags.drive_number,0,0,1,1);
}
/* Create Booteasy MBR */
void Create_BootEasy_MBR(void)
{
//Qprintf("Create_BootEasy_MBR()\n");
Read_Physical_Sectors(flags.drive_number,0,0,1,1);
memcpy(sector_buffer,booteasy_code,SIZE_OF_MBR);
sector_buffer[0x1fe]=0x55;
sector_buffer[0x1ff]=0xaa;
Write_Physical_Sectors(flags.drive_number,0,0,1,1);
}
/* Create Normal MBR */
void Create_BootNormal_MBR(void)
{
//Qprintf("Creating normal MBR\n");
Read_Physical_Sectors(flags.drive_number,0,0,1,1);
memcpy(sector_buffer,bootnormal_code,SIZE_OF_MBR);
sector_buffer[0x1fe]=0x55;
sector_buffer[0x1ff]=0xaa;
Write_Physical_Sectors(flags.drive_number,0,0,1,1);
}
/* Create Master Boot Code */
void Create_MBR(void)
{
if(flags.use_ambr==TRUE)
{
Create_Alternate_MBR();
}
else
{
Create_BootNormal_MBR(); /* BootEasy disabled */
}
}
/* Create Master Boot Code if it is not present */
void Create_MBR_If_Not_Present()
{
Read_Physical_Sectors(0x80,0,0,1,1);
if( (sector_buffer[0x1fe]!=0x55) && (sector_buffer[0x1ff]!=0xaa) )
{
Create_MBR();
}
}
/* Load External Partition Type Lookup Table */
void Load_External_Lookup_Table()
{
int index=0;
int offset=0;
int sub_index=0;
long line_counter=1;
FILE *file;
char character_number[5];
char home_path[255];
char line_buffer[256];
/* Clear the buffers */
do
{
sub_index=0;
do
{
partition_lookup_table_buffer_short[index] [sub_index]=0;
sub_index++;
}while(sub_index<9);
sub_index=0;
do
{
partition_lookup_table_buffer_long[index] [sub_index]=0;
sub_index++;
}while(sub_index<16);
index++;
}while(index<256);
index=0;
strcpy(home_path,path);
strcat(home_path,"fdiskpt.ini");
/* Search the directory Free FDISK resides in before searching the PATH */
/* in the environment for the part.cfg file. */
file=fopen(home_path,"rt");
if(!file) file=fopen(searchpath("fdiskpt.ini"),"rt");
flags.partition_type_lookup_table=INTERNAL;
if(file)
{
while(fgets(line_buffer,255,file) !=NULL)
{
line_counter++;
if (0==strncmp(line_buffer,"end",3) ||
0==strncmp(line_buffer,"END",3) ||
0==strncmp(line_buffer,"999",3) )
break;
if( 0==strncmp(line_buffer,";",1) ||
line_buffer[0] ==0x0a)
continue;
/* Determine what partition type this line is referring to. */
character_number[3]=0;
if(line_buffer[0]=='0')
{
character_number[0]=line_buffer[1];
character_number[1]=line_buffer[2];
character_number[2]=0;
}
else
{
character_number[0]=line_buffer[0];
character_number[1]=line_buffer[1];
character_number[2]=line_buffer[2];
}
index=atoi(character_number);
if( (index<0) || (index>255) )
{
printf("\nPartition type out of range in line %d of \"fdiskpt.ini\"...Program Terminated.\n",line_counter);
exit(9);
}
/* Load the short description buffer (8) */
offset=4;
do
{
/* */
partition_lookup_table_buffer_short[index] [(offset-4)]=line_buffer[offset];
offset++;
} while(offset<=11);
/* Load the long description buffer (15) */
offset=13;
do
{
partition_lookup_table_buffer_long[index] [(offset-13)]=line_buffer[offset];
offset++;
}while(offset<=27);
index++;
}
fclose(file);
flags.partition_type_lookup_table=EXTERNAL;
}
}
/* Read and process the fdisk.ini file */
void Process_Fdiskini_File()
{
// char char_number[2];
char command_buffer[20];
char home_path[255];
char line_buffer[256];
char setting_buffer[20];
int index=0;
int command_ok=FALSE;
int done_looking=FALSE;
int end_of_file_marker_encountered=FALSE;
int number;
int object_found=FALSE;
int sub_buffer_index=0;
long line_counter=1;
// long setting;
FILE *file;
/* Set values to UNCHANGED */
#ifdef DEBUG
debug.all=UNCHANGED;
debug.command_line_arguments=UNCHANGED;
debug.create_partition=UNCHANGED;
debug.determine_free_space=UNCHANGED;
debug.emulate_disk=UNCHANGED;
debug.input_routine=UNCHANGED;
debug.lba=UNCHANGED;
debug.path=UNCHANGED;
debug.read_sector=UNCHANGED;
debug.write=UNCHANGED;
#endif
flags.allow_4gb_fat16=UNCHANGED;
flags.allow_abort=UNCHANGED;
flags.check_for_extra_cylinder=UNCHANGED;
flags.del_non_dos_log_drives=UNCHANGED;
flags.extended_options_flag=UNCHANGED;
flags.flag_sector=UNCHANGED;
flags.monochrome=UNCHANGED;
flags.label=UNCHANGED;
flags.reboot=UNCHANGED;
flags.screen_color=UNCHANGED;
flags.set_any_pri_part_active=UNCHANGED;
flags.use_ambr=UNCHANGED;
flags.version=UNCHANGED;
flags.use_freedos_label=FALSE;
strcpy(home_path,path);
strcat(home_path,"fdisk.ini");
/* Search the directory Free FDISK resides in before searching the PATH */
/* in the environment for the fdisk.ini file. */
file=fopen(home_path,"rt");
if(!file) file=fopen(searchpath("fdisk.ini"),"rt");
if(file)
{
while(fgets(line_buffer,255,file) !=NULL)
{
if( (0!=strncmp(line_buffer,";",1))
&& (0!=strncmp(line_buffer,"end",3))
&& (0!=strncmp(line_buffer,"END",3))
&& (0!=strncmp(line_buffer,"999",3))
&& (line_buffer[0]!=0x0a)
&& (end_of_file_marker_encountered==FALSE) )
{
/* Clear the command_buffer and setting_buffer */
index=0;
do
{
command_buffer[index]=0x00;
setting_buffer[index]=0x00;
index++;
}while(index<20);
/* Extract the command and setting from the line_buffer */
/* Find the command */
index=0;
sub_buffer_index=0;
done_looking=FALSE;
object_found=FALSE;
do
{
if( (line_buffer[index]!='=')
&& ( (line_buffer[index]>=0x30)
&& (line_buffer[index]<=0x7a) ) )
{
object_found=TRUE;
command_buffer[sub_buffer_index]=line_buffer[index];
sub_buffer_index++;
}
if( (object_found==TRUE)
&& ( (line_buffer[index]=='=')
|| (line_buffer[index]==' ') ) )
{
//command_buffer[sub_buffer_index]=0x0a;
done_looking=TRUE;
}
if( (index==254) || (line_buffer[index]==0x0a) )
{
printf("Error encountered on line %d of the \"fdisk.ini\" file...Program Terminated.\n",line_counter);
exit(3);
}
index++;
}while(done_looking==FALSE);
/* Find the setting */
sub_buffer_index=0;
object_found=FALSE;
done_looking=FALSE;
do
{
if( (line_buffer[index]!='=')
&& ( ( (line_buffer[index]>=0x30)
&& (line_buffer[index]<=0x7a) )
|| (line_buffer[index]=='-') ) )
{
object_found=TRUE;
setting_buffer[sub_buffer_index]=line_buffer[index];
sub_buffer_index++;
}
if( (object_found==TRUE)
&& ( (line_buffer[index]==0x0a)
|| ( line_buffer[index]==' ') ) )
{
done_looking=TRUE;
//setting_buffer[sub_buffer_index]=0x0a;
}
if(index==254)
{
printf("Error encountered on line %d of the \"fdisk.ini\" file...Program Terminated.\n",line_counter);
exit(3);
}
index++;
}while(done_looking==FALSE);
/* Adjust for the possibility of TRUE or FALSE in the fdisk.ini file. */
if(0==stricmp(setting_buffer,"TRUE")) strcpy(setting_buffer,"ON");
if(0==stricmp(setting_buffer,"FALSE")) strcpy(setting_buffer,"OFF");
/* Process the command found in the line buffer */
command_ok=FALSE;
/* Check for the ALLOW_4GB_FAT16 statement */
if(0==stricmp(command_buffer,"ALLOW_4GB_FAT16"))
{
if(0==stricmp(setting_buffer,"ON")) flags.allow_4gb_fat16=TRUE;
if(0==stricmp(setting_buffer,"OFF")) flags.allow_4gb_fat16=FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -