@@ -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+
5670static 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+
117149static 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};
285317static 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+
287325MP_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
0 commit comments