After being chainloaded by GRUB, where is my partition?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

After being chainloaded by GRUB, where is my partition?

Post by TylerH »

After I get chainloaded by Grub, how can I tell where the second of sector in my partition is? Or more generically, where my partition I was loaded from is?

Thanks.
User avatar
JackScott
Member
Member
Posts: 1032
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Mastodon: https://aus.social/@jackscottau
GitHub: https://github.com/JackScottAU
Contact:

Re: After being chainloaded by GRUB, where is my partition?

Post by JackScott »

If I understand the question correctly, you want to know how your kernel can tell what boot device GRUB loaded your kernel from.

The Multiboot Specification reserves a place in the boot information structure for that information. Read this. What I think you're looking for is offset 12 in the structure.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: After being chainloaded by GRUB, where is my partition?

Post by Love4Boobies »

Also, using Multiboot 2 (WIP but usable), that would be tag type 5.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: After being chainloaded by GRUB, where is my partition?

Post by TylerH »

Multiboot Specification wrote: If the OS image was not loaded from a bios disk
Does this mean for chainloaded bootloaders too? I don't want to load my kernel image with Grub, just my bootloader.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: After being chainloaded by GRUB, where is my partition?

Post by Combuster »

A bootloader receives the drive number in DL, you can then get from that drive the relevant information. Or you can patch your bootloader binary to hold the location of the partition you want to load from (both have advantages and disadvantages).
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: After being chainloaded by GRUB, where is my partition?

Post by TylerH »

Yeah I was thinking about hard coding the location into the boot sector, thought it was a little crude, but if you consider it a possibility I guess hard coding is okay. Thanks.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: After being chainloaded by GRUB, where is my partition?

Post by Brendan »

Hi,
Combuster wrote:A bootloader receives the drive number in DL, you can then get from that drive the relevant information.
The MBR code is also meant to leave DS:SI pointing to the boot loader's partition table entry; so that the boot loader can find out which partition (on which device) it came from.

This gives 3 possibilities:
a) Use DL and DS:SI to find out where you came from, and hope that the MBR/boot manager did the right thing. This is what Microsoft do.
b) Use DL to find out which device, then load the partition table yourself and search for your partition. This would work, even if the MBR/boot manager is dodgy and doesn't set DS:SI; but fails in most "multiple OSs" scenarios (e.g. where there's some sort of boot manager that creates a dummy/virtual partition table to get around the "only one partition table entry can be active" restriction).
c) Hard-code it when the OS is installed. This makes the boot loader code easier, but makes it a little harder to install the OS and makes it very hard to relocate the partition with generic tools.

In all of these cases, you'd need to supply your own MBR/boot manager (in case you're installing the OS on a blank device that doesn't already have an MBR, which includes most USB flash devices). Your own MBR could be capable of more than just booting one active partition though.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: After being chainloaded by GRUB, where is my partition?

Post by TylerH »

Interesting, but one question, if the partition pointed to is an extended partition, how can I know how many EBRs to skip?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: After being chainloaded by GRUB, where is my partition?

Post by Brendan »

Hi,
TylerAnon wrote:Interesting, but one question, if the partition pointed to is an extended partition, how can I know how many EBRs to skip?
DS:SI should point to a structure that contains the starting and ending sector for your partition. It doesn't matter if it's a primary partition or an extended partition, or even if it's a virtual partition (that was artificially constructed by some sort of boot manager) that isn't mentioned (or isn't the same) in any partition table.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: After being chainloaded by GRUB, where is my partition?

Post by TylerH »

Oh, okay. Thanks.
Post Reply