Linux system boot Process specific explanation-what happened after power-on--linux kernel profiling (0)

Source: Internet
Author: User

This article is for example the following articles

Deep understanding of the Linux boot process

MBR (Master boot Record)

The process that the computer executes before powering it on to the operating system main function

Explain the startup process and system initialization of Linux system in detail

Linux system start-up process

About the start-up process for Linux systems we are able to follow the steps to divide it into such as the following:

    • Bios
      1. Post Self Test
      2. BIOS (Boot Sequence)
    • Booting the operating system
      1. Loading the MBR (bootloader) on the corresponding boot
      2. The master boot setting loads its bootloader
    • Loading the operating system
Boot BIOS, prepare interrupt vector table and interrupt service program in real mode

After the computer starts. The CPU logic circuit is designed to perform only in-memory programs and does not have the ability to directly execute an operating system that exists on a floppy disk or hard disk, and is assumed to be executed and must be loaded into memory (RAM).
How the BIOS is started. The CPU hardware logic is designed to force the CS value to 0xf000 at power-up moment. IP is 0xfff0. This cs:ip points to the 0xffff0 location, which is the entry address of the BIOS program.

The BIOS program is cured in a very small ROM chip on the computer's motherboard. Now Cs:ip has pointed to 0xffff0, which means the BIOS starts.

Post Self Test

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. With the execution of the BIOS program. The screen will display video card information, memory information, etc.,

Initializing the device

The second step of the BIOS is to enumerate the local devices and initialize the

One thing that is critical to starting the operating system is that the BIOS establishes interrupt vector tables and interrupt service programs in memory

The BIOS program (0x00000) uses 1KB of memory space (0X00000~0X003FF) to build the interrupt vector table, building the BIOS data area (0X00400~0X004FF) in 256KB of memory space next to its location. and the position (0x0e05b) after approximately 57KB is loaded into some interrupt service programs corresponding to the interrupt vector table about 8KB.

The Interrupt vector table has 256 interrupt vectors, each of which is 4 bytes, of which two bytes is the CS value and two bytes is the IP value. Each of the broken vectors points to a detailed interrupt service program.

Bios-runtime Service Search device according to boot boot order, find BBR

Because the BIOS features are used differently. It consists of two parts: the Post and runtime services. After the post is finished, it will be purged from the memory. However, the BIOS runtime service is reserved for the target operating system.

In order to start the operating system. The runtime service of the BIOS 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). One floppy drive, one 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 typically booted from a hard disk. The MBR (Master boot record) on the hard disk includes the primary 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.

Boot the operating system kernel and prepare for protected mode

The primary boot loader in the MBR is a 512-byte image. This includes not only the bootload program code . Also included is a small partition table .

The initial 446 bytes are the main boot loader, which includes executable code and error message text. The next 64 bytes are the partition table. This includes four separate records for each partition (16 bytes per partition).

The MBR passes the special digital 0xaa55 (translator Note: AA55 is really a legendary figure in the electronics world. You want to know why? Expand it into binary form to see what the rule is, as a two-byte end flag. 0X55AA at the same time is also a valid check confirmation of MBR.

First, the CPU is sent an int 0x19 interrupt, which causes the CPU to perform an int 0x19 interrupt corresponding to the interrupt service program. The purpose of this interrupt service program is to load the first sector of the floppy disk program into the specified location of memory.

The main boot loader job is to find and load the secondary boot loader (kernel loader)

It completes this task by parsing the partition table and locating the active partition when it finds an active partition. It will continue to scan the partitions in the remaining partition table. In order to confirm that they are inactive.

When the confirmation is complete, the activation record of the active partition (boot loader) is read from the device to RAM and executed.

The loading process needs to be completed by using the Interrupt service program provided by the Int 0x13 interrupt vector that the BIOS provides. The program will start the second sector of the floppy disk with 4 sectors. That is, the SETUP.S corresponding program is loaded into the memory setupseg (0x90200).

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). Because grub fixes some of the flaws in Lilo, let's take a look at grub (let's say you want to get a lot of other resources on Grub,lilo and related topics. Please see the following document for reference)

One of the better things about grub is that it includes knowledge of the Linux file system. Unlike Lilo, which uses a bare sector, grub can load the Linux kernel from the 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 stage1.5 's boot loader is loaded and executed. Stage2 's boot loader talent is loaded.

