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