Grub2 startup and composition

Source: Internet
Author: User

1.1 grub2Features

The grub2 version used in this article is 1.99, and grub2 is the second version completely rewritten to replace grub. Reference:

Grub 2, which is a rewrite of grub, is alive and under development. grub 2 aims at merging sources from pupa in order to create the next generation of GNU grub. a mailing list and a wiki have been setup for discussing the development of grub 2.

Grub 2 has the following tasks:

* Create a compact core image. This will make stage 1.5 unnecessary.
* Add support for dynamic loading to the core image, so that it can extend itself at the run time rather than at the build time.
* Add real memory management, to make GNU grub more extensible.
* Add a more object-oriented framework for file systems, files, devices, drives, terminals, commands, and OS loaders.
* Add support for internationalization. This includes des support for non-ASCII character code, message catalogs like gettext, fonts, graphics console, and so on.
* Add an actual localization, based on the above internationalization support. We will target on Japan as the first step.
* Segregate code specific to i386-pc from generic code, to make GNU grub portable.
* Add support for cross-platform installation.
* Develop additional software packages which will help our project and hopefully other projects.

 

Grub2 adopts the idea of modular dynamic loading. Compared with grub, grub2 does not need to add all functions during construction, which makes grub2 smaller in size, grub2 kernel images can be controlled within 31 KB (grub images are at the hundred kb level), so grub2 can completely remove stage 1.5 In grub, you can store the entire image in stage1.5 in grub.

 

1.2X86 grub2 Startup Process

Grub2 must have nothing unique to start on x86. Here I will introduce it again:

1) power on-> BIOS

After power-on, the hardware circuit generates a reset signal and enters the CPU through the reset pin. The CPU enters the real mode. The default value of CS registers is 0xf000, and the default value of IP registers is 0xfff0, that is, jump to the 0xffff0 physical address and execute the First Command. 0xffff0 physical address (ROM) stores the BIOS Execution Code. the BIOS completes the initialization of power-on post and bios interruption calls, finally, the data of 512b in the primary Boot Sector is read to the starting point of 0x7c00 physical memory address, and the code is executed at 0x7c00;

 

Data format of the primary Boot Sector 512b:

A) 0x000 ~ 0x1bd: 446b, which is the MBR boot code;

B) 0x1be ~ 0x1fd: 64b, 4 groups of disk partition table information (DPT), each group of partition information accounted for 16 B (this also

Is there a maximum of four primary partitions); take the first partition as an example, the partition field of the 16B size is as follows:

 

 

C) 0x1fe ~ 0x1ff: End mark, 0x55 and 0xaa;

Early hard disks were composed of disks and heads. Each disk had two sides and each side had one head. When the hard disk accessed data, the head moved on the disk to read data; each disk is composed of many concentric circles. Each concentric circle is called a track. Each track is logically divided into 63 sectors (the actual physical storage is not like this ), disk access data is measured in slices. concentric circles in the same position on all sides form a cylindrical surface. Early CHS (cylinder head, and sector Sector) Addressing are based on such a structure;

When a hard disk is in use, all sectors with 0 sides and 0 channels are the first 63 sectors for special purposes. The bios and operating system do not place any data here (for example, the first boot sector is generally located in 64th sectors, which is generally used as bootloader. This is why grub must use stage1.5 (generally around KB, exceeds the limit of 62 sectors ).

2) BIOS-> MBR (grub2 boot. IMG)

In this scenario, the MBR Boot Code is grub2's boot/i386/PC/boot. the Code Compiled by S is called boot. IMG; Code Execution location: 0x7c00. Main Behavior: Set the stack pointer to 0x2000, test the working mode of the hard disk CHS/LBA, and load the second sector to 0x8000, and jump to 0x8000 for execution;

 

Additionally, the first 1 MB memory space distribution of the PC is provided (from documentation/i386/boot.txt ):

|

0a0000 + ------------------------ +

| Reserved for BIOS | do not use. Reserved for bios ebda.

09a000 + ------------------------ +

| Stack/Heap/pipeline Line | for use by the kernel real-mode code.

098000 + ------------------------ +

| Kernel setup | the kernel real-mode code.

090200 + ------------------------ +

| Kernel Boot Sector | the kernel legacy Boot Sector.

090000 + ------------------------ +

| Protected-mode kernel | the bulk of the kernel image.

010000 + ------------------------ +

| Boot Loader | <-Boot Sector entry point 0000: 7c00

001000 + ------------------------ +

| Reserved for MBr/BIOS |

000800 + ------------------------ +

| Typically used by MBR |

000600 + ------------------------ +

| BIOS use only |

000000 + ------------------------ +

 

According to the above structure, bootloader uses 0x1000 ~ 0x10000 memory segment;

3) grub2 boot. IMG-> grub2 diskboot. IMG (2nd sectors)

The second sector code is compiled and generated by boot/i386/PC/diskboot. S, which is called diskboot. IMG;

Boot. IMG loads diskboot. IMG to 0x8000 and jumps to 0x8000 to execute the code;

