How to compile Linux bootloader

Source: Internet
Author: User

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.

Bootsect_pos [0x1fc] = root_minor;
Bootsect_pos [0x1fd] = root_major;

Root_minor and root_major are the root master and secondary device numbers respectively.

Current display mode:

Bootsect_pos [0x1fa] = 0xff;
Bootsect_pos [0x1fb] = 0xff;

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. Download loaderx.tar.gz from the following address:

References

The Linux/i386 Boot Protocol 2.03

--------------------

Author: Fan xiaoju, Development Engineer of the embedded R & D department of the software design center of Lenovo (Beijing) Co., Ltd. He is interested in Linux kernel, network security, XWindow system, Linux desktop applications, and artificial intelligence systems. You can contact him via xiaoju_f@263.net.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.