mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-01 18:50:34 +03:00
fatfs: add read_only flag to esp_vfs_fat_mount_config_t
This commit is contained in:
@@ -15,6 +15,7 @@ set(requires "wear_levelling" "esp_blockdev")
|
||||
if(${target} STREQUAL "linux")
|
||||
list(APPEND srcs "port/linux/ffsystem.c"
|
||||
"vfs/vfs_fat.c"
|
||||
"vfs/vfs_fat_spiflash.c"
|
||||
"vfs/vfs_fat_bdl.c")
|
||||
list(APPEND include_dirs "vfs")
|
||||
list(APPEND priv_requires "vfs" "linux")
|
||||
|
||||
@@ -38,6 +38,16 @@ components/fatfs/test_apps/flash_wl:
|
||||
- vfs
|
||||
- wear_leveling
|
||||
|
||||
components/fatfs/test_apps/flash_wl_readonly:
|
||||
disable_test:
|
||||
- if: IDF_TARGET not in ["esp32", "esp32c3"]
|
||||
reason: only one target per arch needed
|
||||
depends_components:
|
||||
- esp_partition
|
||||
- fatfs
|
||||
- vfs
|
||||
- wear_leveling
|
||||
|
||||
components/fatfs/test_apps/sdcard:
|
||||
disable:
|
||||
- if: SOC_GPSPI_SUPPORTED != 1
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
set(COMPONENTS main)
|
||||
set(EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/../test_fatfs_common")
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
|
||||
project(test_fatfs_flash_wl_readonly)
|
||||
@@ -0,0 +1,4 @@
|
||||
idf_component_register(SRCS "test_fatfs_flash_wl_readonly.c"
|
||||
INCLUDE_DIRS "."
|
||||
PRIV_REQUIRES unity spi_flash fatfs vfs
|
||||
WHOLE_ARCHIVE)
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/unistd.h>
|
||||
#include "unity.h"
|
||||
#include "esp_vfs.h"
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "wear_levelling.h"
|
||||
|
||||
static const char *BASE_PATH = "/spiflash";
|
||||
static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
|
||||
|
||||
static void prepare_filesystem(void)
|
||||
{
|
||||
esp_vfs_fat_mount_config_t cfg = {
|
||||
.format_if_mount_failed = true,
|
||||
.max_files = 5,
|
||||
};
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl(BASE_PATH, NULL, &cfg, &s_wl_handle));
|
||||
|
||||
FILE *f = fopen("/spiflash/hello.txt", "w");
|
||||
TEST_ASSERT_NOT_NULL(f);
|
||||
fprintf(f, "hello");
|
||||
fclose(f);
|
||||
|
||||
TEST_ASSERT_EQUAL(0, mkdir("/spiflash/testdir", 0755));
|
||||
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl(BASE_PATH, s_wl_handle));
|
||||
s_wl_handle = WL_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
static void mount_readonly(void)
|
||||
{
|
||||
esp_vfs_fat_mount_config_t cfg = {
|
||||
.format_if_mount_failed = false,
|
||||
.max_files = 5,
|
||||
.read_only = true,
|
||||
};
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl(BASE_PATH, NULL, &cfg, &s_wl_handle));
|
||||
}
|
||||
|
||||
static void unmount(void)
|
||||
{
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl(BASE_PATH, s_wl_handle));
|
||||
s_wl_handle = WL_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
prepare_filesystem();
|
||||
unity_run_menu();
|
||||
}
|
||||
|
||||
TEST_CASE("read_only mount: can read existing file", "[fatfs][readonly]")
|
||||
{
|
||||
mount_readonly();
|
||||
|
||||
FILE *f = fopen("/spiflash/hello.txt", "r");
|
||||
TEST_ASSERT_NOT_NULL(f);
|
||||
char buf[16] = {};
|
||||
TEST_ASSERT_EQUAL(5, fread(buf, 1, sizeof(buf), f));
|
||||
TEST_ASSERT_EQUAL_STRING("hello", buf);
|
||||
fclose(f);
|
||||
|
||||
unmount();
|
||||
}
|
||||
|
||||
TEST_CASE("read_only mount: open for write fails with EROFS", "[fatfs][readonly]")
|
||||
{
|
||||
mount_readonly();
|
||||
|
||||
int fd = open("/spiflash/new.txt", O_CREAT | O_RDWR, 0666);
|
||||
TEST_ASSERT_EQUAL(-1, fd);
|
||||
TEST_ASSERT_EQUAL(EROFS, errno);
|
||||
|
||||
fd = open("/spiflash/hello.txt", O_WRONLY);
|
||||
TEST_ASSERT_EQUAL(-1, fd);
|
||||
TEST_ASSERT_EQUAL(EROFS, errno);
|
||||
|
||||
unmount();
|
||||
}
|
||||
|
||||
TEST_CASE("read_only mount: mkdir fails with EROFS", "[fatfs][readonly]")
|
||||
{
|
||||
mount_readonly();
|
||||
|
||||
TEST_ASSERT_EQUAL(-1, mkdir("/spiflash/newdir", 0755));
|
||||
TEST_ASSERT_EQUAL(EROFS, errno);
|
||||
|
||||
unmount();
|
||||
}
|
||||
|
||||
TEST_CASE("read_only mount: unlink fails with EROFS", "[fatfs][readonly]")
|
||||
{
|
||||
mount_readonly();
|
||||
|
||||
TEST_ASSERT_EQUAL(-1, unlink("/spiflash/hello.txt"));
|
||||
TEST_ASSERT_EQUAL(EROFS, errno);
|
||||
|
||||
unmount();
|
||||
}
|
||||
|
||||
TEST_CASE("read_only mount: rename fails with EROFS", "[fatfs][readonly]")
|
||||
{
|
||||
mount_readonly();
|
||||
|
||||
TEST_ASSERT_EQUAL(-1, rename("/spiflash/hello.txt", "/spiflash/bye.txt"));
|
||||
TEST_ASSERT_EQUAL(EROFS, errno);
|
||||
|
||||
unmount();
|
||||
}
|
||||
|
||||
TEST_CASE("read_only mount: stat and opendir still work", "[fatfs][readonly]")
|
||||
{
|
||||
mount_readonly();
|
||||
|
||||
struct stat st;
|
||||
TEST_ASSERT_EQUAL(0, stat("/spiflash/hello.txt", &st));
|
||||
TEST_ASSERT(st.st_mode & S_IFREG);
|
||||
|
||||
TEST_ASSERT_EQUAL(0, stat("/spiflash/testdir", &st));
|
||||
TEST_ASSERT(st.st_mode & S_IFDIR);
|
||||
|
||||
DIR *dir = opendir("/spiflash");
|
||||
TEST_ASSERT_NOT_NULL(dir);
|
||||
TEST_ASSERT_EQUAL(0, closedir(dir));
|
||||
|
||||
unmount();
|
||||
}
|
||||
|
||||
TEST_CASE("read_only mount: format_if_mount_failed and read_only are mutually exclusive", "[fatfs][readonly]")
|
||||
{
|
||||
esp_vfs_fat_mount_config_t cfg = {
|
||||
.format_if_mount_failed = true,
|
||||
.max_files = 5,
|
||||
.read_only = true,
|
||||
};
|
||||
wl_handle_t wl_handle = WL_INVALID_HANDLE;
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG,
|
||||
esp_vfs_fat_spiflash_mount_rw_wl(BASE_PATH, NULL, &cfg, &wl_handle));
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
factory, app, factory, 0x10000, 1M,
|
||||
storage, data, fat, , 528k,
|
||||
|
@@ -0,0 +1,11 @@
|
||||
# SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: CC0-1.0
|
||||
import pytest
|
||||
from pytest_embedded import Dut
|
||||
from pytest_embedded_idf.utils import idf_parametrize
|
||||
|
||||
|
||||
@pytest.mark.generic
|
||||
@idf_parametrize('target', ['esp32', 'esp32c3'], indirect=['target'])
|
||||
def test_fatfs_flash_wl_readonly(dut: Dut) -> None:
|
||||
dut.run_all_single_board_cases()
|
||||
@@ -0,0 +1,14 @@
|
||||
# General options for additional checks
|
||||
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
|
||||
CONFIG_COMPILER_WARN_WRITE_STRINGS=y
|
||||
CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y
|
||||
CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y
|
||||
CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y
|
||||
CONFIG_COMPILER_STACK_CHECK=y
|
||||
|
||||
# disable task watchdog since this app uses an interactive menu
|
||||
CONFIG_ESP_TASK_WDT_INIT=n
|
||||
|
||||
# use custom partition table
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -512,6 +512,19 @@ void esp_vfs_dump_fds(FILE *fp);
|
||||
*/
|
||||
void esp_vfs_dump_registered_paths(FILE *fp);
|
||||
|
||||
/**
|
||||
* @brief Set the read-only flag for a registered VFS entry
|
||||
*
|
||||
* After this call, all write operations (open for write, mkdir, unlink,
|
||||
* rename, etc.) on the given path will fail with errno set to EROFS.
|
||||
*
|
||||
* @param base_path Path prefix of the already-registered VFS entry
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_INVALID_STATE if no VFS entry matches base_path
|
||||
*/
|
||||
esp_err_t esp_vfs_set_readonly_flag(const char *base_path);
|
||||
|
||||
#if !defined(__DOXYGEN__)
|
||||
#pragma pop_macro("deprecated")
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user