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