git: 136d2dc09310 - main - kboot: Start to move efi common routines to libkboot

From: Warner Losh <imp_at_FreeBSD.org>
Date: Wed, 09 Apr 2025 21:17:58 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=136d2dc093105625e943466384efa1e1c16afad3

commit 136d2dc093105625e943466384efa1e1c16afad3
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2025-04-09 21:16:57 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2025-04-09 21:16:57 +0000

    kboot: Start to move efi common routines to libkboot
    
    Start to move the common efi routines into libkboot by moving the efi
    memory map walking and implementing a printing routine around it.
    
    Sponsored by:           Netflix
---
 stand/kboot/include/efi.h                  | 17 ++++++
 stand/kboot/kboot/arch/aarch64/load_addr.c | 87 +--------------------------
 stand/kboot/libkboot/Makefile              |  4 ++
 stand/kboot/libkboot/efi.c                 | 97 ++++++++++++++++++++++++++++++
 4 files changed, 120 insertions(+), 85 deletions(-)

diff --git a/stand/kboot/include/efi.h b/stand/kboot/include/efi.h
new file mode 100644
index 000000000000..10368bd4a829
--- /dev/null
+++ b/stand/kboot/include/efi.h
@@ -0,0 +1,17 @@
+/*-
+ * Copyright (c) 2024, Netflix, Inc.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <sys/efi.h>
+#include <machine/metadata.h>
+
+/* Note, we mix and match FreeBSD types and EFI standard defined types */
+
+typedef void (*efi_map_entry_cb)(struct efi_md *, void *argp);
+
+void foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb, void *argp);
+void print_efi_map(struct efi_map_header *efihdr);
diff --git a/stand/kboot/kboot/arch/aarch64/load_addr.c b/stand/kboot/kboot/arch/aarch64/load_addr.c
index 9d6f23a9d344..8ceb21007c45 100644
--- a/stand/kboot/kboot/arch/aarch64/load_addr.c
+++ b/stand/kboot/kboot/arch/aarch64/load_addr.c
@@ -5,7 +5,6 @@
  */
 
 #include <sys/param.h>
-#include <sys/efi.h>
 #include <machine/metadata.h>
 #include <sys/linker.h>
 #include <fdt_platform.h>
@@ -13,6 +12,7 @@
 
 #include "kboot.h"
 #include "bootstrap.h"
+#include "efi.h"
 
 /*
  * Info from dtb about the EFI system
@@ -23,87 +23,6 @@ uint32_t efi_map_size;
 vm_paddr_t efi_map_phys_src;	/* From DTB */
 vm_paddr_t efi_map_phys_dst;	/* From our memory map metadata module */
 
