kfsw-platform/src/storage_zms_disk.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <string.h> | ||
| 3 | |||
| 4 | #include <zephyr/drivers/disk.h> | ||
| 5 | #include <zephyr/drivers/flash.h> | ||
| 6 | #include <zephyr/init.h> | ||
| 7 | #include <zephyr/kernel.h> | ||
| 8 | #include <zephyr/kvss/zms.h> | ||
| 9 | #include <zephyr/storage/disk_access.h> | ||
| 10 | #include <zephyr/storage/flash_map.h> | ||
| 11 | #include <zephyr/sys/byteorder.h> | ||
| 12 | #include <zephyr/sys/crc.h> | ||
| 13 | #include <zephyr/sys/util.h> | ||
| 14 | |||
| 15 | #include "storage_zms_disk.h" | ||
| 16 | |||
| 17 | #define ZMS_PARTITION DT_CHOSEN(kfsw_storage_partition) | ||
| 18 | #define SECTOR_COUNT CONFIG_KFSW_STORAGE_ZMS_SECTOR_COUNT | ||
| 19 | #define PHYSICAL_SECTOR_SIZE (DT_REG_SIZE(ZMS_PARTITION) / 2U) | ||
| 20 | #define ZMS_HEADER_SIZE 16U | ||
| 21 | #define ZMS_HEADER_METADATA 0x4201U | ||
| 22 | |||
| 23 | BUILD_ASSERT(IS_ENABLED(CONFIG_LITTLE_ENDIAN), "Pinned ZMS format uses native byte order"); | ||
| 24 | BUILD_ASSERT(!IS_ENABLED(CONFIG_ZMS_ID_64BIT), "Recognition supports 32-bit ZMS IDs only"); | ||
| 25 | BUILD_ASSERT(DT_REG_SIZE(ZMS_PARTITION) % 2U == 0U); | ||
| 26 | BUILD_ASSERT(SECTOR_COUNT *(KFSW_STORAGE_ZMS_SECTOR_SIZE + 32U) + 160U < PHYSICAL_SECTOR_SIZE, | ||
| 27 | "One physical sector must hold the whole logical live set"); | ||
| 28 | |||
| 29 | static struct zms_fs store; | ||
| 30 | K_MUTEX_DEFINE(disk_lock); | ||
| 31 | static uint8_t scan_buffer[KFSW_STORAGE_ZMS_SECTOR_SIZE]; | ||
| 32 | |||
| 33 | /* Pinned Zephyr v4.4.0: empty ATE at the end of each physical sector. | ||
| 34 | * Recognize only its framing, CRC and version; ZMS owns recovery and GC. | ||
| 35 | */ | ||
| 36 | 34190 | static int recognize(const struct flash_area *area) | |
| 37 | { | ||
| 38 | 34190 | const size_t write_size = flash_get_write_block_size(area->fa_dev); | |
| 39 | 34190 | const size_t ate_size = ROUND_UP(ZMS_HEADER_SIZE, write_size); | |
| 40 | uint8_t header[ZMS_HEADER_SIZE]; | ||
| 41 | 34190 | bool recognized = false; | |
| 42 | 34190 | bool blank = true; | |
| 43 | int result; | ||
| 44 | |||
| 45 | 4410254 | for (size_t offset = 0U; offset < area->fa_size; offset += sizeof(scan_buffer)) { | |
| 46 | 4376066 | const size_t size = MIN(sizeof(scan_buffer), area->fa_size - offset); | |
| 47 | |||
| 48 | 4376066 | result = flash_area_read(area, offset, scan_buffer, size); | |
| 49 | 4376066 | if (result != 0) { | |
| 50 | 2 | return result; | |
| 51 | } | ||
| 52 | ✗ | for (size_t index = 0U; index < size; index++) { | |
| 53 | ✗ | blank &= scan_buffer[index] == flash_area_erased_val(area); | |
| 54 | } | ||
| 55 | } | ||
| 56 | 34188 | if (blank) { | |
| 57 | 28 | return 0; | |
| 58 | } | ||
| 59 | |||
| 60 | 102476 | for (size_t sector = 0U; sector < 2U; sector++) { | |
| 61 | 68318 | result = flash_area_read(area, (sector + 1U) * PHYSICAL_SECTOR_SIZE - ate_size, | |
| 62 | header, sizeof(header)); | ||
| 63 | 68318 | if (result != 0) { | |
| 64 | ✗ | return result; | |
| 65 | } | ||
| 66 | 104732 | if (header[0] != crc8_ccitt(0xff, header + 1U, sizeof(header) - 1U) || | |
| 67 | 72828 | sys_get_le16(header + 2U) != UINT16_MAX || | |
| 68 | 36414 | sys_get_le32(header + 4U) != UINT32_MAX) { | |
| 69 | 31904 | continue; | |
| 70 | } | ||
| 71 | 36414 | if (sys_get_le32(header + 12U) != ZMS_HEADER_METADATA) { | |
| 72 | 2 | return -EPROTONOSUPPORT; | |
| 73 | } | ||
| 74 | 36412 | recognized = true; | |
| 75 | } | ||
| 76 | 34158 | return recognized ? 0 : -EFAULT; | |
| 77 | } | ||
| 78 | |||
| 79 | 34190 | static int initialize(struct disk_info *disk) | |
| 80 | { | ||
| 81 | const struct flash_area *area; | ||
| 82 | struct flash_pages_info page; | ||
| 83 | int result; | ||
| 84 | |||
| 85 | ARG_UNUSED(disk); | ||
| 86 | 34190 | k_mutex_lock(&disk_lock, K_FOREVER); | |
| 87 | 34190 | if (store.ready) { | |
| 88 | ✗ | result = 0; | |
| 89 | ✗ | goto out; | |
| 90 | } | ||
| 91 | 34190 | result = flash_area_open(DT_FIXED_PARTITION_ID(ZMS_PARTITION), &area); | |
| 92 | 34190 | if (result != 0) { | |
| 93 | ✗ | goto out; | |
| 94 | } | ||
| 95 | 34190 | if (!device_is_ready(area->fa_dev)) { | |
| 96 | ✗ | result = -ENODEV; | |
| 97 | ✗ | goto close; | |
| 98 | } | ||
| 99 | 34190 | const size_t write_size = flash_get_write_block_size(area->fa_dev); | |
| 100 | |||
| 101 | 34190 | if (write_size == 0U || write_size > 32U || flash_area_erased_val(area) != 0xff) { | |
| 102 | ✗ | result = -ENOTSUP; | |
| 103 | ✗ | goto close; | |
| 104 | } | ||
| 105 | /* Check every erase page, including the partition end, before mounting. */ | ||
| 106 | 102570 | for (size_t offset = 0U; offset < area->fa_size; offset += page.size) { | |
| 107 | 68380 | result = flash_get_page_info_by_offs(area->fa_dev, area->fa_off + offset, &page); | |
| 108 | 68380 | if (result != 0) { | |
| 109 | ✗ | goto close; | |
| 110 | } | ||
| 111 | 68380 | if (page.start_offset != area->fa_off + offset || | |
| 112 | 68380 | PHYSICAL_SECTOR_SIZE % page.size != 0U) { | |
| 113 | ✗ | result = -EINVAL; | |
| 114 | ✗ | goto close; | |
| 115 | } | ||
| 116 | } | ||
| 117 | 34190 | result = recognize(area); | |
| 118 | 34190 | if (result == 0) { | |
| 119 | 34180 | memset(&store, 0, sizeof(store)); | |
| 120 | 34180 | store.flash_device = area->fa_dev; | |
| 121 | 34180 | store.offset = area->fa_off; | |
| 122 | 34180 | store.sector_size = PHYSICAL_SECTOR_SIZE; | |
| 123 | 34180 | store.sector_count = 2U; | |
| 124 | 34180 | result = zms_mount(&store); | |
| 125 | } | ||
| 126 | 10 | close: | |
| 127 | 34190 | flash_area_close(area); | |
| 128 | 34190 | out: | |
| 129 | 34190 | k_mutex_unlock(&disk_lock); | |
| 130 | 34190 | return result; | |
| 131 | } | ||
| 132 | |||
| 133 | ✗ | static int status(struct disk_info *disk) | |
| 134 | { | ||
| 135 | int result; | ||
| 136 | |||
| 137 | ARG_UNUSED(disk); | ||
| 138 | ✗ | k_mutex_lock(&disk_lock, K_FOREVER); | |
| 139 | ✗ | result = store.ready ? DISK_STATUS_OK : DISK_STATUS_UNINIT; | |
| 140 | ✗ | k_mutex_unlock(&disk_lock); | |
| 141 | ✗ | return result; | |
| 142 | } | ||
| 143 | |||
| 144 | 434724 | static int read_sector(uint8_t *data, uint32_t sector) | |
| 145 | { | ||
| 146 | 434724 | ssize_t result = zms_get_data_length(&store, sector + 1U); | |
| 147 | |||
| 148 | 434724 | if (result == -ENOENT) { | |
| 149 | 188 | memset(data, 0xff, KFSW_STORAGE_ZMS_SECTOR_SIZE); | |
| 150 | 188 | return 0; | |
| 151 | } | ||
| 152 | 434536 | if (result < 0) { | |
| 153 | 2 | return result; | |
| 154 | } | ||
| 155 | 434534 | if (result != KFSW_STORAGE_ZMS_SECTOR_SIZE) { | |
| 156 | 4 | return -EIO; | |
| 157 | } | ||
| 158 | 434530 | result = zms_read(&store, sector + 1U, data, KFSW_STORAGE_ZMS_SECTOR_SIZE); | |
| 159 | 434530 | return result == KFSW_STORAGE_ZMS_SECTOR_SIZE ? 0 : (result < 0 ? result : -EIO); | |
| 160 | } | ||
| 161 | |||
| 162 | 452908 | static bool valid_request(const void *data, uint32_t start, uint32_t count) | |
| 163 | { | ||
| 164 | 452908 | return data != NULL && count != 0U && start < SECTOR_COUNT && count <= SECTOR_COUNT - start; | |
| 165 | } | ||
| 166 | |||
| 167 | 434626 | static int read_disk(struct disk_info *disk, uint8_t *data, uint32_t start, uint32_t count) | |
| 168 | { | ||
| 169 | 434626 | int result = 0; | |
| 170 | |||
| 171 | ARG_UNUSED(disk); | ||
| 172 | 434626 | if (!valid_request(data, start, count)) { | |
| 173 | ✗ | return -EINVAL; | |
| 174 | } | ||
| 175 | 434626 | k_mutex_lock(&disk_lock, K_FOREVER); | |
| 176 | 434626 | if (!store.ready) { | |
| 177 | 2 | result = -EACCES; | |
| 178 | 2 | goto out; | |
| 179 | } | ||
| 180 | 869240 | for (uint32_t index = 0U; index < count; index++) { | |
| 181 | 434626 | result = read_sector(data + index * KFSW_STORAGE_ZMS_SECTOR_SIZE, start + index); | |
| 182 | 434626 | if (result != 0) { | |
| 183 | 10 | break; | |
| 184 | } | ||
| 185 | } | ||
| 186 | 434614 | out: | |
| 187 | 434626 | k_mutex_unlock(&disk_lock); | |
| 188 | 434626 | return result; | |
| 189 | } | ||
| 190 | |||
| 191 | 18282 | static int write_disk(struct disk_info *disk, const uint8_t *data, uint32_t start, uint32_t count) | |
| 192 | { | ||
| 193 | 18282 | int result = 0; | |
| 194 | |||
| 195 | ARG_UNUSED(disk); | ||
| 196 | 18282 | if (!valid_request(data, start, count)) { | |
| 197 | 8 | return -EINVAL; | |
| 198 | } | ||
| 199 | 18274 | k_mutex_lock(&disk_lock, K_FOREVER); | |
| 200 | 18274 | if (!store.ready) { | |
| 201 | 2 | result = -EACCES; | |
| 202 | 2 | goto out; | |
| 203 | } | ||
| 204 | 19486 | for (uint32_t index = 0U; index < count; index++) { | |
| 205 | 18278 | ssize_t written = zms_write(&store, start + index + 1U, | |
| 206 | 18278 | data + index * KFSW_STORAGE_ZMS_SECTOR_SIZE, | |
| 207 | KFSW_STORAGE_ZMS_SECTOR_SIZE); | ||
| 208 | |||
| 209 | 18278 | if (written != 0 && written != KFSW_STORAGE_ZMS_SECTOR_SIZE) { | |
| 210 | 17064 | result = written < 0 ? written : -EIO; | |
| 211 | /* A failed append may have partially programmed flash. Recover | ||
| 212 | * through mount before accepting any further disk operations. | ||
| 213 | */ | ||
| 214 | 17064 | store.ready = false; | |
| 215 | 17064 | break; | |
| 216 | } | ||
| 217 | } | ||
| 218 | 1208 | out: | |
| 219 | 18274 | k_mutex_unlock(&disk_lock); | |
| 220 | 18274 | return result; | |
| 221 | } | ||
| 222 | |||
| 223 | 34370 | static int ioctl_disk(struct disk_info *disk, uint8_t command, void *buffer) | |
| 224 | { | ||
| 225 | 34370 | int result = 0; | |
| 226 | |||
| 227 | 34370 | if (command == DISK_IOCTL_CTRL_INIT) { | |
| 228 | ✗ | return initialize(disk); | |
| 229 | } | ||
| 230 | 34370 | k_mutex_lock(&disk_lock, K_FOREVER); | |
| 231 | 34370 | switch (command) { | |
| 232 | 34192 | case DISK_IOCTL_CTRL_DEINIT: | |
| 233 | 34192 | store.ready = false; | |
| 234 | 34192 | break; | |
| 235 | 168 | case DISK_IOCTL_CTRL_SYNC: | |
| 236 | 168 | result = store.ready ? 0 : -EACCES; | |
| 237 | 168 | break; | |
| 238 | 10 | case DISK_IOCTL_GET_SECTOR_COUNT: | |
| 239 | case DISK_IOCTL_GET_SECTOR_SIZE: | ||
| 240 | case DISK_IOCTL_GET_ERASE_BLOCK_SZ: | ||
| 241 | 10 | if (buffer == NULL) { | |
| 242 | ✗ | result = -EINVAL; | |
| 243 | ✗ | break; | |
| 244 | } | ||
| 245 | 10 | *(uint32_t *)buffer = command == DISK_IOCTL_GET_SECTOR_COUNT ? SECTOR_COUNT | |
| 246 | 12 | : command == DISK_IOCTL_GET_SECTOR_SIZE | |
| 247 | ? KFSW_STORAGE_ZMS_SECTOR_SIZE | ||
| 248 | 2 | : 1U; | |
| 249 | 10 | break; | |
| 250 | ✗ | default: | |
| 251 | ✗ | result = -ENOTSUP; | |
| 252 | } | ||
| 253 | 34370 | k_mutex_unlock(&disk_lock); | |
| 254 | 34370 | return result; | |
| 255 | } | ||
| 256 | |||
| 257 | 6 | int kfsw_storage_zms_is_erased(bool *is_erased) | |
| 258 | { | ||
| 259 | 6 | int result = 0; | |
| 260 | |||
| 261 | 6 | *is_erased = false; | |
| 262 | 6 | k_mutex_lock(&disk_lock, K_FOREVER); | |
| 263 | 6 | if (!store.ready) { | |
| 264 | 2 | result = -EACCES; | |
| 265 | 2 | goto out; | |
| 266 | } | ||
| 267 | 100 | for (uint32_t sector = 0U; sector < SECTOR_COUNT; sector++) { | |
| 268 | 98 | result = read_sector(scan_buffer, sector); | |
| 269 | 98 | if (result != 0) { | |
| 270 | ✗ | goto out; | |
| 271 | } | ||
| 272 | 393314 | for (size_t index = 0U; index < sizeof(scan_buffer); index++) { | |
| 273 | 393218 | if (scan_buffer[index] != 0xff) { | |
| 274 | 2 | goto out; | |
| 275 | } | ||
| 276 | } | ||
| 277 | } | ||
| 278 | 2 | *is_erased = true; | |
| 279 | 6 | out: | |
| 280 | 6 | k_mutex_unlock(&disk_lock); | |
| 281 | 6 | return result; | |
| 282 | } | ||
| 283 | |||
| 284 | static const struct disk_operations operations = { | ||
| 285 | .init = initialize, | ||
| 286 | .status = status, | ||
| 287 | .read = read_disk, | ||
| 288 | .write = write_disk, | ||
| 289 | .ioctl = ioctl_disk, | ||
| 290 | }; | ||
| 291 | |||
| 292 | static struct disk_info disk = { | ||
| 293 | .name = KFSW_STORAGE_ZMS_DISK_NAME, | ||
| 294 | .ops = &operations, | ||
| 295 | }; | ||
| 296 | |||
| 297 | 2 | static int register_disk(void) | |
| 298 | { | ||
| 299 | 2 | return disk_access_register(&disk); | |
| 300 | } | ||
| 301 | |||
| 302 | SYS_INIT(register_disk, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); | ||
| 303 |