Establishing multiple PRI dos partitions in C language

Source: Internet
Author: User
Tags one table relative reserved backup

First, the introduction

In the DOS system, in computer application training due to different training requirements, the requirements of the software are different, due to the wrong operation of students, stored on the hard disk of software and important data easily be illegally deleted or damaged. In this respect, the author through the in-depth analysis and practice of hard disk Management, summed up a new and effective way to backup and protect important data and software on the hard disk: Establish multiple PRI dos partitions, different training requirements using different PRI dos partitions, and only one PRI DOS partition available when used , the remaining PRI dos partitions are suppressed.

II. Structure of partitioned table

It is well known that under DOS operating system, a hard disk can be divided into PRI dos partition and extended partition, and multiple logical partitions can be further established in the extended partition. These PRI dos partitions and logical partitions can be used like a separate physical hard disk. So how does DOS manage and use these partitions? The secret of DOS management hard disk is two tables: Hard disk partition bracelet. During the boot process, DOS establishes the disk parameter table (BPB table) for each partition based on the partition information provided by the hard disk partition table and the partition tables, and the disk parameter table is the basis of DOS access to the hard disk. Therefore, the partition table and the partitioned tables have a very important position in hard disk access.

The partition chain is stored on the hard disk and is generally composed of a main boot node and multiple common nodes. The main boot node, corresponding to the PRI DOS partition, is the 0-cylinder 0-head 1-sector area of the hard disk, which is the main boot record sector of the hard disk. In the head node sector, 218 bytes from start to 0DAH are a primary boot program, from 0DBH to 1BDH, 228 bytes to 00H, and 64 bytes from 1BEH to 1FDH as the primary partition table for the hard disk, four table entries, and 16 bytes per table entry, The first two table entries indicate the primary partition and the extended partition's information on the hard disk, and the latter two table entries are generally not used, all are 00H; the last two bytes of the sector are end flags 55H, AAH.

The data structure of the table entry is as follows:
Relative offset length (BYTE) meaning
0 1 Activation flag
1 3 partition starting position (column, head, fan)
4 1 Partition type
5 3 partition termination position (column, head, fan)
8 The relative ordinal number of the starting sector of the 4 partition
12 4 partition size
The activation flags in the table item structure are 80H when activated (otherwise 00H). The starting position, for the purpose of this partitioned table entry, is the starting column of 1 first 1 sectors of the partition, and for the extended table entry is the starting column of the corresponding extended partition, 0 first 1 sectors. Common values for partition types are 1, 4, 6, 5, and so on. 1 represents a partition with 12-bit FAT, 4 indicates 16-bit FAT partitions, 6 represents a partition with a capacity greater than 32M, and 5 represents an extended partition. The partition size equals the number of sectors from the starting sector to the terminating sector for each corresponding partition. For this partitioned table entry, this value does not contain a hidden sector, and for an extended table entry this value contains the implied sector, and the extension table entry in the PRI DOS partition equals the sum of all sectors of each logical partition including the implied sector. The relative number of the starting sector is called, and its relative starting point is divided into three different cases: for this partitioned table entry, the ordinal number is 0 first 1 sectors relative to the starting column of the partition, and for extended table entries, if the extended partition of PRI dos is relative to the main boot record sector of the PRI DOS partition, if the logical partition is extended, Are all relative to the beginning of the entire extended partition, this starting point is generally the first logical partition of the starting column 0 first 1 sectors.

Iii. establishment of multiple PRI DOS partitions

From the analysis above, it is clear that the partitioned table consists of four table entries (although typically up to only two items), and the first table entry for the primary partition table points to the PRI DOS partition, and the second table entry points to the extended partition. If you follow normal usage, there is absolutely no need to set up four table entries, this is the case, it must be used for the purpose. Because some of the items in the extended table entry in the primary partition table contain information about all the extended partitions, I first use FDISK to establish an extended partition with only one logical partition. The partition type of the Extended partition table entry is then changed from 5 to 6, and the activation flag from 00H to 80H (note that the original PRI DOS partition's activation flag must be changed from 80H to 00H). Then you must restart the machine with the floppy disk and format the C disk, then you can use the newly established PRI DOS Partition boot machine (the original PRI DOS partition's drive letter becomes D). This creates two PRI dos partitions.

Iv. setting and recovery of suppressed partitions

We already know that the hard drive master boot record sector from 0DBH to 1BDH a total of 228 bytes to 00H, after the establishment of more than one PRI DOS partition, you can use the last 64 bytes of these 228 bytes to save the four table entries have content. Select a PRI DOS partition, and then change the contents of all the table entries in the four table entries to 00H for all other partition types that are not extended. This means that there is only one PRI DOS partition on the hard disk, and others are suppressed.

