PC Booting 簡介

基於 BIOS 的開機流程簡介

描述基於 linux 的開機流程步驟, 適用於有 BIOS 的機器

  1. 開機的時候會執行 BIOS, BIOS 先對硬體做檢測. 在根據設定所指定的開機 device 去讀取 MBR(Master Boot Record). MBR 處於 device 的第一個 sector, 裡面包含了基本的 boot loader 與 partition table 資訊.
  2. 礙於 MBR 大小的限制(512 bytes = 446(basic boot loader code, e.g. boot.img) + 64(partition table)), boot loader 的會再去其他 sector 讀取完整的 boot loader 程式(e.g. core.img). 程式依據 MBR 或 GPT 來判斷放 boot loader 的位置[3][4].
    (1) 為MBR時候, 通常 boot loader 安裝於 MBR 與第一個 partition 之間.
    (2) 當為GPT, 則使用一個 Boot Partition 來存放 boot loader
  3. 當 boot loader 被執行後, 會去讀取 /boot/grub 裡面的設定來決定所要讀取的 OS 核心檔案 (kernel.img 和 initrd.img)
  4. initrd.img 裡面包含 kernel 所需的基本模組(mod), 當被掛載後 kernel 便可以去讀取外接裝置, 並視設定來決定是否有需要重掛載檔案系統
  5. 接著就執行第一支程式 e.g. initd or systemd

範例

範例1 [6]
  • Stage 1:

    boot.img is stored in the master boot record (MBR) or optionally in any of the volume boot records (VBRs), and addresses the next stage by an LBA48 address (thus, the 1024-cylinder limitation of GRUB legacy is avoided); at installation time it is configured to load the first sector of core.img.

  • Stage 1.5:

    core.img is by default written to the sectors between the MBR and the first partition, when these sectors are free and available. For legacy reasons, the first partition of a hard drive does not begin at sector 1 (counting begins with 0) but at sector 63, leaving 62 sectors of empty space not part of any partition or file system, and therefore not prone to any problems related with it. Once executed, core.img will load its configuration file and any other modules needed, particularly file system drivers; at installation time, it is generated from diskboot.img and configured to load the stage 2 by its file path.

  • Stage 2:

    files belonging to the stage 2 are all being held in the /boot/grub, which is a subdirectory of the /boot directory specified by the Filesystem Hierarchy Standard(FHS).

範例2 [7]

stage 1 = boot.img
stage 1.5 = diskboot.img + kernel.img + pc.mod + ext2.mod (the core image)
stage 2 = normal.mod+_chain.mod

The boot process for GRUB 2 then would be:
  • BIOS code reads MBR from a disk (looks at last 2 bytes to verify if MBR).
  • Starts executing bootloader code (boot.img).
  • This loads the first sector of the core image (diskboot.img), whose location was stored at a particular location in boot.img at GRUB install time and usually points to the core image located in the "DOS compat space" immediately after the MBR. diskboot.img in turn loads and executes the rest of the core image.
  • These core image modules know about the boot file system and can open the file system on the specified (at install time) partition. It looks for and loads modules there, including normal.mod
  • normal.mod loads the grub.cfg file and executes the statements, usually providing a menu to the user etc.

img 的功能描述

補充說明上述範例所提到的 img [2]
boot.img

On PC BIOS systems, this image is the first part of GRUB to start. It is written to a master boot record (MBR) or to the boot sector of a partition. Because a PC boot sector is 512 bytes, the size of this image is exactly 512 bytes.

The sole function of boot.img is to read the first sector of the core image from a local disk and jump to it. Because of the size restriction, boot.img cannot understand any file system structure, so grub-setup hardcodes the location of the first sector of the core image into boot.img when installing GRUB.

diskboot.img

This image is used as the first sector of the core image when booting from a hard disk. It reads the rest of the core image into memory and starts the kernel. Since file system handling is not yet available, it encodes the location of the core image using a block list format.

kernel.img

This image contains GRUB’s basic run-time facilities: frameworks for device and file handling, environment variables, the rescue mode command-line parser, and so on. It is rarely used directly, but is built into all core images.

core.img

This is the core image of GRUB. It is built dynamically from the kernel image and an arbitrary list of modules by the grub-mkimage program. Usually, it contains enough modules to access /boot/grub, and loads everything else (including menu handling, the ability to load target operating systems, and so on) from the file system at run-time. The modular design allows the core image to be kept small, since the areas of disk where it must be installed are often as small as 32KB.

See BIOS installation, for details on where the core image can be installed on PC systems.

*.mod

Everything else in GRUB resides in dynamically loadable modules. These are often loaded automatically, or built into the core image if they are essential, but may also be loaded manually using the insmod command (see insmod).

Reference

  • [1] M. Tim Jones, "Inside the Linux boot process", 2006
  • [2] "Where is the bootloader stored - in ROM, RAM, or elsewhere?", http://askubuntu.com/questions/173248/where-is-the-bootloader-stored-in-rom-ram-or-elsewhere
  • [3] "10 GRUB image files", https://www.gnu.org/software/grub/manual/html_node/Images.html
  • [4] "3.4 BIOS installation", https://www.gnu.org/software/grub/manual/html_node/BIOS-installation.html
  • [5] "2.2.3 開機流程中的 BIOS 與 UEFI 開機檢測程式", http://linux.vbird.org/linux_basic/0130designlinux.php#partition_bios_uefi
  • [6] "GNU_GRUB", https://en.wikipedia.org/wiki/GNU_GRUB
  • [7] "GRUB 2 boot process", http://www.pixelbeat.org/docs/disk/
  • [7] "bootloader", http://albert-oma.blogspot.tw/2011/12/bootloader.html

留言

這個網誌中的熱門文章

yocto recipe : (1) 撰寫 recipe

yocto recipe : (2) 撰寫 bbappend

yocto recipe : (3) 使用 External Source 來編譯軟體