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