For your reference, below is a list of the articles in this series:
- Part 1a: Linux - BIOS and Master Boot Record (this article)
- Part 1b: Linux - The Bootloader
- Part 1c: Linux - The Kernel and Initial Ramdisk
- Part 1d: Linux - Init Process
Linux booting process can generally be divided into 4 major steps:
- BIOS loads the bootloader
- The bootloader finds and loads the kernel
- Kernel loads drivers to configure hardware, mounts root filesystem and sets up init process
- Init process executes startup scripts and starts daemon processes
In this first post of this series, we will start off with the BIOS whose responsibility is to find and load the boot loader.
BIOS and Master Boot Record
When you powered on a physical machine, i.e. your desktop or a server, usually the power supply will supply electric power to hardware components, i.e. motherboard, CPU, hard disks, SSD, graphics processors, etc.
A firmware on motherboard that held in a nonvolatile flash RAM (CMOS) called BIOS performs Power-on-self-test (POST) to discover a limited number of devices, i.e. Ethernet and storage (USB, hard disk) that required to boot.
BIOS can only communicate to a few devices because it does not contain many drivers. It starts out by scanning the PCIs and PCI buses to detect all the devices attached to them. If the devices present are different from when the system was last booted, the new devices are configured.
The BIOS then determines the boot device from list of devices stored in the CMOS memory. If the boot device is hard drive, the very first sector (the boot sector or Master Boot Record or MBR) of the disk is loaded by BIOS into a fixed memory location and executed.
This sector is the first 512 bytes (or 4069 bytes in modern boot sector) on a hard drive (with zero as its offset) in which 446 bytes belongs to stage-1 boot program (boot.img). The end of MBR contains partition table describing the partitions of a storage device. The organization of the partition table in the MBR limits the maximum addressable storage space of a partitioned disk to 2TB.
Up to this stage, the machine does not access any mass storage media. Therefore, information on the date, time, and important peripherals are loaded from the CMOS - a battery-backed chip inside computer that stores information.
A more formalized and modern standard - UEFI was developed to replace BIOS. UEFI support almost all new PC hardware these days, but plenty of BIOS systems remain in the field. Moreover, virtualized environments often adopt BIOS as their underlying boot mechanism. Booting is different on UEFI systems: rather than executable boot code residing outside of a filesystem, there is a special filesystem called the EFI System Partition (ESP), which contains a directory named efi. Each boot loader has its own identifier and a corresponding subdirectory, such as efi/microsoft, efi/apple, or efi/grub. A boot loader file has an .efi extension and resides in one of these sub-directories, along with other supporting files. UEFI is also more secure as it requires boot loaders to be digitally signed by a trusted authority.
From here onward, the bootloader will take over the process.