|
| 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 | +#include "drivers/memory/spiflash.h" |
| 32 | + |
| 33 | +#include "flash.h" |
| 34 | +#include "qspi.h" |
| 35 | +#include "storage.h" |
| 36 | + |
| 37 | +#if MICROPY_VFS_ROM_IOCTL |
| 38 | + |
| 39 | +#if MICROPY_HW_ROMFS_ENABLE_PART1 && !defined(MICROPY_HW_ROMFS_PART1_START) |
| 40 | +#define MICROPY_HW_ROMFS_PART1_START (uintptr_t)(&_micropy_hw_romfs_part1_start) |
| 41 | +#define MICROPY_HW_ROMFS_PART1_SIZE (uintptr_t)(&_micropy_hw_romfs_part1_size) |
| 42 | +extern uint8_t _micropy_hw_romfs_part1_start; |
| 43 | +extern uint8_t _micropy_hw_romfs_part1_size; |
| 44 | +#endif |
| 45 | + |
| 46 | +#if MICROPY_HW_ROMFS_ENABLE_PART2 && !defined(MICROPY_HW_ROMFS_PART2_START) |
| 47 | +#define MICROPY_HW_ROMFS_PART2_START (uintptr_t)(&_micropy_hw_romfs_part2_start) |
| 48 | +#define MICROPY_HW_ROMFS_PART2_SIZE (uintptr_t)(&_micropy_hw_romfs_part2_size) |
| 49 | +extern uint8_t _micropy_hw_romfs_part2_start; |
| 50 | +extern uint8_t _micropy_hw_romfs_part2_size; |
| 51 | +#endif |
| 52 | + |
| 53 | +#define ROMFS_MEMORYVIEW(base, size) {{&mp_type_memoryview}, 'B', 0, (size), (void *)(base)} |
| 54 | + |
| 55 | +static const mp_obj_array_t romfs_obj_table[] = { |
| 56 | + #if MICROPY_HW_ROMFS_ENABLE_PART1 |
| 57 | + ROMFS_MEMORYVIEW(MICROPY_HW_ROMFS_PART1_START, MICROPY_HW_ROMFS_PART1_SIZE), |
| 58 | + #endif |
| 59 | + #if MICROPY_HW_ROMFS_ENABLE_PART2 |
| 60 | + ROMFS_MEMORYVIEW(MICROPY_HW_ROMFS_PART2_START, MICROPY_HW_ROMFS_PART2_SIZE), |
| 61 | + #endif |
| 62 | +}; |
| 63 | + |
| 64 | +mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) { |
| 65 | + mp_int_t cmd = mp_obj_get_int(args[0]); |
| 66 | + if (cmd == MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS) { |
| 67 | + return MP_OBJ_NEW_SMALL_INT(MP_ARRAY_SIZE(romfs_obj_table)); |
| 68 | + } |
| 69 | + |
| 70 | + if (n_args < 2) { |
| 71 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 72 | + } |
| 73 | + |
| 74 | + mp_int_t romfs_id = mp_obj_get_int(args[1]); |
| 75 | + if (!(0 <= romfs_id && romfs_id < MP_ARRAY_SIZE(romfs_obj_table))) { |
| 76 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 77 | + } |
| 78 | + |
| 79 | + const mp_obj_array_t *romfs_obj = &romfs_obj_table[romfs_id]; |
| 80 | + uintptr_t romfs_base = (uintptr_t)romfs_obj->items; |
| 81 | + uintptr_t romfs_len = romfs_obj->len; |
| 82 | + |
| 83 | + if (cmd == MP_VFS_ROM_IOCTL_GET_SEGMENT) { |
| 84 | + // Return the ROMFS memoryview object. |
| 85 | + return MP_OBJ_FROM_PTR(romfs_obj); |
| 86 | + } |
| 87 | + |
| 88 | + if (cmd == MP_VFS_ROM_IOCTL_WRITE_PREPARE) { |
| 89 | + // Erase sectors in given range. |
| 90 | + if (n_args < 3) { |
| 91 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 92 | + } |
| 93 | + uint32_t dest = romfs_base; |
| 94 | + uint32_t dest_max = dest + mp_obj_get_int(args[2]); |
| 95 | + if (dest_max > romfs_base + romfs_len) { |
| 96 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 97 | + } |
| 98 | + |
| 99 | + #if MICROPY_HW_ROMFS_ENABLE_INTERNAL_FLASH |
| 100 | + if (flash_is_valid_addr(dest)) { |
| 101 | + while (dest < dest_max) { |
| 102 | + int ret = flash_erase(dest); |
| 103 | + if (ret < 0) { |
| 104 | + return MP_OBJ_NEW_SMALL_INT(ret); |
| 105 | + } |
| 106 | + uint32_t sector_size = 0; |
| 107 | + flash_get_sector_info(dest, NULL, §or_size); |
| 108 | + dest += sector_size; |
| 109 | + } |
| 110 | + return MP_OBJ_NEW_SMALL_INT(16); |
| 111 | + } |
| 112 | + #endif |
| 113 | + |
| 114 | + #if MICROPY_HW_ROMFS_ENABLE_EXTERNAL_QSPI |
| 115 | + if (qspi_is_valid_addr(dest)) { |
| 116 | + dest -= QSPI_MAP_ADDR; |
| 117 | + dest_max -= QSPI_MAP_ADDR; |
| 118 | + while (dest < dest_max) { |
| 119 | + int ret = mp_spiflash_erase_block(MICROPY_HW_ROMFS_QSPI_SPIFLASH_OBJ, dest); |
| 120 | + if (ret < 0) { |
| 121 | + return MP_OBJ_NEW_SMALL_INT(ret); |
| 122 | + } |
| 123 | + dest += MP_SPIFLASH_ERASE_BLOCK_SIZE; |
| 124 | + } |
| 125 | + return MP_OBJ_NEW_SMALL_INT(4); |
| 126 | + } |
| 127 | + #endif |
| 128 | + } |
| 129 | + |
| 130 | + if (cmd == MP_VFS_ROM_IOCTL_WRITE) { |
| 131 | + // Write data to flash. |
| 132 | + if (n_args < 4) { |
| 133 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 134 | + } |
| 135 | + uint32_t dest = romfs_base + mp_obj_get_int(args[2]); |
| 136 | + mp_buffer_info_t bufinfo; |
| 137 | + mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); |
| 138 | + if (dest + bufinfo.len > romfs_base + romfs_len) { |
| 139 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 140 | + } |
| 141 | + |
| 142 | + #if MICROPY_HW_ROMFS_ENABLE_INTERNAL_FLASH |
| 143 | + if (flash_is_valid_addr(dest)) { |
| 144 | + int ret = flash_write(dest, bufinfo.buf, bufinfo.len / 4); |
| 145 | + return MP_OBJ_NEW_SMALL_INT(ret); |
| 146 | + } |
| 147 | + #endif |
| 148 | + |
| 149 | + #if MICROPY_HW_ROMFS_ENABLE_EXTERNAL_QSPI |
| 150 | + if (qspi_is_valid_addr(dest)) { |
| 151 | + dest -= QSPI_MAP_ADDR; |
| 152 | + int ret = mp_spiflash_write(MICROPY_HW_ROMFS_QSPI_SPIFLASH_OBJ, dest, bufinfo.len, bufinfo.buf); |
| 153 | + return MP_OBJ_NEW_SMALL_INT(ret); |
| 154 | + } |
| 155 | + #endif |
| 156 | + } |
| 157 | + |
| 158 | + return MP_OBJ_NEW_SMALL_INT(-MP_EINVAL); |
| 159 | +} |
| 160 | + |
| 161 | +#endif // MICROPY_VFS_ROM_IOCTL |
0 commit comments