How does the mbr transfer its control to the bootloader

What i know is the following:

Upon start-up, the BIOS goes through the following sequence:

1.Power-on self-test (POST)
2.Detect the video card’s (chip’s) BIOS and execute its code to initialize the video hardware.
3.Detect any other device BIOSes and invoke their initialize functions
4.Display the BIOS start-up screen
5.Perform a brief memory test (identify how much memory is in the system)
6.Set memory and drive parameters
7.Configure Plug & Play devices (traditionally PCI bus devices)
8.Assign resources (DMA channels & IRQs)
9.Identify the boot device

When the BIOS identifies the boot device (typically one of several disks that has been tagged as the bootable disk), it reads block 0 from that device into memory location 0x7c00 and jumps there.

But what happens after this i.e how does the BIOS give the control to the bootloader at this stage?


Solution 1:

As mentioned in the post above, the BIOS simply gives control to the bootloader by executing a jump instruction to 0x7C00.

A non bootable device may have the INT 18 instruction (0xCD 0x18) in its first two bytes (and the 55aa at bytes 511, 512), which would then simply cause the BIOS to get to the next device in the boot order.

Solution 2:

Since you mentioned 0x7C00, I'll focus on MBR booting (GPT is a different deal).

As you said, once BIOS determines that a device is bootable (i.e. has 0x55AA as the last two bytes of the first sector), it reads that first sector from the disk into and loads it into memory starting at 0x7C00. Once it issues the jump instruction, the CPU starts running whatever code is there.

It's then up to the bootloader to figure out what to load and execute next. The code that does the loading and jumping to the next stage has to fit in the 510 bytes remaining in that first sector, less actually, since hard drives also have the partition table in there.

Further reading: Boot Sequence, Rolling Your Own Bootloader