Skip to content

Commit

Permalink
drivers: flash: simulator: simulate pre-provisioned flash
Browse files Browse the repository at this point in the history
Enables the flash simulator to simulate pre-provisioned flash with QEMU
(or any other application using the flash simulator).

Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
  • Loading branch information
fg-cfh committed Sep 27, 2024
1 parent d05c9c1 commit b186266
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmake/emu/qemu.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions drivers/flash/Kconfig.simulator
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion drivers/flash/flash_simulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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( \
Expand All @@ -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;
Expand Down Expand Up @@ -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) */
Expand Down
3 changes: 3 additions & 0 deletions include/zephyr/kernel/internal/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b186266

Please sign in to comment.