How PC Boots up?

Have you ever given it a thought that when you press the power button on your laptop or PC, what happens behind the logo of Windows XP/Vista/Seven or Linux. From the pressing of the power button to the appearance of the login screen there are more than hundred components/peripherals that are initialized and thousand lines of code is executed. We will look inside the machine, that actually what all happens. So let us start…

What is Booting?

Booting is a process or set of operations that loads and hence starts the operating system, starting from the point when user switches on the power button.

Boot Sequence:

Basically documents related to booting are generally confusing as they are often related to some specific operating system that is Linux machine or Windows machine. But let us see it as general . General Booting sequence comprises of the following steps:

· Turn on the Power button.

· CPU pins are reset and registers are set to specific value.

· CPU jump to address of BIOS (0xFFFF0).

· BIOS run POST (Power-On Self Test) and other necessary checks.

· BIOS jumps to MBR(Master Boot Record).

· Primary Bootloader runs from MBR and jumps to Secondary Bootloader.

· Secondary Bootloaders loads Operating System.

These are the tasks that are carried during booting process. Now let us discuss them in detail.

As soon as we turn the power button, the reset signal is sent and the registers in the CPU are set to their pre defined value.  The first and foremost is the reset vector as shown in the figure (example is taken of 4GB RAM). It should be noted that RAM contains the garbage value at this time, and the instructions/data stored at any memory location is due to the memory map of the chipset. Memory map maps the location (address) to flash memory containing values or instructions.  It is ensured that the instruction stored at this reset vector location is jump to system BIOS, as BIOS takes up further process of powering up the system.

BIOS-Basic Input Output System
As we have seen that at power up CPU is reset and its registers are set to the default value, which is an address pointing or directing to the hardware containing BIOS. Generally the hardware is EEPROM containing the BIOS. The tasks performed by BIOS are categorized as follows:
·         POST– Power on Self Test is the foremost routine which checks and tests the basic hardware. If it fails then it displays error.
·         Initialization of the hardware devices by letting them run their individual BIOS( eg. video card have their own inbuilt BIOS code).
·         Searching for the Master Boot Record and reading it.
·         Copying the boot sector code to RAM and then switching the control to it.
Boot Sectors
A sector is a part of the hard disk having length of 512 bytes. A sector is termed as boot sector because of its location and because this sector is responsible for the further boot process of the system. This boot sector is generally called Master Boot Record. The MBR is a 512-byte sector, which is located in the first sector on the disk (sector 1 of cylinder 0, head 0). As soon as BIOS gets the boot sector, it tends to copy MBR to RAM and switches the execution authority to it.
  • In the MBR the first 446 bytes are the primary boot loader, which is also referred as PBL.
  • The next sixty-four bytes are the partition table, which has the record for each of the partitions.
  • The MBR ends with two bytes that should be 0xAA55. These numbers act as validation that this sector is the boot sector or Master Boot Record.
  • If machine has 2 operating system, then the Primary Bootloader gives user a choice to select which operating system user wants, and after his/her selection jumps to the Secondary Bootloader-which is specific to an operating system.

Let us consider an example.
  •  If a machine has Windows/Linux machine, then it will have only one bootloader (generally it is split into multiple stages) that is DOS/GRUB bootloader which will load the operating system.
  •  If a machine has 2 operating systems then the primary bootloader (GRUB if Windows and Linux are present) will give user a choice to select which operating system he/she wants that is either Windows or Linux, and corresponding to OS bootloader (secondary) DOS or GRUB will load the operating system.
  • In any of the cases, due to small size of master boot record (512 bytes) the initial stage of any bootloader just contains basic information and leads to another sector, that contains the further code needed by the boot procedure. This is stage 2 of bootloader and in Linux is GRUB2 stage and in Windows XP NTLDR and winload.exe in case of Windows Vista/7/Server. Then this leads to reading of the configuration file, which contains the settings associated with bootloader, for example in case of Linux this is grub.conf and boot.ini in case of Windows XP (in Windows Vista, Windows 7 Windows Boot Manager reads the Boot Configuration Data-BCD which is configuration data needed at boot time). These are the configuration files, and contain the settings and the arguments that are passed to the kernel because at runtime no arguments can be passed to the kernel.

After this process is operating system specific so we will discuss it individually

After the configuration file boot.ini, there is NTDETECT.COM, which checks the hardware of the system and passes the related information to NTLDR. Then NTLDR creates a hardware key by using the passed information and that is the reason that after installation of a new device, system asks for restart because change in the hardware causes change in the hardware key and it needs to be updated.
In case of vista and 7 windows boot manager does the initialization task by reading the boot configuration data and boot.ini is replaced by BSC found in boot/bcd. Rest process related to kernel and device driver remains the same.
When all the tasks associated with bootloader is over the jump is made to operating system core that is its heart – kernel. ntoskrnal.exe is the kernel file in a Windows machine, located in system 32 folder. It contains the cache manger, executive, kernel, security reference monitor, memory manager, and scheduler. This file initializes kernel data structure, non-boot CPUs, object manager, I/O manager. Kernel is the layer which allows the user space applications and request to interact with the systems hardware. Kernel is operating system heart and is responsible for memory management, synchronization, interrupt handling etc. With ntoskrnal.exe; hal.dll, bootvid.dll, boot start drivers are also loaded. It then switches to protected mode. Next thing which comes in picture is HAL layer that is Hardware Abstraction Layer. From its name it is clear that this is machine specific file (board dependent) and in case of Windows it is hal.dll located in system 32 directory. The last thing left to be loaded are Device Driver. They are integral part of any system. They are specific for devices and allow the interaction of outer world with devices and vice versa. They are responsible for the operations performed by devices and are loaded from system32\config\system.  The Csrss.exe and Winlogon.exe are then started and then Winlogon starts Services.exe, which starts the Windows machine Services, Service Control Manager (SCM) and the Local Security Authentication subsystem Lsass.exe.
In case of Windows 7, the boot procedure has taken a giant march ahead of its predecessors. In case of Windows 7 the image displayed at the boot up time changed from Vista in terms of resolution (from 640*480 to 1024*768) and colour depthness/richness. The animation at boot time doesn’t use and drivers (because at initialization no driver is initiated) and hence uses CPU directly. This also increases time to initialize kernel and drivers at the background. So if we see then over the time configuration files, bootloader have considerably changed in case of Windows family.
The bootloader loads kernel as well as the temporary filesystem before the actual file system is loaded. In earlier versions of kernel initrd (ram disk) was used but since 2.6 kernel initramfs (ram file system) is in use. This is just temporary, in case of embedded system it is sometimes the final file system.
In case of Linux the image of the kernel is divided into two parts on the basis of its loading that is real mode and protected mode. The process always occurs in the real mode. The first entry point in the initialization of kernel is head.S (arch/i386/boot/head.S), which is assembly language written file whose task is basic hardware setup. From here there is jump to head.S (this is present in boot/compressed directory) whose task is set the basic environment and calls decompress_kernel (present in boot/compresses/misc.c) and then to startup_32 (present in kernel/head.S). Then it moves to main.c present in arch/x86/boot directory.
After all these /sbin/init, /etc/init, /bin/init, and /bin/sh are called in the order; the first user process is init and they read etc/events.d or etc/inittab. Hence the system starts.
In case of linux machines the booting procedure has hardly changed with time; except for initrd to initramfs.
This is how computer boots up and we see our login screen.