Compilation Method of Linuxbootloader-general Linux technology-Linux programming and kernel information. The following is a detailed description. Writing boot loader is an inevitable process for developers who have transplanted linux to other development boards. Writing bootloader is also a challenging task for linux learners. This article analyzes the linux boot protocol and explains in detail how to compile a basic bootloader that can guide the 2.4.20 Kernel on the i386 machine.
1. Overview
Linux runs in protection mode, but it is in real mode when the machine starts to reset. Therefore, the work of writing bootloader is also done in the real mode.
Linux Kernel has a variety of formats, such as the old-fashioned zImage and the new bzImage. The biggest difference between them is the size limit on the kernel. Because the zImage kernel needs to be placed within 1 MB of memory in the real mode, its volume is limited. At present, most of the kernel formats used are bzImage, which has no 1 MB memory limit. The following sections take bzImage as an example.
2. bzImage kernel structure
The bzImage kernel is divided into three parts from the front to the back. The first 512 bytes are called bootsect. This is the bootloader used when a floppy disk is used to guide linux. If it is not booted from a floppy disk, this part is useless, the default values of kernel startup options generated during compilation are stored. The 512 * n Bytes starting from 512 bytes are called the setup part, which is the real mode part of the Linux kernel and runs in the real mode, the main function is to prepare an environment for Linux kernel startup in protected mode. This part will switch to the protection mode and jump to the kernel execution in the protection mode. The last part is the kernel of the protection mode, that is, the Linux kernel in the true sense. The size of n can be obtained from the rear half of bootsect. For details, refer to linux boot protocol.
3. Overview of the guiding process
Step 1: open the refrigerator door; Step 2: Put the elephant in the refrigerator ...... Don't laugh. The process is so simple. First, copy the setup part of the Linux kernel to the address starting from 9020 H: 0, and then copy the kernel in the protection mode to the address starting from 1 MB, then, set the content of the parameter area according to the content of Linux Boot Protocol 2.03. The base address is 9000 H: 0, and then use an ljmp $0x9020, $0 to jump to the setup section, the rest is linux's own ^ _ ^. It's really easy!
4. the linux/I386 BOOT PROTOCOL
This is the Protocol we use to guide linux. It is located in: deleetation/i386/boot.txt. It details all the knowledge needed to guide linux. For CPU of other architectures, there must be something similar, just follow the method in this article.
5. Details 1: Basic boot parameters
Of course, the Linux kernel can be started without specifying any parameters, but this may start into a framebuffer mode that we do not support, resulting in no screen display; it is also possible that the wrong root partition fails to be mounted, resulting in No Init Found's kernel panic. So we must specify something.
If you are a lazy person like me, you can copy the bootsect directly to the 9000 H: 0 location. When using a soft disk boot, the hacker will copy the token to this location. For details, see boot.txt.
The first is the root location. Here, bootsect_pos points to the 9000 H: 0 address.
The two values are equivalent to the value of the guiding parameter vga = 0xHHH, and the two 0xff represent the text mode.
Bootsect_pos [0x210] = 0xff;
This is to set the type of your bootloader. In fact, as long as it is not 0, it means that the loader is too old to guide the new kernel. After setup finds this, it will stop. According to the specification, you should write it as 0xff, which indicates the unknown boot loader. If your bootloader has an officially assigned type id, write your own value.
6. Details 2: how to load the kernel
If your current environment is nothing, you must use the bios interrupt or ATA command to read the hard disk. However, if you have a basic DOS system, you can use the DOS program. In order to be able to operate the entire 4 GB address space, I used WATCOM C to write a small program to read the kernel. However, you can follow the practice in bootsect to read a part in the real mode, then, copy the data to the protection mode to 1 MB or more, and then read some data from the actual mode ...... Note that 1: 9000 H: 0 is also the address space occupied by DOS, so do not return DOS after reading the kernel; otherwise there will be problems;
NOTE 2: it must be a pure DOS. Do not load HIMEM or EMM386, which will cause the above boot process to fail. Loadlin can be used by anyone to eat almost all DOS, but its authors are also very familiar with the memory management in DOS. We are studying these old things and it is difficult to find information. Besides, we are writing bootloader, not DOS killer.
7. Advanced features during boot
1) initrd
Initrd is a small virtual disk at startup. It is generally used to implement a modular kernel. There are two main points to guide initrd:
First, read initrd into the memory. We can put it at the top of the memory in most boot loader methods;
Second, set the start position and length of initrd.
The four bytes starting with bootsect_pos [0x218] are the starting physical address, and the four bytes starting with bootsect_pos [0x21c] are the length of initrd.
2) command_line support
With command_line, you can pass some parameters to the kernel to customize the kernel behavior. In this case, I first put command_line in the address 9900 H: 0, and then put 9900 H: the physical address 0 is stored in four bytes starting with bootsect_pos [0x228. Note that it must be a physical address, so you should put the number of 99000H, and then the kernel will recognize your command_line.
8. Conclusion
Hosts file. I have written an example of loaderx. Using WATCOM C and TASM, WATCOM C is a C compiler that can generate a 4 GB physical address program under DOS, there are also detailed notes and documentation instructions.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.