All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] efi_loader: implement EFI_DT_FIXUP_PROTOCOL
@ 2020-12-13 21:48 Heinrich Schuchardt
  2020-12-13 21:48 ` [PATCH 1/2] " Heinrich Schuchardt
  2020-12-13 21:48 ` [PATCH 2/2] efi_selftest: dtbdump support EFI_DT_FIXUP_PROTOCOL Heinrich Schuchardt
  0 siblings, 2 replies; 3+ messages in thread
From: Heinrich Schuchardt @ 2020-12-13 21:48 UTC (permalink / raw)
  To: u-boot

Device-trees are often Linux version specific. So it seems reasonable to
let GRUB load device-trees according to the selected kernel version.
What currently is missing is a means to let the firmware apply necessary
fix-ups.

In the Device Tree Evolution Project (DTE) the idea was born to provide a
UEFI protocol which can be called by a boot manager to request the firmware
to apply fix-ups to a device-tree.

A draft specification for such a protocol can be found at:
https://github.com/U-Boot-EFI/EFI_DT_FIXUP_PROTOCOL

The first patch implements the protocol. The second allows the dtbdump.efi
test tool to load a flattened device-tree and call the
EFI_DT_FIXUP_PROTOCOL to apply fix-ups, make memory reservations and
finally install the device tree as a configuration table. In a next step
this configuration table can be dumped to a file.

Heinrich Schuchardt (2):
  efi_loader: implement EFI_DT_FIXUP_PROTOCOL
  efi_selftest: dtbdump support EFI_DT_FIXUP_PROTOCOL

 cmd/bootefi.c                  |  80 ---------
 cmd/efidebug.c                 |   5 +
 include/efi_dt_fixup.h         |  39 +++++
 include/efi_loader.h           |   2 +
 lib/efi_loader/Makefile        |   3 +
 lib/efi_loader/efi_dt_fixup.c  | 160 ++++++++++++++++++
 lib/efi_loader/efi_root_node.c |   6 +
 lib/efi_selftest/dtbdump.c     | 298 +++++++++++++++++++++++++++++----
 8 files changed, 477 insertions(+), 116 deletions(-)
 create mode 100644 include/efi_dt_fixup.h
 create mode 100644 lib/efi_loader/efi_dt_fixup.c

--
2.29.2

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] efi_loader: implement EFI_DT_FIXUP_PROTOCOL
  2020-12-13 21:48 [PATCH 0/2] efi_loader: implement EFI_DT_FIXUP_PROTOCOL Heinrich Schuchardt
@ 2020-12-13 21:48 ` Heinrich Schuchardt
  2020-12-13 21:48 ` [PATCH 2/2] efi_selftest: dtbdump support EFI_DT_FIXUP_PROTOCOL Heinrich Schuchardt
  1 sibling, 0 replies; 3+ messages in thread
From: Heinrich Schuchardt @ 2020-12-13 21:48 UTC (permalink / raw)
  To: u-boot

A boot manager like GRUB can use the protocol to

* apply U-Boot's fix-ups to the a device-tree
* let U-Boot make memory reservations according to the device-tree
* install the device-tree as a configuration table

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 cmd/bootefi.c                  |  80 -----------------
 cmd/efidebug.c                 |   5 ++
 include/efi_dt_fixup.h         |  39 ++++++++
 include/efi_loader.h           |   2 +
 lib/efi_loader/Makefile        |   3 +
 lib/efi_loader/efi_dt_fixup.c  | 160 +++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_root_node.c |   6 ++
 7 files changed, 215 insertions(+), 80 deletions(-)
 create mode 100644 include/efi_dt_fixup.h
 create mode 100644 lib/efi_loader/efi_dt_fixup.c

diff --git a/cmd/bootefi.c b/cmd/bootefi.c
index fdf909f8da..66159b237b 100644
--- a/cmd/bootefi.c
+++ b/cmd/bootefi.c
@@ -135,86 +135,6 @@ done:
 	return ret;
 }

