What Happens after you power on your Computer System?
Tags: bios, bootstrapping, kernelloading, operatingsystem
Published: 2021-04-14 05:18:16

In this article we’ll discuss about the booting process of our computer system. Our Computer system is just the combination of hardware and software. The team work of software and hardware is the way our life becoming easy and easy. The point any member of this team denies to cooperate , we’re left with just a piece of electronic , inert in nature.

In our computer system we consider our hardware(keyboard , mouse , memory chip , monitor , disk etc)as a resource and we’re being provided a manager who’s job is to manage those resources in an effective manner.So who’s this resource manager? ‘Operating System’ is this resource manager. Operating system is actually combination of several software modules which are being developed for any specific task(We’ll go deeper into operating system someday later). This operating system somewhat works like an intermediary between user and hardware.We store it on some permanent storage like our hard disk.So basically for faster processing we load it(not entirely but only the module we need at present) to RAM(Volatile memory) during execution.

So the interesting thing is , when our computer system is in shut-down state.We can guess when system is off , our RAM must be totally empty(due to volatile nature)So here comes our main point. You power on your Computer and RAM is empty and to load operating system from Hard drive to RAM you need to execute some piece of code (there must be some program who perform this loading job) but RAM is empty so you can’t anymore use your computer system unless you’ve found some way to tell CPU how to load operating system (booting) without RAM in initial phase(startup) until RAM comes into picture after booting.

