preface

Back then, when I first got into Android, I always wanted to know what the startup process of Android was like. But at that time, on one hand, I didn’t know anything and I didn’t know what to do. On the other hand, I was busy with my work and I didn’t have time. Now, I just have a little time to try to figure out what happened to you when you started up your Android system. This article tries not to get into the code, just the flow.

The body of the

Those who have read my last article “Android System Framework” may still have the impression that Android system can be roughly divided into five layers from top to bottom, namely Applications layer, Framework layer, Native layer and Hardware layer. The Kernel layer. Obviously, among the five layers, the Kernel layer is the one Android devices enter first. So let’s start with this layer and see how Android enters this layer.

The Kernel layer

The so-called Kernel layer is actually the system into the Linux Kernel, and before the system into the Linux Kernel, in fact, also went through a stage, that is the bootloader. In Android system, uboot is a common bootloader program. Here, the MTK platform is used as an example to briefly describe what happens from the machine powering up to the kernel layer. It should be noted that MTK platforms have one more preloader stage than other platforms, which will be discussed below.

Preloader according to MTK is MTK in-house developed loader, which is a loader developed internally by MTK.

First of all, preloader, LK, kernel, and Android image files are stored in Flash, not in CPU chips or DDR.

Then each MTK chip has a Boot ROM (can be viewed as a section of instructions). At power-on time, the Boot ROM starts, and the Boot ROM loads the preloader into the internal SRAM. After preloader is loaded, the program jumps from the Boot ROM to preloader and starts execution. After Preloader initializes the external RAM, Preloader loads LK (or Uboot) into the external RAM. Then jump to LK (or Uboot) to execute, lk (or Uboot) then load bootimage (including kernel and ramdisk) into external RAM, and execute kernel part. The startup process is shown in the figure:

The summary is as follows:

Power on the system >Rom Boot >Pre-loader > LK (U-boot) >kernelCopy the code

From the time the system is powered on to the kernel stage, this is roughly what the system does. Next up is the Linux kernel’s “sky-number” process, init.

init&zygote

The init process is actually the first process on Android because Android is based on the Linux kernel. Init has process number 1, and it’s a very important process with a lot of responsibility. It created Zygote, the creator of the Java world.

So how was the Zygote process created to create the entire Java world?

Here’s how init works:

1. Parse the init.rc file. 2. Execute the actions of the phases in the init.rc file, of which the zygote was created; 3. Call property_init to initialize property-related resources and use property_start_service to start the property service. 4. Init enters an infinite loop and waits for something to happen.Copy the code

Here, we are only concerned with the second stage, the creation of zygote. Since zygote is created by parsing the init.rc file, let’s first look at the init.rc file:

//init.rc
import /init.${ro.zygote}.rc
Copy the code

As you can see from the above statement, init.rc does not import a fixed file directly. Instead, it imports different files based on the contents of the property ro.zygote.

The value of the property ro.zygote has four cases:

Zygote32: pure 32-bit mode ZyGOte32_64: mixed 32-bit mode (32-bit as the main mode, 64-bit as the auxiliary mode) ZyGOte64: pure 64-bit mode ZyGOte64_32: mixed 64-bit mode (64-bit as the main mode, 32-bit as the auxiliary mode)Copy the code

Using zygote32 as an example, let’s look at the zygote32.rc file:

//zygote32.rc
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
class main
socket zygote stream 660 root system
onrestart write /sys/android_power/request_state wake
onrestart write /sys/power/state on
onrestart restart media
onrestart restart netd
writepid /dev/cpuset/foreground/tasks
Copy the code

Start zygote with the start service, and fork a process named Zygote. After zygote is created, the following bin files are executed:

/system/bin/app_process
Copy the code

The corresponding source file for app_process is app_main. CPP. App_main. CPP path is as follows:

/frameworks/base/cmds/app_process/app_main.cpp
Copy the code

And one of the main things we do in app_process is call the start method of AppRuntime. AppRuntime is derived from the AndroidRuntime class, and the AndroidRuntime start method does three main things:

/frameworks/base/core/jni/AndroidRuntime.cpp 1. Create a VM --startVm; 2. Register JNI function --startReg; 3. Call a Java function from JNI, using ZygoteInit's main function.Copy the code

These were three big steps in creating the Java world for Android, and after that zygote officially entered the Java world and spawned the framework’s core system_server process. Then it waits for requests, incubates the app, and creates one child after another. Launcher is a descendant of Zygote.

conclusion

The Android system goes through the following process:

Power on the system >Rom Boot > pre-loader > LK (U-boot) >kernel > Init > Zygote > System_server &appCopy the code

If the startup interface is used to distinguish the logo displayed during power-on and startup, the first stage is Uboot and the second stage is kernel. If you see that the logo has not changed, it is because the logo used in the two stages is the same. The third stage is startup animation. At this time, we have entered the Java world and wait for the completion of the loading of the launcher. Then we can finish the startup animation and enter the launcher interface that we are familiar with.

Here’s a quick rundown of what happens when Android is powered on. There’s no source code analysis involved at all, and it’s just a cursory overview of the process. For example, the initialization of hardware parameters in the uboot phase, such as init, not only starts zygote, but also creates a number of daemons. It also creates a property server for the Android system, such as zygote incubated system_server does. It launches the core Android services…

Android is huge, and everything we know about it is just the tip of the iceberg. Hopefully this document will give you some insight into the android startup process.

interactive

If the article has the wrong description, can leave a message directly, discuss together!

Articles that may be of interest

Android System Framework

Android Studio uses the AIDL interface of the system source code

Android status bar height 0 is still displayed

The last

I also write articles in wechat public number, update more timely, interested people can pay attention to the following public number!