4) grub2 diskboot. IMG-> grub2 kernel. IMG (3rd ~ N sectors, n <63)

Diskboot. IMG is used to read the kernel. IMG of grub2 (generated by the file under Kern \) to a certain location in the memory. It is composed of diskboot. IMG:

A) 0x0 ~ 0x147: A total of B commands;

B) 0x148 ~ 0x1ff: A total of 184b, used to save the dataset; each data set is 12B, 15 data sets can be saved; 12B data set structure: 0 ~ 7b indicates the number of start sectors, 8 ~ 9b indicates the number of sectors, 10 ~ 11B indicates that the destination segment address, that is, the location where kernel. IMG is stored in the memory (usually 0x8200), is determined when the image (grub-mkimage) is generated;

 

Diskboot. IMG determines the kernel based on the data of the dataset. storage location and memory address of IMG on hard disk; kernel. IMG is compressed, and the image starts with i386/PC/startup. S and i386/PC/lzma_decode.s start and lzma extract code (7zip compression algorithm) code for decompression (similar to bzimage processing), enter the protection mode, and decompress it to 0x10000, finally, move the extracted code to 0x8200 for execution (0x8200 ~ Between 0 x, cannot exceed 0 x memory address). At this time, the CPU is already in protection mode and the grub_main () in Kern/Main. C is finally called. The Code is as follows:

 

 

Main Tasks: Initialize the system, load the module, and enter normal or rescue mode. grub2 loads and runs the Operating System Based on the configuration file or user input. Call grub_load_modules () to load all modules, which is one of the manifestations of grub2's expansion performance.

1.3 grub2 compilation and Installation

Compile:

Go to grub-1.99/directory:

1)./configure -- prefix =/xlpang/grub_install -- Exec-Prefix =/xlpang/grub_install/

Cc =/opt/ZTE/x86_64_gcc4.1.2_glibc2.5.0/bin/x86_64-pc-linux-gnu-gcc cflags =-static

2) Make

3) make install

After the execution is complete (pay attention to the user permissions), go to/xlpang/grub_install/and you will see the grub2 Installation File.

 

Installation:

Log on to the target PC. Assume that/dev/sda1 has been mounted to the/directory,

1) Copy grub_install/to/xlpang/(the installation path must be consistent with the compiling path; or

Modify the variable path at the beginning of the/xlpang/grub_install/sbin/grub-install script file and execute:

/Xlpang/grub_install/sbin/grub-install -- no-floppy/dev/SDA command to install grub2 to the/boot directory of the first partition of the SDA hard disk, and boot. IMG, diskboot. IMG and kernel. IMG writes data to the corresponding sector. Other files are installed in the/boot/GRUB/directory.

2) generate the/boot/GRUB/grub. cfg file (place the bzimage kernel image in the/boot/directory ):

Edit the/xlpang/grub_install/etc/grub. d/40_custom file and add:

Menuentry "linux_x86_64-2.6.21 "{

Set root = (hd0, 1)

Linux/boot/bzimage Ro

Initrd/boot/initramfs

}

3) Run:/home/grub-mkconfig-O/boot/GRUB/grub. cfg.

 

NOTE: If grub exists, install grub2 in the same directory as grub. After the host is started, the grub interface is still loaded, that is, grub2 is compatible with grub;

Solution: Remove or rename the original grub installation directory and then install grub2.

 

1.4 grub2 image example

After grub2 is installed, you can verify the data to see if the installation is successful, which helps to form a further understanding of grub2. Take the PC as an example:

1) extract the boot. IMG image: dd If =/dev/SDA of = boot. img bs = 512 COUNT = 1

 

[Root @ ip49 sbin] # OD-tx1-Ax boot. img

000000 EB 63 90 8e C0 8e D8 8e D0 BC 00 7C 8B F4 FB FC

000010 BF 00 06 B9 00 01 F3 A5 B8 DF 06 50 C3 00 0f 00

000020 00 0a 45 72 72 6f 72 20 6C 6f 61 64 69 6e 67 20

000030 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 03 02

000040 ff 00 00 20 01 00 00 00 00 02 fa 90 90 F6 C2 80

000050 75 02 B2 80 EA 59 7C 00 00 31 00 80 01 00 00 00

000060 00 00 00 00 FF fa 90 90 F6 C2 80 74 05 F6 C2 70

000070 74 02 B2 80 EA 79 7C 00 00 31 C0 8e D8 8e D0 BC

000080 00 20 FB A0 64 7C 3C FF 74 02 88 C2 52 be 80 7d

000090 E8 17 01 be 05 7C B4 41 bb aa 55 CD 13 5A 52 72

2017a0 3D 81 FB 55 AA 75 37 83 E1 01 74 32 31 C0 89 44

Listen B0 04 40 88 44 ff 89 44 02 C7 04 10 00 66 8B 1E 5C

2017c0 7C 66 89 5C 08 66 8B 1E 60 7C 66 89 5C 0C C7 44