-/**
- * efi_reserve_memory() - add reserved memory to memory map
- *
- * @addr:	start address of the reserved memory range
- * @size:	size of the reserved memory range
- * @nomap:	indicates that the memory range shall not be accessed by the
- *		UEFI payload
- */
-static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
-{
-	int type;
-	efi_uintn_t ret;
-
-	/* Convert from sandbox address space. */
-	addr = (uintptr_t)map_sysmem(addr, 0);
-
-	if (nomap)
-		type = EFI_RESERVED_MEMORY_TYPE;
-	else
-		type = EFI_BOOT_SERVICES_DATA;
-
-	ret = efi_add_memory_map(addr, size, type);
-	if (ret != EFI_SUCCESS)
-		log_err("Reserved memory mapping failed addr %llx size %llx\n",
-			addr, size);
-}
-
-/**
- * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
- *
- * The mem_rsv entries of the FDT are added to the memory map. Any failures are
- * ignored because this is not critical and we would rather continue to try to
- * boot.
- *
- * @fdt: Pointer to device tree
- */
-static void efi_carve_out_dt_rsv(void *fdt)
-{
-	int nr_rsv, i;
-	u64 addr, size;
-	int nodeoffset, subnode;
-
-	nr_rsv = fdt_num_mem_rsv(fdt);
-
-	/* Look for an existing entry and add it to the efi mem map. */
-	for (i = 0; i < nr_rsv; i++) {
-		if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
-			continue;
-		efi_reserve_memory(addr, size, false);
-	}
-
-	/* process reserved-memory */
-	nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
-	if (nodeoffset >= 0) {
-		subnode = fdt_first_subnode(fdt, nodeoffset);
-		while (subnode >= 0) {
-			fdt_addr_t fdt_addr;
-			fdt_size_t fdt_size;
-
-			/* check if this subnode has a reg property */
-			fdt_addr = fdtdec_get_addr_size_auto_parent(
-						fdt, nodeoffset, subnode,
-						"reg", 0, &fdt_size, false);
-			/*
-			 * The /reserved-memory node may have children with
-			 * a size instead of a reg property.
-			 */
-			if (fdt_addr != FDT_ADDR_T_NONE &&
-			    fdtdec_get_is_enabled(fdt, subnode)) {
-				bool nomap;
-
-				nomap = !!fdt_getprop(fdt, subnode, "no-map",
-						      NULL);
-				efi_reserve_memory(fdt_addr, fdt_size, nomap);
-			}
-			subnode = fdt_next_subnode(fdt, subnode);
-		}
-	}
-}
-
 /**
  * get_config_table() - get configuration table
  *
diff --git a/cmd/efidebug.c b/cmd/efidebug.c
index 5288b9920b..b9f76f6dd4 100644
--- a/cmd/efidebug.c
+++ b/cmd/efidebug.c
@@ -8,6 +8,7 @@
 #include <charset.h>
 #include <common.h>
 #include <command.h>
+#include <efi_dt_fixup.h>
 #include <efi_loader.h>
 #include <efi_rng.h>
 #include <exports.h>
@@ -261,6 +262,10 @@ static const struct {
 		"PXE Base Code",
 		EFI_PXE_BASE_CODE_PROTOCOL_GUID,
 	},
+	{
+		"Device-Tree Fixup",
+		EFI_DT_FIXUP_PROTOCOL_GUID,
+	},
 	/* Configuration table GUIDs */
 	{
 		"ACPI table",
diff --git a/include/efi_dt_fixup.h b/include/efi_dt_fixup.h
new file mode 100644
index 0000000000..9066e8dd8e
--- /dev/null
+++ b/include/efi_dt_fixup.h
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * EFI_DT_FIXUP_PROTOCOL
+ *
+ * Copyright (c) 2020 Heinrich Schuchardt
+ */
+
+#include <efi_api.h>
+
+#define EFI_DT_FIXUP_PROTOCOL_GUID \
+	EFI_GUID(0xe617d64c, 0xfe08, 0x46da, 0xf4, 0xdc, \
+		 0xbb, 0xd5, 0x87, 0x0c, 0x73, 0x00)
+
+#define EFI_DT_FIXUP_PROTOCOL_REVISION 0x00010000
+
+/* Add nodes and update properties */
+#define EFI_DT_APPLY_FIXUPS    0x00000001
+/*
+ * Reserve memory according to the /reserved-memory node
+ * and the memory reservation block
+ */
+#define EFI_DT_RESERVE_MEMORY  0x00000002
+/* Install the device-tree as configuration table */
+#define EFI_DT_INSTALL_TABLE   0x00000004
+
+#define EFI_DT_ALL (EFI_DT_APPLY_FIXUPS | \
+		    EFI_DT_RESERVE_MEMORY | \
+		    EFI_DT_INSTALL_TABLE)
+
+struct efi_dt_fixup_protocol {
+	u64 revision;
+	efi_status_t (EFIAPI *fixup) (struct efi_dt_fixup_protocol *this,
+				      void *dtb,
+				      efi_uintn_t *buffer_size,
+				      u32 flags);
+};
+
+extern struct efi_dt_fixup_protocol efi_dt_fixup_prot;
+extern const efi_guid_t efi_guid_dt_fixup_protocol;
diff --git a/include/efi_loader.h b/include/efi_loader.h
index 3c68b85b68..d291a962e0 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -402,6 +402,8 @@ void efi_runtime_detach(void);
 /* efi_convert_pointer() - convert pointer to virtual address */
 efi_status_t EFIAPI efi_convert_pointer(efi_uintn_t debug_disposition,
 					void **address);
