Skip to content

The “magic” of a PC booting

(Quick translation of old Swedish text I wrote a long time ago after looking into how a PC actually starts up)

Introduction

In order for a PC to be able to boot, there are a lot of criteria that must be met. A key component in the entire procedure is the BIOS (Basic Input / Output System) located on the motherboard. But the magic behind how the BIOS manages to understand that it should boot a device and how it manages to boot operating systems such as Windows, Linux, etc. from there. is not really that magical. Instead, it is governed by strict rules for how the boot process should go and conditions that must be met. Although there are some differences between different BIOSs, etc., here we go through the very basics of how a PC actually boots.

Time to get up

Power

When you press the power switch, a signal is sent to the system board control circuit which starts the power supply. As it takes a while before the power supply can deliver a reliable voltage to the components, the control circuit sends a so-called reset signal to the processor. Simply explained, it can be said that this signal is sent for the processor to wait for the system to be ready for the processor to start working. This signal is the same signal that is sent when you press the reset button on your computer’s chassis. If you have ever held down this button, you may have noticed that the computer does not restart until you release the button again. When the current is stable, the signal “PWR OK” is sent from the power supply. This signal is then sent continuously from the power supply as long as no faults are detected. When this signal is received, the boot process can begin.

BIOS

In order for the processor, which currently has no instructions in memory to execute, to be able to do something productive, it is pre-programmed to look for the system BIOS in a specific place in the system memory. The position for this is usually 0xFFFF, which is 16 bytes from the end of the system memory. By putting the BIOS there, you guarantee that the processor knows where to start executing. However, it is difficult to fit the entire BIOS at 16 bytes. This has been solved by placing a single instruction at the position 0xFFFF, a “jump” instruction that tells the processor where it can find the correct BIOS code. This way, the actual BIOS code can grow without creating compatibility issues and the processor always knows where to look to find its first instruction.

The BIOS now performs something called POST. POST stands for Power On Self Test and performs some checks such as that each component has the correct voltage, that the memory is not corrupt, etc. If any fatal errors are detected, the boot process is interrupted. POST can also return error codes in the form of so-called “beep codes”. These can differ between different BIOS manufacturers but are very useful when it comes to specifying errors that prevent your computer from booting.

Other components in the computer such as sound cards, HDDs, graphics cards, TV cards, etc. are also equipped with their own small BIOS programs. The BIOS now scans these components and runs their BIOS programs. First out is usually the graphics card and its BIOS is usually found at 0xC000. This initializes the graphics card. The BIOS of the other components is then run in the same way.

Now that the graphics card is initialized, the BIOS has the ability to draw to the screen and now does a number of additional tests and prints errors it finds on the screen. You may have seen a message complaining that you do not have a keyboard or mouse plugged in at any time. That message is generated by the BIOS here.

The BIOS now checks the type of hardware in the system. Automatic HDD parameters are set and logical drives such as COM and LPT ports are named.

There’s no place like… 0x7C00

Boot sector

Now the BIOS will look for what is called boot sector on any of the connected devices. The boot sector contains code to boot from the device and, for example, boot your operating system (OS). Which devices are checked is set in the BIOS. Usually your HDDs and then other devices are checked. The first device in the list that contains a boot sector that meets the requirements to be bootable will be the device being booted.

I mentioned earlier that the seemingly magical start-up of a computer is actually tightly controlled. The boot sector must therefore meet a number of criteria for the BIOS to be able to load and run it.

  • Must be located in the first sector of the device
  • Must be exactly 512 bytes
  • Must end with 0x55 and 0xAA at positions 511 and 512

If the BIOS finds a sector that meets these requirements, it is loaded into memory at a specific position, 0x7C00. The register dl is set to the number of the unit from which the boot sector is loaded. The first HDD has 0x8000 and the first floppy drive has the number 0x0000. If no device contains a boot sector, the BIOS reports a “Disk boot failure” error.

A lot of registers are now set to 0 and the processor ends up in what is called “real mode”. The BIOS then instructs the processor to perform a “jump” to 0x7C00, which transfers control to the code loaded from the boot sector.

As the boot sector is only 512 bytes, it can not contain huge amounts of code. Instead, it is usually just responsible for loading and executing more code. For MS-DOS, for example, IO.SYS is loaded, which then loads and executes more data.

Master boot record

Since hard drives can be partitioned into several partitions, all of which can contain bootable OS, the reasoning above leads to a problem. As different OSes may require different bootloaders to be able to boot, we must replace the bootloader in the boot sector every time we are to boot another OS.

To get around this, the real bootloader in the first sector is not on an HDD. Instead, there is something called the Master Boot Record (MBR). However, it is handled exactly the same by the BIOS. That is, it loads at the position 0x7C00 and ends at 0x55AA. Executable code is found between offset offset 0x0000 – 0x01bd. But in addition to only containing executable code as the boot sector does, MBR also contains a partition table with information about the partitions on the HDD. These entries for the four primary partitions are 16 bytes each and are located on offset 0x01BE – 0x01FD followed by a signature of two bytes 0x01FE – 0x01FF.

OffsetSize (bytes)Explanation
0x001Boot indicator (0x80 = bootable, 0x00 = non bootable)
0x011First “Head” number
0x022First cylinder number (10 bits) and sector (6 bits)
0x041“Descriptor” (Type of partition/filesystem)
0x051Ending “Head” number
0x062Ending cylinder and sector number
0x084Starting Sector (relative to the beginning of the HDD)
0x0C4Number of sectors in partition
The form of partition entries

The boot loader for a partition is located in the first sector of a partition and is in the same shape as a boot sector. The MBR checks which partition is active, and then loads its boot sector at location 0x7C00 and instructs the processor to make a “jump” to that position. Note that MBR was previously loaded on 0x7C00. The MBR must thus move itself out of the way before the boot sector is loaded.

This makes it possible for several different OSes to be located in different partitions on the same physical HDD and be able to be loaded with different bootloaders.

Comments

Share on activity feed

Powered by WP LinkPress