How did the computer start?
Nanyi
Date: February 16, 2013
From powering on to getting started, computer startup is a very complex process.
I'm not sure how this process is going to happen, just see the screen quickly scrolling various tips ... These days, I looked up some information and tried to understand it. Here are the notes I've compiled.
0, the meaning of boot
Ask a question first, how to say "Start" in English?
The answer is boot. However, boot originally meant boots, what does "boot" have to do with boots? Originally, the boot here is bootstrap (shoelace) abbreviation, it comes from a proverb:
"Pull oneself up by one ' s bootstraps"
It is certainly impossible to literally "pull your shoelaces up". At the earliest, engineers used it as a metaphor, computer startup is a very contradictory process: you must run the program before the computer can start, but the computer does not start to run the program!
In the early days, it was true that all sorts of things had to be done to put a small piece of the program into memory before the computer could run properly. So, engineers call this process "pull the shoelace", and over time it's called Boot.
The entire startup process of a computer is divided into four stages.
First, Phase one: BIOS
In the early 70, "read-Only Memory" (Read-only Memories, abbreviated ROM) invented, the boot program was brushed into the ROM chip, after the computer power, the first thing is to read it.
The program in this chip is called "Basic output Input System" (Input/output System), referred to as BIOS.
1.1 Hardware self-test
BIOS program first check that the computer hardware can meet the basic conditions of operation, this is called "Hardware self-Test" (power-on self-test), abbreviated to post.
If there is a problem with the hardware, the motherboard will beep with a different meaning and start the abort. If there is no problem, the screen will display the CPU, memory, hard disk and other information.
1.2 Boot order
After the hardware self-test is complete, the BIOS transfers control to the next stage of the startup program.
At this point, the BIOS needs to know which device the "Next stage startup program" is specifically stored in. That is, the BIOS needs to have a sort of external storage device, and the device that precedes it is the one that gives priority to control. This sort of ordering is called the boot sequence (boot Sequence).
Open the BIOS operating interface, there is a "set the boot sequence."
Ii. Second stage: Master boot Record
The BIOS transfers control to the first storage device in accordance with the "Boot sequence".
At this point, the computer reads the first sector of the device, that is, the top 512 bytes are read. If the last two bytes of these 512 bytes are 0x55 and 0xAA, indicating that the device can be used for booting, and if not, that the device cannot be used for booting, control is then forwarded to the next device in the "boot order".
The first 512 bytes are called "Master Boot Records" (The Master Boot Record, abbreviated as MBR).
2.1 Structure of the Master boot record
The "Master boot Record" has only 512 bytes and doesn't put too much. Its main function is to tell the computer to the location of the hard disk to find the operating system.
The master boot record consists of three parts:
(1) 第1-446 bytes: Call the operating system's machine code.
(2) 第447-510 bytes: Partitioned Tables (Partition table).
(3) 第511-512 bytes: Master boot Record signature (0x55 and 0xAA).
The second part of the "Partition table" is the role of dividing the hard disk into a number of zones.
2.2 Partition Table
Hard disk partitioning has many benefits. Given that each zone can have a different operating system installed, the master boot record must therefore know which zone to transfer control to.
The partition table is only 64 bytes long, and it is divided into four items, each 16 bytes. Therefore, a hard disk can only be divided into four primary partitions, also known as the "main partition."
16 bytes per primary partition, consisting of 6 parts:
(1) 1th byte: If 0x80, it means that the primary partition is an active partition and 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 (cylinder, head, sector area code, and so on).
(3) 5th byte: Primary partition type.
(4) 第6-8个 bytes: The physical location of the last sector of the primary partition.
(5) 第9-12 bytes: The logical address of the first sector of the primary partition.
(6) 第13-16 Bytes: The total number of sectors in the primary partition.
The last four bytes (the total number of sectors in the primary partition) determine the length of the primary partition. That is, the total number of sectors in a primary partition is not more than 2 32.
If each sector is 512 bytes, it means that the maximum number of individual partitions is not more than 2TB. Considering that the logical address of the sector is also 32 bits, the maximum space available for a single hard drive is no more than 2TB. If you want to use a larger hard disk, there are only 2 methods: one is to increase the number of bytes per sector, and the second is to increase the total number of sectors.
Three, the third stage: hard drive boot
At this point, the control of the computer will be transferred to a partition of the hard disk, which is divided into three kinds of situations.
3.1 Situation 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, which is abbreviated to VBR, Volume.
The primary function of the "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 Scenario B: Extended partitions and logical partitions
With the hard drive getting bigger, four primary partitions are not enough and require more partitions. However, there are only four partition tables, so there is only one area that can be defined as an "extended Partition" (Extended partition).
The so-called "extended partition" means that the area is divided into multiple zones. The partitions inside this partition are called "Logical Partitions" (logical partition).
The computer reads the first sector of the extended partition, called the "Extended Boot Record" (Extended Boot recording, abbreviated to 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 its own (that is, only one partition entry). Therefore, an extended partition can contain countless logical partitions.
However, it seems that the operating system is rarely started this way. If the operating system is indeed installed on an extended partition, it is typically started the next way.
3.3 Scenario 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 partition, but instead runs the pre-installed Boot Manager (boot loader), which is the user's choice to start the operating system.
In the Linux environment, the most popular boot manager currently is grub.
Iv. Fourth stage: Operating system
Once control is transferred to the operating system, the kernel of the operating system is first loaded into memory.
Take the Linux system as an example and 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 based on the configuration file (Debian system is/etc/initab). This is the first process after Linux starts, the PID process number is 1, and the other process is its descendants.
The init line then loads the various modules of the system, such as window programs and network programs, until the/bin/login program is executed, jumping out of the login screen and waiting for the user to enter the user name and password.
At this point, the entire startup process is complete.
Finish
How does the turn computer start?