+/* Carve out DT reserved memory ranges */
+void efi_carve_out_dt_rsv(void *fdt);
 /* Called by bootefi to make console interface available */
 efi_status_t efi_console_register(void);
 /* Called by bootefi to make all disk storage accessible as EFI objects */
diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
index cd4b252a41..814aa53f01 100644
--- a/lib/efi_loader/Makefile
+++ b/lib/efi_loader/Makefile
@@ -27,6 +27,9 @@ obj-y += efi_console.o
 obj-y += efi_device_path.o
 obj-$(CONFIG_EFI_DEVICE_PATH_TO_TEXT) += efi_device_path_to_text.o
 obj-y += efi_device_path_utilities.o
+ifeq ($(CONFIG_GENERATE_ACPI_TABLE),)
+obj-y += efi_dt_fixup.o
+endif
 obj-y += efi_file.o
 obj-$(CONFIG_EFI_LOADER_HII) += efi_hii.o efi_hii_config.o
 obj-y += efi_image_loader.o
diff --git a/lib/efi_loader/efi_dt_fixup.c b/lib/efi_loader/efi_dt_fixup.c
new file mode 100644
index 0000000000..5f0ae5c338
--- /dev/null
+++ b/lib/efi_loader/efi_dt_fixup.c
@@ -0,0 +1,160 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * EFI_DT_FIXUP_PROTOCOL
+ *
+ * Copyright (c) 2020 Heinrich Schuchardt
+ */
+
+#include <common.h>
+#include <efi_dt_fixup.h>
+#include <efi_loader.h>
+#include <mapmem.h>
+
+static efi_status_t EFIAPI efi_dt_fixup(struct efi_dt_fixup_protocol *this,
+					void *dtb,
+					efi_uintn_t *buffer_size,
+					u32 flags);
+
+struct efi_dt_fixup_protocol efi_dt_fixup_prot = {
+	.revision = EFI_DT_FIXUP_PROTOCOL_REVISION,
+	.fixup = efi_dt_fixup
+};
+
+const efi_guid_t efi_guid_dt_fixup_protocol = EFI_DT_FIXUP_PROTOCOL_GUID;
+
+/**
+ * efi_reserve_memory() - add reserved memory to memory map
+ *
+ * @addr:	start address of the reserved memory range
+ * @size:	size of the reserved memory range
+ * @nomap:	indicates that the memory range shall not be accessed by the
+ *		UEFI payload
+ */
+static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
+{
+	int type;
+	efi_uintn_t ret;
+
+	/* Convert from sandbox address space. */
+	addr = (uintptr_t)map_sysmem(addr, 0);
+
+	if (nomap)
+		type = EFI_RESERVED_MEMORY_TYPE;
+	else
+		type = EFI_BOOT_SERVICES_DATA;
+
+	ret = efi_add_memory_map(addr, size, type);
+	if (ret != EFI_SUCCESS)
+		log_err("Reserved memory mapping failed addr %llx size %llx\n",
+			addr, size);
+}
+
+/**
+ * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
+ *
+ * The mem_rsv entries of the FDT are added to the memory map. Any failures are
+ * ignored because this is not critical and we would rather continue to try to
+ * boot.
+ *
+ * @fdt: Pointer to device tree
+ */
+void efi_carve_out_dt_rsv(void *fdt)
+{
+	int nr_rsv, i;
+	u64 addr, size;
+	int nodeoffset, subnode;
+
+	nr_rsv = fdt_num_mem_rsv(fdt);
+
+	/* Look for an existing entry and add it to the efi mem map. */
+	for (i = 0; i < nr_rsv; i++) {
+		if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
+			continue;
+		efi_reserve_memory(addr, size, false);
+	}
+
+	/* process reserved-memory */
+	nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
+	if (nodeoffset >= 0) {
+		subnode = fdt_first_subnode(fdt, nodeoffset);
+		while (subnode >= 0) {
+			fdt_addr_t fdt_addr;
+			fdt_size_t fdt_size;
+
+			/* check if this subnode has a reg property */
+			fdt_addr = fdtdec_get_addr_size_auto_parent(
+						fdt, nodeoffset, subnode,
+						"reg", 0, &fdt_size, false);
+			/*
+			 * The /reserved-memory node may have children with
+			 * a size instead of a reg property.
+			 */
+			if (fdt_addr != FDT_ADDR_T_NONE &&
+			    fdtdec_get_is_enabled(fdt, subnode)) {
+				bool nomap;
+
+				nomap = !!fdt_getprop(fdt, subnode, "no-map",
+						      NULL);
+				efi_reserve_memory(fdt_addr, fdt_size, nomap);
+			}
+			subnode = fdt_next_subnode(fdt, subnode);
+		}
+	}
+}
+
+static efi_status_t EFIAPI efi_dt_fixup(struct efi_dt_fixup_protocol *this,
+					void *dtb,
+					efi_uintn_t *buffer_size,
+					u32 flags)
+{
+	efi_status_t ret;
+	size_t required_size;
+	bootm_headers_t img = { 0 };
+
+	EFI_ENTRY("%p, %p, %p, %d", this, dtb, buffer_size, flags);
+
+	if (this != &efi_dt_fixup_prot || !dtb || !buffer_size ||
+	    !flags || (flags & ~EFI_DT_ALL)) {
+		ret = EFI_INVALID_PARAMETER;
+		goto out;
+	}
+	if (fdt_check_header(dtb)) {
+		ret = EFI_INVALID_PARAMETER;
+		goto out;
+	}
+	if (flags & EFI_DT_APPLY_FIXUPS) {
+		required_size = fdt_off_dt_strings(dtb) +
+				fdt_size_dt_strings(dtb) +
+				0x3000;
+	} else {
+		required_size = fdt_totalsize(dtb);
+	}
+	if (required_size > *buffer_size) {
+		*buffer_size = required_size;
+		ret = EFI_BUFFER_TOO_SMALL;
+		goto out;
+	}
+	fdt_set_totalsize(dtb, *buffer_size);
+
+	if (flags & EFI_DT_APPLY_FIXUPS) {
+		if (image_setup_libfdt(&img, dtb, 0, NULL)) {
+			log_err("failed to process device tree\n");
+			ret = EFI_INVALID_PARAMETER;
+			goto out;
+		}
+	}
+	if (flags & EFI_DT_RESERVE_MEMORY)
+		efi_carve_out_dt_rsv(dtb);
+
+	if (EFI_DT_INSTALL_TABLE) {
+		ret = efi_install_configuration_table(&efi_guid_fdt, dtb);
+		if (ret != EFI_SUCCESS) {
+			log_err("ERROR: failed to install device tree\n");
+			goto out;
+		}
+	}
+
+	ret = EFI_SUCCESS;
+out:
+	return EFI_EXIT(ret);
+}
diff --git a/lib/efi_loader/efi_root_node.c b/lib/efi_loader/efi_root_node.c
index f68b0fdc61..649371a6a4 100644
--- a/lib/efi_loader/efi_root_node.c
+++ b/lib/efi_loader/efi_root_node.c
@@ -7,6 +7,7 @@

 #include <common.h>
 #include <malloc.h>
