Загрузка...

Linux 04: Demystify boot process (2) - boot.img

Before we dive into GRUB 2 source code, let's take a look at the important files we mentioned before in Linux file system. In Ubuntu terminal, go to /boot directory. List the files. You will see three Linux OS related files: System.map, initrd.img (initial RAM disk), vmlinuz (zipped Linux kernel executable). Under /grub subdirectory, it's GRUB configuration file (grub.cfg). Under /i386-pc subdirectory, there are three kinds of files: .lst, mod, img. We are interested in these 3 files: boot.img, core.img, and normal.mod.

Now we know where those files related to boot process are. Let's download GRUB 2 source code from github website:
https://github.com/rhboot/grub2

At the time I prepared this video, the version is 2.04.

You can select zip file format. After download and unzip, I open the source code in Visual Studio Code.

To understand how boot.img and core.img files are installed on the computer in the first place. We will look at the source code for grub-install utility. Open grub-install.c under /util directory. It's written in C language, has around 2,000 lines of code. We will directly jump to main function. It loads /etc/default/grub file, which is the base for grub configuration file located at /boot/grub. We will skip most of the code, and go directly to the section related to boot.img and core.img. At line number 1688, this block of switch statement performs the platform-dependent installation. If install_bootsector flag is true, grub_util_bios_setup() function is called to install boot.img and core.img. If the program can reach the end of main function, at line 1957, this message is displayed: "Installation finished. No error reported.".

That's all I want to cover on grub-install source code. Now we have boot.img, core.img along other boot-related grub files installed on Linux computer.

Next, we will look at the source code for GRUB stage 1 file boot.img. The source file boot.S is located at /grub-core/boot/i386/pc/. It's written in assembly language using AT&T syntax. Go to starting point defined with directive global at line 119. It is loaded at 0x7c00. The first statement is to jump to label LOCAL(after_BPB). BPB stands for BIOS Parameter Blocks. Disable interrupt with cli or Clear interrupt flag. Go to label real_start to set up the real stack. Enable interrupt with sti or Set Interrupt flag. Then we can call interrupt 0x13 function 0x41 to check if LBA or Logical Block Addressing is supported. It requires register BX is set to magic number 0x55AA. If it is supported, the code at section LOCAL(lba_mode) will be executed. Interrupt 0x13 function 0x42 is called. Register si should be set as: offset 8 and 12 bytes for low and high word of the start of the sectors to be read, offset 6 for the memory buffer to which sectors will be transferred. This block of code can be interpreted as:
(1) read from LOCAL(kernel_sector) that is defined as 1, which is the first sector of core.img, in other words, diskboot.img sector.
(2) then save to GRUB_BOOT_MACHINE_BUFFER_SEG, which is 0x7000.

After boot.img is loaded to 0x7000, jump to label LOCAL(copy_buffer). In this section, boot.img is copied to GRUB_BOOT_MACHINE_KERNEL_ADDR or 0x8000 with rep instruction or repeat string operation, and movsw instruction or move word operation.

At the end of boot.s, it sets the magic number or boot signature 0x55AA as the last 2 bytes.

Obviously there are more in boot.s source code than I have covered, but I don't want to get you overwhelmed by all those details and lose sight of the big picture.

Please move on to the next video about diskboot.img. Thanks for watching!

Видео Linux 04: Demystify boot process (2) - boot.img канала LiLeoWang
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять