How do computers know which operating system to boot?
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.
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:
On startup, your motherboard will power all (or most) electronic components attached to it. One of them is your hard drive or SSD, called the system drive, because the primary operating system is on it. Unless you've set your BIOS or (U)EFI to boot from a different, external drive, be it USB, FireWire or network, that's the drive that'll be used first.
On this drive, there is a master boot record partition (to simplify) that's usually read-only (can be edited via the bcdedit.exe set of commands, though), and it will review all available OSes on the drive. One has been set as default, either by you, or by itself when installed, either in factory or when you reinstalled/upgraded your main OS.
Windows doesn't allow your booting from a USB hard drive or SSD. You can boot on a USB key, that hosts a specific USB controller, but booting from a USB drive connecting via cable is not allowed by Windows. The necessary drivers are not loaded until well after the first Windows services and processes (smss.exe and crss.exe) are already loaded, which means that Windows itself has started.
Solution 4:
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).