+#include <efi_dt_fixup.h>
 #include <efi_loader.h>

 const efi_guid_t efi_u_boot_guid = U_BOOT_GUID;
@@ -60,6 +61,11 @@ efi_status_t efi_root_node_register(void)
 			 /* Device path utilities protocol */
 			 &efi_guid_device_path_utilities_protocol,
 			 (void *)&efi_device_path_utilities,
+#if !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
+			 /* Device-tree fix-up protocol */
+			 &efi_guid_dt_fixup_protocol,
+			 (void *)&efi_dt_fixup_prot,
+#endif
 #if CONFIG_IS_ENABLED(EFI_UNICODE_COLLATION_PROTOCOL2)
 #if CONFIG_IS_ENABLED(EFI_UNICODE_COLLATION_PROTOCOL)
 			 /* Deprecated Unicode collation protocol */
--
2.29.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] efi_selftest: dtbdump support EFI_DT_FIXUP_PROTOCOL
  2020-12-13 21:48 [PATCH 0/2] efi_loader: implement EFI_DT_FIXUP_PROTOCOL Heinrich Schuchardt
  2020-12-13 21:48 ` [PATCH 1/2] " Heinrich Schuchardt
@ 2020-12-13 21:48 ` Heinrich Schuchardt
  1 sibling, 0 replies; 3+ messages in thread
