dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | |
| 34 | #include <assert.h> |
| 35 | #include <errno.h> |
| 36 | #include <getopt.h> |
| 37 | #include <limits.h> |
| 38 | #include <stdarg.h> |
| 39 | #include <stdint.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <unistd.h> |
| 44 | |
dp-arm | 12e893b | 2016-08-24 13:21:08 +0100 | [diff] [blame] | 45 | #include <openssl/sha.h> |
| 46 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 47 | #include "fiptool.h" |
| 48 | #include "firmware_image_package.h" |
| 49 | #include "tbbr_config.h" |
| 50 | |
| 51 | #define OPT_TOC_ENTRY 0 |
| 52 | #define OPT_PLAT_TOC_FLAGS 1 |
| 53 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 54 | static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid); |
| 55 | static image_t *lookup_image_from_uuid(const uuid_t *uuid); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 56 | static int info_cmd(int argc, char *argv[]); |
| 57 | static void info_usage(void); |
| 58 | static int create_cmd(int argc, char *argv[]); |
| 59 | static void create_usage(void); |
| 60 | static int update_cmd(int argc, char *argv[]); |
| 61 | static void update_usage(void); |
| 62 | static int unpack_cmd(int argc, char *argv[]); |
| 63 | static void unpack_usage(void); |
| 64 | static int remove_cmd(int argc, char *argv[]); |
| 65 | static void remove_usage(void); |
| 66 | static int version_cmd(int argc, char *argv[]); |
| 67 | static void version_usage(void); |
| 68 | static int help_cmd(int argc, char *argv[]); |
| 69 | static void usage(void); |
| 70 | |
| 71 | /* Available subcommands. */ |
| 72 | static cmd_t cmds[] = { |
| 73 | { .name = "info", .handler = info_cmd, .usage = info_usage }, |
| 74 | { .name = "create", .handler = create_cmd, .usage = create_usage }, |
| 75 | { .name = "update", .handler = update_cmd, .usage = update_usage }, |
| 76 | { .name = "unpack", .handler = unpack_cmd, .usage = unpack_usage }, |
| 77 | { .name = "remove", .handler = remove_cmd, .usage = remove_usage }, |
| 78 | { .name = "version", .handler = version_cmd, .usage = version_usage }, |
| 79 | { .name = "help", .handler = help_cmd, .usage = NULL }, |
| 80 | }; |
| 81 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 82 | static image_desc_t *image_desc_head; |
| 83 | static size_t nr_image_descs; |
| 84 | static image_t *image_head; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 85 | static size_t nr_images; |
| 86 | static uuid_t uuid_null = { 0 }; |
| 87 | static int verbose; |
| 88 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 89 | static void vlog(int prio, const char *msg, va_list ap) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 90 | { |
| 91 | char *prefix[] = { "DEBUG", "WARN", "ERROR" }; |
| 92 | |
| 93 | fprintf(stderr, "%s: ", prefix[prio]); |
| 94 | vfprintf(stderr, msg, ap); |
| 95 | fputc('\n', stderr); |
| 96 | } |
| 97 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 98 | static void log_dbgx(const char *msg, ...) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 99 | { |
| 100 | va_list ap; |
| 101 | |
| 102 | va_start(ap, msg); |
| 103 | vlog(LOG_DBG, msg, ap); |
| 104 | va_end(ap); |
| 105 | } |
| 106 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 107 | static void log_warnx(const char *msg, ...) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 108 | { |
| 109 | va_list ap; |
| 110 | |
| 111 | va_start(ap, msg); |
| 112 | vlog(LOG_WARN, msg, ap); |
| 113 | va_end(ap); |
| 114 | } |
| 115 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 116 | static void log_err(const char *msg, ...) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 117 | { |
| 118 | char buf[512]; |
| 119 | va_list ap; |
| 120 | |
| 121 | va_start(ap, msg); |
| 122 | snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno)); |
| 123 | vlog(LOG_ERR, buf, ap); |
| 124 | va_end(ap); |
| 125 | exit(1); |
| 126 | } |
| 127 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 128 | static void log_errx(const char *msg, ...) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 129 | { |
| 130 | va_list ap; |
| 131 | |
| 132 | va_start(ap, msg); |
| 133 | vlog(LOG_ERR, msg, ap); |
| 134 | va_end(ap); |
| 135 | exit(1); |
| 136 | } |
| 137 | |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 138 | static char *xstrdup(const char *s, const char *msg) |
| 139 | { |
| 140 | char *d; |
| 141 | |
| 142 | d = strdup(s); |
| 143 | if (d == NULL) |
| 144 | log_errx("strdup: ", msg); |
| 145 | return d; |
| 146 | } |
| 147 | |
| 148 | static void *xmalloc(size_t size, const char *msg) |
| 149 | { |
| 150 | void *d; |
| 151 | |
| 152 | d = malloc(size); |
| 153 | if (d == NULL) |
| 154 | log_errx("malloc: ", msg); |
| 155 | return d; |
| 156 | } |
| 157 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 158 | static image_desc_t *new_image_desc(const uuid_t *uuid, |
| 159 | const char *name, const char *cmdline_name) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 160 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 161 | image_desc_t *desc; |
| 162 | |
| 163 | desc = xmalloc(sizeof(*desc), |
| 164 | "failed to allocate memory for image descriptor"); |
| 165 | memcpy(&desc->uuid, uuid, sizeof(uuid_t)); |
| 166 | desc->name = xstrdup(name, |
| 167 | "failed to allocate memory for image name"); |
| 168 | desc->cmdline_name = xstrdup(cmdline_name, |
| 169 | "failed to allocate memory for image command line name"); |
| 170 | desc->action = DO_UNSPEC; |
| 171 | desc->action_arg = NULL; |
| 172 | return desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 173 | } |
| 174 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 175 | static void free_image_desc(image_desc_t *desc) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 176 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 177 | free(desc->name); |
| 178 | free(desc->cmdline_name); |
| 179 | free(desc->action_arg); |
| 180 | free(desc); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 181 | } |
| 182 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 183 | static void add_image_desc(image_desc_t *desc) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 184 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 185 | assert(lookup_image_desc_from_uuid(&desc->uuid) == NULL); |
| 186 | desc->next = image_desc_head; |
| 187 | image_desc_head = desc; |
| 188 | nr_image_descs++; |
| 189 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 190 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 191 | static void free_image_descs(void) |
| 192 | { |
| 193 | image_desc_t *desc = image_desc_head, *tmp; |
| 194 | |
| 195 | while (desc != NULL) { |
| 196 | tmp = desc->next; |
| 197 | free_image_desc(desc); |
| 198 | desc = tmp; |
| 199 | nr_image_descs--; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 200 | } |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 201 | assert(nr_image_descs == 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 202 | } |
| 203 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 204 | static void fill_image_descs(void) |
| 205 | { |
| 206 | toc_entry_t *toc_entry; |
| 207 | |
| 208 | for (toc_entry = toc_entries; |
| 209 | toc_entry->cmdline_name != NULL; |
| 210 | toc_entry++) { |
| 211 | image_desc_t *desc; |
| 212 | |
| 213 | desc = new_image_desc(&toc_entry->uuid, |
| 214 | toc_entry->name, |
| 215 | toc_entry->cmdline_name); |
| 216 | add_image_desc(desc); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static void add_image(image_t *image) |
| 221 | { |
| 222 | assert(lookup_image_from_uuid(&image->uuid) == NULL); |
| 223 | image->next = image_head; |
| 224 | image_head = image; |
| 225 | nr_images++; |
| 226 | } |
| 227 | |
| 228 | static void free_image(image_t *image) |
| 229 | { |
| 230 | free(image->buffer); |
| 231 | free(image); |
| 232 | } |
| 233 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 234 | static void remove_image(image_t *image) |
| 235 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 236 | image_t *tmp = image_head, *prev; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 237 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 238 | if (tmp == image) { |
| 239 | image_head = tmp->next; |
| 240 | free_image(tmp); |
| 241 | } else { |
| 242 | while (tmp != NULL && tmp != image) { |
| 243 | prev = tmp; |
| 244 | tmp = tmp->next; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 245 | } |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 246 | assert(tmp != NULL); |
| 247 | prev->next = tmp->next; |
| 248 | free_image(tmp); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 249 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 250 | nr_images--; |
| 251 | } |
| 252 | |
| 253 | static void free_images(void) |
| 254 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 255 | image_t *image = image_head, *tmp; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 256 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 257 | while (image != NULL) { |
| 258 | tmp = image->next; |
| 259 | free_image(image); |
| 260 | image = tmp; |
| 261 | nr_images--; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 262 | } |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 263 | assert(nr_images == 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 264 | } |
| 265 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 266 | static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 267 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 268 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 269 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 270 | for (desc = image_desc_head; desc != NULL; desc = desc->next) |
| 271 | if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0) |
| 272 | return desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 273 | return NULL; |
| 274 | } |
| 275 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 276 | static image_desc_t *lookup_image_desc_from_opt(const char *opt) |
| 277 | { |
| 278 | image_desc_t *desc; |
| 279 | |
| 280 | for (desc = image_desc_head; desc != NULL; desc = desc->next) |
| 281 | if (strcmp(desc->cmdline_name, opt) == 0) |
| 282 | return desc; |
| 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | static image_t *lookup_image_from_uuid(const uuid_t *uuid) |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 287 | { |
| 288 | image_t *image; |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 289 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 290 | for (image = image_head; image != NULL; image = image->next) |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 291 | if (memcmp(&image->uuid, uuid, sizeof(uuid_t)) == 0) |
| 292 | return image; |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 293 | return NULL; |
| 294 | } |
| 295 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 296 | static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 297 | { |
| 298 | struct stat st; |
| 299 | FILE *fp; |
| 300 | char *buf, *bufend; |
| 301 | fip_toc_header_t *toc_header; |
| 302 | fip_toc_entry_t *toc_entry; |
| 303 | image_t *image; |
| 304 | int terminated = 0; |
| 305 | |
| 306 | fp = fopen(filename, "r"); |
| 307 | if (fp == NULL) |
| 308 | log_err("fopen %s", filename); |
| 309 | |
| 310 | if (fstat(fileno(fp), &st) == -1) |
| 311 | log_err("fstat %s", filename); |
| 312 | |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 313 | buf = xmalloc(st.st_size, "failed to load file into memory"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 314 | if (fread(buf, 1, st.st_size, fp) != st.st_size) |
| 315 | log_errx("Failed to read %s", filename); |
| 316 | bufend = buf + st.st_size; |
| 317 | fclose(fp); |
| 318 | |
| 319 | if (st.st_size < sizeof(fip_toc_header_t)) |
| 320 | log_errx("FIP %s is truncated", filename); |
| 321 | |
| 322 | toc_header = (fip_toc_header_t *)buf; |
| 323 | toc_entry = (fip_toc_entry_t *)(toc_header + 1); |
| 324 | |
| 325 | if (toc_header->name != TOC_HEADER_NAME) |
| 326 | log_errx("%s is not a FIP file", filename); |
| 327 | |
| 328 | /* Return the ToC header if the caller wants it. */ |
| 329 | if (toc_header_out != NULL) |
| 330 | *toc_header_out = *toc_header; |
| 331 | |
| 332 | /* Walk through each ToC entry in the file. */ |
| 333 | while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) { |
| 334 | /* Found the ToC terminator, we are done. */ |
| 335 | if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) { |
| 336 | terminated = 1; |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Build a new image out of the ToC entry and add it to the |
| 342 | * table of images. |
| 343 | */ |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 344 | image = xmalloc(sizeof(*image), |
| 345 | "failed to allocate memory for image"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 346 | memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t)); |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 347 | image->buffer = xmalloc(toc_entry->size, |
| 348 | "failed to allocate image buffer, is FIP file corrupted?"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 349 | /* Overflow checks before memory copy. */ |
| 350 | if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address) |
| 351 | log_errx("FIP %s is corrupted", filename); |
| 352 | if (toc_entry->size + toc_entry->offset_address > st.st_size) |
| 353 | log_errx("FIP %s is corrupted", filename); |
| 354 | |
| 355 | memcpy(image->buffer, buf + toc_entry->offset_address, |
| 356 | toc_entry->size); |
| 357 | image->size = toc_entry->size; |
| 358 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 359 | add_image(image); |
| 360 | |
| 361 | toc_entry++; |
| 362 | } |
| 363 | |
| 364 | if (terminated == 0) |
| 365 | log_errx("FIP %s does not have a ToC terminator entry", |
| 366 | filename); |
| 367 | free(buf); |
| 368 | return 0; |
| 369 | } |
| 370 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 371 | static image_t *read_image_from_file(const uuid_t *uuid, const char *filename) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 372 | { |
| 373 | struct stat st; |
| 374 | image_t *image; |
| 375 | FILE *fp; |
| 376 | |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 377 | assert(uuid != NULL); |
| 378 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 379 | fp = fopen(filename, "r"); |
| 380 | if (fp == NULL) |
| 381 | log_err("fopen %s", filename); |
| 382 | |
| 383 | if (fstat(fileno(fp), &st) == -1) |
| 384 | log_errx("fstat %s", filename); |
| 385 | |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 386 | image = xmalloc(sizeof(*image), "failed to allocate memory for image"); |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 387 | memcpy(&image->uuid, uuid, sizeof(uuid_t)); |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 388 | image->buffer = xmalloc(st.st_size, "failed to allocate image buffer"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 389 | if (fread(image->buffer, 1, st.st_size, fp) != st.st_size) |
| 390 | log_errx("Failed to read %s", filename); |
| 391 | image->size = st.st_size; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 392 | |
| 393 | fclose(fp); |
| 394 | return image; |
| 395 | } |
| 396 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 397 | static int write_image_to_file(const image_t *image, const char *filename) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 398 | { |
| 399 | FILE *fp; |
| 400 | |
| 401 | fp = fopen(filename, "w"); |
| 402 | if (fp == NULL) |
| 403 | log_err("fopen"); |
| 404 | if (fwrite(image->buffer, 1, image->size, fp) != image->size) |
| 405 | log_errx("Failed to write %s", filename); |
| 406 | fclose(fp); |
| 407 | return 0; |
| 408 | } |
| 409 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 410 | static struct option *add_opt(struct option *opts, size_t *nr_opts, |
| 411 | const char *name, int has_arg, int val) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 412 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 413 | opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts)); |
| 414 | if (opts == NULL) |
| 415 | log_err("realloc"); |
| 416 | opts[*nr_opts].name = name; |
| 417 | opts[*nr_opts].has_arg = has_arg; |
| 418 | opts[*nr_opts].flag = NULL; |
| 419 | opts[*nr_opts].val = val; |
| 420 | ++*nr_opts; |
| 421 | return opts; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 422 | } |
| 423 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 424 | static struct option *fill_common_opts(struct option *opts, size_t *nr_opts, |
| 425 | int has_arg) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 426 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 427 | image_desc_t *desc; |
| 428 | |
| 429 | for (desc = image_desc_head; desc != NULL; desc = desc->next) |
| 430 | opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg, |
| 431 | OPT_TOC_ENTRY); |
| 432 | return opts; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 433 | } |
| 434 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 435 | static void md_print(const unsigned char *md, size_t len) |
dp-arm | 12e893b | 2016-08-24 13:21:08 +0100 | [diff] [blame] | 436 | { |
| 437 | size_t i; |
| 438 | |
| 439 | for (i = 0; i < len; i++) |
| 440 | printf("%02x", md[i]); |
| 441 | } |
| 442 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 443 | static int info_cmd(int argc, char *argv[]) |
| 444 | { |
| 445 | image_t *image; |
| 446 | uint64_t image_offset; |
| 447 | uint64_t image_size = 0; |
| 448 | fip_toc_header_t toc_header; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 449 | |
| 450 | if (argc != 2) |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 451 | info_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 452 | argc--, argv++; |
| 453 | |
| 454 | parse_fip(argv[0], &toc_header); |
| 455 | |
| 456 | if (verbose) { |
| 457 | log_dbgx("toc_header[name]: 0x%llX", |
| 458 | (unsigned long long)toc_header.name); |
| 459 | log_dbgx("toc_header[serial_number]: 0x%llX", |
| 460 | (unsigned long long)toc_header.serial_number); |
| 461 | log_dbgx("toc_header[flags]: 0x%llX", |
| 462 | (unsigned long long)toc_header.flags); |
| 463 | } |
| 464 | |
| 465 | image_offset = sizeof(fip_toc_header_t) + |
| 466 | (sizeof(fip_toc_entry_t) * (nr_images + 1)); |
| 467 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 468 | for (image = image_head; image != NULL; image = image->next) { |
| 469 | image_desc_t *desc; |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 470 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 471 | desc = lookup_image_desc_from_uuid(&image->uuid); |
| 472 | if (desc != NULL) |
| 473 | printf("%s: ", desc->name); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 474 | else |
| 475 | printf("Unknown entry: "); |
| 476 | image_size = image->size; |
| 477 | printf("offset=0x%llX, size=0x%llX", |
| 478 | (unsigned long long)image_offset, |
| 479 | (unsigned long long)image_size); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 480 | if (desc != NULL) |
dp-arm | 12e893b | 2016-08-24 13:21:08 +0100 | [diff] [blame] | 481 | printf(", cmdline=\"--%s\"", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 482 | desc->cmdline_name); |
dp-arm | 12e893b | 2016-08-24 13:21:08 +0100 | [diff] [blame] | 483 | if (verbose) { |
| 484 | unsigned char md[SHA256_DIGEST_LENGTH]; |
| 485 | |
| 486 | SHA256(image->buffer, image_size, md); |
| 487 | printf(", sha256="); |
| 488 | md_print(md, sizeof(md)); |
| 489 | } |
| 490 | putchar('\n'); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 491 | image_offset += image_size; |
| 492 | } |
| 493 | |
| 494 | free_images(); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static void info_usage(void) |
| 499 | { |
| 500 | printf("fiptool info FIP_FILENAME\n"); |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 501 | exit(1); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 502 | } |
| 503 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 504 | static int pack_images(const char *filename, uint64_t toc_flags) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 505 | { |
| 506 | FILE *fp; |
| 507 | image_t *image; |
| 508 | fip_toc_header_t *toc_header; |
| 509 | fip_toc_entry_t *toc_entry; |
| 510 | char *buf; |
| 511 | uint64_t entry_offset, buf_size, payload_size; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 512 | |
| 513 | /* Calculate total payload size and allocate scratch buffer. */ |
| 514 | payload_size = 0; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 515 | for (image = image_head; image != NULL; image = image->next) |
| 516 | payload_size += image->size; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 517 | |
| 518 | buf_size = sizeof(fip_toc_header_t) + |
| 519 | sizeof(fip_toc_entry_t) * (nr_images + 1); |
| 520 | buf = calloc(1, buf_size); |
| 521 | if (buf == NULL) |
| 522 | log_err("calloc"); |
| 523 | |
| 524 | /* Build up header and ToC entries from the image table. */ |
| 525 | toc_header = (fip_toc_header_t *)buf; |
| 526 | toc_header->name = TOC_HEADER_NAME; |
| 527 | toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER; |
| 528 | toc_header->flags = toc_flags; |
| 529 | |
| 530 | toc_entry = (fip_toc_entry_t *)(toc_header + 1); |
| 531 | |
| 532 | entry_offset = buf_size; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 533 | for (image = image_head; image != NULL; image = image->next) { |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 534 | memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t)); |
| 535 | toc_entry->offset_address = entry_offset; |
| 536 | toc_entry->size = image->size; |
| 537 | toc_entry->flags = 0; |
| 538 | entry_offset += toc_entry->size; |
| 539 | toc_entry++; |
| 540 | } |
| 541 | |
| 542 | /* Append a null uuid entry to mark the end of ToC entries. */ |
| 543 | memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)); |
| 544 | toc_entry->offset_address = entry_offset; |
| 545 | toc_entry->size = 0; |
| 546 | toc_entry->flags = 0; |
| 547 | |
| 548 | /* Generate the FIP file. */ |
| 549 | fp = fopen(filename, "w"); |
| 550 | if (fp == NULL) |
| 551 | log_err("fopen %s", filename); |
| 552 | |
| 553 | if (verbose) |
| 554 | log_dbgx("Metadata size: %zu bytes", buf_size); |
| 555 | |
| 556 | if (fwrite(buf, 1, buf_size, fp) != buf_size) |
| 557 | log_errx("Failed to write image to %s", filename); |
| 558 | free(buf); |
| 559 | |
| 560 | if (verbose) |
| 561 | log_dbgx("Payload size: %zu bytes", payload_size); |
| 562 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 563 | for (image = image_head; image != NULL; image = image->next) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 564 | if (fwrite(image->buffer, 1, image->size, fp) != image->size) |
| 565 | log_errx("Failed to write image to %s", filename); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 566 | |
| 567 | fclose(fp); |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * This function is shared between the create and update subcommands. |
| 573 | * The difference between the two subcommands is that when the FIP file |
| 574 | * is created, the parsing of an existing FIP is skipped. This results |
| 575 | * in update_fip() creating the new FIP file from scratch because the |
| 576 | * internal image table is not populated. |
| 577 | */ |
| 578 | static void update_fip(void) |
| 579 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 580 | image_desc_t *desc; |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 581 | image_t *new_image, *old_image; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 582 | |
| 583 | /* Add or replace images in the FIP file. */ |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 584 | for (desc = image_desc_head; desc != NULL; desc = desc->next) { |
| 585 | if (desc->action != DO_PACK) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 586 | continue; |
| 587 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 588 | new_image = read_image_from_file(&desc->uuid, |
| 589 | desc->action_arg); |
| 590 | old_image = lookup_image_from_uuid(&desc->uuid); |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 591 | if (old_image != NULL) { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 592 | if (verbose) { |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 593 | log_dbgx("Replacing image %s.bin with %s", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 594 | desc->cmdline_name, |
| 595 | desc->action_arg); |
| 596 | } |
| 597 | remove_image(old_image); |
| 598 | add_image(new_image); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 599 | } else { |
| 600 | if (verbose) |
| 601 | log_dbgx("Adding image %s", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 602 | desc->action_arg); |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 603 | add_image(new_image); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 604 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 608 | static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 609 | { |
| 610 | unsigned long long flags; |
| 611 | char *endptr; |
| 612 | |
| 613 | errno = 0; |
| 614 | flags = strtoull(arg, &endptr, 16); |
| 615 | if (*endptr != '\0' || flags > UINT16_MAX || errno != 0) |
| 616 | log_errx("Invalid platform ToC flags: %s", arg); |
| 617 | /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */ |
| 618 | *toc_flags |= flags << 32; |
| 619 | } |
| 620 | |
| 621 | static int create_cmd(int argc, char *argv[]) |
| 622 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 623 | struct option *opts = NULL; |
| 624 | size_t nr_opts = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 625 | unsigned long long toc_flags = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 626 | |
| 627 | if (argc < 2) |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 628 | create_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 629 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 630 | opts = fill_common_opts(opts, &nr_opts, required_argument); |
| 631 | opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument, |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 632 | OPT_PLAT_TOC_FLAGS); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 633 | opts = add_opt(opts, &nr_opts, NULL, 0, 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 634 | |
| 635 | while (1) { |
dp-arm | fe92b89 | 2016-11-07 11:13:54 +0000 | [diff] [blame] | 636 | int c, opt_index = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 637 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 638 | c = getopt_long(argc, argv, "", opts, &opt_index); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 639 | if (c == -1) |
| 640 | break; |
| 641 | |
| 642 | switch (c) { |
| 643 | case OPT_TOC_ENTRY: { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 644 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 645 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 646 | desc = lookup_image_desc_from_opt(opts[opt_index].name); |
| 647 | assert(desc != NULL); |
| 648 | if (desc->action != DO_UNSPEC) |
| 649 | free(desc->action_arg); |
| 650 | desc->action = DO_PACK; |
| 651 | desc->action_arg = xstrdup(optarg, |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 652 | "failed to allocate memory for argument"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | case OPT_PLAT_TOC_FLAGS: |
| 656 | parse_plat_toc_flags(optarg, &toc_flags); |
| 657 | break; |
| 658 | default: |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 659 | create_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | argc -= optind; |
| 663 | argv += optind; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 664 | free(opts); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 665 | |
| 666 | if (argc == 0) |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 667 | create_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 668 | |
| 669 | update_fip(); |
| 670 | |
| 671 | pack_images(argv[0], toc_flags); |
| 672 | free_images(); |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | static void create_usage(void) |
| 677 | { |
| 678 | toc_entry_t *toc_entry = toc_entries; |
| 679 | |
dp-arm | 1b434b0 | 2016-08-23 14:31:41 +0100 | [diff] [blame] | 680 | printf("fiptool create [--plat-toc-flags <value>] [opts] FIP_FILENAME\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 681 | printf(" --plat-toc-flags <value>\t16-bit platform specific flag field " |
| 682 | "occupying bits 32-47 in 64-bit ToC header.\n"); |
| 683 | fputc('\n', stderr); |
| 684 | printf("Specific images are packed with the following options:\n"); |
| 685 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 686 | printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, |
| 687 | toc_entry->name); |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 688 | exit(1); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static int update_cmd(int argc, char *argv[]) |
| 692 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 693 | struct option *opts = NULL; |
| 694 | size_t nr_opts = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 695 | char outfile[FILENAME_MAX] = { 0 }; |
| 696 | fip_toc_header_t toc_header = { 0 }; |
| 697 | unsigned long long toc_flags = 0; |
| 698 | int pflag = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 699 | |
| 700 | if (argc < 2) |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 701 | update_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 702 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 703 | opts = fill_common_opts(opts, &nr_opts, required_argument); |
| 704 | opts = add_opt(opts, &nr_opts, "out", required_argument, 'o'); |
| 705 | opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument, |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 706 | OPT_PLAT_TOC_FLAGS); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 707 | opts = add_opt(opts, &nr_opts, NULL, 0, 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 708 | |
| 709 | while (1) { |
dp-arm | fe92b89 | 2016-11-07 11:13:54 +0000 | [diff] [blame] | 710 | int c, opt_index = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 711 | |
| 712 | c = getopt_long(argc, argv, "o:", opts, &opt_index); |
| 713 | if (c == -1) |
| 714 | break; |
| 715 | |
| 716 | switch (c) { |
| 717 | case OPT_TOC_ENTRY: { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 718 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 719 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 720 | desc = lookup_image_desc_from_opt(opts[opt_index].name); |
| 721 | assert(desc != NULL); |
| 722 | if (desc->action != DO_UNSPEC) |
| 723 | free(desc->action_arg); |
| 724 | desc->action = DO_PACK; |
| 725 | desc->action_arg = xstrdup(optarg, |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 726 | "failed to allocate memory for argument"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 727 | break; |
| 728 | } |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 729 | case OPT_PLAT_TOC_FLAGS: |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 730 | parse_plat_toc_flags(optarg, &toc_flags); |
| 731 | pflag = 1; |
| 732 | break; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 733 | case 'o': |
| 734 | snprintf(outfile, sizeof(outfile), "%s", optarg); |
| 735 | break; |
| 736 | default: |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 737 | update_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | argc -= optind; |
| 741 | argv += optind; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 742 | free(opts); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 743 | |
| 744 | if (argc == 0) |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 745 | update_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 746 | |
| 747 | if (outfile[0] == '\0') |
| 748 | snprintf(outfile, sizeof(outfile), "%s", argv[0]); |
| 749 | |
| 750 | if (access(outfile, F_OK) == 0) |
| 751 | parse_fip(argv[0], &toc_header); |
| 752 | |
| 753 | if (pflag) |
| 754 | toc_header.flags &= ~(0xffffULL << 32); |
| 755 | toc_flags = (toc_header.flags |= toc_flags); |
| 756 | |
| 757 | update_fip(); |
| 758 | |
| 759 | pack_images(outfile, toc_flags); |
| 760 | free_images(); |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | static void update_usage(void) |
| 765 | { |
| 766 | toc_entry_t *toc_entry = toc_entries; |
| 767 | |
dp-arm | 1b434b0 | 2016-08-23 14:31:41 +0100 | [diff] [blame] | 768 | printf("fiptool update [--out FIP_FILENAME] " |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 769 | "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n"); |
| 770 | printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n"); |
| 771 | printf(" --plat-toc-flags <value>\t16-bit platform specific flag field " |
| 772 | "occupying bits 32-47 in 64-bit ToC header.\n"); |
| 773 | fputc('\n', stderr); |
| 774 | printf("Specific images are packed with the following options:\n"); |
| 775 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 776 | printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, |
| 777 | toc_entry->name); |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 778 | exit(1); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static int unpack_cmd(int argc, char *argv[]) |
| 782 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 783 | struct option *opts = NULL; |
| 784 | size_t nr_opts = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 785 | char file[FILENAME_MAX], outdir[PATH_MAX] = { 0 }; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 786 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 787 | int fflag = 0; |
| 788 | int unpack_all = 1; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 789 | |
| 790 | if (argc < 2) |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 791 | unpack_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 792 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 793 | opts = fill_common_opts(opts, &nr_opts, required_argument); |
| 794 | opts = add_opt(opts, &nr_opts, "force", no_argument, 'f'); |
| 795 | opts = add_opt(opts, &nr_opts, "out", required_argument, 'o'); |
| 796 | opts = add_opt(opts, &nr_opts, NULL, 0, 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 797 | |
| 798 | while (1) { |
dp-arm | fe92b89 | 2016-11-07 11:13:54 +0000 | [diff] [blame] | 799 | int c, opt_index = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 800 | |
| 801 | c = getopt_long(argc, argv, "fo:", opts, &opt_index); |
| 802 | if (c == -1) |
| 803 | break; |
| 804 | |
| 805 | switch (c) { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 806 | case OPT_TOC_ENTRY: { |
| 807 | image_desc_t *desc; |
| 808 | |
| 809 | desc = lookup_image_desc_from_opt(opts[opt_index].name); |
| 810 | assert(desc != NULL); |
| 811 | if (desc->action != DO_UNSPEC) |
| 812 | free(desc->action_arg); |
| 813 | desc->action = DO_UNPACK; |
| 814 | desc->action_arg = xstrdup(optarg, |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 815 | "failed to allocate memory for argument"); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 816 | unpack_all = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 817 | break; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 818 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 819 | case 'f': |
| 820 | fflag = 1; |
| 821 | break; |
| 822 | case 'o': |
| 823 | snprintf(outdir, sizeof(outdir), "%s", optarg); |
| 824 | break; |
| 825 | default: |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 826 | unpack_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | argc -= optind; |
| 830 | argv += optind; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 831 | free(opts); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 832 | |
| 833 | if (argc == 0) |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 834 | unpack_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 835 | |
| 836 | parse_fip(argv[0], NULL); |
| 837 | |
| 838 | if (outdir[0] != '\0') |
| 839 | if (chdir(outdir) == -1) |
| 840 | log_err("chdir %s", outdir); |
| 841 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 842 | /* Unpack all specified images. */ |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 843 | for (desc = image_desc_head; desc != NULL; desc = desc->next) { |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 844 | image_t *image; |
| 845 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 846 | if (!unpack_all && desc->action != DO_UNPACK) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 847 | continue; |
| 848 | |
| 849 | /* Build filename. */ |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 850 | if (desc->action_arg == NULL) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 851 | snprintf(file, sizeof(file), "%s.bin", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 852 | desc->cmdline_name); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 853 | else |
| 854 | snprintf(file, sizeof(file), "%s", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 855 | desc->action_arg); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 856 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 857 | image = lookup_image_from_uuid(&desc->uuid); |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 858 | if (image == NULL) { |
| 859 | if (!unpack_all) |
| 860 | log_warnx("Requested image %s is not in %s", |
| 861 | file, argv[0]); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 862 | continue; |
| 863 | } |
| 864 | |
| 865 | if (access(file, F_OK) != 0 || fflag) { |
| 866 | if (verbose) |
| 867 | log_dbgx("Unpacking %s", file); |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 868 | write_image_to_file(image, file); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 869 | } else { |
| 870 | log_warnx("File %s already exists, use --force to overwrite it", |
| 871 | file); |
| 872 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | free_images(); |
| 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | static void unpack_usage(void) |
| 880 | { |
| 881 | toc_entry_t *toc_entry = toc_entries; |
| 882 | |
| 883 | printf("fiptool unpack [--force] [--out <path>] [opts] FIP_FILENAME\n"); |
| 884 | printf(" --force\tIf the output file already exists, use --force to " |
| 885 | "overwrite it.\n"); |
| 886 | printf(" --out path\tSet the output directory path.\n"); |
| 887 | fputc('\n', stderr); |
| 888 | printf("Specific images are unpacked with the following options:\n"); |
| 889 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 890 | printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, |
| 891 | toc_entry->name); |
| 892 | fputc('\n', stderr); |
| 893 | printf("If no options are provided, all images will be unpacked.\n"); |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 894 | exit(1); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static int remove_cmd(int argc, char *argv[]) |
| 898 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 899 | struct option *opts = NULL; |
| 900 | size_t nr_opts = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 901 | char outfile[FILENAME_MAX] = { 0 }; |
| 902 | fip_toc_header_t toc_header; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 903 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 904 | int fflag = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 905 | |
| 906 | if (argc < 2) |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 907 | remove_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 908 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 909 | opts = fill_common_opts(opts, &nr_opts, no_argument); |
| 910 | opts = add_opt(opts, &nr_opts, "force", no_argument, 'f'); |
| 911 | opts = add_opt(opts, &nr_opts, "out", required_argument, 'o'); |
| 912 | opts = add_opt(opts, &nr_opts, NULL, 0, 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 913 | |
| 914 | while (1) { |
dp-arm | fe92b89 | 2016-11-07 11:13:54 +0000 | [diff] [blame] | 915 | int c, opt_index = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 916 | |
| 917 | c = getopt_long(argc, argv, "fo:", opts, &opt_index); |
| 918 | if (c == -1) |
| 919 | break; |
| 920 | |
| 921 | switch (c) { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 922 | case OPT_TOC_ENTRY: { |
| 923 | image_desc_t *desc; |
| 924 | |
| 925 | desc = lookup_image_desc_from_opt(opts[opt_index].name); |
| 926 | assert(desc != NULL); |
| 927 | desc->action = DO_REMOVE; |
| 928 | desc->action_arg = NULL; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 929 | break; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 930 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 931 | case 'f': |
| 932 | fflag = 1; |
| 933 | break; |
| 934 | case 'o': |
| 935 | snprintf(outfile, sizeof(outfile), "%s", optarg); |
| 936 | break; |
| 937 | default: |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 938 | remove_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | argc -= optind; |
| 942 | argv += optind; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 943 | free(opts); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 944 | |
| 945 | if (argc == 0) |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 946 | remove_usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 947 | |
| 948 | if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag) |
| 949 | log_errx("File %s already exists, use --force to overwrite it", |
| 950 | outfile); |
| 951 | |
| 952 | if (outfile[0] == '\0') |
| 953 | snprintf(outfile, sizeof(outfile), "%s", argv[0]); |
| 954 | |
| 955 | parse_fip(argv[0], &toc_header); |
| 956 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 957 | for (desc = image_desc_head; desc != NULL; desc = desc->next) { |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 958 | image_t *image; |
| 959 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 960 | if (desc->action != DO_REMOVE) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 961 | continue; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 962 | image = lookup_image_from_uuid(&desc->uuid); |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 963 | if (image != NULL) { |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 964 | if (verbose) |
| 965 | log_dbgx("Removing %s.bin", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 966 | desc->cmdline_name); |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 967 | remove_image(image); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 968 | } else { |
| 969 | log_warnx("Requested image %s.bin is not in %s", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 970 | desc->cmdline_name, argv[0]); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
| 974 | pack_images(outfile, toc_header.flags); |
| 975 | free_images(); |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | static void remove_usage(void) |
| 980 | { |
| 981 | toc_entry_t *toc_entry = toc_entries; |
| 982 | |
| 983 | printf("fiptool remove [--force] [--out FIP_FILENAME] [opts] FIP_FILENAME\n"); |
| 984 | printf(" --force\t\tIf the output FIP file already exists, use --force to " |
| 985 | "overwrite it.\n"); |
| 986 | printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n"); |
| 987 | fputc('\n', stderr); |
| 988 | printf("Specific images are removed with the following options:\n"); |
| 989 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 990 | printf(" --%-16s\t%s\n", toc_entry->cmdline_name, |
| 991 | toc_entry->name); |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 992 | exit(1); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | static int version_cmd(int argc, char *argv[]) |
| 996 | { |
| 997 | #ifdef VERSION |
| 998 | puts(VERSION); |
| 999 | #else |
| 1000 | /* If built from fiptool directory, VERSION is not set. */ |
| 1001 | puts("Unknown version"); |
| 1002 | #endif |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | static void version_usage(void) |
| 1007 | { |
| 1008 | printf("fiptool version\n"); |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 1009 | exit(1); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | static int help_cmd(int argc, char *argv[]) |
| 1013 | { |
| 1014 | int i; |
| 1015 | |
| 1016 | if (argc < 2) |
| 1017 | usage(); |
| 1018 | argc--, argv++; |
| 1019 | |
| 1020 | for (i = 0; i < NELEM(cmds); i++) { |
| 1021 | if (strcmp(cmds[i].name, argv[0]) == 0 && |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 1022 | cmds[i].usage != NULL) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1023 | cmds[i].usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1024 | } |
| 1025 | if (i == NELEM(cmds)) |
| 1026 | printf("No help for subcommand '%s'\n", argv[0]); |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | static void usage(void) |
| 1031 | { |
| 1032 | printf("usage: [--verbose] fiptool <command> [<args>]\n"); |
| 1033 | printf("Global options supported:\n"); |
| 1034 | printf(" --verbose\tEnable verbose output for all commands.\n"); |
| 1035 | fputc('\n', stderr); |
| 1036 | printf("Commands supported:\n"); |
| 1037 | printf(" info\t\tList images contained in FIP.\n"); |
| 1038 | printf(" create\tCreate a new FIP with the given images.\n"); |
| 1039 | printf(" update\tUpdate an existing FIP with the given images.\n"); |
| 1040 | printf(" unpack\tUnpack images from FIP.\n"); |
| 1041 | printf(" remove\tRemove images from FIP.\n"); |
| 1042 | printf(" version\tShow fiptool version.\n"); |
| 1043 | printf(" help\t\tShow help for given command.\n"); |
| 1044 | exit(1); |
| 1045 | } |
| 1046 | |
| 1047 | int main(int argc, char *argv[]) |
| 1048 | { |
| 1049 | int i, ret = 0; |
| 1050 | |
dp-arm | 5cd10ae | 2016-11-07 10:45:59 +0000 | [diff] [blame] | 1051 | while (1) { |
| 1052 | int c, opt_index = 0; |
| 1053 | static struct option opts[] = { |
| 1054 | { "verbose", no_argument, NULL, 'v' }, |
| 1055 | { NULL, no_argument, NULL, 0 } |
| 1056 | }; |
| 1057 | |
| 1058 | /* |
| 1059 | * Set POSIX mode so getopt stops at the first non-option |
| 1060 | * which is the subcommand. |
| 1061 | */ |
| 1062 | c = getopt_long(argc, argv, "+v", opts, &opt_index); |
| 1063 | if (c == -1) |
| 1064 | break; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1065 | |
dp-arm | 5cd10ae | 2016-11-07 10:45:59 +0000 | [diff] [blame] | 1066 | switch (c) { |
| 1067 | case 'v': |
| 1068 | verbose = 1; |
| 1069 | break; |
| 1070 | default: |
Masahiro Yamada | 48a2497 | 2016-10-26 13:24:26 +0900 | [diff] [blame] | 1071 | usage(); |
dp-arm | 5cd10ae | 2016-11-07 10:45:59 +0000 | [diff] [blame] | 1072 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1073 | } |
dp-arm | 5cd10ae | 2016-11-07 10:45:59 +0000 | [diff] [blame] | 1074 | argc -= optind; |
| 1075 | argv += optind; |
| 1076 | /* Reset optind for subsequent getopt processing. */ |
| 1077 | optind = 0; |
| 1078 | |
| 1079 | if (argc == 0) |
| 1080 | usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1081 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 1082 | fill_image_descs(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1083 | for (i = 0; i < NELEM(cmds); i++) { |
| 1084 | if (strcmp(cmds[i].name, argv[0]) == 0) { |
| 1085 | ret = cmds[i].handler(argc, argv); |
| 1086 | break; |
| 1087 | } |
| 1088 | } |
| 1089 | if (i == NELEM(cmds)) |
| 1090 | usage(); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame^] | 1091 | free_image_descs(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1092 | return ret; |
| 1093 | } |