MBR: How does BIOS decide if a drive is bootable or not?

12

4

While trying to answer this question on askubuntu: How do I Uninstall GRUB I read the Wikipedia article on MBR and also the perfect answer on a somewhat related question here on Superuser, however, one thing is still not clear to me:

What exactly makes BIOS decide if a drive is bootable or not? How does the boot sequence skip from drive #1 and proceed trying to boot from drive #2 if more than one drive is installed in the system?

My understanding is that the only thing BIOS normally checks on an MBR is its signature at the very end of the 512-byte sector, and then it just transfers control to the initial bootloader situated in the first 446 bytes of the boot sector.

Does it imply that the first 446 bytes of the boot sector MUST contain some meaningful bootloader code even if the disk is not bootable?

After BIOS transferred control to the bootloader on drive #1 which happened to have no "bootable" partitions - how exactly is the bootloader on the second drive invoked?

Sorry if this is too technical :) Short question is: "How exactly does BIOS skip a drive and proceed to try to boot from the next one?"

Sergey

Posted 2012-05-04T14:29:01.347

Reputation: 293

Answers

16

What exactly makes BIOS decide if a drive is bootable or not?

The BIOS decides if a drive is bootable based on the 16-byte partition record, present after the MBR code area (held in a table starting at the 446th byte). The first byte in each partition record represents the drive's bootable status (and is set to 0x80 if bootable, or 0x00 if not). Some BIOSes may check other parts of the MBR (e.g. partition types, checksums), but the basic requirement is the bootable flag.

How does the boot sequence skip from drive #1 and proceed trying to boot from drive #2 if more than one drive is installed in the system?

This is implementation dependent, and is why you need to properly select a boot order. In most cases, the BIOS will look through each storage medium in the order you set, and determine whether it can boot from that device (via the MBR data). If it can, it does - if not, it continues looping through the other devices (again, in the order you selected).

After BIOS transferred control to the bootloader on drive #1 which happened to have no "bootable" partitions - how exactly is the bootloader on the second drive invoked?

Once a valid boot device is found (i.e. the bootable flag is set, and other additional checks pass), the BIOS copies the MBR sector into RAM. The BIOS then relocates the instruction pointer to the beginning of this location (using a JUMP instruction), where the MBR code segment is located, and the computer then starts.

If the BIOS supports the BIOS Boot Specification, MBR code can return control to BIOS with a certain instruction, signaling it of boot failure and prompting it to try the next device. Older BIOSes just print an error message though. A good tell if you BIOS supports it is whether you can boot from USB.

My understanding is that the only thing BIOS normally checks on an MBR is its signature at the very end of the 512-byte sector, and then it just transfers control to the initial bootloader situated in the first 446 bytes of the boot sector.

This is correct, although it should be noted that most modern BIOSes will also look for a GUID Partition Table as well as the older, conventional MBR-style table.

Does it imply that the first 446 bytes of the boot sector MUST contain some meaningful bootloader code even if the disk is not bootable?

No, but the drive must have a valid MBR or GUID partition table - otherwise, it won't be detected by the computer. While the code part of the MBR can indeed be empty, the first sector of the drive must have a well-formed MBR/GPT.

Breakthrough

Posted 2012-05-04T14:29:01.347

Reputation: 32 927

This answer isn’t entirely correct. FWIU, checking for an active partition and whatnot is something the “default” MBR boot code does, not the BIOS. NeoSmart presents a more plausible description: “On IBM-compatible PCs (basically, everything) the final two bytes of the 512-byte MBR are called the boot signature and are used by the BIOS to determine if the selected boot drive is actually bootable or not.” The last part is just wrong. Legacy BIOS can boot from GPT (with appropriate BIOS boot code), because it’s essentially “free-style” booting.

– Daniel B – 2016-12-16T10:05:38.853

@DanielB both of these things are directly addressed in the answer: "BIOS normally checks on an MBR is its signature at the very end of the 512-byte sector, and then it just transfers control to the initial bootloader situated in the first 446 bytes of the boot sector" and "most modern BIOSes will also look for a GUID Partition Table as well as the older, conventional MBR-style table". – Breakthrough – 2016-12-17T17:38:58.573

Yeah, they are addressed. Paragraphs 1/6 and 6/6 are wrong, though. Again: Legacy BIOS does not care about the partition table at all. It just loads 512 bytes into RAM and executes whatever is there. The whole active partition business is of no concern to the BIOS. – Daniel B – 2016-12-17T17:58:39.063

Wrong and misleading answer, as already pointed out. – Remember Monica – 2019-08-13T05:11:21.070

Thanks for the very detailed answer. So the criteria for a valid bootable device is the presence of a bootable partition in the partition table and some "other additional checks"? I'm just a bit confused because in the original question on AskUbuntu the author shows that all of the partitions on the drive are not bootable and still he sees the error caused by broken GRUB - which shouldn't have been invoked by BIOS at all because the drive does not meet "bootable" criteria, so BIOS should've just skipped the drive and go to the next one – Sergey – 2012-05-05T09:43:01.440

@Sergey I looked at that question, and am unsure. It's possible that the MBR is corrupted, or the bootable flag is set to an invalid value (not sure how fdisk will handle those cases, although the original version did flag invalid MBR headers). I advised the OP to post the raw MBR header, but he/she may also want to "toggle" on/off the bootable flag (to set/reset the flag manually).

– Breakthrough – 2012-05-05T16:40:33.173

Keep in mind that BIOS code is not generic. It is rather specific to each mainboard, so it knows what kind of drives there might be attached to what ports. – LatinSuD – 2014-06-04T11:53:48.300