📄 pcompute.c
字号:
/*
// Program: Free FDISK
// Written By: Brian E. Reifsnyder
// Module: PCOMPUTE.C
// Module Description: Partition Computation and Modification Functions
// Version: 1.2.1
// Copyright: 1998-2002 under the terms of the GNU GPL, Version 2
*/
/*
/////////////////////////////////////////////////////////////////////////////
// DEFINES
/////////////////////////////////////////////////////////////////////////////
*/
#define PCOMPUTE
#define MAXFAT16NORM 2047
#define MAXFAT16LARGE 4095
/*
/////////////////////////////////////////////////////////////////////////////
// INCLUDES
/////////////////////////////////////////////////////////////////////////////
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include "cmd.h"
#include "main.h"
#include "fdiskio.h"
#include "pcompute.h"
#include "pdiskio.h"
#include "userint1.h"
/*
/////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
/////////////////////////////////////////////////////////////////////////////
*/
extern char **environ;
/*
/////////////////////////////////////////////////////////////////////////////
// PROTOTYPES
/////////////////////////////////////////////////////////////////////////////
*/
unsigned long Number_Of_Cylinders(unsigned long size);
/*
/////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
/////////////////////////////////////////////////////////////////////////////
*/
/* Clear the Active Partition */
void Clear_Active_Partition()
{
int index=0;
Partition_Table *pDrive = &part_table[flags.drive_number-0x80];
do
{
pDrive->pri_part[index].active_status=0x00;
index++;
}while(index<4);
pDrive->part_values_changed=TRUE;
flags.partitions_have_changed=TRUE;
}
/* Clear the Extended Partition Table Buffers */
void Clear_Extended_Partition_Table(int drive)
{
int index;
Partition_Table *pDrive = &part_table[drive];
pDrive->ptr_ext_part = NULL;
pDrive->ext_part_size_in_MB=0;
pDrive->ext_part_num_sect=0;
pDrive->ext_part_largest_free_space=0;
pDrive->log_drive_free_space_start_cyl=0;
pDrive->log_drive_free_space_end_cyl=0;
pDrive->log_drive_largest_free_space_location=0;
pDrive->num_of_log_drives=0;
pDrive->num_of_non_dos_log_drives=0;
index=0;
do
{
memset(&pDrive->log_drive[index],0,sizeof(pDrive->log_drive[0]));
pDrive->next_ext_exists[index]=FALSE;
memset(&pDrive->next_ext[index],0,sizeof(pDrive->next_ext[0]));
index++;
}while(index<23);
}
/* Create Logical Drive */
/* Returns a 0 if successful and a 1 if unsuccessful */
int Create_Logical_Drive(int numeric_type, long size_in_MB)
{
int index;
Partition_Table *pDrive = &part_table[flags.drive_number-0x80];
long computed_ending_cylinder;
long maximum_size_in_cylinders=pDrive->ext_part_largest_free_space;
long requested_size_in_cylinders=Number_Of_Cylinders(size_in_MB*2048);
unsigned long computed_partition_size;
unsigned long cylinder_size=(pDrive->total_head+1)*(pDrive->total_sect);
//Qprintf("Creating logical drive type %02x, size %lu MB\n",numeric_type, size_in_MB);
/* Adjust the size of the partition to fit boundaries, if necessary. */
if(requested_size_in_cylinders>maximum_size_in_cylinders)
requested_size_in_cylinders=maximum_size_in_cylinders;
/* If the requested size of the partition is close to the end of the */
/* maximum available space, fill the maximum available space. */
/* This ensures more aggressive use of the hard disk. */
if( (maximum_size_in_cylinders - 3) <= requested_size_in_cylinders)
requested_size_in_cylinders = maximum_size_in_cylinders;
/* Adjust the partition type, if necessary. */
numeric_type=Partition_Type_To_Create(
( ( (requested_size_in_cylinders+1)
*(pDrive->total_head+1)
*(pDrive->total_sect) ) / 2048 ),
numeric_type);
/* Compute the size of the partition. */
computed_partition_size=(requested_size_in_cylinders)*cylinder_size;
//Qprintf("calculated type is %02x\n",numeric_type);
/* Make space in the part_table structure, if necessary. */
if( pDrive->log_drive_largest_free_space_location <=pDrive->num_of_log_drives
&& pDrive->log_drive_largest_free_space_location >0 )
{
index=pDrive->num_of_log_drives+1;
do
{
memcpy(&pDrive->log_drive[index],&pDrive->log_drive[index-1],sizeof(pDrive->log_drive[0]));
pDrive->next_ext_exists[index]=pDrive->next_ext_exists[index-1];
memcpy(&pDrive->next_ext[index],&pDrive->next_ext[index-1],sizeof(pDrive->next_ext[0]));
index--;
}while(index>=pDrive->log_drive_largest_free_space_location);
}
/* Add the logical drive entry. */ /*????????????*/
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].num_type
=numeric_type;
strcpy(pDrive->log_drive[pDrive->log_drive_largest_free_space_location].vol_label,"");
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].start_cyl =pDrive->log_drive_free_space_start_cyl;
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].start_head=1;
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].start_sect=1;
/* Compute the ending cylinder */
computed_ending_cylinder
=pDrive->log_drive_free_space_start_cyl
+requested_size_in_cylinders-1;
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].end_cyl=computed_ending_cylinder;
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].end_head=pDrive->total_head;
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].end_sect=pDrive->total_sect;
if( pDrive->log_drive[pDrive->log_drive_largest_free_space_location].end_cyl>1023
&& pDrive->ext_int_13==TRUE )
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].num_type
=LBA_Partition_Type_To_Create(numeric_type);
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].rel_sect
=pDrive->total_sect;
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].num_sect
=computed_partition_size
-pDrive->total_sect;
pDrive->log_drive[pDrive->log_drive_largest_free_space_location].size_in_MB
=Convert_Sect_To_MB(computed_partition_size);
pDrive->num_of_log_drives++;
pDrive->log_drive_created[pDrive->log_drive_largest_free_space_location]=TRUE;
/* Add the linkage entry. */
/* Add the linkage entry if there is a logical drive after this one. */
if(pDrive->log_drive[pDrive->log_drive_largest_free_space_location+1].num_type>0)
{
pDrive->next_ext_exists[pDrive->log_drive_largest_free_space_location]=TRUE;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location].num_type=5;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location].start_cyl
=pDrive->log_drive[pDrive->log_drive_largest_free_space_location+1].start_cyl;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location].start_head=0;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location].start_sect=1;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location].end_cyl
=pDrive->log_drive[pDrive->log_drive_largest_free_space_location+1].end_cyl;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location].end_head
=pDrive->total_head;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location].end_sect
=pDrive->total_sect;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location].rel_sect
=( pDrive->log_drive[pDrive->log_drive_largest_free_space_location+1].start_cyl
-pDrive->ptr_ext_part->start_cyl) *
(pDrive->total_head+1)*pDrive->total_sect;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location].num_sect
=pDrive->log_drive[pDrive->log_drive_largest_free_space_location+1].num_sect
+pDrive->total_sect;
}
/* Add the linkage entry if there is a logical drive before this one. */
if(pDrive->log_drive_largest_free_space_location>0)
{
pDrive->next_ext_exists[pDrive->log_drive_largest_free_space_location-1]=TRUE;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].num_type=5;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].start_cyl
=pDrive->log_drive_free_space_start_cyl;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].start_head=0;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].start_sect=1;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].end_cyl
=computed_ending_cylinder;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].end_head
=pDrive->total_head;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].end_sect
=pDrive->total_sect;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].rel_sect
=( pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].start_cyl
- pDrive->ptr_ext_part->start_cyl )*
(pDrive->total_head+1)*pDrive->total_sect;
pDrive->next_ext[pDrive->log_drive_largest_free_space_location-1].num_sect=computed_partition_size;
}
pDrive->part_values_changed=TRUE;
flags.partitions_have_changed=TRUE;
#ifdef DEBUG
if(debug.create_partition==TRUE)
{
int offset;
Clear_Screen(NULL);
Print_Centered(1,"int Create_Logical_Drive(int numeric_type,long size_in_MB)",BOLD);
printAt(4,3,"int numeric_type=%d",numeric_type);
printAt(4,4,"long size_in_MB=%d",size_in_MB);
printAt(4,5,"Number of partition that was created: %d",pDrive->log_drive_largest_free_space_location);
printAt(4,7,"Brief logical drive table:");
index=pDrive->log_drive_largest_free_space_location-1;
offset=9;
printAt(4,8," # NT SC SH SS EC EH ES Rel. Sect. Size in MB ");
do
{
if( (index>=0) && (index<24) )
{
printAt( 4,offset,"%2d",index);
printAt( 7,offset,"%3d",pDrive->log_drive[index].num_type);
printAt(13,offset,"%4d",pDrive->log_drive[index].start_cyl);
printAt(19,offset,"%4d",pDrive->log_drive[index].start_head);
printAt(25,offset,"%4d",pDrive->log_drive[index].start_sect);
printAt(33,offset,"%4d",pDrive->log_drive[index].end_cyl);
printAt(38,offset,"%4d",pDrive->log_drive[index].end_head);
printAt(43,offset,"%4d",pDrive->log_drive[index].end_sect);
printAt(58,offset,"%d",pDrive->log_drive[index].rel_sect);
printAt(72,offset,"%5d",pDrive->log_drive[index].size_in_MB);
}
else
{
printAt(4,offset,"N/A");
}
offset++;
index++;
}while(offset<=11);
printAt(4,15,"Next extended location table:");
printAt(4,16," # SC SH SS EC EH ES Rel. Sect. Size in MB ");
index=pDrive->log_drive_largest_free_space_location-1;
offset=17;
do
{
if( (index>=0) && (index<24) && (pDrive->next_ext_exists[index]==TRUE) )
{
printAt( 4,offset,"%2d",index);
printAt(13,offset,"%4d",pDrive->next_ext[index].start_cyl);
printAt(19,offset,"%4d",pDrive->next_ext[index].start_head);
printAt(25,offset,"%4d",pDrive->next_ext[index].start_sect);
printAt(33,offset,"%4d",pDrive->next_ext[index].end_cyl);
printAt(38,offset,"%4d",pDrive->next_ext[index].end_head);
printAt(43,offset,"%4d",pDrive->next_ext[index].end_sect);
/*
Temporarily removed because the size of the relative sector field
exceeds that handled by the printf statement. As a result,
incorrect information is returned.
Position_Cursor(58,offset);
printf("%4d",pDrive->next_ext_rel_sect[index]);
*/
printAt(72,offset,"%4d",((pDrive->next_ext[index].num_sect)/2048));
}
else
{
printAt( 4,offset,"N/A");
}
offset++;
index++;
}while(offset<=19);
Pause();
}
#endif
return(0);
}
/* Create a Primary Partition */
/* Returns partition number if successful and a 99 if unsuccessful */
int Create_Primary_Partition(int numeric_type,long size_in_MB)
{
int index;
int empty_partition_number;
struct Partition *newPartition;
Partition_Table *pDrive = &part_table[flags.drive_number-0x80];
long computed_ending_cylinder;
long maximum_size_in_cylinders
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -