Skip to content

Commit 0255cb7

Browse files
committed
esp32: Implement vfs.rom_ioctl with support for external flash.
Not enabled by default on any board. For a board to enable ROMFS it must: - Add `#define MICROPY_VFS_ROM (1)` to its `mpconfigboard.h` file. - Use `partitions-4MiB-romfs.csv` as its partitions file (or a similar partitions definition that has an entry labelled "romfs"). Signed-off-by: Damien George <[email protected]>
1 parent 50a7362 commit 0255cb7

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

ports/esp32/esp32_partition.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ typedef struct _esp32_partition_obj_t {
5353
uint16_t block_size;
5454
} esp32_partition_obj_t;
5555

56+
#if MICROPY_VFS_ROM_IOCTL
57+
58+
static esp32_partition_obj_t esp32_partition_romfs_obj = {
59+
.base = { .type = NULL },
60+
.part = NULL,
61+
.cache = NULL,
62+
.block_size = NATIVE_BLOCK_SIZE_BYTES,
63+
};
64+
65+
static const void *esp32_partition_romfs_ptr = NULL;
66+
static esp_partition_mmap_handle_t esp32_partition_romfs_handle;
67+
68+
#endif
69+
5670
static esp32_partition_obj_t *esp32_partition_new(const esp_partition_t *part, uint16_t block_size) {
5771
if (part == NULL) {
5872
mp_raise_OSError(MP_ENOENT);
@@ -114,6 +128,24 @@ static mp_obj_t esp32_partition_make_new(const mp_obj_type_t *type, size_t n_arg
114128
return MP_OBJ_FROM_PTR(esp32_partition_new(part, block_size));
115129
}
116130

131+
#if MICROPY_VFS_ROM_IOCTL
132+
static mp_int_t esp32_partition_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
133+
esp32_partition_obj_t *self = MP_OBJ_TO_PTR(self_in);
134+
if (self == &esp32_partition_romfs_obj && flags == MP_BUFFER_READ) {
135+
if (esp32_partition_romfs_ptr == NULL) {
136+
check_esp_err(esp_partition_mmap(self->part, 0, self->part->size, ESP_PARTITION_MMAP_DATA, &esp32_partition_romfs_ptr, &esp32_partition_romfs_handle));
137+
}
138+
bufinfo->buf = (void *)esp32_partition_romfs_ptr;
139+
bufinfo->len = self->part->size;
140+
bufinfo->typecode = 'B';
141+
return 0;
142+
} else {
143+
// Unsupported.
144+
return 1;
145+
}
146+
}
147+
#endif
148+
117149
static mp_obj_t esp32_partition_find(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
118150
// Parse args
119151
enum { ARG_type, ARG_subtype, ARG_label, ARG_block_size };
@@ -284,11 +316,55 @@ static const mp_rom_map_elem_t esp32_partition_locals_dict_table[] = {
284316
};
285317
static MP_DEFINE_CONST_DICT(esp32_partition_locals_dict, esp32_partition_locals_dict_table);
286318

319+
#if MICROPY_VFS_ROM_IOCTL
320+
#define ESP32_PARTITION_GET_BUFFER buffer, esp32_partition_get_buffer,
321+
#else
322+
#define ESP32_PARTITION_GET_BUFFER
323+
#endif
324+
287325
MP_DEFINE_CONST_OBJ_TYPE(
288326
esp32_partition_type,
289327
MP_QSTR_Partition,
290328
MP_TYPE_FLAG_NONE,
291329
make_new, esp32_partition_make_new,
292330
print, esp32_partition_print,
331+
ESP32_PARTITION_GET_BUFFER
293332
locals_dict, &esp32_partition_locals_dict
294333
);
334+
335+
/******************************************************************************/
336+
// romfs partition
337+
338+
#if MICROPY_VFS_ROM_IOCTL
339+
340+
mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) {
341+
if (esp32_partition_romfs_obj.base.type == NULL) {
342+
esp32_partition_romfs_obj.base.type = &esp32_partition_type;
343+
// Get the romfs partition.
344+
// TODO: number of segments ioctl can be used if there is more than one romfs.
345+
esp_partition_iterator_t iter = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "romfs");
346+
if (iter != NULL) {
347+
esp32_partition_romfs_obj.part = esp_partition_get(iter);
348+
}
349+
esp_partition_iterator_release(iter);
350+
}
351+
352+
switch (mp_obj_get_int(args[0])) {
353+
case MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS:
354+
if (esp32_partition_romfs_obj.part == NULL) {
355+
return MP_OBJ_NEW_SMALL_INT(0);
356+
} else {
357+
return MP_OBJ_NEW_SMALL_INT(1);
358+
}
359+
case MP_VFS_ROM_IOCTL_GET_SEGMENT:
360+
if (esp32_partition_romfs_obj.part == NULL) {
361+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
362+
} else {
363+
return MP_OBJ_FROM_PTR(&esp32_partition_romfs_obj);
364+
}
365+
default:
366+
return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL);
367+
}
368+
}
369+
370+
#endif // MICROPY_VFS_ROM_IOCTL

ports/esp32/partitions-4MiB-romfs.csv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Notes: the offset of the partition table itself is set in
2+
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
3+
# Name, Type, SubType, Offset, Size, Flags
4+
nvs, data, nvs, 0x9000, 0x6000,
5+
phy_init, data, phy, 0xf000, 0x1000,
6+
factory, app, factory, 0x10000, 0x1D0000,
7+
romfs, data, 0x8f, 0x1E0000, 0x20000,
8+
vfs, data, fat, 0x200000, 0x200000,

0 commit comments

Comments
 (0)