Wednesday 15 July 2020

What happens when you press power button of your Android phone? | How Android boots up?

Hi Guys, 
In this blog, I am going to talk about, how your android phone starts when you press the power button.
Or in short I can say that how Android boots up.

Lets get started.


So what exactly is the boot-up process?


In Computer terminology booting up is a process of starting of computer till it can be used. It can be initiated by hardware such as button press or by software command.


As you can see from the diagram, the android boot-up process consists of 5 main stages:
  1. Boot ROM and Bootloader
  2. Kernel
  3. Init process
  4. System Server and Managers
  5. Launcher
1. Boot ROM and Bootloader
    When we press the power button the BOOT ROM starts executing from a predefined location which is hardwired into the ROM. It loads the Bootloader into the RAM and starts executing.
The bootloader is a small program that does hardware initialization, reading Linux kernel and loading it to the RAM. It setups the initial register and the command line arguments for Linux kernel and jumps to the kernel.
The bootloader can be found at :

<android source>/bootable/bootloader/legacy/usbloader

Bootloader executes in two stages :
  • -> It detects external RAM and loads a program that will help in the second stage.
  • -> The bootloader set up the network, memory, etc which require to run the kernel. 

2. Kernel :
    The bootloader loads kernel binary onto memory and then it executes it. The first hardware-independent piece of code that runs in the kernel is start_kernel(). As the kernel launches, it starts to set up cache, protected memory, scheduling and loading drivers, starts kernel daemons, mount root file system, initializing I/O, starts interrupts and initializes process table. When the kernel finishes the system setup, it looks for "init" in the system files and launch the root process of the System.

3. Init Process: 
    The third stage is the init process. It is the very first process or root process or we can say it is the grandfather process of all processes. It has two main responsibilities.
  1. Mount directories like /sys, /dev , /proc.
  2. Runs init.rc script that resides at <android source>/system/core/rootdir/
The rc file is written in init's own language. Init parse this rc file and launch the Zygote process. Many Linux daemons are started by init such as usbd , installed, adbd, etc. At this stage, you can finally see the Android logo on the screen.

4. Zygote :
    If init is called as the grandfather of all processes, then Zygote is the father of all processes. In Java separate virtual machine(VM) will popup for each separate app. But in the case of Android, VM should run as quick as possible. Now here one question comes in our mind that, for several apps, several instances of a virtual machine would be launched? This would consume a lot of memory. So to overcome this problem Android OS has a system called zygote. It enables code sharing across Dalvik Virtual Machine, thus achieving a lower memory footprint and minimal startup time. Zygote is a daemon started by init process. 
Role of Zygote process :
  1. registerZygoteSocket() : It register a server socket for zygote command connection.
    It keeps polling a socket /dev/socket/zygote. When you launch an Application, the request to create a new process comes from system_server to zygote on this socket.
  2. preloadClasses() : It is a simple text file that contains a list of classes that need to be preloaded.
  3. preloadResources() : Everything that is included in the android.R file will be included with this method.
5. System Server and Managers :  
    After zygote preloads all the classes and resources, the Zygote forks a new process to launch system server. The system server is the core of the Android system. The main entry point from Zygote is new SystemServer().run(). The first thing that happen is the SystemServer will load a native library called android_server, that provides interfaces to native functionality. After setting up the native services it creates a server thread. This thread will start the remaining services like bootstrap services, core services, and other services. Each service is running in a separate Dalvik thread in SystemServer. Once the system services are up and running in memory, Android has completed the boot process. At this time standard "ACTION_BOOT_COMPLETED" broadcast will be fired.


Thanks for reading,
Swetabh

No comments:

Post a Comment

What is Image Stabilization? OIS and EIS explained.

Hi Guys, Today I'm going to talk about image stabilization, what are OIS and EIS? How these works and What are the advantages of having ...