1 /* 2 * (C) Copyright 2008 Semihalf 3 * 4 * (C) Copyright 2000-2009 5 * DENX Software Engineering 6 * Wolfgang Denk, wd@denx.de 7 * 8 * SPDX-License-Identifier: GPL-2.0+ 9 */ 10 11 #include "mkimage.h" 12 #include "imximage.h" 13 #include <image.h> 14 #include <version.h> 15 16 static void copy_file(int, const char *, int); 17 18 /* parameters initialized by core will be used by the image type code */ 19 static struct image_tool_params params = { 20 .os = IH_OS_LINUX, 21 .arch = IH_ARCH_PPC, 22 .type = IH_TYPE_KERNEL, 23 .comp = IH_COMP_GZIP, 24 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS, 25 .imagename = "", 26 .imagename2 = "", 27 }; 28 29 static enum ih_category cur_category; 30 31 static int h_compare_category_name(const void *vtype1, const void *vtype2) 32 { 33 const int *type1 = vtype1; 34 const int *type2 = vtype2; 35 const char *name1 = genimg_get_cat_short_name(cur_category, *type1); 36 const char *name2 = genimg_get_cat_short_name(cur_category, *type2); 37 38 return strcmp(name1, name2); 39 } 40 41 static int show_valid_options(enum ih_category category) 42 { 43 int *order; 44 int count; 45 int item; 46 int i; 47 48 count = genimg_get_cat_count(category); 49 order = calloc(count, sizeof(*order)); 50 if (!order) 51 return -ENOMEM; 52 53 /* Sort the names in order of short name for easier reading */ 54 for (item = 0; item < count; item++) 55 order[item] = item; 56 cur_category = category; 57 qsort(order, count, sizeof(int), h_compare_category_name); 58 59 fprintf(stderr, "\nInvalid %s, supported are:\n", 60 genimg_get_cat_desc(category)); 61 for (i = 0; i < count; i++) { 62 item = order[i]; 63 fprintf(stderr, "\t%-15s %s\n", 64 genimg_get_cat_short_name(category, item), 65 genimg_get_cat_name(category, item)); 66 } 67 fprintf(stderr, "\n"); 68 free(order); 69 70 return 0; 71 } 72 73 static void usage(const char *msg) 74 { 75 fprintf(stderr, "Error: %s\n", msg); 76 fprintf(stderr, "Usage: %s -l image\n" 77 " -l ==> list image header information\n", 78 params.cmdname); 79 fprintf(stderr, 80 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n" 81 " -A ==> set architecture to 'arch'\n" 82 " -O ==> set operating system to 'os'\n" 83 " -T ==> set image type to 'type'\n" 84 " -C ==> set compression type 'comp'\n" 85 " -a ==> set load address to 'addr' (hex)\n" 86 " -e ==> set entry point to 'ep' (hex)\n" 87 " -n ==> set image name to 'name'\n" 88 " -d ==> use image data from 'datafile'\n" 89 " -x ==> set XIP (execute in place)\n", 90 params.cmdname); 91 fprintf(stderr, 92 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n" 93 " <dtb> file is used with -f auto, it may occur multiple times.\n", 94 params.cmdname); 95 fprintf(stderr, 96 " -D => set all options for device tree compiler\n" 97 " -f => input filename for FIT source\n" 98 " -i => input filename for ramdisk file\n"); 99 #ifdef CONFIG_FIT_SIGNATURE 100 fprintf(stderr, 101 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n" 102 " -E => place data outside of the FIT structure\n" 103 " -k => set directory containing private keys\n" 104 " -K => write public keys to this .dtb file\n" 105 " -c => add comment in signature node\n" 106 " -F => re-sign existing FIT image\n" 107 " -p => place external data at a static position\n" 108 " -r => mark keys used as 'required' in dtb\n" 109 " -N => engine to use for signing (pkcs11)\n"); 110 #else 111 fprintf(stderr, 112 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n"); 113 #endif 114 fprintf(stderr, " %s -V ==> print version information and exit\n", 115 params.cmdname); 116 fprintf(stderr, "Use '-T list' to see a list of available image types\n"); 117 118 exit(EXIT_FAILURE); 119 } 120 121 static int add_content(int type, const char *fname) 122 { 123 struct content_info *cont; 124 125 cont = calloc(1, sizeof(*cont)); 126 if (!cont) 127 return -1; 128 cont->type = type; 129 cont->fname = fname; 130 if (params.content_tail) 131 params.content_tail->next = cont; 132 else 133 params.content_head = cont; 134 params.content_tail = cont; 135 136 return 0; 137 } 138 139 static void process_args(int argc, char **argv) 140 { 141 char *ptr; 142 int type = IH_TYPE_INVALID; 143 char *datafile = NULL; 144 int opt; 145 146 while ((opt = getopt(argc, argv, 147 "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVxX:")) != -1) { 148 switch (opt) { 149 case 'a': 150 params.addr = strtoull(optarg, &ptr, 16); 151 if (*ptr) { 152 fprintf(stderr, "%s: invalid load address %s\n", 153 params.cmdname, optarg); 154 exit(EXIT_FAILURE); 155 } 156 break; 157 case 'A': 158 params.arch = genimg_get_arch_id(optarg); 159 if (params.arch < 0) { 160 show_valid_options(IH_ARCH); 161 usage("Invalid architecture"); 162 } 163 break; 164 case 'b': 165 if (add_content(IH_TYPE_FLATDT, optarg)) { 166 fprintf(stderr, 167 "%s: Out of memory adding content '%s'", 168 params.cmdname, optarg); 169 exit(EXIT_FAILURE); 170 } 171 break; 172 case 'c': 173 params.comment = optarg; 174 break; 175 case 'C': 176 params.comp = genimg_get_comp_id(optarg); 177 if (params.comp < 0) { 178 show_valid_options(IH_COMP); 179 usage("Invalid compression type"); 180 } 181 break; 182 case 'd': 183 params.datafile = optarg; 184 params.dflag = 1; 185 break; 186 case 'D': 187 params.dtc = optarg; 188 break; 189 case 'e': 190 params.ep = strtoull(optarg, &ptr, 16); 191 if (*ptr) { 192 fprintf(stderr, "%s: invalid entry point %s\n", 193 params.cmdname, optarg); 194 exit(EXIT_FAILURE); 195 } 196 params.eflag = 1; 197 break; 198 case 'E': 199 params.external_data = true; 200 break; 201 case 'f': 202 datafile = optarg; 203 params.auto_its = !strcmp(datafile, "auto"); 204 /* no break */ 205 case 'F': 206 /* 207 * The flattened image tree (FIT) format 208 * requires a flattened device tree image type 209 */ 210 params.type = IH_TYPE_FLATDT; 211 params.fflag = 1; 212 break; 213 case 'i': 214 params.fit_ramdisk = optarg; 215 break; 216 case 'k': 217 params.keydir = optarg; 218 break; 219 case 'K': 220 params.keydest = optarg; 221 break; 222 case 'l': 223 params.lflag = 1; 224 break; 225 case 'n': 226 params.imagename = optarg; 227 break; 228 case 'N': 229 params.engine_id = optarg; 230 break; 231 case 'O': 232 params.os = genimg_get_os_id(optarg); 233 if (params.os < 0) { 234 show_valid_options(IH_OS); 235 usage("Invalid operating system"); 236 } 237 break; 238 case 'p': 239 params.external_offset = strtoull(optarg, &ptr, 16); 240 if (*ptr) { 241 fprintf(stderr, "%s: invalid offset size %s\n", 242 params.cmdname, optarg); 243 exit(EXIT_FAILURE); 244 } 245 break; 246 case 'q': 247 params.quiet = 1; 248 break; 249 case 'r': 250 params.require_keys = 1; 251 break; 252 case 'R': 253 /* 254 * This entry is for the second configuration 255 * file, if only one is not enough. 256 */ 257 params.imagename2 = optarg; 258 break; 259 case 's': 260 params.skipcpy = 1; 261 break; 262 case 'T': 263 if (strcmp(optarg, "list") == 0) { 264 show_valid_options(IH_TYPE); 265 exit(EXIT_SUCCESS); 266 } 267 type = genimg_get_type_id(optarg); 268 if (type < 0) { 269 show_valid_options(IH_TYPE); 270 usage("Invalid image type"); 271 } 272 break; 273 case 'v': 274 params.vflag++; 275 break; 276 case 'V': 277 printf("mkimage version %s\n", PLAIN_VERSION); 278 exit(EXIT_SUCCESS); 279 case 'x': 280 params.xflag++; 281 break; 282 case 'X': 283 params.extraparams = optarg; 284 break; 285 default: 286 usage("Invalid option"); 287 } 288 } 289 290 /* The last parameter is expected to be the imagefile */ 291 if (optind < argc) 292 params.imagefile = argv[optind]; 293 294 /* 295 * For auto-generated FIT images we need to know the image type to put 296 * in the FIT, which is separate from the file's image type (which 297 * will always be IH_TYPE_FLATDT in this case). 298 */ 299 if (params.type == IH_TYPE_FLATDT) { 300 params.fit_image_type = type ? type : IH_TYPE_KERNEL; 301 /* For auto_its, datafile is always 'auto' */ 302 if (!params.auto_its) 303 params.datafile = datafile; 304 else if (!params.datafile) 305 usage("Missing data file for auto-FIT (use -d)"); 306 } else if (type != IH_TYPE_INVALID) { 307 params.type = type; 308 } 309 310 if (!params.imagefile) 311 usage("Missing output filename"); 312 } 313 314 int main(int argc, char **argv) 315 { 316 int ifd = -1; 317 struct stat sbuf; 318 char *ptr; 319 int retval = 0; 320 struct image_type_params *tparams = NULL; 321 int pad_len = 0; 322 int dfd; 323 324 params.cmdname = *argv; 325 params.addr = 0; 326 params.ep = 0; 327 328 process_args(argc, argv); 329 330 /* set tparams as per input type_id */ 331 tparams = imagetool_get_type(params.type); 332 if (tparams == NULL) { 333 fprintf (stderr, "%s: unsupported type %s\n", 334 params.cmdname, genimg_get_type_name(params.type)); 335 exit (EXIT_FAILURE); 336 } 337 338 /* 339 * check the passed arguments parameters meets the requirements 340 * as per image type to be generated/listed 341 */ 342 if (tparams->check_params) 343 if (tparams->check_params (¶ms)) 344 usage("Bad parameters for image type"); 345 346 if (!params.eflag) { 347 params.ep = params.addr; 348 /* If XIP, entry point must be after the U-Boot header */ 349 if (params.xflag) 350 params.ep += tparams->header_size; 351 } 352 353 if (params.fflag){ 354 if (tparams->fflag_handle) 355 /* 356 * in some cases, some additional processing needs 357 * to be done if fflag is defined 358 * 359 * For ex. fit_handle_file for Fit file support 360 */ 361 retval = tparams->fflag_handle(¶ms); 362 363 if (retval != EXIT_SUCCESS) 364 exit (retval); 365 } 366 367 if (params.lflag || params.fflag) { 368 ifd = open (params.imagefile, O_RDONLY|O_BINARY); 369 } else { 370 ifd = open (params.imagefile, 371 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); 372 } 373 374 if (ifd < 0) { 375 fprintf (stderr, "%s: Can't open %s: %s\n", 376 params.cmdname, params.imagefile, 377 strerror(errno)); 378 exit (EXIT_FAILURE); 379 } 380 381 if (params.lflag || params.fflag) { 382 /* 383 * list header information of existing image 384 */ 385 if (fstat(ifd, &sbuf) < 0) { 386 fprintf (stderr, "%s: Can't stat %s: %s\n", 387 params.cmdname, params.imagefile, 388 strerror(errno)); 389 exit (EXIT_FAILURE); 390 } 391 392 if ((unsigned)sbuf.st_size < tparams->header_size) { 393 fprintf (stderr, 394 "%s: Bad size: \"%s\" is not valid image\n", 395 params.cmdname, params.imagefile); 396 exit (EXIT_FAILURE); 397 } 398 399 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); 400 if (ptr == MAP_FAILED) { 401 fprintf (stderr, "%s: Can't read %s: %s\n", 402 params.cmdname, params.imagefile, 403 strerror(errno)); 404 exit (EXIT_FAILURE); 405 } 406 407 /* 408 * scan through mkimage registry for all supported image types 409 * and verify the input image file header for match 410 * Print the image information for matched image type 411 * Returns the error code if not matched 412 */ 413 retval = imagetool_verify_print_header(ptr, &sbuf, 414 tparams, ¶ms); 415 416 (void) munmap((void *)ptr, sbuf.st_size); 417 (void) close (ifd); 418 419 exit (retval); 420 } 421 422 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT) && 423 (params.type != IH_TYPE_RKNAND)) { 424 dfd = open(params.datafile, O_RDONLY | O_BINARY); 425 if (dfd < 0) { 426 fprintf(stderr, "%s: Can't open %s: %s\n", 427 params.cmdname, params.datafile, 428 strerror(errno)); 429 exit(EXIT_FAILURE); 430 } 431 432 if (fstat(dfd, &sbuf) < 0) { 433 fprintf(stderr, "%s: Can't stat %s: %s\n", 434 params.cmdname, params.datafile, 435 strerror(errno)); 436 exit(EXIT_FAILURE); 437 } 438 439 params.file_size = sbuf.st_size + tparams->header_size; 440 close(dfd); 441 } 442 443 /* 444 * In case there an header with a variable 445 * length will be added, the corresponding 446 * function is called. This is responsible to 447 * allocate memory for the header itself. 448 */ 449 if (tparams->vrec_header) 450 pad_len = tparams->vrec_header(¶ms, tparams); 451 else 452 memset(tparams->hdr, 0, tparams->header_size); 453 454 if (write(ifd, tparams->hdr, tparams->header_size) 455 != tparams->header_size) { 456 fprintf (stderr, "%s: Write error on %s: %s\n", 457 params.cmdname, params.imagefile, strerror(errno)); 458 exit (EXIT_FAILURE); 459 } 460 461 if (!params.skipcpy) { 462 if (params.type == IH_TYPE_MULTI || 463 params.type == IH_TYPE_SCRIPT) { 464 char *file = params.datafile; 465 uint32_t size; 466 467 for (;;) { 468 char *sep = NULL; 469 470 if (file) { 471 if ((sep = strchr(file, ':')) != NULL) { 472 *sep = '\0'; 473 } 474 475 if (stat (file, &sbuf) < 0) { 476 fprintf (stderr, "%s: Can't stat %s: %s\n", 477 params.cmdname, file, strerror(errno)); 478 exit (EXIT_FAILURE); 479 } 480 size = cpu_to_uimage (sbuf.st_size); 481 } else { 482 size = 0; 483 } 484 485 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { 486 fprintf (stderr, "%s: Write error on %s: %s\n", 487 params.cmdname, params.imagefile, 488 strerror(errno)); 489 exit (EXIT_FAILURE); 490 } 491 492 if (!file) { 493 break; 494 } 495 496 if (sep) { 497 *sep = ':'; 498 file = sep + 1; 499 } else { 500 file = NULL; 501 } 502 } 503 504 file = params.datafile; 505 506 for (;;) { 507 char *sep = strchr(file, ':'); 508 if (sep) { 509 *sep = '\0'; 510 copy_file (ifd, file, 1); 511 *sep++ = ':'; 512 file = sep; 513 } else { 514 copy_file (ifd, file, 0); 515 break; 516 } 517 } 518 } else if (params.type == IH_TYPE_PBLIMAGE) { 519 /* PBL has special Image format, implements its' own */ 520 pbl_load_uboot(ifd, ¶ms); 521 } else { 522 copy_file(ifd, params.datafile, pad_len); 523 } 524 if (params.type == IH_TYPE_FIRMWARE_IVT) { 525 /* Add alignment and IVT */ 526 uint32_t aligned_filesize = (params.file_size + 0x1000 527 - 1) & ~(0x1000 - 1); 528 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 }, 529 params.addr, 0, 0, 0, params.addr 530 + aligned_filesize 531 - tparams->header_size, 532 params.addr + aligned_filesize 533 - tparams->header_size 534 + 0x20, 0 }; 535 int i = params.file_size; 536 for (; i < aligned_filesize; i++) { 537 if (write(ifd, (char *) &i, 1) != 1) { 538 fprintf(stderr, 539 "%s: Write error on %s: %s\n", 540 params.cmdname, 541 params.imagefile, 542 strerror(errno)); 543 exit(EXIT_FAILURE); 544 } 545 } 546 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t)) 547 != sizeof(flash_header_v2_t)) { 548 fprintf(stderr, "%s: Write error on %s: %s\n", 549 params.cmdname, 550 params.imagefile, 551 strerror(errno)); 552 exit(EXIT_FAILURE); 553 } 554 } 555 } 556 557 /* We're a bit of paranoid */ 558 #if defined(_POSIX_SYNCHRONIZED_IO) && \ 559 !defined(__sun__) && \ 560 !defined(__FreeBSD__) && \ 561 !defined(__OpenBSD__) && \ 562 !defined(__APPLE__) 563 (void) fdatasync (ifd); 564 #else 565 (void) fsync (ifd); 566 #endif 567 568 if (fstat(ifd, &sbuf) < 0) { 569 fprintf (stderr, "%s: Can't stat %s: %s\n", 570 params.cmdname, params.imagefile, strerror(errno)); 571 exit (EXIT_FAILURE); 572 } 573 params.file_size = sbuf.st_size; 574 575 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); 576 if (ptr == MAP_FAILED) { 577 fprintf (stderr, "%s: Can't map %s: %s\n", 578 params.cmdname, params.imagefile, strerror(errno)); 579 exit (EXIT_FAILURE); 580 } 581 582 /* Setup the image header as per input image type*/ 583 if (tparams->set_header) 584 tparams->set_header (ptr, &sbuf, ifd, ¶ms); 585 else { 586 fprintf (stderr, "%s: Can't set header for %s: %s\n", 587 params.cmdname, tparams->name, strerror(errno)); 588 exit (EXIT_FAILURE); 589 } 590 591 /* Print the image information by processing image header */ 592 if (tparams->print_header) 593 tparams->print_header (ptr); 594 else { 595 fprintf (stderr, "%s: Can't print header for %s: %s\n", 596 params.cmdname, tparams->name, strerror(errno)); 597 exit (EXIT_FAILURE); 598 } 599 600 (void) munmap((void *)ptr, sbuf.st_size); 601 602 /* We're a bit of paranoid */ 603 #if defined(_POSIX_SYNCHRONIZED_IO) && \ 604 !defined(__sun__) && \ 605 !defined(__FreeBSD__) && \ 606 !defined(__OpenBSD__) && \ 607 !defined(__APPLE__) 608 (void) fdatasync (ifd); 609 #else 610 (void) fsync (ifd); 611 #endif 612 613 if (close(ifd)) { 614 fprintf (stderr, "%s: Write error on %s: %s\n", 615 params.cmdname, params.imagefile, strerror(errno)); 616 exit (EXIT_FAILURE); 617 } 618 619 exit (EXIT_SUCCESS); 620 } 621 622 static void 623 copy_file (int ifd, const char *datafile, int pad) 624 { 625 int dfd; 626 struct stat sbuf; 627 unsigned char *ptr; 628 int tail; 629 int zero = 0; 630 uint8_t zeros[4096]; 631 int offset = 0; 632 int size; 633 struct image_type_params *tparams = imagetool_get_type(params.type); 634 635 memset(zeros, 0, sizeof(zeros)); 636 637 if (params.vflag) { 638 fprintf (stderr, "Adding Image %s\n", datafile); 639 } 640 641 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { 642 fprintf (stderr, "%s: Can't open %s: %s\n", 643 params.cmdname, datafile, strerror(errno)); 644 exit (EXIT_FAILURE); 645 } 646 647 if (fstat(dfd, &sbuf) < 0) { 648 fprintf (stderr, "%s: Can't stat %s: %s\n", 649 params.cmdname, datafile, strerror(errno)); 650 exit (EXIT_FAILURE); 651 } 652 653 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); 654 if (ptr == MAP_FAILED) { 655 fprintf (stderr, "%s: Can't read %s: %s\n", 656 params.cmdname, datafile, strerror(errno)); 657 exit (EXIT_FAILURE); 658 } 659 660 if (params.xflag) { 661 unsigned char *p = NULL; 662 /* 663 * XIP: do not append the image_header_t at the 664 * beginning of the file, but consume the space 665 * reserved for it. 666 */ 667 668 if ((unsigned)sbuf.st_size < tparams->header_size) { 669 fprintf (stderr, 670 "%s: Bad size: \"%s\" is too small for XIP\n", 671 params.cmdname, datafile); 672 exit (EXIT_FAILURE); 673 } 674 675 for (p = ptr; p < ptr + tparams->header_size; p++) { 676 if ( *p != 0xff ) { 677 fprintf (stderr, 678 "%s: Bad file: \"%s\" has invalid buffer for XIP\n", 679 params.cmdname, datafile); 680 exit (EXIT_FAILURE); 681 } 682 } 683 684 offset = tparams->header_size; 685 } 686 687 size = sbuf.st_size - offset; 688 if (write(ifd, ptr + offset, size) != size) { 689 fprintf (stderr, "%s: Write error on %s: %s\n", 690 params.cmdname, params.imagefile, strerror(errno)); 691 exit (EXIT_FAILURE); 692 } 693 694 tail = size % 4; 695 if ((pad == 1) && (tail != 0)) { 696 697 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { 698 fprintf (stderr, "%s: Write error on %s: %s\n", 699 params.cmdname, params.imagefile, 700 strerror(errno)); 701 exit (EXIT_FAILURE); 702 } 703 } else if (pad > 1) { 704 while (pad > 0) { 705 int todo = sizeof(zeros); 706 707 if (todo > pad) 708 todo = pad; 709 if (write(ifd, (char *)&zeros, todo) != todo) { 710 fprintf(stderr, "%s: Write error on %s: %s\n", 711 params.cmdname, params.imagefile, 712 strerror(errno)); 713 exit(EXIT_FAILURE); 714 } 715 pad -= todo; 716 } 717 } 718 719 (void) munmap((void *)ptr, sbuf.st_size); 720 (void) close (dfd); 721 } 722