EFI: Having difficulties opening the loaded image protocol

Programming, for all ages and all languages.
Post Reply
fredito
Posts: 2
Joined: Sun Apr 03, 2016 7:09 am

EFI: Having difficulties opening the loaded image protocol

Post by fredito »

Hi everyone,
I think I need a fresh eye on my issue. Using FASM I kind of fail opening the loaded image protocol at the very start of a small EFI project. I think I'm following what the specs say, I disassembled to check that the instructions are in place, I read the assembly to check that the protocol GUID is right. But whatever the case, the BootService.OpenProtocol always returns a 2 for "invalid parameter".
Can someone check that this call seems right?

Code: Select all

format pe64 dll efi
entry main

section '.text' code executable readable
include "efi_test.inc"

main:
 mov [Handle], rcx         ; ImageHandle
 mov [SystemTable], rdx    ; pointer to SystemTable
 
 ; this is calling OpenProtocol on the image handle for EFI_LOADED_IMAGE_PROTOCOL_GUID
 mov rcx, [Handle]							; param 1: image handle
 lea rdx, [EFI_LOADED_IMAGE_PROTOCOL_GUID]	; param 2: GUID
 lea r8, [LoadedImageProtocol]				; param 3: pointer where to store the protocol
 mov r9, rcx								; param 4: image handle
 mov r10, 0
 push r10									; param 5: optional null, not a driver
 mov r10, 2
 push r10									; param 6: attributes=2=EFI_OPEN_PROTOCOL_GET_PROTOCOL
 mov rax, [SystemTable]
 mov rax, [rax + EFI_SYSTEM_TABLE.BootServices]
 call [rax + EFI_BOOT_SERVICES_TABLE.OpenProtocol]
 pop rcx
 pop rcx
 cmp rax, EFI_SUCCESS
 je got_protocol
	; ... display error message here
I got the 6 parameters correctly, right? Or do I need to do something before so that the call is successful? I can't figure out this one.
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: EFI: Having difficulties opening the loaded image protoc

Post by Combuster »

It looks like the wrong calling convention. You need to have empty stack space so that register parameters can be shoved back, and the stack needs to remain SSE-aligned.

I haven't dealt with the win64 convention in a while, so please look it up for the exact details.
"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 ]
fredito
Posts: 2
Joined: Sun Apr 03, 2016 7:09 am

Re: EFI: Having difficulties opening the loaded image protoc

Post by fredito »

A small update. I managed to get the loaded image protocol but by using HandleProtocol, which is supposed to be deprecated by OpenProtocol.

Apparently, in order to successfully use OpenProtocol, I need to use a "EFI core image handle" as the agent instead of the currently running EFI image. The only issue is that I have no idea where to find it, so I'm stuck using a deprecated API.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: EFI: Having difficulties opening the loaded image protoc

Post by kzinti »

For applications. just pass your application handle as the "agent". This is what the UEFI 2.5 specs says to do and it works.

That said, both Linux and FreeBSD use HandleProtocol() and not OpenProtocol(). So we know it is working and not going away soon. Up to you if you insist on using OpenProtocol() / CloseProtocol().
Post Reply