From: Heinrich Schuchardt @ 2020-12-13 21:48 UTC (permalink / raw)
  To: u-boot

The dtbdump.efi binary can already be used to dump the configuration table
with the device-tree to a file.

With this patch a device-tree file can be loaded. The EFI_DT_FIXUP_PROTOCOL
is called to

* apply U-Boot's fix-ups
* let U-Boot make memory reservations as required by the device-tree
* install the new device-tree as configuration table

In a next step this configuration table can be dumped.

A dtbdump.efi session would look like:

    DTB Dump
    ========

    => load test.dtb
    device-tree installed
    => save fixed-up.dtb
    fixed-up.dtb written
    => exit

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 lib/efi_selftest/dtbdump.c | 298 ++++++++++++++++++++++++++++++++-----
 1 file changed, 262 insertions(+), 36 deletions(-)

diff --git a/lib/efi_selftest/dtbdump.c b/lib/efi_selftest/dtbdump.c
index d90e3eb768..737bc9d884 100644
--- a/lib/efi_selftest/dtbdump.c
+++ b/lib/efi_selftest/dtbdump.c
@@ -8,10 +8,12 @@

 #include <common.h>
 #include <efi_api.h>
+#include <efi_dt_fixup.h>

 #define BUFFER_SIZE 64
 #define ESC 0x17
-#define DEFAULT_FILENAME L"dtb.dtb"
+
+#define efi_size_in_pages(size) ((size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)

 static struct efi_simple_text_output_protocol *cerr;
 static struct efi_simple_text_output_protocol *cout;
@@ -21,6 +23,22 @@ static const efi_guid_t fdt_guid = EFI_FDT_GUID;
 static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
 static const efi_guid_t guid_simple_file_system_protocol =
 					EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
+static efi_handle_t handle;
+static struct efi_system_table *systable;
+static const efi_guid_t efi_dt_fixup_protocol_guid = EFI_DT_FIXUP_PROTOCOL_GUID;
+static const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID;
+
+/**
+ * error() - print error string
+ *
+ * @string:	error text
+ */
+static void error(u16 *string)
+{
+	cout->set_attribute(cout, EFI_LIGHTRED | EFI_BACKGROUND_BLACK);
+	cout->output_string(cout, string);
+	cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
+}

 /**
  * input() - read string from console
@@ -39,6 +57,7 @@ static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)

 	/* Drain the console input */
 	ret = cin->reset(cin, true);
