Linux bootloader write method

xiaoxiao2021-03-06  21

For those who transplanted Linux to other development boards, writing Boot Loader is an inevitable process. For those who learn Linux, writing bootloader is also a very challenging job. This paper analyzes how to prepare a basic bootloader that can be guided on the I386 machine in detail by analyzing the Linux boot protocol. 1. Overview Linux runs in the protection mode, but is in real mode when the machine starts reset. So Write bootloader's work is also under real mode. Linux's kernel has a variety of formats, vintage zimage and new Bzimage. The biggest difference between them is limitations for the size of the kernel. Since the Zimage core needs to be put within 1MB of the real mode, its volume is limited. The core format currently used is BZImage, which does not have 1MB of memory restrictions in this format. The following sections hereinafter mainly analyzed as examples in BZImage. 2. The structure of the Bzimage format kernel is divided into 3 parts from the previous one before, the first 512 bytes are called bootsect, which is the bootloader used when the floppy boot Linux, if not booting from the floppy disk, this part is useless, There is a default value for the kernel start options generated when compiled. 512 * n bytes starting from 512 bytes are called the setup section, this is the real-mode part of the Linux kernel, which is run in real mode, the main function is to start the preparation environment for the Linux kernel of the protection mode. This part will finally switch into the protection mode, jump to the kernel execution of the protection mode. The final part is the kernel of the protection mode, which is the true Linux kernel. Where n's size can be obtained from the second half of the bootsect, detailed address can be referred to Linux Boot Protocol. 3. Overview of the boot process, open the refrigerator door; the second step is put into the refrigerator ... Don't laugh, the process is so simple. First, you need to copy the setup part of the Linux kernel to the address started by 9020h: 0, then copy the kernel's core to the 1MB start address, and then set the contents of the parameter area according to the content of Linux Boot Protocol 2.03, the base address is 9000h: 0 Finally, use a LJMP $ 0x9020, $ 0 jump to the setup segment, the rest is Linux yourself ^ _ ^, really simple! 4.The Linux / i386 Boot Protocol This is the protocol we use to boot Linux, its location in Documetation / i386 / boot.txt. The inside has written all the knowledge you need to know to boot Linux. For the CPU of other architectures, there must be similar stuff, which will be like this method. 5. Details 1: Basic boot parameters Of course, we don't specify any parameter Linux kernel can also start, but it is possible to start entering a FrameBuffer mode we don't support, causing no screen display; it is also possible that Mount has failed, Kernel PANIC leads to no init found. So we have to specify something. If you are a lazy person like me, you can directly copy the bootsect to the location of 9000H: 0. When you use the floppy to boot, it will copy yourself to this place, which is some default settings, please see Boot.txt for details. . The first is the location of the root, where bootsect_pos points to the address of 9000h: 0.

Bootsect_pos [0x1fc] = root_minor; bootsect_pos [0x1fd] = root_major; where root_minor and root_major are the host number and secondary device number of root, respectively. Current display mode: bootsect_pos [0x1fa] = 0xff; bootsect_pos [0x1fb] = 0xff; these two values ​​are equivalent to the value of the boot parameter VGA = 0XHHH, two 0xFF represents text mode. Bootsect_pos [0x210] = 0xff; this is in setting your bootloader type, in fact, as long as it is not 0, because 0 represented Loader is too old unable to boot new kernel, setup discovers this will stop. According to the specification, you should write 0xFF, which means unknown Boot Loader, if your bootloader has got an officially assigned type ID, then write its own value. 6. Details 2: How to load the kernel If your current environment is nothing, you must use the BIOS interrupt or the ATA instruction to read the hard drive, but if you have a basic DOS system in your hand, you can use the DOS program. In order to operate the entire 4GB address space, I used Watcom C to write a small program reading kernel, but you can read a part in the real model, then enter the protective mode to 1MB, then then Read part from the real mode ... Need to note 1: 9000h: 0 is also the address space occupied by DOS, so don't return DOS after reading the kernel, otherwise there will be a problem; pay attention 2: must ensure that it is pure DOS, do not load HIMEM or EMM386 Such things, they will fail the above guidance process. Loadlin is an almost all DOS, but its author is also a big cow in this area, and is very familiar with memory management under DOS. We now study these ancient things is hard to find information, and we are writing bootloader, not dos killer ^ _ ^. 7. Advanced Features of Boot Time 1) Initrd Initrd is a small virtual disk at startup, which is generally used to implement modular kernels. There are two main points of the method of booting the initrd: First, read the initrd into memory, we can put it on the most boot loader method to put it in the highest end of memory; second, set the starting position and length of Initrd and length BootSect_pos [0x218] The 4 bytes started were released by the starting physical address, and the 4 bytes started by bootsect_pos [0x21c] were released in initrd. 2) Command_line support with command_line You can give some parameters to the kernel, customize the behavior of the kernel. I did this, first put your command_line in the address of the 9900h: 0, then store the physical address of 9900h: 0 in 4 bytes starting in bootsect_pos [0x228]. Note It must be a physical address, so you should put the number of 99000h, then the kernel will identify your command_line. 8. The purpose of ending the text is mainly to illustrate the principle of bootloader with the least language and the shortest time, and the real authority is still to see the Linux kernel source and the boot.txt file. I have written an example loaderx, using Watcom C and Tasm, Watcom C is a C compiler that can be generated under the DOS to access the 4GB physical address program, which also has detailed comments and document descriptions. Can be downloaded from below: loaderx.tar.gz References The Linux / i386 Boot Protocol 2.03

-------------------- Author: Fan Xiao Ju, Lenovo (Beijing) Co., Ltd. R & D of embedded software design center at development engineers, research interests include the Linux kernel, network security, XWindow System, Linux desktop application, artificial intelligence system. You can contact him through xiaoju_f@263.net.

转载请注明原文地址:https://www.9cbs.com/read-42982.html

New Post(0)