2021-12-01 08:49:38 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-04-10 10:06:35 -04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_vfs.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"
|
2017-04-10 10:06:35 -04:00
|
|
|
|
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";
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
esp_err_t result = ESP_OK;
|
|
|
|
const size_t workbuf_size = 4096;
|
|
|
|
void *workbuf = NULL;
|
|
|
|
|
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);
|
2017-04-10 10:06:35 -04:00
|
|
|
if (data_partition == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
|
|
|
return ESP_ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = wl_mount(data_partition, wl_handle);
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "failed to mount wear levelling layer. result = %i", result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// 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};
|
|
|
|
|
|
|
|
result = ff_diskio_register_wl_partition(pdrv, *wl_handle);
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "ff_diskio_register_wl_partition failed pdrv=%i, error - 0x(%x)", pdrv, result);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
FATFS *fs;
|
|
|
|
result = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
|
|
|
|
if (result == ESP_ERR_INVALID_STATE) {
|
|
|
|
// it's okay, already registered with VFS
|
|
|
|
} else if (result != ESP_OK) {
|
|
|
|
ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", result);
|
|
|
|
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);
|
2019-08-26 11:00:24 -04:00
|
|
|
if (!((fresult == FR_NO_FILESYSTEM || fresult == FR_INT_ERR)
|
|
|
|
&& mount_config->format_if_mount_failed)) {
|
2017-04-10 10:06:35 -04:00
|
|
|
result = ESP_FAIL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-11-28 12:08:59 -05:00
|
|
|
workbuf = ff_memalloc(workbuf_size);
|
2018-01-08 03:21:44 -05:00
|
|
|
if (workbuf == NULL) {
|
|
|
|
result = ESP_ERR_NO_MEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-01-08 03:21:02 -05:00
|
|
|
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);
|
2021-12-01 08:49:38 -05:00
|
|
|
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 0, alloc_unit_size};
|
|
|
|
fresult = f_mkfs(drv, &opt, workbuf, workbuf_size);
|
2017-04-10 10:06:35 -04:00
|
|
|
if (fresult != FR_OK) {
|
|
|
|
result = ESP_FAIL;
|
|
|
|
ESP_LOGE(TAG, "f_mkfs failed (%d)", fresult);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
free(workbuf);
|
2017-07-25 08:14:23 -04:00
|
|
|
workbuf = NULL;
|
2017-04-10 10:06:35 -04:00
|
|
|
ESP_LOGI(TAG, "Mounting again");
|
2024-07-23 09:44:40 -04:00
|
|
|
fresult = f_mount(fs, drv, 1);
|
2017-04-10 10:06:35 -04:00
|
|
|
if (fresult != FR_OK) {
|
|
|
|
result = ESP_FAIL;
|
|
|
|
ESP_LOGE(TAG, "f_mount failed after formatting (%d)", fresult);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free(workbuf);
|
|
|
|
esp_vfs_fat_unregister_path(base_path);
|
|
|
|
ff_diskio_unregister(pdrv);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (pdrv == 0xff) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
if (err == ESP_OK) err = err_drv;
|
|
|
|
return err;
|
|
|
|
}
|
2018-06-08 08:09:00 -04:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
esp_err_t result = ESP_OK;
|
|
|
|
|
|
|
|
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
|
|
|
ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label);
|
|
|
|
if (data_partition == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
|
|
|
return ESP_ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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};
|
|
|
|
|
|
|
|
result = ff_diskio_register_raw_partition(pdrv, data_partition);
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "ff_diskio_register_raw_partition failed pdrv=%i, error - 0x(%x)", pdrv, result);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
FATFS *fs;
|
|
|
|
result = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
|
|
|
|
if (result == ESP_ERR_INVALID_STATE) {
|
|
|
|
// it's okay, already registered with VFS
|
|
|
|
} else if (result != ESP_OK) {
|
|
|
|
ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", result);
|
|
|
|
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);
|
|
|
|
result = ESP_FAIL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
esp_vfs_fat_unregister_path(base_path);
|
|
|
|
ff_diskio_unregister(pdrv);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (data_partition == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
|
|
|
return ESP_ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
BYTE pdrv = ff_diskio_get_pdrv_raw(data_partition);
|
|
|
|
if (pdrv == 0xff) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
|
|
|
|
|
|
|
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")));
|