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 45 #include <openssl/sha.h> 46 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 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); 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 82 static image_desc_t *image_desc_head; 83 static size_t nr_image_descs; 84 static image_t *image_head; 85 static size_t nr_images; 86 static uuid_t uuid_null = { 0 }; 87 static int verbose; 88 89 static void vlog(int prio, const char *msg, va_list ap) 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 98 static void log_dbgx(const char *msg, ...) 99 { 100 va_list ap; 101 102 va_start(ap, msg); 103 vlog(LOG_DBG, msg, ap); 104 va_end(ap); 105 } 106 107 static void log_warnx(const char *msg, ...) 108 { 109 va_list ap; 110 111 va_start(ap, msg); 112 vlog(LOG_WARN, msg, ap); 113 va_end(ap); 114 } 115 116 static void log_err(const char *msg, ...) 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 128 static void log_errx(const char *msg, ...) 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 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: %s", 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: %s", msg); 155 return d; 156 } 157 158 static void *xzalloc(size_t size, const char *msg) 159 { 160 return memset(xmalloc(size, msg), 0, size); 161 } 162 163 static image_desc_t *new_image_desc(const uuid_t *uuid, 164 const char *name, const char *cmdline_name) 165 { 166 image_desc_t *desc; 167 168 desc = xzalloc(sizeof(*desc), 169 "failed to allocate memory for image descriptor"); 170 memcpy(&desc->uuid, uuid, sizeof(uuid_t)); 171 desc->name = xstrdup(name, 172 "failed to allocate memory for image name"); 173 desc->cmdline_name = xstrdup(cmdline_name, 174 "failed to allocate memory for image command line name"); 175 desc->action = DO_UNSPEC; 176 return desc; 177 } 178 179 static void set_image_desc_action(image_desc_t *desc, int action, 180 const char *arg) 181 { 182 assert(desc != NULL); 183 184 if (desc->action_arg != DO_UNSPEC) 185 free(desc->action_arg); 186 desc->action = action; 187 desc->action_arg = NULL; 188 if (arg != NULL) 189 desc->action_arg = xstrdup(arg, 190 "failed to allocate memory for argument"); 191 } 192 193 static void free_image_desc(image_desc_t *desc) 194 { 195 free(desc->name); 196 free(desc->cmdline_name); 197 free(desc->action_arg); 198 free(desc); 199 } 200 201 static void add_image_desc(image_desc_t *desc) 202 { 203 image_desc_t **p = &image_desc_head; 204 205 while (*p) 206 p = &(*p)->next; 207 208 assert(*p == NULL); 209 *p = desc; 210 nr_image_descs++; 211 } 212 213 static void free_image_descs(void) 214 { 215 image_desc_t *desc = image_desc_head, *tmp; 216 217 while (desc != NULL) { 218 tmp = desc->next; 219 free_image_desc(desc); 220 desc = tmp; 221 nr_image_descs--; 222 } 223 assert(nr_image_descs == 0); 224 } 225 226 static void fill_image_descs(void) 227 { 228 toc_entry_t *toc_entry; 229 230 for (toc_entry = toc_entries; 231 toc_entry->cmdline_name != NULL; 232 toc_entry++) { 233 image_desc_t *desc; 234 235 desc = new_image_desc(&toc_entry->uuid, 236 toc_entry->name, 237 toc_entry->cmdline_name); 238 add_image_desc(desc); 239 } 240 } 241 242 static void add_image(image_t *image) 243 { 244 image_t **p = &image_head; 245 246 while (*p) 247 p = &(*p)->next; 248 249 assert(*p == NULL); 250 *p = image; 251 252 nr_images++; 253 } 254 255 static void replace_image(image_t *image) 256 { 257 image_t **p = &image_head; 258 259 while (*p) { 260 if (!memcmp(&(*p)->uuid, &image->uuid, sizeof(image->uuid))) 261 break; 262 p = &(*p)->next; 263 } 264 265 assert(*p != NULL); 266 267 image->next = (*p)->next; 268 *p = image; 269 } 270 271 static void free_image(image_t *image) 272 { 273 free(image->buffer); 274 free(image); 275 } 276 277 static void remove_image(image_t *image) 278 { 279 image_t *tmp, **p = &image_head; 280 281 while (*p) { 282 if (*p == image) 283 break; 284 p = &(*p)->next; 285 } 286 287 assert(*p != NULL); 288 289 tmp = *p; 290 *p = tmp->next; 291 free_image(tmp); 292 293 nr_images--; 294 } 295 296 static void free_images(void) 297 { 298 image_t *image = image_head, *tmp; 299 300 while (image != NULL) { 301 tmp = image->next; 302 free_image(image); 303 image = tmp; 304 nr_images--; 305 } 306 assert(nr_images == 0); 307 } 308 309 static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid) 310 { 311 image_desc_t *desc; 312 313 for (desc = image_desc_head; desc != NULL; desc = desc->next) 314 if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0) 315 return desc; 316 return NULL; 317 } 318 319 static image_desc_t *lookup_image_desc_from_opt(const char *opt) 320 { 321 image_desc_t *desc; 322 323 for (desc = image_desc_head; desc != NULL; desc = desc->next) 324 if (strcmp(desc->cmdline_name, opt) == 0) 325 return desc; 326 return NULL; 327 } 328 329 static image_t *lookup_image_from_uuid(const uuid_t *uuid) 330 { 331 image_t *image; 332 333 for (image = image_head; image != NULL; image = image->next) 334 if (memcmp(&image->uuid, uuid, sizeof(uuid_t)) == 0) 335 return image; 336 return NULL; 337 } 338 339 static void uuid_to_str(char *s, size_t len, const uuid_t *u) 340 { 341 assert(len >= (_UUID_STR_LEN + 1)); 342 343 snprintf(s, len, "%08X-%04X-%04X-%04X-%04X%04X%04X", 344 u->time_low, 345 u->time_mid, 346 u->time_hi_and_version, 347 ((uint16_t)u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low, 348 ((uint16_t)u->node[0] << 8) | u->node[1], 349 ((uint16_t)u->node[2] << 8) | u->node[3], 350 ((uint16_t)u->node[4] << 8) | u->node[5]); 351 } 352 353 static void uuid_from_str(uuid_t *u, const char *s) 354 { 355 int n; 356 357 if (s == NULL) 358 log_errx("UUID cannot be NULL"); 359 if (strlen(s) != _UUID_STR_LEN) 360 log_errx("Invalid UUID: %s", s); 361 362 n = sscanf(s, 363 "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", 364 &u->time_low, &u->time_mid, &u->time_hi_and_version, 365 &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0], 366 &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]); 367 /* 368 * Given the format specifier above, we expect 11 items to be scanned 369 * for a properly formatted UUID. 370 */ 371 if (n != 11) 372 log_errx("Invalid UUID: %s", s); 373 } 374 375 static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) 376 { 377 struct stat st; 378 FILE *fp; 379 char *buf, *bufend; 380 fip_toc_header_t *toc_header; 381 fip_toc_entry_t *toc_entry; 382 int terminated = 0; 383 384 fp = fopen(filename, "r"); 385 if (fp == NULL) 386 log_err("fopen %s", filename); 387 388 if (fstat(fileno(fp), &st) == -1) 389 log_err("fstat %s", filename); 390 391 buf = xmalloc(st.st_size, "failed to load file into memory"); 392 if (fread(buf, 1, st.st_size, fp) != st.st_size) 393 log_errx("Failed to read %s", filename); 394 bufend = buf + st.st_size; 395 fclose(fp); 396 397 if (st.st_size < sizeof(fip_toc_header_t)) 398 log_errx("FIP %s is truncated", filename); 399 400 toc_header = (fip_toc_header_t *)buf; 401 toc_entry = (fip_toc_entry_t *)(toc_header + 1); 402 403 if (toc_header->name != TOC_HEADER_NAME) 404 log_errx("%s is not a FIP file", filename); 405 406 /* Return the ToC header if the caller wants it. */ 407 if (toc_header_out != NULL) 408 *toc_header_out = *toc_header; 409 410 /* Walk through each ToC entry in the file. */ 411 while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) { 412 image_t *image; 413 image_desc_t *desc; 414 415 /* Found the ToC terminator, we are done. */ 416 if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) { 417 terminated = 1; 418 break; 419 } 420 421 /* 422 * Build a new image out of the ToC entry and add it to the 423 * table of images. 424 */ 425 image = xzalloc(sizeof(*image), 426 "failed to allocate memory for image"); 427 memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t)); 428 image->buffer = xmalloc(toc_entry->size, 429 "failed to allocate image buffer, is FIP file corrupted?"); 430 /* Overflow checks before memory copy. */ 431 if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address) 432 log_errx("FIP %s is corrupted", filename); 433 if (toc_entry->size + toc_entry->offset_address > st.st_size) 434 log_errx("FIP %s is corrupted", filename); 435 436 memcpy(image->buffer, buf + toc_entry->offset_address, 437 toc_entry->size); 438 image->size = toc_entry->size; 439 440 /* If this is an unknown image, create a descriptor for it. */ 441 desc = lookup_image_desc_from_uuid(&image->uuid); 442 if (desc == NULL) { 443 char name[_UUID_STR_LEN + 1], filename[PATH_MAX]; 444 445 uuid_to_str(name, sizeof(name), &image->uuid); 446 snprintf(filename, sizeof(filename), "%s%s", 447 name, ".bin"); 448 desc = new_image_desc(&image->uuid, name, "blob"); 449 desc->action = DO_UNPACK; 450 desc->action_arg = xstrdup(filename, 451 "failed to allocate memory for blob filename"); 452 add_image_desc(desc); 453 } 454 455 add_image(image); 456 457 toc_entry++; 458 } 459 460 if (terminated == 0) 461 log_errx("FIP %s does not have a ToC terminator entry", 462 filename); 463 free(buf); 464 return 0; 465 } 466 467 static image_t *read_image_from_file(const uuid_t *uuid, const char *filename) 468 { 469 struct stat st; 470 image_t *image; 471 FILE *fp; 472 473 assert(uuid != NULL); 474 475 fp = fopen(filename, "r"); 476 if (fp == NULL) 477 log_err("fopen %s", filename); 478 479 if (fstat(fileno(fp), &st) == -1) 480 log_errx("fstat %s", filename); 481 482 image = xzalloc(sizeof(*image), "failed to allocate memory for image"); 483 memcpy(&image->uuid, uuid, sizeof(uuid_t)); 484 image->buffer = xmalloc(st.st_size, "failed to allocate image buffer"); 485 if (fread(image->buffer, 1, st.st_size, fp) != st.st_size) 486 log_errx("Failed to read %s", filename); 487 image->size = st.st_size; 488 489 fclose(fp); 490 return image; 491 } 492 493 static int write_image_to_file(const image_t *image, const char *filename) 494 { 495 FILE *fp; 496 497 fp = fopen(filename, "w"); 498 if (fp == NULL) 499 log_err("fopen"); 500 if (fwrite(image->buffer, 1, image->size, fp) != image->size) 501 log_errx("Failed to write %s", filename); 502 fclose(fp); 503 return 0; 504 } 505 506 static struct option *add_opt(struct option *opts, size_t *nr_opts, 507 const char *name, int has_arg, int val) 508 { 509 opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts)); 510 if (opts == NULL) 511 log_err("realloc"); 512 opts[*nr_opts].name = name; 513 opts[*nr_opts].has_arg = has_arg; 514 opts[*nr_opts].flag = NULL; 515 opts[*nr_opts].val = val; 516 ++*nr_opts; 517 return opts; 518 } 519 520 static struct option *fill_common_opts(struct option *opts, size_t *nr_opts, 521 int has_arg) 522 { 523 image_desc_t *desc; 524 525 for (desc = image_desc_head; desc != NULL; desc = desc->next) 526 opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg, 527 OPT_TOC_ENTRY); 528 return opts; 529 } 530 531 static void md_print(const unsigned char *md, size_t len) 532 { 533 size_t i; 534 535 for (i = 0; i < len; i++) 536 printf("%02x", md[i]); 537 } 538 539 static int info_cmd(int argc, char *argv[]) 540 { 541 image_t *image; 542 uint64_t image_offset; 543 uint64_t image_size; 544 fip_toc_header_t toc_header; 545 546 if (argc != 2) 547 info_usage(); 548 argc--, argv++; 549 550 parse_fip(argv[0], &toc_header); 551 552 if (verbose) { 553 log_dbgx("toc_header[name]: 0x%llX", 554 (unsigned long long)toc_header.name); 555 log_dbgx("toc_header[serial_number]: 0x%llX", 556 (unsigned long long)toc_header.serial_number); 557 log_dbgx("toc_header[flags]: 0x%llX", 558 (unsigned long long)toc_header.flags); 559 } 560 561 image_offset = sizeof(fip_toc_header_t) + 562 (sizeof(fip_toc_entry_t) * (nr_images + 1)); 563 564 for (image = image_head; image != NULL; image = image->next) { 565 image_desc_t *desc; 566 567 desc = lookup_image_desc_from_uuid(&image->uuid); 568 assert(desc != NULL); 569 printf("%s: ", desc->name); 570 image_size = image->size; 571 printf("offset=0x%llX, size=0x%llX, cmdline=\"--%s\"", 572 (unsigned long long)image_offset, 573 (unsigned long long)image_size, 574 desc->cmdline_name); 575 if (verbose) { 576 unsigned char md[SHA256_DIGEST_LENGTH]; 577 578 SHA256(image->buffer, image_size, md); 579 printf(", sha256="); 580 md_print(md, sizeof(md)); 581 } 582 putchar('\n'); 583 image_offset += image_size; 584 } 585 586 free_images(); 587 return 0; 588 } 589 590 static void info_usage(void) 591 { 592 printf("fiptool info FIP_FILENAME\n"); 593 exit(1); 594 } 595 596 static int pack_images(const char *filename, uint64_t toc_flags) 597 { 598 FILE *fp; 599 image_t *image; 600 fip_toc_header_t *toc_header; 601 fip_toc_entry_t *toc_entry; 602 char *buf; 603 uint64_t entry_offset, buf_size, payload_size; 604 605 /* Calculate total payload size and allocate scratch buffer. */ 606 payload_size = 0; 607 for (image = image_head; image != NULL; image = image->next) 608 payload_size += image->size; 609 610 buf_size = sizeof(fip_toc_header_t) + 611 sizeof(fip_toc_entry_t) * (nr_images + 1); 612 buf = calloc(1, buf_size); 613 if (buf == NULL) 614 log_err("calloc"); 615 616 /* Build up header and ToC entries from the image table. */ 617 toc_header = (fip_toc_header_t *)buf; 618 toc_header->name = TOC_HEADER_NAME; 619 toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER; 620 toc_header->flags = toc_flags; 621 622 toc_entry = (fip_toc_entry_t *)(toc_header + 1); 623 624 entry_offset = buf_size; 625 for (image = image_head; image != NULL; image = image->next) { 626 memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t)); 627 toc_entry->offset_address = entry_offset; 628 toc_entry->size = image->size; 629 toc_entry->flags = 0; 630 entry_offset += toc_entry->size; 631 toc_entry++; 632 } 633 634 /* Append a null uuid entry to mark the end of ToC entries. */ 635 memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)); 636 toc_entry->offset_address = entry_offset; 637 toc_entry->size = 0; 638 toc_entry->flags = 0; 639 640 /* Generate the FIP file. */ 641 fp = fopen(filename, "w"); 642 if (fp == NULL) 643 log_err("fopen %s", filename); 644 645 if (verbose) 646 log_dbgx("Metadata size: %zu bytes", buf_size); 647 648 if (fwrite(buf, 1, buf_size, fp) != buf_size) 649 log_errx("Failed to write image to %s", filename); 650 free(buf); 651 652 if (verbose) 653 log_dbgx("Payload size: %zu bytes", payload_size); 654 655 for (image = image_head; image != NULL; image = image->next) 656 if (fwrite(image->buffer, 1, image->size, fp) != image->size) 657 log_errx("Failed to write image to %s", filename); 658 659 fclose(fp); 660 return 0; 661 } 662 663 /* 664 * This function is shared between the create and update subcommands. 665 * The difference between the two subcommands is that when the FIP file 666 * is created, the parsing of an existing FIP is skipped. This results 667 * in update_fip() creating the new FIP file from scratch because the 668 * internal image table is not populated. 669 */ 670 static void update_fip(void) 671 { 672 image_desc_t *desc; 673 674 /* Add or replace images in the FIP file. */ 675 for (desc = image_desc_head; desc != NULL; desc = desc->next) { 676 image_t *new_image, *old_image; 677 678 if (desc->action != DO_PACK) 679 continue; 680 681 new_image = read_image_from_file(&desc->uuid, 682 desc->action_arg); 683 old_image = lookup_image_from_uuid(&desc->uuid); 684 if (old_image != NULL) { 685 if (verbose) { 686 log_dbgx("Replacing %s with %s", 687 desc->cmdline_name, 688 desc->action_arg); 689 } 690 replace_image(new_image); 691 } else { 692 if (verbose) 693 log_dbgx("Adding image %s", 694 desc->action_arg); 695 add_image(new_image); 696 } 697 } 698 } 699 700 static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags) 701 { 702 unsigned long long flags; 703 char *endptr; 704 705 errno = 0; 706 flags = strtoull(arg, &endptr, 16); 707 if (*endptr != '\0' || flags > UINT16_MAX || errno != 0) 708 log_errx("Invalid platform ToC flags: %s", arg); 709 /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */ 710 *toc_flags |= flags << 32; 711 } 712 713 static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len) 714 { 715 char *p; 716 717 for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) { 718 if (strncmp(p, "uuid=", strlen("uuid=")) == 0) { 719 p += strlen("uuid="); 720 uuid_from_str(uuid, p); 721 } else if (strncmp(p, "file=", strlen("file=")) == 0) { 722 p += strlen("file="); 723 snprintf(filename, len, "%s", p); 724 } 725 } 726 } 727 728 static int create_cmd(int argc, char *argv[]) 729 { 730 struct option *opts = NULL; 731 size_t nr_opts = 0; 732 unsigned long long toc_flags = 0; 733 734 if (argc < 2) 735 create_usage(); 736 737 opts = fill_common_opts(opts, &nr_opts, required_argument); 738 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument, 739 OPT_PLAT_TOC_FLAGS); 740 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b'); 741 opts = add_opt(opts, &nr_opts, NULL, 0, 0); 742 743 while (1) { 744 int c, opt_index = 0; 745 746 c = getopt_long(argc, argv, "b:", opts, &opt_index); 747 if (c == -1) 748 break; 749 750 switch (c) { 751 case OPT_TOC_ENTRY: { 752 image_desc_t *desc; 753 754 desc = lookup_image_desc_from_opt(opts[opt_index].name); 755 set_image_desc_action(desc, DO_PACK, optarg); 756 break; 757 } 758 case OPT_PLAT_TOC_FLAGS: 759 parse_plat_toc_flags(optarg, &toc_flags); 760 break; 761 case 'b': { 762 char name[_UUID_STR_LEN + 1]; 763 char filename[PATH_MAX] = { 0 }; 764 uuid_t uuid = { 0 }; 765 image_desc_t *desc; 766 767 parse_blob_opt(optarg, &uuid, 768 filename, sizeof(filename)); 769 770 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 || 771 filename[0] == '\0') 772 create_usage(); 773 774 desc = lookup_image_desc_from_uuid(&uuid); 775 if (desc == NULL) { 776 uuid_to_str(name, sizeof(name), &uuid); 777 desc = new_image_desc(&uuid, name, "blob"); 778 add_image_desc(desc); 779 } 780 set_image_desc_action(desc, DO_PACK, filename); 781 break; 782 } 783 default: 784 create_usage(); 785 } 786 } 787 argc -= optind; 788 argv += optind; 789 free(opts); 790 791 if (argc == 0) 792 create_usage(); 793 794 update_fip(); 795 796 pack_images(argv[0], toc_flags); 797 free_images(); 798 return 0; 799 } 800 801 static void create_usage(void) 802 { 803 toc_entry_t *toc_entry = toc_entries; 804 805 printf("fiptool create [opts] FIP_FILENAME\n"); 806 printf("\n"); 807 printf("Options:\n"); 808 printf(" --blob uuid=...,file=...\tAdd an image with the given UUID " 809 "pointed to by file.\n"); 810 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field " 811 "occupying bits 32-47 in 64-bit ToC header.\n"); 812 fputc('\n', stderr); 813 printf("Specific images are packed with the following options:\n"); 814 for (; toc_entry->cmdline_name != NULL; toc_entry++) 815 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, 816 toc_entry->name); 817 exit(1); 818 } 819 820 static int update_cmd(int argc, char *argv[]) 821 { 822 struct option *opts = NULL; 823 size_t nr_opts = 0; 824 char outfile[PATH_MAX] = { 0 }; 825 fip_toc_header_t toc_header = { 0 }; 826 unsigned long long toc_flags = 0; 827 int pflag = 0; 828 829 if (argc < 2) 830 update_usage(); 831 832 opts = fill_common_opts(opts, &nr_opts, required_argument); 833 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b'); 834 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o'); 835 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument, 836 OPT_PLAT_TOC_FLAGS); 837 opts = add_opt(opts, &nr_opts, NULL, 0, 0); 838 839 while (1) { 840 int c, opt_index = 0; 841 842 c = getopt_long(argc, argv, "b:o:", opts, &opt_index); 843 if (c == -1) 844 break; 845 846 switch (c) { 847 case OPT_TOC_ENTRY: { 848 image_desc_t *desc; 849 850 desc = lookup_image_desc_from_opt(opts[opt_index].name); 851 set_image_desc_action(desc, DO_PACK, optarg); 852 break; 853 } 854 case OPT_PLAT_TOC_FLAGS: 855 parse_plat_toc_flags(optarg, &toc_flags); 856 pflag = 1; 857 break; 858 case 'b': { 859 char name[_UUID_STR_LEN + 1]; 860 char filename[PATH_MAX] = { 0 }; 861 uuid_t uuid = { 0 }; 862 image_desc_t *desc; 863 864 parse_blob_opt(optarg, &uuid, 865 filename, sizeof(filename)); 866 867 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 || 868 filename[0] == '\0') 869 update_usage(); 870 871 desc = lookup_image_desc_from_uuid(&uuid); 872 if (desc == NULL) { 873 uuid_to_str(name, sizeof(name), &uuid); 874 desc = new_image_desc(&uuid, name, "blob"); 875 add_image_desc(desc); 876 } 877 set_image_desc_action(desc, DO_PACK, filename); 878 break; 879 } 880 case 'o': 881 snprintf(outfile, sizeof(outfile), "%s", optarg); 882 break; 883 default: 884 update_usage(); 885 } 886 } 887 argc -= optind; 888 argv += optind; 889 free(opts); 890 891 if (argc == 0) 892 update_usage(); 893 894 if (outfile[0] == '\0') 895 snprintf(outfile, sizeof(outfile), "%s", argv[0]); 896 897 if (access(argv[0], F_OK) == 0) 898 parse_fip(argv[0], &toc_header); 899 900 if (pflag) 901 toc_header.flags &= ~(0xffffULL << 32); 902 toc_flags = (toc_header.flags |= toc_flags); 903 904 update_fip(); 905 906 pack_images(outfile, toc_flags); 907 free_images(); 908 return 0; 909 } 910 911 static void update_usage(void) 912 { 913 toc_entry_t *toc_entry = toc_entries; 914 915 printf("fiptool update [opts] FIP_FILENAME\n"); 916 printf("\n"); 917 printf("Options:\n"); 918 printf(" --blob uuid=...,file=...\tAdd or update an image " 919 "with the given UUID pointed to by file.\n"); 920 printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n"); 921 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field " 922 "occupying bits 32-47 in 64-bit ToC header.\n"); 923 fputc('\n', stderr); 924 printf("Specific images are packed with the following options:\n"); 925 for (; toc_entry->cmdline_name != NULL; toc_entry++) 926 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, 927 toc_entry->name); 928 exit(1); 929 } 930 931 static int unpack_cmd(int argc, char *argv[]) 932 { 933 struct option *opts = NULL; 934 size_t nr_opts = 0; 935 char outdir[PATH_MAX] = { 0 }; 936 image_desc_t *desc; 937 int fflag = 0; 938 int unpack_all = 1; 939 940 if (argc < 2) 941 unpack_usage(); 942 943 opts = fill_common_opts(opts, &nr_opts, required_argument); 944 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b'); 945 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f'); 946 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o'); 947 opts = add_opt(opts, &nr_opts, NULL, 0, 0); 948 949 while (1) { 950 int c, opt_index = 0; 951 952 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index); 953 if (c == -1) 954 break; 955 956 switch (c) { 957 case OPT_TOC_ENTRY: { 958 image_desc_t *desc; 959 960 desc = lookup_image_desc_from_opt(opts[opt_index].name); 961 set_image_desc_action(desc, DO_UNPACK, optarg); 962 unpack_all = 0; 963 break; 964 } 965 case 'b': { 966 char name[_UUID_STR_LEN + 1]; 967 char filename[PATH_MAX] = { 0 }; 968 uuid_t uuid = { 0 }; 969 image_desc_t *desc; 970 971 parse_blob_opt(optarg, &uuid, 972 filename, sizeof(filename)); 973 974 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 || 975 filename[0] == '\0') 976 unpack_usage(); 977 978 desc = lookup_image_desc_from_uuid(&uuid); 979 if (desc == NULL) { 980 uuid_to_str(name, sizeof(name), &uuid); 981 desc = new_image_desc(&uuid, name, "blob"); 982 add_image_desc(desc); 983 } 984 set_image_desc_action(desc, DO_UNPACK, filename); 985 unpack_all = 0; 986 break; 987 } 988 case 'f': 989 fflag = 1; 990 break; 991 case 'o': 992 snprintf(outdir, sizeof(outdir), "%s", optarg); 993 break; 994 default: 995 unpack_usage(); 996 } 997 } 998 argc -= optind; 999 argv += optind; 1000 free(opts); 1001 1002 if (argc == 0) 1003 unpack_usage(); 1004 1005 parse_fip(argv[0], NULL); 1006 1007 if (outdir[0] != '\0') 1008 if (chdir(outdir) == -1) 1009 log_err("chdir %s", outdir); 1010 1011 /* Unpack all specified images. */ 1012 for (desc = image_desc_head; desc != NULL; desc = desc->next) { 1013 char file[PATH_MAX]; 1014 image_t *image; 1015 1016 if (!unpack_all && desc->action != DO_UNPACK) 1017 continue; 1018 1019 /* Build filename. */ 1020 if (desc->action_arg == NULL) 1021 snprintf(file, sizeof(file), "%s.bin", 1022 desc->cmdline_name); 1023 else 1024 snprintf(file, sizeof(file), "%s", 1025 desc->action_arg); 1026 1027 image = lookup_image_from_uuid(&desc->uuid); 1028 if (image == NULL) { 1029 if (!unpack_all) 1030 log_warnx("%s does not exist in %s", 1031 file, argv[0]); 1032 continue; 1033 } 1034 1035 if (access(file, F_OK) != 0 || fflag) { 1036 if (verbose) 1037 log_dbgx("Unpacking %s", file); 1038 write_image_to_file(image, file); 1039 } else { 1040 log_warnx("File %s already exists, use --force to overwrite it", 1041 file); 1042 } 1043 } 1044 1045 free_images(); 1046 return 0; 1047 } 1048 1049 static void unpack_usage(void) 1050 { 1051 toc_entry_t *toc_entry = toc_entries; 1052 1053 printf("fiptool unpack [opts] FIP_FILENAME\n"); 1054 printf("\n"); 1055 printf("Options:\n"); 1056 printf(" --blob uuid=...,file=...\tUnpack an image with the given UUID " 1057 "to file.\n"); 1058 printf(" --force\t\t\tIf the output file already exists, use --force to " 1059 "overwrite it.\n"); 1060 printf(" --out path\t\t\tSet the output directory path.\n"); 1061 fputc('\n', stderr); 1062 printf("Specific images are unpacked with the following options:\n"); 1063 for (; toc_entry->cmdline_name != NULL; toc_entry++) 1064 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, 1065 toc_entry->name); 1066 fputc('\n', stderr); 1067 printf("If no options are provided, all images will be unpacked.\n"); 1068 exit(1); 1069 } 1070 1071 static int remove_cmd(int argc, char *argv[]) 1072 { 1073 struct option *opts = NULL; 1074 size_t nr_opts = 0; 1075 char outfile[PATH_MAX] = { 0 }; 1076 fip_toc_header_t toc_header; 1077 image_desc_t *desc; 1078 int fflag = 0; 1079 1080 if (argc < 2) 1081 remove_usage(); 1082 1083 opts = fill_common_opts(opts, &nr_opts, no_argument); 1084 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b'); 1085 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f'); 1086 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o'); 1087 opts = add_opt(opts, &nr_opts, NULL, 0, 0); 1088 1089 while (1) { 1090 int c, opt_index = 0; 1091 1092 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index); 1093 if (c == -1) 1094 break; 1095 1096 switch (c) { 1097 case OPT_TOC_ENTRY: { 1098 image_desc_t *desc; 1099 1100 desc = lookup_image_desc_from_opt(opts[opt_index].name); 1101 set_image_desc_action(desc, DO_REMOVE, NULL); 1102 break; 1103 } 1104 case 'b': { 1105 char name[_UUID_STR_LEN + 1], filename[PATH_MAX]; 1106 uuid_t uuid = { 0 }; 1107 image_desc_t *desc; 1108 1109 parse_blob_opt(optarg, &uuid, 1110 filename, sizeof(filename)); 1111 1112 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0) 1113 remove_usage(); 1114 1115 desc = lookup_image_desc_from_uuid(&uuid); 1116 if (desc == NULL) { 1117 uuid_to_str(name, sizeof(name), &uuid); 1118 desc = new_image_desc(&uuid, name, "blob"); 1119 add_image_desc(desc); 1120 } 1121 set_image_desc_action(desc, DO_REMOVE, NULL); 1122 break; 1123 } 1124 case 'f': 1125 fflag = 1; 1126 break; 1127 case 'o': 1128 snprintf(outfile, sizeof(outfile), "%s", optarg); 1129 break; 1130 default: 1131 remove_usage(); 1132 } 1133 } 1134 argc -= optind; 1135 argv += optind; 1136 free(opts); 1137 1138 if (argc == 0) 1139 remove_usage(); 1140 1141 if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag) 1142 log_errx("File %s already exists, use --force to overwrite it", 1143 outfile); 1144 1145 if (outfile[0] == '\0') 1146 snprintf(outfile, sizeof(outfile), "%s", argv[0]); 1147 1148 parse_fip(argv[0], &toc_header); 1149 1150 for (desc = image_desc_head; desc != NULL; desc = desc->next) { 1151 image_t *image; 1152 1153 if (desc->action != DO_REMOVE) 1154 continue; 1155 1156 image = lookup_image_from_uuid(&desc->uuid); 1157 if (image != NULL) { 1158 if (verbose) 1159 log_dbgx("Removing %s", 1160 desc->cmdline_name); 1161 remove_image(image); 1162 } else { 1163 log_warnx("%s does not exist in %s", 1164 desc->cmdline_name, argv[0]); 1165 } 1166 } 1167 1168 pack_images(outfile, toc_header.flags); 1169 free_images(); 1170 return 0; 1171 } 1172 1173 static void remove_usage(void) 1174 { 1175 toc_entry_t *toc_entry = toc_entries; 1176 1177 printf("fiptool remove [opts] FIP_FILENAME\n"); 1178 printf("\n"); 1179 printf("Options:\n"); 1180 printf(" --blob uuid=...\tRemove an image with the given UUID.\n"); 1181 printf(" --force\t\tIf the output FIP file already exists, use --force to " 1182 "overwrite it.\n"); 1183 printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n"); 1184 fputc('\n', stderr); 1185 printf("Specific images are removed with the following options:\n"); 1186 for (; toc_entry->cmdline_name != NULL; toc_entry++) 1187 printf(" --%-16s\t%s\n", toc_entry->cmdline_name, 1188 toc_entry->name); 1189 exit(1); 1190 } 1191 1192 static int version_cmd(int argc, char *argv[]) 1193 { 1194 #ifdef VERSION 1195 puts(VERSION); 1196 #else 1197 /* If built from fiptool directory, VERSION is not set. */ 1198 puts("Unknown version"); 1199 #endif 1200 return 0; 1201 } 1202 1203 static void version_usage(void) 1204 { 1205 printf("fiptool version\n"); 1206 exit(1); 1207 } 1208 1209 static int help_cmd(int argc, char *argv[]) 1210 { 1211 int i; 1212 1213 if (argc < 2) 1214 usage(); 1215 argc--, argv++; 1216 1217 for (i = 0; i < NELEM(cmds); i++) { 1218 if (strcmp(cmds[i].name, argv[0]) == 0 && 1219 cmds[i].usage != NULL) 1220 cmds[i].usage(); 1221 } 1222 if (i == NELEM(cmds)) 1223 printf("No help for subcommand '%s'\n", argv[0]); 1224 return 0; 1225 } 1226 1227 static void usage(void) 1228 { 1229 printf("usage: fiptool [--verbose] <command> [<args>]\n"); 1230 printf("Global options supported:\n"); 1231 printf(" --verbose\tEnable verbose output for all commands.\n"); 1232 fputc('\n', stderr); 1233 printf("Commands supported:\n"); 1234 printf(" info\t\tList images contained in FIP.\n"); 1235 printf(" create\tCreate a new FIP with the given images.\n"); 1236 printf(" update\tUpdate an existing FIP with the given images.\n"); 1237 printf(" unpack\tUnpack images from FIP.\n"); 1238 printf(" remove\tRemove images from FIP.\n"); 1239 printf(" version\tShow fiptool version.\n"); 1240 printf(" help\t\tShow help for given command.\n"); 1241 exit(1); 1242 } 1243 1244 int main(int argc, char *argv[]) 1245 { 1246 int i, ret = 0; 1247 1248 while (1) { 1249 int c, opt_index = 0; 1250 static struct option opts[] = { 1251 { "verbose", no_argument, NULL, 'v' }, 1252 { NULL, no_argument, NULL, 0 } 1253 }; 1254 1255 /* 1256 * Set POSIX mode so getopt stops at the first non-option 1257 * which is the subcommand. 1258 */ 1259 c = getopt_long(argc, argv, "+v", opts, &opt_index); 1260 if (c == -1) 1261 break; 1262 1263 switch (c) { 1264 case 'v': 1265 verbose = 1; 1266 break; 1267 default: 1268 usage(); 1269 } 1270 } 1271 argc -= optind; 1272 argv += optind; 1273 /* Reset optind for subsequent getopt processing. */ 1274 optind = 0; 1275 1276 if (argc == 0) 1277 usage(); 1278 1279 fill_image_descs(); 1280 for (i = 0; i < NELEM(cmds); i++) { 1281 if (strcmp(cmds[i].name, argv[0]) == 0) { 1282 ret = cmds[i].handler(argc, argv); 1283 break; 1284 } 1285 } 1286 if (i == NELEM(cmds)) 1287 usage(); 1288 free_image_descs(); 1289 return ret; 1290 } 1291