In the previous article, we know that the INIT process will be started after the kernel initialization. What will this process do? In addition, what else does the kernel need to do? (When is the file system and root storage device initialized ?) Start with file system initialization. I have never understood why I still need a file system with a kernel? After thinking over and over again, I understand the truth that after the kernel is loaded into the memory, it is okay to run the kernel, but if there is no root FS, it is as if there is no hard disk on the PC ...... In addition, many Linux virtual file systems (such as proc, sys, Dev) are affiliated with rootfs, so rootfs is more critical in Linux (the necessary condition is simply ). (FS in the kernel is another huge part) a file system 1 FHS: File System hierachy Standard: The file system layout standard on Linux. For example, what is the USR directory, what is the tmp directory. If you have time, you can see that there are too many Linux OS instances. 2. Common folder layout: it is actually a common file directory on ES:
- Bin; Dev; etc; home; LIB; sbin; TMP; USR; var;
Ii. Post boot: this is the case before execve init, because in the source code:
- Run_init_process ("/usr/bin/init"): The FS layout already exists, that is, the INIT program must be placed in a FS.
The three initinit processes are very important, but the INIT process on Android is relatively simple. Here we will introduce non-android init. Generally, the configuration file it reads is in/etc/inittab (this file does not seem to exist on Ubuntu, so you have to find a fcore system later ). In addition, there is also a concept called run level. See figure 1.
Figure 1 run level
To put it bluntly, the run level divides the system running status into several levels. For example, during shutdown, init needs to perform some operations and reboot needs to perform some operations. I will not introduce init here, but I have a lot of knowledge about Linux system configuration. (Indeed more complex than the android init) 4. In the early initialization process, initial Ram disklk needs to mount a FS. Currently, there are two new and old methods:
- The old method is to use initial RAM disk, also called initrd.
- The new method is to use iniramfs.
These two things are very common. Let's take a good look at them. 4.1 For the initrd function, you need to configure the kernel compilation option.
Arm supports packaging initrd and vmlinux into an image. In fact, only the ARM architecture is supported. (This option should be selected during kernel compilation ). After talking about this, how can we use it?
- Initrd is also an image. When bootloader is started, or bootloader is downloaded to a certain place
- Bootloader tells the kernel the initrd address. Decompress and mount the image when the kernel is started.
- Another method is to put initrd and kernel into an image during compilation. This method is only supported by the ARM architecture. We recommend that you use initramfs. Note: In Android, a single image of kernel + initramfs is used. That is, the second method.
(There are a lot of details here, and we will understand it later by analyzing the source code) When BL starts the kernel, it needs to pass a parameter to LK, that is, to tell LK where the initrd is... it's easy, isn't it? How does KL use this initrd?
- KL first copies the image to the memory based on the initrd address specified by the parameter, decompress it, and mount it/
- Find the linuxrc file in this disk, and then execute the statement "=" in it, which gives us a good plan to customize our es.
- After linuxrc is processed, KL unmount the initrd and load the real root device. (If you do not see it, this initrd is used for initialization, but you can also disable the umount initrd .) The processing here is slightly different. If BL specifies root =/dev/ram0 In the parameter (these statements can be seen in the Code), KL will not execute linuxrc and will not umount initrd. That is, this initrd is the final root file system.
So how to make this initrd? It is actually a gzip package folder .... (This part of the code is in the prepare_namespace function in do_mounts.c) 4.2 initramfs (for details: Refer to kernel/documentations/filesystems/ramfs-rootfs-initramfs.txt) kernel supports this initramfs by default, so when compiling, the entire default initramfs will be placed in the kernel. Initramfs is a cpio package file. I specifically checked the information of cpio. The general usage is to read the information of all files in a directory and the content of all files (possibly directly read data to a buffer), and then write the information to a file. To put it bluntly, it may be a serialization tool. Then LK uses the same method to deserialize and restore the content in the original directory. As mentioned above, lk will have a simple initramfs directory structure by default during compilation. This structure is generated by the kernel/scripts/gen_initramfs_list.sh script. This script is simple: DIR/dev 0755 0 0 NOD/dev/console/0600 0 0 C 5 1dir/root 0700 0 0
) How to create your own initramfs?
- Create a folder, which is similar to the file structure of Linux on a PC. You can also put busybox.
- Find testramfs-depth-print | cpio-ov> testramfs. cpio the input is the file name, and the output is directed to testramfs. cpio through>. You can try it.
- Cpio-IVD <testramfs. cpio. In this way, the content in the testramfs folder can be restored.
Cpio:-O indicates output,-V indicates printing verbose information,-I indicates input, and-d Indicates creating the entire folder structure. If there is no-D, there will be problems. However, with support for Kernel compilation, we do not need to call cpio by ourselves. In the compilation option, there is an initramfs_source that points it to the target folder. This initramfs will naturally be generated during compilation. References: The most important of these references is the last, ols2k-9.ps, download and process to get a PDF, the actual previous paper. This section describes some issues about Linux Startup. I will explain again why I need init RAM disk. FS is generally installed on storage media, and reading these storage media requires drivers. When the kernel is started, it will be very troublesome to load these drivers. Even if you statically compile the driver into the kernel, it is not a perfect solution. So. First, a simple, memory-based FS, so that initialization can proceed smoothly. Finally, after the drivers are loaded, mount the FS on the actual storage. We need to understand that without a FS, lk cannot work normally. The full name of 5-u-boot is das U-boot, which is a widely used bootloader. I will write a special article to introduce UB in the future. Here are two simple points:
- The code structure of UB starts from the CPU start. S. Initialization is performed based on different CPUs, and most of the Code does not need to be modified.
- Then start the board, which is related to the specific board. Now the name is lowlevel_init.s.
(The most difficult part is the initialization of each device, which must be done with the datasheet of the development board.) the following reference books are listed here:
In this case, the URL of sdram.pdf has been moved to: Workshop.