[PATCH 5/5] efi: add device tree configuration table support

Ahmad Fatoum a.fatoum at pengutronix.de
Sun Oct 9 23:08:42 PDT 2022


When running on device-tree enabled systems, barebox as EFI payload may
be passed a device tree via the system table. We've no use for that just
yet, but lets make it available as /efi.dtb.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/efi/guid.c           |  1 +
 common/efi/payload/Makefile |  1 +
 common/efi/payload/fdt.c    | 43 +++++++++++++++++++++++++++++++++++++
 include/efi.h               |  1 +
 4 files changed, 46 insertions(+)
 create mode 100644 common/efi/payload/fdt.c

diff --git a/common/efi/guid.c b/common/efi/guid.c
index 93418d57b469..ca16f4520ff2 100644
--- a/common/efi/guid.c
+++ b/common/efi/guid.c
@@ -14,6 +14,7 @@ efi_guid_t efi_block_io_protocol_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
 efi_guid_t efi_rng_protocol_guid = EFI_RNG_PROTOCOL_GUID;
 efi_guid_t efi_barebox_vendor_guid = EFI_BAREBOX_VENDOR_GUID;
 efi_guid_t efi_systemd_vendor_guid = EFI_SYSTEMD_VENDOR_GUID;
+efi_guid_t efi_fdt_guid = EFI_DEVICE_TREE_GUID;
 
 #define EFI_GUID_STRING(guid, short, long) do {	\
 	if (!efi_guidcmp(guid, *g))		\
diff --git a/common/efi/payload/Makefile b/common/efi/payload/Makefile
index bcbdda335f06..eeed046b0045 100644
--- a/common/efi/payload/Makefile
+++ b/common/efi/payload/Makefile
@@ -2,5 +2,6 @@
 
 obj-y += init.o
 obj-y += image.o
+obj-$(CONFIG_OFTREE) += fdt.o
 bbenv-y += env-efi
 obj-$(CONFIG_CMD_IOMEM) += iomem.o
diff --git a/common/efi/payload/fdt.c b/common/efi/payload/fdt.c
new file mode 100644
index 000000000000..dbbc93fb7d68
--- /dev/null
+++ b/common/efi/payload/fdt.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define pr_fmt(fmt) "efi-fdt: " fmt
+
+#include <common.h>
+#include <init.h>
+#include <libfile.h>
+#include <efi/efi-payload.h>
+#include <efi/efi-device.h>
+
+static int efi_fdt_probe(void)
+{
+	efi_config_table_t *ect;
+
+	for_each_efi_config_table(ect) {
+		struct fdt_header *oftree;
+		u32 magic, size;
+		int ret;
+
+		if (efi_guidcmp(ect->guid, EFI_DEVICE_TREE_GUID))
+			continue;
+
+		oftree = (void *)ect->table;
+		magic = be32_to_cpu(oftree->magic);
+
+		if (magic != FDT_MAGIC) {
+			pr_err("table has invalid magic 0x%08x\n", magic);
+			return -EILSEQ;
+		}
+
+		size = be32_to_cpu(oftree->totalsize);
+		ret = write_file("/efi.dtb", oftree, size);
+		if (ret) {
+			pr_err("error saving /efi.dtb: %pe\n", ERR_PTR(ret));
+			return ret;
+		}
+
+		return 0;
+	}
+
+	return 0;
+}
+late_initcall(efi_fdt_probe);
diff --git a/include/efi.h b/include/efi.h
index 36e08af67bf3..3595cf05ccb7 100644
--- a/include/efi.h
+++ b/include/efi.h
@@ -541,6 +541,7 @@ extern efi_guid_t efi_block_io_protocol_guid;
 extern efi_guid_t efi_rng_protocol_guid;
 extern efi_guid_t efi_barebox_vendor_guid;
 extern efi_guid_t efi_systemd_vendor_guid;
+extern efi_guid_t efi_fdt_guid;
 
 typedef struct {
 	efi_guid_t guid;
-- 
2.30.2




More information about the barebox mailing list