stm32/main: Do extended readblocks call when auto-detecting littlefs.

When littlefs is enabled extended reading must be supported, and using this
function to read the first block for auto-detection is more efficient (a
smaller read) and does not require a cached SPI-flash read.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2020-12-17 15:09:53 +11:00
parent 80883a82c0
commit 061cb1a73a
3 changed files with 15 additions and 6 deletions

View File

@ -164,17 +164,18 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
// Default block device to entire flash storage
mp_obj_t bdev = MP_OBJ_FROM_PTR(&pyb_flash_obj);
int ret;
#if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2
// Try to detect the block device used for the main filesystem, based on the first block
uint8_t buf[FLASH_BLOCK_SIZE];
storage_read_blocks(buf, FLASH_PART1_START_BLOCK, 1);
uint8_t buf[64];
ret = storage_readblocks_ext(buf, 0, 0, sizeof(buf));
mp_int_t len = -1;
#if MICROPY_VFS_LFS1
if (memcmp(&buf[40], "littlefs", 8) == 0) {
if (ret == 0 && memcmp(&buf[40], "littlefs", 8) == 0) {
// LFS1
lfs1_superblock_t *superblock = (void *)&buf[12];
uint32_t block_size = lfs1_fromle32(superblock->d.block_size);
@ -184,7 +185,7 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
#endif
#if MICROPY_VFS_LFS2
if (memcmp(&buf[8], "littlefs", 8) == 0) {
if (ret == 0 && memcmp(&buf[8], "littlefs", 8) == 0) {
// LFS2
lfs2_superblock_t *superblock = (void *)&buf[20];
uint32_t block_size = lfs2_fromle32(superblock->block_size);
@ -203,7 +204,7 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
// Try to mount the flash on "/flash" and chdir to it for the boot-up directory.
mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash);
int ret = mp_vfs_mount_and_chdir_protected(bdev, mount_point);
ret = mp_vfs_mount_and_chdir_protected(bdev, mount_point);
if (ret == -MP_ENODEV && bdev == MP_OBJ_FROM_PTR(&pyb_flash_obj) && reset_mode != 3) {
// No filesystem, bdev is still the default (so didn't detect a possibly corrupt littlefs),

View File

@ -250,6 +250,13 @@ int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_bl
#define PYB_FLASH_NATIVE_BLOCK_SIZE (FLASH_BLOCK_SIZE)
#endif
#if defined(MICROPY_HW_BDEV_READBLOCKS_EXT)
// Size of blocks is PYB_FLASH_NATIVE_BLOCK_SIZE
int storage_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len) {
return MICROPY_HW_BDEV_READBLOCKS_EXT(dest, block, offset, len);
}
#endif
typedef struct _pyb_flash_obj_t {
mp_obj_base_t base;
uint32_t start; // in bytes

View File

@ -50,6 +50,7 @@ bool storage_write_block(const uint8_t *src, uint32_t block);
// these return 0 on success, negative errno on error
int storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks);
int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks);
int storage_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len);
int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg);
bool flash_bdev_readblock(uint8_t *dest, uint32_t block);