From d4b8ca2ffc3967007297d17e995d955ca39b1d6d Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 21 Feb 2025 00:37:49 +1100 Subject: [PATCH] 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 --- extmod/vfs.c | 30 ++++++++++++++++++++++++++++++ extmod/vfs.h | 3 +++ py/qstrdefs.h | 5 +++++ 3 files changed, 38 insertions(+) diff --git a/extmod/vfs.c b/extmod/vfs.c index b9c5ab0fc..c83b8ce7d 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -46,6 +46,10 @@ #include "extmod/vfs_posix.h" #endif +#if MICROPY_VFS_ROM && MICROPY_VFS_ROM_IOCTL +#include "extmod/vfs_rom.h" +#endif + // For mp_vfs_proxy_call, the maximum number of additional args that can be passed. // A fixed maximum size is used to avoid the need for a costly variable array. #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) { return ret; } +#if MICROPY_VFS_ROM && MICROPY_VFS_ROM_IOCTL + +int mp_vfs_mount_romfs_protected(void) { + int ret; + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(MP_VFS_ROM_IOCTL_GET_SEGMENT), MP_OBJ_NEW_SMALL_INT(0) }; + mp_obj_t rom = mp_vfs_rom_ioctl(2, args); + mp_obj_t romfs = mp_call_function_1(MP_OBJ_FROM_PTR(&mp_type_vfs_rom), rom); + mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_rom); + mp_call_function_2(MP_OBJ_FROM_PTR(&mp_vfs_mount_obj), romfs, mount_point); + #if MICROPY_PY_SYS_PATH_ARGV_DEFAULTS + // Add "/rom" and "/rom/lib" to `sys.path`. + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_rom)); + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_rom_slash_lib)); + #endif + ret = 0; // success + nlr_pop(); + } else { + ret = -MP_EIO; + } + return ret; +} + +#endif + MP_REGISTER_ROOT_POINTER(struct _mp_vfs_mount_t *vfs_cur); MP_REGISTER_ROOT_POINTER(struct _mp_vfs_mount_t *vfs_mount_table); diff --git a/extmod/vfs.h b/extmod/vfs.h index cc9c50d29..3acc09db1 100644 --- a/extmod/vfs.h +++ b/extmod/vfs.h @@ -112,6 +112,9 @@ mp_obj_t mp_vfs_stat(mp_obj_t path_in); mp_obj_t mp_vfs_statvfs(mp_obj_t path_in); int mp_vfs_mount_and_chdir_protected(mp_obj_t bdev, mp_obj_t mount_point); +#if MICROPY_VFS_ROM && MICROPY_VFS_ROM_IOCTL +int mp_vfs_mount_romfs_protected(void); +#endif MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj); MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_umount_obj); diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 5003636df..0b50d279f 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -68,6 +68,11 @@ Q(utf-8) Q(.frozen) #endif +#if MICROPY_VFS_ROM && MICROPY_VFS_ROM_IOCTL +Q(/rom) +Q(/rom/lib) +#endif + #if MICROPY_ENABLE_PYSTACK Q(pystack exhausted) #endif