Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Assembly Instructions Live In the Master Boot Record?

Tags:

x86

mbr

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.

like image 668
iokevins Avatar asked Jan 26 '10 19:01

iokevins


People also ask

What is stored in master boot record?

The MBR contains programs that determine which partition on the hard disk is used for the system boot. Without the MBR, the system is unable to start. The MBR is about 512 bytes. As the first sector of the hard disk, it has a specific address: Cylinder 0, Head 0, Sector 1.

Where does the master boot record reside?

The MBR is stored on the first sector of the hard disk and is created along with the first partition on the drive.

How is a master boot record created?

A master boot record (often shortened as MBR) is a kind of boot sector stored on a hard disk drive or other storage device that contains the necessary computer code to start the boot process. It's created when a hard drive is partitioned, but it's not located within a partition.


2 Answers

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.

like image 71
t0mm13b Avatar answered Oct 21 '22 04:10

t0mm13b


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

like image 41
Leonidas Avatar answered Oct 21 '22 06:10

Leonidas