Skip to content

Commit 4b4d2b5

Browse files
committed
kernel-stub: Use common print services
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 <jan.kiszka@siemens.com>
1 parent 6fa7f3f commit 4b4d2b5

5 files changed

Lines changed: 29 additions & 41 deletions

File tree

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ kernel_stub_name = kernel-stub$(MACHINE_TYPE_NAME).efi
246246

247247
kernel_stub_sources = \
248248
loader_interface.c \
249+
print.c \
249250
kernel-stub/fdt.c \
250251
kernel-stub/initrd.c \
251252
kernel-stub/main.c

kernel-stub/fdt.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* EFI Boot Guard, unified kernel stub
33
*
4-
* Copyright (c) Siemens AG, 2022
4+
* Copyright (c) Siemens AG, 2022-2026
55
*
66
* Authors:
77
* Jan Kiszka <jan.kiszka@siemens.com>
@@ -18,6 +18,7 @@
1818
#include <endian.h>
1919

2020
#include "kernel-stub.h"
21+
#include "print.h"
2122

2223
#if __BYTE_ORDER == __LITTLE_ENDIAN
2324
#define BE32_TO_HOST(val) bswap_32(val)
@@ -163,7 +164,7 @@ static EFI_STATUS clone_fdt(const VOID *fdt, UINTN size,
163164
status = BS->AllocatePages(AllocateAnyPages, EfiACPIReclaimMemory,
164165
SIZE_IN_PAGES(size), fdt_buffer);
165166
if (EFI_ERROR(status)) {
166-
error(L"Error allocating device tree buffer", status);
167+
ERROR(L"Failed to allocate device tree buffer\n");
167168
return status;
168169
}
169170
CopyMem((VOID *)(uintptr_t)*fdt_buffer, (VOID*)fdt,
@@ -182,7 +183,7 @@ EFI_STATUS replace_fdt(const VOID *fdt)
182183
if (EFI_ERROR(status)) {
183184
const FDT_HEADER *header = fdt;
184185

185-
info(L"Firmware does not provide device tree fixup protocol");
186+
WARNING(L"Firmware does not provide device tree fixup protocol\n");
186187

187188
size = BE32_TO_HOST(header->TotalSize);
188189
status = clone_fdt(fdt, size, &fdt_buffer);
@@ -195,7 +196,8 @@ EFI_STATUS replace_fdt(const VOID *fdt)
195196
status = protocol->Fixup(protocol, (VOID *) fdt, &size,
196197
EFI_DT_APPLY_FIXUPS);
197198
if (status != EFI_BUFFER_TOO_SMALL) {
198-
error(L"Device tree fixup: unexpected error", status);
199+
ERROR(L"Early device tree fixup failure (%r)\n",
200+
status);
199201
return status;
200202
}
201203

@@ -210,7 +212,7 @@ EFI_STATUS replace_fdt(const VOID *fdt)
210212
EFI_DT_RESERVE_MEMORY);
211213
if (EFI_ERROR(status)) {
212214
(VOID) BS->FreePages(fdt_buffer, SIZE_IN_PAGES(size));
213-
error(L"Device tree fixup failed", status);
215+
ERROR(L"Device tree fixup failed (%r)\n", status);
214216
return status;
215217
}
216218
}
@@ -219,7 +221,8 @@ EFI_STATUS replace_fdt(const VOID *fdt)
219221
(VOID *)(uintptr_t)fdt_buffer);
220222
if (EFI_ERROR(status)) {
221223
(VOID) BS->FreePages(fdt_buffer, SIZE_IN_PAGES(size));
222-
error(L"Failed to install alternative device tree", status);
224+
ERROR(L"Failed to install alternative device tree (%r)\n",
225+
status);
223226
}
224227

225228
return status;

kernel-stub/initrd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <efilib.h>
1717

1818
#include "kernel-stub.h"
19+
#include "print.h"
1920

