Search This Blog

Tuesday, September 15, 2009

Boot Loader

In the simplest of terms, a boot loader is something which loads the OS.

It basically performs the following tasks :

- decide what to load
- load the kernel and additional data such as initrd or parameters for the kernel
- set up an execution environment suitable for the kernel
- run the kernel

Types of boot loaders :

There are different types of boot-loaders depending upon how they interact with the underlying system.

a) Specialized loaders

They are typically aware of only one storage device. eg : flash memory or floppy disk on which a small number of kernels is stored in some format specific to the boot-loader.
eg: SYSLINUX, LinuxBIOS

b) General loaders running under other operating system

They normally use the services provided by the host operating system for reading the kernel image and additional data. Disadvantages of this approach are (i) slow loading and (ii) problem of overwriting the host OS while loading the other OS.
eg: LOADLIN

c) File-system aware general loaders running on firmware

They are little operating systems by themselves. They know the structure of one or more filesystems. They access the devices via the services provided by the firmware and sometimes they may have their own drivers to access hardware directly.
eg: GRUB


d) File-system unaware general loaders running on firmware

They rely on third-party to map the on-disk data structures to a more general and convenient representation.
eg:LILO


Lets have a look at the GRUB MBR :)

MBR is a 512-byte sector located in the first sector of the disk i.e. sector 1 of cylinder 0, head 0.
GRUB usually replaces the MBR when linux is installed. But it maintains the same structure as the MBR.
i.e. 446 bytes - Executable code and error messages
64 bytes - Partition table
2 bytes - Magic number (0xAA55)

To take a hexdump of the GRUB MBR code ,
# dd if=/dev/sda of=mbr.bin bs=512 count=1

# hexdump -Cv mbr.bin | less

00000000 eb 48 90 00 00 00 00 00 00 00 00 00 00 00 00 00 |ëH..............|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 02 |................|
00000040 ff 00 00 20 01 00 00 00 00

The first three bytes specify a Jmp instruction. so the execution jumps over 71 bytes which is also called the BIOS Parameter Block(BPB) and is marked in blue.




00000160 00 eb 0e be 84 7d e8 38 00 eb 06 be 8e 7d e8 30 |.ë.¾.}è8.ë.¾.}è0|
00000170 00 be 93 7d e8 2a 00 eb fe 47 52 55 42 20 00 47 |.¾.}è*.ëþGRUB .G|
00000180 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 65 |eom.Hard Disk.Re|
00000190 61 64 00 20 45 72 72 6f 72 00 bb 01 00 b4 0e cd |ad. Error.»..´.Í|
000001a0 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 00 00 00 |.¬<.uôÃ.........|

At offset 179h , we find the zero-terminated string GRUB followed by some error messages.



000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 |................|
000001c0 01 00 83 fe ff ff 3f 00 00 00 5b 01 5c 08 00 fe |...þÿÿ?...[.\..þ|
000001d0 ff ff 82 fe ff ff 9a 01 5c 08 42 8e 2f 00 00 00 |ÿÿ.þÿÿ..\.B./...|
000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............Uª|

And finally the MBR ends with the magic number AA55h. (seen in reverse because of little-endianness ;))




Reference :
1) Almesberger, Werner; "Booting Linux: The History and the Future"
http://www.almesberger.net/cv/papers/ols2k-9.ps.gz

2) http://mirror.href.com/thestarman/asm/mbr/GRUB.htm


0 comments:

Post a Comment