2

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.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
DanielSun
  • 33
  • 4
  • If this is legacy BIOS It should be chainloaded into memory at 0x7c00. By convention chainloaders set DS:SI to point to the partition table entry for the active partition that was loaded before passing control to the chainloaded volume boot record (VBR) that it loaded at physical address 0x07c00. The 16-byte partition table entry DS:SI will point at is defined here: https://en.wikipedia.org/wiki/Master_boot_record#PTE – Michael Petch Mar 10 '23 at 13:29
  • @Michael Petch Thanks. What you said is correct!!! I found the same answer from the [grub2 chainloader source code](https://github.com/rhboot/grub2/blob/de07df28d0cc3f506ac56b35803d283b1b4484d7/grub-core/loader/i386/pc/chainloader.c#L70) . – DanielSun Mar 10 '23 at 15:01
  • There have been some close votes. I consider this a programming question as you need to know where in memory the boot sector will be loaded by GRUB when chainloading (to properly set an origin point for the code you generate) and how to programmatically gain access to the information about the partition like its starting block address, its size etc. – Michael Petch Mar 10 '23 at 17:44

1 Answers1

2

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.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198