First download the android source code and compile it. There are a lot of online materials. You can refer to this:
Http://www.bkjia.com/kf/201202/118141.html
Follow the steps above to compile the android kernel successfully. If no accident occurs, three files will be generated in the out/target/product/generic Directory, which are ramdisk. img, sytem. img, userdata. img. What are the purposes of these three files? Next, let's analyze it.
First, run the file ramdisk command on the linux terminal. img, print out the following characters ramdisk. img: gzip compressed data, from snapshot), you can see that a new ramdisk is decompressed. img, this ramdisk. img is compressed using cpio. You can decompress it using the cpio command, cpio-I-F ramdisk. img. After decompression, some folders and files are generated. When you see these files, you will understand that they are identical to those in the root directory. It shows that ramdisk. img is actually packaging and compressing the root directory.
The following describes the source of system. img. You can see this text in the 629 lines in build/core/Makefile.
# The installed image, which may be optimized or unoptimized.
#
INSTALLED_SYSTEMIMAGE: = $ (PRODUCT_OUT)/system. img
From this we can see that the system should generate system. img in the $ (PRODUCT_OUT) directory.
Next, there is a copy-file-to-target in row 662, which copies system. img from an intermediate directory to the/generic Directory.
BUILD_SYSTEM is defined in row 636.
The system. img here is not the system. img we see under the/generic Directory, but under another intermediate directory, but the same file. The first copy is to copy the system. img in the out/target/product/generic/obj/PACKAGING/systemimage_unopt_intermediates directory to the/generic Directory.
Now that we know the origins of system. img, we need to analyze what it is and what it contains ??
Makefile line624
$ (BUILT_SYSTEMIMAGE_UNOPT): $ (INTERNAL_SYSTEMIMAGE_FILES) $ (INTERNAL_MKUSERFS)
$ (Call build-systemimage-target, $ @)
Here, build-systemimg-target Makefile line605 is called.
Ifeq ($ (TARGET_USERIMAGES_USE_EXT2), true)
# Generate an ext2 image
# $ (1): output file
Define build-systemimage-target
@ Echo "Target system fs image: $(1 )"
$ (Call build-userimage-ext2-target, $ (TARGET_OUT), $ (1), system ,)
Endef
Else # TARGET_USERIMAGES_USE_EXT2! = True
# Generate a yaffs2 image
# $ (1): output file
Define build-systemimage-target
@ Echo "Target system fs image: $(1 )"
@ Mkdir-p $ (dir $(1 ))
* $ (Hide) $ (MKYAFFS2)-f $ (TARGET_OUT) $(1 )*
Endef
Endif # TARGET_USERIMAGES_USE_EXT2
The definition of TARGET_USERIMAGES_USE_EXT2 cannot be found !!! However, from the above analysis, we can infer that it should be the yaffs2 file system.
Among them, MKYAFFS2 :( core/config. mk line161)
MKYAFFS2: = $ (HOST_OUT_EXECUTABLES)/mkyaffs2image $ (HOST_EXECUTABLE_SUFFIX)
Define MKYAFFS2 as an executable file mkyaffs2image in the directory/media/disk/mydroid/out/host/linux-x86/bin. Run this program to get the following information:
Lzj @ lzj-laptop:/media/disk/mydroid/out/host/ linux-x86/bin $./mkyaffs2image
Mkyaffs2image: image building tool for YAFFS2 built Nov 13 2009
Usage: mkyaffs2image [-f] dir image_file [convert]
-F fix file stat (mod, user, group) for device
Dir the directory tree to be converted
Image_file the output file to hold the image
'Convert' produce a big-endian image from a little-endian machine
We know that this program can generate a file system image for yaffs2. The above functions of * $ (hide) $ (MKYAFFS2)-f $ (TARGET_OUT) $ (1) * are also clarified, convert the TARGET_OUT directory to the yaffs2 format and output it to/media/disk/mydroid/out/target/product/generic/obj/PACKAGING/systemimage_unopt_intermediates/system. img (that is, the system we finally see in the/generic Directory. img ).
Now we know about the generation process of system. img. To find out the content in system. img, we need to analyze the content in the TARGET_OUT directory. (I want to mount system. img to linux to see what is in it, but it does not support the yaffs and yaffs2 file systems !!!)
Next: Analyze TARGET_OUT and find the definition of TARGET_OUT In the build/core/envsetup. sh file (line205:
TARGET_OUT: = $ (PRODUCT_OUT)/system
That is, the system directory under the/media/disk/mydroid/out/target/product/generic Directory.
Lzj @ lzj-laptop:/media/disk/mydroid/out/target/product/generic/system $ tree-L 1
.
| -- App
| -- Bin
| -- Build. prop
| -- Etc
| -- Fonts
| -- Framework
| -- Lib
| -- Usr
'-- Xbin
Now everything is clear, we finally see the system. the imgfile is an image of the system directory under this directory. It is similar to the linux root file system image and contains android applications, configuration files, fonts, and so on.
Userdata. img comes from the data directory. By default, there is no file in it.
Android Startup Process
During Android startup, UBOOT will pass in an init parameter. This init parameter specifies the first program to run during startup. The default value is the init program, which is stored in ramdisk. img. You can analyze its code to see what initialization tasks have been done in it. Its source file is in system/core/init. c.
It will call init. rc initialization file, which is in the out/target/product/generic/root directory. After the file is started, we will find that the root directory is read-only and the sdcard owner is system, in this file, you can modify it to implement the root directory's read/write.
By analyzing these files, we can also find that ramdisk is first loaded when android is started. the img image is mounted to the/directory, and a series of initialization actions are performed, including creating various Required Directories, initializing the console, and enabling services. System. img is used to specify some script commands in init. rc. It is parsed through init. c and mounted to the/system directory under the root directory.
From the column of andy_android