b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * FIS table updating code for mtd |
| 3 | * |
| 4 | * Copyright (C) 2009 Felix Fietkau <nbd@nbd.name> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License v2 |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | #include <sys/mman.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <unistd.h> |
| 20 | #include <stdio.h> |
| 21 | #include "crc32.h" |
| 22 | #include "mtd.h" |
| 23 | #include "fis.h" |
| 24 | |
| 25 | struct fis_image_hdr { |
| 26 | unsigned char name[16]; |
| 27 | uint32_t flash_base; |
| 28 | uint32_t mem_base; |
| 29 | uint32_t size; |
| 30 | uint32_t entry_point; |
| 31 | uint32_t data_length; |
| 32 | } __attribute__((packed)); |
| 33 | |
| 34 | struct fis_image_crc { |
| 35 | uint32_t desc; |
| 36 | uint32_t file; |
| 37 | } __attribute__((packed)); |
| 38 | |
| 39 | struct fis_image_desc { |
| 40 | struct fis_image_hdr hdr; |
| 41 | char _pad[256 - sizeof(struct fis_image_hdr) - sizeof(struct fis_image_crc)]; |
| 42 | struct fis_image_crc crc; |
| 43 | } __attribute__((packed)); |
| 44 | |
| 45 | static int fis_fd = -1; |
| 46 | static struct fis_image_desc *fis_desc; |
| 47 | static int fis_erasesize = 0; |
| 48 | |
| 49 | static void |
| 50 | fis_close(void) |
| 51 | { |
| 52 | if (fis_desc) |
| 53 | munmap(fis_desc, fis_erasesize); |
| 54 | |
| 55 | if (fis_fd >= 0) |
| 56 | close(fis_fd); |
| 57 | |
| 58 | fis_fd = -1; |
| 59 | fis_desc = NULL; |
| 60 | } |
| 61 | |
| 62 | static struct fis_image_desc * |
| 63 | fis_open(void) |
| 64 | { |
| 65 | struct fis_image_desc *desc; |
| 66 | |
| 67 | if (fis_fd >= 0) |
| 68 | fis_close(); |
| 69 | |
| 70 | fis_fd = mtd_check_open("FIS directory"); |
| 71 | if (fis_fd < 0) |
| 72 | goto error; |
| 73 | |
| 74 | close(fis_fd); |
| 75 | fis_fd = mtd_open("FIS directory", true); |
| 76 | if (fis_fd < 0) |
| 77 | goto error; |
| 78 | |
| 79 | fis_erasesize = erasesize; |
| 80 | desc = mmap(NULL, erasesize, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fis_fd, 0); |
| 81 | if (desc == MAP_FAILED) |
| 82 | goto error; |
| 83 | |
| 84 | fis_desc = desc; |
| 85 | return desc; |
| 86 | |
| 87 | error: |
| 88 | fis_close(); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | fis_validate(struct fis_part *old, int n_old, struct fis_part *new, int n_new) |
| 94 | { |
| 95 | struct fis_image_desc *desc; |
| 96 | void *end; |
| 97 | int found = 0; |
| 98 | int i; |
| 99 | |
| 100 | desc = fis_open(); |
| 101 | if (!desc) |
| 102 | return -1; |
| 103 | |
| 104 | for (i = 0; i < n_new - 1; i++) { |
| 105 | if (!new[i].size) { |
| 106 | fprintf(stderr, "FIS error: only the last partition can detect the size automatically\n"); |
| 107 | i = -1; |
| 108 | goto done; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | end = desc; |
| 113 | end = (char *) end + fis_erasesize; |
| 114 | while ((void *) desc < end) { |
| 115 | if (!desc->hdr.name[0] || (desc->hdr.name[0] == 0xff)) |
| 116 | break; |
| 117 | |
| 118 | for (i = 0; i < n_old; i++) { |
| 119 | if (!strncmp((char *) desc->hdr.name, (char *) old[i].name, sizeof(desc->hdr.name))) { |
| 120 | found++; |
| 121 | goto next; |
| 122 | } |
| 123 | } |
| 124 | next: |
| 125 | desc++; |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | if (found == n_old) |
| 130 | i = 1; |
| 131 | else |
| 132 | i = -1; |
| 133 | |
| 134 | done: |
| 135 | fis_close(); |
| 136 | return i; |
| 137 | } |
| 138 | |
| 139 | int |
| 140 | fis_remap(struct fis_part *old, int n_old, struct fis_part *new, int n_new) |
| 141 | { |
| 142 | struct fis_image_desc *first = NULL; |
| 143 | struct fis_image_desc *last = NULL; |
| 144 | struct fis_image_desc *first_fb = NULL; |
| 145 | struct fis_image_desc *last_fb = NULL; |
| 146 | struct fis_image_desc *desc; |
| 147 | struct fis_part *part; |
| 148 | uint32_t offset = 0, size = 0; |
| 149 | char *start, *end, *tmp; |
| 150 | int i; |
| 151 | |
| 152 | desc = fis_open(); |
| 153 | if (!desc) |
| 154 | return -1; |
| 155 | |
| 156 | if (!quiet) |
| 157 | fprintf(stderr, "Updating FIS table... \n"); |
| 158 | |
| 159 | start = (char *) desc; |
| 160 | end = (char *) desc + fis_erasesize; |
| 161 | while ((char *) desc < end) { |
| 162 | if (!desc->hdr.name[0] || (desc->hdr.name[0] == 0xff)) |
| 163 | break; |
| 164 | |
| 165 | /* update max offset */ |
| 166 | if (offset < desc->hdr.flash_base) |
| 167 | offset = desc->hdr.flash_base; |
| 168 | |
| 169 | for (i = 0; i < n_old; i++) { |
| 170 | if (!strncmp((char *) desc->hdr.name, (char *) old[i].name, sizeof(desc->hdr.name))) { |
| 171 | last = desc; |
| 172 | if (!first) |
| 173 | first = desc; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | desc++; |
| 178 | } |
| 179 | desc--; |
| 180 | |
| 181 | first_fb = first; |
| 182 | last_fb = last; |
| 183 | |
| 184 | if (first_fb->hdr.flash_base > last_fb->hdr.flash_base) { |
| 185 | first_fb = last; |
| 186 | last_fb = first; |
| 187 | } |
| 188 | |
| 189 | /* determine size of available space */ |
| 190 | desc = (struct fis_image_desc *) start; |
| 191 | while ((char *) desc < end) { |
| 192 | if (!desc->hdr.name[0] || (desc->hdr.name[0] == 0xff)) |
| 193 | break; |
| 194 | |
| 195 | if (desc->hdr.flash_base > last_fb->hdr.flash_base && |
| 196 | desc->hdr.flash_base < offset) |
| 197 | offset = desc->hdr.flash_base; |
| 198 | |
| 199 | desc++; |
| 200 | } |
| 201 | desc--; |
| 202 | |
| 203 | size = offset - first_fb->hdr.flash_base; |
| 204 | |
| 205 | last++; |
| 206 | desc = first + n_new; |
| 207 | offset = first_fb->hdr.flash_base; |
| 208 | |
| 209 | if (desc != last) { |
| 210 | if (desc > last) |
| 211 | tmp = (char *) desc; |
| 212 | else |
| 213 | tmp = (char *) last; |
| 214 | |
| 215 | memmove(desc, last, end - tmp); |
| 216 | if (desc < last) { |
| 217 | tmp = end - (last - desc) * sizeof(struct fis_image_desc); |
| 218 | memset(tmp, 0xff, tmp - end); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | for (part = new, desc = first; desc < first + n_new; desc++, part++) { |
| 223 | memset(desc, 0, sizeof(struct fis_image_desc)); |
| 224 | memcpy(desc->hdr.name, part->name, sizeof(desc->hdr.name)); |
| 225 | desc->crc.desc = 0; |
| 226 | desc->crc.file = part->crc; |
| 227 | |
| 228 | desc->hdr.flash_base = offset; |
| 229 | desc->hdr.mem_base = part->loadaddr; |
| 230 | desc->hdr.entry_point = part->loadaddr; |
| 231 | desc->hdr.size = (part->size > 0) ? part->size : size; |
| 232 | desc->hdr.data_length = (part->length > 0) ? part->length : |
| 233 | desc->hdr.size; |
| 234 | offset += desc->hdr.size; |
| 235 | size -= desc->hdr.size; |
| 236 | } |
| 237 | |
| 238 | msync(fis_desc, fis_erasesize, MS_SYNC|MS_INVALIDATE); |
| 239 | fis_close(); |
| 240 | |
| 241 | return 0; |
| 242 | } |