XPE tips

External debugging and compatibility

  • Categories

  • Archives

Fixing boot sector and MBR

Posted by himselfv on September 20, 2009

Two tools you’ll ever need to make your disk bootable: mbrfix and bootsect.

MbrFix allows you to read and write the Master Boot Record of a hard disk drive (or anything that resembles one). The thing to know about MBR is that it consists of static and dynamic data. Static data is something you can take from one hard drive in one computer and write to another hard drive in another computer, and it’ll still work. The MBR boot code is like that. As long as it’s used in x86-compatible systems, it’ll work. (Don’t nitpick). Dynamic data, on the other hand, is specific to a hard drive on which it’s written. It includes drive signature and partition table, which stores among other things offsets and lengths of drive partitions.

Obviously, blindly writing the whole MBR onto the hard drive would result in a rewritten partition table, and, consequently, all partitions lost. (Probably). MbrFix does this the clever way, preserving dynamic and reading/writing only static data. In addition to reading/writing the data you feed to it, mbrfix includes standard MBRs for NT/XP, Vista and Windows 7 in it, so you can do “/fixmbr” with it just like you would from recovery console of any of those systems. It also allows you to get/set active partition and the aforementioned drive signature (how about 0xBADC0FEE). So it contains basically anything you might ever need from MBR editing tool. Aside from editing the partition table manually. And from hex editing the drive. No, wait, you can save MBR into file, edit it and write it back.

Download MbrFix here, in case you couldn’t figure that out from the link above.

MbrFix writes bootsectors too, but it’s not good at that so we’ll use another tool. This time it’s bootsect.exe from Microsoft. It works on both FAT and NTFS partitions and contains both pre-Vista (ntldr) and post-Vista code. The only thing it lacks is Win9x code (the one for io.sys, msdos.sys).

Naturally, boot code alone is not enough to actually boot. You’ll need to place the loader itself on the partition in order for it to be bootable. For pre-Vista systems such as XP Embedded it’s NTLDR, which consists of ntldr.exe, ntdetect.com and boot.ini. If you’re building XPE, these will be included into your image automatically; else take them from any XP distro you have around, they’re all (almost) the same.

You can get bootsect.exe from several sources, including Windows Automated Installation Kit and Vista installation DVD (it should be in \boot\Bootsect.exe according to this article). Or you can google for it and dowload it from somewhere else; don’t worry, it doesn’t require Vista and runs on older NT systems just fine.

Leave a comment