From 8cd4a817d90d2dfa52fc2272547a3da762060e7c Mon Sep 17 00:00:00 2001 From: Jan Van Winkel Date: Sun, 10 Nov 2019 02:16:54 +0100 Subject: [PATCH] drivers/flash/flash_simulator: Added file back end for posix arch Extended flash simulator for posix architecture to read/write data from a binary file on the host file system. Further enable the flash simulator by default on native_posix(_64) boards and updated the documentation accordingly. Signed-off-by: Jan Van Winkel --- boards/posix/native_posix/Kconfig.defconfig | 2 +- boards/posix/native_posix/doc/index.rst | 14 +- boards/posix/native_posix/dts_fixup.h | 2 +- boards/posix/native_posix/native_posix.dts | 4 +- drivers/flash/flash_simulator.c | 121 +++++++++++++++++- .../fs/littlefs/boards/native_posix.conf | 1 - .../fs/littlefs/boards/native_posix_64.conf | 1 - 7 files changed, 123 insertions(+), 22 deletions(-) delete mode 100644 tests/subsys/fs/littlefs/boards/native_posix.conf delete mode 100644 tests/subsys/fs/littlefs/boards/native_posix_64.conf diff --git a/boards/posix/native_posix/Kconfig.defconfig b/boards/posix/native_posix/Kconfig.defconfig index 0b04a987395..59d4349a038 100644 --- a/boards/posix/native_posix/Kconfig.defconfig +++ b/boards/posix/native_posix/Kconfig.defconfig @@ -90,7 +90,7 @@ endif # TRACING_CTF if FLASH -config FLASH_NATIVE_POSIX +config FLASH_SIMULATOR default y endif # FLASH diff --git a/boards/posix/native_posix/doc/index.rst b/boards/posix/native_posix/doc/index.rst index 9665cc2a857..a38425ad8f7 100644 --- a/boards/posix/native_posix/doc/index.rst +++ b/boards/posix/native_posix/doc/index.rst @@ -537,17 +537,9 @@ The following peripherals are currently provided with this board: **Flash driver**: A flash driver is provided that accesses all flash data through a binary file - on the host file system. - - The size of the flash device can be configured through the native POSIX board - devicetree and the sector size is configurable via the Kconfig option - :option:`CONFIG_FLASH_NATIVE_POSIX_SECTOR_SIZE`. The sector size will only be - used to return flash page layout related information and no restrictions are - imposed by the driver based on the configured sector size. As such an erase - operation of arbitrary size will succeed on the emulated flash device. - Further the emulated device will not impose any write restriction that are - applicable for a regular flash device, including changing the state of a bit - from zero to one. + on the host file system. The behavior of the flash device can be configured + through the native POSIX board devicetree or Kconfig settings under + :option:`CONFIG_FLASH_SIMULATOR`. By default the binary data is located in the file *flash.bin* in the current working directory. The location of this file can be changed through the diff --git a/boards/posix/native_posix/dts_fixup.h b/boards/posix/native_posix/dts_fixup.h index 2e6f58ac8e2..c512dc62c42 100644 --- a/boards/posix/native_posix/dts_fixup.h +++ b/boards/posix/native_posix/dts_fixup.h @@ -4,6 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#define DT_FLASH_DEV_NAME DT_INST_0_ZEPHYR_NATIVE_POSIX_FLASH_CONTROLLER_LABEL +#define DT_FLASH_DEV_NAME DT_INST_0_ZEPHYR_SIM_FLASH_LABEL #define DT_UART_0_DEV_NAME DT_ZEPHYR_NATIVE_POSIX_UART_UART_LABEL diff --git a/boards/posix/native_posix/native_posix.dts b/boards/posix/native_posix/native_posix.dts index b7bb9e5133d..dfb82429595 100644 --- a/boards/posix/native_posix/native_posix.dts +++ b/boards/posix/native_posix/native_posix.dts @@ -23,7 +23,7 @@ }; flashcontroller0: flash-controller@0 { - compatible = "zephyr,native-posix-flash-controller"; + compatible = "zephyr,sim-flash"; reg = <0x00000000 DT_SIZE_K(2048)>; #address-cells = <1>; @@ -35,7 +35,7 @@ status = "okay"; compatible = "soc-nv-flash"; label = "flash"; - erase-block-size = <1>; + erase-block-size = <4096>; write-block-size = <1>; reg = <0x00000000 DT_SIZE_K(2048)>; diff --git a/drivers/flash/flash_simulator.c b/drivers/flash/flash_simulator.c index a7ddebccfae..2eaadd8405f 100644 --- a/drivers/flash/flash_simulator.c +++ b/drivers/flash/flash_simulator.c @@ -13,11 +13,34 @@ #include #include +#ifdef CONFIG_ARCH_POSIX + +#include +#include +#include +#include +#include +#include + +#include "cmdline.h" +#include "soc.h" + +#endif /* CONFIG_ARCH_POSIX */ + /* configuration derived from DT */ +#ifdef CONFIG_ARCH_POSIX +#define FLASH_SIMULATOR_BASE_OFFSET DT_FLASH_BASE_ADDRESS +#define FLASH_SIMULATOR_ERASE_UNIT DT_FLASH_ERASE_BLOCK_SIZE +#define FLASH_SIMULATOR_PROG_UNIT DT_FLASH_WRITE_BLOCK_SIZE +#define FLASH_SIMULATOR_FLASH_SIZE (DT_FLASH_SIZE * 1024) +#define FLASH_SIMULATOR_DEV_NAME DT_FLASH_DEV_NAME +#else #define FLASH_SIMULATOR_BASE_OFFSET DT_FLASH_SIM_BASE_ADDRESS #define FLASH_SIMULATOR_ERASE_UNIT DT_FLASH_SIM_ERASE_BLOCK_SIZE #define FLASH_SIMULATOR_PROG_UNIT DT_FLASH_SIM_WRITE_BLOCK_SIZE #define FLASH_SIMULATOR_FLASH_SIZE DT_FLASH_SIM_SIZE +#define FLASH_SIMULATOR_DEV_NAME "FLASH_SIMULATOR" +#endif /* CONFIG_ARCH_POSIX */ #define FLASH_SIMULATOR_PAGE_COUNT (FLASH_SIMULATOR_FLASH_SIZE / \ FLASH_SIMULATOR_ERASE_UNIT) @@ -102,7 +125,15 @@ STATS_NAME(flash_sim_thresholds, max_erase_calls) STATS_NAME(flash_sim_thresholds, max_len) STATS_NAME_END(flash_sim_thresholds); +#ifdef CONFIG_ARCH_POSIX +static u8_t *mock_flash; +static int flash_fd = -1; +static const char *flash_file_path; +static const char default_flash_file_path[] = "flash.bin"; +#else static u8_t mock_flash[FLASH_SIMULATOR_FLASH_SIZE]; +#endif /* CONFIG_ARCH_POSIX */ + static bool write_protection; static const struct flash_driver_api flash_sim_api; @@ -322,16 +353,96 @@ static const struct flash_driver_api flash_sim_api = { #endif }; +#ifdef CONFIG_ARCH_POSIX + +static int flash_mock_init(struct device *dev) +{ + if (flash_file_path == NULL) { + flash_file_path = default_flash_file_path; + } + + flash_fd = open(flash_file_path, O_RDWR | O_CREAT, (mode_t)0600); + if (flash_fd == -1) { + posix_print_warning("Failed to open flash device file " + "%s: %s\n", + flash_file_path, strerror(errno)); + return -EIO; + } + + if (ftruncate(flash_fd, FLASH_SIMULATOR_FLASH_SIZE) == -1) { + posix_print_warning("Failed to resize flash device file " + "%s: %s\n", + flash_file_path, strerror(errno)); + return -EIO; + } + + mock_flash = mmap(NULL, FLASH_SIMULATOR_FLASH_SIZE, + PROT_WRITE | PROT_READ, MAP_SHARED, flash_fd, 0); + if (mock_flash == MAP_FAILED) { + posix_print_warning("Failed to mmap flash device file " + "%s: %s\n", + flash_file_path, strerror(errno)); + return -EIO; + } + + return 0; +} + +#else + +static int flash_mock_init(struct device *dev) +{ + memset(mock_flash, 0xFF, ARRAY_SIZE(mock_flash)); + return 0; +} + +#endif /* CONFIG_ARCH_POSIX */ + static int flash_init(struct device *dev) { STATS_INIT_AND_REG(flash_sim_stats, STATS_SIZE_32, "flash_sim_stats"); STATS_INIT_AND_REG(flash_sim_thresholds, STATS_SIZE_32, "flash_sim_thresholds"); - memset(mock_flash, 0xFF, ARRAY_SIZE(mock_flash)); - - return 0; + return flash_mock_init(dev); } -DEVICE_AND_API_INIT(flash_simulator, "FLASH_SIMULATOR", flash_init, NULL, NULL, - POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, +DEVICE_AND_API_INIT(flash_simulator, FLASH_SIMULATOR_DEV_NAME, flash_init, + NULL, NULL, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &flash_sim_api); + +#ifdef CONFIG_ARCH_POSIX + +static void flash_native_posix_cleanup(void) +{ + if ((mock_flash != MAP_FAILED) && (mock_flash != NULL)) { + munmap(mock_flash, FLASH_SIMULATOR_FLASH_SIZE); + } + + if (flash_fd != -1) { + close(flash_fd); + } +} + +static void flash_native_posix_options(void) +{ + static struct args_struct_t flash_options[] = { + { .manual = false, + .is_mandatory = false, + .is_switch = false, + .option = "flash", + .name = "path", + .type = 's', + .dest = (void *)&flash_file_path, + .call_when_found = NULL, + .descript = "Path to binary file to be used as flash" }, + ARG_TABLE_ENDMARKER + }; + + native_add_command_line_opts(flash_options); +} + + +NATIVE_TASK(flash_native_posix_options, PRE_BOOT_1, 1); +NATIVE_TASK(flash_native_posix_cleanup, ON_EXIT, 1); + +#endif /* CONFIG_ARCH_POSIX */ diff --git a/tests/subsys/fs/littlefs/boards/native_posix.conf b/tests/subsys/fs/littlefs/boards/native_posix.conf deleted file mode 100644 index fbae4915663..00000000000 --- a/tests/subsys/fs/littlefs/boards/native_posix.conf +++ /dev/null @@ -1 +0,0 @@ -CONFIG_FLASH_NATIVE_POSIX_SECTOR_SIZE=4 diff --git a/tests/subsys/fs/littlefs/boards/native_posix_64.conf b/tests/subsys/fs/littlefs/boards/native_posix_64.conf deleted file mode 100644 index fbae4915663..00000000000 --- a/tests/subsys/fs/littlefs/boards/native_posix_64.conf +++ /dev/null @@ -1 +0,0 @@ -CONFIG_FLASH_NATIVE_POSIX_SECTOR_SIZE=4