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>
This commit is contained in:
Damien George
2025-02-21 00:37:49 +11:00
parent 89e6c58c80
commit d4b8ca2ffc
3 changed files with 38 additions and 0 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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