From Power Button to Operating System

abhilash reddy
7 min readMay 20, 2023

If you’re curious about how a computer goes from being an inanimate combination of silicon and metal to loading a functional operating system, you’ve come to the right place. In the rest of this blog, I’ll explain the process in a straightforward manner.

Part 1 — BOOT-ROM

It all starts with the BOOT-ROM. It is the first piece of code that executes after CPU starts. When we say code it means a set of instructions. The CPU jumps to the BOOT-ROM instructions. The processor generally will be designed to start executing from this location (by initializing the program counter).

Part 2 — BIOS

BOOT-ROM consists of the instructions for BIOS setup.

The instructions that are running now are BIOS instructions.

OK, What is BIOS (Basic Input Output System). This is the Firmware code that is supplied by the motherboard manufacturer. The BIOS instructions that are running this is what it does. In other words this is what BIOS does.

1. Identify, test and initialize system

This process is called POST (power on self-test). Which check that the systems hardware is in good condition and can function properly. The checks are mainly performed on processor, storage devices, memory, CPU registers, DMA (system buses) and other peripheral devices like terminals, printers, external floppy disk drives and other data storage devices, video monitors, keyboards, interface boards, external memory expansion cards, and other input/output devices.

In simple words. It's basically checks all the hardware that enables users and operating systems interact with hardware.

If something goes wrong here, we can hear beeping from computer. Beeping patterns may vary based on the type of hardware problem.

2. Configure System Settings

If you have pressed that f12 button multiple times to open BIOS settings. You would have seen screen like this (this is for old computers)

BIOS

This is the stage where these intractable settings are setup. However, in the modern systems uses UEFI (Unified Extensible Firmware Interface).

UEFI (BIOS++)

But why ? whey should they change it form BIOS to UEFI. By the way they both are low-level software (or firmware) that starts when you boot your PC. But UEFI supports larger hard drives, faster boot times, more security features (Eg: secure boot), and — conveniently — graphics and mouse cursors. Indeed, BIOS cannot load from hard disks larger than 2.1 TB. But why? — I will explain this, once we understand Master Boot Record(MBR) in the next section.

3. System power management (ACPI).

This is basically an interface that operating systems can use to perform power management (e.g. putting unused hardware components to sleep). ACPI defines hardware abstraction interfaces between the device’s firmware (e.g. BIOS, UEFI), the computer hardware components, and the operating systems.

Fun task: Explore if this is how windows allows us to use our pc in different power models like power saving, performance etc.

Finally, BIOS contains code to locate and execute MBR (Master Boot Record). Here is where the control transfers to MBR.

MBR (Master Boot Record)

Let's understand what MBR is before going into what it does

What is MBR and what does it contain?

MBR contains all the code and partition information related to the current disks. It basically has 4 things.

512 Bytes of MBR (source : Knowitlikepro)

1. Master Boot Code

This scans partition table . Finds the starting sector (512 bytes) of the bootable partition. Loads a copy of the boot sector from the active partition into memory. Transfers control to the executable code in the boot sector (we will talk about this in detail).

2. Partition Table

This table contains the memory location of bootloader.

It is a 64-byte data structure used to identify the type and location of partitions on hard disk. This data structure is standard and independent of OS. MBR supports a total of 4 partitions each of 16 byte long. Each partition data structure has various fields, This blog have more detailed information about that.

3. End of Sector Marker

The basic order of operations that happens while a BIOS is trying to find something to boot is to Load first sector (512 bytes) of the device you’re trying to boot from (HDD, SSD, Floppy Disk, Optical Disc etc) into memory. Then check if the 511th and 512th bytes are 0x55 and 0xAA (we can observe that in the above code 2.1 final bits are 55 and AA), respectively. If yes, start executing code at the beginning of this sector, thus passing control to a boot loader/manager.

Let me explain what I promised in the BIOS section about.

why does BIOS cannot support larger hard drives ?

It is because the way the BIOS’s Master Boot Record (MBR) address system. It only support 32 bit address and the granularity of memory access is 512 bytes ( one sector in memory). So if we calculate (2**32) * 512 = 2.2 TB approx. UEFI uses GPT instead of MBR and GPT supports 62 bit address which can support up to 9.44 Zeta Bytes. Isn’t that cool !!!

When Master boot code — as explained above — loads a copy of the boot sector from the active partition. This is OS bootloader, the user supplied code used to boot the operating system. Let's discuss about this in detail.

Part 3 — OS Bootloader

From here different systems implements it a little differently but its pretty much similar and it is customizable too !! . However, its not necessary that the bootloader belongs to an OS. We can also use GRUB to choose which operating system we want to load. If you have ever dual boot your PC you should have encountered this step. Lets talk abou how bootloader sets up Linux kernel in x86 syatems

When we dual boot our PC with Linux parallel to the windows. Grub sets up the windows disk partition as an option to boot along with Linux option.

We can see the grub file after we boot Linux. It looks something like this.

Here we can see that the Ubuntu is the menu entry in GRUB and there are various fields under it among which most important ones are initrd and linux.

intird is the first program that gets executed by the bootloader is the initramfs (initial RAM file system), which is also known as initrd (initial RAM disk). The initrd is a small-sized file system that gets loaded into the memory during the early stages of the boot process.

The main purpose of initrd is to provide the necessary drivers and utilities that are required to mount the root file system, which may be located on a different storage device or require specific drivers that are not included in the kernel image.

Fun Task: You can view contents of the initrd using the command lsinitramfs /boot/<name of initrd image>. If you find anything interesting comment it.

The initrd image is usually created by the system administrator or the distribution maintainers during the installation process. It can contain various types of files, including executable programs, device drivers, configuration files, and other resources needed for booting.

Once the initrd is loaded into the memory, the bootloader passes control to the initrd, which then runs the init script, which performs the necessary tasks to bring up the system.

If you are interested in exploring more, the code for loading and executing the initrd is located in the arch/x86/boot/compressed/head_64.S file for 64-bit x86 architecture, and in the arch/x86/boot/compressed/head_32.S file for 32-bit x86 architecture.

The init script may perform a variety of tasks, including mounting the root file system, initializing hardware, setting up network interfaces, starting system services, and running user-defined scripts.

After the init script has completed its tasks, it hands over control to the kernel, which run as a bunch of code and loads the necessary modules and completes the boot process. The kernel then runs start_kernel() in init/main.c, which is responsible for starting all the other system services and user-level applications.

The start_kernel() function is the entry point for the Linux kernel initialization process and is responsible for setting up various kernel subsystems, such as memory management, file systems, device drivers, and process management, among other things.

That concludes the blog. Right now the system will be in a state where users can run their applications and get the results on screen. However each function that is running in side start_kernel() does tons of things. Which we will cover another time.

Hope this helps. If you have any doubts please comment them. I will be happy to answer them.

--

--