If you’ve studied about [Firmware](https://laymentogeeks.wordpress.com/2020/07/20/firmware/) you can guess how can we solve this issue. [BIOS](https://en.wikipedia.org/wiki/BIOS) is the firmware responsible for basic hardware diagnostics and utilities necessary for loading operating system , without a BIOS no computer system can ever function. Now we’ll discuss the entire [Booting](https://en.wikipedia.org/wiki/Booting) process step by step. We’re dividing the boot process in different stages , albeit the actual implementation of those stages highly depends on the hardware and Operating system but still basic functionality and protocol won’t change.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/71n3d3rx41wljq24ym2x.jpeg)



## 1)Pressing power on button ##

As soon as we press power on button , it’s [Power supply unit(PSU)](https://en.wikipedia.org/wiki/Power_supply_unit_(computer)) that receives the power and as we know computer is an electronic device and electronic devices work on stable DC voltage so it’s responsibility of PSU to inform the processor to start the booting work when it has done with all its internal self-tests and the output power stability work. PSU do it using [Power good signal](https://en.wikipedia.org/wiki/Power_good_signal). PG signal is +5V signal through which PSU tells processor that the magnitude of output voltage and current won’t rise or fall to any abnormal level and now system may proceed to boot(PSU takes almost some milliseconds to generate it).Until Processor Timer chip receives this PG Signal they keep on applying RESET signal to processor and this stops processor to boot prematurely.

```in the x86 architecture, asserting the RESET line halts the CPU; this is done after the system is switched on and before the power supply has asserted “power good” to indicate that it is ready to supply stable voltages at sufficient power levels.```

Wiki:- [Reset line](https://en.m.wikipedia.org/wiki/Reset_(computing))



Now as soon as the voltage is stabilized and Motherboard is being told by PSU(by PG Signal) that System may now happily proceed to boot(RESET Signal disabled now) , Firmware([ROM BIOS](https://en.wikipedia.org/wiki/BIOS) or [UEFI](https://en.wikipedia.org/wiki/Unified_Extensible_Firmware_Interface)) comes into picture , that’s the actual program(coded on main memory non volatile chip) behind all booting process. So let’s discuss it , dig it ( again a reminder that actual booting is entirely architecture dependent , we’ll only discuss the rudimentary paradigm)

##Booting (Bootstrapping)##

At this very moment when processor just received the green signal to start bootstrapping the system , our system is unaware of any operating system, any RAM or hard disk but we as a user know it that Operating system is installed on hard disk(for a time suppose, albeit Firmware is coded properly to find the bootable device) and RAM is empty then how to load OS without RAM?

Ofcourse program the motherboard’s non-volatile chip with initial instructions (those would help the processor to test all the available hardware and make them workable).Processor is programmed ([Microcode](https://en.wikipedia.org/wiki/Microcode)) to execute it’s very first instruction from a fix location in ROM(Address of this location is again architectural specific) and this location is known as [Reset vector](https://en.wikipedia.org/wiki/Reset_vector). 

```When a computer is powered on, the processor first accesses a predetermined area of the system BIOS ROM to access the BIOS boot program. This location is at memory offset OxFFFFO, sixteen bytes below the top of real mode memory. Real mode is the default operating mode for modern Intel-architecture processors, allowing the processor to mimic the 8088 chip. Offset OxFFFFO is not located in RAM, but actually on the BIOS ROM chip. This location contains a jump instruction to a memory offset that contains the BIOS startup code.```



 
       
```A processor starts it’s booting in Real mode and stays in this mode until operating system is loaded enough modules that can make it possible to switch to Protected mode.Now if you’re going to ask why doesn’t processor start immediately in 32-bit protected mode then it’s Backward compatibility.```


Just a matter of architecture , the Reset vector might guide us to the very first instruction([POST](https://en.wikipedia.org/wiki/Power-on_self-test))address of the Firmware (BIOS or UEFI) or it may itself contain this.




##Shadowing##

A technique used to increase a computer’s speed by using high-speed RAM memory in place of slower ROM memory (RAM is about three times as fast as ROM). On PCs, for example, all code to control hardware devices, such as keyboards, is normally executed in a special ROM chip called the BIOS ROM. However, this chip is slower than the general-purpose RAM that comprises main memory. Many PC manufacturers, therefore, configure their PCs to copy the BIOS code into RAM when the computer boots. The RAM used to hold the BIOS code is called shadow RAM.

**1) POST :-**  without any external help(that’s why call ‘Self Test’) the firmware(UEFI or BIOS) performs the routine diagnostic power test on the hardware. It’s the first code BIOS runs and after the successful completion of this test BIOS can start to bootstrap operating system. Till the Video card is being tested and activated properly , POST uses different beep signals to alert the user about fatal errors being encountered during the process.As soon as video card is activated(by running it’s firmware) now user can see booting process on monitor and all fatal errors would be displayed on the screen now onwards.For more details about the POST Hardware test read [this](http://www.bitsavers.org/pdf/solbourne/101486-AC_System_Power-on_Self_Test_Manual_Aug89.pdf) and [this](https://docs.oracle.com/cd/E19464-01/820-6850-11/POST.html#50602040_20571).

**2) Loading Boot loader :-** The very last step of POST procedure is to execute INT 0x19 instruction. This very last instruction actually find the [Boot device](https://simple.wikipedia.org/wiki/Boot_device) (consider Hard disk as our boot device) using bootstrap sequence and then it reads 512B(in hard disk we call this 512B sector [MBR](https://en.wikipedia.org/wiki/Master_boot_record)) from the first sector of ([Track0](https://en.wikipedia.org/wiki/Track0) sector 0) of Boot device into RAM at absolute address 0x7C00(not boot device specific). 

```The GUID Partition Table(GPT)was introduced as part of the Unified Extensible Firmware Interface (UEFI) initiative. GPT provides a more flexible mechanism for partitioning disks than the older Master Boot Record (MBR) partitioning scheme that was common to PCs.```




![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7bntuol05l19la2i83ii.gif)


**Stage 1 Bootloader :-**  As soon as MBR is loaded to physical memory , BIOS (with the help of 2B boot signature) identifies the boot loader code at the beginning of MBR and then BIOS starts scanning MBR’s partition table to identify the active(bootable)partition. Bootable partition possess copy of operating system installed and in case of multiple operating system installed ([Multi booting](https://en.wikipedia.org/wiki/Multi-booting) like windows and linux) there can be multiple active partitions. MBR boot code isn’t big enough to contain a boot loader supporting complex and multiple file systems so such boot loaders are split into pieces. MBR code possess the smallest piece of boot loader and other pieces are placed with the boot loader of active partition.After execution of MBR boot code(stage1 completion), control is transferred to active partition boot code(stage2 bootloader) ,which possess rest of the kernel code.

**Stage 2 Bootloader :-**  Primary bootloader (MBR bootloader stage-1) loads the additional bootstrap loader (stage-2 booting) and this provides user an option for multibooting. [GNU GRUB](https://en.wikipedia.org/wiki/GNU_GRUB) and [LILO](https://en.wikipedia.org/wiki/LILO_(boot_loader)) are to most common bootstrap loaders. GRUB gives us freedom for [Chain loading](https://en.wikipedia.org/wiki/Chain_loading) while lilo is Linux specific. With the help of file system stage-2 bootloader loads the default kernel image and initrd image into memory. With the images ready, the stage 2 boot loader invokes the kernel image.
As soon as [GUI](https://en.wikipedia.org/wiki/Graphical_user_interface) appears the boot process is fully completed and now user can interact with the system happily

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sc3nc1xc4uelngazvlyz.png)


Visit my [Blog](https://laymentogeeks.wordpress.com/) for more such in depth articles and don't forget to share your valuable feedback.

Thanks!