To restore the suppressed partition to normal, simply write it back to the table key by saving it in the master boot sector of the primary hard disk.

V. SOURCE program

Below gives the source program: PDOS.CPP. The source program compiles on a compatible machine under the Turboc++ 3.0 compilation environment. Use the method to type at a DOS prompt: [D:] [PATH]
PDOs <C/S/R/1/2/3/4> (s: establishing a PRI DOS partition; s: Backing Up the PRI DOS partition table entries; R: Regaining the PRI DOS partition table entries; 1: Only the first item of the PRI DOS Partition table entry is retained, the rest is implied; 2: only the PRI The second item of a DOS partition table entry; 3: Only the third item of the PRI DOS Partition table entry is retained; 4: Only the last item in the PRI DOS Partition table entry is retained.

The following is a list of PDOS.CPP programs:

#include <dos.h>
#include <bios.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct HSC
{char hd,sc,cy;};
struct partitiontable
{char Flag;/* Activation Flag * *
HSC begin; /* Start position */
Char TYpe; /* Partition Type * *
HSC end; /* End Position * *
Long ID; /* The relative serial number of the start sector * *
Long size; /* Partition Size * *
};
struct NOTE/* Node structure * *
{char boot1[366];
Partitiontable Pt[1];
Partitiontable Pt1[4];
Partitiontable Pt2[4]; /* Primary DOS Partition table entry * *
Char f[2];
}dosboot;
void Create_pri_dos (); /* Establish (Modify extended partition to) Primary DOS Partition/*
void Set_hidde_dos (); /* Make a backup of the primary DOS Partition table entry * *
void Recall_dos (); /* Restore primary DOS Partition table entry * *
void Change_dos (char n); /* Only one primary DOS partition is reserved, and the rest is implied * *
Main (int Argc,char *argv[])
{
char N;
if (argc!=2)
{printf ("Bad CONNAMD!");
return 0;
};
STRLWR (argv[1]); /* Convert uppercase letters in parameters to lowercase letters * *
while ((* (argv[1]++)) ==0); /* Search for first non-whitespace characters * *
n=* (--argv[1]);
Switch (n) {
Case ' C ':
Create_pri_dos ();
Break
Case ' s ':
Set_hidde_dos ();
Break
Case ' R ':
Recall_dos ();
Break
Case ' 1 ':/* retains only the first item in the Primary DOS partition table, and the remaining primary DOS partition entries are implied.
Change_dos (n);
Break
Case ' 2 ':/* Only the second item in the Primary DOS partition table is retained * *
Change_dos (n);
Break
Case ' 3 ':/* Only the third item on the primary DOS partition table is reserved * *
Change_dos (n);
Break
Case ' 4 ': * Keep only the fourth item on the Primary 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[i]. type!=0) && (dosboot. Pt2[i]. type!=5))
{Dosboot. Pt2[i]. flag=0x00;};
if (dosboot. Pt2[i]. TYPE==5)
{Dosboot. Pt2[i]. type=6;
Dosboot. Pt2[i]. 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[i]. type!=0) && (dosboot. Pt2[i]. type!=5))
{Dosboot. Pt1[i]=dosboot. Pt2[i];};
};
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[i]. type!= ' 0 ')
{
Dosboot. Pt2[i]=dosboot. Pt1[i];
Dosboot. PT1[I]=DOSBOOT.PT[0];
};
};
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[M]. type!=0)
{for (i=0;i<4;i++)
{if (dosboot). Pt1[i]. type!=0)
{Dosboot. Pt2[i]=dosboot.pt[0];};
};
Dosboot. Pt2[m]=dosboot. PT1[M];
Dosboot. PT2[M]. flag=0x80;
};
Biosdisk (3,0x80,0,0,1,1,&dosboot);
Return
};

Vi. examples and matters needing attention

Set up three PRI dos partitions on a 1.2G hard drive, with an example of 300Mb, 400M and 500MB in size, respectively:

First, use Fdisk to create a 300MB pri DOS partition and 400MB extended partition, activate the PRI DOS partition, start the computer with a floppy disk, format the C disk, execute the pdos s command, make the extended partition into the PRI DOS partition, and then start the computer with a floppy disk and format the C disk. The original PRI DOS partition's letter changed to D. Then use Fdisk to build a 500MB extended partition, and after executing the PDOs s command, start the computer with a floppy disk and format the C disk so that three PRI dos partitions are established. The corresponding relationship between the disk character and the capacity is C:500MB D:300MB;E:400MB.

Because only one table entry is kept for use when switching between PRI DOS partition table entries, you must copy the PDOS.EXE program to all PRI dos partitions. Another reader must accurately enter the source program when applying this program to prevent damage to the data on the hard drive.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.