aboutsummaryrefslogtreecommitdiff
path: root/extmod/vfs.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-10-29 17:16:30 +1100
committerDamien George <damien.p.george@gmail.com>2019-11-06 12:15:34 +1100
commit9b27069e2f6d5e754adc62b3aa20e14f5b10de66 (patch)
treecbce109edf07b9d25bd1b73a0a201a8dba50f717 /extmod/vfs.c
parent2f71d66ef7f6bfa93bdc51ab0eaf32cd03a81189 (diff)
extmod/vfs: Add autodetect of littlefs filesystem when mounting.
Diffstat (limited to 'extmod/vfs.c')
-rw-r--r--extmod/vfs.c47
1 files changed, 43 insertions, 4 deletions
diff --git a/extmod/vfs.c b/extmod/vfs.c
index f99be3098..5f5fc633d 100644
--- a/extmod/vfs.c
+++ b/extmod/vfs.c
@@ -38,6 +38,10 @@
#include "extmod/vfs_fat.h"
#endif
+#if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2
+#include "extmod/vfs_lfs.h"
+#endif
+
#if MICROPY_VFS_POSIX
#include "extmod/vfs_posix.h"
#endif
@@ -156,6 +160,44 @@ mp_import_stat_t mp_vfs_import_stat(const char *path) {
}
}
+STATIC mp_obj_t mp_vfs_autodetect(mp_obj_t bdev_obj) {
+ #if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2
+ nlr_buf_t nlr;
+ if (nlr_push(&nlr) == 0) {
+ mp_obj_t vfs = MP_OBJ_NULL;
+ mp_vfs_blockdev_t blockdev;
+ mp_vfs_blockdev_init(&blockdev, bdev_obj);
+ uint8_t buf[44];
+ mp_vfs_blockdev_read_ext(&blockdev, 0, 8, sizeof(buf), buf);
+ #if MICROPY_VFS_LFS1
+ if (memcmp(&buf[32], "littlefs", 8) == 0) {
+ // LFS1
+ vfs = mp_type_vfs_lfs1.make_new(&mp_type_vfs_lfs1, 1, 0, &bdev_obj);
+ nlr_pop();
+ return vfs;
+ }
+ #endif
+ #if MICROPY_VFS_LFS2
+ if (memcmp(&buf[0], "littlefs", 8) == 0) {
+ // LFS2
+ vfs = mp_type_vfs_lfs2.make_new(&mp_type_vfs_lfs2, 1, 0, &bdev_obj);
+ nlr_pop();
+ return vfs;
+ }
+ #endif
+ nlr_pop();
+ } else {
+ // Ignore exception (eg block device doesn't support extended readblocks)
+ }
+ #endif
+
+ #if MICROPY_VFS_FAT
+ return mp_fat_vfs_type.make_new(&mp_fat_vfs_type, 1, 0, &bdev_obj);
+ #endif
+
+ return bdev_obj;
+}
+
mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_readonly, ARG_mkfs };
static const mp_arg_t allowed_args[] = {
@@ -178,10 +220,7 @@ mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args
if (dest[0] == MP_OBJ_NULL) {
// Input object has no mount method, assume it's a block device and try to
// auto-detect the filesystem and create the corresponding VFS entity.
- // (At the moment we only support FAT filesystems.)
- #if MICROPY_VFS_FAT
- vfs_obj = mp_fat_vfs_type.make_new(&mp_fat_vfs_type, 1, 0, &vfs_obj);
- #endif
+ vfs_obj = mp_vfs_autodetect(vfs_obj);
}
// create new object