From http://opens.itpub.net/7668319/viewspace-856199/
the boot process of a Linux system is made up of many steps, but, whether you are launching a standard x86 desktop computer or an embedded PowerPC target board, most of the processes are surprisingly similar. This article explores the start-up process between Linux from the initial start-up preparation to the opening of a program in the user space, and you can learn many other startup-related knowledge, such as boot loaders, kernel decompression, initializing memory disk, And some other Linux-enabled parts. [@[email protected]] at an early age, starting a computer means feeding the tape that contains the starter program, or loading the startup program by manually using the dense address/data/control switch on the front panel, but simplifying the process is not necessary.
Let's start with a comprehensive analysis of the Linux startup from a high level, and then we'll review what's going on in each individual process. The reference source code on the way will help you not get lost in the kernel tree and continue digging deeper.
I. OverviewFigure 1 will be a holistic review of the startup process:
When the system is started, or restarted, the processor executes the code in a well-known location. On the PC, this position is in the basic input and output system, which is what we call the BIOS, which is stored in the flash memory on the motherboard. In an embedded system, the central processing unit (CPU) will be reset to activate the region to start executing known programs in Flash or ROM. In other cases, the basic process is the same. Since the PC offers many flexible optional devices, the BIOS must determine which device to perform the boot process. We'll cover more of this in the next article.
When the boot device is found, the boot loader used in the first phase is loaded into RAM and executed. The boot loader here is smaller than the size of a sector, which is 512 bytes, and its task is to load the second stage of the boot loader.
when the boot loader in charge of the second stage is in memory and executed, a flashing screen is usually displayed, and then Linux and the optional Initialization memory disk (a temporary root filesystem, if you want to get a specific introduction, please visitHttp://likunarmstrong.bokee.com/5502266.html) will be loaded into the memory. When the system image is loaded, the second-stage boot loader transfers control to the kernel image, while the kernel begins to self-extract and initialize. At this stage, the second stage of boot loader checks the hardware of the system, enumerates those additional hardware devices, mounts the root device, and then loads the required kernel modules. Once completed, the first User space program (INIT) begins execution and a higher level of system initialization begins.
This is what the Linux startup process looks like on the surface. Well, now, let's go further and explore some of the details of the Linux boot process in more depth.
second, the system start
the system-initiated phase relies on which hardware the Linux device starts on. In an embedded system, when the system is opened or restarted, it is necessary to use the boot-loaded environment. Examples of this include U-boot,redboot, and the Micromonitor launched by Lucent. An embedded platform is typically a boot monitor that is bound. These programs are located in a specific location on the target hardware flash memory, providing a way to download the Linux kernel image to flash memory and execute it in the next procedure. In addition to having the ability to store and start Linux images, these boot monitors also perform some degree of system detection and hardware initialization. In an embedded target board, these boot monitors typically cover the first and second-stage boot loader functions.
/************************************************************************************************/
tip: How to view your MBR content. If you want to see the specifics of your MBR, use the following command:
# dd If=/dev/hda of=mbr.bin bs=512 count=1
# Od-xa Mbr.bin
you need to run the DD command as root, read your first 512 bytes of an integrated electronic drive or IDE drive, and write them
Mbr.bim file. The OD command prints the binary as a 16 binary and ASCII format
/************************************************************************************************/
in a PC, Linux starts with a 0xffff0 address. The first step in the BIOS is to perform a power-on self-test (POST). The job of post is to check the hardware device. The second step of the BIOS is to enumerate the local devices and initialize them.
due to the different use of the BIOS function, it consists of two parts: The Post Code Runtime service. After the post is completed, it will be purged from memory, but the BIOS runtime service will be retained for the target operating system.
in order to start the operating system, the BIOS runtime service will search for those devices that are active or bootable, and the order of the searches is determined by the CMOS settings (that is, the boot order that we normally set in the BIOS). A floppy drive, an optical drive, a partition on a hard disk, a device on the network, or even a USB flash drive can be used as a boot device.
Of course, Linux is usually booted from a hard disk. The MBR (Master boot record) on the hard disk contains the basic boot loader, which is a 512-byte sector, located on the first sector of the disk (0 heads 0 Tracks 1 sectors). When the MBR is loaded into RAM, the BIOS will turn control over to the MBR.
Third, the first stage boot loader
the primary boot loader in the MBR is a 512-byte image that contains not only the program code, but also a small partition table, as shown in 2. The initial 446 bytes are the main boot loader, which contains executable code and error message text. The next 64 bytes are the partition table, which contains the individual records of four partitions (a partition is 16 bytes). MBR through the special digital 0xaa55 (translator Note: In the electronic world AA55 is really a legendary figure, want to know why? Expand it into binary form to see what the rule is, as a two-byte end flag. 0X55AA is also a valid check confirmation for MBR.
The main boot loader's job is to find and load the second boot loader. It does this by parsing the partition table, locating the activation partition, and when it finds an active partition, it will continue to scan the partitions in the remaining partition tables to make sure they are inactive. When the confirmation is complete, the boot record of the active partition is read from the device to RAM and executed.
Iv. Phase II boot loader
play a sub-function, or a second boot loader, can be more visually known as the kernel loader. The task at this stage is to load the Linux kernel and optionally initialize the memory disk.
/*******************************************************************/
tip: Boot loaders in the Grub stage
The /boot/grub directory contains the boot loaders for Stage1,stage2 and stage1.5, along with a number of optional loaders (for example, the CD-ROM uses iso9660_stage_1_5)
/*******************************************************************/
The first and second stages of the boot loaders are combined, that is, in x86 personal computers, we call the Linux loader (LILO) or Grand Unified Bootloader (GRUB). Since grub fixes some of the flaws in Lilo, let's look at grub below (if you'd like to get more resources on Grub,lilo and related topics, see resources later in this article)
a good thing about grub is that it contains knowledge of the Linux file system. Unlike Lilo, which uses bare sectors, grub can load the Linux kernel from a ext2 or ext3 file system. It is by converting the original two-stage boot loader into a three-stage boot loader. In the first stage (MBR), the boot loader of stage1.5 is started to understand the special file system format in the Linux kernel image, for example, reiserfs_stage1-5 (for loading from the Reiserf log file system) or e2fs+ Stage1_5 (for loading from the WXT2 or ext3 file system). When the boot loader of stage1.5 is loaded and running, Stage2 boot loader can be loaded.
when Stage2 is loaded, grub can display a list of optional kernels based on the request (defined in/etc/grub.conf with a few soft symbolic links/etc/grub/menu.lst and/etc/grub.conf). You can select a kernel and modify its additional kernel parameters. At the same time, you can choose to use the shell of the command line for a more in-depth manual control of the startup process.
after the second stage boot loader is present and in memory, the file system can be queried, and the default kernel image and the initialized memory disk image are also loaded into memory. After all is ready, the second stage of boot loader will invoke kernel mirroring.
Five, the kernel
(Translator notes: In translating this chapter, the translator discovers that there are already translated articles on the IBM website, so from this chapter it is based on the content of the official website). The kernel phase begins when the kernel image is loaded into memory and the boot loader for phase 2 releases control. The kernel image is not an executable kernel, but a compressed kernel image. Usually it is a zimage (compressed image, less than 512KB) or a bzimage (larger compressed image, greater than 512KB), which is compressed in advance using zlib. In front of this kernel image is a routine that implements a small set of hardware settings, extracts the kernel contained in the kernel image, and then puts it into high-end memory, and if there is an initial RAM disk image, it is moved into memory and marked for later use. The routine then invokes the kernel and starts the process of booting the kernel.
when Bzimage (for i386 images) is called, we are from the./arch/i386/boot/head. The start assembly routine of S begins execution (see Figure 3 for the main flowchart). This routine executes some basic hardware settings and calls the./arch/i386/boot/compressed/head. The startup_32 routines in S. This routine sets up a basic environment (stack, etc.) and clears the Block Started by Symbol (BSS). Then call a C function called Decompress_kernel (in./arch/i386/boot/compressed/misc.c) to decompress the kernel. Once the kernel has been extracted into memory, it can be called. This is another startup_32 function, but this function is in./arch/i386/kernel/head. S in.
in this new Startup_32 function (also known as the Purge program or process 0), the page table is initialized and the memory paging feature is enabled. The type of CPU is then detected for any optional floating-point unit (FPU) and stored for later use. Then call the Start_kernel function (in init/main.c), which takes you into the architecture-independent Linux kernel section. In fact, this is the main function of the Linux kernel.
/*******************************************************************/
tip: Manual boot in GRUB
In the GRUB command line, we can use the INITRD image to boot a specific kernel, as follows:
grub> kernel/bzimage-2.6.14.2
[Linux-bzimage, setup=0x1400, size=0x29672e]
grub> initrd/initrd-2.6.14.2.img
[Linux-initrd @ 0x5f13000, 0xcc199 bytes]
grub> Boot
uncompressing Linux ... Ok, booting the kernel.
If you do not know the name of the kernel to boot, simply use the slash (/) and press the Tab key. GRUB displays a list of kernel and INITRD images.
/*******************************************************************/
by calling Start_kernel, a series of initialization functions are called to set interrupts, perform further memory configuration, and load the initial RAM disk. Finally, to invoke Kernel_thread (in arch/i386/kernel/process.c) to start the Init function, which is the first user-space process (user-space). Finally, start the empty task, and now the scheduler can take over control (after calling Cpu_idle). By enabling interrupts, the preemptive scheduler can periodically take over control to provide multitasking capabilities.
During the kernel boot process, the initial RAM disk (INITRD) is loaded into memory by the Phase 2 boot loader, which is copied into RAM and mounted on the system. This initrd is used as a temporary root file system in RAM and allows the kernel to fully boot without mounting any physical disks. Because the modules required to interact with peripherals may be part of the INITRD, the kernel can be very small, but it still needs to support a large number of possible hardware configurations. After the kernel boots, you can formally equip the root filesystem (via Pivot_root): The INITRD root file system is unloaded and the real root filesystem is mounted.
The INITRD function allows us to create a small Linux kernel that includes drivers that are compiled as loadable modules. These loadable modules provide the kernel with a way to access the file system on disk and disk, and provide drivers for other hardware. Because the root file system is a file system on disk, the INITRD function provides a startup method to gain access to the disk and mount the real root filesystem. In an embedded environment without a hard drive, the INITRD can be the ultimate root file system, or it can be mounted on a network file system (NFS) to mount the final root file system.
Vi. Introduction and concluding remarks of Init
Once the kernel is booted and initialized, the kernel can launch its first user-space application. This is the first call to a program compiled using the standard C library. Prior to this, no standard C application was implemented.
on a desktop Linux system, the first program to start is usually/sbin/init. But that's not certain. Few embedded systems will need to use the rich initialization functionality provided by Init (which is configured through/etc/inittab). In many cases, we can invoke a simple shell script to launch the required embedded application.
very similar to Linux itself, the Linux boot process is also very flexible and can support many processor and hardware platforms. Initially, loading the bootloader provides an easy way to boot Linux without any showy. The LILO bootloader extends the boot capability, but it lacks the ability to perceive the file system. The latest generation of bootloader, such as GRUB, allows Linux to boot from some file systems (from Minix to Reise).