Listen D0 06 00 70 B4 42 CD 13 72 05 BB 00 70 EB 76 B4 08

0000e0 CD 13 73 0d F6 C2 80 0f 84 D8 00 be 8B 7d E9 82

2017f0 00 66 0f B6 C6 88 64 FF 40 66 89 44 04 0f B6 d1

000100 C1 E2 02 88 E8 88 F4 40 89 44 08 0f B6 C2 C0 E8

000110 02 66 89 04 66 A1 60 7C 66 09 C0 75 4E 66 A1 5C

000120 7C 66 31 D2 66 F7 34 88 D1 31 D2 66 F7 74 04 3B

000130 44 08 7d 37 Fe C1 88 C5 30 C0 C1 E8 02 08 C1 88

000140 D0 5A 88 C6 BB 00 70 8e C3 31 db B8 01 02 CD 13

000150 72 1E 8C C3 60 1E B9 00 01 8e dB 31 F6 BF 00 80

000160 8e C6 FC F3 A5 1f 61 ff 26 5A 7C be 86 7d EB 03

000170 be 95 7d E8 34 00 be 9A 7d E8 2E 00 CD 18 EB fe

000180 47 52 55 42 20 00 47 65 6f 6D 00 48 61 72 64 20

000190 44 69 73 6B 00 52 65 61 64 00 20 45 72 72 6f 72

0001a0 0d 0a 00 BB 01 00 B4 0e CD 10 AC 3C 00 75 F4 C3

0001b0 00 00 00 00 00 00 00 C5 A5 A5 00 00 80 01

0001c0 01 00 83 Fe FF 3f 00 00 00 8d F2 34 0C 00 fe

0001d0 FF 83 Fe ff cc F2 34 0C 76 D0 A8 10 00 fe

0001e0 FF 82 Fe FF 42 C3 dd 1C 3f 82 3E 00 00

0001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA

 

 

 

Run "cat boot. IMG" and find the output "Grub geomhard diskread error". For details, see boot. S.

2) extract the diskboot. IMG image: dd If =/dev/SDA of = boot. img bs = 512 COUNT = 1 skip = 1

 

[Root @ ip49 sbin] # OD-tx1-Ax diskboot. img

000000 52 56 be 1B 81 E8 39 01 5E BF F4 81 66 8B 2D 83

000010 7d 08 00 0f 84 E2 00 80 7C ff 00 74 46 66 8B 1d

000020 66 8B 4D 04 66 31 C0 B0 7f 39 45 08 7f 03 8B 45

000030 08 29 45 08 66 01 05 66 83 55 04 00 C7 04 10 00

000040 89 44 02 66 89 5C 08 66 89 4C 0C C7 44 06 00 70

000050 50 C7 44 04 00 00 B4 42 CD 13 0f 82 af 00 BB 00

000060 70 EB 66 66 8B 45 04 66 09 C0 0f 85 97 00 66 8b

000070 05 66 31 D2 66 F7 34 88 54 0a 66 31 D2 66 F7 74

000080 04 88 54 0b 89 44 0C 3B 44 08 7d 79 8B 04 2a 44

000090 0a 39 45 08 7f 03 8B 45 08 29 45 08 66 01 05 66

2017a0 83 55 04 00 8A 54 0d C0 E2 06 8A 4C 0a Fe C1 08

Listen B0 D1 8A 6C 0C 5A 52 8A 74 0b 50 BB 00 70 8e C3 31

2017c0 dB B4 02 CD 13 72 46 8C C3 8e 45 0a 58 C1 E0 05

Listen D0 01 45 0a 60 1E C1 E0 03 89 C1 31 ff 31 F6 8e DB

0000e0 FC F3 A5 1f be 23 81 E8 57 00 61 83 7d 08 00 0f

Limit F0 85 24 ff 83 EF 0C E9 16 ff be 25 81 E8 42 00 5A

000100 EA 00 82 00 00 be 28 81 E8 36 00 EB 06 be 2D 81

000110 E8 2E 00 be 32 81 E8 28 00 EB Fe 6C 6f 61 64 69

000120 6e 67 00 2E 00 0d 0a 00 47 65 6f 6D 00 52 65 61

000130 64 00 20 45 72 72 6f 72 00 BB 01 00 B4 0e CD 10

000140 46 8A 04 3C 00 75 F2 C3 00 00 00 00 00 00 00 00

000150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

*

0001f0 00 00 00 00 02 00 00 00 00 00 00 3C 00 20 08

 

 

Run "cat diskboot. IMG" and find the "loading." and "geomread error" output. For details, see diskboot. S;

In addition, the meaning of the last line is as follows:

A) Start slice LBA address: 0x0000 0000 0000 0002 that is, the third slice starts to store kernel. IMG.

B) number of sectors: 0x003c, that is, 60*512 = 30kb

C) Destination segment address: 0x0820 (where the memory is 0x8200)

 

3) extract the grub2 kernel. IMG Image Based on the information in 2:

Dd If =/dev/SDA of = kernel. img bs = 1 k count = 30 skip = 1

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.