Article Title: Linux System Boot Process and disk partition information. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source. System Boot Process and disk partition information
On a PC, the initial startup is completed by the BIOS. When Power-On Self-Test (POST) is over, the BIOS tries to read the first sector of the disk as the Boot Sector. Because most BIOS do not support SCSI, the SCSI adapter must provide its own BIOS to start from the SCSI disk. If nothing is found, the old BIOS will start the built-in rom basic, or print "NO ROM-BASIC" directly ".
Start the operating system in several steps. Because the boot sector is relatively small, usually the main task is to read the second loader. The second loader then reads the third loader until the entire operating system is fully read.
Boot area of the hard disk:
Code:
OFFSET
0x000 JMP xx Near jump into the program code
0x003 Disk parameters
0x03E Program code loading the DOS kernel
0x1FE 0xAA55 Magic number for BIOS
It can be seen that the structure of the boot area is relatively simple. The length is always 512 bytes. It is important that the boot zone starts from 0 and ends with 0xAA55 at 0x1FE of BIOS.
It is relatively easy to start from a floppy disk, because there is only one boot sector: the first sector. The hard disk is more difficult. It is divided into many partitions. However, the BIOS treats the hard disk like a floppy disk and reads the first partition called master boot record. (MBR ). Therefore, the MBR should have the same structure as described above. At the end of MBR, there should be a partition table. For example:
Code:
OFFSET Length
0x000 0x1BE code loading and starting the boot sector of the active
Partitian
0x1BE 0x010 partition1
0x1CE 0x010 partition2
0x1DE 0x010 partition3
0x1EE 0x010 partition4
0x1FE 0x0012 0xAA55 Disk parameters
Starting from 0x1BE (462nd bytes), it is the information of primary partitions. Each partition information occupies 16 bytes. The structure is as follows:
1 BOOT Boot flag: 0 = not active, 0x80 active
1 HD Begin: head number
2 sec cyl Begin: sector and cylinder number of boot sector
1 SYS System Code: 0x83 linux, 0x82 linux swap etc.
1 HD End: head number
2 sec cyl End: sector and cylinder number of boot sector
4 low byte high byte Relative sector number of start sector
4 low byte high byte Number of sectors in the partition
Therefore, the hard disk can have four primary partitions: primary prititions. If they are not enough, you can set the so-called extended partitions. An extended partition contains at least one logical partition. The first sector structure of the extended partition is similar to MBR. The first table item of its partition table corresponds to the first logical partition. If the second logical partition exists, the second table item in the partition table contains a pointer. This pointer points to an address next to the first logical partition. This address contains a partition table. The first table item of the partition table corresponds to the second logical partition. In this way, a linked list is formed, so that the extended partition can have any number of logical partitions.
Each primary partition and expansion area contains a boot sector. The system can only be started from one of these locations. The BOOT flag determines which zone is guided.
The MBR code must perform the following operations:
1: Determine the active partition.
2: Use BIOS to read the startup sector of the active partition.
3: Jump to the 0 position of the Start sector.
The MBR has enough space to complete these tasks. As mentioned above, each partition theoretically contains a boot sector, and the second hard disk also contains a structure similar to the first one. MBR can fully accommodate a complex boot program. The so-called boot manager dynamically determines the active partition. Linux provides lilo, grub, and other tools to manage startup items.
The following C program can be used to check the disk partition information to verify the correctness of the above theory.
Code:
/* Marco corvi
* @ Date feb 2003
*
* \ Brief Read disk partition table
* Modified by zhoulifa * Zhou Lifa Linux enthusiast Linux knowledge transmitter sohou developers
*/
# Include // Printf
# Include // Exit
# Include
# Include
# Include
# Include
# Include
Int
Main (int argc, char ** argv)
{
Int fd;
Unsigned char mbs [512]; // master boot sector
Unsigned char * pp; // partition pointer
Int head, sect, cyl;
Unsigned int sector;
Int n;
If (argc <= 1 ){
Fprintf (stderr, "Usage: % s \ N ", argv [0]);
Exit (1 );
}
Fd = open (argv [1], O_RDONLY );
If (fd <0 ){
Fprintf (stderr, "Error: unable to open device % s \ n", argv [1]);
Perror ("");
Exit (1 );
}
N = read (fd, mbs, 512 );
Close (fd );
If (n <512 ){
Fprintf (stderr, "Error: short read % d \ n", n );
Exit (1 );
}
Compile this program and run it directly. Without any parameters, the corresponding information is read from the/dev/partitions file. If a parameter exists, the first parameter indicates the device to be viewed.
Reference to http://www.linuxdby.com/articlesdisplay.php? Newsid = 75 and marco_corvi's "Linux kernel programming by example" are verified on Red Hat Linux 7.2.
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