From b186266cfa32023734c6412739a8e06c1aacb992 Mon Sep 17 00:00:00 2001 From: Florian Grandel Date: Fri, 27 Sep 2024 07:56:58 +0200 Subject: [PATCH] drivers: flash: simulator: simulate pre-provisioned flash Enables the flash simulator to simulate pre-provisioned flash with QEMU (or any other application using the flash simulator). Signed-off-by: Florian Grandel --- cmake/emu/qemu.cmake | 6 ++++++ drivers/flash/Kconfig.simulator | 6 ++++++ drivers/flash/flash_simulator.c | 8 +++++++- include/zephyr/kernel/internal/mm.h | 3 +++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cmake/emu/qemu.cmake b/cmake/emu/qemu.cmake index cc47f48d1ffea6..71095eb5bbac7e 100644 --- a/cmake/emu/qemu.cmake +++ b/cmake/emu/qemu.cmake @@ -391,6 +391,12 @@ else() add_custom_target(qemu_nvme_disk) endif() +if(CONFIG_FLASH_SIMULATOR_PROVISION) + list(APPEND QEMU_EXTRA_FLAGS + -device loader,file=${CMAKE_CURRENT_BINARY_DIR}/soc-nv-flash-image.hex + ) +endif() + if(NOT QEMU_PIPE) set(QEMU_PIPE_COMMENT "\nTo exit from QEMU enter: 'CTRL+a, x'\n") endif() diff --git a/drivers/flash/Kconfig.simulator b/drivers/flash/Kconfig.simulator index 9c51f66c1a06d9..53303c86c7c25c 100644 --- a/drivers/flash/Kconfig.simulator +++ b/drivers/flash/Kconfig.simulator @@ -49,6 +49,12 @@ config FLASH_SIMULATOR_DOUBLE_WRITES In case when FLASH_SIMULATOR_EXPLICIT_ERASE is selected multiple writes to the same bit but only change from erase-value to opposite will be registered. +config FLASH_SIMULATOR_PROVISION + bool "Initialize flash in QEMU from an image file to simulate provisioning." + help + This option maps simulated flash to a memory file on QEMU targets. This + file can be a pre-built binary image so that flash will be pre-populated. + DT_COMPAT_Z_SIM_FLASH:=zephyr,sim-flash DT_NODELABEL_ENABLED_WITH_Z_SIM_FLASH:=$(dt_nodelabel_enabled_with_compat,sim_flash,$(DT_COMPAT_Z_SIM_FLASH)) DT_NODELABEL_SIM_FLASH_MEMORY_REGION:=$(dt_node_ph_prop_path,$(dt_nodelabel_path,sim_flash),memory-region) diff --git a/drivers/flash/flash_simulator.c b/drivers/flash/flash_simulator.c index b78c844e1ad4fd..0f558c903d8aa6 100644 --- a/drivers/flash/flash_simulator.c +++ b/drivers/flash/flash_simulator.c @@ -148,6 +148,9 @@ static bool flash_erase_at_start; static bool flash_rm_at_exit; static bool flash_in_ram; #else +#if CONFIG_FLASH_SIMULATOR_PROVISION +static uint8_t *mock_flash = (uint8_t *)FLASH_SIMULATOR_BASE_OFFSET; +#else #if DT_NODE_HAS_PROP(DT_PARENT(SOC_NV_FLASH_NODE), memory_region) #define FLASH_SIMULATOR_MREGION \ LINKER_DT_NODE_REGION_NAME( \ @@ -156,6 +159,7 @@ static uint8_t mock_flash[FLASH_SIMULATOR_FLASH_SIZE] Z_GENERIC_SECTION(FLASH_SI #else static uint8_t mock_flash[FLASH_SIMULATOR_FLASH_SIZE]; #endif +#endif /* CONFIG_FLASH_SIMULATOR_PROVISION */ #endif /* CONFIG_ARCH_POSIX */ static const struct flash_driver_api flash_sim_api; @@ -414,7 +418,9 @@ static int flash_mock_init(const struct device *dev) static int flash_mock_init(const struct device *dev) { ARG_UNUSED(dev); - memset(mock_flash, FLASH_SIMULATOR_ERASE_VALUE, ARRAY_SIZE(mock_flash)); + if (!IS_ENABLED(CONFIG_FLASH_SIMULATOR_PROVISION)) { + memset(mock_flash, FLASH_SIMULATOR_ERASE_VALUE, FLASH_SIMULATOR_FLASH_SIZE); + } return 0; } #endif /* DT_NODE_HAS_PROP(DT_PARENT(SOC_NV_FLASH_NODE), memory_region) */ diff --git a/include/zephyr/kernel/internal/mm.h b/include/zephyr/kernel/internal/mm.h index 2b5a9330e54619..0fbb37a2c38f09 100644 --- a/include/zephyr/kernel/internal/mm.h +++ b/include/zephyr/kernel/internal/mm.h @@ -74,6 +74,9 @@ #ifdef CONFIG_XIP #error "XIP and a virtual memory kernel are not allowed" #endif +#ifdef CONFIG_FLASH_SIMULATOR_PROVISION +#error "Simulated flash memory cannot be provisioned for a virtual memory kernel." +#endif #endif #ifndef _ASMLANGUAGE