Creating Multiple PRI DOS Partitions and Hiding Them with C Language
Nantong Institute of Scientific and Technical Information Li Zhaokun
1. Introduction
Under the DOS system, in computer application training, different training requirements call for different software. Because of students' mistaken operations, the software and important data stored on the hard disk are easily deleted illegally or damaged. In response to this, through in-depth analysis and practice in hard disk management, I have summed up a new effective method for backing up and protecting important data and software on a hard disk: create multiple PRI DOS partitions; use different PRI DOS partitions for different training requirements; when in use, only one PRI DOS partition is available at a time, and the remaining PRI DOS partitions are all hidden.
2. Partition table structure
As everyone knows, under the DOS operating system, one hard disk can be divided into two major parts: PRI DOS partitions and extended partitions, and multiple logical partitions can be further created inside the extended partition. These PRI DOS partitions and logical partitions can all be used like separate physical hard disks. So how does DOS manage and use these partitions? The secret of DOS hard disk management is two tables: the hard disk partition table chain. During system startup, DOS establishes the disk parameter table (BPB table) for each partition according to the hard disk partition table chain and the partition information provided by the partition table, and the disk parameter table is the basis for DOS to access the hard disk. Therefore, the partition table chain and partition table have a very important place in hard disk access.
The partition table chain is stored on the hard disk and generally consists of one master boot node and multiple ordinary nodes. The master boot node corresponds to the PRI DOS partition and is cylinder 0, head 0, sector 1 of the hard disk, that is, the hard disk master boot record sector. In the head node sector, from the beginning to 0DAH, 218 bytes are a section of master boot program; from 0DBH to 1BDH, a total of 228 bytes are 00H; from 1BEH to 1FDH, 64 bytes are the hard disk master partition table, with a total of four entries, each 16 bytes. The first two entries respectively indicate the information of the primary partition and the extended partition on the hard disk. The last two entries are generally unused and are all 00H; the last two bytes of the sector are the end markers 55H and AAH.
The data structure of a table entry is as follows:
Relative offset Length (BYTE) Meaning
0 1 Active flag
1 3 Partition start position (cylinder, head, sector)
4 1 Partition type
5 3 Partition end position (cylinder, head, sector)
8 4 Relative serial number of the partition's starting sector
12 4 Partition size
In the table entry structure, the active flag is 80H when active (otherwise 00H). The so-called start position, for this partition table entry, is the partition's starting cylinder 1 head 1 sector; for the extended entry, it refers to the corresponding extended partition's starting cylinder 0 head 1 sector. Common values for partition type are 1, 4, 6, 5, etc. 1 means a 12-bit FAT partition, 4 means a 16-bit FAT partition, 6 means a partition with capacity greater than 32M, and 5 means an extended partition. Partition size equals the number of sectors from the start sector to the end sector of each corresponding partition. For this partition table entry, this value does not include hidden sectors; for an extended entry, this value includes hidden sectors; and for the extended entry inside a PRI DOS partition, this value equals the sum of all sectors of each logical partition including hidden sectors. As for the so-called relative serial number of the starting sector, there are three cases for its relative starting point: for this partition table entry, the serial number is relative to the partition's starting cylinder 0 head 1 sector; for an extended entry, if it is the extended partition of PRI DOS, then it is relative to the master boot record sector of the PRI DOS partition; if it is the extension of a logical partition, then it is always relative to the start of the entire extended partition, which is generally the starting cylinder 0 head 1 sector of the first logical partition.
3. Creating multiple PRI DOS partitions
From the above analysis we can clearly see that the partition table is composed of four entries (although generally at most only two are used), and the first entry of the master partition table points to the PRI DOS partition, while the second entry points to the extended partition. If used normally, there is absolutely no need to set four entries. Since it is set this way, there must be a purpose to its use. Because some items in the extended entry in the master partition table contain information related to all extended partitions, I first use FDISK to create an extended partition containing only one logical partition. Then change the partition type of that extended partition entry from 5 to 6, and then change its active flag from 00H to 80H (note that at the same time, the original PRI DOS partition's active flag must be changed from 80H to 00H). Then the machine must be rebooted from floppy and drive C formatted, and then the newly created PRI DOS partition can be used to boot the machine (the original PRI DOS partition's drive letter becomes D). In this way two PRI DOS partitions are created.
4. Setting and restoring hidden partitions
We already know that in the hard disk master boot record sector, from 0DBH to 1BDH, a total of 228 bytes are 00H. After multiple PRI DOS partitions have been created, the last 64 bytes of these 228 bytes can be used to save the contents of four entries. Select one PRI DOS partition, then change to 00H the entire contents of all other entries among the four entries whose partition types are non-extended partitions. In this way there will be only one PRI DOS partition on the hard disk, and the others are hidden.
If you want to restore a hidden partition to normal, you only need to write back the contents saved in the main hard disk's master boot sector into that entry.
5. Source program
The source program is given below: PDOS.CPP. This source program was compiled on a compatible PC under the TURBOC++ 3.0 compile environment. Usage is to type at the DOS prompt:
PDOS (S: create a PRI DOS partition; S: back up PRI DOS partition table entries; R: restore PRI DOS partition table entries; 1: keep only the first PRI DOS partition table entry, hide all the rest; 2: keep only the second PRI DOS partition table entry; 3: keep only the third PRI DOS partition table entry; 4: keep only the last PRI DOS partition table entry).
The following is the PDOS.CPP program listing:
#include
#include
#include
#include
#include
#include
struct HSC
{char hd,sc,cy;};
struct PartitionTable
{char Flag; /* Active flag */
HSC begin; /* Start position */
char TYpe; /* Partition type */
HSC end; /* End position */
long id; /* Relative serial number of starting sector */
long size; /* Partition size */
};
struct note /* Node structure */
{char boot1;
PartitionTable PT;
PartitionTable PT1;
PartitionTable PT2; /* Master DOS partition table entries */
char f;
}dosboot;
void create_pri_dos(); /* Create (modify the extended partition into) a primary DOS partition */
void set_hidde_dos(); /* Back up master DOS partition table entries */
void recall_dos(); /* Restore master DOS partition table entries */
void change_dos(char n); /* Keep only one master DOS partition, hide all the rest */
main(int argc,char *argv)
{
char n;
if (argc!=2)
{ printf("Bad connamd !"
;
return 0;
};
strlwr(argv); /* Convert uppercase letters in the parameter to lowercase */
while((*(argv++))==0); /* Search for the first non-space character */
n=*(--argv);
switch(n){
case 'c':
create_pri_dos();
break;
case 's':
set_hidde_dos();
break;
case 'r':
recall_dos();
break;
case '1': /* Keep only the first entry in the master DOS partition table; hide all other master DOS partition table entries */
change_dos(n);
break;
case '2': /* Keep only the second entry in the master DOS partition table */
change_dos(n);
break;
case '3': /* Keep only the third entry in the master DOS partition table */
change_dos(n);
break;
case '4': /* Keep only the fourth entry in the master DOS partition table */
change_dos(n);
break;
default:
return 0;
};
return 0;
}
void create_pri_dos()
{int i;
biosdisk(2,0x80,0,0,1,1,&dosboot);
for (i=0;i<4;i++)
{if ((dosboot.PT2.TYpe!=0)&&(dosboot.PT2.TYpe!=5))
{dosboot.PT2.Flag=0x00;};
if (dosboot.PT2.TYpe==5)
{dosboot.PT2.TYpe=6;
dosboot.PT2.Flag=0x80;};
};
biosdisk(3,0x80,0,0,1,1,&dosboot);
return;
};
void set_hidde_dos()
{
int i;
biosdisk(2,0x80,0,0,1,1,&dosboot);
for(i=0;i<4;i++)
{if ((dosboot.PT2.TYpe!=0)&&(dosboot.PT2.TYpe!=5))
{dosboot.PT1=dosboot.PT2;};
};
biosdisk(3,0x80,0,0,1,1,&dosboot);
return;
};
void recall_dos()
{
int i;
biosdisk(2,0x80,0,0,1,1,&dosboot);
for (i=0;i<4;i++)
{if (dosboot.PT1.TYpe!='0'
{
dosboot.PT2=dosboot.PT1;
dosboot.PT1=dosboot.PT;
};
};
biosdisk(3,0x80,0,0,1,1,&dosboot);
return;
};
void change_dos(char n)
{
int i,m;
m=n-49;
biosdisk(2,0x80,0,0,1,1,&dosboot);
if (dosboot.PT1.TYpe!=0)
{ for (i=0;i<4;i++)
{if (dosboot.PT1.TYpe!=0)
{dosboot.PT2=dosboot.PT;};
};
dosboot.PT2=dosboot.PT1;
dosboot.PT2.Flag=0x80;
};
biosdisk(3,0x80,0,0,1,1,&dosboot);
return;
};
6. Example and points to note
An example of creating three PRI DOS partitions on a 1.2G hard disk, with sizes 300Mb, 400M, and 500MB respectively:
First use FDISK to create one 300MB PRI DOS partition and one 400MB extended partition, and activate the PRI DOS partition. Boot the computer from floppy and format drive C, then execute the PDOS S command to turn the extended partition into a PRI DOS partition. Then boot the computer from floppy again and format drive C, and the original PRI DOS partition's drive letter becomes D. Then use FDISK to create a 500MB extended partition. After executing the PDOS S command, boot the computer from floppy again and format drive C. In this way three PRI DOS partitions are created. The correspondence between drive letters and capacities is: C: 500MB; D: 300MB; E: 400MB.
Since when switching between PRI DOS partition table entries only one entry is kept available for use, PDOS.EXE must be copied into all PRI DOS partitions. In addition, readers using this program must enter the source program accurately and without error, in order to avoid damaging the data on the hard disk.
Mailing address: Li Zhaokun, 17 Yaogang Road, Nantong City, Jiangsu Province 226006
Telephone: 5516018
E-mail: lzk@mail.info.net.cn
Nantong Institute of Scientific and Technical Information Li Zhaokun
1. Introduction
Under the DOS system, in computer application training, different training requirements call for different software. Because of students' mistaken operations, the software and important data stored on the hard disk are easily deleted illegally or damaged. In response to this, through in-depth analysis and practice in hard disk management, I have summed up a new effective method for backing up and protecting important data and software on a hard disk: create multiple PRI DOS partitions; use different PRI DOS partitions for different training requirements; when in use, only one PRI DOS partition is available at a time, and the remaining PRI DOS partitions are all hidden.
2. Partition table structure
As everyone knows, under the DOS operating system, one hard disk can be divided into two major parts: PRI DOS partitions and extended partitions, and multiple logical partitions can be further created inside the extended partition. These PRI DOS partitions and logical partitions can all be used like separate physical hard disks. So how does DOS manage and use these partitions? The secret of DOS hard disk management is two tables: the hard disk partition table chain. During system startup, DOS establishes the disk parameter table (BPB table) for each partition according to the hard disk partition table chain and the partition information provided by the partition table, and the disk parameter table is the basis for DOS to access the hard disk. Therefore, the partition table chain and partition table have a very important place in hard disk access.
The partition table chain is stored on the hard disk and generally consists of one master boot node and multiple ordinary nodes. The master boot node corresponds to the PRI DOS partition and is cylinder 0, head 0, sector 1 of the hard disk, that is, the hard disk master boot record sector. In the head node sector, from the beginning to 0DAH, 218 bytes are a section of master boot program; from 0DBH to 1BDH, a total of 228 bytes are 00H; from 1BEH to 1FDH, 64 bytes are the hard disk master partition table, with a total of four entries, each 16 bytes. The first two entries respectively indicate the information of the primary partition and the extended partition on the hard disk. The last two entries are generally unused and are all 00H; the last two bytes of the sector are the end markers 55H and AAH.
The data structure of a table entry is as follows:
Relative offset Length (BYTE) Meaning
0 1 Active flag
1 3 Partition start position (cylinder, head, sector)
4 1 Partition type
5 3 Partition end position (cylinder, head, sector)
8 4 Relative serial number of the partition's starting sector
12 4 Partition size
In the table entry structure, the active flag is 80H when active (otherwise 00H). The so-called start position, for this partition table entry, is the partition's starting cylinder 1 head 1 sector; for the extended entry, it refers to the corresponding extended partition's starting cylinder 0 head 1 sector. Common values for partition type are 1, 4, 6, 5, etc. 1 means a 12-bit FAT partition, 4 means a 16-bit FAT partition, 6 means a partition with capacity greater than 32M, and 5 means an extended partition. Partition size equals the number of sectors from the start sector to the end sector of each corresponding partition. For this partition table entry, this value does not include hidden sectors; for an extended entry, this value includes hidden sectors; and for the extended entry inside a PRI DOS partition, this value equals the sum of all sectors of each logical partition including hidden sectors. As for the so-called relative serial number of the starting sector, there are three cases for its relative starting point: for this partition table entry, the serial number is relative to the partition's starting cylinder 0 head 1 sector; for an extended entry, if it is the extended partition of PRI DOS, then it is relative to the master boot record sector of the PRI DOS partition; if it is the extension of a logical partition, then it is always relative to the start of the entire extended partition, which is generally the starting cylinder 0 head 1 sector of the first logical partition.
3. Creating multiple PRI DOS partitions
From the above analysis we can clearly see that the partition table is composed of four entries (although generally at most only two are used), and the first entry of the master partition table points to the PRI DOS partition, while the second entry points to the extended partition. If used normally, there is absolutely no need to set four entries. Since it is set this way, there must be a purpose to its use. Because some items in the extended entry in the master partition table contain information related to all extended partitions, I first use FDISK to create an extended partition containing only one logical partition. Then change the partition type of that extended partition entry from 5 to 6, and then change its active flag from 00H to 80H (note that at the same time, the original PRI DOS partition's active flag must be changed from 80H to 00H). Then the machine must be rebooted from floppy and drive C formatted, and then the newly created PRI DOS partition can be used to boot the machine (the original PRI DOS partition's drive letter becomes D). In this way two PRI DOS partitions are created.
4. Setting and restoring hidden partitions
We already know that in the hard disk master boot record sector, from 0DBH to 1BDH, a total of 228 bytes are 00H. After multiple PRI DOS partitions have been created, the last 64 bytes of these 228 bytes can be used to save the contents of four entries. Select one PRI DOS partition, then change to 00H the entire contents of all other entries among the four entries whose partition types are non-extended partitions. In this way there will be only one PRI DOS partition on the hard disk, and the others are hidden.
If you want to restore a hidden partition to normal, you only need to write back the contents saved in the main hard disk's master boot sector into that entry.
5. Source program
The source program is given below: PDOS.CPP. This source program was compiled on a compatible PC under the TURBOC++ 3.0 compile environment. Usage is to type at the DOS prompt:
PDOS (S: create a PRI DOS partition; S: back up PRI DOS partition table entries; R: restore PRI DOS partition table entries; 1: keep only the first PRI DOS partition table entry, hide all the rest; 2: keep only the second PRI DOS partition table entry; 3: keep only the third PRI DOS partition table entry; 4: keep only the last PRI DOS partition table entry).
The following is the PDOS.CPP program listing:
#include
#include
#include
#include
#include
#include
struct HSC
{char hd,sc,cy;};
struct PartitionTable
{char Flag; /* Active flag */
HSC begin; /* Start position */
char TYpe; /* Partition type */
HSC end; /* End position */
long id; /* Relative serial number of starting sector */
long size; /* Partition size */
};
struct note /* Node structure */
{char boot1;
PartitionTable PT;
PartitionTable PT1;
PartitionTable PT2; /* Master DOS partition table entries */
char f;
}dosboot;
void create_pri_dos(); /* Create (modify the extended partition into) a primary DOS partition */
void set_hidde_dos(); /* Back up master DOS partition table entries */
void recall_dos(); /* Restore master DOS partition table entries */
void change_dos(char n); /* Keep only one master DOS partition, hide all the rest */
main(int argc,char *argv)
{
char n;
if (argc!=2)
{ printf("Bad connamd !"
;return 0;
};
strlwr(argv); /* Convert uppercase letters in the parameter to lowercase */
while((*(argv++))==0); /* Search for the first non-space character */
n=*(--argv);
switch(n){
case 'c':
create_pri_dos();
break;
case 's':
set_hidde_dos();
break;
case 'r':
recall_dos();
break;
case '1': /* Keep only the first entry in the master DOS partition table; hide all other master DOS partition table entries */
change_dos(n);
break;
case '2': /* Keep only the second entry in the master DOS partition table */
change_dos(n);
break;
case '3': /* Keep only the third entry in the master DOS partition table */
change_dos(n);
break;
case '4': /* Keep only the fourth entry in the master DOS partition table */
change_dos(n);
break;
default:
return 0;
};
return 0;
}
void create_pri_dos()
{int i;
biosdisk(2,0x80,0,0,1,1,&dosboot);
for (i=0;i<4;i++)
{if ((dosboot.PT2.TYpe!=0)&&(dosboot.PT2.TYpe!=5))
{dosboot.PT2.Flag=0x00;};
if (dosboot.PT2.TYpe==5)
{dosboot.PT2.TYpe=6;
dosboot.PT2.Flag=0x80;};
};
biosdisk(3,0x80,0,0,1,1,&dosboot);
return;
};
void set_hidde_dos()
{
int i;
biosdisk(2,0x80,0,0,1,1,&dosboot);
for(i=0;i<4;i++)
{if ((dosboot.PT2.TYpe!=0)&&(dosboot.PT2.TYpe!=5))
{dosboot.PT1=dosboot.PT2;};
};
biosdisk(3,0x80,0,0,1,1,&dosboot);
return;
};
void recall_dos()
{
int i;
biosdisk(2,0x80,0,0,1,1,&dosboot);
for (i=0;i<4;i++)
{if (dosboot.PT1.TYpe!='0'

{
dosboot.PT2=dosboot.PT1;
dosboot.PT1=dosboot.PT;
};
};
biosdisk(3,0x80,0,0,1,1,&dosboot);
return;
};
void change_dos(char n)
{
int i,m;
m=n-49;
biosdisk(2,0x80,0,0,1,1,&dosboot);
if (dosboot.PT1.TYpe!=0)
{ for (i=0;i<4;i++)
{if (dosboot.PT1.TYpe!=0)
{dosboot.PT2=dosboot.PT;};
};
dosboot.PT2=dosboot.PT1;
dosboot.PT2.Flag=0x80;
};
biosdisk(3,0x80,0,0,1,1,&dosboot);
return;
};
6. Example and points to note
An example of creating three PRI DOS partitions on a 1.2G hard disk, with sizes 300Mb, 400M, and 500MB respectively:
First use FDISK to create one 300MB PRI DOS partition and one 400MB extended partition, and activate the PRI DOS partition. Boot the computer from floppy and format drive C, then execute the PDOS S command to turn the extended partition into a PRI DOS partition. Then boot the computer from floppy again and format drive C, and the original PRI DOS partition's drive letter becomes D. Then use FDISK to create a 500MB extended partition. After executing the PDOS S command, boot the computer from floppy again and format drive C. In this way three PRI DOS partitions are created. The correspondence between drive letters and capacities is: C: 500MB; D: 300MB; E: 400MB.
Since when switching between PRI DOS partition table entries only one entry is kept available for use, PDOS.EXE must be copied into all PRI DOS partitions. In addition, readers using this program must enter the source program accurately and without error, in order to avoid damaging the data on the hard disk.
Mailing address: Li Zhaokun, 17 Yaogang Road, Nantong City, Jiangsu Province 226006
Telephone: 5516018
E-mail: lzk@mail.info.net.cn
我完全同意设想建立DOS组织“DOS联盟” ,也就是说和Wengier、以及“起步”站长莫老师等DOS战友一起来建立这个“DOS联盟”,以发展我国自主OS(操作系统)的高度去完成我们共同的愿望。
------党委书记
------党委书记


