1 /* 2 * Copyright (c) 2013, Google Inc. 3 * 4 * (C) Copyright 2008 Semihalf 5 * 6 * (C) Copyright 2000-2006 7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 8 * 9 * SPDX-License-Identifier: GPL-2.0+ 10 */ 11 12 #ifdef USE_HOSTCC 13 #include "mkimage.h" 14 #include <time.h> 15 #else 16 #include <linux/compiler.h> 17 #include <common.h> 18 #include <errno.h> 19 #include <mapmem.h> 20 #include <asm/io.h> 21 DECLARE_GLOBAL_DATA_PTR; 22 #endif /* !USE_HOSTCC*/ 23 24 #include <image.h> 25 #include <bootstage.h> 26 #include <u-boot/crc.h> 27 #include <u-boot/md5.h> 28 #include <u-boot/sha1.h> 29 #include <u-boot/sha256.h> 30 31 /*****************************************************************************/ 32 /* New uImage format routines */ 33 /*****************************************************************************/ 34 #ifndef USE_HOSTCC 35 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr, 36 ulong *addr, const char **name) 37 { 38 const char *sep; 39 40 *addr = addr_curr; 41 *name = NULL; 42 43 sep = strchr(spec, sepc); 44 if (sep) { 45 if (sep - spec > 0) 46 *addr = simple_strtoul(spec, NULL, 16); 47 48 *name = sep + 1; 49 return 1; 50 } 51 52 return 0; 53 } 54 55 /** 56 * fit_parse_conf - parse FIT configuration spec 57 * @spec: input string, containing configuration spec 58 * @add_curr: current image address (to be used as a possible default) 59 * @addr: pointer to a ulong variable, will hold FIT image address of a given 60 * configuration 61 * @conf_name double pointer to a char, will hold pointer to a configuration 62 * unit name 63 * 64 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>, 65 * where <addr> is a FIT image address that contains configuration 66 * with a <conf> unit name. 67 * 68 * Address part is optional, and if omitted default add_curr will 69 * be used instead. 70 * 71 * returns: 72 * 1 if spec is a valid configuration string, 73 * addr and conf_name are set accordingly 74 * 0 otherwise 75 */ 76 int fit_parse_conf(const char *spec, ulong addr_curr, 77 ulong *addr, const char **conf_name) 78 { 79 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name); 80 } 81 82 /** 83 * fit_parse_subimage - parse FIT subimage spec 84 * @spec: input string, containing subimage spec 85 * @add_curr: current image address (to be used as a possible default) 86 * @addr: pointer to a ulong variable, will hold FIT image address of a given 87 * subimage 88 * @image_name: double pointer to a char, will hold pointer to a subimage name 89 * 90 * fit_parse_subimage() expects subimage spec in the form of 91 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains 92 * subimage with a <subimg> unit name. 93 * 94 * Address part is optional, and if omitted default add_curr will 95 * be used instead. 96 * 97 * returns: 98 * 1 if spec is a valid subimage string, 99 * addr and image_name are set accordingly 100 * 0 otherwise 101 */ 102 int fit_parse_subimage(const char *spec, ulong addr_curr, 103 ulong *addr, const char **image_name) 104 { 105 return fit_parse_spec(spec, ':', addr_curr, addr, image_name); 106 } 107 #endif /* !USE_HOSTCC */ 108 109 static void fit_get_debug(const void *fit, int noffset, 110 char *prop_name, int err) 111 { 112 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n", 113 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL), 114 fdt_strerror(err)); 115 } 116 117 /** 118 * fit_get_subimage_count - get component (sub-image) count 119 * @fit: pointer to the FIT format image header 120 * @images_noffset: offset of images node 121 * 122 * returns: 123 * number of image components 124 */ 125 int fit_get_subimage_count(const void *fit, int images_noffset) 126 { 127 int noffset; 128 int ndepth; 129 int count = 0; 130 131 /* Process its subnodes, print out component images details */ 132 for (ndepth = 0, count = 0, 133 noffset = fdt_next_node(fit, images_noffset, &ndepth); 134 (noffset >= 0) && (ndepth > 0); 135 noffset = fdt_next_node(fit, noffset, &ndepth)) { 136 if (ndepth == 1) { 137 count++; 138 } 139 } 140 141 return count; 142 } 143 144 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) 145 /** 146 * fit_print_contents - prints out the contents of the FIT format image 147 * @fit: pointer to the FIT format image header 148 * @p: pointer to prefix string 149 * 150 * fit_print_contents() formats a multi line FIT image contents description. 151 * The routine prints out FIT image properties (root node level) followed by 152 * the details of each component image. 153 * 154 * returns: 155 * no returned results 156 */ 157 void fit_print_contents(const void *fit) 158 { 159 char *desc; 160 char *uname; 161 int images_noffset; 162 int confs_noffset; 163 int noffset; 164 int ndepth; 165 int count = 0; 166 int ret; 167 const char *p; 168 time_t timestamp; 169 170 /* Indent string is defined in header image.h */ 171 p = IMAGE_INDENT_STRING; 172 173 /* Root node properties */ 174 ret = fit_get_desc(fit, 0, &desc); 175 printf("%sFIT description: ", p); 176 if (ret) 177 printf("unavailable\n"); 178 else 179 printf("%s\n", desc); 180 181 if (IMAGE_ENABLE_TIMESTAMP) { 182 ret = fit_get_timestamp(fit, 0, ×tamp); 183 printf("%sCreated: ", p); 184 if (ret) 185 printf("unavailable\n"); 186 else 187 genimg_print_time(timestamp); 188 } 189 190 /* Find images parent node offset */ 191 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 192 if (images_noffset < 0) { 193 printf("Can't find images parent node '%s' (%s)\n", 194 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 195 return; 196 } 197 198 /* Process its subnodes, print out component images details */ 199 for (ndepth = 0, count = 0, 200 noffset = fdt_next_node(fit, images_noffset, &ndepth); 201 (noffset >= 0) && (ndepth > 0); 202 noffset = fdt_next_node(fit, noffset, &ndepth)) { 203 if (ndepth == 1) { 204 /* 205 * Direct child node of the images parent node, 206 * i.e. component image node. 207 */ 208 printf("%s Image %u (%s)\n", p, count++, 209 fit_get_name(fit, noffset, NULL)); 210 211 fit_image_print(fit, noffset, p); 212 } 213 } 214 215 /* Find configurations parent node offset */ 216 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 217 if (confs_noffset < 0) { 218 debug("Can't get configurations parent node '%s' (%s)\n", 219 FIT_CONFS_PATH, fdt_strerror(confs_noffset)); 220 return; 221 } 222 223 /* get default configuration unit name from default property */ 224 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL); 225 if (uname) 226 printf("%s Default Configuration: '%s'\n", p, uname); 227 228 /* Process its subnodes, print out configurations details */ 229 for (ndepth = 0, count = 0, 230 noffset = fdt_next_node(fit, confs_noffset, &ndepth); 231 (noffset >= 0) && (ndepth > 0); 232 noffset = fdt_next_node(fit, noffset, &ndepth)) { 233 if (ndepth == 1) { 234 /* 235 * Direct child node of the configurations parent node, 236 * i.e. configuration node. 237 */ 238 printf("%s Configuration %u (%s)\n", p, count++, 239 fit_get_name(fit, noffset, NULL)); 240 241 fit_conf_print(fit, noffset, p); 242 } 243 } 244 } 245 246 /** 247 * fit_image_print_data() - prints out the hash node details 248 * @fit: pointer to the FIT format image header 249 * @noffset: offset of the hash node 250 * @p: pointer to prefix string 251 * @type: Type of information to print ("hash" or "sign") 252 * 253 * fit_image_print_data() lists properties for the processed hash node 254 * 255 * This function avoid using puts() since it prints a newline on the host 256 * but does not in U-Boot. 257 * 258 * returns: 259 * no returned results 260 */ 261 static void fit_image_print_data(const void *fit, int noffset, const char *p, 262 const char *type) 263 { 264 const char *keyname; 265 uint8_t *value; 266 int value_len; 267 char *algo; 268 int required; 269 int ret, i; 270 271 debug("%s %s node: '%s'\n", p, type, 272 fit_get_name(fit, noffset, NULL)); 273 printf("%s %s algo: ", p, type); 274 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 275 printf("invalid/unsupported\n"); 276 return; 277 } 278 printf("%s", algo); 279 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); 280 required = fdt_getprop(fit, noffset, "required", NULL) != NULL; 281 if (keyname) 282 printf(":%s", keyname); 283 if (required) 284 printf(" (required)"); 285 printf("\n"); 286 287 ret = fit_image_hash_get_value(fit, noffset, &value, 288 &value_len); 289 printf("%s %s value: ", p, type); 290 if (ret) { 291 printf("unavailable\n"); 292 } else { 293 for (i = 0; i < value_len; i++) 294 printf("%02x", value[i]); 295 printf("\n"); 296 } 297 298 debug("%s %s len: %d\n", p, type, value_len); 299 300 /* Signatures have a time stamp */ 301 if (IMAGE_ENABLE_TIMESTAMP && keyname) { 302 time_t timestamp; 303 304 printf("%s Timestamp: ", p); 305 if (fit_get_timestamp(fit, noffset, ×tamp)) 306 printf("unavailable\n"); 307 else 308 genimg_print_time(timestamp); 309 } 310 } 311 312 /** 313 * fit_image_print_verification_data() - prints out the hash/signature details 314 * @fit: pointer to the FIT format image header 315 * @noffset: offset of the hash or signature node 316 * @p: pointer to prefix string 317 * 318 * This lists properties for the processed hash node 319 * 320 * returns: 321 * no returned results 322 */ 323 static void fit_image_print_verification_data(const void *fit, int noffset, 324 const char *p) 325 { 326 const char *name; 327 328 /* 329 * Check subnode name, must be equal to "hash" or "signature". 330 * Multiple hash/signature nodes require unique unit node 331 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc. 332 */ 333 name = fit_get_name(fit, noffset, NULL); 334 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) { 335 fit_image_print_data(fit, noffset, p, "Hash"); 336 } else if (!strncmp(name, FIT_SIG_NODENAME, 337 strlen(FIT_SIG_NODENAME))) { 338 fit_image_print_data(fit, noffset, p, "Sign"); 339 } 340 } 341 342 /** 343 * fit_image_print - prints out the FIT component image details 344 * @fit: pointer to the FIT format image header 345 * @image_noffset: offset of the component image node 346 * @p: pointer to prefix string 347 * 348 * fit_image_print() lists all mandatory properties for the processed component 349 * image. If present, hash nodes are printed out as well. Load 350 * address for images of type firmware is also printed out. Since the load 351 * address is not mandatory for firmware images, it will be output as 352 * "unavailable" when not present. 353 * 354 * returns: 355 * no returned results 356 */ 357 void fit_image_print(const void *fit, int image_noffset, const char *p) 358 { 359 char *desc; 360 uint8_t type, arch, os, comp; 361 size_t size; 362 ulong load, entry; 363 const void *data; 364 int noffset; 365 int ndepth; 366 int ret; 367 368 /* Mandatory properties */ 369 ret = fit_get_desc(fit, image_noffset, &desc); 370 printf("%s Description: ", p); 371 if (ret) 372 printf("unavailable\n"); 373 else 374 printf("%s\n", desc); 375 376 if (IMAGE_ENABLE_TIMESTAMP) { 377 time_t timestamp; 378 379 ret = fit_get_timestamp(fit, 0, ×tamp); 380 printf("%s Created: ", p); 381 if (ret) 382 printf("unavailable\n"); 383 else 384 genimg_print_time(timestamp); 385 } 386 387 fit_image_get_type(fit, image_noffset, &type); 388 printf("%s Type: %s\n", p, genimg_get_type_name(type)); 389 390 fit_image_get_comp(fit, image_noffset, &comp); 391 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp)); 392 393 ret = fit_image_get_data(fit, image_noffset, &data, &size); 394 395 #ifndef USE_HOSTCC 396 printf("%s Data Start: ", p); 397 if (ret) { 398 printf("unavailable\n"); 399 } else { 400 void *vdata = (void *)data; 401 402 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata)); 403 } 404 #endif 405 406 printf("%s Data Size: ", p); 407 if (ret) 408 printf("unavailable\n"); 409 else 410 genimg_print_size(size); 411 412 /* Remaining, type dependent properties */ 413 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 414 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) || 415 (type == IH_TYPE_FLATDT)) { 416 fit_image_get_arch(fit, image_noffset, &arch); 417 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch)); 418 } 419 420 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) { 421 fit_image_get_os(fit, image_noffset, &os); 422 printf("%s OS: %s\n", p, genimg_get_os_name(os)); 423 } 424 425 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 426 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) || 427 (type == IH_TYPE_FPGA)) { 428 ret = fit_image_get_load(fit, image_noffset, &load); 429 printf("%s Load Address: ", p); 430 if (ret) 431 printf("unavailable\n"); 432 else 433 printf("0x%08lx\n", load); 434 } 435 436 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 437 (type == IH_TYPE_RAMDISK)) { 438 ret = fit_image_get_entry(fit, image_noffset, &entry); 439 printf("%s Entry Point: ", p); 440 if (ret) 441 printf("unavailable\n"); 442 else 443 printf("0x%08lx\n", entry); 444 } 445 446 /* Process all hash subnodes of the component image node */ 447 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); 448 (noffset >= 0) && (ndepth > 0); 449 noffset = fdt_next_node(fit, noffset, &ndepth)) { 450 if (ndepth == 1) { 451 /* Direct child node of the component image node */ 452 fit_image_print_verification_data(fit, noffset, p); 453 } 454 } 455 } 456 457 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */ 458 459 /** 460 * fit_get_desc - get node description property 461 * @fit: pointer to the FIT format image header 462 * @noffset: node offset 463 * @desc: double pointer to the char, will hold pointer to the description 464 * 465 * fit_get_desc() reads description property from a given node, if 466 * description is found pointer to it is returned in third call argument. 467 * 468 * returns: 469 * 0, on success 470 * -1, on failure 471 */ 472 int fit_get_desc(const void *fit, int noffset, char **desc) 473 { 474 int len; 475 476 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len); 477 if (*desc == NULL) { 478 fit_get_debug(fit, noffset, FIT_DESC_PROP, len); 479 return -1; 480 } 481 482 return 0; 483 } 484 485 /** 486 * fit_get_timestamp - get node timestamp property 487 * @fit: pointer to the FIT format image header 488 * @noffset: node offset 489 * @timestamp: pointer to the time_t, will hold read timestamp 490 * 491 * fit_get_timestamp() reads timestamp property from given node, if timestamp 492 * is found and has a correct size its value is returned in third call 493 * argument. 494 * 495 * returns: 496 * 0, on success 497 * -1, on property read failure 498 * -2, on wrong timestamp size 499 */ 500 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp) 501 { 502 int len; 503 const void *data; 504 505 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len); 506 if (data == NULL) { 507 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len); 508 return -1; 509 } 510 if (len != sizeof(uint32_t)) { 511 debug("FIT timestamp with incorrect size of (%u)\n", len); 512 return -2; 513 } 514 515 *timestamp = uimage_to_cpu(*((uint32_t *)data)); 516 return 0; 517 } 518 519 /** 520 * fit_image_get_node - get node offset for component image of a given unit name 521 * @fit: pointer to the FIT format image header 522 * @image_uname: component image node unit name 523 * 524 * fit_image_get_node() finds a component image (within the '/images' 525 * node) of a provided unit name. If image is found its node offset is 526 * returned to the caller. 527 * 528 * returns: 529 * image node offset when found (>=0) 530 * negative number on failure (FDT_ERR_* code) 531 */ 532 int fit_image_get_node(const void *fit, const char *image_uname) 533 { 534 int noffset, images_noffset; 535 536 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 537 if (images_noffset < 0) { 538 debug("Can't find images parent node '%s' (%s)\n", 539 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 540 return images_noffset; 541 } 542 543 noffset = fdt_subnode_offset(fit, images_noffset, image_uname); 544 if (noffset < 0) { 545 debug("Can't get node offset for image unit name: '%s' (%s)\n", 546 image_uname, fdt_strerror(noffset)); 547 } 548 549 return noffset; 550 } 551 552 /** 553 * fit_image_get_os - get os id for a given component image node 554 * @fit: pointer to the FIT format image header 555 * @noffset: component image node offset 556 * @os: pointer to the uint8_t, will hold os numeric id 557 * 558 * fit_image_get_os() finds os property in a given component image node. 559 * If the property is found, its (string) value is translated to the numeric 560 * id which is returned to the caller. 561 * 562 * returns: 563 * 0, on success 564 * -1, on failure 565 */ 566 int fit_image_get_os(const void *fit, int noffset, uint8_t *os) 567 { 568 int len; 569 const void *data; 570 571 /* Get OS name from property data */ 572 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len); 573 if (data == NULL) { 574 fit_get_debug(fit, noffset, FIT_OS_PROP, len); 575 *os = -1; 576 return -1; 577 } 578 579 /* Translate OS name to id */ 580 *os = genimg_get_os_id(data); 581 return 0; 582 } 583 584 /** 585 * fit_image_get_arch - get arch id for a given component image node 586 * @fit: pointer to the FIT format image header 587 * @noffset: component image node offset 588 * @arch: pointer to the uint8_t, will hold arch numeric id 589 * 590 * fit_image_get_arch() finds arch property in a given component image node. 591 * If the property is found, its (string) value is translated to the numeric 592 * id which is returned to the caller. 593 * 594 * returns: 595 * 0, on success 596 * -1, on failure 597 */ 598 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch) 599 { 600 int len; 601 const void *data; 602 603 /* Get architecture name from property data */ 604 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len); 605 if (data == NULL) { 606 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len); 607 *arch = -1; 608 return -1; 609 } 610 611 /* Translate architecture name to id */ 612 *arch = genimg_get_arch_id(data); 613 return 0; 614 } 615 616 /** 617 * fit_image_get_type - get type id for a given component image node 618 * @fit: pointer to the FIT format image header 619 * @noffset: component image node offset 620 * @type: pointer to the uint8_t, will hold type numeric id 621 * 622 * fit_image_get_type() finds type property in a given component image node. 623 * If the property is found, its (string) value is translated to the numeric 624 * id which is returned to the caller. 625 * 626 * returns: 627 * 0, on success 628 * -1, on failure 629 */ 630 int fit_image_get_type(const void *fit, int noffset, uint8_t *type) 631 { 632 int len; 633 const void *data; 634 635 /* Get image type name from property data */ 636 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len); 637 if (data == NULL) { 638 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len); 639 *type = -1; 640 return -1; 641 } 642 643 /* Translate image type name to id */ 644 *type = genimg_get_type_id(data); 645 return 0; 646 } 647 648 /** 649 * fit_image_get_comp - get comp id for a given component image node 650 * @fit: pointer to the FIT format image header 651 * @noffset: component image node offset 652 * @comp: pointer to the uint8_t, will hold comp numeric id 653 * 654 * fit_image_get_comp() finds comp property in a given component image node. 655 * If the property is found, its (string) value is translated to the numeric 656 * id which is returned to the caller. 657 * 658 * returns: 659 * 0, on success 660 * -1, on failure 661 */ 662 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp) 663 { 664 int len; 665 const void *data; 666 667 /* Get compression name from property data */ 668 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len); 669 if (data == NULL) { 670 fit_get_debug(fit, noffset, FIT_COMP_PROP, len); 671 *comp = -1; 672 return -1; 673 } 674 675 /* Translate compression name to id */ 676 *comp = genimg_get_comp_id(data); 677 return 0; 678 } 679 680 static int fit_image_get_address(const void *fit, int noffset, char *name, 681 ulong *load) 682 { 683 int len, cell_len; 684 const fdt32_t *cell; 685 uint64_t load64 = 0; 686 687 cell = fdt_getprop(fit, noffset, name, &len); 688 if (cell == NULL) { 689 fit_get_debug(fit, noffset, name, len); 690 return -1; 691 } 692 693 if (len > sizeof(ulong)) { 694 printf("Unsupported %s address size\n", name); 695 return -1; 696 } 697 698 cell_len = len >> 2; 699 /* Use load64 to avoid compiling warning for 32-bit target */ 700 while (cell_len--) { 701 load64 = (load64 << 32) | uimage_to_cpu(*cell); 702 cell++; 703 } 704 *load = (ulong)load64; 705 706 return 0; 707 } 708 /** 709 * fit_image_get_load() - get load addr property for given component image node 710 * @fit: pointer to the FIT format image header 711 * @noffset: component image node offset 712 * @load: pointer to the uint32_t, will hold load address 713 * 714 * fit_image_get_load() finds load address property in a given component 715 * image node. If the property is found, its value is returned to the caller. 716 * 717 * returns: 718 * 0, on success 719 * -1, on failure 720 */ 721 int fit_image_get_load(const void *fit, int noffset, ulong *load) 722 { 723 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load); 724 } 725 726 /** 727 * fit_image_get_entry() - get entry point address property 728 * @fit: pointer to the FIT format image header 729 * @noffset: component image node offset 730 * @entry: pointer to the uint32_t, will hold entry point address 731 * 732 * This gets the entry point address property for a given component image 733 * node. 734 * 735 * fit_image_get_entry() finds entry point address property in a given 736 * component image node. If the property is found, its value is returned 737 * to the caller. 738 * 739 * returns: 740 * 0, on success 741 * -1, on failure 742 */ 743 int fit_image_get_entry(const void *fit, int noffset, ulong *entry) 744 { 745 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry); 746 } 747 748 /** 749 * fit_image_get_data - get data property and its size for a given component image node 750 * @fit: pointer to the FIT format image header 751 * @noffset: component image node offset 752 * @data: double pointer to void, will hold data property's data address 753 * @size: pointer to size_t, will hold data property's data size 754 * 755 * fit_image_get_data() finds data property in a given component image node. 756 * If the property is found its data start address and size are returned to 757 * the caller. 758 * 759 * returns: 760 * 0, on success 761 * -1, on failure 762 */ 763 int fit_image_get_data(const void *fit, int noffset, 764 const void **data, size_t *size) 765 { 766 int len; 767 768 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len); 769 if (*data == NULL) { 770 fit_get_debug(fit, noffset, FIT_DATA_PROP, len); 771 *size = 0; 772 return -1; 773 } 774 775 *size = len; 776 return 0; 777 } 778 779 /** 780 * fit_image_hash_get_algo - get hash algorithm name 781 * @fit: pointer to the FIT format image header 782 * @noffset: hash node offset 783 * @algo: double pointer to char, will hold pointer to the algorithm name 784 * 785 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. 786 * If the property is found its data start address is returned to the caller. 787 * 788 * returns: 789 * 0, on success 790 * -1, on failure 791 */ 792 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo) 793 { 794 int len; 795 796 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len); 797 if (*algo == NULL) { 798 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len); 799 return -1; 800 } 801 802 return 0; 803 } 804 805 /** 806 * fit_image_hash_get_value - get hash value and length 807 * @fit: pointer to the FIT format image header 808 * @noffset: hash node offset 809 * @value: double pointer to uint8_t, will hold address of a hash value data 810 * @value_len: pointer to an int, will hold hash data length 811 * 812 * fit_image_hash_get_value() finds hash value property in a given hash node. 813 * If the property is found its data start address and size are returned to 814 * the caller. 815 * 816 * returns: 817 * 0, on success 818 * -1, on failure 819 */ 820 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, 821 int *value_len) 822 { 823 int len; 824 825 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len); 826 if (*value == NULL) { 827 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len); 828 *value_len = 0; 829 return -1; 830 } 831 832 *value_len = len; 833 return 0; 834 } 835 836 /** 837 * fit_image_hash_get_ignore - get hash ignore flag 838 * @fit: pointer to the FIT format image header 839 * @noffset: hash node offset 840 * @ignore: pointer to an int, will hold hash ignore flag 841 * 842 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node. 843 * If the property is found and non-zero, the hash algorithm is not verified by 844 * u-boot automatically. 845 * 846 * returns: 847 * 0, on ignore not found 848 * value, on ignore found 849 */ 850 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) 851 { 852 int len; 853 int *value; 854 855 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len); 856 if (value == NULL || len != sizeof(int)) 857 *ignore = 0; 858 else 859 *ignore = *value; 860 861 return 0; 862 } 863 864 ulong fit_get_end(const void *fit) 865 { 866 return map_to_sysmem((void *)(fit + fdt_totalsize(fit))); 867 } 868 869 /** 870 * fit_set_timestamp - set node timestamp property 871 * @fit: pointer to the FIT format image header 872 * @noffset: node offset 873 * @timestamp: timestamp value to be set 874 * 875 * fit_set_timestamp() attempts to set timestamp property in the requested 876 * node and returns operation status to the caller. 877 * 878 * returns: 879 * 0, on success 880 * -ENOSPC if no space in device tree, -1 for other error 881 */ 882 int fit_set_timestamp(void *fit, int noffset, time_t timestamp) 883 { 884 uint32_t t; 885 int ret; 886 887 t = cpu_to_uimage(timestamp); 888 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, 889 sizeof(uint32_t)); 890 if (ret) { 891 debug("Can't set '%s' property for '%s' node (%s)\n", 892 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL), 893 fdt_strerror(ret)); 894 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1; 895 } 896 897 return 0; 898 } 899 900 /** 901 * calculate_hash - calculate and return hash for provided input data 902 * @data: pointer to the input data 903 * @data_len: data length 904 * @algo: requested hash algorithm 905 * @value: pointer to the char, will hold hash value data (caller must 906 * allocate enough free space) 907 * value_len: length of the calculated hash 908 * 909 * calculate_hash() computes input data hash according to the requested 910 * algorithm. 911 * Resulting hash value is placed in caller provided 'value' buffer, length 912 * of the calculated hash is returned via value_len pointer argument. 913 * 914 * returns: 915 * 0, on success 916 * -1, when algo is unsupported 917 */ 918 int calculate_hash(const void *data, int data_len, const char *algo, 919 uint8_t *value, int *value_len) 920 { 921 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { 922 *((uint32_t *)value) = crc32_wd(0, data, data_len, 923 CHUNKSZ_CRC32); 924 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); 925 *value_len = 4; 926 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) { 927 sha1_csum_wd((unsigned char *)data, data_len, 928 (unsigned char *)value, CHUNKSZ_SHA1); 929 *value_len = 20; 930 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) { 931 sha256_csum_wd((unsigned char *)data, data_len, 932 (unsigned char *)value, CHUNKSZ_SHA256); 933 *value_len = SHA256_SUM_LEN; 934 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { 935 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); 936 *value_len = 16; 937 } else { 938 debug("Unsupported hash alogrithm\n"); 939 return -1; 940 } 941 return 0; 942 } 943 944 static int fit_image_check_hash(const void *fit, int noffset, const void *data, 945 size_t size, char **err_msgp) 946 { 947 uint8_t value[FIT_MAX_HASH_LEN]; 948 int value_len; 949 char *algo; 950 uint8_t *fit_value; 951 int fit_value_len; 952 int ignore; 953 954 *err_msgp = NULL; 955 956 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 957 *err_msgp = "Can't get hash algo property"; 958 return -1; 959 } 960 printf("%s", algo); 961 962 if (IMAGE_ENABLE_IGNORE) { 963 fit_image_hash_get_ignore(fit, noffset, &ignore); 964 if (ignore) { 965 printf("-skipped "); 966 return 0; 967 } 968 } 969 970 if (fit_image_hash_get_value(fit, noffset, &fit_value, 971 &fit_value_len)) { 972 *err_msgp = "Can't get hash value property"; 973 return -1; 974 } 975 976 if (calculate_hash(data, size, algo, value, &value_len)) { 977 *err_msgp = "Unsupported hash algorithm"; 978 return -1; 979 } 980 981 if (value_len != fit_value_len) { 982 *err_msgp = "Bad hash value len"; 983 return -1; 984 } else if (memcmp(value, fit_value, value_len) != 0) { 985 *err_msgp = "Bad hash value"; 986 return -1; 987 } 988 989 return 0; 990 } 991 992 /** 993 * fit_image_verify - verify data integrity 994 * @fit: pointer to the FIT format image header 995 * @image_noffset: component image node offset 996 * 997 * fit_image_verify() goes over component image hash nodes, 998 * re-calculates each data hash and compares with the value stored in hash 999 * node. 1000 * 1001 * returns: 1002 * 1, if all hashes are valid 1003 * 0, otherwise (or on error) 1004 */ 1005 int fit_image_verify(const void *fit, int image_noffset) 1006 { 1007 const void *data; 1008 size_t size; 1009 int noffset = 0; 1010 char *err_msg = ""; 1011 int verify_all = 1; 1012 int ret; 1013 1014 /* Get image data and data length */ 1015 if (fit_image_get_data(fit, image_noffset, &data, &size)) { 1016 err_msg = "Can't get image data/size"; 1017 goto error; 1018 } 1019 1020 /* Verify all required signatures */ 1021 if (IMAGE_ENABLE_VERIFY && 1022 fit_image_verify_required_sigs(fit, image_noffset, data, size, 1023 gd_fdt_blob(), &verify_all)) { 1024 err_msg = "Unable to verify required signature"; 1025 goto error; 1026 } 1027 1028 /* Process all hash subnodes of the component image node */ 1029 fdt_for_each_subnode(noffset, fit, image_noffset) { 1030 const char *name = fit_get_name(fit, noffset, NULL); 1031 1032 /* 1033 * Check subnode name, must be equal to "hash". 1034 * Multiple hash nodes require unique unit node 1035 * names, e.g. hash@1, hash@2, etc. 1036 */ 1037 if (!strncmp(name, FIT_HASH_NODENAME, 1038 strlen(FIT_HASH_NODENAME))) { 1039 if (fit_image_check_hash(fit, noffset, data, size, 1040 &err_msg)) 1041 goto error; 1042 puts("+ "); 1043 } else if (IMAGE_ENABLE_VERIFY && verify_all && 1044 !strncmp(name, FIT_SIG_NODENAME, 1045 strlen(FIT_SIG_NODENAME))) { 1046 ret = fit_image_check_sig(fit, noffset, data, 1047 size, -1, &err_msg); 1048 1049 /* 1050 * Show an indication on failure, but do not return 1051 * an error. Only keys marked 'required' can cause 1052 * an image validation failure. See the call to 1053 * fit_image_verify_required_sigs() above. 1054 */ 1055 if (ret) 1056 puts("- "); 1057 else 1058 puts("+ "); 1059 } 1060 } 1061 1062 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { 1063 err_msg = "Corrupted or truncated tree"; 1064 goto error; 1065 } 1066 1067 return 1; 1068 1069 error: 1070 printf(" error!\n%s for '%s' hash node in '%s' image node\n", 1071 err_msg, fit_get_name(fit, noffset, NULL), 1072 fit_get_name(fit, image_noffset, NULL)); 1073 return 0; 1074 } 1075 1076 /** 1077 * fit_all_image_verify - verify data integrity for all images 1078 * @fit: pointer to the FIT format image header 1079 * 1080 * fit_all_image_verify() goes over all images in the FIT and 1081 * for every images checks if all it's hashes are valid. 1082 * 1083 * returns: 1084 * 1, if all hashes of all images are valid 1085 * 0, otherwise (or on error) 1086 */ 1087 int fit_all_image_verify(const void *fit) 1088 { 1089 int images_noffset; 1090 int noffset; 1091 int ndepth; 1092 int count; 1093 1094 /* Find images parent node offset */ 1095 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 1096 if (images_noffset < 0) { 1097 printf("Can't find images parent node '%s' (%s)\n", 1098 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 1099 return 0; 1100 } 1101 1102 /* Process all image subnodes, check hashes for each */ 1103 printf("## Checking hash(es) for FIT Image at %08lx ...\n", 1104 (ulong)fit); 1105 for (ndepth = 0, count = 0, 1106 noffset = fdt_next_node(fit, images_noffset, &ndepth); 1107 (noffset >= 0) && (ndepth > 0); 1108 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1109 if (ndepth == 1) { 1110 /* 1111 * Direct child node of the images parent node, 1112 * i.e. component image node. 1113 */ 1114 printf(" Hash(es) for Image %u (%s): ", count, 1115 fit_get_name(fit, noffset, NULL)); 1116 count++; 1117 1118 if (!fit_image_verify(fit, noffset)) 1119 return 0; 1120 printf("\n"); 1121 } 1122 } 1123 return 1; 1124 } 1125 1126 /** 1127 * fit_image_check_os - check whether image node is of a given os type 1128 * @fit: pointer to the FIT format image header 1129 * @noffset: component image node offset 1130 * @os: requested image os 1131 * 1132 * fit_image_check_os() reads image os property and compares its numeric 1133 * id with the requested os. Comparison result is returned to the caller. 1134 * 1135 * returns: 1136 * 1 if image is of given os type 1137 * 0 otherwise (or on error) 1138 */ 1139 int fit_image_check_os(const void *fit, int noffset, uint8_t os) 1140 { 1141 uint8_t image_os; 1142 1143 if (fit_image_get_os(fit, noffset, &image_os)) 1144 return 0; 1145 return (os == image_os); 1146 } 1147 1148 /** 1149 * fit_image_check_arch - check whether image node is of a given arch 1150 * @fit: pointer to the FIT format image header 1151 * @noffset: component image node offset 1152 * @arch: requested imagearch 1153 * 1154 * fit_image_check_arch() reads image arch property and compares its numeric 1155 * id with the requested arch. Comparison result is returned to the caller. 1156 * 1157 * returns: 1158 * 1 if image is of given arch 1159 * 0 otherwise (or on error) 1160 */ 1161 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch) 1162 { 1163 uint8_t image_arch; 1164 1165 if (fit_image_get_arch(fit, noffset, &image_arch)) 1166 return 0; 1167 return (arch == image_arch) || 1168 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64); 1169 } 1170 1171 /** 1172 * fit_image_check_type - check whether image node is of a given type 1173 * @fit: pointer to the FIT format image header 1174 * @noffset: component image node offset 1175 * @type: requested image type 1176 * 1177 * fit_image_check_type() reads image type property and compares its numeric 1178 * id with the requested type. Comparison result is returned to the caller. 1179 * 1180 * returns: 1181 * 1 if image is of given type 1182 * 0 otherwise (or on error) 1183 */ 1184 int fit_image_check_type(const void *fit, int noffset, uint8_t type) 1185 { 1186 uint8_t image_type; 1187 1188 if (fit_image_get_type(fit, noffset, &image_type)) 1189 return 0; 1190 return (type == image_type); 1191 } 1192 1193 /** 1194 * fit_image_check_comp - check whether image node uses given compression 1195 * @fit: pointer to the FIT format image header 1196 * @noffset: component image node offset 1197 * @comp: requested image compression type 1198 * 1199 * fit_image_check_comp() reads image compression property and compares its 1200 * numeric id with the requested compression type. Comparison result is 1201 * returned to the caller. 1202 * 1203 * returns: 1204 * 1 if image uses requested compression 1205 * 0 otherwise (or on error) 1206 */ 1207 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp) 1208 { 1209 uint8_t image_comp; 1210 1211 if (fit_image_get_comp(fit, noffset, &image_comp)) 1212 return 0; 1213 return (comp == image_comp); 1214 } 1215 1216 /** 1217 * fit_check_format - sanity check FIT image format 1218 * @fit: pointer to the FIT format image header 1219 * 1220 * fit_check_format() runs a basic sanity FIT image verification. 1221 * Routine checks for mandatory properties, nodes, etc. 1222 * 1223 * returns: 1224 * 1, on success 1225 * 0, on failure 1226 */ 1227 int fit_check_format(const void *fit) 1228 { 1229 /* mandatory / node 'description' property */ 1230 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) { 1231 debug("Wrong FIT format: no description\n"); 1232 return 0; 1233 } 1234 1235 if (IMAGE_ENABLE_TIMESTAMP) { 1236 /* mandatory / node 'timestamp' property */ 1237 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { 1238 debug("Wrong FIT format: no timestamp\n"); 1239 return 0; 1240 } 1241 } 1242 1243 /* mandatory subimages parent '/images' node */ 1244 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) { 1245 debug("Wrong FIT format: no images parent node\n"); 1246 return 0; 1247 } 1248 1249 return 1; 1250 } 1251 1252 1253 /** 1254 * fit_conf_find_compat 1255 * @fit: pointer to the FIT format image header 1256 * @fdt: pointer to the device tree to compare against 1257 * 1258 * fit_conf_find_compat() attempts to find the configuration whose fdt is the 1259 * most compatible with the passed in device tree. 1260 * 1261 * Example: 1262 * 1263 * / o image-tree 1264 * |-o images 1265 * | |-o fdt@1 1266 * | |-o fdt@2 1267 * | 1268 * |-o configurations 1269 * |-o config@1 1270 * | |-fdt = fdt@1 1271 * | 1272 * |-o config@2 1273 * |-fdt = fdt@2 1274 * 1275 * / o U-Boot fdt 1276 * |-compatible = "foo,bar", "bim,bam" 1277 * 1278 * / o kernel fdt1 1279 * |-compatible = "foo,bar", 1280 * 1281 * / o kernel fdt2 1282 * |-compatible = "bim,bam", "baz,biz" 1283 * 1284 * Configuration 1 would be picked because the first string in U-Boot's 1285 * compatible list, "foo,bar", matches a compatible string in the root of fdt1. 1286 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1. 1287 * 1288 * returns: 1289 * offset to the configuration to use if one was found 1290 * -1 otherwise 1291 */ 1292 int fit_conf_find_compat(const void *fit, const void *fdt) 1293 { 1294 int ndepth = 0; 1295 int noffset, confs_noffset, images_noffset; 1296 const void *fdt_compat; 1297 int fdt_compat_len; 1298 int best_match_offset = 0; 1299 int best_match_pos = 0; 1300 1301 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 1302 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 1303 if (confs_noffset < 0 || images_noffset < 0) { 1304 debug("Can't find configurations or images nodes.\n"); 1305 return -1; 1306 } 1307 1308 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len); 1309 if (!fdt_compat) { 1310 debug("Fdt for comparison has no \"compatible\" property.\n"); 1311 return -1; 1312 } 1313 1314 /* 1315 * Loop over the configurations in the FIT image. 1316 */ 1317 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth); 1318 (noffset >= 0) && (ndepth > 0); 1319 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1320 const void *kfdt; 1321 const char *kfdt_name; 1322 int kfdt_noffset; 1323 const char *cur_fdt_compat; 1324 int len; 1325 size_t size; 1326 int i; 1327 1328 if (ndepth > 1) 1329 continue; 1330 1331 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len); 1332 if (!kfdt_name) { 1333 debug("No fdt property found.\n"); 1334 continue; 1335 } 1336 kfdt_noffset = fdt_subnode_offset(fit, images_noffset, 1337 kfdt_name); 1338 if (kfdt_noffset < 0) { 1339 debug("No image node named \"%s\" found.\n", 1340 kfdt_name); 1341 continue; 1342 } 1343 /* 1344 * Get a pointer to this configuration's fdt. 1345 */ 1346 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) { 1347 debug("Failed to get fdt \"%s\".\n", kfdt_name); 1348 continue; 1349 } 1350 1351 len = fdt_compat_len; 1352 cur_fdt_compat = fdt_compat; 1353 /* 1354 * Look for a match for each U-Boot compatibility string in 1355 * turn in this configuration's fdt. 1356 */ 1357 for (i = 0; len > 0 && 1358 (!best_match_offset || best_match_pos > i); i++) { 1359 int cur_len = strlen(cur_fdt_compat) + 1; 1360 1361 if (!fdt_node_check_compatible(kfdt, 0, 1362 cur_fdt_compat)) { 1363 best_match_offset = noffset; 1364 best_match_pos = i; 1365 break; 1366 } 1367 len -= cur_len; 1368 cur_fdt_compat += cur_len; 1369 } 1370 } 1371 if (!best_match_offset) { 1372 debug("No match found.\n"); 1373 return -1; 1374 } 1375 1376 return best_match_offset; 1377 } 1378 1379 /** 1380 * fit_conf_get_node - get node offset for configuration of a given unit name 1381 * @fit: pointer to the FIT format image header 1382 * @conf_uname: configuration node unit name 1383 * 1384 * fit_conf_get_node() finds a configuration (within the '/configurations' 1385 * parent node) of a provided unit name. If configuration is found its node 1386 * offset is returned to the caller. 1387 * 1388 * When NULL is provided in second argument fit_conf_get_node() will search 1389 * for a default configuration node instead. Default configuration node unit 1390 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations' 1391 * node. 1392 * 1393 * returns: 1394 * configuration node offset when found (>=0) 1395 * negative number on failure (FDT_ERR_* code) 1396 */ 1397 int fit_conf_get_node(const void *fit, const char *conf_uname) 1398 { 1399 int noffset, confs_noffset; 1400 int len; 1401 1402 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 1403 if (confs_noffset < 0) { 1404 debug("Can't find configurations parent node '%s' (%s)\n", 1405 FIT_CONFS_PATH, fdt_strerror(confs_noffset)); 1406 return confs_noffset; 1407 } 1408 1409 if (conf_uname == NULL) { 1410 /* get configuration unit name from the default property */ 1411 debug("No configuration specified, trying default...\n"); 1412 conf_uname = (char *)fdt_getprop(fit, confs_noffset, 1413 FIT_DEFAULT_PROP, &len); 1414 if (conf_uname == NULL) { 1415 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP, 1416 len); 1417 return len; 1418 } 1419 debug("Found default configuration: '%s'\n", conf_uname); 1420 } 1421 1422 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname); 1423 if (noffset < 0) { 1424 debug("Can't get node offset for configuration unit name: '%s' (%s)\n", 1425 conf_uname, fdt_strerror(noffset)); 1426 } 1427 1428 return noffset; 1429 } 1430 1431 int fit_conf_get_prop_node(const void *fit, int noffset, 1432 const char *prop_name) 1433 { 1434 char *uname; 1435 int len; 1436 1437 /* get kernel image unit name from configuration kernel property */ 1438 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len); 1439 if (uname == NULL) 1440 return len; 1441 1442 return fit_image_get_node(fit, uname); 1443 } 1444 1445 /** 1446 * fit_conf_print - prints out the FIT configuration details 1447 * @fit: pointer to the FIT format image header 1448 * @noffset: offset of the configuration node 1449 * @p: pointer to prefix string 1450 * 1451 * fit_conf_print() lists all mandatory properties for the processed 1452 * configuration node. 1453 * 1454 * returns: 1455 * no returned results 1456 */ 1457 void fit_conf_print(const void *fit, int noffset, const char *p) 1458 { 1459 char *desc; 1460 const char *uname; 1461 int ret; 1462 int loadables_index; 1463 1464 /* Mandatory properties */ 1465 ret = fit_get_desc(fit, noffset, &desc); 1466 printf("%s Description: ", p); 1467 if (ret) 1468 printf("unavailable\n"); 1469 else 1470 printf("%s\n", desc); 1471 1472 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL); 1473 printf("%s Kernel: ", p); 1474 if (uname == NULL) 1475 printf("unavailable\n"); 1476 else 1477 printf("%s\n", uname); 1478 1479 /* Optional properties */ 1480 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL); 1481 if (uname) 1482 printf("%s Init Ramdisk: %s\n", p, uname); 1483 1484 uname = fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL); 1485 if (uname) 1486 printf("%s FDT: %s\n", p, uname); 1487 1488 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL); 1489 if (uname) 1490 printf("%s FPGA: %s\n", p, uname); 1491 1492 /* Print out all of the specified loadables */ 1493 for (loadables_index = 0; 1494 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP, 1495 loadables_index, NULL), uname; 1496 loadables_index++) { 1497 if (loadables_index == 0) { 1498 printf("%s Loadables: ", p); 1499 } else { 1500 printf("%s ", p); 1501 } 1502 printf("%s\n", uname); 1503 } 1504 } 1505 1506 static int fit_image_select(const void *fit, int rd_noffset, int verify) 1507 { 1508 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS) 1509 const void *data; 1510 size_t size; 1511 int ret; 1512 #endif 1513 1514 fit_image_print(fit, rd_noffset, " "); 1515 1516 if (verify) { 1517 puts(" Verifying Hash Integrity ... "); 1518 if (!fit_image_verify(fit, rd_noffset)) { 1519 puts("Bad Data Hash\n"); 1520 return -EACCES; 1521 } 1522 puts("OK\n"); 1523 } 1524 1525 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS) 1526 ret = fit_image_get_data(fit, rd_noffset, &data, &size); 1527 if (ret) 1528 return ret; 1529 1530 /* perform any post-processing on the image data */ 1531 board_fit_image_post_process((void **)&data, &size); 1532 1533 /* 1534 * update U-Boot's understanding of the "data" property start address 1535 * and size according to the performed post-processing 1536 */ 1537 ret = fdt_setprop((void *)fit, rd_noffset, FIT_DATA_PROP, data, size); 1538 if (ret) 1539 return ret; 1540 #endif 1541 1542 return 0; 1543 } 1544 1545 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name, 1546 ulong addr) 1547 { 1548 int cfg_noffset; 1549 void *fit_hdr; 1550 int noffset; 1551 1552 debug("* %s: using config '%s' from image at 0x%08lx\n", 1553 prop_name, images->fit_uname_cfg, addr); 1554 1555 /* Check whether configuration has this property defined */ 1556 fit_hdr = map_sysmem(addr, 0); 1557 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg); 1558 if (cfg_noffset < 0) { 1559 debug("* %s: no such config\n", prop_name); 1560 return -EINVAL; 1561 } 1562 1563 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name); 1564 if (noffset < 0) { 1565 debug("* %s: no '%s' in config\n", prop_name, prop_name); 1566 return -ENOENT; 1567 } 1568 1569 return noffset; 1570 } 1571 1572 /** 1573 * fit_get_image_type_property() - get property name for IH_TYPE_... 1574 * 1575 * @return the properly name where we expect to find the image in the 1576 * config node 1577 */ 1578 static const char *fit_get_image_type_property(int type) 1579 { 1580 /* 1581 * This is sort-of available in the uimage_type[] table in image.c 1582 * but we don't have access to the short name, and "fdt" is different 1583 * anyway. So let's just keep it here. 1584 */ 1585 switch (type) { 1586 case IH_TYPE_FLATDT: 1587 return FIT_FDT_PROP; 1588 case IH_TYPE_KERNEL: 1589 return FIT_KERNEL_PROP; 1590 case IH_TYPE_RAMDISK: 1591 return FIT_RAMDISK_PROP; 1592 case IH_TYPE_X86_SETUP: 1593 return FIT_SETUP_PROP; 1594 case IH_TYPE_LOADABLE: 1595 return FIT_LOADABLE_PROP; 1596 case IH_TYPE_FPGA: 1597 return FIT_FPGA_PROP; 1598 } 1599 1600 return "unknown"; 1601 } 1602 1603 int fit_image_load(bootm_headers_t *images, ulong addr, 1604 const char **fit_unamep, const char **fit_uname_configp, 1605 int arch, int image_type, int bootstage_id, 1606 enum fit_load_op load_op, ulong *datap, ulong *lenp) 1607 { 1608 int cfg_noffset, noffset; 1609 const char *fit_uname; 1610 const char *fit_uname_config; 1611 const void *fit; 1612 const void *buf; 1613 size_t size; 1614 int type_ok, os_ok; 1615 ulong load, data, len; 1616 uint8_t os; 1617 const char *prop_name; 1618 int ret; 1619 1620 fit = map_sysmem(addr, 0); 1621 fit_uname = fit_unamep ? *fit_unamep : NULL; 1622 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL; 1623 prop_name = fit_get_image_type_property(image_type); 1624 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr); 1625 1626 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT); 1627 if (!fit_check_format(fit)) { 1628 printf("Bad FIT %s image format!\n", prop_name); 1629 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT); 1630 return -ENOEXEC; 1631 } 1632 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK); 1633 if (fit_uname) { 1634 /* get FIT component image node offset */ 1635 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME); 1636 noffset = fit_image_get_node(fit, fit_uname); 1637 } else { 1638 /* 1639 * no image node unit name, try to get config 1640 * node first. If config unit node name is NULL 1641 * fit_conf_get_node() will try to find default config node 1642 */ 1643 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME); 1644 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) { 1645 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob()); 1646 } else { 1647 cfg_noffset = fit_conf_get_node(fit, 1648 fit_uname_config); 1649 } 1650 if (cfg_noffset < 0) { 1651 puts("Could not find configuration node\n"); 1652 bootstage_error(bootstage_id + 1653 BOOTSTAGE_SUB_NO_UNIT_NAME); 1654 return -ENOENT; 1655 } 1656 fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL); 1657 printf(" Using '%s' configuration\n", fit_uname_config); 1658 if (image_type == IH_TYPE_KERNEL) { 1659 /* Remember (and possibly verify) this config */ 1660 images->fit_uname_cfg = fit_uname_config; 1661 if (IMAGE_ENABLE_VERIFY && images->verify) { 1662 puts(" Verifying Hash Integrity ... "); 1663 if (fit_config_verify(fit, cfg_noffset)) { 1664 puts("Bad Data Hash\n"); 1665 bootstage_error(bootstage_id + 1666 BOOTSTAGE_SUB_HASH); 1667 return -EACCES; 1668 } 1669 puts("OK\n"); 1670 } 1671 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG); 1672 } 1673 1674 noffset = fit_conf_get_prop_node(fit, cfg_noffset, 1675 prop_name); 1676 fit_uname = fit_get_name(fit, noffset, NULL); 1677 } 1678 if (noffset < 0) { 1679 puts("Could not find subimage node\n"); 1680 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE); 1681 return -ENOENT; 1682 } 1683 1684 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name); 1685 1686 ret = fit_image_select(fit, noffset, images->verify); 1687 if (ret) { 1688 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH); 1689 return ret; 1690 } 1691 1692 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); 1693 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX) 1694 if (!fit_image_check_target_arch(fit, noffset)) { 1695 puts("Unsupported Architecture\n"); 1696 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); 1697 return -ENOEXEC; 1698 } 1699 #endif 1700 if (image_type == IH_TYPE_FLATDT && 1701 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) { 1702 puts("FDT image is compressed"); 1703 return -EPROTONOSUPPORT; 1704 } 1705 1706 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); 1707 type_ok = fit_image_check_type(fit, noffset, image_type) || 1708 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) || 1709 (image_type == IH_TYPE_KERNEL && 1710 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD)); 1711 1712 os_ok = image_type == IH_TYPE_FLATDT || 1713 image_type == IH_TYPE_FPGA || 1714 fit_image_check_os(fit, noffset, IH_OS_LINUX) || 1715 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) || 1716 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS); 1717 1718 /* 1719 * If either of the checks fail, we should report an error, but 1720 * if the image type is coming from the "loadables" field, we 1721 * don't care what it is 1722 */ 1723 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) { 1724 fit_image_get_os(fit, noffset, &os); 1725 printf("No %s %s %s Image\n", 1726 genimg_get_os_name(os), 1727 genimg_get_arch_name(arch), 1728 genimg_get_type_name(image_type)); 1729 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); 1730 return -EIO; 1731 } 1732 1733 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK); 1734 1735 /* get image data address and length */ 1736 if (fit_image_get_data(fit, noffset, &buf, &size)) { 1737 printf("Could not find %s subimage data!\n", prop_name); 1738 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA); 1739 return -ENOENT; 1740 } 1741 len = (ulong)size; 1742 1743 /* verify that image data is a proper FDT blob */ 1744 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) { 1745 puts("Subimage data is not a FDT"); 1746 return -ENOEXEC; 1747 } 1748 1749 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK); 1750 1751 /* 1752 * Work-around for eldk-4.2 which gives this warning if we try to 1753 * cast in the unmap_sysmem() call: 1754 * warning: initialization discards qualifiers from pointer target type 1755 */ 1756 { 1757 void *vbuf = (void *)buf; 1758 1759 data = map_to_sysmem(vbuf); 1760 } 1761 1762 if (load_op == FIT_LOAD_IGNORED) { 1763 /* Don't load */ 1764 } else if (fit_image_get_load(fit, noffset, &load)) { 1765 if (load_op == FIT_LOAD_REQUIRED) { 1766 printf("Can't get %s subimage load address!\n", 1767 prop_name); 1768 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD); 1769 return -EBADF; 1770 } 1771 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) { 1772 ulong image_start, image_end; 1773 ulong load_end; 1774 void *dst; 1775 1776 /* 1777 * move image data to the load address, 1778 * make sure we don't overwrite initial image 1779 */ 1780 image_start = addr; 1781 image_end = addr + fit_get_size(fit); 1782 1783 load_end = load + len; 1784 if (image_type != IH_TYPE_KERNEL && 1785 load < image_end && load_end > image_start) { 1786 printf("Error: %s overwritten\n", prop_name); 1787 return -EXDEV; 1788 } 1789 1790 printf(" Loading %s from 0x%08lx to 0x%08lx\n", 1791 prop_name, data, load); 1792 1793 dst = map_sysmem(load, len); 1794 memmove(dst, buf, len); 1795 data = load; 1796 } 1797 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD); 1798 1799 *datap = data; 1800 *lenp = len; 1801 if (fit_unamep) 1802 *fit_unamep = (char *)fit_uname; 1803 if (fit_uname_configp) 1804 *fit_uname_configp = (char *)fit_uname_config; 1805 1806 return noffset; 1807 } 1808 1809 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch, 1810 ulong *setup_start, ulong *setup_len) 1811 { 1812 int noffset; 1813 ulong addr; 1814 ulong len; 1815 int ret; 1816 1817 addr = map_to_sysmem(images->fit_hdr_os); 1818 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr); 1819 if (noffset < 0) 1820 return noffset; 1821 1822 ret = fit_image_load(images, addr, NULL, NULL, arch, 1823 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START, 1824 FIT_LOAD_REQUIRED, setup_start, &len); 1825 1826 return ret; 1827 } 1828