U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v9 15/18] efi: sandbox: Tidy up copy_fdt() to work with sandbox
Date: Wed,  8 Aug 2018 03:54:30 -0600	[thread overview]
Message-ID: <20180808095433.230882-16-sjg@chromium.org> (raw)
In-Reply-To: <20180808095433.230882-1-sjg@chromium.org>

At present this function takes a pointer as its argument, then passes this
to efi_allocate_pages(), which actually takes an address. It uses casts,
which are not supported on sandbox.

Also the function calculates the FDT size rounded up to the neared EFI
page size, then its caller recalculates the size and adds a bit more to
it.

This function is much better written as something that works with
addresses only, and returns both the address and the size of the relocated
FDT.

Also, copy_fdt() returns NULL on error, but really should propagate the
error from efi_allocate_pages(). To do this it needs to return an
efi_status_t, not a void *.

Update the code in this way, so that it is easier to follow, and also
supports sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v9:
- Drop fdt_end variable in efi_install_fdt()

Changes in v8: None
Changes in v7: None
Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 cmd/bootefi.c | 79 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 50 insertions(+), 29 deletions(-)

diff --git a/cmd/bootefi.c b/cmd/bootefi.c
index 6481444cca6..18378f2eacc 100644
--- a/cmd/bootefi.c
+++ b/cmd/bootefi.c
@@ -131,17 +131,30 @@ static void set_load_options(struct efi_loaded_image *loaded_image_info,
 	loaded_image_info->load_options_size = size * 2;
 }
 
-static void *copy_fdt(void *fdt)
+/**
+ * copy_fdt() - Copy the device tree to a new location available to EFI
+ *
+ * The FDT is relocated into a suitable location within the EFI memory map.
+ * An additional 12KB is added to the space in case the device tree needs to be
+ * expanded later with fdt_open_into().
+ *
+ * @fdt_addr: On entry, address of start of FDT. On exit, address of relocated
+ *	FDT start
+ * @fdt_sizep: Returns new size of FDT, including
+ * @return new relocated address of FDT
+ */
+static efi_status_t copy_fdt(ulong *fdt_addrp, ulong *fdt_sizep)
 {
-	u64 fdt_size = fdt_totalsize(fdt);
 	unsigned long fdt_ram_start = -1L, fdt_pages;
+	efi_status_t ret = 0;
+	void *fdt, *new_fdt;
 	u64 new_fdt_addr;
-	void *new_fdt;
+	uint fdt_size;
 	int i;
 
-        for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
-                u64 ram_start = gd->bd->bi_dram[i].start;
-                u64 ram_size = gd->bd->bi_dram[i].size;
+	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
+		u64 ram_start = gd->bd->bi_dram[i].start;
+		u64 ram_size = gd->bd->bi_dram[i].size;
 
 		if (!ram_size)
 			continue;
@@ -154,30 +167,37 @@ static void *copy_fdt(void *fdt)
 	 * Give us at least 4KB of breathing room in case the device tree needs
 	 * to be expanded later. Round up to the nearest EFI page boundary.
 	 */
-	fdt_size += 4096;
+	fdt = map_sysmem(*fdt_addrp, 0);
+	fdt_size = fdt_totalsize(fdt);
+	fdt_size += 4096 * 3;
 	fdt_size = ALIGN(fdt_size + EFI_PAGE_SIZE - 1, EFI_PAGE_SIZE);
 	fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
 
 	/* Safe fdt location is at 127MB */
 	new_fdt_addr = fdt_ram_start + (127 * 1024 * 1024) + fdt_size;
-	if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
-			       EFI_RUNTIME_SERVICES_DATA, fdt_pages,
-			       &new_fdt_addr) != EFI_SUCCESS) {
+	ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
+				 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
+				 &new_fdt_addr);
+	if (ret != EFI_SUCCESS) {
 		/* If we can't put it there, put it somewhere */
 		new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
-		if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
-				       EFI_RUNTIME_SERVICES_DATA, fdt_pages,
-				       &new_fdt_addr) != EFI_SUCCESS) {
+		ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
+					 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
+					 &new_fdt_addr);
+		if (ret != EFI_SUCCESS) {
 			printf("ERROR: Failed to reserve space for FDT\n");
-			return NULL;
+			goto done;
 		}
 	}
 
-	new_fdt = (void*)(ulong)new_fdt_addr;
+	new_fdt = map_sysmem(new_fdt_addr, fdt_size);
 	memcpy(new_fdt, fdt, fdt_totalsize(fdt));
 	fdt_set_totalsize(new_fdt, fdt_size);
 
-	return new_fdt;
+	*fdt_addrp = new_fdt_addr;
+	*fdt_sizep = fdt_size;
+done:
+	return ret;
 }
 
 static efi_status_t efi_do_enter(
@@ -250,22 +270,27 @@ static void efi_carve_out_dt_rsv(void *fdt)
 	}
 }
 
