2021-12-01 08:49:38 -05:00
|
|
|
/*
|
2024-01-24 11:51:16 -05:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2021-12-01 08:49:38 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-04-10 10:06:35 -04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-12-27 04:55:37 -05:00
|
|
|
#include "esp_check.h"
|
2017-04-10 10:06:35 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_vfs_fat.h"
|
2018-01-08 03:21:02 -05:00
|
|
|
#include "vfs_fat_internal.h"
|
2019-06-19 12:07:51 -04:00
|
|
|
#include "diskio_impl.h"
|
2018-06-08 08:09:00 -04:00
|
|
|
#include "diskio_rawflash.h"
|
2017-04-10 10:06:35 -04:00
|
|
|
#include "wear_levelling.h"
|
2018-06-08 08:09:00 -04:00
|
|
|
#include "diskio_wl.h"
|
2017-04-10 10:06:35 -04:00
|
|
|
|
2022-03-04 14:15:49 -05:00
|
|
|
static const char* TAG = "vfs_fat_spiflash";
|
|
|
|
|
2023-01-04 01:59:11 -05:00
|
|
|
static vfs_fat_spiflash_ctx_t *s_ctx[FF_VOLUMES] = {};
|
|
|
|
|
2023-07-17 05:59:28 -04:00
|
|
|
extern esp_err_t esp_vfs_set_readonly_flag(const char* base_path); // from vfs/vfs.c to set readonly flag in esp_vfs_t struct externally
|
|
|
|
|
2023-01-04 01:59:11 -05:00
|
|
|
static bool s_get_context_id_by_label(const char *label, uint32_t *out_id)
|
|
|
|
{
|
|
|
|
vfs_fat_spiflash_ctx_t *p_ctx = NULL;
|
|
|
|
for (int i = 0; i < FF_VOLUMES; i++) {
|
|
|
|
p_ctx = s_ctx[i];
|
|
|
|
if (p_ctx) {
|
|
|
|
if (!label && !p_ctx->by_label) {
|
|
|
|
*out_id = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (label && p_ctx->by_label && strncmp(label, p_ctx->partition->label, 20) == 0) {
|
|
|
|
*out_id = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool s_get_context_id_by_wl_handle(wl_handle_t wlhandle, uint32_t *out_id)
|
|
|
|
{
|
|
|
|
vfs_fat_spiflash_ctx_t *p_ctx = NULL;
|
|
|
|
for (int i = 0; i < FF_VOLUMES; i++) {
|
|
|
|
p_ctx = s_ctx[i];
|
|
|
|
if (p_ctx) {
|
|
|
|
if (p_ctx->wlhandle == wlhandle) {
|
|
|
|
*out_id = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t s_get_unused_context_id(void)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < FF_VOLUMES; i++) {
|
|
|
|
if (!s_ctx[i]) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FF_VOLUMES;
|
|
|
|
}
|
|
|
|
|
2024-01-24 11:51:16 -05:00
|
|
|
vfs_fat_spiflash_ctx_t* get_vfs_fat_spiflash_ctx(wl_handle_t wlhandle)
|
|
|
|
{
|
|
|
|
uint32_t id = FF_VOLUMES;
|
|
|
|
if (s_get_context_id_by_wl_handle(wlhandle, &id)) {
|
|
|
|
return s_ctx[id];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t s_f_mount_rw(FATFS *fs, const char *drv, const esp_vfs_fat_mount_config_t *mount_config, vfs_fat_x_ctx_flags_t *out_flags)
|
2023-01-04 01:59:11 -05:00
|
|
|
{
|
|
|
|
FRESULT fresult = f_mount(fs, drv, 1);
|
|
|
|
if (fresult != FR_OK) {
|
|
|
|
ESP_LOGW(TAG, "f_mount failed (%d)", fresult);
|
|
|
|
|
|
|
|
bool need_mount_again = (fresult == FR_NO_FILESYSTEM || fresult == FR_INT_ERR) && mount_config->format_if_mount_failed;
|
|
|
|
if (!need_mount_again) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t workbuf_size = 4096;
|
|
|
|
void *workbuf = ff_memalloc(workbuf_size);
|
|
|
|
if (workbuf == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(CONFIG_WL_SECTOR_SIZE, mount_config->allocation_unit_size);
|
|
|
|
ESP_LOGI(TAG, "Formatting FATFS partition, allocation unit size=%d", alloc_unit_size);
|
2024-01-14 19:34:02 -05:00
|
|
|
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), (mount_config->use_one_fat ? 1 : 2), 0, 0, alloc_unit_size};
|
2023-01-04 01:59:11 -05:00
|
|
|
fresult = f_mkfs(drv, &opt, workbuf, workbuf_size);
|
|
|
|
free(workbuf);
|
|
|
|
workbuf = NULL;
|
|
|
|
ESP_RETURN_ON_FALSE(fresult == FR_OK, ESP_FAIL, TAG, "f_mkfs failed (%d)", fresult);
|
|
|
|
|
2024-01-24 11:51:16 -05:00
|
|
|
if (out_flags) {
|
|
|
|
*out_flags |= FORMATTED_DURING_LAST_MOUNT; // set flag
|
|
|
|
}
|
|
|
|
|
2023-01-04 01:59:11 -05:00
|
|
|
ESP_LOGI(TAG, "Mounting again");
|
|
|
|
fresult = f_mount(fs, drv, 0);
|
|
|
|
ESP_RETURN_ON_FALSE(fresult == FR_OK, ESP_FAIL, TAG, "f_mount failed after formatting (%d)", fresult);
|
2024-01-24 11:51:16 -05:00
|
|
|
} else {
|
|
|
|
if (out_flags) {
|
|
|
|
*out_flags &= ~FORMATTED_DURING_LAST_MOUNT; // reset flag
|
|
|
|
}
|
2023-01-04 01:59:11 -05:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:15:49 -05:00
|
|
|
esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path,
|
2017-04-10 10:06:35 -04:00
|
|
|
const char* partition_label,
|
|
|
|
const esp_vfs_fat_mount_config_t* mount_config,
|
|
|
|
wl_handle_t* wl_handle)
|
|
|
|
{
|
2022-12-27 04:55:37 -05:00
|
|
|
esp_err_t ret = ESP_OK;
|
2023-01-04 01:59:11 -05:00
|
|
|
vfs_fat_spiflash_ctx_t *ctx = NULL;
|
|
|
|
uint32_t ctx_id = FF_VOLUMES;
|
2017-04-10 10:06:35 -04:00
|
|
|
|
2017-09-07 10:37:59 -04:00
|
|
|
esp_partition_subtype_t subtype = partition_label ?
|
|
|
|
ESP_PARTITION_SUBTYPE_ANY : ESP_PARTITION_SUBTYPE_DATA_FAT;
|
|
|
|
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
|
|
|
subtype, partition_label);
|
2022-12-27 04:55:37 -05:00
|
|
|
ESP_RETURN_ON_FALSE(data_partition, ESP_ERR_NOT_FOUND, TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
|
|
|
|
|
|
|
ESP_RETURN_ON_ERROR(wl_mount(data_partition, wl_handle), TAG, "failed to mount wear levelling layer. ret = %i", ret);
|
2017-04-10 10:06:35 -04:00
|
|
|
|
|
|
|
// connect driver to FATFS
|
|
|
|
BYTE pdrv = 0xFF;
|
2017-05-04 07:57:11 -04:00
|
|
|
if (ff_diskio_get_drive(&pdrv) != ESP_OK) {
|
2017-04-10 10:06:35 -04:00
|
|
|
ESP_LOGD(TAG, "the maximum count of volumes is already mounted");
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
2017-05-04 07:57:11 -04:00
|
|
|
ESP_LOGD(TAG, "using pdrv=%i", pdrv);
|
2017-04-10 10:06:35 -04:00
|
|
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
2022-12-27 04:55:37 -05:00
|
|
|
ESP_GOTO_ON_ERROR(ff_diskio_register_wl_partition(pdrv, *wl_handle), fail, TAG, "ff_diskio_register_wl_partition failed pdrv=%i, error - 0x(%x)", pdrv, ret);
|
2017-04-10 10:06:35 -04:00
|
|
|
|
|
|
|
FATFS *fs;
|
2024-02-07 12:33:18 -05:00
|
|
|
esp_vfs_fat_conf_t conf = {
|
|
|
|
.base_path = base_path,
|
|
|
|
.fat_drive = drv,
|
|
|
|
.max_files = mount_config->max_files,
|
|
|
|
};
|
|
|
|
ret = esp_vfs_fat_register_cfg(&conf, &fs);
|
2022-12-27 04:55:37 -05:00
|
|
|
if (ret == ESP_ERR_INVALID_STATE) {
|
2017-04-10 10:06:35 -04:00
|
|
|
// it's okay, already registered with VFS
|
2022-12-27 04:55:37 -05:00
|
|
|
} else if (ret != ESP_OK) {
|
2024-02-07 12:33:18 -05:00
|
|
|
ESP_LOGD(TAG, "esp_vfs_fat_register_cfg failed 0x(%x)", ret);
|
2017-04-10 10:06:35 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2024-01-24 11:51:16 -05:00
|
|
|
vfs_fat_x_ctx_flags_t flags = 0;
|
|
|
|
|
2017-04-10 10:06:35 -04:00
|
|
|
// Try to mount partition
|
2024-01-24 11:51:16 -05:00
|
|
|
ret = s_f_mount_rw(fs, drv, mount_config, &flags);
|
2023-01-04 01:59:11 -05:00
|
|
|
if (ret != ESP_OK) {
|
|
|
|
goto fail;
|
2017-04-10 10:06:35 -04:00
|
|
|
}
|
2023-01-04 01:59:11 -05:00
|
|
|
|
|
|
|
ctx = calloc(sizeof(vfs_fat_spiflash_ctx_t), 1);
|
|
|
|
ESP_GOTO_ON_FALSE(ctx, ESP_ERR_NO_MEM, fail, TAG, "no mem");
|
|
|
|
ctx->partition = data_partition;
|
|
|
|
ctx->by_label = (partition_label != NULL);
|
|
|
|
ctx->pdrv = pdrv;
|
|
|
|
ctx->fs = fs;
|
|
|
|
ctx->wlhandle = *wl_handle;
|
2024-01-24 11:51:16 -05:00
|
|
|
ctx->flags = flags;
|
2023-01-04 01:59:11 -05:00
|
|
|
memcpy(&ctx->mount_config, mount_config, sizeof(esp_vfs_fat_mount_config_t));
|
|
|
|
ctx_id = s_get_unused_context_id();
|
|
|
|
//At this stage, we should always get a free context, otherwise program should return already
|
2023-02-17 03:22:10 -05:00
|
|
|
assert(ctx_id != FF_VOLUMES);
|
2023-01-04 01:59:11 -05:00
|
|
|
s_ctx[ctx_id] = ctx;
|
|
|
|
|
2023-07-17 05:59:28 -04:00
|
|
|
if (data_partition->readonly) {
|
|
|
|
esp_vfs_set_readonly_flag(base_path);
|
|
|
|
}
|
|
|
|
|
2017-04-10 10:06:35 -04:00
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
esp_vfs_fat_unregister_path(base_path);
|
|
|
|
ff_diskio_unregister(pdrv);
|
2023-01-04 01:59:11 -05:00
|
|
|
free(ctx);
|
2022-12-27 04:55:37 -05:00
|
|
|
return ret;
|
2017-04-10 10:06:35 -04:00
|
|
|
}
|
|
|
|
|
2022-03-04 14:15:49 -05:00
|
|
|
esp_err_t esp_vfs_fat_spiflash_unmount_rw_wl(const char* base_path, wl_handle_t wl_handle)
|
2017-04-10 10:06:35 -04:00
|
|
|
{
|
2017-05-04 07:57:11 -04:00
|
|
|
BYTE pdrv = ff_diskio_get_pdrv_wl(wl_handle);
|
2022-12-27 04:55:37 -05:00
|
|
|
ESP_RETURN_ON_FALSE(pdrv != 0xff, ESP_ERR_INVALID_STATE, TAG, "partition isn't registered, call esp_vfs_fat_spiflash_mount_rw_wl first");
|
2017-04-10 10:06:35 -04:00
|
|
|
|
2023-02-17 03:22:10 -05:00
|
|
|
uint32_t id = FF_VOLUMES;
|
|
|
|
ESP_RETURN_ON_FALSE(s_get_context_id_by_wl_handle(wl_handle, &id), ESP_ERR_INVALID_STATE, TAG, "partition isn't registered, call esp_vfs_fat_spiflash_mount_rw_wl first");
|
|
|
|
//At this stage, as the wl_handle is valid, we should always get its context id, otherwise program should return already
|
|
|
|
assert(id != FF_VOLUMES);
|
|
|
|
|
2022-12-27 04:55:37 -05:00
|
|
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
2017-04-10 10:06:35 -04:00
|
|
|
f_mount(0, drv, 0);
|
2017-05-04 07:57:11 -04:00
|
|
|
ff_diskio_unregister(pdrv);
|
2018-06-12 04:06:13 -04:00
|
|
|
ff_diskio_clear_pdrv_wl(wl_handle);
|
2017-04-10 10:06:35 -04:00
|
|
|
// release partition driver
|
|
|
|
esp_err_t err_drv = wl_unmount(wl_handle);
|
|
|
|
esp_err_t err = esp_vfs_fat_unregister_path(base_path);
|
2023-01-04 01:59:11 -05:00
|
|
|
if (err == ESP_OK) {
|
|
|
|
err = err_drv;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(s_ctx[id]);
|
|
|
|
s_ctx[id] = NULL;
|
|
|
|
|
2017-04-10 10:06:35 -04:00
|
|
|
return err;
|
|
|
|
}
|
2018-06-08 08:09:00 -04:00
|
|
|
|
2024-01-24 11:51:16 -05:00
|
|
|
esp_err_t esp_vfs_fat_spiflash_format_cfg_rw_wl(const char* base_path, const char* partition_label, esp_vfs_fat_mount_config_t *cfg)
|
2023-01-04 01:59:11 -05:00
|
|
|
{
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
bool partition_was_mounted = false;
|
|
|
|
|
|
|
|
wl_handle_t temp_handle = WL_INVALID_HANDLE;
|
|
|
|
uint32_t id = FF_VOLUMES;
|
|
|
|
|
|
|
|
bool found = s_get_context_id_by_label(partition_label, &id);
|
|
|
|
if (!found) {
|
2024-01-24 11:51:16 -05:00
|
|
|
esp_vfs_fat_mount_config_t default_mount_config = {
|
2023-01-04 01:59:11 -05:00
|
|
|
.max_files = 1,
|
|
|
|
.format_if_mount_failed = true,
|
|
|
|
};
|
2024-01-24 11:51:16 -05:00
|
|
|
esp_vfs_fat_mount_config_t *mount_cfg = NULL;
|
|
|
|
if (cfg) {
|
|
|
|
mount_cfg = cfg;
|
|
|
|
} else {
|
|
|
|
mount_cfg = &default_mount_config;
|
|
|
|
}
|
|
|
|
ESP_RETURN_ON_ERROR(esp_vfs_fat_spiflash_mount_rw_wl(base_path, partition_label, mount_cfg, &temp_handle), TAG, "Failed to mount");
|
2023-01-04 01:59:11 -05:00
|
|
|
found = s_get_context_id_by_label(partition_label, &id);
|
|
|
|
assert(found);
|
2024-01-24 11:51:16 -05:00
|
|
|
if (s_ctx[id]->flags & FORMATTED_DURING_LAST_MOUNT) {
|
|
|
|
ESP_LOGD(TAG, "partition was formatted during mounting, skipping another format");
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2023-01-04 01:59:11 -05:00
|
|
|
} else {
|
|
|
|
partition_was_mounted = true;
|
2024-01-24 11:51:16 -05:00
|
|
|
if (cfg) {
|
|
|
|
s_ctx[id]->mount_config = *cfg;
|
|
|
|
}
|
2023-01-04 01:59:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//unmount
|
2023-11-16 15:21:33 -05:00
|
|
|
char drv[3] = {(char)('0' + s_ctx[id]->pdrv), ':', 0};
|
|
|
|
FRESULT fresult = f_mount(0, drv, 0);
|
|
|
|
ESP_RETURN_ON_FALSE(fresult != FR_INVALID_DRIVE, ESP_FAIL, TAG, "f_mount unmount failed (%d) - the logical drive number is invalid", fresult);
|
|
|
|
ESP_GOTO_ON_FALSE(fresult == FR_OK, ESP_FAIL, recycle, TAG, "f_mount unmount failed (%d), go to recycle", fresult);
|
2023-01-04 01:59:11 -05:00
|
|
|
|
|
|
|
const size_t workbuf_size = 4096;
|
|
|
|
void *workbuf = ff_memalloc(workbuf_size);
|
|
|
|
if (workbuf == NULL) {
|
|
|
|
ret = ESP_ERR_NO_MEM;
|
|
|
|
goto mount_back;
|
|
|
|
}
|
|
|
|
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(CONFIG_WL_SECTOR_SIZE, s_ctx[id]->mount_config.allocation_unit_size);
|
|
|
|
ESP_LOGI(TAG, "Formatting FATFS partition, allocation unit size=%d", alloc_unit_size);
|
2024-01-14 19:34:02 -05:00
|
|
|
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), (s_ctx[id]->mount_config.use_one_fat ? 1 : 2), 0, 0, alloc_unit_size};
|
2023-11-16 15:21:33 -05:00
|
|
|
fresult = f_mkfs(drv, &opt, workbuf, workbuf_size);
|
2023-01-04 01:59:11 -05:00
|
|
|
free(workbuf);
|
|
|
|
workbuf = NULL;
|
|
|
|
ESP_GOTO_ON_FALSE(fresult == FR_OK, ESP_FAIL, mount_back, TAG, "f_mkfs failed (%d)", fresult);
|
|
|
|
|
|
|
|
mount_back:
|
|
|
|
if (partition_was_mounted) {
|
2024-01-24 11:51:16 -05:00
|
|
|
esp_err_t err = s_f_mount_rw(s_ctx[id]->fs, drv, &s_ctx[id]->mount_config, NULL);
|
2023-01-04 01:59:11 -05:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "failed to mount back, go to recycle");
|
|
|
|
goto recycle;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
esp_vfs_fat_spiflash_unmount_rw_wl(base_path, s_ctx[id]->wlhandle);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
recycle:
|
|
|
|
ff_diskio_unregister(s_ctx[id]->pdrv);
|
|
|
|
ff_diskio_clear_pdrv_wl(s_ctx[id]->wlhandle);
|
|
|
|
wl_unmount(s_ctx[id]->wlhandle);
|
|
|
|
esp_vfs_fat_unregister_path(base_path);
|
|
|
|
free(s_ctx[id]);
|
|
|
|
s_ctx[id] = NULL;
|
|
|
|
ESP_LOGE(TAG, "failed to format, resources recycled, please mount again");
|
|
|
|
return ret;
|
|
|
|
}
|
2022-12-27 04:55:37 -05:00
|
|
|
|
2024-01-24 11:51:16 -05:00
|
|
|
esp_err_t esp_vfs_fat_spiflash_format_rw_wl(const char* base_path, const char* partition_label)
|
|
|
|
{
|
|
|
|
return esp_vfs_fat_spiflash_format_cfg_rw_wl(base_path, partition_label, NULL);
|
|
|
|
}
|
2022-03-04 14:15:49 -05:00
|
|
|
|
|
|
|
esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
|
2018-06-08 08:09:00 -04:00
|
|
|
const char* partition_label,
|
|
|
|
const esp_vfs_fat_mount_config_t* mount_config)
|
|
|
|
{
|
2022-12-27 04:55:37 -05:00
|
|
|
esp_err_t ret = ESP_OK;
|
2018-06-08 08:09:00 -04:00
|
|
|
|
|
|
|
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
|
|
|
ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label);
|
2022-12-27 04:55:37 -05:00
|
|
|
ESP_RETURN_ON_FALSE(data_partition, ESP_ERR_NOT_FOUND, TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
2018-06-08 08:09:00 -04:00
|
|
|
|
|
|
|
// connect driver to FATFS
|
|
|
|
BYTE pdrv = 0xFF;
|
|
|
|
if (ff_diskio_get_drive(&pdrv) != ESP_OK) {
|
|
|
|
ESP_LOGD(TAG, "the maximum count of volumes is already mounted");
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
ESP_LOGD(TAG, "using pdrv=%i", pdrv);
|
|
|
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
2022-12-27 04:55:37 -05:00
|
|
|
ESP_GOTO_ON_ERROR(ff_diskio_register_raw_partition(pdrv, data_partition), fail, TAG, "ff_diskio_register_raw_partition failed pdrv=%i, error - 0x(%x)", pdrv, ret);
|
2018-06-08 08:09:00 -04:00
|
|
|
|
|
|
|
FATFS *fs;
|
2024-02-07 12:33:18 -05:00
|
|
|
esp_vfs_fat_conf_t conf = {
|
|
|
|
.base_path = base_path,
|
|
|
|
.fat_drive = drv,
|
|
|
|
.max_files = mount_config->max_files,
|
|
|
|
};
|
|
|
|
ret = esp_vfs_fat_register_cfg(&conf, &fs);
|
2022-12-27 04:55:37 -05:00
|
|
|
if (ret == ESP_ERR_INVALID_STATE) {
|
2018-06-08 08:09:00 -04:00
|
|
|
// it's okay, already registered with VFS
|
2022-12-27 04:55:37 -05:00
|
|
|
} else if (ret != ESP_OK) {
|
2024-02-07 12:33:18 -05:00
|
|
|
ESP_LOGD(TAG, "esp_vfs_fat_register_cfg failed 0x(%x)", ret);
|
2018-06-08 08:09:00 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to mount partition
|
|
|
|
FRESULT fresult = f_mount(fs, drv, 1);
|
|
|
|
if (fresult != FR_OK) {
|
|
|
|
ESP_LOGW(TAG, "f_mount failed (%d)", fresult);
|
2022-12-27 04:55:37 -05:00
|
|
|
ret = ESP_FAIL;
|
2018-06-08 08:09:00 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
2023-07-17 05:59:28 -04:00
|
|
|
|
|
|
|
if (data_partition->readonly) {
|
|
|
|
esp_vfs_set_readonly_flag(base_path);
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:09:00 -04:00
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
esp_vfs_fat_unregister_path(base_path);
|
|
|
|
ff_diskio_unregister(pdrv);
|
2022-12-27 04:55:37 -05:00
|
|
|
return ret;
|
2018-06-08 08:09:00 -04:00
|
|
|
}
|
|
|
|
|
2022-03-04 14:15:49 -05:00
|
|
|
esp_err_t esp_vfs_fat_spiflash_unmount_ro(const char* base_path, const char* partition_label)
|
2018-06-08 08:09:00 -04:00
|
|
|
{
|
|
|
|
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
|
|
|
ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label);
|
2022-12-27 04:55:37 -05:00
|
|
|
ESP_RETURN_ON_FALSE(data_partition, ESP_ERR_NOT_FOUND, TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
2018-06-08 08:09:00 -04:00
|
|
|
|
|
|
|
BYTE pdrv = ff_diskio_get_pdrv_raw(data_partition);
|
2022-12-27 04:55:37 -05:00
|
|
|
ESP_RETURN_ON_FALSE(pdrv != 0xff, ESP_ERR_INVALID_STATE, TAG, "partition isn't registered, call esp_vfs_fat_spiflash_mount_ro first");
|
2018-06-08 08:09:00 -04:00
|
|
|
|
2022-12-27 04:55:37 -05:00
|
|
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
2018-06-08 08:09:00 -04:00
|
|
|
f_mount(0, drv, 0);
|
|
|
|
ff_diskio_unregister(pdrv);
|
|
|
|
esp_err_t err = esp_vfs_fat_unregister_path(base_path);
|
|
|
|
return err;
|
|
|
|
}
|
2022-03-04 14:15:49 -05:00
|
|
|
|
|
|
|
esp_err_t esp_vfs_fat_spiflash_mount(const char* base_path,
|
|
|
|
const char* partition_label,
|
|
|
|
const esp_vfs_fat_mount_config_t* mount_config,
|
|
|
|
wl_handle_t* wl_handle)
|
|
|
|
__attribute__((alias("esp_vfs_fat_spiflash_mount_rw_wl")));
|
|
|
|
esp_err_t esp_vfs_fat_spiflash_unmount(const char* base_path, wl_handle_t wl_handle)
|
|
|
|
__attribute__((alias("esp_vfs_fat_spiflash_unmount_rw_wl")));
|
|
|
|
esp_err_t esp_vfs_fat_rawflash_mount(const char* base_path,
|
|
|
|
const char* partition_label,
|
|
|
|
const esp_vfs_fat_mount_config_t* mount_config)
|
|
|
|
__attribute__((alias("esp_vfs_fat_spiflash_mount_ro")));
|
|
|
|
esp_err_t esp_vfs_fat_rawflash_unmount(const char* base_path, const char* partition_label)
|
|
|
|
__attribute__((alias("esp_vfs_fat_spiflash_unmount_ro")));
|