The Not-So-Magic Of How Your Computer Boots Up
A step-by-step walkthrough the booting process.
From a press on your computer’s power button to an operating system’s kernel loaded into the memory and executed! Sounds magical right? This article is a full description of the booting process. It will change the way you perceive your computer.
Due to some tiny differences between operating systems booting processes, I chose to cover the Linux kernel’s booting process on a BIOS x86_64 architecture/platform. By understanding this process, you’ll be able to understand how BIOS PCs load an operating system.
Ready? Let’s dig into the booting process lengthy journey!
It all starts when you press the power button of your laptop or desktop. The motherboard sends a signal to the power supply to turn it on. The power supply responds by providing a signal to the motherboard to indicate when the DC voltages are in spec, so that the computer is able to safely power up and boot. This signal is called the “Power good signal”.
“Power good signal” is then received by the processor timer chip that controls the reset line to the processor. The processor starts working in real mode.
Now the CPU has to find the first instruction to execute after the reset. This instruction is located at an address called “Reset Vector”. The reset vector is a pointer or address, where the CPU should always begin as soon as it is able to execute instructions.
The address is in a section of non-volatile memory initialized to contain instructions to start the operation of the CPU, as the first step in the process of booting the system.
The first instruction is a jump (jmp
) instruction that usually points to the BIOS (Basic Input/Output System) entry point.
Now the CPU starts executing the BIOS. The BIOS starts by performing a process called the “power-on self-test (POST)”. The POST process consists in:
- verifying CPU registers
- verifying the integrity of the BIOS code itself
- verifying some basic components like DMA, timer, interrupt controller
- identifying, organizing, and selecting which devices are available for booting
After performing POST, the BIOS should now find a device to boot from.
A boot order defines which devices the computer should check for the operating system’s boot files. It also specifies the order devices are checked in.
This list is stored in the BIOS configuration. The BIOS now attempts to boot from a device. It checks the list of devices looking for a boot sector.
Usually, the very first sector of the hard disk is the boot sector.
On hard drives partitioned with an MBR partition layout, the boot sector is called MBR(Master Boot Record). Let’s look at the components of the MBR:
As the figure shows, MBR contains a maximum of four partitions that occupies 64 bytes combined and a disk signature (two bytes), this leaves 446 bytes for the machine code of a boot loader.
Trying to boot from a device,
- BIOS loads the contents of the 512-byte MBR into memory location 0x7c00 and jumps to that location to start executing whatever code is in the MBR.
- BIOS checks if the 511th and 512th bytes are 0x55 and 0xAA, respectively.
- If not, go back to step 1 and try the next device, or if there is no suitable boot device the BIOS halts with a complaint like “Non-System Disk or Disk Error.” A dead hard drive might present with this symptom.
- If yes, start the bootstrap code.
The MBR bootstrap code processes as follows:
- The MBR itself contains the first stage of the boot loader called stage 1 boot loader. This first stage starts from
boot.img
. Due to the limited amount of space available, its only purpose is to load another sector from disk that contains additional bootstrap code and transfer control to it. - The bootstrap code that will continue the booting process is
diskboot.img
: the first sector ofcore.img
. Its purpose is to load the rest ofcore.img
which contains GRUB’s kernel and drivers for handling filesystems.
On MBR partitioned disks:core.img
is stored in the empty sectors between the MBR and the first partition. - Once the rest of the core image has been loaded into memory, the GRUB’s kernel takes over and initializes the modules that were packed alongside it into the core image.
- Once GRUB is running, the first thing it will do is to load
normal.mod
module. GRUB modules may specify dependencies of other modules, so loadingnormal
will usually entail another couple of modules being loaded. Once GRUB has successfully loaded thenormal
module, it will run thenormal
command, which moves GRUB to normal mode and displays the GRUB menu.
The user now can choose an operating system. GRUB can be configured to automatically load a specified OS after a user-defined timeout.
Once the user’s boot options have been selected, GRUB loads the selected kernel into memory and passes control to it.
The bootloader has now loaded the Linux kernel into memory, and jumped to the kernel entry point.
Wrap-up
This is then how the simple press on your computer’s power button leads to loading the operating system’s kernel into memory. More complicated than you thought, isn’t it? There are many other details behind the process. But the essential steps were discussed in the article.
Let’s revisit quickly what you’ve learned: