What happens when you switch on a computer?
By: Divanshu Singh
Tags: Programming, Linux, Hacking, Coding, Electronics

Last year in December, I gave an interview at Microsoft and in the last round of the interview, I was asked this question. What happens when you switch on a computer?

The power button symbol represents a one cutting a zero that comes from the electronics field.

Now before I answer this question, let us spend a minute looking at the internal structure of a hard drive.

The internal structure of a typical hard drive.

For the sake of simplicity, I am assuming that we have just one hard drive attached to our computer. First 512 bytes of the hard drive store something called a Master Boot Record or MBR. MBR contains a table known as a partition table that stores the structure of the remaining hard drive. The remaining of the hard drive is divided into multiple partitions. For a Windows PC, these partitions are generally named C and D drives. At last, there is a small partition generally few GBs in size that is called swap or virtual memory. Swap space is used by a computer to store data that should ideally be in RAM but the RAM may not be big enough. Please note, swap space is not available to the user to store data.

Now in the partitions that are available for the user to store data, one or more of them may be bootable i.e they may have a copy of an operating system installed on them. In our example, we have just one copy of the operating system present in our c drive. But, if we were running a dual boot computer then I might have another copy of an operating system installed in the other partition. A typical example of this case is when you have a dual boot computer having windows installed in one partition and Linux installed in another partition.

If a partition is bootable i.e it has a copy of an operating system installed on it then the first 512 bytes of that partition will contain a program known as bootloader. If you are wondering why there is a magic number of 512 bytes both in the case of MBR and bootloader, then we will take up that question in a few minutes.

Now we know a bit about the internal structure of a hard-drive let’s dive into the original question.

The BIOS

When the power-on button is pressed, the first chipset that gets activated is called BIOS. BIOS stands for Basic Input Output System and is responsible for three things.

First, BIOS performs a Power On Self Test also known as POST. In this BIOS verifies if the computer keyboard, random access memory, disk drives, and other hardware are working correctly or not. If everything is working correctly it will give one beep sound as output. If something is wrong, it will give some other beep sound pattern as output. You can actually use this beep pattern to identify the error. I use a gigabyte motherboard which has the following beep patterns.

1 short beep means the system is normal

1 long beep followed by 1 short beep means RAM is not working correctly or RAM is missing

1 long beep followed by 2 short beeps means Graphic card error and so on.

You can actually find beep patterns for your motherboard manufacturer by doing a Google search.

BIOS settings showing boot sequence options.

Next, BIOS looks at the boot sequence to find a storage device and loads the master boot record or MBR into the RAM. We already know that MBR contains a partition table that has entries regarding partitions on your storage device. Now BIOS will look at the partition table and identify all the bootable partitions. If there is just one bootable partition then BIOS will proceed with the boot process else it will give you an option to select the OS that you want to boot.

The Bootloader

When BIOS knows which partition to boot from, it will go to that partition and again load a program stored in the first 512 bytes of the selected partition. This program is known as a bootloader. If you are using Linux then your bootloader will either be Grub or LILO. In the case of windows, the program is very creatively called windows bootloader. The bootloader is responsible for loading the operating system into the RAM.

If you are wondering why BIOS does not load the operating system on its own but delegates the same task to the bootloader, then the answer is BIOS is a pretty old piece of hardware. It was designed in 1975 and can only access 512 bytes of memory. This is where the magic number of 512 that puts a hard limit on the size of MBR and bootloader comes from. Nowadays, computer hardware manufacturers are moving towards a new implementation of BIOS that is known as UEFI which stands for Unified Extensible Firmware Interface. UEFI can work with 32-bit and 64-bit machines and provides additional features as secure boot. Secure boot simply means to check the operating system for any kind of data tampering since the last boot.

Once the bootloader is copied successfully in the RAM, the BIOS gives the execution control to the bootloader. The bootloader is responsible for loading the first major piece of the operating system into the RAM. This is called a kernel. If you are using Linux, it is called Linux kernel and on Windows, it is called WNT which stands for Windows New Technology kernel.

Once the kernel is loaded into the RAM, the execution control is given to it and it starts the 3 stage process to bring the computer to life.

The Kernel

3 stage process of kernel booting an operating system.

In the first stage, the kernel performs a test known as auto-probing. Auto-probing simply means to check if other hardware components such as wifi-card, ethernet card, hard-drives, USB devices, keyboard, mouse, touchpad, etc are working correctly or not. Once, auto-probing is done the kernel will check if the hard drive is okay or not. Traditionally, mechanical hard drives used to be the most fragile part of the whole computer system. But, this is no longer a problem with new SSD or solid-state devices.

Once all the checks are done, in the second stage the kernel will start the system daemons. Daemons are programs that run in the background and provide services to other programs. There are daemons for services such as printer spooling and networking.

Once the daemons are running, the kernel will execute the GUI manager or the display manager that will actually give you a login screen.

Login screen on Windows 7 operating system.

On a typical modern computer, it takes about 10–15 seconds to see a login screen after pressing the power-on button. During this time, the computer actually executes over a million lines of code just to give you a login screen, which is quite magical.

In my next article, I will describe what actually happens when you enter your username and password into an operating system and how we can tweak the logic with our understanding to login to a local machine without having a username or a password.

Until next time, goodbye.