2021
typedef struct {
2122
VENDOR_DEVICE_PATH vendor;

kernel-stub/kernel-stub.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
#include <efi.h>
1616

17-
VOID error(CHAR16 *message, EFI_STATUS status);
18-
VOID __attribute__((noreturn)) error_exit(CHAR16 *message, EFI_STATUS status);
19-
VOID info(CHAR16 *message);
20-
2117
const VOID *get_fdt_compatible(VOID);
2218
BOOLEAN match_fdt(const VOID *fdt, const CHAR8 *compatible);
2319
EFI_STATUS replace_fdt(const VOID *fdt);

kernel-stub/main.c

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
#include <efilib.h>
1717

1818
#include "kernel-stub.h"
19-
#include "version.h"
2019
#include "loader_interface.h"
20+
#include "print.h"
21+
#include "version.h"
2122

2223
typedef struct {
2324
UINT8 Ignore[60];
@@ -55,7 +56,6 @@ typedef struct {
5556
UINT8 Ignore[24];
5657
} __attribute__((packed)) SECTION;
5758

58-
static EFI_HANDLE this_image;
5959
static EFI_LOADED_IMAGE kernel_image;
6060

6161
EFI_PHYSICAL_ADDRESS align_addr(EFI_PHYSICAL_ADDRESS ptr,
@@ -64,24 +64,6 @@ EFI_PHYSICAL_ADDRESS align_addr(EFI_PHYSICAL_ADDRESS ptr,
6464
return (ptr + align - 1) & ~(align - 1);
6565
}
6666

67-
VOID info(CHAR16 *message)
68-
{
69-
Print(L"Unified kernel stub: %s\n", message);
70-
}
71-
72-
VOID error(CHAR16 *message, EFI_STATUS status)
73-
{
74-
Print(L"Unified kernel stub: %s (%r).\n", message, status);
75-
(VOID) BS->Stall(3 * 1000 * 1000);
76-
}
77-
78-
VOID __attribute__((noreturn)) error_exit(CHAR16 *message, EFI_STATUS status)
79-
{
80-
error(message, status);
81-
(VOID) BS->Exit(this_image, status, 0, NULL);
82-
__builtin_unreachable();
83-
}
84-
8567
static const PE_HEADER *get_pe_header(const VOID *image)
8668
{
8769
const DOS_HEADER *dos_header = image;
@@ -119,8 +101,10 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
119101
this_image = image_handle;
120102
InitializeLib(image_handle, system_table);
121103

122-
Print(L"Unified kernel stub (EFI Boot Guard %s)\n",
123-
L"" EFIBOOTGUARD_VERSION);
104+
#if !defined(SILENT_BOOT)
105+
PrintC(EFI_CYAN, L"Unified kernel stub (EFI Boot Guard %s)\n",
106+
L"" EFIBOOTGUARD_VERSION);
107+
#endif
124108

125109
fdt_compatible = get_fdt_compatible();
126110

@@ -133,7 +117,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
133117

134118
/* consider zero-termination for string length */
135119
if (stub_image->LoadOptionsSize > sizeof(CHAR16)) {
136-
info(L"WARNING: Passed command line options ignored, only built-in used");
120+
WARNING(L"Passed command line options ignored, only built-in used\n");
137121
}
138122

139123
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)
192176
status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
193177
kernel_pages, &kernel_buffer);
194178
if (EFI_ERROR(status)) {
195-
error(L"Error allocating memory for kernel image", status);
179+
ERROR(L"Could not allocate memory for kernel image\n");
196180
goto cleanup_initrd;
197181
}
198182

199183
aligned_kernel_buffer =
200184
align_addr(kernel_buffer, pe_header->Opt.SectionAlignment);
201185
if ((uintptr_t) aligned_kernel_buffer != aligned_kernel_buffer) {
202-
error(L"Alignment overflow for kernel image", EFI_LOAD_ERROR);
186+
ERROR(L"Alignment overflow for kernel image\n");
203187
status = EFI_LOAD_ERROR;
204188
goto cleanup_buffer;
205189
}
@@ -216,7 +200,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
216200
&kernel_handle, &LoadedImageProtocol, &kernel_image,
217201
NULL);
218202
if (EFI_ERROR(status)) {
219-
error(L"Error registering kernel image", status);
203+
ERROR(L"Registering kernel image failed (%r)\n", status);
220204
goto cleanup_buffer;
221205
}
222206

@@ -225,20 +209,20 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
225209
if (EFI_ERROR(status)) {
226210
goto cleanup_protocols;
227211
}
228-
info(L"Using matched embedded device tree");
212+
INFO(L"Using matched embedded device tree\n");
229213
} else if (fdt_compatible) {
230214
if (has_dtbs) {
231-
info(L"WARNING: No embedded device tree matched firmware-provided one");
215+
WARNING(L"No embedded device tree matched firmware-provided one\n");
232216
}
233-
info(L"Using firmware-provided device tree");
217+
INFO(L"Using firmware-provided device tree\n");
234218
}
235219

236220
UINT16 *boot_medium_uuidstr =
237221
disk_get_part_uuid(stub_image->DeviceHandle);
238222
bg_interface_params.loader_device_part_uuid = boot_medium_uuidstr;
239223
status = set_bg_interface_vars(&bg_interface_params);
240224
if (EFI_ERROR(status)) {
241-
error(L"could not set interface vars", status);
225+
WARNING(L"Could not set interface vars (%r)\n", status);
242226
}
243227
FreePool(boot_medium_uuidstr);
244228

@@ -253,7 +237,7 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
253237
kernel_handle, &LoadedImageProtocol, &kernel_image,
254238
NULL);
255239
if (EFI_ERROR(cleanup_status)) {
256-
error(L"Error unregistering kernel image", status);
240+
ERROR(L"Unregistering kernel image failed (%r)\n", status);
257241
if (!EFI_ERROR(status)) {
258242
status = cleanup_status;
259243
}
@@ -263,5 +247,8 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
263247
cleanup_initrd:
264248
uninstall_initrd_loader();
265249

250+
if (EFI_ERROR(status)) {
251+
(VOID) BS->Stall(3 * 1000 * 1000);
252+
}
266253
return status;
267254
}

0 commit comments

Comments
 (0)