Saturday, June 12, 2010

[Custom MBR] Ignore the "Active" flag, bootstrap a designated "boot" partition


This is a versatile MBR boot code that I wrote to facilitate the use of grub4dos as a primary boot manager. In addition to the standard DOS/Windows MBR functionality (bootstrap the active primary partition), it can be configured to always bootstrap a designated "boot" partition (regardless of the status of the "Active" flag in the partition table). This enables a flexible grub4dos setup as primary boot manager, which:

  • Doesn't rely on the status of "Active" flag and supports booting from logical partitions (just like grub stage1 loader)

  • Doesn't write to the unused sectors of master boot track (LBA 0x01-0x3E, where grub usually embeds its stage1.5 code)

  • Doesn't use a block-list to load boot components (safe from defragmentation of the filesystem)

  • Supports NTFS/FAT/EXT3 (primary or logical) boot partitions (therefore, any existing NTFS/FAT data partition can be configured to serve as a boot partition)

[An alternate approach would be to install legacy grub as you'd normally do and use it to chainload grldr (grub4dos). However, aside from being slow and incompatible with NTFS, this method requires either writing intermediate stage1.5 boot code to the master boot track or loading stage2 as block-list, which is prone to corruption by defragmentation.]


Download mbr.bin from here. Make sure you backup your data and verify the md5 checksum before proceeding. From Linux, install the custom MBR using the "dd" command:

$ dd if=mbr.bin of=/dev/sda

where sda is the device (hard drive) where you want to install this MBR


In its default configuration, the MBR boot code will chainload the volume boot sector the active primary partition just like traditional DOS/Windows MBR. However, this behavior can be overridden to always bootstrap a certain partition, by patching the control byte at offset 0x0195 (406th byte starting from 1, highlighted in red below) with the partition number of the that partition (1-4 for primary/extended partitions, 5 onwards for logical partitions). When this byte is non-zero, the MBR boot code will ignore the "Active" flag in the partition table and chainload the volume boot sector of the partition corresponding to this number.

477da2d302fe8cc16f05e3c5dba08d0c mbr.bin

0000000: fa31 c08e d88e d0bc 007c 89e6 0657 528e
0000010: c0fb fcbf 0006 b900 01f3 a5ea 2006 0000
0000020: 6631 c066 99bb be07 8a2e 9507 20ed 7555
0000030: b904 00f6 0780 7405 4266 8b47 0883 c310
0000040: e2f1 4a0f 84ee 0079 1ce8 3301 426f 6f74
0000050: 2070 6172 7469 7469 6f6e 206d 6973 7369
0000060: 6e67 2e0d 0ae8 1701 4d75 6c74 6970 6c65
0000070: 2061 6374 6976 6520 7061 7274 6974 696f
0000080: 6e73 2e0d 0a30 c9fe c18a 4704 38cd 741c
0000090: 3c05 740a 3c0f 7406 3c85 7402 eb04 668b
00000a0: 5708 80f9 0474 0f83 c310 ebdb 20c0 7499
00000b0: 668b 4708 eb7f 6621 d274 8e51 6689 d0e8
00000c0: 6200 5966 50bb be07 fec1 8a47 0438 cd74
00000d0: 2383 c310 8a47 043c 0574 0b3c 0f74 073c
00000e0: 8574 03e9 63ff 5166 8b47 0866 01d0 e833
00000f0: 0059 ebcf 20c0 0f84 4fff 6658 6603 4708
0000100: eb33 6660 6631 d2bb 007c 6652 6650 0653
0000110: 6a01 6a10 89e6 b442 8a16 fa7b cd13 8d64
0000120: 1066 61c3 e8db ff72 40be be7d bfbe 07b9
0000130: 2000 f3a5 c3e8 caff 722f 813e fe7d 55aa
0000140: 7509 bcfa 7b5a 5f07 faff e4e8 3100 4d69
0000150: 7373 696e 6720 4f70 6572 6174 696e 6720
0000160: 5379 7374 656d 2e0d 0ae8 1300 4572 726f
0000170: 7220 6c6f 6164 696e 6720 4f53 2e0d 0a5e
0000180: acb4 0e8a 3e62 04b3 07cd 103c 0a75 f1cd
0000190: 18f4 ebfd 0000 0000 0000 0000 0000 0000
00001a0: 0000 0000 0000 0000 0000 0000 0000 0000
00001b0: 0000 0000 0000 0000

The volume boot sector of the boot partition can be configured to load grub4dos (/grldr) either by using (bundled with grub4dos distribution) or, in case of ntfs/fat partition formatted by Windows, simply by renaming /grldr as /ntldr (for XP formatted partition) or /bootmgr (for Vista/7 formatted partition).

Note: To use the second method with logical partitions, you would additionally need to correct the HiddenSector parameter in the ntfs/fat volume boot sector to make the partition bootable, as described here.