[U-Boot] [PATCH] efi_loader: Respect DT reserved regions

With legacy boot (booti, bootz), people can declare memory regions as reserved using device tree memory reservations. This feature is some times used to indicate memory regions that should not be touched.
Since in a UEFI world, the DT memory reservations do not get honored, let's copy them into the UEFI memory map so everyone has a coherent view of the world and we give people the chance to add reservations on demand.
Signed-off-by: Alexander Graf agraf@suse.de --- cmd/bootefi.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
diff --git a/cmd/bootefi.c b/cmd/bootefi.c index 2a31a914cd..5a2a81005f 100644 --- a/cmd/bootefi.c +++ b/cmd/bootefi.c @@ -178,6 +178,27 @@ static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)( } #endif
+/* Carve out DT reserved memory ranges */ +static efi_status_t efi_carve_out_dt_rsv(void *fdt) +{ + int nr_rsv, i; + uint64_t addr, size, pages; + + 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; + + pages = ALIGN(size, EFI_PAGE_SIZE) >> EFI_PAGE_SHIFT; + efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE, + false); + } + + return EFI_SUCCESS; +} + static efi_status_t efi_install_fdt(void *fdt) { bootm_headers_t img = { 0 }; @@ -199,6 +220,11 @@ static efi_status_t efi_install_fdt(void *fdt) return EFI_LOAD_ERROR; }
+ if (efi_carve_out_dt_rsv(fdt) != EFI_SUCCESS) { + printf("ERROR: failed to carve out memory\n"); + return EFI_LOAD_ERROR; + } + /* Link to it in the efi tables */ ret = efi_install_configuration_table(&efi_guid_fdt, fdt); if (ret != EFI_SUCCESS)

From: Alexander Graf agraf@suse.de Date: Fri, 6 Apr 2018 09:42:47 +0200
With legacy boot (booti, bootz), people can declare memory regions as reserved using device tree memory reservations. This feature is some times used to indicate memory regions that should not be touched.
Since in a UEFI world, the DT memory reservations do not get honored, let's copy them into the UEFI memory map so everyone has a coherent view of the world and we give people the chance to add reservations on demand.
This won't fix the issue raised here:
https://lists.denx.de/pipermail/u-boot/2018-March/324336.html
but would open up yet another way to solve that issue.
But with my analysis of that issue still in the back of my mind I have a question. What happens with regions added by U-Boot itself through fdt_add_mem_rsv() calls? Also note that the is code in arch/arm/mach-meson/board.c that explicitly calls efi_add_memory_map() to reserve memory it adds using fdt_add_mem_rsv().
cmd/bootefi.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
diff --git a/cmd/bootefi.c b/cmd/bootefi.c index 2a31a914cd..5a2a81005f 100644 --- a/cmd/bootefi.c +++ b/cmd/bootefi.c @@ -178,6 +178,27 @@ static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)( } #endif
+/* Carve out DT reserved memory ranges */ +static efi_status_t efi_carve_out_dt_rsv(void *fdt) +{
- int nr_rsv, i;
- uint64_t addr, size, pages;
- 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;
pages = ALIGN(size, EFI_PAGE_SIZE) >> EFI_PAGE_SHIFT;
efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
false);
- }
- return EFI_SUCCESS;
+}
static efi_status_t efi_install_fdt(void *fdt) { bootm_headers_t img = { 0 }; @@ -199,6 +220,11 @@ static efi_status_t efi_install_fdt(void *fdt) return EFI_LOAD_ERROR; }
- if (efi_carve_out_dt_rsv(fdt) != EFI_SUCCESS) {
printf("ERROR: failed to carve out memory\n");
return EFI_LOAD_ERROR;
- }
- /* Link to it in the efi tables */ ret = efi_install_configuration_table(&efi_guid_fdt, fdt); if (ret != EFI_SUCCESS)
-- 2.12.3
U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot

On 06.04.18 11:25, Mark Kettenis wrote:
From: Alexander Graf agraf@suse.de Date: Fri, 6 Apr 2018 09:42:47 +0200
With legacy boot (booti, bootz), people can declare memory regions as reserved using device tree memory reservations. This feature is some times used to indicate memory regions that should not be touched.
Since in a UEFI world, the DT memory reservations do not get honored, let's copy them into the UEFI memory map so everyone has a coherent view of the world and we give people the chance to add reservations on demand.
This won't fix the issue raised here:
https://lists.denx.de/pipermail/u-boot/2018-March/324336.html
Oh, I must've missed that email.
but would open up yet another way to solve that issue.
My main goal with this is to allow memory reservation overrides for platforms where people don't want to or can not touch U-Boot source code.
Doing explicit reservations in board files is obviously the much better alternative.
But with my analysis of that issue still in the back of my mind I have a question. What happens with regions added by U-Boot itself through fdt_add_mem_rsv() calls? Also note that the is code in arch/arm/mach-meson/board.c that explicitly calls efi_add_memory_map() to reserve memory it adds using fdt_add_mem_rsv().
There are way too many layers around for memory reservations :).
We have implicit memory reservations that get handled through CONFIG_LMB. These are not mirrored into the EFI memory map FWIW.
Explicit calls to fdt_add_mem_rsv() with this patch also get mirrored into the EFI memory map *if* a DT is used. That means in this case calls to both end up redundant. I don't think that's an issue - especially given that maybe not everyone wants to boot using DT.
Alex

On 04/06/2018 11:25 AM, Mark Kettenis wrote:
From: Alexander Graf agraf@suse.de Date: Fri, 6 Apr 2018 09:42:47 +0200
With legacy boot (booti, bootz), people can declare memory regions as reserved using device tree memory reservations. This feature is some times used to indicate memory regions that should not be touched.
Since in a UEFI world, the DT memory reservations do not get honored, let's copy them into the UEFI memory map so everyone has a coherent view of the world and we give people the chance to add reservations on demand.
This won't fix the issue raised here:
https://lists.denx.de/pipermail/u-boot/2018-March/324336.html
Hello Mark,
take a look at meson_board_add_reserved_memory().
Best regards
Heinrich
participants (3)
-
Alexander Graf
-
Heinrich Schuchardt
-
Mark Kettenis