Search code examples
x86bootloaderosdevgrubgrub2

Where in memory will os boot code be loaded when using grub chainloader?


Problem Description

Hi. I want to use grub chainloader to load my minios (an os I developed myself, not linux or windows). Before using grub chainloader, minios boot process was BIOS->MBR->os boot(stored in sector 0 of the primary partition)->loader.bin->os kernel.elf . I think after using grub chainloader, the boot process should be BIOS->grub->os boot->loader.bin->os kernel.elf. I know that after executing

set root=(hd0,1)
chainloader +1 
boot

os boot (stored in sector 0 of the primary partition) will be loaded into memory and start executing it. I don't know where the os boot will be loaded in memory?

What I want to know

Please tell me where in memory grub chainloader will load os boot. So I can modify the pseudo instruction org 0x???? in my os boot code (assembly code) to ensure the normal execution of os boot.

And I also wonder if there is a way for grub to pass the start sector information of the primary partition to os boot?

Thank you very much.


Solution

  • GRUB will chainload the Volume Boot Record (VBR) of the partition you requested. The VBR will be placed into memory by GRUB at physical address 0x7c00. Prior to transferring control to the VBR it will set DS:SI to point at a 16-byte Partition Table Entry for the partition you requested to boot. Using the partition table entry at DS:SI you can determine the starting sector and the number of sectors in the partition.


    Notes

    • Chainloading with legacy BIOS has been done for decades. The DS:SI convention of pointing to the partition table entry is not unique to GRUB but is also what the DOS/Windows Master Boot Record (MBR) would do to chainload to the VBR of the active partition specified in the MBR's partition table.

    • Legacy BIOS chainloading will also ensure that the boot drive number is passed to the VBR in register DL. This is the same convention the BIOS uses to pass the boot drive number to the boot sectors it loads.