1 /* 2 * (C) Copyright 2008 Semihalf 3 * 4 * (C) Copyright 2000-2009 5 * DENX Software Engineering 6 * Wolfgang Denk, wd@denx.de 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation; either version 2 of 11 * the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 * MA 02111-1307 USA 22 */ 23 24 #include "mkimage.h" 25 #include <image.h> 26 27 static void copy_file(int, const char *, int); 28 static void usage(void); 29 30 /* image_type_params link list to maintain registered image type supports */ 31 struct image_type_params *mkimage_tparams = NULL; 32 33 /* parameters initialized by core will be used by the image type code */ 34 struct mkimage_params params = { 35 .os = IH_OS_LINUX, 36 .arch = IH_ARCH_PPC, 37 .type = IH_TYPE_KERNEL, 38 .comp = IH_COMP_GZIP, 39 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS, 40 }; 41 42 /* 43 * mkimage_register - 44 * 45 * It is used to register respective image generation/list support to the 46 * mkimage core 47 * 48 * the input struct image_type_params is checked and appended to the link 49 * list, if the input structure is already registered, error 50 */ 51 void mkimage_register (struct image_type_params *tparams) 52 { 53 struct image_type_params **tp; 54 55 if (!tparams) { 56 fprintf (stderr, "%s: %s: Null input\n", 57 params.cmdname, __FUNCTION__); 58 exit (EXIT_FAILURE); 59 } 60 61 /* scan the linked list, check for registry and point the last one */ 62 for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) { 63 if (!strcmp((*tp)->name, tparams->name)) { 64 fprintf (stderr, "%s: %s already registered\n", 65 params.cmdname, tparams->name); 66 return; 67 } 68 } 69 70 /* add input struct entry at the end of link list */ 71 *tp = tparams; 72 /* mark input entry as last entry in the link list */ 73 tparams->next = NULL; 74 75 debug ("Registered %s\n", tparams->name); 76 } 77 78 /* 79 * mkimage_get_type - 80 * 81 * It scans all registers image type supports 82 * checks the input type_id for each supported image type 83 * 84 * if successful, 85 * returns respective image_type_params pointer if success 86 * if input type_id is not supported by any of image_type_support 87 * returns NULL 88 */ 89 struct image_type_params *mkimage_get_type(int type) 90 { 91 struct image_type_params *curr; 92 93 for (curr = mkimage_tparams; curr != NULL; curr = curr->next) { 94 if (curr->check_image_type) { 95 if (!curr->check_image_type (type)) 96 return curr; 97 } 98 } 99 return NULL; 100 } 101 102 /* 103 * mkimage_verify_print_header - 104 * 105 * It scans mkimage_tparams link list, 106 * verifies image_header for each supported image type 107 * if verification is successful, prints respective header 108 * 109 * returns negative if input image format does not match with any of 110 * supported image types 111 */ 112 int mkimage_verify_print_header (void *ptr, struct stat *sbuf) 113 { 114 int retval = -1; 115 struct image_type_params *curr; 116 117 for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) { 118 if (curr->verify_header) { 119 retval = curr->verify_header ( 120 (unsigned char *)ptr, sbuf->st_size, 121 ¶ms); 122 123 if (retval == 0) { 124 /* 125 * Print the image information 126 * if verify is successful 127 */ 128 if (curr->print_header) 129 curr->print_header (ptr); 130 else { 131 fprintf (stderr, 132 "%s: print_header undefined for %s\n", 133 params.cmdname, curr->name); 134 } 135 break; 136 } 137 } 138 } 139 return retval; 140 } 141 142 int 143 main (int argc, char **argv) 144 { 145 int ifd = -1; 146 struct stat sbuf; 147 unsigned char *ptr; 148 int retval = 0; 149 struct image_type_params *tparams = NULL; 150 151 /* Init Kirkwood Boot image generation/list support */ 152 init_kwb_image_type (); 153 /* Init FIT image generation/list support */ 154 init_fit_image_type (); 155 /* Init Default image generation/list support */ 156 init_default_image_type (); 157 158 params.cmdname = *argv; 159 params.addr = params.ep = 0; 160 161 while (--argc > 0 && **++argv == '-') { 162 while (*++*argv) { 163 switch (**argv) { 164 case 'l': 165 params.lflag = 1; 166 break; 167 case 'A': 168 if ((--argc <= 0) || 169 (params.arch = 170 genimg_get_arch_id (*++argv)) < 0) 171 usage (); 172 goto NXTARG; 173 case 'C': 174 if ((--argc <= 0) || 175 (params.comp = 176 genimg_get_comp_id (*++argv)) < 0) 177 usage (); 178 goto NXTARG; 179 case 'D': 180 if (--argc <= 0) 181 usage (); 182 params.dtc = *++argv; 183 goto NXTARG; 184 185 case 'O': 186 if ((--argc <= 0) || 187 (params.os = 188 genimg_get_os_id (*++argv)) < 0) 189 usage (); 190 goto NXTARG; 191 case 'T': 192 if ((--argc <= 0) || 193 (params.type = 194 genimg_get_type_id (*++argv)) < 0) 195 usage (); 196 goto NXTARG; 197 198 case 'a': 199 if (--argc <= 0) 200 usage (); 201 params.addr = strtoul (*++argv, 202 (char **)&ptr, 16); 203 if (*ptr) { 204 fprintf (stderr, 205 "%s: invalid load address %s\n", 206 params.cmdname, *argv); 207 exit (EXIT_FAILURE); 208 } 209 goto NXTARG; 210 case 'd': 211 if (--argc <= 0) 212 usage (); 213 params.datafile = *++argv; 214 params.dflag = 1; 215 goto NXTARG; 216 case 'e': 217 if (--argc <= 0) 218 usage (); 219 params.ep = strtoul (*++argv, 220 (char **)&ptr, 16); 221 if (*ptr) { 222 fprintf (stderr, 223 "%s: invalid entry point %s\n", 224 params.cmdname, *argv); 225 exit (EXIT_FAILURE); 226 } 227 params.eflag = 1; 228 goto NXTARG; 229 case 'f': 230 if (--argc <= 0) 231 usage (); 232 params.type = IH_TYPE_FLATDT; 233 params.datafile = *++argv; 234 params.fflag = 1; 235 goto NXTARG; 236 case 'n': 237 if (--argc <= 0) 238 usage (); 239 params.imagename = *++argv; 240 goto NXTARG; 241 case 'v': 242 params.vflag++; 243 break; 244 case 'x': 245 params.xflag++; 246 break; 247 default: 248 usage (); 249 } 250 } 251 NXTARG: ; 252 } 253 254 if (argc != 1) 255 usage (); 256 257 /* set tparams as per input type_id */ 258 tparams = mkimage_get_type(params.type); 259 if (tparams == NULL) { 260 fprintf (stderr, "%s: unsupported type %s\n", 261 params.cmdname, genimg_get_type_name(params.type)); 262 exit (EXIT_FAILURE); 263 } 264 265 /* 266 * check the passed arguments parameters meets the requirements 267 * as per image type to be generated/listed 268 */ 269 if (tparams->check_params) 270 if (tparams->check_params (¶ms)) 271 usage (); 272 273 if (!params.eflag) { 274 params.ep = params.addr; 275 /* If XIP, entry point must be after the U-Boot header */ 276 if (params.xflag) 277 params.ep += tparams->header_size; 278 } 279 280 /* 281 * If XIP, ensure the entry point is equal to the load address plus 282 * the size of the U-Boot header. 283 */ 284 if (params.xflag) { 285 if (params.ep != params.addr + tparams->header_size) { 286 fprintf (stderr, 287 "%s: For XIP, the entry point must be the load addr + %lu\n", 288 params.cmdname, 289 (unsigned long)tparams->header_size); 290 exit (EXIT_FAILURE); 291 } 292 } 293 294 params.imagefile = *argv; 295 296 if (!params.fflag){ 297 if (params.lflag) { 298 ifd = open (params.imagefile, O_RDONLY|O_BINARY); 299 } else { 300 ifd = open (params.imagefile, 301 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); 302 } 303 304 if (ifd < 0) { 305 fprintf (stderr, "%s: Can't open %s: %s\n", 306 params.cmdname, params.imagefile, 307 strerror(errno)); 308 exit (EXIT_FAILURE); 309 } 310 } 311 312 if (params.lflag) { 313 /* 314 * list header information of existing image 315 */ 316 if (fstat(ifd, &sbuf) < 0) { 317 fprintf (stderr, "%s: Can't stat %s: %s\n", 318 params.cmdname, params.imagefile, 319 strerror(errno)); 320 exit (EXIT_FAILURE); 321 } 322 323 if ((unsigned)sbuf.st_size < tparams->header_size) { 324 fprintf (stderr, 325 "%s: Bad size: \"%s\" is not valid image\n", 326 params.cmdname, params.imagefile); 327 exit (EXIT_FAILURE); 328 } 329 330 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); 331 if (ptr == MAP_FAILED) { 332 fprintf (stderr, "%s: Can't read %s: %s\n", 333 params.cmdname, params.imagefile, 334 strerror(errno)); 335 exit (EXIT_FAILURE); 336 } 337 338 /* 339 * scan through mkimage registry for all supported image types 340 * and verify the input image file header for match 341 * Print the image information for matched image type 342 * Returns the error code if not matched 343 */ 344 retval = mkimage_verify_print_header (ptr, &sbuf); 345 346 (void) munmap((void *)ptr, sbuf.st_size); 347 (void) close (ifd); 348 349 exit (retval); 350 } else if (params.fflag) { 351 if (tparams->fflag_handle) 352 /* 353 * in some cases, some additional processing needs 354 * to be done if fflag is defined 355 * 356 * For ex. fit_handle_file for Fit file support 357 */ 358 retval = tparams->fflag_handle(¶ms); 359 360 exit (retval); 361 } 362 363 /* 364 * Must be -w then: 365 * 366 * write dummy header, to be fixed later 367 */ 368 memset (tparams->hdr, 0, tparams->header_size); 369 370 if (write(ifd, tparams->hdr, tparams->header_size) 371 != tparams->header_size) { 372 fprintf (stderr, "%s: Write error on %s: %s\n", 373 params.cmdname, params.imagefile, strerror(errno)); 374 exit (EXIT_FAILURE); 375 } 376 377 if (params.type == IH_TYPE_MULTI || params.type == IH_TYPE_SCRIPT) { 378 char *file = params.datafile; 379 uint32_t size; 380 381 for (;;) { 382 char *sep = NULL; 383 384 if (file) { 385 if ((sep = strchr(file, ':')) != NULL) { 386 *sep = '\0'; 387 } 388 389 if (stat (file, &sbuf) < 0) { 390 fprintf (stderr, "%s: Can't stat %s: %s\n", 391 params.cmdname, file, strerror(errno)); 392 exit (EXIT_FAILURE); 393 } 394 size = cpu_to_uimage (sbuf.st_size); 395 } else { 396 size = 0; 397 } 398 399 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { 400 fprintf (stderr, "%s: Write error on %s: %s\n", 401 params.cmdname, params.imagefile, 402 strerror(errno)); 403 exit (EXIT_FAILURE); 404 } 405 406 if (!file) { 407 break; 408 } 409 410 if (sep) { 411 *sep = ':'; 412 file = sep + 1; 413 } else { 414 file = NULL; 415 } 416 } 417 418 file = params.datafile; 419 420 for (;;) { 421 char *sep = strchr(file, ':'); 422 if (sep) { 423 *sep = '\0'; 424 copy_file (ifd, file, 1); 425 *sep++ = ':'; 426 file = sep; 427 } else { 428 copy_file (ifd, file, 0); 429 break; 430 } 431 } 432 } else { 433 copy_file (ifd, params.datafile, 0); 434 } 435 436 /* We're a bit of paranoid */ 437 #if defined(_POSIX_SYNCHRONIZED_IO) && \ 438 !defined(__sun__) && \ 439 !defined(__FreeBSD__) && \ 440 !defined(__APPLE__) 441 (void) fdatasync (ifd); 442 #else 443 (void) fsync (ifd); 444 #endif 445 446 if (fstat(ifd, &sbuf) < 0) { 447 fprintf (stderr, "%s: Can't stat %s: %s\n", 448 params.cmdname, params.imagefile, strerror(errno)); 449 exit (EXIT_FAILURE); 450 } 451 452 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); 453 if (ptr == MAP_FAILED) { 454 fprintf (stderr, "%s: Can't map %s: %s\n", 455 params.cmdname, params.imagefile, strerror(errno)); 456 exit (EXIT_FAILURE); 457 } 458 459 /* Setup the image header as per input image type*/ 460 if (tparams->set_header) 461 tparams->set_header (ptr, &sbuf, ifd, ¶ms); 462 else { 463 fprintf (stderr, "%s: Can't set header for %s: %s\n", 464 params.cmdname, tparams->name, strerror(errno)); 465 exit (EXIT_FAILURE); 466 } 467 468 /* Print the image information by processing image header */ 469 if (tparams->print_header) 470 tparams->print_header (ptr); 471 else { 472 fprintf (stderr, "%s: Can't print header for %s: %s\n", 473 params.cmdname, tparams->name, strerror(errno)); 474 exit (EXIT_FAILURE); 475 } 476 477 (void) munmap((void *)ptr, sbuf.st_size); 478 479 /* We're a bit of paranoid */ 480 #if defined(_POSIX_SYNCHRONIZED_IO) && \ 481 !defined(__sun__) && \ 482 !defined(__FreeBSD__) && \ 483 !defined(__APPLE__) 484 (void) fdatasync (ifd); 485 #else 486 (void) fsync (ifd); 487 #endif 488 489 if (close(ifd)) { 490 fprintf (stderr, "%s: Write error on %s: %s\n", 491 params.cmdname, params.imagefile, strerror(errno)); 492 exit (EXIT_FAILURE); 493 } 494 495 exit (EXIT_SUCCESS); 496 } 497 498 static void 499 copy_file (int ifd, const char *datafile, int pad) 500 { 501 int dfd; 502 struct stat sbuf; 503 unsigned char *ptr; 504 int tail; 505 int zero = 0; 506 int offset = 0; 507 int size; 508 struct image_type_params *tparams = mkimage_get_type (params.type); 509 510 if (params.vflag) { 511 fprintf (stderr, "Adding Image %s\n", datafile); 512 } 513 514 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { 515 fprintf (stderr, "%s: Can't open %s: %s\n", 516 params.cmdname, datafile, strerror(errno)); 517 exit (EXIT_FAILURE); 518 } 519 520 if (fstat(dfd, &sbuf) < 0) { 521 fprintf (stderr, "%s: Can't stat %s: %s\n", 522 params.cmdname, datafile, strerror(errno)); 523 exit (EXIT_FAILURE); 524 } 525 526 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); 527 if (ptr == MAP_FAILED) { 528 fprintf (stderr, "%s: Can't read %s: %s\n", 529 params.cmdname, datafile, strerror(errno)); 530 exit (EXIT_FAILURE); 531 } 532 533 if (params.xflag) { 534 unsigned char *p = NULL; 535 /* 536 * XIP: do not append the image_header_t at the 537 * beginning of the file, but consume the space 538 * reserved for it. 539 */ 540 541 if ((unsigned)sbuf.st_size < tparams->header_size) { 542 fprintf (stderr, 543 "%s: Bad size: \"%s\" is too small for XIP\n", 544 params.cmdname, datafile); 545 exit (EXIT_FAILURE); 546 } 547 548 for (p = ptr; p < ptr + tparams->header_size; p++) { 549 if ( *p != 0xff ) { 550 fprintf (stderr, 551 "%s: Bad file: \"%s\" has invalid buffer for XIP\n", 552 params.cmdname, datafile); 553 exit (EXIT_FAILURE); 554 } 555 } 556 557 offset = tparams->header_size; 558 } 559 560 size = sbuf.st_size - offset; 561 if (write(ifd, ptr + offset, size) != size) { 562 fprintf (stderr, "%s: Write error on %s: %s\n", 563 params.cmdname, params.imagefile, strerror(errno)); 564 exit (EXIT_FAILURE); 565 } 566 567 if (pad && ((tail = size % 4) != 0)) { 568 569 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { 570 fprintf (stderr, "%s: Write error on %s: %s\n", 571 params.cmdname, params.imagefile, 572 strerror(errno)); 573 exit (EXIT_FAILURE); 574 } 575 } 576 577 (void) munmap((void *)ptr, sbuf.st_size); 578 (void) close (dfd); 579 } 580 581 void 582 usage () 583 { 584 fprintf (stderr, "Usage: %s -l image\n" 585 " -l ==> list image header information\n", 586 params.cmdname); 587 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp " 588 "-a addr -e ep -n name -d data_file[:data_file...] image\n" 589 " -A ==> set architecture to 'arch'\n" 590 " -O ==> set operating system to 'os'\n" 591 " -T ==> set image type to 'type'\n" 592 " -C ==> set compression type 'comp'\n" 593 " -a ==> set load address to 'addr' (hex)\n" 594 " -e ==> set entry point to 'ep' (hex)\n" 595 " -n ==> set image name to 'name'\n" 596 " -d ==> use image data from 'datafile'\n" 597 " -x ==> set XIP (execute in place)\n", 598 params.cmdname); 599 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n", 600 params.cmdname); 601 602 exit (EXIT_FAILURE); 603 } 604