China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-25 13:06
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » Creating Multiple PRI DOS Partitions and Hiding Them with C Language [Repost] View 1,130 Replies 3
Original Poster Posted 2002-12-19 00:00 ·  中国 广东 河源 连平县 电信
高级用户
★★
PCDOS
Credits 912
Posts 217
Joined 2002-10-17 00:00
23-year member
UID 11
Gender Male
From 广东
Status Offline
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
我完全同意设想建立DOS组织“DOS联盟” ,也就是说和Wengier、以及“起步”站长莫老师等DOS战友一起来建立这个“DOS联盟”,以发展我国自主OS(操作系统)的高度去完成我们共同的愿望。
------党委书记
Floor 2 Posted 2002-12-19 00:00 ·  中国 广东 河源 连平县 电信
高级用户
★★
PCDOS
Credits 912
Posts 217
Joined 2002-10-17 00:00
23-year member
UID 11
Gender Male
From 广东
Status Offline
Hard Disk Partitioning Tips under DOS
Computer Room, Anping Middle School, Hebei Zhang Guangle (053600)

As everyone knows, on a hard disk, the cluster is the smallest unit of disk space allocation. Its size has a great deal to do with disk utilization. This is because when allocating hard disk space, if a file writes only a very small number of bytes into a cluster, then although that cluster has not been used up, other files can no longer use that cluster. This is why after installing a piece of software with a size of 20Mb on the hard disk, the free hard disk space may decrease by 3Mb. The larger the hard disk capacity, the more serious this waste becomes. This is because DOS uses the FAT16 partition table, which means that under DOS, 16-bit binary data is used to represent the number of clusters on the disk. This limits the maximum number of clusters on the disk we use to no more than 65536 (2). Therefore, the larger the disk space, the larger the clusters DOS can only use. The wasted hard disk space can roughly be expressed by the following formula:
Wasted hard disk space = cluster size/2×number of files on the disk
In Win98, FAT32 partitions can be used to minimize the cluster size, but because of compatibility issues, this method is somewhat limited. Therefore it is necessary to discuss how to partition a hard disk better under DOS.
People usually divide the whole hard disk into several partitions to improve hard disk space utilization. The more partitions a hard disk is divided into, the smaller the space of each hard disk partition, the smaller the cluster size in each partition, and the higher the utilization of hard disk space. But hard disks nowadays are made with relatively large capacities, and if too many partitions are created, it feels very inconvenient in actual use. So what partition size is more appropriate? We always hope to keep clusters small while making partition space as large as possible. From the following formula we can know the relationship between the size of each disk partition and the size of the clusters used:
number of clusters×size of each cluster=hard disk partition space.
Since under DOS the number of clusters is limited and cannot exceed 65536, we can calculate the relationship between the maximum partition space and the cluster size used. The following table lists the relationship between cluster size used in DOS and the corresponding maximum partition space:
It can be seen from the above table that when the hard disk partition size is less than 512Mb and greater than 256Mb, the partition's cluster size is 8192 bytes. At this time disk utilization is relatively high. Therefore for smaller hard disks (below 2.1Gb), the partition size can be set at 512Mb. But for larger hard disks, too many partitions will make use inconvenient, so somewhat larger partitions may be adopted. Usually it is better not to have more than 3-4 partitions. For example, for hard disks below 4.3Gb, it is more suitable to divide them into several 1024Mb partitions.
For especially large hard disks such as 6.4Gb and above, 2048Mb partitions may be used. Note that this is the largest partition that can be used under DOS, so the largest cluster that can be used under DOS is 32768 bytes.
我完全同意设想建立DOS组织“DOS联盟” ,也就是说和Wengier、以及“起步”站长莫老师等DOS战友一起来建立这个“DOS联盟”,以发展我国自主OS(操作系统)的高度去完成我们共同的愿望。
------党委书记
Floor 3 Posted 2002-12-19 00:00 ·  中国 广东 河源 连平县 电信
高级用户
★★
PCDOS
Credits 912
Posts 217
Joined 2002-10-17 00:00
23-year member
UID 11
Gender Male
From 广东
Status Offline
Actually, using disk tools such as: PQMAGIC SPFDISK etc. makes it easier to create multiple PRI DOS partitions and hide them.
我完全同意设想建立DOS组织“DOS联盟” ,也就是说和Wengier、以及“起步”站长莫老师等DOS战友一起来建立这个“DOS联盟”,以发展我国自主OS(操作系统)的高度去完成我们共同的愿望。
------党委书记
Floor 4 Posted 2002-12-19 00:00 ·  中国 河北 石家庄 联通
中级用户
★★
Credits 349
Posts 72
Joined 2002-11-13 00:00
23-year member
UID 239
Gender Male
Status Offline
How do you use SPFDISK to create multiple PRI DOS partitions? Could you explain it a bit?
Forum Jump: