Android system startup process

Startup process of Android system

  • 1. Kernel startup
  • 2. init startup
  • 3. Zygote process
  • 4. SystemSever process starts
  • 5. SystemSever process
  • 6. Summary of startup process

First, let’s take an overall look at the complete flow chart of Android system startup:


1. Kernel startup

  • After the Android device is powered on, it will first start executing from the startup boot code in the processor’s on-chip ROM. The on-chip ROM will look for the Bootloader code and load it into the memory.
  • Bootloader starts execution. It is first responsible for completing the initialization of the hardware, and then finds the Linux kernel code and loads it into the memory.
  • The Linux kernel starts to boot, initializing various software and hardware environments, loading drivers, mounting the root file system, and executing the init program, thus opening up the world of Android.

2. init startup

When the Linux kernel starts, the first process that runs is init, which is a daemon process. To be precise, it is the first process of user control in the Linux system, so its process number is 1.

After the Linux Kernel is started, the main() method of /system/core/init/Init.cpp will be called to parse the init.rc file (system/core/rootdir/init.rc).

In init.cpp, the order of starting each stage of init.rc is early_init > init > late_init, and late_init will trigger the start of other stages, so the order of starting each stage in init is as follows:

early_init > init > late_init > early-fs > fs > post-fs > late_fs > post-fs-data > zygote-start > early-boot > boot

import /init.environ.rc
import /init.usb.rc
import /init.${<!-- -->ro.hardware}.rc
import /vendor/etc/init/hw/init.${<!-- -->ro.hardware}.rc
import /init.usb.configfs.rc
import /init.${<!-- -->ro.zygote}.rc

on early-init
...
mkdir /dev/memcg 0700 root system
mkdir /dev/memcg/apps/ 0755 system system
mkdir /dev/memcg/system 0550 system system
startueventd

on init
...
mkdir /dev/stune
mkdir /mnt/runtime 0700 root root
\t
on property:sys.boot_from_charger_mode=1
    class_stop charger
    trigger late-init

on load_persist_props_action
    load_persist_props
    start logd
    start logd-reinit

//Start system core services
on late-init
    trigger early-fs
    trigger fs
    trigger post-fs
    trigger late-fs
    trigger post-fs-data

    trigger zygote-start
    trigger load_persist_props_action
    trigger firmware_mounts_complete

    trigger early-boot
    trigger boot

on post-fs
start logd
    start servicemanager
    start hwservicemanager
    start vndservicemanager

on late-fs
    class_start early_hal

on zygote-start & amp; & amp; property:ro.crypto.state=unencrypted
    exec_start update_verifier_nonencrypted
    start netd
    start zygote
    start zygote_secondary

on zygote-start & amp; & amp; property:ro.crypto.state=unsupported
    exec_start update_verifier_nonencrypted
    start netd
    start zygote
    start zygote_secondary

on zygote-start & amp; & amp; property:ro.crypto.state=encrypted & amp; & amp; property:ro.crypto.type=file
    exec_start update_verifier_nonencrypted
    start netd
    start zygote
    start zygote_secondary

//The boot phase will start services with class hal and core
on boot
class_start hal
    class_start core

At this stage, you can see the “Android” logo on the screen of the device (that is, bootanimation). Next, start the zygote process, and the init process will enter the loop state.

3. Zygote process

The Zygote process (framework/bse/cmds/app_process/app_main.cpp) incubates all Android application processes and is the basis of the Android Framework. The startup of this process also marks the beginning of the Framework framework initialization.

Mainly carry out the following matters:

  • Register the JNI function of the underlying function to the virtual machine and start the virtual machine, that is, call the startVm function
  • Preload system classes and resources [preload preload (pre-load load)]
  • fork and start the system_sever core process
  • Serves as a daemon listening for requests to “incubate new processes”

4. SystemSever process startup

The system_sever process is forked from the Zygote process. The following is the system_server startup process.

ZygoteInit.main()
->registerServerSocketFromEnv()
->preload()
->ZygoteInit.forkSystemServer()
->Zygote.forkSystemServer()
->nativeForkSystemServer()
->handleSystemServerProcess()
->ZygoteInit.zygoteInit()
->RuntimeInit.applicationInit()
->findStaticMain()
->new MethodAndArgsCaller(m, argv)//return value of forkSystemServer (implements Runnable)
->r.run()//Execute SystemServer main function
->runSelectLoop()

//r.run()
SystemServer.main()
//First initialize the SystemServer object and call its run() method.
SystemServer.run()
//Start various services

5. SystemSever process

After the SystemSever process is started, it will initialize the system Context (set the theme), create the system service management SystemServiceManager, and then start various system services.

  • Adjust the time. If the system time is earlier than 1970, adjust it to 1970.
  • Initialize Looper to mainLooper
  • Load the library libandroid_server.so. The source code contained in the library is in the frameworks/base/services/ directory.
  • Initialize system Context
  • Create SystemServiceManager to be responsible for system service startup
  • Start various services
  • Call Looper.loop() to enter the message processing loop
startBootstrapServices(); // Start boot services
//Start services ActivityManagerService, PowerManagerService, LightsService, DisplayManagerService, PackageManagerService, UserManagerService.

startCoreServices(); // Start core services
//Start the service BatteryService, which is used to count battery power and requires LightService.
//Start the service UsageStatsService, which is used to count application usage.
//Start the service WebViewUpdateService.

startOtherServices(); // Start other services
//Start the service InputManagerService, WindowManagerService, StatusBarManagerService
//Wait for ServiceManager and SurfaceFlinger to complete startup, and then display the startup interface.
//Get ready window, power, package, display activity services:
// - WindowManagerService.systemReady()
// - PowerManagerService.systemReady()
// - PackageManagerService.systemReady()
// - DisplayManagerService.systemReady()
// - ActivityManagerService.systemReady()

All services will be registered to the ServiceManager after they are started.

After the AMS service is started, it will enter ActivityManagerService.systemReady(), then start SystemUI, WebViewFactory, Watchdog, and finally start the desktop Launcher App.

To start the desktop Launcher App, first a new process will be forked through the Zygote process as the App process, then the Application will be created, and the startup Activity will be created, and finally the user will see the desktop.

6. Summary of startup process

Overview: Loader > Kernel > Native > Framework > Launcher

Subdivision: BootRom > Bootloader > Kernel > init > Zygote > SystemServer > Launcher

  • The Loader layer mainly includes Boot Rom and Boot Loader
  • The Kernel layer is mainly the Android kernel layer
  • The Native layer mainly includes the init process and the forked user space daemon process, HAL layer, boot animation, etc.
  • The Framework layer is mainly the initialization of Services such as AMS and PMS.
  • The Application layer mainly refers to the startup of SystemUI and Launcher