-typedef void (*efi_map_entry_cb)(struct efi_md *, void *argp);
-
-static void
-foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb, void *argp)
-{
-	struct efi_md *map, *p;
-	size_t efisz;
-	int ndesc, i;
-
-	/*
-	 * Memory map data provided by UEFI via the GetMemoryMap
-	 * Boot Services API.
-	 */
-	efisz = roundup2(sizeof(struct efi_map_header), 16);
-	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
-
-	if (efihdr->descriptor_size == 0)
-		return;
-	ndesc = efihdr->memory_size / efihdr->descriptor_size;
-
-	for (i = 0, p = map; i < ndesc; i++,
-	    p = efi_next_descriptor(p, efihdr->descriptor_size)) {
-		cb(p, argp);
-	}
-}
-
-static void
-print_efi_map_entry(struct efi_md *p, void *argp __unused)
-{
-	const char *type;
-	static const char *types[] = {
-		"Reserved",
-		"LoaderCode",
-		"LoaderData",
-		"BootServicesCode",
-		"BootServicesData",
-		"RuntimeServicesCode",
-		"RuntimeServicesData",
-		"ConventionalMemory",
-		"UnusableMemory",
-		"ACPIReclaimMemory",
-		"ACPIMemoryNVS",
-		"MemoryMappedIO",
-		"MemoryMappedIOPortSpace",
-		"PalCode",
-		"PersistentMemory"
-	};
-
-	if (p->md_type < nitems(types))
-		type = types[p->md_type];
-	else
-		type = "<INVALID>";
-	printf("%23s %012lx %012lx %08lx ", type, p->md_phys,
-	    p->md_virt, p->md_pages);
-	if (p->md_attr & EFI_MD_ATTR_UC)
-		printf("UC ");
-	if (p->md_attr & EFI_MD_ATTR_WC)
-		printf("WC ");
-	if (p->md_attr & EFI_MD_ATTR_WT)
-		printf("WT ");
-	if (p->md_attr & EFI_MD_ATTR_WB)
-		printf("WB ");
-	if (p->md_attr & EFI_MD_ATTR_UCE)
-		printf("UCE ");
-	if (p->md_attr & EFI_MD_ATTR_WP)
-		printf("WP ");
-	if (p->md_attr & EFI_MD_ATTR_RP)
-		printf("RP ");
-	if (p->md_attr & EFI_MD_ATTR_XP)
-		printf("XP ");
-	if (p->md_attr & EFI_MD_ATTR_NV)
-		printf("NV ");
-	if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
-		printf("MORE_RELIABLE ");
-	if (p->md_attr & EFI_MD_ATTR_RO)
-		printf("RO ");
-	if (p->md_attr & EFI_MD_ATTR_RT)
-		printf("RUNTIME");
-	printf("\n");
-}
-
 static bool
 do_memory_from_fdt(int fd)
 {
@@ -212,9 +131,7 @@ do_memory_from_fdt(int fd)
 	efi_map_phys_src = 0; /* Mark MODINFOMD_EFI_MAP as valid */
 	close(fd2);
 	printf("UEFI MAP:\n");
-	printf("%23s %12s %12s %8s %4s\n",
-	    "Type", "Physical", "Virtual", "#Pages", "Attr");
-	foreach_efi_map_entry(efihdr, print_efi_map_entry, NULL);
+	print_efi_map(efihdr);
 	return true;	/* OK, we really have the memory map */
 
 no_read:
diff --git a/stand/kboot/libkboot/Makefile b/stand/kboot/libkboot/Makefile
index 249bfef817c5..e23ae9bb9215 100644
--- a/stand/kboot/libkboot/Makefile
+++ b/stand/kboot/libkboot/Makefile
@@ -13,6 +13,10 @@ SRCS+=	seg.c
 SRCS+=	termios.c
 SRCS+=	util.c
 
+.if ${MACHINE_ARCH} != "powerpc64"
+SRCS+=	efi.c
+.endif
+
 .sinclude "${.CURDIR}/arch/${MACHINE_ARCH}/Makefile.inc"
 
 .include <bsd.lib.mk>
diff --git a/stand/kboot/libkboot/efi.c b/stand/kboot/libkboot/efi.c
new file mode 100644
index 000000000000..1f7f28093819
--- /dev/null
+++ b/stand/kboot/libkboot/efi.c
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2024 Netflix, Inc
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/param.h>
+#include "stand.h"
+#include "efi.h"
+
+void
+foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb, void *argp)
+{
+	struct efi_md *map, *p;
+	size_t efisz;
+	int ndesc, i;
+
+	/*
+	 * Memory map data provided by UEFI via the GetMemoryMap
+	 * Boot Services API.
+	 */
+	efisz = roundup2(sizeof(struct efi_map_header), 16);
+	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
+
+	if (efihdr->descriptor_size == 0)
+		return;
+	ndesc = efihdr->memory_size / efihdr->descriptor_size;
+
+	for (i = 0, p = map; i < ndesc; i++,
+	    p = efi_next_descriptor(p, efihdr->descriptor_size)) {
+		cb(p, argp);
+	}
+}
+
+static void
+print_efi_map_entry(struct efi_md *p, void *argp __unused)
+{
+	const char *type;
+	static const char *types[] = {
+		"Reserved",
+		"LoaderCode",
+		"LoaderData",
+		"BootServicesCode",
+		"BootServicesData",
+		"RuntimeServicesCode",
+		"RuntimeServicesData",
+		"ConventionalMemory",
+		"UnusableMemory",
+		"ACPIReclaimMemory",
+		"ACPIMemoryNVS",
+		"MemoryMappedIO",
+		"MemoryMappedIOPortSpace",
+		"PalCode",
+		"PersistentMemory"
+	};
+
+	if (p->md_type < nitems(types))
+		type = types[p->md_type];
+	else
+		type = "<INVALID>";
+	printf("%23s %012lx %012lx %08lx ", type, p->md_phys,
+	    p->md_virt, p->md_pages);
+	if (p->md_attr & EFI_MD_ATTR_UC)
+		printf("UC ");
+	if (p->md_attr & EFI_MD_ATTR_WC)
+		printf("WC ");
+	if (p->md_attr & EFI_MD_ATTR_WT)
+		printf("WT ");
+	if (p->md_attr & EFI_MD_ATTR_WB)
+		printf("WB ");
+	if (p->md_attr & EFI_MD_ATTR_UCE)
+		printf("UCE ");
+	if (p->md_attr & EFI_MD_ATTR_WP)
+		printf("WP ");
+	if (p->md_attr & EFI_MD_ATTR_RP)
+		printf("RP ");
+	if (p->md_attr & EFI_MD_ATTR_XP)
+		printf("XP ");
+	if (p->md_attr & EFI_MD_ATTR_NV)
+		printf("NV ");
+	if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
+		printf("MORE_RELIABLE ");
+	if (p->md_attr & EFI_MD_ATTR_RO)
+		printf("RO ");
+	if (p->md_attr & EFI_MD_ATTR_RT)
+		printf("RUNTIME");
+	printf("\n");
+}
+
+void
+print_efi_map(struct efi_map_header *efihdr)
+{
+	printf("%23s %12s %12s %8s %4s\n",
+	    "Type", "Physical", "Virtual", "#Pages", "Attr");
+
+	foreach_efi_map_entry(efihdr, print_efi_map_entry, NULL);
+}