When we press the phone start button, the phone will start. Then you will see the Logo, start up the animation, and finally enter the mobile phone desktop Launcher, and the mobile phone will start up.

I’ve always wondered, what does this process do? Why do you press a button and the phone starts up?

With more and more understanding of Android, until I read the source code, I gradually answered my doubts. If you have the same question, please read on, I will analyze the Android system boot process from the source point of view.

How does the computer start?

A smartphone is a simplified version of a computer. How does a computer start?

First, get familiar with some concepts. We know that the hardware of a computer includes: CPU, memory, hard disk, video card, display, keyboard, mouse and other input and output devices. All software (such as operating system) is stored in the hard disk, the program execution needs to be read from the hard disk into memory, and then loaded into the CPU to run.

When we press the power button, there is nothing in memory, so we need some way to load the operating system into memory, and the BIOS does that.

BIOS

BIOS: Basic Input/Output System, on IBM PC-compatible systems, is an industry standard firmware interface (wikipedia).

BIOS is generally a program on the motherboard chip, the computer power, the first thing is to read it. After the BIOS program is started, it checks whether the computer hardware meets the basic requirements for operation. This process is called power-on self-test, or POST.

If there is a problem with the hardware in the process of self-check, the motherboard will give out a different meaning of the beep, the start will be suspended. If there is no problem, the screen displays information about CPU, memory, and hard disk. This is what we often see when we press the power button, with prompts quickly scrolling across the screen.

After the hardware self-check is complete, the BIOS passes control to the next stage of the boot program.

The BIOS needs to know on which device the next boot program is stored. That is, the BIOS needs to have a sort of external storage device, the first device is the first to transfer control of the device. This sort is called Boot sort, that is, we usually enter the BIOS interface (such as: F9/F10, etc., here installed system friends should be more familiar with the Boot Sequence) can see.

If we do nothing special, the BIOS passes control to the next storage device in this boot order. This is where we change the boot order when we install a system using a USB drive or CD, giving control that would otherwise be transferred to the usb drive or CD.

Master boot record

The BIOS transfers control to the first storage device in the boot order. After the first storage device is activated, the computer reads the first sector of the device, that is, the first 512 bytes.

The first 512 bytes are called a Master Boot Record (MBR).

The master boot record MBR is a piece of boot code at the front of the disk. It is responsible for judging the validity of partitions and locating partition boot information when the disk operating system reads and writes disks. It is generated when the disk operating system initializes disks.

The hard disk’s master boot record MBR is not owned by any operating system. It is called into memory before all operating systems and takes effect before handing control to the operating system in the primary partition and managing the hard disk with the primary partition information table.

The MBR is only 512 bytes, so it doesn’t hold much. Its main function is to tell the computer where to find the operating system on the hard disk.

The master boot record consists of three parts:

  • Bytes 1-446: the machine code that calls the operating system.
  • Bytes 447-510: Partition table.
  • Bytes 511-512: Master boot record signatures (0x55 and 0xAA).

Among them, the second part “partition table” function, is to divide the hard disk into a number of areas. Hard disk partitioning has many benefits. Considering that each partition can have a different operating system installed, the Master boot Record must therefore know which partition to transfer control to.

The partition table is only 64 bytes long, which is divided into four entries of 16 bytes each. Therefore, a hard drive can be divided into a maximum of four primary partitions, also known as primary partitions.

Of the four primary partitions, only one is active. The computer reads the first sector of the active partition, called a Volume Boot Record (VBR for short). The main purpose of the volume boot record is to tell the computer where the operating system is in the partition.

If the last two bytes of the 512 bytes are 0x55 and 0xAA, the device can be used to boot; If not, the device cannot be used for startup, and control is transferred to the next device in the startup sequence.

After the MBR is loaded to the computer, the computer obtains information such as the file format of the current hard disk, partition status of the hard disk, and storage location of the system disk from the MBR. Then the control is transferred to the partition where the system disk resides.

If multiple systems are installed on the hard disk, after finding an available MBR, the computer reads the first 446 bytes of machine code from the MBR and no longer transfers control to any partition. Instead, it runs a pre-installed Boot Loader, which the user chooses to boot.

In Windows, Boot Manager.

Currently, the most popular boot manager in Linux environments is Grub.

Kernel loading phase

After selecting the operating system, control is transferred to the operating system, whose kernel is first loaded into memory.

In Linux, load the kernel in the /boot directory. After the kernel is successfully loaded, the first program to run is /sbin/init. It generates the init process from the configuration file (/etc/initab on Debian). This is the first user process after Linux is started. The PID process is numbered 1, and all other processes are its descendants.

The init process then loads various modules of the system, such as window programs and network programs, until the /bin/login program is executed, and the login interface is displayed, waiting for the user to enter the user name and password.

