From 97af825b1468dec94a1313939586ed33f6787ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Rohl=C3=ADnek?= Date: Thu, 9 May 2024 15:31:51 +0200 Subject: [PATCH] feat(storage/vfs): make automatic nullfs initialization optional --- components/vfs/Kconfig | 7 +++++++ components/vfs/nullfs.c | 28 +++++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/components/vfs/Kconfig b/components/vfs/Kconfig index e9992e11fa..65042a8826 100644 --- a/components/vfs/Kconfig +++ b/components/vfs/Kconfig @@ -97,4 +97,11 @@ menu "Virtual file system" Define maximum number of host filesystem mount points. endmenu + config VFS_INITIALIZE_DEV_NULL + bool "Initialize /dev/null VFS" + default y + depends on VFS_SUPPORT_IO + help + If enabled, /dev/null VFS will be automatically initialized at startup. + endmenu diff --git a/components/vfs/nullfs.c b/components/vfs/nullfs.c index 9a8f98888f..1324052a5d 100644 --- a/components/vfs/nullfs.c +++ b/components/vfs/nullfs.c @@ -3,6 +3,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ +#include "sdkconfig.h" #include #include #include @@ -16,6 +17,7 @@ #include #include "esp_vfs.h" #include "esp_log.h" +#include "esp_err.h" #include "esp_private/startup_internal.h" #include "esp_vfs_null.h" @@ -27,7 +29,8 @@ typedef enum { VFS_NULL_WRITE = 1 << 1, } vfs_null_flags_t; -typedef uint64_t vfs_null_ctx_t; +typedef uint32_t vfs_null_ctx_t; +#define VFS_NULL_MAX_FDS (sizeof(vfs_null_ctx_t) * 4) #define GET_FLAGS(ctx, fd) ((ctx >> (fd * 2)) & 0x3) #define SET_FLAGS(ctx, fd, flags) (ctx |= (flags << (fd * 2))) @@ -38,12 +41,17 @@ typedef uint64_t vfs_null_ctx_t; #define SET_WRITABLE(ctx, fd) SET_FLAGS(ctx, fd, VFS_NULL_WRITE) #define SET_READABLE(ctx, fd) SET_FLAGS(ctx, fd, VFS_NULL_READ) -#define FD_IN_RANGE(fd) (fd >= 0 && fd < 32) +#define FD_IN_RANGE(fd) (fd >= 0 && fd < VFS_NULL_MAX_FDS) #define IS_FD_VALID(fd) (FD_IN_RANGE(fd) && GET_FLAGS(g_fds, fd) != VFS_NULL_CLOSED) -static vfs_null_ctx_t g_fds = 0; - +#ifdef CONFIG_ESP_CONSOLE_NONE // Prevents recursive logging static const char* TAG = "nullfs"; +#define NULLFS_LOGD(TAG, fmt, ...) ESP_LOGD(TAG, fmt, ##__VA_ARGS__) +#else +#define NULLFS_LOGD(TAG, fmt, ...) +#endif + +static vfs_null_ctx_t g_fds = 0; static ssize_t vfs_null_write(int fd, const void *data, size_t size); static off_t vfs_null_lseek(int fd, off_t offset, int whence); @@ -124,7 +132,7 @@ static ssize_t vfs_null_read(int fd, void *data, size_t size) { UNUSED(data); - ESP_LOGD(TAG, "read %u bytes", size); + NULLFS_LOGD(TAG, "read %u bytes", size); if (FD_IN_RANGE(fd) && READABLE(g_fds, fd)) { return 0; // EOF @@ -140,7 +148,7 @@ static int vfs_null_pread(int fd, void *data, size_t size, off_t offset) UNUSED(size); UNUSED(offset); - ESP_LOGD(TAG, "pread %u bytes at offset %ld", size, offset); + NULLFS_LOGD(TAG, "pread %u bytes at offset %ld", size, offset); if (!FD_IN_RANGE(fd) || !READABLE(g_fds, fd)) { errno = EBADF; @@ -156,7 +164,7 @@ static int vfs_null_pwrite(int fd, const void *data, size_t size, off_t offset) UNUSED(data); UNUSED(offset); - ESP_LOGD(TAG, "pwrite %u bytes at offset %ld", size, offset); + NULLFS_LOGD(TAG, "pwrite %u bytes at offset %ld", size, offset); if (!FD_IN_RANGE(fd) || !WRITABLE(g_fds, fd)) { errno = EBADF; @@ -168,7 +176,7 @@ static int vfs_null_pwrite(int fd, const void *data, size_t size, off_t offset) static int vfs_null_get_empty_fd(void) { - for (int i = 0; i < 32; i++) { + for (int i = 0; i < VFS_NULL_MAX_FDS; i++) { if (GET_FLAGS(g_fds, i) == VFS_NULL_CLOSED) { return i; } @@ -181,7 +189,7 @@ static int vfs_null_open(const char* path, int flags, int mode) { UNUSED(mode); - ESP_LOGD(TAG, "open %s, flags %d", path, flags); + NULLFS_LOGD(TAG, "open %s, flags %d", path, flags); // Possibly check if the flags are valid @@ -302,10 +310,12 @@ static int vfs_null_fsync(int fd) return 0; } +#if defined(CONFIG_VFS_INITIALIZE_DEV_NULL) || defined(CONFIG_ESP_CONSOLE_NONE) ESP_SYSTEM_INIT_FN(init_vfs_nullfs, CORE, BIT(0), 113) { return esp_vfs_null_register(); } +#endif // CONFIG_VFS_INITIALIZE_DEV_NULL void esp_vfs_include_nullfs_register(void) {