Zimage kernel image decompression process
Author:Liu Hongtao, a lecturer at Huaqing vision embedded training center.
In this paper, the linux-2.6.14 kernel runs on the S3C2410 platform as an example to explain the kernel decompression process.
After the kernel is compiled, A zimage kernel image file is generated. The process of loading zimage to the kernel by Bootloader and redirecting to the zimage start address to run zimage is easy to understand. However, it is not so easy to understand how zimage is extracted. This article will explain the zimage decompression process with some key code.
Let's take a look at the composition of zimage. After the kernel is compiled, zimage is generated under ARCH/ARM/boot.
In ARCH/armboot/makefile:
$ (OBJ)/zimage: $ (OBJ)/compressed/vmlinux force
$ (Call if_changed, objcopy)
It can be seen that zimage is obtained in binary mode of elf arch/ARM/boot/compressed/vmlinux.
In ARCH/armboot/compressed/makefile:
$ (OBJ)/vmlinux: $ (OBJ)/vmlinux. LDS $ (OBJ)/$ (head) $ (OBJ)/piggy. O/
$ (Addprefix $ (OBJ)/, $ (objs) Force
$ (Call if_changed, LD)
$ (OBJ)/piggy.gz: $ (OBJ)/../image force
$ (Call if_changed, Gzip)
$ (OBJ)/piggy. O: $ (OBJ)/piggy.gz force
Here, the image is obtained by the vmlinux binary in the kernel top-level directory. Note: ARCH/ARM/boot/compressed/vmlinux is location-independent, which helps you understand the subsequent code ., There is a-FPIC parameter in the Link options:
Extra_cflags: =-FPIC
To sum up the composition of zimage, it is composed of a compressed kernel piggy. o that connects to the Code (head. O Misc. O) of the initialization and decompression functions.
Next, let's take a look at the startup of the kernel. Where does the kernel start to run? Of course, it depends on the LDs file. Zimage has undergone two major links: one is the generation of top-level vmlinux, which is composed of arch/ARM/boot/vmlinux. LDS (this lDs file is composed of arch/ARM/kernel/vmlinux. lDs. the other generation is arch/ARM/boot/compressed/vmlinux, which is generated by arch/ARM/boot/compressed/vmlinux. LDS (this lDs file is composed of arch/ARM/boot/compressed/vmlinux. lDs. in. Zimage entry points should be determined by arch/ARM/boot/compressed/vmlinux. LDS. The entry point is '_ start'
Output_arch (ARM)
Entry (_ start)
Sections
{
. = 0;
_ Text = .;
. Text :{
_ Start = .;
* (. Start)
* (. Text)
......
}
Find the entry point in arch/ARM/boot/compressed/head. S.
Let's see what kind of work head. s will do:
• For debugging output settings of Various arm CPUs, you can define macros for unified operations;
• Set the start and end addresses of the kernel and save the architecture ID;
• If the CPU above arm2 is in normal user mode, it is upgraded to Super User mode, and then disconnected
• Analyze the Delta offset of the lc0 structure to determine whether the kernel address needs to be reloaded (R0 deposit offset to determine whether R0 is zero ).
• The kernel address needs to be reloaded to add the R0 offset to each item in the BSS region and got table.
For location-independent code, the program accesses the global data destination through the got table, that is, the absolute address of the global data destination is recorded in the got table, so each item must be reloaded.
• Clear BSS stack space r2-r3
• Set the cache required for C program running
• R2 indicates the end address of the cache, R4 indicates the last execution address of the kernel, and R5 indicates the start address of the kernel object.
• Use the decompress_kernel () function of the file Misc. C to decompress the kernel at the end of the cache (after the R2 address ).
You may not be clear about the dynamic decompression process after reading the text description above. Use a chart to describe the code handling and decompression process. Then we will discuss some key processes in the middle.
Assume that the initial address of zimage in the memory is 0x30008000 (this address is determined by Bootloader and the location is not fixed)
1. Initial status
|
. Text |
Starting from 0x30008000, including piggydata segments (that is, compressed kernel segments) |
|
. Got |
? |
|
. Data |
? |
|
. BSS |
? |
|
. Stack |
4 K size |
2. After head. s calls decompress_kernel in Misc. C
|
. Text |
Starting from 0x30008000, including piggydata segments (that is, compressed kernel segments) |
|
. Got |
? |
|
. Data |
? |
|
. BSS |
? |
|
. Stack |
4 K size |
|
Extract the buffer required by the function. |
64 K size |
|
Decompressed kernel code |
Less than 4 m |
3. Some code in head. s will be relocated.
|
. Text |
Starting from 0x30008000, including piggydata segments (that is, compressed kernel segments) |
|
. Got |
? |
|
. Data |
? |
|
. BSS |
? |
|
. Stack |
4 K size |
|
Extract the buffer required by the function. |
64 K size |
|
Decompressed kernel code |
Less than 4 m |
|
Code for partial relocation in head. s |
Reloc_start to reloc_end |
4. Jump to the reloc_start after the relocation, copy and unzip the kernel code from reloc_start to reloc_end to 0x30008000, and call call_kernel to jump to 0x30008000 for execution.
|
Decompressed Kernel |
Starting from 0x30008000 |
After learning about the dynamic process through head. S, you may have several problems:
Question 1: How does zimage know that its last running address is 0x30008000?
Question 2: when calling the decompress_kernel function, what are the four parameters and their physical meanings?
Question 3: How does the decompression function determine the location of the compressed kernel in the code?
First answer 1st questions
This address is determined by the makefile and the link script. In the ARCH/ARM/MAKEFILE file
Textaddr-Y: = 0xc0008000 this is the virtual address for Kernel startup.
Textaddr: = $ (textaddr-y)
In ARCH/ARM/mach-s3c2410/makefile. boot
Zreladdr-Y: = 0x30008000. This is the running address of zimage.
In the ARCH/ARM/boot/MAKEFILE file File
Zreladdr: = $ (zreladdr-y)
In the ARCH/ARM/boot/compressed/MAKEFILE file File
Zreladdr = $ (zreladdr)
In ARCH/ARM/boot/compressed/makefile
. Word zreladdr @ r4
The kernel uses this method to let the code know the final running position.
Next, I will answer 2nd questions.
Decompress_kernel (ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
Int arch_id)
L output_start: the starting position of kernel output after decompression. The value of output_start refers to the chart above, which is immediately after the buffer is extracted;
L free_mem_ptr_p: Start address of the memory buffer required by the decompression function;
L ulg free_mem_ptr_end_p: the memory buffer end address required by the decompression function, 64 kB in total;
L arch_id: Architecture ID. The value of smdk2410 is 193;
Last, I answered 3rd questions.
First, let's take a look at how Piggy. O is generated in arch/ARM/boot/compressed/makefie.
$ (OBJ)/piggy. O: $ (OBJ)/piggy.gz force
Piggy. O is generated by piggy. S. Let's look at the content of piggy. S:
. Section. piggydata, # alloc
. Globl input_data
Input_data:
. Incbin "arch/ARM/boot/compressed/piggy.gz"
. Globl input_data_end
Input_data_end:
Let's take a look at the decompress_kernel function in Misc. C. It will call gunzip () to decompress the kernel. Gunzip () is defined in lib/inflate. C. It will call nextbyte () and then call get_byte () to obtain the compressed kernel code.
In Misc. C
# Define get_byte () (inptr <insize? Inbuf [inptr ++]: fill_inbuf ())
View the fill_inbuf Function
Int fill_inbuf (void)
{
If (insize! = 0)
Error ("ran out of input data ");
Inbuf = input_data;
Insize = & input_data_end [0]-& input_data [0];
Inptr = 1;
Return inbuf [0];
}
Nothing found? Isn't input_data exactly the input_data in piggy. S? How should we determine the position of piggy.gz in zimage.
The time relationship may not be detailed enough. You can collect kernel code and other related articles on the Internet to understand how to start the decompression process.