When the Stage2 is loaded. Grub can display a list of optional kernels (defined in/etc/grub.conf) based on the requested condition. At the same time there are several soft symbolic links/etc/grub/menu.lst and/etc/grub.conf). You can choose a kernel. Change the number of 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.

Once the 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 at the same time.

After all is ready, the secondary boot loader will invoke the kernel image and complete the loading of the operating system.

Load kernel and convert from actual mode to protected mode

The kernel phase begins when the kernel image is loaded into memory (the load process still uses the Int 0x13 interrupt vector), and the secondary boot loader releases control.

Load kernel image

The kernel image is not an executable kernel, but a compressed kernel image. Typically it is a zimage (compressed image, less than 512KB) or a bzimage (larger compressed image. Greater than 512KB). It is compressed by using zlib in advance.

In front of this kernel image is a routine that implements a small set of hardware settings, extracts the kernel included in the kernel image, and then puts it into high-end memory, assuming the initial RAM disk image 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 starts execution.

This routine executes some of the major hardware settings and calls the./arch/i386/boot/compressed/head. Startup_32 in S, set a primary environment (stack, etc.) and clear Block Started by Symbol (BSS). Then call a C function called Decompress_kernel (in./arch/i386/boot/compressed/misc.c) to decompress the kernel. When the kernel is decompressed into memory, it can be called.

This is another startup_32 function, but this function is in./arch/i386/kernel/head. S in.

Enter protected mode and initialize
    • Enter protection mode

    • Set interrupt descriptive narrative schedule and global descriptive narrative table

    • Created a memory paging mechanism

Boot the kernel
    • Start_kernel booting the kernel

    • Creating the Init Process

BIOS stage – Prepare the interrupt vector table in real mode and interrupt Service program BIOS background knowledge what is the BIOS

In the early 70, "read-Only Memory" (read-only, abbreviated ROM) was invented. The boot loader is brushed into the ROM chip. After the computer is powered on. The first thing to do is read it.
Computer, the program that starts the chip is called "Basic output Input System" (Input/output System), for short BIOS .

It is a set of programs that solidify onto a ROM chip on the motherboard of a computer. It holds the most important basic input and output program of the computer, the post-boot self-test program and the system self-launcher program, which can read and write the system setup details from the CMOS.

Its main function is to provide the lowest, most direct hardware setup and control for the computer.

BIOS-Stored information

The BIOS chip mainly stores:

    • Self-Diagnostics: Identify the hardware configuration by reading the contents of the Cmosram. And it is self-checking and initializing.

    • CMOS Setup program: During the boot process, start with a special hotkey and set it into CMOS RAM.

    • System Bootstrap loader: After the self-test succeeds, mount the disk in memory relative to the boot program on the 0-channel 0 sector, and let it execute to mount the DOS system.

    • Driver and interrupt services for major I/O devices: Because the BIOS deals directly with the hardware resources of the system, it is always aimed at a certain type of hardware system, and various hardware systems are different, so there are different kinds of bios, with the development of hardware technology. The same BIOS also has a different version number, the new version number of the BIOS than the old version number. More powerful.

BIOS: The first place to read after the computer's power-on self-test is the BIOS (basic input output system, the basic inputs and outputs). The BIOS records the chipset and related settings of the motherboard, such as the frequency of the CPU and interface devices, the search order of the boot device, the information of the hard disk, the system time, memory information, the clock information, the PNP feature, the external bus, the I/O address of various interface devices, and the IRQ interrupt information that has been communicated with the CPU So, start the hypothesis to start smoothly. The first thing to read is the BIOS settings.

The computer will load the BIOS information first, and the BIOS information is so important that the computer must find it at the very beginning.

After the computer is started, the CPU logic circuit is designed to execute only in-memory programs, does not have the ability to execute directly on the floppy disk or the hard disk of the operating system, the false assumption to execute, must be loaded into memory (RAM).

How the BIOS is started

The CPU hardware logic is designed to force the CS value to 0xf000,ip to 0xfff0 at the moment of power-up. This cs:ip points to the 0xffff0 location, which is the entry address of the BIOS program.

BIOS needs to load interrupt vector table and interrupt service program in memory

The BIOS program is cured in a very small ROM chip on the computer's motherboard. Now Cs:ip has pointed to 0xffff0, which means the BIOS starts.