-static efi_status_t efi_install_fdt(void *fdt)
+static efi_status_t efi_install_fdt(ulong fdt_addr)
 {
 	bootm_headers_t img = { 0 };
-	ulong fdt_pages, fdt_size, fdt_start, fdt_end;
+	ulong fdt_pages, fdt_size, fdt_start;
 	efi_status_t ret;
+	void *fdt;
 
+	fdt = map_sysmem(fdt_addr, 0);
 	if (fdt_check_header(fdt)) {
 		printf("ERROR: invalid device tree\n");
 		return EFI_INVALID_PARAMETER;
 	}
 
 	/* Prepare fdt for payload */
-	fdt = copy_fdt(fdt);
-	if (!fdt)
-		return EFI_OUT_OF_RESOURCES;
+	ret = copy_fdt(&fdt_addr, &fdt_size);
+	if (ret)
+		return ret;
 
+	unmap_sysmem(fdt);
+	fdt = map_sysmem(fdt_addr, 0);
+	fdt_size = fdt_totalsize(fdt);
 	if (image_setup_libfdt(&img, fdt, 0, NULL)) {
 		printf("ERROR: failed to process device tree\n");
 		return EFI_LOAD_ERROR;
@@ -279,14 +304,12 @@ static efi_status_t efi_install_fdt(void *fdt)
 		return EFI_OUT_OF_RESOURCES;
 
 	/* And reserve the space in the memory map */
-	fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
-	fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
-	fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
+	fdt_start = fdt_addr;
 	fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
-	/* Give a bootloader the chance to modify the device tree */
-	fdt_pages += 2;
+
 	ret = efi_add_memory_map(fdt_start, fdt_pages,
 				 EFI_BOOT_SERVICES_DATA, true);
+
 	return ret;
 }
 
@@ -506,7 +529,6 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	unsigned long addr;
 	efi_status_t r;
 	unsigned long fdt_addr;
-	void *fdt;
 
 	/* Allow unaligned memory access */
 	allow_unaligned();
@@ -527,8 +549,7 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		if (!fdt_addr && *argv[2] != '0')
 			return CMD_RET_USAGE;
 		/* Install device tree */
-		fdt = map_sysmem(fdt_addr, 0);
-		r = efi_install_fdt(fdt);
+		r = efi_install_fdt(fdt_addr);
 		if (r != EFI_SUCCESS) {
 			printf("ERROR: failed to install device tree\n");
 			return CMD_RET_FAILURE;
-- 
2.18.0.597.ga71716f1ad-goog

  parent reply	other threads:[~2018-08-08  9:54 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-08  9:54 [U-Boot] [PATCH v9 00/18] efi: Enable sandbox support for EFI loader Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 01/18] Revert "efi_loader: Rename sections to allow for implicit data" Simon Glass
2018-08-23 20:18   ` [U-Boot] [U-Boot, v9, 01/18] Partially revert " Tom Rini
2018-08-08  9:54 ` [U-Boot] [PATCH v9 02/18] efi: Don't allow CMD_BOOTEFI_SELFTEST on sandbox Simon Glass
2018-08-26 16:53   ` Alexander Graf
2018-09-14 15:46     ` Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 03/18] efi: sandbox: Add distroboot support Simon Glass
2018-08-26 16:54   ` Alexander Graf
2018-08-08  9:54 ` [U-Boot] [PATCH v9 04/18] efi: sandbox: Enable EFI loader build for sandbox Simon Glass
2018-08-26 16:55   ` Alexander Graf
2018-09-14 15:46     ` Simon Glass
2018-09-15  8:11       ` Alexander Graf
2018-09-15  8:32         ` Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 05/18] efi: Split out test init/uninit into functions Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 06/18] efi: sandbox: Add a simple 'bootefi test' command Simon Glass
2018-08-26 16:58   ` Alexander Graf
2018-09-14 15:46     ` Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 07/18] efi: Create a function to set up for running EFI code Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 08/18] efi: Rename bootefi_test_finish() to bootefi_run_finish() Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 09/18] sandbox: Align RAM buffer to the machine page size Simon Glass
2018-08-26 17:01   ` Alexander Graf
2018-08-08  9:54 ` [U-Boot] [PATCH v9 10/18] sandbox: Try to start the RAM buffer at a particular address Simon Glass
2018-08-26 17:01   ` Alexander Graf
2018-09-14 15:46     ` Simon Glass
2018-10-15 20:07       ` Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 11/18] sandbox: Add support for calling abort() Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 12/18] sandbox: Enhance map_to_sysmem() to handle foreign pointers Simon Glass
2018-08-26 17:11   ` Alexander Graf
2018-09-14 15:46     ` Simon Glass
2018-09-15  8:16       ` Alexander Graf
2018-09-15  8:31         ` Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 13/18] efi: Add a call to exit() along with why we can't use it Simon Glass
2018-08-23 20:37   ` Heinrich Schuchardt
2018-08-26 17:13     ` Alexander Graf
2018-09-14 15:46       ` Simon Glass
2018-08-08  9:54 ` [U-Boot] [PATCH v9 14/18] efi: Relocate FDT to 127MB instead of 128MB Simon Glass
2018-08-26 17:23   ` [U-Boot] [U-Boot, v9, " Alexander Graf
2018-08-08  9:54 ` Simon Glass [this message]
2018-08-26 17:23   ` [U-Boot] [U-Boot, v9, 15/18] efi: sandbox: Tidy up copy_fdt() to work with sandbox Alexander Graf
2018-08-08  9:54 ` [U-Boot] [PATCH v9 16/18] efi: Add more debugging for memory allocations Simon Glass
2018-08-23 20:49   ` Heinrich Schuchardt
2018-08-26 18:27     ` Simon Glass
2018-08-26 17:22   ` Alexander Graf
2018-08-08  9:54 ` [U-Boot] [PATCH v9 17/18] efi_loader: Pass address to fs_read() Simon Glass
2018-08-26 17:23   ` [U-Boot] [U-Boot, v9, " Alexander Graf
2018-08-08  9:54 ` [U-Boot] [PATCH v9 18/18] efi: sandbox: Enable selftest command Simon Glass
2018-08-26 17:28 ` [U-Boot] [PATCH v9 00/18] efi: Enable sandbox support for EFI loader Alexander Graf
2018-09-14 15:46   ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180808095433.230882-16-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox