tags:

views:

197

answers:

4

Curious about what explicit assembly instructions actually make up the Master Boot Record on an X86 architecture. Thanks for any insights.

Other architectures welcome, but this is primarily for X86.

+4  A: 

Look here for a dissected MBR-bootloader from stage1 in grub1: http://mirror.href.com/thestarman/asm/mbr/GRUB.htm

Leonidas
Thanks Leonidas! :)
schultkl
+7  A: 

A Master Boot Record is made up of 512 bytes, the last two bytes must be 0x55 0xAA. That's 510 bytes left, the partition table entries is 16 bytes, at the most 4 partition table entries, which is 64 bytes. Here is what a partition entry looks like.

What's left is 446 bytes of assembler code. Usually the first few bytes consists of a boot identifier record, describing the boot loader's disk data, such as the identifier, system id, to name but a few, then the BIOS expects the boot code at 0x000:0x07C00, then it relocates itself lower down in the memory segment. See here for an example tutorial on boot loaders. There is a detailed technical overview on WikiBooks about it here.

The only thing you have to be careful of, is the bootloader code must not exceed 446 bytes otherwise the partition tables gets screwed and hence the BIOS error message 'Error. Missing operating system' or similar!

Writing the boot sector compiled binary to the disk would involve a messy and dangerous way of screwing the computer usually, under Linux the command would be similar to:

# Assume that /dev/hda1 is the first hard disk then...
dd if=mybootldr.bin of=/dev/hda1 bs=512 cnt=1

The other way involves using a low-level disk editor program or even using the plain old DEBUG.EXE (found on old MSDOS diskettes) or even using FreeDos.

C:\DEBUG.EXE mybootldr.bin 
-W 100 0 0 1 
-Q

If I recall, DR.DOS (Digital Research DOS) a rival to MS-DOS, used a password protection on the boot loader code prior to DR-DOS booting up.

Edit: If you are really curious, as to how the BIOS looks for 0x0000:0x7C00, have a look at this link in which you can download the original IBM XT's BIOS code here.

Hope this helps, Best regards, Tom.

tommieb75
+1 for "must be 0x55 0xAA" and not saying magic number
stacker
@Stacker: I edited the answer to include a link to the original BIOS code...
tommieb75
@tommieb don't be stingy ^
stacker
@Stacker: what do you mean...I'm upvoting your answer ;) if that's what you mean.... :)
tommieb75
Wow, I found the right word ;-) thx tommieb
stacker
This is a fantastic write-up. Thanks Tommieb75! :)
schultkl
Thanks Schultkl :)
tommieb75
+3  A: 

Love these old school topics ;-)

here are two assembler listings because you ask for explicit assembly instructions

stacker
Those old school topics are never boring! ;)
tommieb75
You're welcome! :) ;) Keep those old school topics alive eh!
tommieb75
+1  A: 

Here is a playable Tetris game written in assembly, and which fits into an x86 boot sector. I saw a variant of this which would boot your regular OS only if you could complete ten lines.

Thomas Pornin
That is awesome. :D
schultkl