【Computer】How to start the computer

how the computer starts

  • This topic was unintentionally brought up when my friend chatted. He said that a Tencent scholar asked him this question at the time. Then Balabala said a lot, I still want to understand, or do it by myself. I checked the information on the Internet and found that Ruan Yifeng had already written a blog
    参考文献: "How does the computer start?

notes

Zero, the meaning of boot

Let me ask a question first, how do you say "start" in English?

The answer is boot. However, boot originally meant boots. What does "boot" have to do with boots? It turns out that the boot here is the abbreviation of bootstrap (shoelace), which comes from a proverb:

“pull oneself up by one’s bootstraps”

Literally "pulling myself up with the laces", which is of course impossible. In the earliest days, engineers used it as an analogy, and computer startup is a very contradictory process: a program must be run before the computer can start, but the computer cannot run the program without it!
This was really the case in the early days. You had to find every way to fit a small program into memory before the computer could run normally. Therefore, engineers call this process "pulling the shoelace", and over time it is referred to as boot.

First, the first stage: BIOS

In the early 1970s, "read-only memory" (abbreviated as ROM) was invented. The boot program was flashed into the ROM chip. After the computer was powered on, the first thing to do was to read it.

1.1 Hardware self-test

BIOS首先检查计算机硬件是否满足运行的基本条件---硬件自检post

1.2 Startup sequence

The hardware self-test is completed, and the BIOS transfers control to the next stage of the startup program

Second, set the master boot record

2.1 Master Boot Record

The "Master Boot Record" is only 512 bytes, so it doesn't hold much. Its main function is to tell the computer where on the hard disk to find the operating system.

The master boot record consists of three parts:
  (1) Bytes 1-446: The machine code that calls the operating system.
  (2) Bytes 447-510: Partition table.
  (3) Bytes 511-512: The role of the master boot record signature (0x55 and 0xAA)
分区表is to divide the disc into several areas

2.2 The role of the partition table 

Hard disk partitioning has many benefits. Considering that each zone can install different operating systems, the "Master Boot Record" must therefore know which zone to transfer control to.

The length of the partition table is only 64 bytes, and it is divided into four items of 16 bytes each. Therefore, a hard disk can only be divided into four first-level partitions at most, also known as "primary partitions".

16 bytes for each primary partition, consisting of 6 parts:

  (1) The first byte: If it is 0x80, it means that the main partition is the active partition, and the control right should be transferred to this partition. Only one of the four primary partitions can be active.

  (2) Bytes 2-4: The physical location of the first sector of the main partition (cylinder, head, sector number, etc.).

  (3) The fifth byte: the primary partition type.

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

  (5) Bytes 9-12: The logical address of the first sector of the main partition.

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

The last four bytes ("total number of sectors in the main partition") determine the length of the main partition. That is to say, the total number of sectors of a primary partition cannot exceed 2 to the 32nd power at most.

If each sector is 512 bytes, it means that a single partition does not exceed a maximum of 2TB. Considering that the logical address of the sector is also 32 bits, the space available for a single hard disk does not exceed 2TB at most. If you want to use a larger hard disk, there are only two ways: one is to increase the number of bytes per sector, and the other is to increase the total number of sectors.

Third, the third stage: hard disk boot

At this time, the control of the computer will be transferred to a certain partition of the hard disk, and there are three cases here.

3.1 Case A: Volume Boot Record

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, called the "Volume boot record" (VBR for short).

The main function of "Volume Boot Record" is to tell the computer where the operating system is located in this partition. The computer will then load the operating system.

3.2 Case B: Extended and Logical Partitions

As hard drives get bigger, four primary partitions are no longer enough, and more partitions are needed. However, the partition table has only four entries, so there is one and only one area that can be defined as an "extended partition".

The so-called "extended partition" means that this area is divided into multiple areas. The partitions in this partition are called "logical partitions".

The computer first reads the first sector of the extended partition, which is called "Extended boot record" (Extended boot record, abbreviated as EBR). It also contains a 64-byte partition table, but there are at most two items (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 inside, and so on, until the partition table of a logical partition contains only itself ( i.e. only one partition entry). Therefore, an extended partition can contain an infinite number of logical partitions.

However, it seems that the operating system is rarely booted this way. If the operating system is indeed installed in the extended partition, the next method is generally used to start.

3.3 Case C: Boot Manager

In this case, after the computer reads the 446-byte machine code in front of the "master boot record", it no longer transfers control to a certain partition, but runs the pre-installed "boot loader", It is up to the user to choose which operating system to start.

Fourth, the fourth stage: operating system

After control is transferred to the operating system, the operating system's kernel is first loaded into memory.

Taking the Linux system as an example, first load the kernel under the /boot directory. After the kernel is loaded successfully, the first program to run is /sbin/init. It spawns the init process based on the configuration file (/etc/initab on Debian). This is the first process after Linux starts, 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 the login interface is jumped out, waiting for the user to enter the user name and password.

At this point, the entire startup process is complete.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325863086&siteId=291194637