Page 1 of 1

EFI: Having difficulties opening the loaded image protocol

Posted: Sun Apr 03, 2016 7:37 am
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.

Re: EFI: Having difficulties opening the loaded image protoc

Posted: Mon Apr 04, 2016 2:56 am
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.

Re: EFI: Having difficulties opening the loaded image protoc

Posted: Tue Apr 05, 2016 10:55 pm
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.

Re: EFI: Having difficulties opening the loaded image protoc

Posted: Wed Apr 06, 2016 12:03 am
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().