Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ GRUB how to load Linux kernel     - How do I switch from NetworkManager to systemd-network on Linux (Linux)

- JavaScript event handling Detailed (Programming)

- Binary Packages Golang (Linux)

- Linux FTP setting file description (Server)

- Java development specifications summary (Programming)

- Android Activity launchMode (Programming)

- Java, boolean operators & =, | = ^ = use (Programming)

- How to Debian Linux the default Python version switch to alternative version (Linux)

- Linux file and directory management - ls, cp, mv (Linux)

- OpenSSL Introduction and compilation steps on Windows, Linux, Mac systems (Linux)

- Nginx caching using the official guide (Server)

- socket busy poll of Linux kernel 3.11 to avoid sleep switch (Linux)

- To create and manage virtual machines on Ubuntu Redhat (Linux)

- Asynchronous JavaScript loading (Programming)

- Database start listening TNS-12537, TNS-12560 error (Database)

- CentOS6 installed Tomcat (Server)

- Linux file permissions chmod chown (Linux)

- Supervisor Installation and Configuration (Server)

- How to use SVN to manage our source code (Server)

- Linux command execution judge sentences -;, &&, || (Linux)

  GRUB how to load Linux kernel
  Add Date : 2017-08-31      

For GRUB loading process, the vast majority are medium to write menu.lst, authoring process grub.cfg the GRUB configuration file, like writing a script language, with some keywords can make PC function correctly launch a desktop Linux a. But this is only the analysis of the use GRUB instead of GRUB.

It would have been no want to explore the idea of GRUB, until I made toy kernel of study into the "virtual memory management" section. Many of the agencies are virtual memory management when it comes to Linux memory management, Linux kernel will be loaded into the system 3G ~ 4G virtual memory, but GRUB is not turned on virtual memory, Linux kernel is loaded by whom, and how to load 3G ~ 4G segment corresponding to the region of it.

Analysis kernel


We look at the most original kernel source file compiled vmLinux. The file is a ELF file, use readelf read under Section header of the file.

Here only a few cut-segment display, the back section is similar. You can see the address of the segment to be loaded is indeed after 0xC0000000. But vmLinux not bootable Linux kernel file.

Linux startup information generally in `/ under boot`, we look at the contents inside.

You can see the folder grub, grub is to start the boot Linux bootloader, we look at the contents of `/ boot / grub / grub.cfg` file.

menuentry 'Linux Mint 17 {
    gfxmode $ linux_gfx_mode
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root = 'hd0, msdos1'
    linux /boot/vmlinuz-3.13.0-24-generic
    initrd /boot/initrd.img-3.13.0-24-generic
Linux on a line with the specified boot kernel file vmlinux can not see, but the vmlinuz file.

After the search you can see vmlinuz is a bootable **, ** compressed kernel. initrd is the "initial ramdisk" shorthand, is a temporary virtual disk, temporarily not to discuss. Vmlinuz because my computer is 64 bit, and for 64bit do not know, so look for the vmlinuz file for 32bit resolution. First try `readelf` command.

# Readelf -S vmlinuz

readelf: Error: Unable to read in 0x7269 bytes of section header

readelf: Errors: Not an ELF file - it's the beginning of the magic byte error

 Is not an ELF file, then try `objdump` it.

# Objdump -afh vmlinuz

objdump: vmlinuz: Unrecognized file format
Or not.

 The reason why this time will be very use these commands to see the pieces of information vmlinuz files, because in my toy kernel ELF file is used, and the use of grub loaded ELF file for protection for a number of internal section, the implementation of these section must be in memory specifically address the use of `readelf` view toy kernel of section header information is as follows.

You can see the address of these segments in memory Addr segment is the kernel at runtime. The load my kernel grub configuration is as follows

title toy kernel

root (fd0)

kernel / zkernel

module / initrd

vmlinuz reason is called the compressed kernel, because it is after use gzip to compress, and not just pure packet at the beginning part of the file is embedded gzip decompressor code, the equivalent of "self-extracting." I need to load the kernel from the grub good corresponding section, but vmlinuz have not read how to make grub loading segment?

 The answer lies in the above grub configuration file in the linux kernel is declared using the keyword * linux * in my configuration is used by the kernel * kernel * statement can be clearly seen that, grub loads of linux It is a special treat, but how to deal with the special Envoy, only read from the source code.

