fatfs: raw diskio: Fixed handling read-only filesystem

ff_ routines incorrectly reported disk state and caused whole fatfs
to lock-up when trying to write to read-only device.

Signed-off-by: Michal Jenikovsky <jendo@jmsystems.sk>
This commit is contained in:
Michal Jenikovsky 2023-08-10 11:56:02 +02:00 committed by BOT
parent 5dafc957ef
commit b1e5ef2c47

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -11,30 +11,71 @@
#include "esp_log.h"
#include "diskio_rawflash.h"
#include "esp_compiler.h"
#include "spi_flash_mmap.h"
static const char* TAG = "diskio_rawflash";
const esp_partition_t* ff_raw_handles[FF_VOLUMES];
static const esp_partition_t* s_ff_raw_handles[FF_VOLUMES];
// Determine the sector size and sector count by parsing the boot sector
static size_t s_sector_size[FF_VOLUMES];
static size_t s_sectors_count[FF_VOLUMES];
static uint8_t s_initialized[FF_VOLUMES];
#define BPB_BytsPerSec 11
#define BPB_TotSec16 19
#define BPB_TotSec32 32
DSTATUS ff_raw_initialize (BYTE pdrv)
{
return 0;
uint16_t sector_size_tmp;
uint16_t sectors_count_tmp_16;
uint32_t sectors_count_tmp_32;
const esp_partition_t* part = s_ff_raw_handles[pdrv];
assert(part);
esp_err_t err = esp_partition_read(part, BPB_BytsPerSec, &sector_size_tmp, sizeof(sector_size_tmp));
if (unlikely(err != ESP_OK)) {
ESP_LOGE(TAG, "esp_partition_read failed (0x%x)", err);
return RES_ERROR;
}
s_sector_size[pdrv] = sector_size_tmp;
err = esp_partition_read(part, BPB_TotSec16, &sectors_count_tmp_16, sizeof(sectors_count_tmp_16));
if (unlikely(err != ESP_OK)) {
ESP_LOGE(TAG, "esp_partition_read failed (0x%x)", err);
return RES_ERROR;
}
s_sectors_count[pdrv] = sectors_count_tmp_16;
// For FAT32, the number of sectors is stored in a different field
if (sectors_count_tmp_16 == 0){
err = esp_partition_read(part, BPB_TotSec32, &sectors_count_tmp_32, sizeof(sectors_count_tmp_32));
if (unlikely(err != ESP_OK)) {
ESP_LOGE(TAG, "esp_partition_read failed (0x%x)", err);
return RES_ERROR;
}
s_sectors_count[pdrv] = sectors_count_tmp_32;
}
s_initialized[pdrv] = true;
return STA_PROTECT;
}
DSTATUS ff_raw_status (BYTE pdrv)
{
return 0;
DSTATUS status = STA_PROTECT;
if (!s_initialized[pdrv]) {
status |= STA_NOINIT | STA_NODISK;
}
return status;
}
DRESULT ff_raw_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
{
ESP_LOGV(TAG, "ff_raw_read - pdrv=%i, sector=%i, count=%in", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count);
const esp_partition_t* part = ff_raw_handles[pdrv];
const esp_partition_t* part = s_ff_raw_handles[pdrv];
assert(part);
esp_err_t err = esp_partition_read(part, sector * SPI_FLASH_SEC_SIZE, buff, count * SPI_FLASH_SEC_SIZE);
esp_err_t err = esp_partition_read(part, sector * s_sector_size[pdrv], buff, count * s_sector_size[pdrv]);
if (unlikely(err != ESP_OK)) {
ESP_LOGE(TAG, "esp_partition_read failed (0x%x)", err);
return RES_ERROR;
@ -42,25 +83,24 @@ DRESULT ff_raw_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
return RES_OK;
}
DRESULT ff_raw_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
{
return RES_ERROR;
return RES_WRPRT;
}
DRESULT ff_raw_ioctl (BYTE pdrv, BYTE cmd, void *buff)
{
const esp_partition_t* part = ff_raw_handles[pdrv];
const esp_partition_t* part = s_ff_raw_handles[pdrv];
ESP_LOGV(TAG, "ff_raw_ioctl: cmd=%in", cmd);
assert(part);
switch (cmd) {
case CTRL_SYNC:
return RES_OK;
case GET_SECTOR_COUNT:
*((DWORD *) buff) = part->size / SPI_FLASH_SEC_SIZE;
*((DWORD *) buff) = s_sectors_count[pdrv];
return RES_OK;
case GET_SECTOR_SIZE:
*((WORD *) buff) = SPI_FLASH_SEC_SIZE;
*((WORD *) buff) = s_sector_size[pdrv];
return RES_OK;
case GET_BLOCK_SIZE:
return RES_ERROR;
@ -82,7 +122,7 @@ esp_err_t ff_diskio_register_raw_partition(BYTE pdrv, const esp_partition_t* par
.ioctl = &ff_raw_ioctl
};
ff_diskio_register(pdrv, &raw_impl);
ff_raw_handles[pdrv] = part_handle;
s_ff_raw_handles[pdrv] = part_handle;
return ESP_OK;
}
@ -91,7 +131,7 @@ esp_err_t ff_diskio_register_raw_partition(BYTE pdrv, const esp_partition_t* par
BYTE ff_diskio_get_pdrv_raw(const esp_partition_t* part_handle)
{
for (int i = 0; i < FF_VOLUMES; i++) {
if (part_handle == ff_raw_handles[i]) {
if (part_handle == s_ff_raw_handles[i]) {
return i;
}
}