In the era of mobile phone flooding and rapid development, in the stage of various rapid configuration, show functions, show highlights, large screen + large battery has become the standard, and mobile phone is the most important social tool, people’s mobile phone has rarely turned off, when every minute counts, we do not want to waste the 1 minute boot time. Hopefully, by sliding the fingerprint unlock pattern or touching the fingerprint reader, you can unlock the phone with your cute and handsome face and use it directly. Many people seem to forget that cell phones can be turned on and off. Today, let’s talk about the process of starting the Android phone boot system.
I. Overview of the Startup process of Android system
The startup of Android system includes the following parts: Power on to load the boot program, boot program bootloader to start the Linux kernel, and then the kernel starts the init process, init process starts, open Zygote incubator, enter the framework layer, and create the SystemServer process, SystemServer start, Start various system services, then start the Launcher, load the desktop, display the installed application ICONS, and start.
Second, detailed explanation of the start-up process
1, power load boot program
When the power button is pressed, the boot program starts execution from the predetermined place, booting up part of the hardware and loading the Bootloader into RAM.
2. Boot program Bootloader
Bootloader is a program before the Android system starts. It mainly contains the code executed by the processor during the startup phase. The purpose is to load the compressed kernel zImage and Ram disk into memory. Once loaded, the system controller is transferred to the zImage entry point. Most manufacturers choose LK(Little Kernel) boot loader, LK mainly achieved the following functions:
- Basic hardware support
- Find and start the kernel
- Basic UI (a simple interface that can be accessed by pressing some key combination before system startup, similar to recovery mode)
- Supports console for serial ports
- The device can be used as a USB device (largely thanks to the Fastboot protocol)
- Flash partition support
- Support digital signature
For security reasons, the Bootloader on Android devices is usually locked to prevent the phone from being root (Huawei does not provide unlock codes).
3. The Linux Kernel starts
Since the kernel loaded by Bootloader is still compressed, it needs to be decompressed before it can be formally entered into the kernel. The kernel loads many subsystems after startup, so a solution to the initCall mechanism is called: eight initialization levels are defined, and the kernel_init thread can be called one by one.
level | annotation |
---|---|
0 early | Generate initialization helper threads, such as RCU and work queues |
1 core | Provided for use by core subsystems like Sockets |
2 postcore | Used by BDI (Block device brush thread) and KObjects |
3 arch | Initialization work related to processor architecture |
4 subsys | It is used by subsystems such as BIO and Sound |
5 fs | This document is used by the file system |
6 device | Available for drivers and common modules |
7 late | Work usage allocated for initialization of the last phase of the startup (advanced memory processing, oops handler, etc.) |
In short, what the kernel does is set up the cache, load the driver, and finally find the init.rc file and start the init process.
4. The init process starts
The init process does a lot of work after it starts. The most important work is as follows:
- Create and mount file directories such as /dev, /proc and /sys.
- Call the property_init() function to create a shared memory area and initialize and storage system properties
- Detects whether the device is in “charge mode”, if it is in “charge mode” many initialization phases will be skipped, otherwise the normal startup process will take place.
- Parse the init. rc configuration file and start the Zygote process
System properties provide a repository of configuration file Settings that can be accessed globally. System attributes include network Settings, all hardware Settings, domain name resolution module, security module, log, Bluetooth, and power supply. The init.rc configuration file contains five types of statements: Action, Command, Service, Option, and Import. The init.rc file records operations that may be required during system startup at different startup stages.
5. Zygote starts
Zygote process, also known as an incubator, is mainly used to create Dalvik VIRTUAL machine, ART, and application processes to run the SystemServer process of system critical services. Zygote forks the application and SystemServer processes and creates a copy of the instance in the DVM. The Zygote startup script is imported from the init.rc text. This script records which startup function is used and which processes are restarted when Zygote restarts. The file init.zygote32.rc is as follows:
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server class main priority -20 user root group root readproc socket zygote stream 660 root system onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart audioserver onrestart restart cameraserver onrestart restart media onrestart restart netd onrestart restart wificond writepid /dev/cpuset/foreground/tasksCopy the code
It can be concluded that the Zygote process is mainly started from the main function in app_main. CPP, in which the main function will determine whether the current process is in the Zygote process, if so, the start() function of AppRuntime will be called. The start() function is actually the start() function in AndroidRuntime.cpp. This function calls the main method in the zygoteinit. Java file via JNI, so Zygote moves from Native layer to Java framework layer. Then in ZygoteInit’s main method, we do four things:
- Create a Server Socket
- Preload classes and resources
- Start the SystemServer process
- Wait for AMS request to create a new application process
In short, starting the Zygote process does several things:
- Create the DVM and register the JNI methods for the Java Virtual machine
- Enter Zygote’s Java framework layer by calling ZygoteInit’s main function from JNI
- Create a server Socket
- Start the SystemServer process
6. The SystemServer process starts
The Binder thread pool is created in this thread and the SystemServer main method is entered: Binder thread pools can be used to communicate with other threads. Binder thread pools can be used to communicate with other threads.
- · ActivityManagerService: responsible for starting, switching, scheduling, etc. · LightsService: Management and display backlight LED · UserManagerService: User mode management
- Core services: · BatteryService: battery-related service · WebViewUpdateService: webView update service
- Other services: · CameraService: CameraService · BluetouthService: bluetooth management service · AudioService: audio related management service
7. Launcher process
The final step in starting your system is to use the program Launcher to display all the programs you have installed on your system on your desktop. During the startup process, the PackageManagerService is asked to return information about installed applications in the system, and this information is encapsulated into shortcut ICONS and displayed on the screen. Launcher has the following functions:
- Used to start the application
- ICONS for displaying and managing applications.