With the implementation of the BIOS program, the screen will display video card information, memory information, the BIOS program in the detection of video card, memory, this is the post during post. One thing that is critical to starting the operating system is that the BIOS establishes interrupt vector tables and interrupt service programs in memory

The BIOS program (0x00000) uses 1KB of memory space (0X00000~0X003FF) to build the interrupt vector table, building the BIOS data area (0X00400~0X004FF) in 256KB memory space next to its location. and the position (0x0e05b) after approximately 57KB is loaded into some interrupt service programs corresponding to the interrupt vector table about 8KB.

The Interrupt vector table has 256 interrupt vectors, each of which is 4 bytes, and two bytes is the CS value. Two bytes is the IP value. Each of the broken vectors points to a detailed interrupt service program.

Work post for BIOS stage

The BIOS program is checked first. Whether the computer hardware can meet the basic conditions of execution, this is called "Hardware self-Test" (power-on self-test). Abbreviated to post.

If the hardware fails, the motherboard will beep with a different meaning and start the abort. Assuming there is no problem, the screen displays information such as CPU, memory, hard disk, and so on.

When the mainframe computer is powered on, it will hear a drop of noise. System startup starts with post ( POST-power on self test ) Self Test start

The process is mainly to detect computer hardware devices such as: CPU, memory. Motherboard, video card, CMOS and other equipment failure exists

If there is a hardware failure, it will be the case in two ways:

    • For a serious failure (fatal failure) The outage, at this time because the various initialization operation is not complete, no matter what hint or signal.

    • For non-serious failure, give a hint or sound alarm signal, waiting for the user to process. Assuming there is no fault, post complete its own relay task, handing the tail work to the BIOS for processing

Load BIOS

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 stored in detail.

In other words, the BIOS needs to have an external storage device sorted. In front of the equipment is the priority to transfer control equipment. This sort of ordering is called the boot sequence (boot Sequence).
Open the BIOS operating interface, there is a "set the boot sequence."

After this. The computer has a spectrum and knows which hardware device to read.

Booting the operating system

After the hardware self-test is complete. We expect the operating system to start, but the problem is.
* Where is the operating system stored?
* How does the BIOS find the operating system?
* How is the BIOS loaded into the operating system?

Background knowledge the boot sequence when multiple operating systems

In order to find the operating system, the BIOS follows the "boot order". Transfer control to first-place storage equipment.

Then. The computer reads the first sector of the device, that is, the top 512 bytes are read.

Suppose the last two bytes of these 512 bytes are 0x55 and 0xAA. Indicates that the device can be used for booting;

Suppose not. Indicates that the device cannot be used for startup, and control is then forwarded to the next device in the boot sequence.

This is the first 512 bytes. is called the master boot record. Abbreviated to MBR)

Master Boot Record MBR

The primary boot loader in the MBR is a 512-byte image. This includes not only the program code, but also a small partition table.

