This article describes the computer startup process, mainly the Linux Startup Process on the PC system. Some embedded systems mention that the above things are not necessarily very correct, most of which are related to online technologies.ArticleAnd some LinuxCode.
In general, this article focuses on the nature of learning and communication. If you think something is wrong, please point it out.
Body start:
After the system is reset, the CPU starts to execute code from a fixed address, and the PC starts to execute code from the BIOS. The embedded CPU is executed from a known address in the flash memory or ROM after the reset. The specific address should be used to check the datasheet of the CPU.
Next we will talk about PC. in PC, it is executed from the address 0xffff0. This address is within the BIOS address range. Here is a jump command, jump to the real startup code of BIOS (for more detailed procedures here, you should refer to the architecture design documents related to the PC, which should be useful for Intel ). The BIOS is powered on for self-check at the beginning, that is, the post process. After the post process is completed, its code is cleared out of the memory, followed by the second stage of BIOS, during this phase, local devices are enumerated and initialized. This part of code will still reside in the memory after the BIOS is complete, and these services can be used by the target operating system. The BIOS finds the active partition based on the user's configuration. Here, the hard disk is used as an example. The system BIOS will read and execute the code at MBR on the hard disk, and the boot loading will be started here.ProgramTake grub used in most Linux distributions as an example. Due to the limited storage space for BIOS access, most boot loaders are divided into two phases for guidance.
Grub (GNU grand uniied bootloader)
MBR (Master Boot Record), a total of 512 bytes, located in the disk's 0-way 0-cylinder 1 sector, that is, the first sector. When the MBR is loaded to ram, the BIOS gives control to the MBR. The Master Boot Loader in the MBR is a 512-byte image, which contains program code and a partition table. The first 446 bytes are the first-stage boot loader, which contains executable code and error message text. The next 64 bytes are the partition table, the record contains 4 partitions (each record is 16 bytes ). MBR ends with two special bytes (0xaa55) and is used for the validity check of MBR.
The first-stage bootstrap loader is to find and load the second-stage Bootstrap program. It finds an active partition in the Partition Table to implement this function. When an active partition is found, it scans other partitions in the partition table to make sure they are not active. When the verification is completed, it reads the boot records of the active partition from the device into RAM and executes them.
As mentioned here, grub has a stage 1.5, Which is because grub can identify the file system and access the configuration files in the/boot/GRUB directory, instead of accessing disk blocks, you can check that the/boot/GRUB directory contains many files named in the format of file system + 1.5, for example, on my machine, there are:
Fat_stage00005
Jfs_stage00005
Minix_stage00005
E2fs_stage00005
Reiserfs_stage00005
Xfs_stage00005
We can see that this includes a variety of common Linux file systems. Load grub in grub1.5. after Conf, you can see the startup option interface. There are many options that can be configured here. For the purpose of this article, we will not introduce them. For more information, see the grub documentation.
After the 1.5 Boot Loader loads and runs, the second-stage loader is loaded into the memory and takes over the subsequent work. It queries and locates the kernel and initrd images on the file system and loads them into the memory. When these images are ready, phase 2 guides the loader to call the kernel entry function, the boot loader releases control and starts the kernel phase.
Generally, the kernel is a compressed kernel image. before decompression, a small amount of code is used for hardware configuration, and then the kernel is decompressed and put into the high-end memory. If there is initrd, it will be moved to the memory, indicating that it will be available in the future, and then the kernel will be called to start the kernel boot process. The specific process is generally from arch/i386/boot/head. the START assembler function of S is executed. This function executes some basic hardware configurations and then calls ARCH/i386/boot/compressed/head. startup_32 function in S, which then calls decompress_kernel to decompress the kernel, and then calls the function located at ARCH/i386/kernel/head. the startup_32 function in S, which further initializes hardware, including memory, and then calls start_kernel to start the kernel. Finally, the start_kernel function calls init to start the first user space process, then start the user space startup process until the entire system is started.
Let's start with this. Next time we can better combine the code.
References:
[1] http://en.wikipedia.org/wiki/BIOS
[2] http://www.ibm.com/developerworks/cn/linux/l-linuxboot/
[3] http://www.ibm.com/developerworks/cn/linux/l-bootload.html