Analysis GRUB source
Downloaded from [GRUB official website] (http://www.gnu.org/software/grub/) on the GRUB2 code structure clearer. Because the keyword "linux", we use "linux" to search, you can be found to the following code.

grub-core / loader / i386 / pc / linux.c

cmd_linux = grub_register_command ( "linux", grub_cmd_linux, 0, N _ ( "Load Linux."));

Should be able to be seen from the name is this, register a command, the keyword is "linux". Enter * grub_cmd_linux * function analysis.

struct linux_kernel_header lh;

file = grub_file_open (argv [0]); // open vmlinuz

// Reads linux header structure from the beginning vmlinuz

grub_file_read (file, & lh, sizeof (lh)) = sizeof (lh)!;

// Check the legality

if (lh.boot_flag = grub_cpu_to_le16 (0xaa55)!);

  goto fail;

// Copy linux header structure to specifically address

grub_memmove (grub_linux_real_chunk, & lh, sizeof (lh));

 // Copy data to specifically address vmlinux

 grub_file_read (file, grub_linux_prot_chunk, grub_linux16_prot_size)

Most independent code and discussions have been omitted to protect only load the relevant code. Vmlinuz can be seen in the file header provides a structure to use bootloader to determine the relevant information, using hexedit open vmlinuz indeed can see these data, and the structure is a two-way, providing relevant information to the bootloader in vmlinuz , bootloader will also need to fill the core information to them. You can see the structure more information THE LINUX / x86 BOOT PROTOCOL https://www.kernel.org/doc/Documentation/x86/boot.txt in.

grub and no more detailed analysis vmlinuz information will jump to after loading the kernel part of the next action.

And then look back to grub-core / loader / multiboot_elfxx.c, this is when you should load the ELF kernel performs the action.

static grub_err_t CONCAT (grub_multiboot_load_elf, XX) (grub_file_t fileconst char * filename, void * buffer)


 Elf_Ehdr * ehdr = (Elf_Ehdr *) buffer;

 char * phdr_base;

 int i;

 if (ehdr-> e_ident [EI_MAG0]! = ELFMAG0

      || Ehdr-> e_ident [EI_MAG1]! = ELFMAG1

      || Ehdr-> e_ident [EI_MAG2]! = ELFMAG2

      || Ehdr-> e_ident [EI_MAG3]! = ELFMAG3

      || Ehdr-> e_ident [EI_DATA]! = ELFDATA2LSB)

    return grub_error (GRUB_ERR_UNKNOWN_OS, N _ ( "invalid arch-independent ELF magic"));

  / * Load every loadable segment in memory. * /

  for (i = 0; i e_phnum; i ++)



You can see loads of ELF kernel grub indeed analyzed ELF segments and according to the parameters within the ELF file loaded.
- Oracle partition table data migration, process management automation (Database)
- Debian (Wheezy) Install Redmine 2.6 (Programming)
- Iptables small summary (Linux)
- Git uses a standard process (Linux)
- Disable unnecessary services under Linux (Linux)
- How to use Evernote in the Linux command line (Linux)
- Depth study and understanding for individual users suicide DDoS attacks (Linux)
- ORA-01157 & ORA-01110 Troubleshooting (Database)
- mysqldump issue a note (Database)
- ORA-04031 error appears in the solution run utlrp.sql (Database)
- Linux Shell Scripting Interview Question (Linux)
- 2016, the new Node project Precautions (Programming)
- Laravel cache paged results (Server)
- Installation and configuration to compile MySQL 5.6.10 under CentOS 5.9 (Database)
- Linux LVM - File system extension (Linux)
- Spring REST Exception Handling (Programming)
- Struts2 : combobox label use (Programming)
- Oracle archive log size than the size of the online journal of the much smaller (Database)
- How to Install Xombrero 1.6.4 (minimalist Web browser) on Ubuntu and Archlinux (Linux)
- Workaround CentOS error message during compilation PHP5 common (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.