The initial 446 bytes are the primary boot loader. It includes executable code and error message text. The next 64 bytes are the partition table, which includes the individual records of four partitions (16 bytes per partition). The MBR passes the special digital 0xaa55 (translator Note: AA55 is really a legendary figure in the electronics world. You want to know why? Expand it into binary form to see what the rule is, as a two-byte end flag. 0X55AA at the same time is also a valid check confirmation of MBR.

The main boot loader job is to find and load the secondary boot loader.

It completes this task by analyzing the partition table and locating the active partition. When it finds an active partition, it will continue to scan the partitions in the remaining partition tables to confirm that they are inactive. When the confirmation is complete, the boot record of the active partition is read from the device to RAM and executed.

The master boot record has only 512 bytes. Can'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" role. is to divide the hard disk into several zones.

Partition table

Hard disk partitioning has many advantages. Given that each zone can have a different operating system installed, the master boot record must 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: Assuming 0x80, the primary partition is the active partition and control is transferred to the partition. Only one of the four primary partitions can be activated.

    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 byte: The logical address of the first sector of the primary partition.

    6. 第13-16 Bytes: The total number of sectors for the primary partition.

The last four bytes (the total number of sectors in the primary partition) determine the length of the primary partition. Other words. The total number of sectors in a primary partition is not more than 2 32.

Assuming that each sector is 512 bytes, it means that a single partition has a maximum of 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.

Suppose you want to use a larger hard drive. There are only 2 methods:

    • One is to increase the number of bytes per sector.

    • The second is to add the total number of sectors.

      MBR: The primary boot partition block in the first sector of the first bootable device. Included in the boot loader

      boot loader (boot loader): a software that can read kernel files to execute

      Kernel files: Start operating system features

The process of booting the operating system

When it is started by the hard disk. The BIOS is typically the first sector to turn to the first hard drive. That is, the master boot Record (MBR).
Procedures for loading grub and operating systems, including the following steps:

Loading records

The only thing that the basic boot loader does is load the second boot loader.

Loading grub

This second boot loader actually leads to more advanced functionality to allow users to load a particular operating system.

Loading system

such as the Linux kernel. Grub transfers control over the machine to the operating system.

The difference is. The Microsoft operating system is started with a boot method called chained loading, and the master boot record simply points to the first sector of the partition where the operating system is located.

Load Master boot loader-Basic Loader

As we all know, the first sector of the No. 0 track on a hard disk is called an MBR. This is the master boot record, which is the master boot recording. Its size is 512 bytes, although the place is not big, but it is stored in the pre-boot information, partition table information.

In accordance with the system boot process set by the BIOS, assuming that the detection passes, the boot order (boot Sequence) starts on the first device to support the startup program, our boot device mainly includes hard disk, USB, SD and so on. We usually use a hard disk, then read the first device is the hard disk. The first thing to read is the MBR (Master boot record) of the drive's master boot records, and the system is able to perform the core recognition work based on the boot loader (boot Loader) of the ScanDisk installation.

The MBR program simply finds the boot Loader, which is only the first 446 bytes in the hard disk partition, and then looks for the relevant configuration and definitions.

Then give control to the master boot code. The main boot code tasks include

    • Scan the partition table to find an active (bootable) partition.

    • Locate the starting sector of the active partition.

    • Mount the boot sector of the active partition to the memory 7c00;

    • Give control over to the boot sector code.

Loading sub-boot Chronicle program – Advanced loader bootload such as Grub

The system reads the GRUB configuration information in memory (typically menu.lst or grub.lst) and launches a different operating system according to this configuration information.

Then. Control of the computer will be transferred to a partition of the hard disk, which is divided into three kinds of situations.

    • Scenario A: Volume boot record
      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 role of the volume boot record is. Tell the computer where the operating system is located in this partition. The computer is then loaded into the operating system.

    • Case B: Extended partitions and logical partitions
      As the hard drive grows larger. Four primary partitions are not enough and require a lot of other 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". It means that the area is divided into several districts.

      Partitions inside such a partition. is called a "logical Partition" (logical partition).

The computer reads the first sector of an extended partition first. Called "Extended Boot Record" (Extended boot recording. Abbreviated as EBR). It also includes a 64-byte partition table, but there are only two (that is, two logical partitions).

The computer then reads the first sector of the second logical partition, and then finds the location of the third logical partition from the partition table inside, and so on. Until a partition table of a logical partition includes only itself (that is, there is only one partition entry).

Therefore, an extended partition can include countless logical partitions.

But. It seems very rare to start the operating system in this way. Assume that the operating system is indeed installed on an extended partition. Generally used in the next way to start.

    • Case C: Boot Manager

In such a case, after the computer reads the "Master boot Record" 446 bytes of machine code, it no longer transfers control to a partition. Instead, the Boot manager (boot loader) is executed beforehand. The user chooses which operating system to start.

In the Linux environment, the most popular boot manager right now is grub.

Boot Loader is a small program that executes before the operating system kernel executes.

With this applet, we were able to initialize the hardware device and set up a map of the memory space. This brings the system's hardware and software environment to a suitable state in order to get everything ready to finally invoke the operating system kernel.

Boot loader are available in several ways, with Grub, Lilo, and spfdisk as common loader.

Let's take grub as an example to explain, after all, there are not many people with Lilo and Spfdisk.

Why is it so complicated?

The early operating systems were not so complicated. Of course Bootload does not have that much functionality, but today our operating systems are becoming more complex, bootload are becoming larger, and it is now commonplace to install multiple systems on a single computer. As a result, the previous simple bootload has been unable to meet these features.

Both the BIOS and MBR are features that the hardware itself will support, and boot loader is a set of software installed on the MBR by the operating system. Because the MBR is only 446bytes. So this boot loader is very small and perfect. The main tasks of this bootloader are as follows

    • Menu available: User can choose different boot options, which is also an important function of multi-boot

    • Load the kernel file: Point directly to the bootable program segment to start the operating system.

    • Transfer Other loader: Transfer the boot loading function to other loader responsible for

The first two points above are easy to understand, but the 3rd is very interesting! That means you can have more than two boot loader programs in your computer system.

Is it possible? Doesn't our hard drive have just one MBR? However, the boot loader can be installed in the boot sector of each partition in addition to being installed in the MBR.

For example, suppose your personal computer has only one hard disk, which is divided into 4 partitions. Among the first. Windows and Linux are installed in the two partitions respectively. How do you choose to boot up with Windows or Linux? Assume that the MBR is installed with a boot loader that can identify both the Windows and Linux operating systems at the same time. So the entire process such as the following

So here's a summary:

    1. Each partition has its own boot sector

    2. System partitions are first and second partitions

    3. The actual boot-up kernel files are placed in each partition.

    4. Loader only knows the bootable kernel files within the system partition, as well as the other loader.

    5. Loader can direct or indirectly give management authority to a management program

Now think about it. Why people often say, "Suppose you want to install multiboot." It's a good idea to install Windows and install Linux first.

This is because Linux is installed at the time of installation. You can choose to install the boot loader into the boot sector of the MBR or individual partitions, and the Linux loader can set the menu manually, so you can add the Windows boot option to the boot loader of Linux

When Windows installs, his installer will actively overwrite the MBR and the boot sector of its own partition. You have no choice, and he did not let us choose the menu function

Loading the operating system kernel

After the user chooses the kernel to load. The secondary boot loader (GRUB) reads the Linux kernel image from the partition on which the/boot/resides, based on the information set in the/boot/grub.conf configuration file. It then loads the kernel image into memory and gives control over to the Linux kernel.

The Linux kernel gets control and starts doing its job.
* Hardware Detection

    • Unzip yourself and install the necessary drivers

    • Initializing a file system-related virtual device, LVM or raid

    • Mount the root file system and hang it under the root folder

    • After that, Linux loads the INIT program into the process space and the following is the init work

The system reads the memory image according to the path of the kernel image set by grub. and unzip the operation. At this point, the screen will usually output “Uncompressing Linux” a hint. When the decompression core is complete, the screen outputs “OK, booting the kernel” .
The system puts the extracted kernel in memory and invokes the start_kernel() function to start a series of initialization functions and initialize the various devices, completing the establishment of the Linux core environment.

So far. The Linux kernel has been built, and Linux the programs that are based on them should be able to execute properly.

Start the fifth Step-user layer Init sets the execution level according to the Inittab file
After the kernel is loaded. The first procedure to be executed is/sbin/init. The file reads the/etc/inittab file and initializes it based on this file.
In fact, the main function of the/etc/inittab file is to set the Linux execution level, which is set in the form ": Id:5:initdefault:", which indicates that Linux needs to be performed on level 5. Linux execution level settings such as the following:
0: Turn off the machine
1: Single-user mode
2: Multi-user mode with no network support
3: Multi-user mode with network support
4: reserved, not used
5: Multi-user mode with network support with X-window support
6: Another boot system. That is, restart
There is actually a lot of learning about/etc/inittab files.

Init Process Execution Rc.sysinit

After the execution level is set, the first user layer file executed by the Linux system is the/etc/rc.d/rc.sysinit script, which does a lot of work, including setting path, setting the network configuration (/etc/sysconfig/network), Start swap partitions, set/proc, and so on. Suppose you are interested. Be able to see the Rc.sysinit file in/etc/rc.d, the script inside is enough for you to see for a few days

Booting the kernel module

The kernel modules are loaded in detail according to the files under the/etc/modules.conf file or the/ETC/MODULES.D folder.

Executing scripts at different execution levels

Depending on the level of execution, the system executes the corresponding script in RC0.D to RC6.D, completing the corresponding initialization and initiating the corresponding service.

Executive/etc/rc.d/rc.local

You assume that you have opened this file with a word in it, and after reading it, you will have a clear effect on the command:

# This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don’t# want to do the full Sys V style init stuff.

Rc.local is the place where Linux is left to the user to personalize after all initialization work. You can put the things you want to set up and start up here.

Execute/bin/login program, enter login status

At this point, the system has entered the waiting time for users to enter username and password. You are already able to log into the system with your own account.

:)

Linux system boot Process specific explanation-what happened after power-on--linux kernel profiling (0)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.