Grub learning notes

Time:2020-12-10

A few days ago, when I installed the system, I installed Ubuntu first and then windows. As a result, Ubuntu could not be loaded and the booting system with EasyBCD was not satisfactory. Later, I came into contact with grub and thought it was a magic thing. I studied it.
Finally, the most profound understanding of grub is through bean_ Lee’s blog is recommended to you,http://blog.chinaunix.net/uid-24774106-id-3497929.html

  GRUB(Grant unified bootloader) is a multi operating system boot program from the GNU Project. It is a multi operating system boot manager. Used to boot different systems, such as windows, Linux

Now that it’s a bootloader, let’s take a look at where it’s located. (a stolen picture, drawn by M. Tim Jones in inside Linux boot process)
  Grub learning notes

Grub’s work is divided into two phases, stage1 and stage2. But in practical application, there is actually a third stage: stage 1.5, which is revealed below.
Stage1, stage1.5 and stage2 are actually three images, which are loaded and run in turn. So why divide them into three images, and it’s not easy and pleasant to make one image?

This may need to look at the BIOS source code to know the original.
I haven’t seen the source code of BIOS, but I guess it must be because BIOS will move the contents of MBR into memory at the end of the day. However, the MBR only has 512bytes space, which is not enough to put down all the images. Therefore, it has to be divided into three stages.

Here we would like to add some knowledge of MBR:
  MBRThe full name is master boot record, which is the master boot record of the hard disk.
For the convenience of understanding, MBR is generally divided into broad sense and narrow sense: the broad sense MBR includes the whole sector (bootloader, partition table and partition mark), that is, the master boot record mentioned above; and the narrow MBR only refers to the bootloader.
Hard disk0 cylinder, 0 head, 1 sectorCalled the primary boot sector (also known as the master boot record MBR). It consists of three parts: main boot program, disk partition table (DPT) and partition valid flag. In a total of 512 bytes of main boot sector, the main boot loader accounts for 446 bytes. The second part is the partition table area (DPT), which takes up 64 bytes. The number of partitions in the hard disk and the size of each partition are recorded in it. The third part is magic number, which takes up 2 bytes and is fixed as 0xaa55 or 0x55aa, depending on the processor type. If it is a small end mode processor (such as Intel Series), the value is 0xaa55; if it is a large end mode processor (such as Motorola 6800), the value is 0x55aa.
  Grub learning notes

So the MBR is in stage1, BIOS will carry it to 0x7c00 to start execution, then what will it do?
  The function of this 512 byte code is to copy the 512 bytes of 0 cylinder, 0 track and 2 sector to 0x8000 For details, please refer to this section

Stage 1.5 (a part of stage1.5 to be exact) is naturally placed in cylinder 0, track 0 and sector 2. Its size is also 512 bytes. It exists in order to find / boot / grub / stage2, while stage2 exists in the file system. We know that stage 1 has no OS, and there is no concept of file system. The task of identifying file system is done by stage 1.5. However, a program with the size of 512bytes does not operate on the file system. It is only an introduction. It copies several sectors to memory from LBA sector number 2 (0 cylinder 0 track 3 sector), and the latter part is the real part of operating the file system.
But the file system is tens of millions. We can’t put all the function files of the file system in the disk sector. What should we do? When grub performs setup, it can identify the file system of the boot device. For example, we are ext4 file system, so we only need to change the E2Fs of ext4 part_ stage1_ 5 into the sector.

Now that stage1.5 has found stage2, stage2 can start loading the operating system.

So, finally, the following is a summary of Embezzlement:
1. The MBR code part is the same as the / boot / grub / stage1 part. The binary file of this part is compiled by / stage1 / stage1. S in grub source code. The so-called stage1 has only one function, which is to load the contents of the second sector of the disk into memory
2. The contents of the second sector and / boot / grub / E2Fs_ stage1_ The first 512 bytes of the 5 file are the same. This part is compiled by grub source code / stage2 / start. S. The function of this start. S is to load the third sector of the disk to the nth sector to the memory. The number of N fetches depends on the size of the supporting code of the file system.
3. After the supporting code of file system is put into memory, we do not need to call int 13 directly to load the sector content. We have the file system, and we can operate the file directly. Then large files like / boot / grub / stage2 can be operated directly.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]