How do computers know which operating system to boot?

5,298

Solution 1

Unfortunately this has become more complex as computers have evolved.

Initially (ie the first PC's) there was a fixed order (which depended on cabling).

Almost all computers still have a default boot order which can be controlled/influenced by the BIOS. Newer computers supplement this functionality with UEFI, which in some ways supercedes the BIOS.

For BIOS booting there is a preferred order. The BIOS looks at each drive for bootable boot sectors, and will attempt to load these, going down its list of options until one boots.

For UEFI there is a filesystem on the disk, (a small fat partition labeled as UEFI which contains a first stage boot loader and instructions. In some cases this is supplemented by instructions programmed into the UEFI (ie settings stored in nvram similar to the BIOS)

Once a boot loader/UEFI partition has been found, a program takes over the boot process, and this may offer the ability to boot into different modes or OSs.

Solution 2

As others said, it's become a bit complicated, but the way I understand it:

When the computer is first powered on, it's not your hard drives at all that matter. There's a special piece of software in a chip on the motherboard which gets loaded first and the CPU starts to execute that. This is known as BIOS or UEFI (UEFI is actually the successor of BIOS).

This code does a bit of work initializing various parts of hardware and then starts to look for the next thing to pass control to. Now, what exactly that will be depends on a lot of things. There are many possible options - your internal hard drives, your external drives, network adapters, RAID controllers, add-on cards, etc.

BIOS/UEFI also has settings that determine the order in which to check these options. It will go through each one of them in the configured order and ask - "can you continue to boot?" And the device will either say "yay" or "nay". The first one that says "yay" gets to continue. That device then gives BIOS a piece of code which is loaded in memory and the CPU continues the execution with that.

In the particular case of a hard drive, the disk itself doesn't really know if it can boot or not. So what the BIOS does is that it reads the very first sector (512 bytes classically) of the drive and checks for a special few bytes in a certain location of the sector. If they contain the value 55 AA (in hexadecimal), then this sector is executed as code. Otherwise it moves on to check the next disk.

Now, 512 bytes isn't a lot of space, but it's enough to do a few simple things. For example, classical DOS put a bit of code in there which found a partition marked as "bootable", then loaded the first sector of that partition and then executed that. And that next sector contained enough code to load a few more sectors, and then start parsing the file system, and load files from that, and so on and so forth.

But really, there is nothing that says what this code must do. It could also not load the active partition, but some other sectors on the disk where more code resides. And that code then could give you a menu which allows selecting among multiple bootable partitions or OSes or whatever.

Anyways, the code in the first sector of the drive is known as "bootloader". There are different bootloaders out there. The Linux world these days leans heavily on grub, Windows has its own thing, etc. The bootloader really comes before the operating system, but it does need to know how to load the operating system into memory and give control to it.

Does this make it clearer?

Solution 3

The motherboard contains firmware that runs bootloaders that boot OSes, and the two main types of such firmware are UEFI (new) and BIOS (old). In both cases, there’s a small amount of non-volatile storage on the motherboard which records the user’s preference as to which bootloader to use. It’s also often possible to press a certain button during boot to override this preference for this particular boot.

BIOS simply records an order of physical drives (plus generic entries such as “USB drives” which may be tried, if present, before other things, or after, or never), and booting involves loading the MBR of the first drive (the first 512 bytes) into memory and running the code it contains. If that fails, BIOS tries the next drive. 512 bytes isn’t much, so the typical BIOS bootloader installation process stores the bootloader somewhere else, notes its location on the drive and puts a small piece of code into the MBR that merely runs the actual bootloader.

UEFI, unlike BIOS, has the capability of reading files from FAT32 partitions. There has to be a partition marked with a certain GPT code, and UEFI settings include a list of paths to files found therein. UEFI will find that partition, load the file that’s the first in the list into memory and execute the code it contains. Later, having booted an OS, it’s possible to modify UEFI settings from within the OS, something that typically isn’t possible with BIOS.

In either case, a bootloader such as GRUB is started, and it can be quite a sophisticated program, containing its own hardware and filesystem drivers so it can read files from partitions, even encrypted ones. Typically, it reads its own configuration from a file in a predefined place on a predefined partition, which contains a list of OSes and parameters to be passed to those, and displays a menu to the user. Once the choice is made, it reads the file containing the OS kernel, puts it into memory according to the requirements of that OS and launches it. From that point on, the kernel is in control.

Some OSes, such as Linux, have their kernel file laid out in a certain way compatible with UEFI requirements, so they can be booted from UEFI directly without the use of a bootloader, but this approach is uncommon.

The bootable flag comes from the times when bootloaders were typically put in volume boot records at the start of a partition (as opposed to the start of the entire drive), and the MBR code would enumerate the partitions and chainload the bootloader from the one marked bootable. The flag is irrelevant with modern bootloaders.

So the user can choose which OS to boot both by means of UEFI or BIOS and by means of the bootloader, in practice it’s almost always the latter (UEFI/BIOS load the same bootloader each time which then displays a pretty menu and boots the chosen OS).

Share:
5,298

Related videos on Youtube

Matthew Inbox
Author by

Matthew Inbox

Updated on September 18, 2022

Comments

  • Matthew Inbox
    Matthew Inbox over 1 year

    When I turn on my computer, what is the software that selects which operating system to boot, and how does it decide when given multiple options?

    More specifically, what if I have a computer with multiple operating systems living on the same disk? And what if I have external drives - does it search every partition across all storage devices for formats that it recognizes?

    From my understanding every disk has its own boot sector that contains information on which partition the computer should boot. But what if I have multiple disks attached, where each disk has a different boot sector - which one does it load?

    When trying to research this topic, I've run into some confusion since not all technologies behave the same (e.g. some say that the bootable flag is important while others say it is ignored). Some clarity in how different systems work would be much appreciated.

  • user13267
    user13267 about 4 years
    Do you have links that explain the UEFI process in very simple terms, and a comparison to the older BIOS boot procedure? I understand the BIOS and MBR method for the most part, but everything I could find about UEFI was too complicated. Are EFI/UEFI the same thing? Most articles that talk about UEFI bring up something called "information in the NVRAM". Does the NVRAM mean the BIOS? Or is there a permanent memory besides the BIOS on a motherboard that supports UEFI booting?
  • davidgo
    davidgo about 4 years
    @user13267 These questions are slightly past the edge of my knowledge - I believe UEFI is the newer version of EFI, so effectively the same thing. As I understand it, BIOS is different from UEFI booting, so no, NVRAM does not mean BIOS. I get the distinct impression that this is stored in the same type of memory (probably on the same chips) as the BIOS. I think the UEFI process itself supports the booting, and is analogous to the BIOS, but hands off to a disk or external memory earlier, and in a more flexible way.
  • TOOGAM
    TOOGAM about 4 years
    @user13267 UEFI is an updated version of EFI after the inventors of EFI donated EFI to the UEFI (Unified EFI) forum and stopped updating EFI themselves. So, yes, UEFI is the modern EFI. NVRAM means "Non-volatile RAM" which, unlike most bytes of RAM, does not lose data when power is turned off. This stuff is more expensive, so instead of having all your RAM be NVRAM, such NVRAM is typically used to store smaller amounts of data, like configuration details storing information about how you want your system started.
  • TOOGAM
    TOOGAM about 4 years
    BIOS is "Basic Input/Output System". For years, "IBM Compatible" systems used compatible BIOS code which some operating systems relied upon. When EFI was released, the statement was "this is not BIOS" because EFI was largely incompatible with other existing popular BIOS implementations. So the term "BIOS" started to clearly refer to the standard ways that older BIOS worked. But some motherboard referred to EFI as a BIOS (e.g., "dual BIOS" options) . As is, the waters are really muddy. Whether EFI is a BIOS or not depends on what a BIOS is, so there is not universal agreement to this question
  • user13267
    user13267 about 4 years
    @TOOGAM do you have a link that explains all of this in simple, possibly layman terms with diagrams?
  • TOOGAM
    TOOGAM about 4 years
    Not offhand. Also, restrictions prevent some of my ability to further help.