2022-09-28 06:58:48 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-06-08 08:09:00 -04:00
|
|
|
|
|
|
|
#include <string.h>
|
2019-06-19 12:07:51 -04:00
|
|
|
#include "diskio_impl.h"
|
2018-06-08 08:09:00 -04:00
|
|
|
#include "ffconf.h"
|
|
|
|
#include "ff.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "diskio_rawflash.h"
|
2019-12-04 09:01:25 -05:00
|
|
|
#include "esp_compiler.h"
|
2018-06-08 08:09:00 -04:00
|
|
|
|
|
|
|
static const char* TAG = "diskio_rawflash";
|
|
|
|
|
2022-09-28 06:58:48 -04:00
|
|
|
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];
|
|
|
|
|
|
|
|
#define BPB_BytsPerSec 11
|
|
|
|
#define BPB_TotSec16 19
|
|
|
|
#define BPB_TotSec32 32
|
2018-06-08 08:09:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
DSTATUS ff_raw_initialize (BYTE pdrv)
|
|
|
|
{
|
2022-09-28 06:58:48 -04:00
|
|
|
|
|
|
|
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, §or_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, §ors_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, §ors_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;
|
|
|
|
}
|
|
|
|
|
2018-06-08 08:09:00 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DSTATUS ff_raw_status (BYTE pdrv)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-09-28 06:58:48 -04:00
|
|
|
const esp_partition_t* part = s_ff_raw_handles[pdrv];
|
2018-06-08 08:09:00 -04:00
|
|
|
assert(part);
|
2022-09-28 06:58:48 -04:00
|
|
|
esp_err_t err = esp_partition_read(part, sector * s_sector_size[pdrv], buff, count * s_sector_size[pdrv]);
|
2019-10-15 17:01:05 -04:00
|
|
|
if (unlikely(err != ESP_OK)) {
|
2018-06-08 08:09:00 -04:00
|
|
|
ESP_LOGE(TAG, "esp_partition_read failed (0x%x)", err);
|
|
|
|
return RES_ERROR;
|
|
|
|
}
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DRESULT ff_raw_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
|
|
|
|
{
|
|
|
|
return RES_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
DRESULT ff_raw_ioctl (BYTE pdrv, BYTE cmd, void *buff)
|
|
|
|
{
|
2022-09-28 06:58:48 -04:00
|
|
|
const esp_partition_t* part = s_ff_raw_handles[pdrv];
|
2018-06-08 08:09:00 -04:00
|
|
|
ESP_LOGV(TAG, "ff_raw_ioctl: cmd=%in", cmd);
|
|
|
|
assert(part);
|
|
|
|
switch (cmd) {
|
|
|
|
case CTRL_SYNC:
|
|
|
|
return RES_OK;
|
|
|
|
case GET_SECTOR_COUNT:
|
2022-09-28 06:58:48 -04:00
|
|
|
*((DWORD *) buff) = s_sectors_count[pdrv];
|
2018-06-08 08:09:00 -04:00
|
|
|
return RES_OK;
|
|
|
|
case GET_SECTOR_SIZE:
|
2022-09-28 06:58:48 -04:00
|
|
|
*((WORD *) buff) = s_sector_size[pdrv];
|
2018-06-08 08:09:00 -04:00
|
|
|
return RES_OK;
|
|
|
|
case GET_BLOCK_SIZE:
|
|
|
|
return RES_ERROR;
|
|
|
|
}
|
|
|
|
return RES_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t ff_diskio_register_raw_partition(BYTE pdrv, const esp_partition_t* part_handle)
|
|
|
|
{
|
|
|
|
if (pdrv >= FF_VOLUMES) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
static const ff_diskio_impl_t raw_impl = {
|
|
|
|
.init = &ff_raw_initialize,
|
|
|
|
.status = &ff_raw_status,
|
|
|
|
.read = &ff_raw_read,
|
|
|
|
.write = &ff_raw_write,
|
|
|
|
.ioctl = &ff_raw_ioctl
|
|
|
|
};
|
|
|
|
ff_diskio_register(pdrv, &raw_impl);
|
2022-09-28 06:58:48 -04:00
|
|
|
s_ff_raw_handles[pdrv] = part_handle;
|
2018-06-08 08:09:00 -04:00
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BYTE ff_diskio_get_pdrv_raw(const esp_partition_t* part_handle)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < FF_VOLUMES; i++) {
|
2022-09-28 06:58:48 -04:00
|
|
|
if (part_handle == s_ff_raw_handles[i]) {
|
2018-06-08 08:09:00 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0xff;
|
|
|
|
}
|