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>
diff --git a/ports/stm32/main.c b/ports/stm32/main.c
index d00c2ec..fb10b96 100644
--- a/ports/stm32/main.c
+++ b/ports/stm32/main.c
@@ -164,17 +164,18 @@
     // 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 @@
     #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 @@
 
     // 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),
diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c
index c8805d6..6581860 100644
--- a/ports/stm32/storage.c
+++ b/ports/stm32/storage.c
@@ -250,6 +250,13 @@
 #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
diff --git a/ports/stm32/storage.h b/ports/stm32/storage.h
index 490fc4a..73058aa 100644
--- a/ports/stm32/storage.h
+++ b/ports/stm32/storage.h
@@ -50,6 +50,7 @@
 // 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);