How to get the boot device on 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.
Post Reply
ilmmatias
Member
Member
Posts: 35
Joined: Fri Jun 02, 2017 3:01 pm
Contact:

How to get the boot device on UEFI?

Post by ilmmatias »

Hello everyone, until now, I had been hardcoding the boot device to be the first CD/DVD (when no boot device is specified on the configuration file), but now I want to get the boot device in the right way, for CD/DVD and USB boot.
How exactly should I do this?
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

Re: How to get the boot device on UEFI?

Post by zhiayang »

if i am not mistaken, you can get the DeviceHandle from your LoadedImageProtocol; then, the volume that you are booted from should have that handle as well.

you can get handles using bs->LocateHandleBuffer(...).
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: How to get the boot device on UEFI?

Post by kzinti »

zhiayang is correct, you can obtain the boot device handle from the LoadedImageProtocol:

Code: Select all

    extern "C" EFI_STATUS efi_main(EFI_HANDLE hImage, EFI_SYSTEM_TABLE* systemTable)
    {
        // Get access to the boot file system
        EFI_LOADED_IMAGE_PROTOCOL* image;
        status = BS->HandleProtocol(hImage, &efiLoadedImageProtocolGuid, (void**)&image);
Then the boot device handle is at "image->DeviceHandle".
Post Reply