Skip to content

Commit 6b2488f

Browse files
iabdalkaderdpgeorge
authored andcommitted
qemu: Add basic ROMFS support.
This commit adds basic ROMFS support. This helps with running tests since mpremote mount is too slow with big files. To be configured per-board, and then run with: $ make QEMU_ROMFS_IMG0=image.romfs ... Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent e8992eb commit 6b2488f

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

ports/qemu/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ QEMU_SYSTEM = $(QEMU_BASE)$(QEMU_ARCH)
158158
QEMU_ARGS += -machine $(QEMU_MACHINE) -nographic -monitor null -semihosting
159159
QEMU_ARGS += $(QEMU_EXTRA)
160160

161+
# Load ROMFS images into emulated memory if configured.
162+
ifneq ($(QEMU_ROMFS_IMG0),)
163+
QEMU_ARGS += -device loader,file=$(QEMU_ROMFS_IMG0),addr=$(MICROPY_HW_ROMFS_PART0_START),force-raw=on
164+
endif
165+
ifneq ($(QEMU_ROMFS_IMG1),)
166+
QEMU_ARGS += -device loader,file=$(QEMU_ROMFS_IMG1),addr=$(MICROPY_HW_ROMFS_PART1_START),force-raw=on
167+
endif
168+
161169
# Specifying QEMU_DEBUG=1 will block qemu until a debugger is connected.
162170
ifeq ($(QEMU_DEBUG),1)
163171
QEMU_DEBUG_ARGS ?= -s
@@ -209,6 +217,7 @@ SRC_C += \
209217
main.c \
210218
uart.c \
211219
mphalport.c \
220+
vfs_rom_ioctl.c \
212221
shared/libc/string0.c \
213222
shared/readline/readline.c \
214223
shared/runtime/interrupt_char.c \

ports/qemu/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ The following options can be specified on the `make` command line:
166166
- `QEMU_DEBUG_ARGS`: defaults to `-s` (gdb on TCP port 1234), but can be overridden
167167
with different qemu gdb arguments.
168168
- `QEMU_DEBUG_EXTRA`: extra options to pass to qemu when `QEMU_DEBUG=1` is used.
169+
- `QEMU_ROMFS_IMG<n>`: pass in romfs image to be loaded by qemu (if enabled by the board).
169170
- `TEST_NATMODS`: pass an optional list of space-separated names of natmods to test,
170171
so only the given subset of example natmods will be used by `test_natmod` (for
171172
example, `make test_natmod TEST_NATMODS="btree heapq re"`).

ports/qemu/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
#define MICROPY_PY_MACHINE_PIN_BASE (1)
6868
#define MICROPY_VFS (1)
6969
#define MICROPY_VFS_ROM (1)
70-
#define MICROPY_VFS_ROM_IOCTL (0)
70+
#define MICROPY_VFS_ROM_IOCTL (MICROPY_HW_ROMFS_ENABLE_PART0 || MICROPY_HW_ROMFS_ENABLE_PART1)
7171

7272
// type definitions for the specific machine
7373

ports/qemu/vfs_rom_ioctl.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/obj.h"
28+
#include "py/objarray.h"
29+
#include "py/mperrno.h"
30+
#include "extmod/vfs.h"
31+
32+
#if MICROPY_VFS_ROM_IOCTL
33+
34+
#if MICROPY_HW_ROMFS_ENABLE_PART0 && !defined(MICROPY_HW_ROMFS_PART0_START)
35+
#define MICROPY_HW_ROMFS_PART0_START (uintptr_t)(&_micropy_hw_romfs_part0_start)
36+
#define MICROPY_HW_ROMFS_PART0_SIZE (uintptr_t)(&_micropy_hw_romfs_part0_size)
37+
extern uint8_t _micropy_hw_romfs_part0_start;
38+
extern uint8_t _micropy_hw_romfs_part0_size;
39+
#endif
40+
41+
#if MICROPY_HW_ROMFS_ENABLE_PART1 && !defined(MICROPY_HW_ROMFS_PART1_START)
42+
#define MICROPY_HW_ROMFS_PART1_START (uintptr_t)(&_micropy_hw_romfs_part1_start)
43+
#define MICROPY_HW_ROMFS_PART1_SIZE (uintptr_t)(&_micropy_hw_romfs_part1_size)
44+
extern uint8_t _micropy_hw_romfs_part1_start;
45+
extern uint8_t _micropy_hw_romfs_part1_size;
46+
#endif
47+
48+
#define ROMFS_MEMORYVIEW(base, size) {{&mp_type_memoryview}, 'B', 0, (size), (void *)(base)}
49+
50+
static const mp_obj_array_t romfs_obj_table[] = {
51+
#if MICROPY_HW_ROMFS_ENABLE_PART0
52+
ROMFS_MEMORYVIEW(MICROPY_HW_ROMFS_PART0_START, MICROPY_HW_ROMFS_PART0_SIZE),
53+
#endif
54+
#if MICROPY_HW_ROMFS_ENABLE_PART1
55+
ROMFS_MEMORYVIEW(MICROPY_HW_ROMFS_PART1_START, MICROPY_HW_ROMFS_PART1_SIZE),
56+
#endif
57+
};
58+
59+
mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) {
60+
mp_int_t cmd = mp_obj_get_int(args[0]);
61+
if (cmd == MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS) {
62+
return MP_OBJ_NEW_SMALL_INT(MP_ARRAY_SIZE(romfs_obj_table));
63+
}
64+
65+
if (n_args < 2) {
66+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
67+
}
68+
69+
mp_int_t romfs_id = mp_obj_get_int(args[1]);
70+
if (!(0 <= romfs_id && romfs_id < MP_ARRAY_SIZE(romfs_obj_table))) {
71+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
72+
}
73+
74+
if (cmd == MP_VFS_ROM_IOCTL_GET_SEGMENT) {
75+
// Return the ROMFS memoryview object.
76+
const mp_obj_array_t *romfs_obj = &romfs_obj_table[romfs_id];
77+
return MP_OBJ_FROM_PTR(romfs_obj);
78+
}
79+
80+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
81+
}
82+
83+
#endif // MICROPY_VFS_ROM_IOCTL

0 commit comments

Comments
 (0)