+	*buffer = 0;
 	for (;;) {
 		ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
 		if (ret != EFI_SUCCESS)
@@ -62,6 +81,7 @@ static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)
 			break;
 		case 0x0a: /* Linefeed */
 		case 0x0d: /* Carriage return */
+			cout->output_string(cout, L"\n");
 			return EFI_SUCCESS;
 		default:
 			break;
@@ -73,6 +93,7 @@ static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)
 		    pos < buffer_size - 1) {
 			*outbuf = key.unicode_char;
 			buffer[pos++] = key.unicode_char;
+			buffer[pos] = 0;
 			cout->output_string(cout, outbuf);
 		}
 	}
@@ -117,60 +138,218 @@ void *get_dtb(struct efi_system_table *systable)
 }

 /**
- * efi_main() - entry point of the EFI application.
+ * skip_whitespace() - skip over leading whitespace
  *
- * @handle:	handle of the loaded image
- * @systable:	system table
- * @return:	status code
+ * @pos:	UTF-16 string
+ * Return:	pointer to first non-whitespace
  */
-efi_status_t EFIAPI efi_main(efi_handle_t handle,
-			     struct efi_system_table *systable)
+u16 *skip_whitespace(u16 *pos)
 {
-	efi_uintn_t ret;
-	u16 filename[BUFFER_SIZE] = {0};
-	efi_uintn_t dtb_size;
+	for (; *pos && *pos <= 0x20; ++pos)
+		;
+	return pos;
+}
+
+/**
+ * starts_with() - check if @string starts with @keyword
+ *
+ * @string:	string to search for keyword
+ * @keyword:	keyword to be searched
+ * Return:	true fi @string starts with the keyword
+ */
+bool starts_with(u16 *string, u16 *keyword)
+{
+	for (; *keyword; ++string, ++keyword) {
+		if (*string != *keyword)
+			return false;
+	}
+	return true;
+}
+
+/**
+ * do_help() - print help
+ */
+void do_help(void)
+{
+	error(L"load <dtb> - load device-tree from file\n");
+	error(L"save <dtb> - save device-tree to file\n");
+	error(L"exit       - exit the shell\n");
+}
+
+/**
+ * do_load() - load and install device-tree
+ *
+ * @filename:	file name
+ * Return:	status code
+ */
+efi_status_t do_load(u16 *filename)
+{
+	struct efi_dt_fixup_protocol *dt_fixup_prot;
 	struct efi_loaded_image *loaded_image;
 	struct efi_simple_file_system_protocol *file_system;
+	efi_uintn_t buffer_size;
 	struct efi_file_handle *root, *file;
 	struct fdt_header *dtb;
+	struct efi_file_info *info;
+	u64 addr = 0;
+	efi_uintn_t pages;
+	efi_status_t ret;

-	cerr = systable->std_err;
-	cout = systable->con_out;
-	cin = systable->con_in;
-	bs = systable->boottime;
+	ret = bs->locate_protocol(&efi_dt_fixup_protocol_guid, NULL,
+				  (void **)&dt_fixup_prot);
+	if (ret != EFI_SUCCESS)
+		error(L"Device-tree fix-up protocol not found\n");

-	cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
-	cout->clear_screen(cout);
-	cout->set_attribute(cout, EFI_YELLOW | EFI_BACKGROUND_BLACK);
-	cout->output_string(cout, L"DTB Dump\n\n");
-	cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
+	filename = skip_whitespace(filename);
+
+	ret = bs->open_protocol(handle, &loaded_image_guid,
+				(void **)&loaded_image, NULL, NULL,
+				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+	if (ret != EFI_SUCCESS) {
+		error(L"Loaded image protocol not found\n");
+		return ret;
+	}
+
+	/* Open the simple file system protocol */
+	ret = bs->open_protocol(loaded_image->device_handle,
+				&guid_simple_file_system_protocol,
+				(void **)&file_system, NULL, NULL,
+				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+	if (ret != EFI_SUCCESS) {
+		error(L"Failed to open simple file system protocol\n");
+		return ret;
+	}
+
+	/* Open volume */
+	ret = file_system->open_volume(file_system, &root);
+	if (ret != EFI_SUCCESS) {
+		error(L"Failed to open volume\n");
+		return ret;
+	}
+
+	/* Create file */
+	ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0);
+	if (ret != EFI_SUCCESS) {
+		error(L"File not found\n");
+		return ret;
+	}
+	/* Get file size */
+	buffer_size = 0;
+	ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, NULL);
+	if (ret != EFI_BUFFER_TOO_SMALL) {
+		error(L"Can't get file info size\n");
+		goto out;
+	}
+	ret = bs->allocate_pool(EFI_LOADER_DATA, buffer_size, (void **)&info);
+	if (ret != EFI_SUCCESS) {
+		error(L"Out of memory\n");
+		goto out;
+	}
+	ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, info);
+	if (ret != EFI_SUCCESS) {
+		error(L"Can't get file info\n");
+		goto out;
+	}
+	buffer_size = info->file_size;
+	pages = efi_size_in_pages(buffer_size);
+	ret = bs->free_pool(info);
+	if (ret != EFI_SUCCESS)
+		error(L"Can't free memory pool\n");
+	/* Read file */
+	ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
+				 EFI_ACPI_RECLAIM_MEMORY,
+				 pages, &addr);
+	if (ret != EFI_SUCCESS) {
+		error(L"Out of memory\n");
+		goto out;
+	}
+	dtb = (struct fdt_header *)addr;
+	ret = file->read(file, &buffer_size, dtb);
+	if (ret != EFI_SUCCESS) {
+		error(L"Can't read file\n");
+		goto out;
+	}
+	/* Fixup file, expecting EFI_BUFFER_TOO_SMALL */
+	ret = dt_fixup_prot->fixup(dt_fixup_prot, dtb, &buffer_size,
+				   EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY |
+				   EFI_DT_INSTALL_TABLE);
+	if (ret == EFI_BUFFER_TOO_SMALL) {
+		/* Read file into larger buffer */
+		ret = bs->free_pages(addr, pages);
+		if (ret != EFI_SUCCESS)
+			error(L"Can't free memory pages\n");
+		pages = efi_size_in_pages(buffer_size);
+		ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
+					 EFI_ACPI_RECLAIM_MEMORY,
+					 pages, &addr);
+		if (ret != EFI_SUCCESS) {
+			error(L"Out of memory\n");
+			goto out;
+		}
+		dtb = (struct fdt_header *)addr;
+		ret = file->setpos(file, 0);
+		if (ret != EFI_SUCCESS) {
+			error(L"Can't position file\n");
+			goto out;
+		}
+		ret = file->read(file, &buffer_size, dtb);
+		if (ret != EFI_SUCCESS) {
+			error(L"Can't read file\n");
+			goto out;
+		}
+		buffer_size = pages << EFI_PAGE_SHIFT;
+		ret = dt_fixup_prot->fixup(
+				dt_fixup_prot, dtb, &buffer_size,
+				EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY |
+				EFI_DT_INSTALL_TABLE);
+	}
+	if (ret == EFI_SUCCESS)
+		cout->output_string(cout, L"device-tree installed\n");
+	else
+		error(L"Device-tree fix-up failed\n");
+out:
+	if (addr) {
+		ret = bs->free_pages(addr, pages);
+		if (ret != EFI_SUCCESS)
+			error(L"Can't free memory pages\n");
+	}
+	file->close(file);
+	return ret;
+}
+
+/**
+ * do_save() - save current device-tree
+ *
+ * @filename:	file name
+ * Return:	status code
+ */
+efi_status_t do_save(u16 *filename)
+{
+	struct efi_loaded_image *loaded_image;
+	struct efi_simple_file_system_protocol *file_system;
+	efi_uintn_t dtb_size;
+	struct efi_file_handle *root, *file;
+	struct fdt_header *dtb;
+	efi_uintn_t ret;

 	dtb = get_dtb(systable);
 	if (!dtb) {
-		cerr->output_string(cout, L"DTB not found\n");
+		error(L"DTB not found\n");
 		return EFI_NOT_FOUND;
 	}
 	if (f2h(dtb->magic) != FDT_MAGIC) {
-		cerr->output_string(cout, L"Wrong device tree magic\n");
+		error(L"Wrong device tree magic\n");
 		return EFI_NOT_FOUND;
 	}
 	dtb_size = f2h(dtb->totalsize);

-	cout->output_string(cout, L"Filename (" DEFAULT_FILENAME ")?\n");
-	ret = efi_input(filename, sizeof(filename));
-	if (ret != EFI_SUCCESS)
-		return ret;
-	if (!*filename)
-		memcpy(filename, DEFAULT_FILENAME, sizeof(DEFAULT_FILENAME));
-
-	cout->output_string(cout, L"\n");
+	filename = skip_whitespace(filename);

 	ret = bs->open_protocol(handle, &loaded_image_guid,
 				(void **)&loaded_image, NULL, NULL,
 				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
 	if (ret != EFI_SUCCESS) {
-		cerr->output_string(cout,
-				    L"Loaded image protocol not found\n");
+		error(L"Loaded image protocol not found\n");
 		return ret;
 	}

@@ -180,15 +359,14 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle,
 				(void **)&file_system, NULL, NULL,
 				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
 	if (ret != EFI_SUCCESS) {
-		cerr->output_string(
-			cout, L"Failed to open simple file system protocol\n");
+		error(L"Failed to open simple file system protocol\n");
 		return ret;
 	}

 	/* Open volume */
 	ret = file_system->open_volume(file_system, &root);
 	if (ret != EFI_SUCCESS) {
-		cerr->output_string(cerr, L"Failed to open volume\n");
+		error(L"Failed to open volume\n");
 		return ret;
 	}
 	/* Create file */
@@ -199,10 +377,10 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle,
 		/* Write file */
 		ret = file->write(file, &dtb_size, dtb);
 		if (ret != EFI_SUCCESS)
-			cerr->output_string(cerr, L"Failed to write file\n");
+			error(L"Failed to write file\n");
 		file->close(file);
 	} else {
-		cerr->output_string(cerr, L"Failed to open file\n");
+		error(L"Failed to open file\n");
 	}
 	root->close(root);

@@ -213,3 +391,51 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle,

 	return ret;
 }
