Article Title: Learn how to guide the Linux operating system. 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.
1. Introduction
The Linux Startup Process refers to the period from power-on to the shell prompt.
The Linux Startup Process can be divided into several stages. The specific stages depend on the specific system implementation. Generally, Systems Based on the X86 architecture can be divided into three stages. Some embedded systems are divided into two stages, and some are divided into three stages.
Many articles about linux Startup do not clearly describe the details, so let me explain it in detail. If it is caused by an error, please include it more :)
2. Power-on and boot program loading during startup
Regardless of the system, the first phase of linux Startup is always the initial phase of CPU power-on.
After the CPU is powered on, it is a self-check process. After this process is completed, the CPU will jump to a fixed address from which code is executed. The fixed address varies depending on the CPU. For example, the address in the PC is 0XFFFF0. The fixed address is usually some read-only or read-write flash memory. The CPU connects to the flash memory through the bus and can find these flash addresses through the addressing mechanism.
In a general PC, the BIOS system is a system that 'loads the first booter'. The first line of code executed after the CPU of the PC is powered on is always the code saved by the flash memory in the BIOS, this code can be seen as a startup environment. It completes two parts: basic hardware power-on detection and local device enumeration and initialization. After the power-on detection is completed, the BIOS code (the code for completing the power-on detection) will be cleared from the memory, but the service code starts to run when the BIOS system is running, this code detects the configuration of CMOS, that is, to see from which device the user configures to start. When the service code of the BIOS is run and finds the startup device configured by the user, copy the code of the first Bootstrap program to RAM from this device. At this point, the BIOS task is successfully completed. Let's continue with the rest. Note that the so-called 'first booter' must be stored on the selected device, and the service code of the BIOS knows the address saved by the Bootstrap, how can I get it to RAM?
In the embedded system, there may be no such system as BIOS, but there must be a similar Flash/ROM. The CPU can start to execute code from the ROM address, and this code must be a startup environment, that is, a special program, such as U-BOOT or something. This program must be stored in a fixed position on the flash memory. How can you find the CPU? What is this program? They provide a way to download the Linux system image to the flash memory and continue execution. In addition to storing and guiding the Linux image, these programs may also execute a certain level of system testing and hardware initialization processes. The startup Environment Code of the embedded system is similar to the 'first booter' code in the PC, that is, the main boot program included in the hard disk MBR.
In PC Linux, when the BIOS finds that the system is guided by the hard disk, find the MBR of the hard disk and load the boot program stored in the MBR to RAM, then, the control of the CPU is handed over to this piece of code in the MBR. the BIOS task is complete now. Therefore, the BIOS has been busy for a long time since it was powered on. The main purpose is to find the boot device and load the boot program of the boot device into RAM for running. Good BIOS!
3. Phase I boot program
The code in MBR is called the 'first-stage Bootstrap '.
It consists of three parts: The first part is the real code, namely the BootLoader code, which is a piece of executable code; the second part is a 64-byte partition table, which contains four partition records; the third part is the end identifier (0XAA55), used for MBR validity detection.
The role of the 'Phase-1 procat' is to find and load the 'Phase-2 procat '. This function is implemented by searching for an active partition in a partition table. Only one of the four partitions identified by the second part of MBR is an active partition. Generally, this active partition record contains information about the real partition. Bootloader uses this information to find the real partition, then, load the 'second-stage booter' into RAM from this partition.
4. Phase 2 pilot program
The second phase of the boot program can be considered as the last step of the boot process. Its core task is to load the Linux kernel and the optional initialization ramdisk.
In the PC environment, grub is usually used to implement the Bootstrap program. Grub is one of the biggest advantages over lilo, that is, grub can recognize hard disk partitions. In fact, before the first-stage Bootstrap program loads the second-stage Bootstrap program, the first-stage Bootstrap program will first guide a 1st. The 5-phase Bootstrap program enters RAM for execution. This code can be understood as a special file system containing a linux system image. When the pilot program runs, the second-stage Bootstrap program will be loaded. In the second stage, the Bootstrap program is mainly used to load the corresponding kernel image and initialize the ramdisk to the memory.
After the kernel impact and initialization of the ramdisk are loaded successfully, the task of the pilot program in the second stage will be completed successfully, and the rest of the work will be handed over to the kernel image.
5. kernel Startup Process
When the second phase of the boot program completes the task and the kernel image obtains CPU control, the kernel journey begins and finally waits until this day!
Unfortunately, the kernel image at this time is not an executable program, but a compressed program that is compressed using zlib. In fact, this kernel image consists of two parts. The front-end part is a small segment of executable code, followed by a real kernel image. The main task of the front-end is to initialize a small amount of hardware settings, decompress the kernel image, and put the decompressed kernel into the high-end memory. At this time, if you find that the ramdisk is initialized, it will be moved into the memory. Then, the decompressed kernel gets control of the CPU, and the kernel starts to act!
After the kernel is started, initialize the page table, enable the memory paging function, check the CPU type, and call the start_kernel () function to enter the initialization part unrelated to the system, including the memory configuration, load and initialize the ramdisk, and finally start the init function. Now the kernel startup phase is over.