From the time the power is turned on to the start of operation, the boot of the computer is a very complicated process.

Seeing the screen scrolling various prompts quickly, I had never been clear about what was going on under the hood. In the past few days, I checked some information and tried to understand it. Here's my notes.

0. The Meaning Of Boot

First, why do we use "boot" to represent the startup of the computer?

Well, "boot" here is an acronym for bootstrap. It comes from a saying:

"pull oneself up by one's bootstraps"

It literally means "pulling yourself up with your shoelaces", which of course is impossible. Originally, engineers used it to describe the startup of the computer as a very contradictory process: You must run the program before the computer can start, but you cannot run the program without starting the computer!

In the early days, it was really the truth. Finally engineers thought out a variety of ways to put a small piece of program into the memory and then the computer would work properly. Therefore, engineers called the process "bootstrapping," and later it was referred to as "boot".

The entire boot process of the computer is divided into four phases.

1. BIOS

In the early 70s of the last century, "read-only memory" (ROM) was invented. The boot program was written into the ROM chip. After the computer was powered on, the first thing was to read it.

The program in the chip is called "Basic Input/Output System" (BIOS).

1.1 POST

First, the BIOS program checks whether the computer hardware can meet the basic conditions for the operation. It's called "Power-On Self-Test"(POST).

If there is a problem with the hardware, the main board will beep with different meanings and abort. If there is no problem, the screen will show the information of the CPU, memory, hard disk and so on.

1.2 Boot Sequence

After the POST is completed, it turns to the next stage of the boot process.

This time, BIOS needs to know which device stores the "next-stage boot program". In other words, BIOS needs to sort external storage devices. The device in front is the device which is given priority to. The sort is called "Boot Sequence".

Open the BIOS interface, there is a item of "specifying the boot sequence".

2. MBR

BIOS transfers the control to the storage device which gets the priority according to the boot sequence.

At this point, the computer reads the first sector of the device, that is, reads the first 512 bytes. If the last two bytes of the 512 bytes are 0x55 and 0xAA, it indicates that the device can be used for booting. If not, it indicates that the device cannot be used for booting, and then the control will be transferred to the next device in the boot sequence.

The first 512 bytes are called the Master Boot Record (MBR).

2.1 The Compositions of MBR

MBR has only 512 bytes and can't store much. It's used to tell the computer where to find the operating system in the hard disk.

MBR consists of three parts:

(1) 1-445 bytes: The machine code used to call the operating system.

(2) 447-510 bytes: Partition table.

(3) 511-512 bytes: MBR signature (0x55 and 0xAA).

Among them, the second part will divide the hard disk into several partition.

2.2 Partition Table

The partition of hard disk brings many benefits. Given that each partition can have a different operating system installed, therefore MBR must know which partition to transfer control to.

The length of the partition table is only 64 bytes, which is divided into four items, each with 16 bytes. Therefore, one hard disk can only be divided into four primary partitions, which is also called the "primary partition."

The 16 bytes of each primary partition consists of 6 parts:

(1) First byte: If it is 0x80, it indicates that the primary partition is the active partition and then the control is transferred to this partition. Only one of the four primary partitions is active.

(2) 2-4 bytes: The physical location of the first sector of the primary partition (cylinders, heads, sector numbers, etc.).

(3) 5th byte: The type of the primary partition.

(4) 6-8 bytes: The physical location of the last sector of the primary partition.

(5) 9-12 bytes: The physical location of the first sector of the primary partition.

(6) 13-16 bytes: The total number of the sectors in the primary partition.

The last four bytes (the total number of the sectors in the primary partition) determine the length of the primary partition. That is, the total number of the sectors in a primary partition is 2^32 at most.

If the number of each sector is 512 bytes, it means that a single partition does not exceed 2TB. Consider that the physical location of the sector is also 32-bit, so the available space for a single hard disk doesn't exceed 2TB at most. If you want to use a larger hard disk, there are only two methods: one is to increase the number of bytes of each sector, and the other is to increase the total number of sectors.

3. Hard disk boot

Now, the control of the computer is transferred to one partition of the hard disk. And there are three situations here.

3.1 VBR

As mentioned in the previous section, only one of the four primary partitions is active. The computer will read the first sector of the active partition, and it's called Volume Boot Record(VBR).

VBR tells the computer where the operating system is located in the partition. And then the computer will load the operating system.

3.2 Extended Partition And Logical Partition

With hard disks getting larger, the four primary partitions are no longer enough and more partitions are needed. However, there are only four items in the partition table, so it is stipulated that one and only one partition can be defined as an "extended partition".

The so-called "extended partition" means that this one partition is divided into multiple partitions. The partitions inside this partition are called "logical partitions".

At first, the computer reads the first sector of the extended partition, which is called Extended Boot Record (EBR). It also contains a 64-byte partition table, but only two items at most (that is, two logical partitions).

The computer then reads the first sector of the second logical partition, and finds the location of the third logical partition from the partition table inside ... until finally the partition table of the logical partition only contains itself ( That is, there is only one partition item). Therefore, an extended partition can contain an unlimited number of logical partitions.

However, it seems that the operating system is rarely started up in this way. If the operating system is indeed installed in an extended partition, then the way we generally use is as follows.

3.3 Boot Loader

In this case, after the computer reading the 446-byte machine code which is in the front of MBR, it doesn't transfer the control to a certain partition. Instead, it runs a previously installed "boot loader" which lets the user choose which operating system to start up.

In Linux, the most popular boot loader is Grub.

4. Operating System

After the control is transferred to the operating system, the kernel of the operating system will be loaded into memory.

Take the Linux system as an example. The kernel under the /boot directory will be loaded first. After the kernel is loaded successfully, the first program to run is /sbin/init. It generates the init process based on the configuration file(/etc/initab in the Debian system). This is the first process after Linux is started. The number of the process id (pid) is 1, and all other processes are its descendants.

Then, the init thread will load different modules of the system, such as window programs and network programs, until the /bin/login program is executed. Thus it will exit the login interface and wait for the user to enter the user name and password.

At this point, the entire boot process is completed.