Master Boot Record - Programming Considerations

Programming Considerations

The MBR originated in the PC XT. IBM PC-compatible computers are little-endian, which means numeric values spanning two or more bytes are stored by the processor in reverse order in memory. The format of the MBR on media reflects this convention. Thus, the MBR signature will appear in a disk editor as the sequence 55h AAh.

The bootstrap sequence in the BIOS will load the first valid MBR that it finds into the computer's physical memory at address 0000h:7C00h. The last instruction executed in the BIOS code will be a "jump" to that address, to direct execution to the beginning of the MBR copy. The primary validation for most BIOSes is the signature at offset +1FEh, although a BIOS implementor may choose to include other checks, such as verifying that the MBR contains a valid partition table without entries referring to sectors beyond the reported capacity of the disk.

While the MBR boot sector code expects to be loaded at physical address 0000h:7C00h, all the memory from physical address 0000h:0501h (address 0000h:0500h is the last one used by a Phoenix BIOS) to 0000h:7FFFh (and sometimes up to 0000h:FFFFh) is available in real mode . The INT 12h BIOS interrupt call may help in determining how much memory can be allocated safely (it simply reads the base memory size in KiB from segment:offset location 0040h:0013h).

The last 72 bytes of the 512 byte MBR are reserved for the partition table and other information, so the MBR boot sector program must be small enough to fit within 440 bytes of memory. The MBR code may communicate with the user, examine the partition table. Eventually, the MBR will need to perform its main task, and load the program that will perform the next stage of the boot process, usually by making use of INT 13h BIOS calls.

As stated above, the conventional MBR bootstrap code loads and runs (boot loader or operating system dependent) Volume Boot Record code that is located at the beginning of the "active" partition. A conventional Volume Boot Record will fit within a 512-byte sector, but it is safe for MBR code to load additional sectors to accommodate boot loaders longer than one sector, provided they do not make any assumptions on what the sector size is. Like the MBR, a Volume Boot Record normally expects to be loaded at address 0000h:7C00h. Since this is the same location where the MBR is loaded, one of the first tasks of a MBR is to relocate itself somewhere else in memory —most often at 0000h:0600h (for MS-DOS/PC DOS, OS/2 and Windows MBR code) or 0060h:0000h (most DR-DOS MBRs). Even though this resolves to the same memory address in real mode, for Apple Darwin to boot, the MBR must be relocated to 0000h:0600h instead of 0060h:0000h, since the code depends on the DS:SI pointer to the partition entry provided by the MBR, but erroneously refers to it via 0000h:SI only. While the MBR code relocates itself it is still important not to relocate to other addresses in memory because many VBRs will assume a certain standard memory layout when loading their boot file.

The Status field in a partition table record is used to indicate an active partition. Standard-conformant MBRs will allow only one partition marked active and use this as part of a sanity-check to determine the existence of a valid partition table. They will display an error message, if more than one partition has been marked active. Some non-standard MBRs will not treat this as an error condition and just use the first marked partition in the row.

Traditionally, values other than 00h (not active) and 80h (active) were invalid and the bootstrap program would display an error message upon encountering them. However, the Plug and Play BIOS Specification and BIOS Boot Specification (BBS) allowed other devices to become bootable as well since 1994. Consequently, with the introduction of MS-DOS 7.10 (Windows 95B) and higher, the MBR started to treat a set bit 7 as active flag and showed an error message for values 01h..7Fh only. It continued to treat the entry as physical drive unit to be used when loading the corresponding partition's VBR later on, thereby now also accepting other boot drives than 80h as valid, however, MS-DOS did not make use of this extension by itself. Storing the actual physical drive number in the partition table does not normally cause backward compatibility problems, since the value will differ from 80h only on drives other than the first one (which have not been bootable before, anyway). However, even with systems enabled to boot off other drives, the extension may still not work universally, for example, after the BIOS assignment of physical drives has changed, f.e. when drives are removed, added or swapped. Therefore, per the BIOS Boot Specification (BBS), it is best practice for a modern MBR accepting bit 7 as active flag to pass on the DL value originally provided by the BIOS instead of using the entry in the partition table.

Read more about this topic:  Master Boot Record

Famous quotes containing the word programming:

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)