Skip to content

Commit fbd57c7

Browse files
committed
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 <[email protected]>
1 parent 0443c6d commit fbd57c7

File tree

7 files changed

+123
-22
lines changed

7 files changed

+123
-22
lines changed

boards/posix/native_posix/Kconfig.defconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ endif # TRACING_CTF
9090

9191
if FLASH
9292

93-
config FLASH_NATIVE_POSIX
93+
config FLASH_SIMULATOR
9494
default y
9595

9696
endif # FLASH

boards/posix/native_posix/doc/index.rst

+3-11
Original file line numberDiff line numberDiff line change
@@ -537,17 +537,9 @@ The following peripherals are currently provided with this board:
537537

538538
**Flash driver**:
539539
A flash driver is provided that accesses all flash data through a binary file
540-
on the host file system.
541-
542-
The size of the flash device can be configured through the native POSIX board
543-
devicetree and the sector size is configurable via the Kconfig option
544-
:option:`CONFIG_FLASH_NATIVE_POSIX_SECTOR_SIZE`. The sector size will only be
545-
used to return flash page layout related information and no restrictions are
546-
imposed by the driver based on the configured sector size. As such an erase
547-
operation of arbitrary size will succeed on the emulated flash device.
548-
Further the emulated device will not impose any write restriction that are
549-
applicable for a regular flash device, including changing the state of a bit
550-
from zero to one.
540+
on the host file system. The behavior of the flash device can be configured
541+
through the native POSIX board devicetree or Kconfig settings under
542+
:option:`CONFIG_FLASH_SIMULATOR`.
551543

552544
By default the binary data is located in the file *flash.bin* in the current
553545
working directory. The location of this file can be changed through the

boards/posix/native_posix/dts_fixup.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#define DT_FLASH_DEV_NAME DT_INST_0_ZEPHYR_NATIVE_POSIX_FLASH_CONTROLLER_LABEL
7+
#define DT_FLASH_DEV_NAME DT_INST_0_ZEPHYR_SIM_FLASH_LABEL
88
#define DT_UART_0_DEV_NAME DT_ZEPHYR_NATIVE_POSIX_UART_UART_LABEL
99

boards/posix/native_posix/native_posix.dts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
};
2424

2525
flashcontroller0: flash-controller@0 {
26-
compatible = "zephyr,native-posix-flash-controller";
26+
compatible = "zephyr,sim-flash";
2727
reg = <0x00000000 DT_SIZE_K(2048)>;
2828

2929
#address-cells = <1>;
@@ -35,7 +35,7 @@
3535
status = "okay";
3636
compatible = "soc-nv-flash";
3737
label = "flash";
38-
erase-block-size = <1>;
38+
erase-block-size = <4096>;
3939
write-block-size = <1>;
4040
reg = <0x00000000 DT_SIZE_K(2048)>;
4141

drivers/flash/flash_simulator.c

+116-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,34 @@
1313
#include <stats/stats.h>
1414
#include <string.h>
1515

16+
#ifdef CONFIG_ARCH_POSIX
17+
18+
#include <unistd.h>
19+
#include <sys/types.h>
20+
#include <sys/stat.h>
21+
#include <sys/mman.h>
22+
#include <fcntl.h>
23+
#include <errno.h>
24+
25+
#include "cmdline.h"
26+
#include "soc.h"
27+
28+
#endif /* CONFIG_ARCH_POSIX */
29+
1630
/* configuration derived from DT */
31+
#ifdef CONFIG_ARCH_POSIX
32+
#define FLASH_SIMULATOR_BASE_OFFSET DT_FLASH_BASE_ADDRESS
33+
#define FLASH_SIMULATOR_ERASE_UNIT DT_FLASH_ERASE_BLOCK_SIZE
34+
#define FLASH_SIMULATOR_PROG_UNIT DT_FLASH_WRITE_BLOCK_SIZE
35+
#define FLASH_SIMULATOR_FLASH_SIZE (DT_FLASH_SIZE * 1024)
36+
#define FLASH_SIMULATOR_DEV_NAME DT_FLASH_DEV_NAME
37+
#else
1738
#define FLASH_SIMULATOR_BASE_OFFSET DT_FLASH_SIM_BASE_ADDRESS
1839
#define FLASH_SIMULATOR_ERASE_UNIT DT_FLASH_SIM_ERASE_BLOCK_SIZE
1940
#define FLASH_SIMULATOR_PROG_UNIT DT_FLASH_SIM_WRITE_BLOCK_SIZE
2041
#define FLASH_SIMULATOR_FLASH_SIZE DT_FLASH_SIM_SIZE
42+
#define FLASH_SIMULATOR_DEV_NAME "FLASH_SIMULATOR"
43+
#endif /* CONFIG_ARCH_POSIX */
2144

2245
#define FLASH_SIMULATOR_PAGE_COUNT (FLASH_SIMULATOR_FLASH_SIZE / \
2346
FLASH_SIMULATOR_ERASE_UNIT)
@@ -102,7 +125,15 @@ STATS_NAME(flash_sim_thresholds, max_erase_calls)
102125
STATS_NAME(flash_sim_thresholds, max_len)
103126
STATS_NAME_END(flash_sim_thresholds);
104127

