How Linux system boots up?

Power On

It all starts when Power On button is hit.

The Switching Power Supply component takes approx 0.1 to 0.5 seconds after power supply is switched on to generate POWER GOOD signal to motherboard and determine that the power is ready to use after it has passed its internal self tests.

Power Good signal is designed to ensure all the required voltage for computer are always stable, which is +5 volts signal. The motherboard relies on the processor timer chip which manages the reset line to the processor. Once power supply completes its initialization and stabilizes the output, the power good signal is sent to the timer chip which in turn stops reseting the processor else timer chips applies constant reset signal to the processor and the computer cannot be turn on.

The processor is ready to start. But it doesn’t know where to search for instructions and what to do.

So the BIOS comes into the picture:

To get to the operating system, a computer must boot from BIOS. First operation performed by CPU is to read the address space at FFFF:0000h. This is default address set by the makers.

Address is last 16 bytes of memory and it contains only one instruction i.e jump (JMP) which gives the address to the actual BIOS program location in memory. This redirection to the actual BIOS is called bootstrap. BIOS starts with POST (Power-on self-test) which is initialization and test of computer hardware. BIOS will start with motherboard, CPU, timer IC’s, DMA controller, (Interrupt handler) IRQ controllers. If any test fails during this then CPU produces various codes of beeps.

If video ROM is present then BIOS will validate its contents with checksum test, if tests are success then the control is transferred to video BIOS which will initialize and load the video adapter if initialization is a success then cursor will appear on the screen and if it fails then there will be error message shown on screen.

After this, memory will be scanned and if everything looks good then BIOS checks for a flag which tells CPU if it was a warm or cold start. If the value is 1234h then it a warm start — restart, if it’s a cold start then full POST routines are executed.

After POST is complete BIOS identifies other system devices. It uses the information stored in the CMOS (we will come back to this soon) chip, and the information supplied by the devices themselves to identify and configure the devices. At this point the plug and play devices are identified and allocated system resources.

Now it’s time to find the OS. But before that let’s check what is CMOS?

CMOS is a RAM of 64k size that stores various parameters like system setup, date, time. CMOS settings will tell us the boot order i.e the order in which to look for bootable device which will be something like: CD ROM, hard disk, USB… This order can be modified by changing it in CMOS setup.

After getting the boot order from CMOS, BIOS will try to load the first sector (Boot sector) from the disk into RAM. If the sector cannot be read then the BIOS considers device as unbootable and proceeds to the next device.

Say, BIOS was successful in reading the first sector of hard drive. BIOS will check for boot sector signature present at the last two bytes of the sector which is 512 bytes long before accepting the boot sector and considering the device bootable. BIOS transfers the control to the loaded sector which is first sector of the hard disk with jump instruction. This first sector of the bootable disk sometimes called as Master Boot Record (MBR).

NOTE: BIOS will not process the content of the boot sector.

Distribution of 512 bytes of MBR

MBR contains the information about how logical partitions, containing file systems are organized. MBR contains executable code to function as a loader to the operating system. This MBR code is referred to as boot loader.

Confused?

Let’s take a quick look: 1st sector (512 bytes) of bootable device is called boot sector or sometimes MBR. The code present at this location is called boot loader.

Boot loaders with bigger footprints are split into pieces with first piece/stage stored in MBR and other pieces/stages at other locations. There are various types of Boot loaders present in market like: LILO, GRUB 1, GRUB 2, Loadlin, SYSLINUX / ISOLINUX

Here we will discuss about GRUB (Grand Unified Bootloader)

By default, MBR code will look for partition that is marked as active and loads its boot sector into the memory and pass the control to it. Here the GRUB will replace default MBR with its own codes. GRUB works in stages:

Stage 1: boot.img present in MBR or optionally in VBR and configured to point to address of another stage for first sector of core.img

Stage 1.5: Hard disk sectors are counted from 0 to last sector. Sector 0 has stage 1 and partitions starts from sector 63, So the sectors between 1 to 62 is empty and where stage 1.5 (core.img) resides contains file system drivers to address the stage 2 by file path and file name.

Stage 2: Files that belong to stage 2 are in /boot/grub. Now stage 2 is loaded, it presents a user interface for selecting OS to boot. If the timeout is set to 0 seconds then GRUB loads the default OS into the memory and passes the control to the kernel. Location of the kernel image is specified in grub.conf file. This is the guy who loads the KERNEL image and the initrd/initrmfs image.

GRUB on MBR partition

GRUB’s quick look: GRUB stage 1 loads the stage 1.5 in RAM pass control to it and stage1.5 loads drivers and the stage 2 in RAM passes control to it and stage 2 loads the kernel image and passes control to it.

Kernel image (type: zimage, bzimage) is loaded into memory and decompressed. Now control is passed to main kernel start process.

kernel startup function (process 0) will setup the memory management like paging tables and memory paging, detect the type of CPU.

Also, the boot loader along with the kernel image loads the initial file system image into the memory, then start the kernel passing the memory address of the image. The kernel tries to determine the format of the image which can lead to initrd or initrmfs schema. Depending upon the algorithm compiled statically in the kernel, it can unpack initrd or initrmfs image.

initrd is a file system image for loading a temporary root file system, it is mounted at a temporary mount point and rotated/switched via a call to pivot_call() into different place such as /initrd where normal boot scripts can unmount it and free up the space.

initrmfs is same as initrd but it cannot be rotated, rather it is emptied and final root system is mounted on top.

initrd/initrmfs filesystems contains programs and binary files that perform all the actions needed to mount the proper file system, like providing the kernel functionality for the needed file system and device drivers for mass storage controller (udev) for figuring out which devices are present, locating the proper drivers they need to operate them and loading them. When root file system is found it is checked for errors and mounted.

Thus, kernel initializes the devices, mounts the root file system and runs the /sbin/init process.

Few alternatives for init: systemd, Upstart, runit. Here we will be discussing init.

init process few highlights:

  • First process to be started by the os. (PID = 1) continues to run till computer is shutdown.
  • It is directly or indirectly ancestor of all the process and automatically adopts all the orphaned process.

At this point interrupts are enabled, scheduler can take control of the overall management of the system, to provide multi-tasking and so on and the /sbin/init process is left to continue booting the user environment in user space.

/sbin/init process looks for /etc/inittab config file to check for default Runlevel, there are 7 different run levels from 0 to 6. They define the state of the machine after boot. It will enter the runlevel and run all the relevant boot scripts for the given runlevel, includes even loading of modules, checks integrity of the root file system and set up the network. Various runlevel programs are present in directory /etc/rd.d/.

At this stage, kernel has started all the required process init goes into temporarily inactive state waits for one of the event to occur process to die, power fail signal or signal to change the current runlevel. And finally we see the login screen.

And this is how the linux system boots up!!

— ************************************************************** —

NOTE: Linux boot process is a lot more in depth to what is written in this post. It will be a difficult task to put each and every detail of linux boot process in this one post. So, please feel free to suggest or point out if any info seems wrong. And do refer other materials if you wish to learn linux boot process in depth.

Source: