From power on to start operations, computer startup is a very complex process. I still don't know what the process is like. I only saw a variety of prompts on the screen that were quickly scrolled... these days, I checked some information and tried to understand it. Below are my notes.
1. Meaning of boot
First, ask a question, "START?
The answer is boot. However, boot originally meant boots. What is the relationship between "Boot" and boots? It turns out that the boot here is the abbreviation of Bootstrap (shoelaces), which comes from a proverb:
"Pull oneself up by one's bootstraps"
Literally, it is impossible to pull yourself up with your shoelaces. At the earliest time, engineers used it as a metaphor. Computer startup is a very conflicting process: you must first run the program before the computer can start, but the computer cannot run the program if it is not started!
In the early days, we had to try every means to load a small program into the memory before the computer can run normally. Therefore, engineers call this process "shoelaces" and, over time, it is short for boot.
The entire computer startup process is divided into four stages.
2. Stage 1: BIOS
In the early 1970s S, the read-only memory (read-only memory) was invented, and the boot program was flushed into the ROM chip. After the computer was powered on, the first thing is to read it.
The program in this chip is called the basic input/output system (BiOS.
2.1 hardware self-check
The BIOS program first checks whether the computer hardware meets the basic operating conditions. This is called "Hardware self-test" (post.
If there is a hardware problem, the motherboard will beep with different meanings and start to stop. If no problem occurs, the screen displays information such as CPU, memory, and hard disk.
2.2 startup sequence
After the hardware self-check is completed, the BIOS transfers the control to the Startup Program of the next stage.
In this case, the BIOS needs to know which device the next phase of the Startup Program is stored in. That is to say, the BIOS needs to sort an external storage device. The device that comes first is the device that gives priority to control. This sort is called boot sequence ).
Open the BIOS operation interface, which contains "set the startup sequence ".
3. Phase 2: Master Guide record
The BIOS transfers control to the first storage device in the boot sequence.
In this case, the computer reads the first sector of the device, that is, the first 512 bytes. If the last two bytes of the 512 bytes are 0x55 and 0xaa, the device can be used for startup. If not, the device cannot be used for startup, the control is then transferred to the next device in the startup sequence.
The first 512 bytes are called "Master Boot Record" (MBR ).
3.1 Structure of the Master Boot Record
The "primary Boot Record" contains only 512 bytes, which cannot be put too much. It is mainly used to tell the computer where to find the operating system from the hard disk.
The Master Boot Record consists of three parts:
- Bytes 1-446: The machine code that calls the operating system.
- Bytes 447-510: Partition Table ).
- 511-512 Bytes: Signature of the Master Boot Record (0x55 and 0xaa ).
The second part of the partition table is used to divide the hard disk into Several partitions.
3.2 partitioned table
Hard Disk partitioning has many advantages. Considering that different operating systems can be installed in each zone, the "Master Boot Record" must know which zone the control is transferred.
The partition table is only 64 bytes in length and is divided into four items, each of which is 16 bytes. Therefore, a hard disk can be divided into up to four primary partitions, also known as "primary partition ".
The 16 bytes of each primary partition are composed of six parts:
- 1st Bytes: If it is 0x80, it indicates that the primary partition is an active partition, and the control is transferred to the partition. Only one of the four primary partitions can be activated.
- 2-4 Bytes: the physical location of the first slice in the primary partition (cylindrical, Head, sector number, and so on ).
- 5th Bytes: Primary partition type.
- 6-8 Bytes: the physical location of the last slice of the primary partition.
- 9-12 Bytes: The Logical Address of the first sector of the primary partition.
- 13-16 bytes: Total number of sectors in the primary partition.
The last four bytes ("Total Number of Primary partition sectors") determine the length of the primary partition. That is to say, the total number of sectors in a primary partition cannot exceed 2 to the power of 32.
If each slice is 512 bytes, it means that a single partition cannot exceed 2 TB. Considering that the logical address of the slice is also 32 bits, the maximum available space of a single hard disk cannot exceed 2 TB. To use a larger hard disk, there are only two methods: one is to increase the number of bytes for each sector, and the other is to increase the total number of sectors.
4. Phase 3: Hard Disk startup
At this time, the control of the computer will be transferred to a partition of the hard disk, which is divided into three situations.
4.1 case A: volume boot records
As mentioned in the previous section, only one of the four primary partitions is active. The computer reads the first sector of the active partition, which is called the volume Boot Record (VBR ).
The main function of volume Boot Record is to tell the computer where the operating system is located in this partition. Then, the computer will load the operating system.
4.2 case B: Extended partitions and logical partitions
As the hard disk grows, four primary partitions are insufficient and more partitions are needed. However, there are only four partition tables, so there is a rule that only one partition can be defined as an extended partition ).
The so-called "extended partition" means that the partition is divided into multiple zones. In this type of partition, the partition is called "logical partition ).
The computer first reads the first sector of the extended partition, called extended Boot Record (EBR ). It also contains a 64-byte partition table, but there are only two items at most (that is, two logical partitions ).
The computer then reads the first sector of the second logical partition, finds the location of the third logical partition from the partition table, and so on, until the partition table of a logical partition only contains itself (that is, there is only one partition item ). Therefore, extended partitions can contain countless logical partitions.
However, it seems that the operating system is rarely started in this way. If the operating system is indeed installed in the extended partition, it is generally started in one of the following ways.
4.3 Case C: Start the manager
In this case, after the computer reads the machine code of the first 446 bytes in the "Master Boot Record", it no longer transfers control to a partition, instead, run the pre-installed Boot Loader. The user selects which operating system to start.
In Linux, grub is the most popular startup manager.
5. Stage 4: Operating System
After control is transferred to the operating system, the kernel of the operating system is first loaded into the memory.
Take the Linux system as an example. Load the kernel under the/boot directory first. After the kernel is loaded successfully, the first program to run is/sbin/init. It generates the INIT process according to the configuration file (the Debian system is/etc/initab. This is the first process after Linux is started. The PID process number is 1, and other processes are its descendants.
Then, the init thread loads various modules of the system, such as window programs and network programs, until the/bin/login program is executed and jumps out of the logon interface, waiting for the user to enter the user name and password.
So far, all the startup processes have been completed.