Hi,
requimrar wrote:My question differs from the OPs somewhat: I don't have %dl and I'm loaded by GRUB. I have no interest in BIOS's 'drive number', since I'm exclusively using ATA to read/write the disk.
So what I'm looking for is some way to get the ATA drive (ie. primary/secondary and master/slave) that I booted from... I think Windows does this thing where it assumes the first active partition, but I don't want to do that (for obvious reasons)
If you do have the drive number from BIOS, then you can use the
get drive parameters BIOS function to determine which disk on which controller it is.
Now, if you have a look at the
"Boot Information Format" section in the multi-boot spec; you'll see there's "drives_length" and "drives_addr" fields (at offset 52 and 56, if flags[7] is set). You'll also notice that this is optional, does tell you drive numbers for potentially many disks, doesn't tell you which of those disks was the boot device, and doesn't tell you which disk on which controller any of them are. You might feel like assuming you booted from BIOS device 0x80, but of course that assumption can easily be wrong; and you might also feel like assuming that the first disk on the primary controller is device 0x80 (and that assumption can easily be wrong too).
Now imagine that when you install your OS you add a special marker on that drive/partition to say that this is where you booted from; and you write some code to search all the drives/partitions for that special marker. Further, let's assume that the user creates a backup of the drive/partition (e.g. copies the data "as is" to a different drive), or installs a second version of your OS, and now there's 2 drives/partitions with your special marker. This doesn't work reliably either.
Another option is to find and parse GRUB's configuration, and attempt to use that to figure out which drive GRUB was told your OS was booted from. This can't work reliably either (e.g. imagine 2 versions of your OS installed), would be very messy, and means that your OS can't boot from any other boot loader that support the multiboot specification.
Finally, the last option is to give up and use a kernel command line; where the boot loader passes some sort of "bootdev=??" string to your kernel. This mostly means end-user hassles, and hoping that the end-user didn't forget and didn't screw it up (but we all know the end user will forget and/or will screw it up, so...
).
Basically, the only way you can do it reliably is to get the information from the BIOS. Multiboot doesn't give you the information itself, and multiboot also doesn't give you the drive number (so you can't switch back to real mode and ask the BIOS yourself). The only real alternatives are to stop using multiboot (write your own boot loader so that it's easy to get the information you want), or to use a combination of several different unreliable techniques in the hope of ending up with something more reliable than any one of those techniques alone.
For example, you could parse GRUB's configuration to attempt to figure out which device number your OS was booted from, and have a special marker on the drive/partition, and have a command line option; and then assume that if 2 or more of the 3 different techniques give you the same answer then that answer is more likely to be the right answer.
Of course writing your own boot loader is likely to be easier than using multiboot...
Cheers,
Brendan