/flash@0 on ARM64 may not always exist: - Older Qemu versions place the flash at /soc/flash@0 - With secure=on, /flash@0 is renamed to /secflash@0 and is off-limits to barebox running in the normal world Solve both issues by only applying the overlay if the node being partitioned actually exists and is not disabled in the DT. Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx> --- common/boards/qemu-virt/Makefile | 4 ++-- common/boards/qemu-virt/board.c | 14 ++++++++++--- common/boards/qemu-virt/qemu-virt-flash.dtso | 12 +++-------- common/boards/qemu-virt/qemu-virt-flash.h | 22 ++++++++++++++++++++ 4 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 common/boards/qemu-virt/qemu-virt-flash.h diff --git a/common/boards/qemu-virt/Makefile b/common/boards/qemu-virt/Makefile index b6883ff7672e..30bf4f1955ee 100644 --- a/common/boards/qemu-virt/Makefile +++ b/common/boards/qemu-virt/Makefile @@ -3,10 +3,10 @@ obj-y += board.o obj-y += qemu-virt-flash.dtbo.o fitimage-pubkey.dtb.o ifeq ($(CONFIG_RISCV),y) -DTC_CPP_FLAGS_qemu-virt-flash.dtbo := -DRISCV_VIRT=1 +DTC_CPP_FLAGS_qemu-virt-flash.dtbo := -DCONFIG_RISCV endif ifeq ($(CONFIG_ARM),y) -DTC_CPP_FLAGS_qemu-virt-flash.dtbo := -DARM_VIRT=1 +DTC_CPP_FLAGS_qemu-virt-flash.dtbo := -DCONFIG_ARM endif clean-files := *.dtb *.dtb.S .*.dtc .*.pre .*.dts *.dtb.z diff --git a/common/boards/qemu-virt/board.c b/common/boards/qemu-virt/board.c index 4c6df5e30252..b9df129834d9 100644 --- a/common/boards/qemu-virt/board.c +++ b/common/boards/qemu-virt/board.c @@ -7,6 +7,7 @@ #include <init.h> #include <of.h> #include <deep-probe.h> +#include "qemu-virt-flash.h" #ifdef CONFIG_64BIT #define MACHINE "virt64" @@ -53,7 +54,7 @@ BAREBOX_DEEP_PROBE_ENABLE(virt_of_match); static int virt_board_driver_init(void) { struct device_node *root = of_get_root_node(); - struct device_node *overlay, *pubkey; + struct device_node *flash, *overlay, *pubkey; const struct of_device_id *id; void (*init)(void); @@ -66,8 +67,15 @@ static int virt_board_driver_init(void) init(); } - overlay = of_unflatten_dtb(__dtbo_qemu_virt_flash_start, INT_MAX); - of_overlay_apply_tree(root, overlay); + /* + * Catch both old Qemu versions that place /flash in /soc and + * configurations, where the first flash bank is secure-world only + */ + flash = of_find_node_by_path(PARTS_TARGET_PATH_STR); + if (flash && of_device_is_available(flash)) { + overlay = of_unflatten_dtb(__dtbo_qemu_virt_flash_start, INT_MAX); + of_overlay_apply_tree(root, overlay); + } pubkey = of_unflatten_dtb(__dtb_fitimage_pubkey_start, INT_MAX); of_merge_nodes(root, pubkey); diff --git a/common/boards/qemu-virt/qemu-virt-flash.dtso b/common/boards/qemu-virt/qemu-virt-flash.dtso index 16b1c7923d58..087568a26d2a 100644 --- a/common/boards/qemu-virt/qemu-virt-flash.dtso +++ b/common/boards/qemu-virt/qemu-virt-flash.dtso @@ -3,16 +3,10 @@ /dts-v1/; /plugin/; -#ifdef RISCV_VIRT -#define PARTS_TARGET_PATH /flash@20000000 -#define ENV_DEVICE_PATH "/flash@20000000/partitions/partition@3c00000" -#elif defined ARM_VIRT -#define PARTS_TARGET_PATH /flash@0 -#define ENV_DEVICE_PATH "/flash@0/partitions/partition@3c00000" -#endif +#include "qemu-virt-flash.h" &{PARTS_TARGET_PATH} { -#ifdef ARM_VIRT +#ifdef CONFIG_ARM virtual-reg = <0x1000>; #endif partitions { @@ -40,7 +34,7 @@ backend_state_flash: partition@3e00000 { &{/chosen} { environment { compatible = "barebox,environment"; - device-path = ENV_DEVICE_PATH; + device-path = ENV_DEVICE_PATH_STR; }; }; diff --git a/common/boards/qemu-virt/qemu-virt-flash.h b/common/boards/qemu-virt/qemu-virt-flash.h new file mode 100644 index 000000000000..85f67ff03057 --- /dev/null +++ b/common/boards/qemu-virt/qemu-virt-flash.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef __QEMU_VIRT_FLASH_H__ +#define __QEMU_VIRT_FLASH_H__ + +#include <linux/stringify.h> + +#ifdef CONFIG_RISCV +#define PARTS_TARGET_PATH /flash@20000000 +#define ENV_DEVICE_PATH /flash@20000000/partitions/partition@3c00000 +#elif defined CONFIG_ARM +#define PARTS_TARGET_PATH /flash@0 +#define ENV_DEVICE_PATH /flash@0/partitions/partition@3c00000 +#else +#define PARTS_TARGET_PATH +#define ENV_DEVICE_PATH +#endif + +#define PARTS_TARGET_PATH_STR __stringify(PARTS_TARGET_PATH) +#define ENV_DEVICE_PATH_STR __stringify(ENV_DEVICE_PATH) + +#endif -- 2.39.2