Skip to content

Commit d4b8ca2

Browse files
committed
extmod/vfs: Add mp_vfs_mount_romfs_protected() helper.
This function will attempt to create a `VfsRom` instance and mount it at location "/rom" in the filesystem. Signed-off-by: Damien George <damien@micropython.org>
1 parent 89e6c58 commit d4b8ca2

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

extmod/vfs.c

+30
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
#include "extmod/vfs_posix.h"
4747
#endif
4848

49+
#if MICROPY_VFS_ROM && MICROPY_VFS_ROM_IOCTL
50+
#include "extmod/vfs_rom.h"
51+
#endif
52+
4953
// For mp_vfs_proxy_call, the maximum number of additional args that can be passed.
5054
// A fixed maximum size is used to avoid the need for a costly variable array.
5155
#define PROXY_MAX_ARGS (2)
@@ -552,6 +556,32 @@ int mp_vfs_mount_and_chdir_protected(mp_obj_t bdev, mp_obj_t mount_point) {
552556
return ret;
553557
}
554558

559+
#if MICROPY_VFS_ROM && MICROPY_VFS_ROM_IOCTL
560+
561+
int mp_vfs_mount_romfs_protected(void) {
562+
int ret;
563+
nlr_buf_t nlr;
564+
if (nlr_push(&nlr) == 0) {
565+
mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(MP_VFS_ROM_IOCTL_GET_SEGMENT), MP_OBJ_NEW_SMALL_INT(0) };
566+
mp_obj_t rom = mp_vfs_rom_ioctl(2, args);
567+
mp_obj_t romfs = mp_call_function_1(MP_OBJ_FROM_PTR(&mp_type_vfs_rom), rom);
568+
mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_rom);
569+
mp_call_function_2(MP_OBJ_FROM_PTR(&mp_vfs_mount_obj), romfs, mount_point);
570+
#if MICROPY_PY_SYS_PATH_ARGV_DEFAULTS
571+
// Add "/rom" and "/rom/lib" to `sys.path`.
572+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_rom));
573+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_rom_slash_lib));
574+
#endif
575+
ret = 0; // success
576+
nlr_pop();
577+
} else {
578+
ret = -MP_EIO;
579+
}
580+
return ret;
581+
}
582+
583+
#endif
584+
555585
MP_REGISTER_ROOT_POINTER(struct _mp_vfs_mount_t *vfs_cur);
556586
MP_REGISTER_ROOT_POINTER(struct _mp_vfs_mount_t *vfs_mount_table);
557587

extmod/vfs.h

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ mp_obj_t mp_vfs_stat(mp_obj_t path_in);
112112
mp_obj_t mp_vfs_statvfs(mp_obj_t path_in);
113113

114114
int mp_vfs_mount_and_chdir_protected(mp_obj_t bdev, mp_obj_t mount_point);
115+
#if MICROPY_VFS_ROM && MICROPY_VFS_ROM_IOCTL
116+
int mp_vfs_mount_romfs_protected(void);
117+
#endif
115118

116119
MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj);
117120
MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_umount_obj);

py/qstrdefs.h

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ Q(utf-8)
6868
Q(.frozen)
6969
#endif
7070

71+
#if MICROPY_VFS_ROM && MICROPY_VFS_ROM_IOCTL
72+
Q(/rom)
73+
Q(/rom/lib)
74+
#endif
75+
7176
#if MICROPY_ENABLE_PYSTACK
7277
Q(pystack exhausted)
7378
#endif

0 commit comments

Comments
 (0)