In Depth : Android Boot Sequence / Process
What is Android boot sequence ?
What is linux kernel ?
What is different between desktop linux kernel and Android linux kernel ?
What is bootloader ?
What is Zygote ?
What is x86 and ARM linux ?
What is init.rc ?
What is System Server ?
Many questions pop-up in mind when we think about Android boot sequence.
Android Boot Sequence / Process |
When power start Boot ROM code start execution from pre defined location which is hardwired on ROM. It load Bootloader into RAM and start execution
Step 2 : Bootloader
Bootloader is small program which runs before Android operating system running. Bootloader is first program to run so It is specific for board and processor. Device manufacturer either use popular bootloaders like redboot,uboot, qi bootloader or they develop own bootloaders, It’s not part of Android Operating System. bootloader is the place where OEMs and Carriers put there locks and restrictions.
Bootloader perform execution in two stages, first stage It to detect external RAM and load program which helps in second stage, In second stage bootloader setup network, memory, etc. which requires to run kernel, bootloader is able to provide configuration parameters or inputs to the kernel for specific purpose.
Android bootloader can be found at
2. main.c - Initializes hardware (clocks, board, keypad, console), creates Linux tags
Refer this link to know more about Android bootloader :
https://motorola-global-portal.custhelp.com/app/answers/detail/a_id/86208/~/bootloader-frequently-asked-questions
Step 3: Kernel
Android kernel start similar way as desktop linux kernel starts, as kernel launch it start setup cache, protected memory, scheduling, loads drivers. When kernel finish system setup first thing it look for “init” in system files and launch root process or first process of system.
Step 4: init process
init it very first process, we can say it is root process or grandmother of all processes. init process has two responsibilities 1. mount directories like /sys, /dev, /proc and 2. run init.rc script.
- init process can be found at init : <android source>/system/core/init
- init.rc file can be found in source tree at <android source>/system/core/rootdir/init.rc
- readme.txtfile can be found in source tree at <andorid source>/system/core/init/readme.txt
Android has specific format and rules for init.rc files. In Android we call it as “Android Init Language”
The Android Init Language consists of four broad classes of statements,which are Actions, Commands, Services, and Options.
Action : Actions are named sequences of commands. Actions have a trigger which is used to determine when the action should occur.
Syntax
on <trigger>
<command>
<command>
<command>
Service : Services are programs which init launches and (optionally) restarts when they exit. Syntax
service <name> <pathname> [ <argument> ]*
<option>
<option>
...
Options : Options are modifiers to services. They affect how and when init runs the service.
Let’s take a look of default init.rc file. Here I have listed only major events and services.
Action / Service | Description |
on early-init | Set init and its forked children's oom_adj. Set the security context for the init process. |
on init | setup the global environment Create cgroup mount point for cpu accounting and many |
on fs | mount mtd partitions |
on post-fs | change permissions of system directories |
on post-fs-data | change permission of /data folders and sub folders |
on boot | basic network init ,Memory Management ,etc |
service servicemanager | start system manager to manage all native services like location, audio, shared preference etc.. |
service zygote | start zygote as app_process |
At this stage you can see “Android” logo on device screen.
Step 5: Zygote and Dalvik
1. Load ZygoteInit class,
Source Code :<Android Source> /frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
2. registerZygoteSocket() - Registers a server socket for zygote command connections
3. preloadClasses() - “preloaded-classes” is simple text file contains list of classes that need to be preloaded, you cna find “preloaded-classes” file at <Android Source>/frameworks/base
4. preloadResources() - preloadReaources means native themes and layouts, everything that include android.R file will be load using this method.
Step 6: System Service or Services
Other services
Step 7 : Boot Completed
Once System Services up and running in memory, Android has completed booting process, At this time “ACTION_BOOT_COMPLETED” standard broadcast action will fire.