1. The overall design
A question can be obtained from the above figure. Why can't it directly load kernel from boot and jump to run?
The idea of this design
1. Boot must be less than 512 bytes and cannot be multi-functional
2. Kernel needs to run in 32-bit protected mode (assembly + c language)
3. Use loader to transfer: obtain necessary hardware information, and enter the protected mode
. The reconstruction scheme is shown below.
File function definition
common.asm--constant definition, macro definition
blfunc.asm--file loading function definition in real mode
boot.asm--load loader and jump [boot sector]
loader. asm--The operations that need to be performed here are necessary hardware initialization, load the kernel, enter the protected mode, and jump to the kernel for execution
Modifications needed
1. Modify the previously defined inc.asm to common.asm, and modify its related names in the link relationship in the makefile
2. Modify the related header files in loader.asm
3. Function re The realization of the structure-to implement different interfaces [Rewrite the previous code, subdivide the module
blfunc.asm Note
1.%include "blfunc.asm" must be the first "include" statement
2.%include " blfunc.asm "forces execution from the BLMain tag.
3. Buffer is a necessary memory buffer and must be defined at the end of the code
The kernel prototype structure
can be seen from the figure above, kernel.out cannot be directly run by the x86 processor after being loaded into the memory. There are three reasons:
1.kernel.out is an executable program
in the Linux system 2. The executable program in Linux is elf format file-fixed data format
3. The processor only recognizes code and data, and cannot execute the elf executable program.
Improvement methods
1. Extract the code and data segments in the elf file-delete the elf file format information
2. Relocate and extract After the code and data segment, get the kernel file
3. Load the kernel file to the memory-the starting address can be customized
4. Jump to the kernel entry address for execution
Introducing a tool here, there will be three data processing formats-as shown in the figure below, the
required experimental file link https://down.51cto.com/data/2468702
Summary
1. The experimental nasm and gcc compile the elf object file
2.ld assembles the elf target file into an elf executable program
3. Experiments elf2kobj converts the executable program into a kernel file
4. Loads the converted kernel file in real mode
5. After entering the protected mode, executes the jump to the kernel starting position Execute