1. When the system is powered on or reset, the CPU will assign the PC pointer to a specific address 0xFFFF0 and execute the command at the address. In PC, the address is located in BIOS, And it is stored in ROM or Flash on the motherboard.
2. When the BIOS is running, search for the active and bootable devices according to the boot device sequence defined in the CMOS settings. If you start from the hard disk, the BIOS will load the content in the hard disk MBR (Master Boot Record) to RAM. MBR is a 512-byte sector located in the first sector of the disk (0-way, 0-way, 1-sector ). When the MBR is loaded into RAM, the BIOS will give control to the MBR.
3. The main boot loader finds and loads the next boot loader. It searches for active partitions in a partition table. When an active partition is found, it scans other partitions in the partition table to ensure that they are not active. After the verification is completed, read the Boot Record of the active partition from the device to RAM and execute it.
4. The next Boot Loader loads the Linux kernel and the optional initial RAM disk, giving control to the Linux kernel source code.
5. Run the loaded kernel and start the user space application.
In addition, in Stage 1, it starts the kernel and runs the init process of the user space.
After the kernel image is loaded to RAM, the control of Bootloader is released and the kernel stage begins. The kernel image is not a fully executable target code, but a compressed zImage (Small kernel) or bzImage (large kernel, B Indicates big)
However, not everything in the zImage and bzImage images is compressed. Otherwise, the Bootloader will give control to the core image of Heze brother, and it will be silly. In fact, the image contains unzipped parts, which include the decompression program, which will decompress the compressed part of the image. Both zImage and bzImage are compressed by gzip. They are not only a compressed file, but also are embedded with gzip decompression code at the beginning of the two files.
When bzImage (for i386 image) is called, it starts from the start assembly routine of/arch/i386/boot/head. S. This program executes some basic hardware settings and calls
Startup_32 in/arch/i386/boot/compressed/head. S. After the startup_32 Program sets some basic runtime environments (such as stacks), clear the BSS segment and call
The decompress_kernel () c function in/arch/i386/boot/compressed/misc. C decompress the kernel. After the kernel is decompressed to the memory, it will call
/Arch/i386/kernel/head. the startup_32 routine in the S file. This new startup_32 routine (called purge program or process 0) initializes the page table, enables the memory paging mechanism, and then acts as any optional Floating Point Unit (FPU) check the CPU type and store it for future use. After all these operations are completed, the start_kernel () function in/init/main. c is called to enter the Linux kernel section unrelated to the architecture.
Start_kernel () calls a series of initialization functions to set the interrupt and execute further memory configuration. Later,/arch/i386/kernel/process. in c, kernel_thread () is scheduled to start the first core thread, which executes the init () function, and the original execution sequence calls cpu_idle () to wait for scheduling.
As the Core Thread init () function, it loads and initializes peripherals and drivers and mounts the root file system.
Init () Open the/dev/console device and redirect stdin, stdout, and stderr to the console. Then, it searches for the init program in the file system (you can also specify the init program by the "init =" command line parameter), and uses the execve () system to call and execute the init program. The order for searching the init program is/sbin/init,/etc/init,/bin/init, And/bin/sh. In the embedded system, in most cases, a simple shell script can be passed to the kernel to start necessary embedded applications.
So far, the long Linux kernel boot and startup process have ended, and the first thread created by start_kernel () corresponding to init () has also entered the user mode.