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