From: Jan Kiszka <[email protected]> Align the kernel stub to the print services that the EBG core uses as well. That brings coloring, silent boot, and avoids duplicating logic between both.
Signed-off-by: Jan Kiszka <[email protected]> --- Makefile.am | 1 + kernel-stub/fdt.c | 15 +++++++----- kernel-stub/initrd.c | 1 + kernel-stub/kernel-stub.h | 4 ---- kernel-stub/main.c | 49 ++++++++++++++------------------------- 5 files changed, 29 insertions(+), 41 deletions(-) diff --git a/Makefile.am b/Makefile.am index a4cf201..7c87244 100644 --- a/Makefile.am +++ b/Makefile.am @@ -246,6 +246,7 @@ kernel_stub_name = kernel-stub$(MACHINE_TYPE_NAME).efi kernel_stub_sources = \ loader_interface.c \ + print.c \ kernel-stub/fdt.c \ kernel-stub/initrd.c \ kernel-stub/main.c diff --git a/kernel-stub/fdt.c b/kernel-stub/fdt.c index e10b891..90a27cd 100644 --- a/kernel-stub/fdt.c +++ b/kernel-stub/fdt.c @@ -1,7 +1,7 @@ /* * EFI Boot Guard, unified kernel stub * - * Copyright (c) Siemens AG, 2022 + * Copyright (c) Siemens AG, 2022-2026 * * Authors: * Jan Kiszka <[email protected]> @@ -18,6 +18,7 @@ #include <endian.h> #include "kernel-stub.h" +#include "print.h" #if __BYTE_ORDER == __LITTLE_ENDIAN #define BE32_TO_HOST(val) bswap_32(val) @@ -163,7 +164,7 @@ static EFI_STATUS clone_fdt(const VOID *fdt, UINTN size, status = BS->AllocatePages(AllocateAnyPages, EfiACPIReclaimMemory, SIZE_IN_PAGES(size), fdt_buffer); if (EFI_ERROR(status)) { - error(L"Error allocating device tree buffer", status); + ERROR(L"Failed to allocate device tree buffer\n"); return status; } CopyMem((VOID *)(uintptr_t)*fdt_buffer, (VOID*)fdt, @@ -182,7 +183,7 @@ EFI_STATUS replace_fdt(const VOID *fdt) if (EFI_ERROR(status)) { const FDT_HEADER *header = fdt; - info(L"Firmware does not provide device tree fixup protocol"); + WARNING(L"Firmware does not provide device tree fixup protocol\n"); size = BE32_TO_HOST(header->TotalSize); status = clone_fdt(fdt, size, &fdt_buffer); @@ -195,7 +196,8 @@ EFI_STATUS replace_fdt(const VOID *fdt) status = protocol->Fixup(protocol, (VOID *) fdt, &size, EFI_DT_APPLY_FIXUPS); if (status != EFI_BUFFER_TOO_SMALL) { - error(L"Device tree fixup: unexpected error", status); + ERROR(L"Early device tree fixup failure (%r)\n", + status); return status; } @@ -210,7 +212,7 @@ EFI_STATUS replace_fdt(const VOID *fdt) EFI_DT_RESERVE_MEMORY); if (EFI_ERROR(status)) { (VOID) BS->FreePages(fdt_buffer, SIZE_IN_PAGES(size)); - error(L"Device tree fixup failed", status); + ERROR(L"Device tree fixup failed (%r)\n", status); return status; } } @@ -219,7 +221,8 @@ EFI_STATUS replace_fdt(const VOID *fdt) (VOID *)(uintptr_t)fdt_buffer); if (EFI_ERROR(status)) { (VOID) BS->FreePages(fdt_buffer, SIZE_IN_PAGES(size)); - error(L"Failed to install alternative device tree", status); + ERROR(L"Failed to install alternative device tree (%r)\n", + status); } return status; diff --git a/kernel-stub/initrd.c b/kernel-stub/initrd.c index 969acca..346ac8d 100644 --- a/kernel-stub/initrd.c +++ b/kernel-stub/initrd.c @@ -16,6 +16,7 @@ #include <efilib.h> #include "kernel-stub.h" +#include "print.h" typedef struct { VENDOR_DEVICE_PATH vendor; diff --git a/kernel-stub/kernel-stub.h b/kernel-stub/kernel-stub.h index b0f10df..fd6322c 100644 --- a/kernel-stub/kernel-stub.h +++ b/kernel-stub/kernel-stub.h @@ -14,10 +14,6 @@ #include <efi.h> -VOID error(CHAR16 *message, EFI_STATUS status); -VOID __attribute__((noreturn)) error_exit(CHAR16 *message, EFI_STATUS status); -VOID info(CHAR16 *message); - const VOID *get_fdt_compatible(VOID); BOOLEAN match_fdt(const VOID *fdt, const CHAR8 *compatible); EFI_STATUS replace_fdt(const VOID *fdt); diff --git a/kernel-stub/main.c b/kernel-stub/main.c index 5b83eba..9d77dcf 100644 --- a/kernel-stub/main.c +++ b/kernel-stub/main.c @@ -16,8 +16,9 @@ #include <efilib.h> #include "kernel-stub.h" -#include "version.h" #include "loader_interface.h" +#include "print.h" +#include "version.h" typedef struct { UINT8 Ignore[60]; @@ -55,7 +56,6 @@ typedef struct { UINT8 Ignore[24]; } __attribute__((packed)) SECTION; -static EFI_HANDLE this_image; static EFI_LOADED_IMAGE kernel_image; EFI_PHYSICAL_ADDRESS align_addr(EFI_PHYSICAL_ADDRESS ptr, @@ -64,24 +64,6 @@ EFI_PHYSICAL_ADDRESS align_addr(EFI_PHYSICAL_ADDRESS ptr, return (ptr + align - 1) & ~(align - 1); } -VOID info(CHAR16 *message) -{ - Print(L"Unified kernel stub: %s\n", message); -} - -VOID error(CHAR16 *message, EFI_STATUS status) -{ - Print(L"Unified kernel stub: %s (%r).\n", message, status); - (VOID) BS->Stall(3 * 1000 * 1000); -} - -VOID __attribute__((noreturn)) error_exit(CHAR16 *message, EFI_STATUS status) -{ - error(message, status); - (VOID) BS->Exit(this_image, status, 0, NULL); - __builtin_unreachable(); -} - static const PE_HEADER *get_pe_header(const VOID *image) { const DOS_HEADER *dos_header = image; @@ -119,8 +101,10 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) this_image = image_handle; InitializeLib(image_handle, system_table); - Print(L"Unified kernel stub (EFI Boot Guard %s)\n", - L"" EFIBOOTGUARD_VERSION); +#if !defined(SILENT_BOOT) + PrintC(EFI_CYAN, L"Unified kernel stub (EFI Boot Guard %s)\n", + L"" EFIBOOTGUARD_VERSION); +#endif fdt_compatible = get_fdt_compatible(); @@ -133,7 +117,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) /* consider zero-termination for string length */ if (stub_image->LoadOptionsSize > sizeof(CHAR16)) { - info(L"WARNING: Passed command line options ignored, only built-in used"); + WARNING(L"Passed command line options ignored, only built-in used\n"); } pe_header = get_pe_header(stub_image->ImageBase); @@ -192,14 +176,14 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, kernel_pages, &kernel_buffer); if (EFI_ERROR(status)) { - error(L"Error allocating memory for kernel image", status); + ERROR(L"Could not allocate memory for kernel image\n"); goto cleanup_initrd; } aligned_kernel_buffer = align_addr(kernel_buffer, pe_header->Opt.SectionAlignment); if ((uintptr_t) aligned_kernel_buffer != aligned_kernel_buffer) { - error(L"Alignment overflow for kernel image", EFI_LOAD_ERROR); + ERROR(L"Alignment overflow for kernel image\n"); status = EFI_LOAD_ERROR; goto cleanup_buffer; } @@ -216,7 +200,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) &kernel_handle, &LoadedImageProtocol, &kernel_image, NULL); if (EFI_ERROR(status)) { - error(L"Error registering kernel image", status); + ERROR(L"Registering kernel image failed (%r)\n", status); goto cleanup_buffer; } @@ -225,12 +209,12 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) if (EFI_ERROR(status)) { goto cleanup_protocols; } - info(L"Using matched embedded device tree"); + INFO(L"Using matched embedded device tree\n"); } else if (fdt_compatible) { if (has_dtbs) { - info(L"WARNING: No embedded device tree matched firmware-provided one"); + WARNING(L"No embedded device tree matched firmware-provided one\n"); } - info(L"Using firmware-provided device tree"); + INFO(L"Using firmware-provided device tree\n"); } UINT16 *boot_medium_uuidstr = @@ -238,7 +222,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) bg_interface_params.loader_device_part_uuid = boot_medium_uuidstr; status = set_bg_interface_vars(&bg_interface_params); if (EFI_ERROR(status)) { - error(L"could not set interface vars", status); + WARNING(L"Could not set interface vars (%r)\n", status); } FreePool(boot_medium_uuidstr); @@ -253,7 +237,7 @@ cleanup_protocols: kernel_handle, &LoadedImageProtocol, &kernel_image, NULL); if (EFI_ERROR(cleanup_status)) { - error(L"Error unregistering kernel image", status); + ERROR(L"Unregistering kernel image failed (%r)\n", status); if (!EFI_ERROR(status)) { status = cleanup_status; } @@ -263,5 +247,8 @@ cleanup_buffer: cleanup_initrd: uninstall_initrd_loader(); + if (EFI_ERROR(status)) { + (VOID) BS->Stall(3 * 1000 * 1000); + } return status; } -- 2.47.3 -- You received this message because you are subscribed to the Google Groups "EFI Boot Guard" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/efibootguard-dev/4b4d2b5d8cf698cca833582caebbcdf6394d1813.1775543350.git.jan.kiszka%40siemens.com.
