Get Boot Device in UEFI

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.
User avatar
robertapengelly
Posts: 14
Joined: Mon Jun 24, 2024 6:29 am
Contact:

Get Boot Device in UEFI

Post by robertapengelly »

Hey guys,

Does anyone know if it's possible to get the boot drive number in UEFI? I've tried searching the web but all I can find is stuff related to BIOS (i.e. BIOS passes the drive no. in dl). I've also had a look at the UEFI specifications and I can't find anything related to boot drive number (maybe I'm looking at the wrong structures).

Any help as to whether or not it's possible would be great.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Get Boot Device in UEFI

Post by iansjack »

What exactly do you mean by “boot device”?

The device containing the UEFI partition?
The device containing the bootloader?
The device containing the kernel?

I guess the question is, why do you need this information?
User avatar
robertapengelly
Posts: 14
Joined: Mon Jun 24, 2024 6:29 am
Contact:

Re: Get Boot Device in UEFI

Post by robertapengelly »

I mean the device with the partition that is marked bootable. In BIOS the booted device is always 0x80 (as far as I know) and then other disks are incremented (i.e. 0x81, 0x82, ...).

As for why, the reason is cause when you exit UEFI then you'd need to know what disk was booted to say make that disk the root device (either C: if your DOS/Windows like or / if your unix like).

I hope that makes sense.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Get Boot Device in UEFI

Post by iansjack »

More than one partition can be marked as bootable. For example, my main Windows computer has four disks, one of which contains a Linux installation. So two of the disks are bootable.

The bootloader should have the information about the root device (which need not be the one the system was booted from or the one containing the kernel).
User avatar
robertapengelly
Posts: 14
Joined: Mon Jun 24, 2024 6:29 am
Contact:

Re: Get Boot Device in UEFI

Post by robertapengelly »

iansjack wrote: Mon Dec 02, 2024 10:21 am More than one partition can be marked as bootable. For example, my main Windows computer has four disks, one of which contains a Linux installation. So two of the disks are bootable.
I thought only one would be bootable (e.g. partition 1) and then that would do the rest (e.g. if it's grub it'll do the grub boot stuff).
iansjack wrote: Mon Dec 02, 2024 10:21 am The bootloader should have the information about the root device (which need not be the one the system was booted from or the one containing the kernel).
Good point, but how would you tell which partition/disk is the correct one. With BIOS you can check the boot drive number to see if it's the same as the one you expect (e.g. first disk will be 0x80, second would be 0x81, ...). Also with BIOS you can choose which device is booted. Is there something similar in UEFI?

Sorry for all these questions, just I'm still a bit new to UEFI.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Get Boot Device in UEFI

Post by iansjack »

UEFI disks use GUID partition tables. The GUID is a large number that uniquely identifies the disk and is stored in the GPT header. Each partition on the disk also has a unique GUID. This means that disks can be freely moved between ports and still be correctly identified.

If you look at a grub configuration file for a UEFI setup you will see that it uses the GUID to specify the root partition.
User avatar
robertapengelly
Posts: 14
Joined: Mon Jun 24, 2024 6:29 am
Contact:

Re: Get Boot Device in UEFI

Post by robertapengelly »

iansjack wrote: Mon Dec 02, 2024 11:43 am UEFI disks use GUID partition tables
Not always as you can boot UEFI from a MBR disk. Also the partitions can be FAT 12/16/32. There's a lot of misinformation about UEFI.
Octocontrabass
Member
Member
Posts: 5571
Joined: Mon Mar 25, 2013 7:01 pm

Re: Get Boot Device in UEFI

Post by Octocontrabass »

robertapengelly wrote: Mon Dec 02, 2024 10:45 amI thought only one would be bootable (e.g. partition 1) and then that would do the rest (e.g. if it's grub it'll do the grub boot stuff).
At least on paper, UEFI allows you to have multiple boot devices and choose between them. You can even have multiple bootloaders coexist within a single EFI system partition, as long as each one installs its own boot option.

How well this works tends to vary between UEFI implementations (and operating systems).
robertapengelly wrote: Mon Dec 02, 2024 10:45 amGood point, but how would you tell which partition/disk is the correct one.
The firmware will attach several protocols to your bootloader's image handle, including a device path protocol that tells you exactly where it found your bootloader. Usually you'll want your bootloader to load a configuration file stored alongside it that tells it where to look to continue booting your OS, but you can also try to guess by examining the disk you booted from (and do other things if you didn't boot from a disk at all).

Your bootloader doesn't have to use the device path protocol directly; there should be some I/O protocols available that already point to the correct partition.
robertapengelly wrote: Mon Dec 02, 2024 10:45 amWith BIOS you can check the boot drive number to see if it's the same as the one you expect (e.g. first disk will be 0x80, second would be 0x81, ...).
BIOS drive numbers aren't reliable. They tend to change when you boot from different disks or have different drives (including USB drives) plugged in. There's a BIOS call (INT 0x13 AH=0x48) to translate BIOS drive numbers into useful hardware identifiers. UEFI uses the hardware identifier directly in the form of device path protocols.
robertapengelly wrote: Mon Dec 02, 2024 10:45 amAlso with BIOS you can choose which device is booted. Is there something similar in UEFI?
UEFI (again, on paper) has a boot menu that lets you choose what to boot. In addition to boot options that point to specific bootloaders, there are also generic device boot options that will attempt to do things like find a default bootloader on a disk or PXE boot over the network.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Get Boot Device in UEFI

Post by iansjack »

you can boot UEFI from a MBR disk.
That’s not correct. UEFI requires a GUID partitioned disk. However, some UEFI implementations include a compatibility module (essentially a BIOS), that lets them boot a non-UEFI operating system from an MBR disk.

The file system is a completely separate matter from the partition scheme. The UEFI system partition must use the FAT file system, the other partitions can be any file system.
User avatar
robertapengelly
Posts: 14
Joined: Mon Jun 24, 2024 6:29 am
Contact:

Re: Get Boot Device in UEFI

Post by robertapengelly »

iansjack wrote: Mon Dec 02, 2024 12:06 pm That’s not correct. UEFI requires a GUID partitioned disk. However, some UEFI implementations include a compatibility module (essentially a BIOS), that lets them boot a non-UEFI operating system from an MBR disk.
That's wrong provided I've read it right. I have a MBR partitions scheme disk and UEFI gets called not BIOS. That's half the reason for my questions as I'm trying to figure out how it all works. My file gets booted and then I exit to the UEFI shell, I just don't exactly know how to go further yet without needing to use UEFI stuff.
User avatar
robertapengelly
Posts: 14
Joined: Mon Jun 24, 2024 6:29 am
Contact:

Re: Get Boot Device in UEFI

Post by robertapengelly »

Octocontrabass wrote: Mon Dec 02, 2024 11:58 am The firmware will attach several protocols to your bootloader's image handle, including a device path protocol that tells you exactly where it found your bootloader. Usually you'll want your bootloader to load a configuration file stored alongside it that tells it where to look to continue booting your OS, but you can also try to guess by examining the disk you booted from (and do other things if you didn't boot from a disk at all).

Your bootloader doesn't have to use the device path protocol directly; there should be some I/O protocols available that already point to the correct partition.
Am I right in thinking your talking about the interaction with the UEFI stuff? If I am how would I do it without UEFI? Lets say I use IO ports (i.e. 0x1F0) to get the disk information, how would I know if it's the booted device?
Octocontrabass
Member
Member
Posts: 5571
Joined: Mon Mar 25, 2013 7:01 pm

Re: Get Boot Device in UEFI

Post by Octocontrabass »

iansjack wrote: Mon Dec 02, 2024 12:06 pmUEFI requires a GUID partitioned disk.
Several UEFI implementations require GPT on internal drives, but I don't see anything in the UEFI specification that requires it. I'm not sure if I'm just interpreting it wrong (the UEFI spec isn't very clear in some places) or if it's a common firmware bug that just happens to not affect Windows.

On removable drives, MBR is supported; the Windows installer relies on it.
robertapengelly wrote: Mon Dec 02, 2024 12:25 pmAm I right in thinking your talking about the interaction with the UEFI stuff?
Yes. UEFI boot services include standard APIs for accessing disks. You should use them. Don't waste your time writing a bunch of drivers for your bootloader when you're just going to have to write the same drivers again for your kernel. (The same applies for BIOS and INT 0x13.)
robertapengelly wrote: Mon Dec 02, 2024 12:25 pmIf I am how would I do it without UEFI? Lets say I use IO ports (i.e. 0x1F0) to get the disk information, how would I know if it's the booted device?
That's what the device path protocol is for. Your bootloader can pass a copy to your kernel, and then your kernel will know exactly where the boot disk is connected. For example, a device path protocol that starts with "ACPI(PNP0A03)/PCI(0x1F,0x1)/ATA(0,0)" means PCI bus, device 0x1F function 0x1, primary IDE channel, primary drive. (The rest of the device path protocol will tell you the partition and file name, but I don't have a good example of that handy.)
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Get Boot Device in UEFI

Post by iansjack »

Several UEFI implementations require GPT on internal drives, but I don't see anything in the UEFI specification that requires it.
Fair enough. My error.
User avatar
robertapengelly
Posts: 14
Joined: Mon Jun 24, 2024 6:29 am
Contact:

Re: Get Boot Device in UEFI

Post by robertapengelly »

I have [PCIRoot(0x0)/PCI(0x7,0x1)/Ata(Primary,Master,0x0)/HD(1,MBR,0x6EF86D7A,011,0x1043F)] so am I right in guessing that it's PCI bus, device 0x07, function 0x01, secondary IDE channel, primary drive? Still a little confused as to what the rest of it means though.
AnotherIdiot
Posts: 4
Joined: Sat Sep 28, 2024 8:00 pm
GitHub: https://github.com/ThatOSDeveloper

Re: Get Boot Device in UEFI

Post by AnotherIdiot »

iansjack wrote: Mon Dec 02, 2024 12:06 pm That’s not correct. UEFI requires a GUID partitioned disk. However, some UEFI implementations include a compatibility module (essentially a BIOS), that lets them boot a non-UEFI operating system from an MBR disk.
This is a false statement, some UEFI implementations allow you to boot from a MBR type of disk as long as it has a FAT32 partition marked as bootable and with an EFI folder.
Post Reply