At this point, all computer startup process is complete.

Android startup process

Knowing the startup process of the computer, let’s look at the startup process of the Android system. The Android system is based on the Linux kernel, so the startup process is very similar to Linux.

Since Android is an embedded device, there is no BIOS program like on a computer. Instead, there is a Bootloader — a system Bootloader. It is similar to the BIOS. Before the system is loaded, it is used to initialize the hardware device, establish the image map of the memory space, and prepare the environment for the final call to the system kernel.

In Android, there is no hard disk, but ROM, which is similar to a hard disk to store the operating system, user programs, etc. The ROM, like the hard drive, is divided into different areas for storing different programs. In Android, the main partitions are as follows:

  • /boot: Stores the boot programs, including the kernel and memory operators.
  • /system: it is equivalent to the C disk of a computer, storing the Android system and system applications.
  • /recovery: recovery zone. You can access this zone to recover the system.
  • /data: User data area, which contains user data: contacts, SMS, Settings, and user installed programs.
  • /cache: Android cache, which stores the most frequently accessed data and applications.
  • / MISC: contains miscellaneous items such as system Settings and system function enable disable Settings.
  • /sdcard: user’s own storage area, can store photos, music, video and other files.

Bootloader

So how is the Bootloader loaded?

Similar to the computer boot process, when the power button is pressed, the boot chip code starts executing from a predefined place (preset code solidified in ROM), and the ROM on the chip looks for the Bootloader code and loads it into memory (RAM).

The Bootloader then starts executing, and the Bootloader reads the ROM to find the operating system and loads the Linux kernel into RAM.

After the Linux kernel is started, it initializes various software and hardware environments, loads drivers, mounts the root file system, and at the end of the Linux kernel load, it starts and executes the first user-space process, init.

The Linux kernel

The Android system is essentially an operating system based on the Linux kernel, similar to Ubuntu Linux and Fedora Linux. If we want to understand the Android system, we must first understand some knowledge of the Linux kernel.

There are many things about the Linux kernel, and it is impossible to finish all of them in this article. This article mainly introduces the process of starting the Android system, so this article mainly introduces some knowledge related to the kernel starting.

The startup process of the Linux kernel mainly involves three special processes, swapper process (also known as idle process, PID = 0), init process (PID = 1) and kthreadd process (PID = 2), which are the basis of the kernel.

  • The Idle process is the first Linux process and the parent of the init process and the kthreadd process.
  • The init process is the first user process in Linux. It is the ancestor of Android applications, and our apps are all directly or indirectly parent to it.
  • The Kthreadd process is the kernel manager of the Linux system, and all kernel threads are directly or indirectly parent to it.

Idle process

A lot of articles on Android start with init, process number 1, process number 1, is there a process number 0? Well, there is.

This process is called init_task and later degenerates into “idle”. It is the first Linux process (init is the first user process) and the only one that is not created by fork or kernel_thread. Mainly responsible for process scheduling and switching.

The Idle process is the first process in The Linux system. The process ID is 0. After completing the initialization of the system environment, the idle process starts the init process and the kthreadd process.

Kthreadd process

The kthreadd process is created by Idle using kernel_thread and always runs in the kernel space. It is responsible for the scheduling and management of all kernel threads. All kernel threads are directly or indirectly parent to KthreadD.

The init process

The init process is divided into two parts. The first part is started in the kernel, which mainly completes the creation and initialization of the kernel. The content is related to the Linux kernel. The latter part is started in user space and mainly completes the initialization of the Android system.

Android will generally in the root directory to devolve a init executable file, that is to say the init process of Linux system in the kernel initialization is completed, will direct execution init the file, the file of the source code in/system/core/init/init. CPP.

With that said, let’s take a look at what Android has done so far, as shown below:

conclusion

This article introduces a relatively simple content, mainly to take you to understand the computer boot process, understand the Android system from pressing the power button to the init process to start the process, to prepare for the next chapter.

The next chapter will involve the Android system source code, it is recommended that you prepare the system source code, to learn the source code will be more impressive, get twice the result with half the effort.

Android Oreo (8.0)

About source code download

You can refer to the following way to download Android source code:

  • Windows environment download Android source code
  • Official documentation (ladder required) – macOS, Linux environment download Android source code

You can also view the source code online:

  • Android XRef
  • Android OS

Source code viewer

The Source view tool is Visual Studio Code, which IS what I use, and you can use Source Insight but you have to pay for it, and it’s Windows only, so you can choose according to your personal preference.

The resources

  • How does the computer start?
  • After pressing the power button, the computer did a lot of silent work
  • Android startup – Overview

My lot

github.com/jeanboydev

My official account

Technical exchange group

Welcome to join the technical exchange group, to exchange and learn together.