From a9863cbd5286626e6484317e101ca953a5605cd3 Mon Sep 17 00:00:00 2001 From: d0p1 Date: Mon, 27 Jan 2025 14:11:59 +0100 Subject: [PATCH] refactor(boot): uefi: add macros --- boot/efi/bootia32.asm | 23 +++++---- boot/efi/fs.inc | 10 +--- boot/efi/logger.inc | 45 ++++++++--------- boot/efi/uefi.inc | 111 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 136 insertions(+), 53 deletions(-) diff --git a/boot/efi/bootia32.asm b/boot/efi/bootia32.asm index c134f9d..efcfee5 100644 --- a/boot/efi/bootia32.asm +++ b/boot/efi/bootia32.asm @@ -14,19 +14,22 @@ ;; Function: efimain ;; - ;; Parameters: + ;; In: + ;; [ESP+4] - handle + ;; [ESP+8] - ;; - ;; [esp+4] - handle - ;; [esp+8] - - ;; - ;; Returns: - ;; - ;; eax - efi status + ;; Out: + ;; EAX - efi status ;; efimain: - mov eax, [esp+4] - mov [hImage], eax + push ebp + mov ebp, esp + + EFI_INIT [ebp + 8], [ebp + 12] + mov eax, [esp+8] + mov [hImage], eax + mov eax, [esp+12] mov [pSystemTable], eax mov ebx, [eax + EFI_SYSTEM_TABLE.RuntimeServices] @@ -44,7 +47,7 @@ efimain: call efi_memory_init call efi_log_init - mov esi, szHelloMsg + mov eax, szHelloMsg call efi_log call efi_fs_init diff --git a/boot/efi/fs.inc b/boot/efi/fs.inc index 677dac3..0cdc6c1 100644 --- a/boot/efi/fs.inc +++ b/boot/efi/fs.inc @@ -5,19 +5,13 @@ efi_fs_init: ; OpenProtocol(EFI_HANDLE Handle, EFI_GUID *proto, VOID **Interface, EFI_HANDLE AgentHandle, UINT32 Attrs) - push 0 - push [hImage] - push pLoadedImage - push aFSProtoGUID - push [hImage] - call [fnOpenProtocol] - add esp, 20 + EFI_CALL [fnOpenProtocol], [hImage], aFSProtoGUID, pLoadedImage, [hImage], 0, 2 or eax, eax jnz .error ret .error: - mov esi, szErrTmp + mov eax, szErrTmp call efi_log ret diff --git a/boot/efi/logger.inc b/boot/efi/logger.inc index b127399..94d0d46 100644 --- a/boot/efi/logger.inc +++ b/boot/efi/logger.inc @@ -4,9 +4,7 @@ ;; Function: efi_log_init efi_log_init: - mov eax, [pSystemTable] - add eax, EFI_SYSTEM_TABLE.ConOut - mov eax, [eax] + EFI_GET_INTERFACE eax, ConOut mov [pConOut], eax mov ecx, [eax + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset] @@ -16,23 +14,18 @@ efi_log_init: mov [fnOutputStr], ecx mov eax, [pRuntimeServices] - mov ecx, [eax + EFI_RUNTIMES_SERVICES.GetTime] - mov [fnGetTime], ecx + mov eax, [eax + EFI_RUNTIMES_SERVICES.GetTime] + mov [fnGetTime], eax - push 1 - push [pConOut] - call [fnOutputReset] - add esp, 8 + EFI_CALL [fnOutputReset], [pConOut], 1 ret ;; Function: efi_log_time efi_log_time: ; GetTime(EFI_TIME *time, EFI_TIME_CAPS *cap) - push 0 - push stEfiTime - call [fnGetTime] - add esp, 8 + + EFI_CALL [fnGetTime], stEfiTime, 0 ; Hours mov al, [stEfiTime + EFI_TIME.Hour] @@ -79,31 +72,31 @@ efi_log_time: movzx cx, ah mov [szTime + 16], cx - push szTime - push [pConOut] - call [fnOutputStr] - add esp, 8 + EFI_CALL [fnOutputStr], [pConOut], szTime ret ;; Function: efi_log ;; ;; In: - ;; ESI - string to print + ;; EAX - string to print efi_log: - call efi_log_time + push ebp + mov ebp, esp push esi - push [pConOut] - call [fnOutputStr] - add esp, 8 + mov esi, eax + + call efi_log_time + + EFI_CALL [fnOutputStr], [pConOut], esi ; print CRLF - push szEndLine - push [pConOut] - call [fnOutputStr] - add esp, 8 + EFI_CALL [fnOutputStr], [pConOut], szEndLine + pop esi + + leave ret section '.data' data readable writable diff --git a/boot/efi/uefi.inc b/boot/efi/uefi.inc index ea9ea04..bfb3271 100644 --- a/boot/efi/uefi.inc +++ b/boot/efi/uefi.inc @@ -1,5 +1,88 @@ ;; File: uefi.inc +macro EFI_CALL fn, [arg] { + common + _ARGS = 0 + reverse + match anything, arg \{ + _ARGS = _ARGS + 4 + push dword arg + \} + common + call fn + if _ARGS + add esp, _ARGS + end if +} + +macro EFI_GET_INTERFACE reg, interface { + if interface in + mov reg, [pEfiSystemTable] + mov reg, [reg + EFI_SYSTEM_TABLE.#interface] + end if +} + +macro EFI_INIT handle, table { + local efi_init_bad + local efi_init_end + clc + ; image handle + mov eax, handle + or eax, eax + jz efi_init_bad + mov [hEfiImage], eax + mov eax, table + mov [pEfiSystemTable], eax + + mov edx, [eax + EFI_SYSTEM_TABLE.BootServices] + mov [pEfiBootServices], edx + + mov edx, [eax + EFI_SYSTEM_TABLE.RuntimeServices] + mov [pEfiRuntimeServices], edx + + jmp efi_init_end +efi_init_bad: + stc +efi_init_end: +} + +EFI_SUCCESS = 0x0 +EFI_ERR = 0x80000000 + +EFI_LOAD_ERROR = (EFI_ERR or 1) +EFI_INVALID_PARAMETER = (EFI_ERR or 2) +EFI_UNSUPPORTED = (EFI_ERR or 3) +EFI_BAD_BUFFER_SIZE = (EFI_ERR or 4) +EFI_BUFFER_TOO_SMALL = (EFI_ERR or 5) +EFI_NOT_READY = (EFI_ERR or 6) +EFI_DEVICE_ERROR = (EFI_ERR or 7) +EFI_WRITE_PROTECTED = (EFI_ERR or 8) +EFI_OUT_OF_RESOURCEs = (EFI_ERR or 9) +EFI_VOLUME_CORRUPTED = (EFI_ERR or 10) +EFI_VOLUME_FULL = (EFI_ERR or 11) +EFI_NO_MEDIA = (EFI_ERR or 12) +EFI_MEDIA_CHANGED = (EFI_ERR or 13) +EFI_NOT_FOUND = (EFI_ERR or 14) +EFI_ACCESS_DEBIED = (EFI_ERR or 15) +EFI_NO_RESPONSE = (EFI_ERR or 16) +EFI_NO_MAPPING = (EFI_ERR or 17) +EFI_TIMEOUT = (EFI_ERR or 18) +EFI_NOT_STARTED = (EFI_ERR or 19) +EFI_ALREADY_STARTED = (EFI_ERR or 20) +EFI_ABORTED = (EFI_ERR or 21) +EFI_ICMP_ERROR = (EFI_ERR or 22) +EFI_TFTP_ERROR = (EFI_ERR or 23) +EFI_PROTOCOL_ERROR = (EFI_ERR or 24) +EFI_INCOMPATIBLE_VERSION = (EFI_ERR or 25) +EFI_SECURITY_VIOLATION = (EFI_ERR or 26) +EFI_CRC_ERROR = (EFI_ERR or 27) +EFI_END_OF_MEDIA = (EFI_ERR or 28) +EFI_END_OF_FILE = (EFI_ERR or 31) +EFI_INVALID_LANGUAGE = (EFI_ERR or 32) +EFI_COMPROMISED_DATA = (EFI_ERR or 33) +EFI_IP_ADDRESS_CONFLICT = (EFI_ERR or 34) +EFI_HTTP_ERROR = (EFI_ERR or 35) + struc BOOLEAN { . db ? @@ -58,14 +141,6 @@ struc UINTPTR . dd ? } -struc EFI_GUID -{ - .Data1 dd ? - .Data2 dw ? - .Data3 dw ? - .Data4 db 8 dup(?) -} - struc EFI_TIME { .Year UINT16 @@ -212,6 +287,17 @@ struc EFI_BOOT_SERVICES } DEFN EFI_BOOT_SERVICES +EFI_LOCATE_SEARCH_ALL_HANDLES = 0x0 +EFI_LOCATE_SEARCH_BY_REGISTER_NOTIFY = 0x1 +EFI_LOCATE_SEARCH_BY_PROTOCOL = 0x2 + +EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL = 0x00000001 +EFI_OPEN_PROTOCOL_GET_PROTOCOL = 0x00000002 +EFI_OPEN_PROTOCOL_TEST_PROTOCOL = 0x00000004 +EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER = 0x00000008 +EFI_OPEN_PROTOCOL_BY_DRIVER = 0x00000010 +EFI_OPEN_PROTOCOL_EXCLUSIVE = 0x00000020 + ;; ======================================================================== ;; EFI_RUNTIMES_SERVICES ;; ======================================================================== @@ -364,7 +450,7 @@ EFI_LOAD_FILE2_PROTOCOL equ EFI_LOAD_FILE_PROTOCOL ;; ======================================================================== ;; EFI_SIMPLE_FILE_SYSTEM_PROTOCOL ;; ======================================================================== -EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID equ 0x022, 0x5b, 0x4e, 0x96, 0x59, 0x64, 0xd2, 0x11, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b +EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID equ 0x22, 0x5b, 0x4e, 0x96, 0x59, 0x64, 0xd2, 0x11, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b ;EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID equ 0x0964e5b22, 0x6459, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION = 0x00010000 @@ -417,3 +503,10 @@ EFI_FILE_RESERVED = 0x0000000000000008 EFI_FILE_DIRECTORY = 0x0000000000000010 EFI_FILE_ARCHIVE = 0x0000000000000020 EFI_FILE_VALID_ATTR = 0x0000000000000037 + +section '.data' data readable writeable + +hEfiImage dd 0 +pEfiSystemTable dd 0 +pEfiBootServices dd 0 +pEfiRuntimeServices dd 0