+
+/**
+ * efi_main() - entry point of the EFI application.
+ *
+ * @handle:	handle of the loaded image
+ * @systab:	system table
+ * @return:	status code
+ */
+efi_status_t EFIAPI efi_main(efi_handle_t image_handle,
+			     struct efi_system_table *systab)
+{
+	handle = image_handle;
+	systable = systab;
+	cerr = systable->std_err;
+	cout = systable->con_out;
+	cin = systable->con_in;
+	bs = systable->boottime;
+
+	cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
+	cout->clear_screen(cout);
+	cout->set_attribute(cout, EFI_WHITE | EFI_BACKGROUND_BLACK);
+	cout->output_string(cout, L"DTB Dump\n========\n\n");
+	cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK);
+
+	for (;;) {
+		u16 command[BUFFER_SIZE];
+		u16 *pos;
+		efi_uintn_t ret;
+
+		cout->output_string(cout, L"=> ");
+		ret = efi_input(command, sizeof(command));
+		if (ret == EFI_ABORTED)
+			break;
+		pos = skip_whitespace(command);
+		if (starts_with(pos, L"exit"))
+			break;
+		else if (starts_with(pos, L"load "))
+			do_load(pos + 5);
+		else if (starts_with(pos, L"save "))
+			do_save(pos + 5);
+		else
+			do_help();
+	}
+
+	cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK);
+	cout->clear_screen(cout);
+	return EFI_SUCCESS;
+}
--
2.29.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-12-13 21:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-13 21:48 [PATCH 0/2] efi_loader: implement EFI_DT_FIXUP_PROTOCOL Heinrich Schuchardt
2020-12-13 21:48 ` [PATCH 1/2] " Heinrich Schuchardt
2020-12-13 21:48 ` [PATCH 2/2] efi_selftest: dtbdump support EFI_DT_FIXUP_PROTOCOL Heinrich Schuchardt

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.