fatfs: add read_only flag to esp_vfs_fat_mount_config_t

This commit is contained in:
Tomas Rohlinek
2026-06-26 09:16:26 +02:00
parent 4d6104211c
commit dfd2e6b6f7
15 changed files with 322 additions and 8 deletions
+9
View File
@@ -136,6 +136,14 @@ typedef struct {
* may be different.
*/
bool use_one_fat;
/**
* Mount the filesystem in read-only mode.
* When set to true, all write operations (open for write, mkdir, unlink,
* rename, etc.) will fail with errno set to EROFS.
* This flag is independent of the underlying storage — it can be used
* with wear-levelled, raw, BDL, or SD card partitions.
*/
bool read_only;
} esp_vfs_fat_mount_config_t;
#define VFS_FAT_MOUNT_DEFAULT_CONFIG() \
@@ -145,6 +153,7 @@ typedef struct {
.allocation_unit_size = 0, \
.disk_status_check_enable = false, \
.use_one_fat = false, \
.read_only = false, \
}
// Compatibility definition
+4 -3
View File
@@ -8,6 +8,7 @@
#include <string.h>
#include "esp_check.h"
#include "esp_log.h"
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "vfs_fat_internal.h"
#include "diskio_impl.h"
@@ -17,8 +18,6 @@ static const char *TAG = "vfs_fat_bdl";
static vfs_fat_bdl_ctx_t *s_bdl_ctx[FF_VOLUMES] = {};
extern esp_err_t esp_vfs_set_readonly_flag(const char *base_path);
static bool get_ctx_id_by_bdl(esp_blockdev_handle_t bdl, uint32_t *out_id)
{
for (int i = 0; i < FF_VOLUMES; i++) {
@@ -104,6 +103,8 @@ esp_err_t esp_vfs_fat_bdl_mount(const char *base_path,
ESP_RETURN_ON_FALSE(base_path, ESP_ERR_INVALID_ARG, TAG, "base_path is NULL");
ESP_RETURN_ON_FALSE(bdl_handle != ESP_BLOCKDEV_HANDLE_INVALID, ESP_ERR_INVALID_ARG, TAG, "invalid BDL handle");
ESP_RETURN_ON_FALSE(mount_config, ESP_ERR_INVALID_ARG, TAG, "mount_config is NULL");
ESP_RETURN_ON_FALSE(!(mount_config->read_only && mount_config->format_if_mount_failed),
ESP_ERR_INVALID_ARG, TAG, "read_only and format_if_mount_failed are mutually exclusive");
BYTE pdrv = 0xFF;
if (ff_diskio_get_drive(&pdrv) != ESP_OK) {
@@ -165,7 +166,7 @@ esp_err_t esp_vfs_fat_bdl_mount(const char *base_path,
assert(ctx_id != FF_VOLUMES);
s_bdl_ctx[ctx_id] = ctx;
if (bdl_handle->device_flags.read_only) {
if (bdl_handle->device_flags.read_only || mount_config->read_only) {
esp_vfs_set_readonly_flag(base_path);
}
+7 -1
View File
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -415,6 +415,8 @@ esp_err_t esp_vfs_fat_mount_initialized(sdmmc_card_t* card,
if (card == NULL || base_path == NULL || mount_config == NULL) {
return ESP_ERR_INVALID_ARG;
}
ESP_RETURN_ON_FALSE(!(mount_config->read_only && mount_config->format_if_mount_failed),
ESP_ERR_INVALID_ARG, TAG, "read_only and format_if_mount_failed are mutually exclusive");
esp_err_t err;
@@ -436,6 +438,10 @@ esp_err_t esp_vfs_fat_mount_initialized(sdmmc_card_t* card,
err = esp_vfs_fat_save_ctx(ldrv, mount_config, card, dup_path, fs, flags);
CHECK_EXECUTE_RESULT(err, "esp_vfs_fat_save_ctx failed");
if (mount_config->read_only) {
esp_vfs_set_readonly_flag(base_path);
}
return ESP_OK;
cleanup:
free(dup_path);
+9 -4
View File
@@ -8,6 +8,7 @@
#include <string.h>
#include "esp_check.h"
#include "esp_log.h"
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "vfs_fat_internal.h"
#include "diskio_impl.h"
@@ -23,8 +24,6 @@ static const char* TAG = "vfs_fat_spiflash";
static vfs_fat_spiflash_ctx_t *s_ctx[FF_VOLUMES] = {};
extern esp_err_t esp_vfs_set_readonly_flag(const char* base_path); // from vfs/vfs.c to set readonly flag externally
static bool s_get_context_id_by_label(const char *label, uint32_t *out_id)
{
vfs_fat_spiflash_ctx_t *p_ctx = NULL;
@@ -149,6 +148,9 @@ esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path,
const esp_vfs_fat_mount_config_t* mount_config,
wl_handle_t* wl_handle)
{
ESP_RETURN_ON_FALSE(!(mount_config->read_only && mount_config->format_if_mount_failed),
ESP_ERR_INVALID_ARG, TAG, "read_only and format_if_mount_failed are mutually exclusive");
esp_err_t ret = ESP_OK;
vfs_fat_spiflash_ctx_t *ctx = NULL;
uint32_t ctx_id = FF_VOLUMES;
@@ -208,7 +210,7 @@ esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path,
assert(ctx_id != FF_VOLUMES);
s_ctx[ctx_id] = ctx;
if (data_partition->readonly) {
if (data_partition->readonly || mount_config->read_only) {
esp_vfs_set_readonly_flag(base_path);
}
@@ -350,6 +352,9 @@ esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
const char* partition_label,
const esp_vfs_fat_mount_config_t* mount_config)
{
ESP_RETURN_ON_FALSE(!(mount_config->read_only && mount_config->format_if_mount_failed),
ESP_ERR_INVALID_ARG, TAG, "read_only and format_if_mount_failed are mutually exclusive");
esp_err_t ret = ESP_OK;
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
@@ -388,7 +393,7 @@ esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
goto fail;
}
if (data_partition->readonly) {
if (data_partition->readonly || mount_config->read_only) {
esp_vfs_set_readonly_flag(base_path);
}