kfsw-services/src/fwu/fwu_fs.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | #include <bootutil/image.h> | ||
| 6 | #include <zephyr/drivers/flash.h> | ||
| 7 | #include <zephyr/fs/fs.h> | ||
| 8 | #include <zephyr/fs/fs_sys.h> | ||
| 9 | #include <zephyr/kernel.h> | ||
| 10 | #include <zephyr/storage/flash_map.h> | ||
| 11 | #include <zephyr/sys/byteorder.h> | ||
| 12 | #include <zephyr/sys/util.h> | ||
| 13 | |||
| 14 | #include <kfsw/services/fwu.h> | ||
| 15 | #include "fwu_internal.h" | ||
| 16 | |||
| 17 | #define BOOT_ROOT "/kfsw/boot" | ||
| 18 | |||
| 19 | BUILD_ASSERT(MAX_FILE_NAME >= sizeof("firmware_1.bin") - 1U); | ||
| 20 | |||
| 21 | static const char *const slot_names[] = {"firmware_1.bin", "firmware_2.bin"}; | ||
| 22 | static const uint8_t slot_ids[] = { | ||
| 23 | DT_FIXED_PARTITION_ID(DT_NODELABEL(slot0_partition)), | ||
| 24 | DT_FIXED_PARTITION_ID(DT_CHOSEN(kfsw_fwu_partition)), | ||
| 25 | }; | ||
| 26 | |||
| 27 | struct slot_file { | ||
| 28 | const struct flash_area *area; | ||
| 29 | uint32_t start; | ||
| 30 | uint32_t size; | ||
| 31 | uint32_t position; | ||
| 32 | bool secondary; | ||
| 33 | }; | ||
| 34 | |||
| 35 | K_MUTEX_DEFINE(slot_files_lock); | ||
| 36 | static struct slot_file files[CONFIG_KFSW_FWU_FILE_HANDLES]; | ||
| 37 | |||
| 38 | 36 | static int slot_number(const char *path) | |
| 39 | { | ||
| 40 | 60 | for (size_t slot = 0; slot < ARRAY_SIZE(slot_names); slot++) { | |
| 41 | 58 | if ((strncmp(path, BOOT_ROOT "/", sizeof(BOOT_ROOT)) == 0) && | |
| 42 | 58 | (strcmp(path + sizeof(BOOT_ROOT), slot_names[slot]) == 0)) { | |
| 43 | 34 | return (int)slot; | |
| 44 | } | ||
| 45 | } | ||
| 46 | 2 | return -ENOENT; | |
| 47 | } | ||
| 48 | |||
| 49 | /* Bounds cover the TLV records as well as their outer length. */ | ||
| 50 | 60 | static int tlv_extent(const struct flash_area *area, uint32_t offset, uint32_t limit, | |
| 51 | uint16_t magic, uint32_t *size) | ||
| 52 | { | ||
| 53 | struct image_tlv_info info; | ||
| 54 | uint32_t position; | ||
| 55 | uint32_t end; | ||
| 56 | int result; | ||
| 57 | |||
| 58 | 60 | if ((offset > limit) || (limit - offset < sizeof(info))) { | |
| 59 | ✗ | return -EBADMSG; | |
| 60 | } | ||
| 61 | 60 | result = flash_area_read(area, offset, &info, sizeof(info)); | |
| 62 | 60 | if (result != 0) { | |
| 63 | ✗ | return result; | |
| 64 | } | ||
| 65 | 60 | *size = sys_le16_to_cpu(info.it_tlv_tot); | |
| 66 | 60 | if ((sys_le16_to_cpu(info.it_magic) != magic) || (*size < sizeof(info)) || | |
| 67 | 60 | (*size > limit - offset)) { | |
| 68 | ✗ | return -EBADMSG; | |
| 69 | } | ||
| 70 | 60 | position = offset + sizeof(info); | |
| 71 | 60 | end = offset + *size; | |
| 72 | 118 | while (position < end) { | |
| 73 | struct image_tlv tlv; | ||
| 74 | uint32_t length; | ||
| 75 | |||
| 76 | 60 | if (end - position < sizeof(tlv)) { | |
| 77 | 2 | return -EBADMSG; | |
| 78 | } | ||
| 79 | 60 | result = flash_area_read(area, position, &tlv, sizeof(tlv)); | |
| 80 | 60 | if (result != 0) { | |
| 81 | ✗ | return result; | |
| 82 | } | ||
| 83 | 60 | position += sizeof(tlv); | |
| 84 | 60 | length = sys_le16_to_cpu(tlv.it_len); | |
| 85 | 60 | if (length > end - position) { | |
| 86 | 2 | return -EBADMSG; | |
| 87 | } | ||
| 88 | 58 | position += length; | |
| 89 | } | ||
| 90 | 58 | return 0; | |
| 91 | } | ||
| 92 | |||
| 93 | 48 | static int image_extent(struct slot_file *file, uint32_t start) | |
| 94 | { | ||
| 95 | struct image_header header; | ||
| 96 | struct flash_pages_info last_page; | ||
| 97 | uint32_t limit; | ||
| 98 | uint32_t header_size; | ||
| 99 | uint32_t body_size; | ||
| 100 | uint32_t protected_size; | ||
| 101 | uint32_t position; | ||
| 102 | uint32_t tlv_size; | ||
| 103 | int result; | ||
| 104 | |||
| 105 | 48 | result = flash_get_page_info_by_offs(flash_area_get_device(file->area), | |
| 106 | 48 | file->area->fa_off + file->area->fa_size - 1U, | |
| 107 | &last_page); | ||
| 108 | 48 | if (result != 0) { | |
| 109 | ✗ | return result; | |
| 110 | } | ||
| 111 | 48 | if (last_page.size >= file->area->fa_size) { | |
| 112 | ✗ | return -EBADMSG; | |
| 113 | } | ||
| 114 | 48 | limit = file->area->fa_size - last_page.size; | |
| 115 | 48 | if ((start > limit) || (limit - start < sizeof(header))) { | |
| 116 | ✗ | return -EBADMSG; | |
| 117 | } | ||
| 118 | 48 | result = flash_area_read(file->area, start, &header, sizeof(header)); | |
| 119 | 48 | if (result != 0) { | |
| 120 | ✗ | return result; | |
| 121 | } | ||
| 122 | 48 | if (sys_le32_to_cpu(header.ih_magic) != IMAGE_MAGIC) { | |
| 123 | 16 | return -ENOENT; | |
| 124 | } | ||
| 125 | 32 | header_size = sys_le16_to_cpu(header.ih_hdr_size); | |
| 126 | 32 | body_size = sys_le32_to_cpu(header.ih_img_size); | |
| 127 | 32 | protected_size = sys_le16_to_cpu(header.ih_protect_tlv_size); | |
| 128 | 32 | if ((header_size < sizeof(header)) || (header_size > limit - start) || (body_size == 0U) || | |
| 129 | 32 | (body_size > limit - start - header_size)) { | |
| 130 | 2 | return -EBADMSG; | |
| 131 | } | ||
| 132 | 30 | position = start + header_size + body_size; | |
| 133 | 30 | if (protected_size != 0U) { | |
| 134 | 30 | result = tlv_extent(file->area, position, limit, IMAGE_TLV_PROT_INFO_MAGIC, | |
| 135 | &tlv_size); | ||
| 136 | 30 | if (result != 0) { | |
| 137 | ✗ | return result; | |
| 138 | } | ||
| 139 | 30 | if (tlv_size != protected_size) { | |
| 140 | ✗ | return -EBADMSG; | |
| 141 | } | ||
| 142 | 30 | position += tlv_size; | |
| 143 | } | ||
| 144 | 30 | result = tlv_extent(file->area, position, limit, IMAGE_TLV_INFO_MAGIC, &tlv_size); | |
| 145 | 30 | if (result != 0) { | |
| 146 | 2 | return result; | |
| 147 | } | ||
| 148 | 28 | file->start = start; | |
| 149 | 28 | file->size = position + tlv_size - start; | |
| 150 | 28 | file->position = 0U; | |
| 151 | 28 | return 0; | |
| 152 | } | ||
| 153 | |||
| 154 | 34 | static void release_image(struct slot_file *file) | |
| 155 | { | ||
| 156 | 34 | flash_area_close(file->area); | |
| 157 | 34 | file->area = NULL; | |
| 158 | 34 | if (file->secondary) { | |
| 159 | 18 | kfsw_fwu_secondary_release(); | |
| 160 | } | ||
| 161 | 34 | } | |
| 162 | |||
| 163 | 38 | static int open_image(int slot, struct slot_file *file) | |
| 164 | { | ||
| 165 | int result; | ||
| 166 | |||
| 167 | 38 | file->secondary = slot == 1; | |
| 168 | 38 | if (file->secondary) { | |
| 169 | 22 | result = kfsw_fwu_secondary_acquire(); | |
| 170 | 22 | if (result != 0) { | |
| 171 | 4 | return result; | |
| 172 | } | ||
| 173 | } | ||
| 174 | 34 | result = flash_area_open(slot_ids[slot], &file->area); | |
| 175 | 34 | if (result != 0) { | |
| 176 | ✗ | if (file->secondary) { | |
| 177 | ✗ | kfsw_fwu_secondary_release(); | |
| 178 | } | ||
| 179 | ✗ | return result; | |
| 180 | } | ||
| 181 | 34 | result = image_extent(file, 0U); | |
| 182 | /* Swap leaves the previous image at the slot base. Incoming images use | ||
| 183 | * the configured offset. Confirmation can change the swap status without | ||
| 184 | * moving those bytes, so locate the header instead of inferring its place. | ||
| 185 | */ | ||
| 186 | 34 | if ((result == -ENOENT) && file->secondary && (CONFIG_KFSW_FWU_SLOT_OFFSET_SECTORS != 0)) { | |
| 187 | 14 | uint32_t offset = kfsw_fwu_slot_write_offset(); | |
| 188 | |||
| 189 | 14 | result = offset == 0U ? -EIO : image_extent(file, offset); | |
| 190 | } | ||
| 191 | 34 | if (result != 0) { | |
| 192 | 6 | release_image(file); | |
| 193 | } | ||
| 194 | 34 | return result; | |
| 195 | } | ||
| 196 | |||
| 197 | 16 | static int slot_open(struct fs_file_t *fp, const char *path, fs_mode_t flags) | |
| 198 | { | ||
| 199 | 16 | int slot = slot_number(path); | |
| 200 | 16 | int result = -EMFILE; | |
| 201 | |||
| 202 | 16 | if (flags != FS_O_READ) { | |
| 203 | ✗ | return -EROFS; | |
| 204 | } | ||
| 205 | 16 | if (slot < 0) { | |
| 206 | ✗ | return slot; | |
| 207 | } | ||
| 208 | 16 | k_mutex_lock(&slot_files_lock, K_FOREVER); | |
| 209 | 18 | for (size_t i = 0; i < ARRAY_SIZE(files); i++) { | |
| 210 | 18 | if (files[i].area == NULL) { | |
| 211 | 16 | result = open_image(slot, &files[i]); | |
| 212 | 16 | if (result == 0) { | |
| 213 | 16 | fp->filep = &files[i]; | |
| 214 | } | ||
| 215 | 16 | break; | |
| 216 | } | ||
| 217 | } | ||
| 218 | 16 | k_mutex_unlock(&slot_files_lock); | |
| 219 | 16 | return result; | |
| 220 | } | ||
| 221 | |||
| 222 | 16 | static int slot_close(struct fs_file_t *fp) | |
| 223 | { | ||
| 224 | 16 | k_mutex_lock(&slot_files_lock, K_FOREVER); | |
| 225 | 16 | release_image(fp->filep); | |
| 226 | 16 | fp->filep = NULL; | |
| 227 | 16 | k_mutex_unlock(&slot_files_lock); | |
| 228 | 16 | return 0; | |
| 229 | } | ||
| 230 | |||
| 231 | 30 | static ssize_t slot_read(struct fs_file_t *fp, void *buffer, size_t size) | |
| 232 | { | ||
| 233 | 30 | struct slot_file *file = fp->filep; | |
| 234 | 30 | int result = 0; | |
| 235 | |||
| 236 | 30 | k_mutex_lock(&slot_files_lock, K_FOREVER); | |
| 237 | 30 | size = MIN(size, file->size - file->position); | |
| 238 | 30 | if (size != 0U) { | |
| 239 | 20 | result = flash_area_read(file->area, file->start + file->position, buffer, size); | |
| 240 | 20 | if (result == 0) { | |
| 241 | 20 | file->position += size; | |
| 242 | } | ||
| 243 | } | ||
| 244 | 30 | k_mutex_unlock(&slot_files_lock); | |
| 245 | 30 | return result == 0 ? (ssize_t)size : result; | |
| 246 | } | ||
| 247 | |||
| 248 | 40 | static int slot_seek(struct fs_file_t *fp, off_t offset, int whence) | |
| 249 | { | ||
| 250 | 40 | struct slot_file *file = fp->filep; | |
| 251 | 40 | int64_t base = 0; | |
| 252 | 40 | int result = 0; | |
| 253 | |||
| 254 | 40 | k_mutex_lock(&slot_files_lock, K_FOREVER); | |
| 255 | 40 | if (whence == FS_SEEK_CUR) { | |
| 256 | 10 | base = file->position; | |
| 257 | 30 | } else if (whence == FS_SEEK_END) { | |
| 258 | 20 | base = file->size; | |
| 259 | 10 | } else if (whence != FS_SEEK_SET) { | |
| 260 | ✗ | result = -EINVAL; | |
| 261 | } | ||
| 262 | 40 | if ((offset < -base) || (offset > (int64_t)file->size - base)) { | |
| 263 | 30 | result = -EINVAL; | |
| 264 | } | ||
| 265 | 40 | if (result == 0) { | |
| 266 | 10 | file->position = (uint32_t)(base + offset); | |
| 267 | } | ||
| 268 | 40 | k_mutex_unlock(&slot_files_lock); | |
| 269 | 40 | return result; | |
| 270 | } | ||
| 271 | |||
| 272 | 20 | static off_t slot_tell(struct fs_file_t *fp) | |
| 273 | { | ||
| 274 | 20 | struct slot_file *file = fp->filep; | |
| 275 | off_t position; | ||
| 276 | |||
| 277 | 20 | k_mutex_lock(&slot_files_lock, K_FOREVER); | |
| 278 | 20 | position = file->position; | |
| 279 | 20 | k_mutex_unlock(&slot_files_lock); | |
| 280 | 20 | return position; | |
| 281 | } | ||
| 282 | |||
| 283 | 20 | static int slot_stat(struct fs_mount_t *mount, const char *path, struct fs_dirent *entry) | |
| 284 | { | ||
| 285 | 20 | struct slot_file file = {0}; | |
| 286 | int slot; | ||
| 287 | int result; | ||
| 288 | |||
| 289 | ARG_UNUSED(mount); | ||
| 290 | 20 | memset(entry, 0, sizeof(*entry)); | |
| 291 | 20 | if (strcmp(path, BOOT_ROOT) == 0) { | |
| 292 | ✗ | entry->type = FS_DIR_ENTRY_DIR; | |
| 293 | ✗ | strcpy(entry->name, "boot"); | |
| 294 | ✗ | return 0; | |
| 295 | } | ||
| 296 | 20 | slot = slot_number(path); | |
| 297 | 20 | if (slot < 0) { | |
| 298 | 2 | return slot; | |
| 299 | } | ||
| 300 | 18 | result = open_image(slot, &file); | |
| 301 | 18 | if (result != 0) { | |
| 302 | 8 | return result; | |
| 303 | } | ||
| 304 | 10 | entry->type = FS_DIR_ENTRY_FILE; | |
| 305 | 10 | entry->size = file.size; | |
| 306 | 10 | strcpy(entry->name, slot_names[slot]); | |
| 307 | 10 | release_image(&file); | |
| 308 | 10 | return 0; | |
| 309 | } | ||
| 310 | |||
| 311 | 2 | static int slot_opendir(struct fs_dir_t *dir, const char *path) | |
| 312 | { | ||
| 313 | 2 | if (strcmp(path, BOOT_ROOT) != 0) { | |
| 314 | ✗ | return -ENOENT; | |
| 315 | } | ||
| 316 | 2 | dir->dirp = (void *)(uintptr_t)1U; | |
| 317 | 2 | return 0; | |
| 318 | } | ||
| 319 | |||
| 320 | 4 | static int slot_readdir(struct fs_dir_t *dir, struct fs_dirent *entry) | |
| 321 | { | ||
| 322 | 6 | while ((uintptr_t)dir->dirp <= ARRAY_SIZE(slot_names)) { | |
| 323 | 4 | struct slot_file file = {0}; | |
| 324 | 4 | int slot = (uintptr_t)dir->dirp - 1U; | |
| 325 | 4 | int result = open_image(slot, &file); | |
| 326 | |||
| 327 | 4 | dir->dirp = (void *)((uintptr_t)dir->dirp + 1U); | |
| 328 | 4 | if ((result == -ENOENT) || (result == -EBUSY) || (result == -EBADMSG)) { | |
| 329 | 2 | continue; | |
| 330 | } | ||
| 331 | 2 | if (result != 0) { | |
| 332 | 2 | return result; | |
| 333 | } | ||
| 334 | 2 | memset(entry, 0, sizeof(*entry)); | |
| 335 | 2 | entry->type = FS_DIR_ENTRY_FILE; | |
| 336 | 2 | entry->size = file.size; | |
| 337 | 2 | strcpy(entry->name, slot_names[slot]); | |
| 338 | 2 | release_image(&file); | |
| 339 | 2 | return 0; | |
| 340 | } | ||
| 341 | 2 | memset(entry, 0, sizeof(*entry)); | |
| 342 | 2 | return 0; | |
| 343 | } | ||
| 344 | |||
| 345 | 2 | static int slot_closedir(struct fs_dir_t *dir) | |
| 346 | { | ||
| 347 | 2 | dir->dirp = NULL; | |
| 348 | 2 | return 0; | |
| 349 | } | ||
| 350 | |||
| 351 | 2 | static int slot_mount(struct fs_mount_t *mount) | |
| 352 | { | ||
| 353 | ARG_UNUSED(mount); | ||
| 354 | 2 | return 0; | |
| 355 | } | ||
| 356 | |||
| 357 | static const struct fs_file_system_t slot_fs = { | ||
| 358 | .open = slot_open, | ||
| 359 | .close = slot_close, | ||
| 360 | .read = slot_read, | ||
| 361 | .lseek = slot_seek, | ||
| 362 | .tell = slot_tell, | ||
| 363 | .stat = slot_stat, | ||
| 364 | .opendir = slot_opendir, | ||
| 365 | .readdir = slot_readdir, | ||
| 366 | .closedir = slot_closedir, | ||
| 367 | .mount = slot_mount, | ||
| 368 | }; | ||
| 369 | |||
| 370 | static struct fs_mount_t slot_mountpoint = { | ||
| 371 | .type = FS_TYPE_EXTERNAL_BASE, | ||
| 372 | .mnt_point = BOOT_ROOT, | ||
| 373 | .flags = FS_MOUNT_FLAG_READ_ONLY, | ||
| 374 | }; | ||
| 375 | |||
| 376 | 2 | int kfsw_fwu_files_mount(void) | |
| 377 | { | ||
| 378 | 2 | int result = fs_register(slot_mountpoint.type, &slot_fs); | |
| 379 | |||
| 380 | 2 | if (result != 0) { | |
| 381 | ✗ | return result; | |
| 382 | } | ||
| 383 | 2 | result = fs_mount(&slot_mountpoint); | |
| 384 | 2 | if (result != 0) { | |
| 385 | ✗ | (void)fs_unregister(slot_mountpoint.type, &slot_fs); | |
| 386 | } | ||
| 387 | 2 | return result; | |
| 388 | } | ||
| 389 |