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>
This commit is contained in:
parent
71ea438561
commit
d1945cc2b5
|
@ -168,11 +168,12 @@ 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) {
|
||||
|
@ -181,6 +182,7 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
|
|||
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
|
||||
|
||||
|
@ -191,8 +193,10 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
|
|||
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
|
||||
}
|
||||
|
||||
if (len != -1) {
|
||||
// Detected a littlefs filesystem so create correct block device for it
|
||||
|
|
Loading…
Reference in New Issue