From ec7dc7f8d796b5b67772d1f40863d13fb5e19be2 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Thu, 9 Feb 2017 12:03:12 +1100
Subject: [PATCH] extmod/vfs: Allow to mount a block device, not just a VFS
 object.

If the mounted object doesn't have a "mount" method then assume it's a
block device and try to detect the filesystem.  Since we currently only
support FAT filesystems, the behaviour is to just try and create a VfsFat
object automatically, using the given block device.
---
 extmod/vfs.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/extmod/vfs.c b/extmod/vfs.c
index 1eb26acf1..c1e32e052 100644
--- a/extmod/vfs.c
+++ b/extmod/vfs.c
@@ -132,11 +132,24 @@ mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args
     mp_uint_t mnt_len;
     const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len);
 
+    // see if we need to auto-detect and create the filesystem
+    mp_obj_t vfs_obj = pos_args[0];
+    mp_obj_t dest[2];
+    mp_load_method_maybe(vfs_obj, MP_QSTR_mount, dest);
+    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
+    }
+
     // create new object
     mp_vfs_mount_t *vfs = m_new_obj(mp_vfs_mount_t);
     vfs->str = mnt_str;
     vfs->len = mnt_len;
-    vfs->obj = pos_args[0];
+    vfs->obj = vfs_obj;
     vfs->next = NULL;
 
     // call the underlying object to do any mounting operation
-- 
GitLab