128+
#ifdef CONFIG_ARCH_POSIX
129+
static u8_t *mock_flash;
130+
static int flash_fd = -1;
131+
static const char *flash_file_path;
132+
static const char default_flash_file_path[] = "flash.bin";
133+
#else
105134
static u8_t mock_flash[FLASH_SIMULATOR_FLASH_SIZE];
135+
#endif /* CONFIG_ARCH_POSIX */
136+
106137
static bool write_protection;
107138

108139
static const struct flash_driver_api flash_sim_api;
@@ -322,16 +353,96 @@ static const struct flash_driver_api flash_sim_api = {
322353
#endif
323354
};
324355

356+
#ifdef CONFIG_ARCH_POSIX
357+
358+
static int flash_mock_init(struct device *dev)
359+
{
360+
if (flash_file_path == NULL) {
361+
flash_file_path = default_flash_file_path;
362+
}
363+
364+
flash_fd = open(flash_file_path, O_RDWR | O_CREAT, (mode_t)0600);
365+
if (flash_fd == -1) {
366+
posix_print_warning("Failed to open flash device file "
367+
"%s: %s\n",
368+
flash_file_path, strerror(errno));
369+
return -EIO;
370+
}
371+
372+
if (ftruncate(flash_fd, FLASH_SIMULATOR_FLASH_SIZE) == -1) {
373+
posix_print_warning("Failed to resize flash device file "
374+
"%s: %s\n",
375+
flash_file_path, strerror(errno));
376+
return -EIO;
377+
}
378+
379+
mock_flash = mmap(NULL, FLASH_SIMULATOR_FLASH_SIZE,
380+
PROT_WRITE | PROT_READ, MAP_SHARED, flash_fd, 0);
381+
if (mock_flash == MAP_FAILED) {
382+
posix_print_warning("Failed to mmap flash device file "
383+
"%s: %s\n",
384+
flash_file_path, strerror(errno));
385+
return -EIO;
386+
}
387+
388+
return 0;
389+
}
390+
391+
#else
392+
393+
static int flash_mock_init(struct device *dev)
394+
{
395+
memset(mock_flash, 0xFF, ARRAY_SIZE(mock_flash));
396+
return 0;
397+
}
398+
399+
#endif /* CONFIG_ARCH_POSIX */
400+
325401
static int flash_init(struct device *dev)
326402
{
327403
STATS_INIT_AND_REG(flash_sim_stats, STATS_SIZE_32, "flash_sim_stats");
328404
STATS_INIT_AND_REG(flash_sim_thresholds, STATS_SIZE_32,
329405
"flash_sim_thresholds");
330-
memset(mock_flash, 0xFF, ARRAY_SIZE(mock_flash));
331-
332-
return 0;
406+
return flash_mock_init(dev);
333407
}
334408

335-
DEVICE_AND_API_INIT(flash_simulator, "FLASH_SIMULATOR", flash_init, NULL, NULL,
336-
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
409+
DEVICE_AND_API_INIT(flash_simulator, FLASH_SIMULATOR_DEV_NAME, flash_init,
410+
NULL, NULL, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
337411
&flash_sim_api);
412+
413+
#ifdef CONFIG_ARCH_POSIX
414+
415+
static void flash_native_posix_cleanup(void)
416+
{
417+
if ((mock_flash != MAP_FAILED) && (mock_flash != NULL)) {
418+
munmap(mock_flash, FLASH_SIMULATOR_FLASH_SIZE);
419+
}
420+
421+
if (flash_fd != -1) {
422+
close(flash_fd);
423+
}
424+
}
425+
426+
static void flash_native_posix_options(void)
427+
{
428+
static struct args_struct_t flash_options[] = {
429+
{ .manual = false,
430+
.is_mandatory = false,
431+
.is_switch = false,
432+
.option = "flash",
433+
.name = "path",
434+
.type = 's',
435+
.dest = (void *)&flash_file_path,
436+
.call_when_found = NULL,
437+
.descript = "Path to binary file to be used as flash" },
438+
ARG_TABLE_ENDMARKER
439+
};
440+
441+
native_add_command_line_opts(flash_options);
442+
}
443+
444+
445+
NATIVE_TASK(flash_native_posix_options, PRE_BOOT_1, 1);
446+
NATIVE_TASK(flash_native_posix_cleanup, ON_EXIT, 1);
447+
448+
#endif /* CONFIG_ARCH_POSIX */

tests/subsys/fs/littlefs/boards/native_posix.conf

-1
This file was deleted.

tests/subsys/fs/littlefs/boards/native_posix_64.conf

-1
This file was deleted.

0 commit comments

Comments
 (0)