diff options
author | Damien George <damien@micropython.org> | 2021-01-28 13:13:09 +1100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2021-01-29 15:02:55 +1100 |
commit | d1945cc2b56bb2159224fa7027d0aadb2362430c (patch) | |
tree | 43da1d3eaf2c4b7aca5ff4b18ca58fe8d3e6a179 | |
parent | 71ea438561b737de7d4bb6bc60412a7e409cf298 (diff) |
stm32/main: Check block 0 and 1 when auto-detecting littlefs.
The superblock for littlefs is in block 0 and 1, but block 0 may be erased
or partially written, so block 1 must be checked if block 0 does not have a
valid littlefs superblock in it.
Prior to this commit, if block 0 did not contain a valid littlefs
superblock (but block 1 did) then the auto-detection would fail, mounting a
FAT filesystem would also fail, and the system would reformat the flash,
even though it may have contained a valid littlefs filesystem. This is now
fixed.
Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r-- | ports/stm32/main.c | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/ports/stm32/main.c b/ports/stm32/main.c index fb10b9650..f0a10fa93 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -168,31 +168,35 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { #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[64]; - ret = storage_readblocks_ext(buf, 0, 0, sizeof(buf)); + // Try to detect the block device used for the main filesystem based on the + // contents of the superblock, which can be the first or second block. mp_int_t len = -1; + uint8_t buf[64]; + for (size_t block_num = 0; block_num <= 1; ++block_num) { + ret = storage_readblocks_ext(buf, block_num, 0, sizeof(buf)); + + #if MICROPY_VFS_LFS1 + 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); + uint32_t block_count = lfs1_fromle32(superblock->d.block_count); + len = block_count * block_size; + break; + } + #endif - #if MICROPY_VFS_LFS1 - 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); - uint32_t block_count = lfs1_fromle32(superblock->d.block_count); - len = block_count * block_size; - } - #endif - - #if MICROPY_VFS_LFS2 - 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); - uint32_t block_count = lfs2_fromle32(superblock->block_count); - len = block_count * block_size; + #if MICROPY_VFS_LFS2 + 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); + uint32_t block_count = lfs2_fromle32(superblock->block_count); + len = block_count * block_size; + break; + } + #endif } - #endif if (len != -1) { // Detected a littlefs filesystem so create correct block device for it |