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 <linux/kconfig.h> 18 #include <common.h> 19 #include <errno.h> 20 #include <mapmem.h> 21 #include <asm/io.h> 22 #include <malloc.h> 23 #include <crypto.h> 24 25 DECLARE_GLOBAL_DATA_PTR; 26 #endif /* !USE_HOSTCC*/ 27 28 #include <image.h> 29 #include <bootstage.h> 30 #include <u-boot/crc.h> 31 #include <u-boot/md5.h> 32 #include <u-boot/sha1.h> 33 #include <u-boot/sha256.h> 34 35 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 36 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 37 38 /*****************************************************************************/ 39 /* New uImage format routines */ 40 /*****************************************************************************/ 41 #ifndef USE_HOSTCC 42 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr, 43 ulong *addr, const char **name) 44 { 45 const char *sep; 46 47 *addr = addr_curr; 48 *name = NULL; 49 50 sep = strchr(spec, sepc); 51 if (sep) { 52 if (sep - spec > 0) 53 *addr = simple_strtoul(spec, NULL, 16); 54 55 *name = sep + 1; 56 return 1; 57 } 58 59 return 0; 60 } 61 62 /** 63 * fit_parse_conf - parse FIT configuration spec 64 * @spec: input string, containing configuration spec 65 * @add_curr: current image address (to be used as a possible default) 66 * @addr: pointer to a ulong variable, will hold FIT image address of a given 67 * configuration 68 * @conf_name double pointer to a char, will hold pointer to a configuration 69 * unit name 70 * 71 * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>, 72 * where <addr> is a FIT image address that contains configuration 73 * with a <conf> unit name. 74 * 75 * Address part is optional, and if omitted default add_curr will 76 * be used instead. 77 * 78 * returns: 79 * 1 if spec is a valid configuration string, 80 * addr and conf_name are set accordingly 81 * 0 otherwise 82 */ 83 int fit_parse_conf(const char *spec, ulong addr_curr, 84 ulong *addr, const char **conf_name) 85 { 86 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name); 87 } 88 89 /** 90 * fit_parse_subimage - parse FIT subimage spec 91 * @spec: input string, containing subimage spec 92 * @add_curr: current image address (to be used as a possible default) 93 * @addr: pointer to a ulong variable, will hold FIT image address of a given 94 * subimage 95 * @image_name: double pointer to a char, will hold pointer to a subimage name 96 * 97 * fit_parse_subimage() expects subimage spec in the form of 98 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains 99 * subimage with a <subimg> unit name. 100 * 101 * Address part is optional, and if omitted default add_curr will 102 * be used instead. 103 * 104 * returns: 105 * 1 if spec is a valid subimage string, 106 * addr and image_name are set accordingly 107 * 0 otherwise 108 */ 109 int fit_parse_subimage(const char *spec, ulong addr_curr, 110 ulong *addr, const char **image_name) 111 { 112 return fit_parse_spec(spec, ':', addr_curr, addr, image_name); 113 } 114 #endif /* !USE_HOSTCC */ 115 116 static void fit_get_debug(const void *fit, int noffset, 117 char *prop_name, int err) 118 { 119 debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n", 120 prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL), 121 fdt_strerror(err)); 122 } 123 124 /** 125 * fit_get_subimage_count - get component (sub-image) count 126 * @fit: pointer to the FIT format image header 127 * @images_noffset: offset of images node 128 * 129 * returns: 130 * number of image components 131 */ 132 int fit_get_subimage_count(const void *fit, int images_noffset) 133 { 134 int noffset; 135 int ndepth; 136 int count = 0; 137 138 /* Process its subnodes, print out component images details */ 139 for (ndepth = 0, count = 0, 140 noffset = fdt_next_node(fit, images_noffset, &ndepth); 141 (noffset >= 0) && (ndepth > 0); 142 noffset = fdt_next_node(fit, noffset, &ndepth)) { 143 if (ndepth == 1) { 144 count++; 145 } 146 } 147 148 return count; 149 } 150 151 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) 152 /** 153 * fit_print_contents - prints out the contents of the FIT format image 154 * @fit: pointer to the FIT format image header 155 * @p: pointer to prefix string 156 * 157 * fit_print_contents() formats a multi line FIT image contents description. 158 * The routine prints out FIT image properties (root node level) followed by 159 * the details of each component image. 160 * 161 * returns: 162 * no returned results 163 */ 164 void fit_print_contents(const void *fit) 165 { 166 char *desc; 167 char *uname; 168 int images_noffset; 169 int confs_noffset; 170 int noffset; 171 int ndepth; 172 int count = 0; 173 int ret; 174 const char *p; 175 time_t timestamp; 176 177 /* Indent string is defined in header image.h */ 178 p = IMAGE_INDENT_STRING; 179 180 /* Root node properties */ 181 ret = fit_get_desc(fit, 0, &desc); 182 printf("%sFIT description: ", p); 183 if (ret) 184 printf("unavailable\n"); 185 else 186 printf("%s\n", desc); 187 188 if (IMAGE_ENABLE_TIMESTAMP) { 189 ret = fit_get_timestamp(fit, 0, ×tamp); 190 printf("%sCreated: ", p); 191 if (ret) 192 printf("unavailable\n"); 193 else 194 genimg_print_time(timestamp); 195 } 196 197 /* Find images parent node offset */ 198 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 199 if (images_noffset < 0) { 200 printf("Can't find images parent node '%s' (%s)\n", 201 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 202 return; 203 } 204 205 /* Process its subnodes, print out component images details */ 206 for (ndepth = 0, count = 0, 207 noffset = fdt_next_node(fit, images_noffset, &ndepth); 208 (noffset >= 0) && (ndepth > 0); 209 noffset = fdt_next_node(fit, noffset, &ndepth)) { 210 if (ndepth == 1) { 211 /* 212 * Direct child node of the images parent node, 213 * i.e. component image node. 214 */ 215 printf("%s Image %u (%s)\n", p, count++, 216 fit_get_name(fit, noffset, NULL)); 217 218 fit_image_print(fit, noffset, p); 219 } 220 } 221 222 /* Find configurations parent node offset */ 223 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 224 if (confs_noffset < 0) { 225 debug("Can't get configurations parent node '%s' (%s)\n", 226 FIT_CONFS_PATH, fdt_strerror(confs_noffset)); 227 return; 228 } 229 230 /* get default configuration unit name from default property */ 231 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL); 232 if (uname) 233 printf("%s Default Configuration: '%s'\n", p, uname); 234 235 /* Process its subnodes, print out configurations details */ 236 for (ndepth = 0, count = 0, 237 noffset = fdt_next_node(fit, confs_noffset, &ndepth); 238 (noffset >= 0) && (ndepth > 0); 239 noffset = fdt_next_node(fit, noffset, &ndepth)) { 240 if (ndepth == 1) { 241 /* 242 * Direct child node of the configurations parent node, 243 * i.e. configuration node. 244 */ 245 printf("%s Configuration %u (%s)\n", p, count++, 246 fit_get_name(fit, noffset, NULL)); 247 248 fit_conf_print(fit, noffset, p); 249 } 250 } 251 } 252 253 /** 254 * fit_image_print_data() - prints out the hash node details 255 * @fit: pointer to the FIT format image header 256 * @noffset: offset of the hash node 257 * @p: pointer to prefix string 258 * @type: Type of information to print ("hash" or "sign") 259 * 260 * fit_image_print_data() lists properties for the processed hash node 261 * 262 * This function avoid using puts() since it prints a newline on the host 263 * but does not in U-Boot. 264 * 265 * returns: 266 * no returned results 267 */ 268 static void fit_image_print_data(const void *fit, int noffset, const char *p, 269 const char *type) 270 { 271 const char *keyname; 272 uint8_t *value; 273 int value_len; 274 char *algo; 275 int required; 276 int ret, i; 277 278 debug("%s %s node: '%s'\n", p, type, 279 fit_get_name(fit, noffset, NULL)); 280 printf("%s %s algo: ", p, type); 281 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 282 printf("invalid/unsupported\n"); 283 return; 284 } 285 printf("%s", algo); 286 keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL); 287 required = fdt_getprop(fit, noffset, "required", NULL) != NULL; 288 if (keyname) 289 printf(":%s", keyname); 290 if (required) 291 printf(" (required)"); 292 printf("\n"); 293 294 ret = fit_image_hash_get_value(fit, noffset, &value, 295 &value_len); 296 printf("%s %s value: ", p, type); 297 if (ret) { 298 printf("unavailable\n"); 299 } else { 300 for (i = 0; i < value_len; i++) 301 printf("%02x", value[i]); 302 printf("\n"); 303 } 304 305 debug("%s %s len: %d\n", p, type, value_len); 306 307 /* Signatures have a time stamp */ 308 if (IMAGE_ENABLE_TIMESTAMP && keyname) { 309 time_t timestamp; 310 311 printf("%s Timestamp: ", p); 312 if (fit_get_timestamp(fit, noffset, ×tamp)) 313 printf("unavailable\n"); 314 else 315 genimg_print_time(timestamp); 316 } 317 } 318 319 /** 320 * fit_image_print_verification_data() - prints out the hash/signature details 321 * @fit: pointer to the FIT format image header 322 * @noffset: offset of the hash or signature node 323 * @p: pointer to prefix string 324 * 325 * This lists properties for the processed hash node 326 * 327 * returns: 328 * no returned results 329 */ 330 static void fit_image_print_verification_data(const void *fit, int noffset, 331 const char *p) 332 { 333 const char *name; 334 335 /* 336 * Check subnode name, must be equal to "hash" or "signature". 337 * Multiple hash/signature nodes require unique unit node 338 * names, e.g. hash@1, hash@2, signature@1, signature@2, etc. 339 */ 340 name = fit_get_name(fit, noffset, NULL); 341 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) { 342 fit_image_print_data(fit, noffset, p, "Hash"); 343 } else if (!strncmp(name, FIT_SIG_NODENAME, 344 strlen(FIT_SIG_NODENAME))) { 345 fit_image_print_data(fit, noffset, p, "Sign"); 346 } 347 } 348 349 /** 350 * fit_image_print - prints out the FIT component image details 351 * @fit: pointer to the FIT format image header 352 * @image_noffset: offset of the component image node 353 * @p: pointer to prefix string 354 * 355 * fit_image_print() lists all mandatory properties for the processed component 356 * image. If present, hash nodes are printed out as well. Load 357 * address for images of type firmware is also printed out. Since the load 358 * address is not mandatory for firmware images, it will be output as 359 * "unavailable" when not present. 360 * 361 * returns: 362 * no returned results 363 */ 364 void fit_image_print(const void *fit, int image_noffset, const char *p) 365 { 366 char *desc; 367 uint8_t type, arch, os, comp; 368 size_t size; 369 ulong load, entry; 370 const void *data; 371 int noffset; 372 int ndepth; 373 int ret; 374 375 /* Mandatory properties */ 376 ret = fit_get_desc(fit, image_noffset, &desc); 377 printf("%s Description: ", p); 378 if (ret) 379 printf("unavailable\n"); 380 else 381 printf("%s\n", desc); 382 383 if (IMAGE_ENABLE_TIMESTAMP) { 384 time_t timestamp; 385 386 ret = fit_get_timestamp(fit, 0, ×tamp); 387 printf("%s Created: ", p); 388 if (ret) 389 printf("unavailable\n"); 390 else 391 genimg_print_time(timestamp); 392 } 393 394 fit_image_get_type(fit, image_noffset, &type); 395 printf("%s Type: %s\n", p, genimg_get_type_name(type)); 396 397 fit_image_get_comp(fit, image_noffset, &comp); 398 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp)); 399 400 ret = fit_image_get_data(fit, image_noffset, &data, &size); 401 402 #ifndef USE_HOSTCC 403 printf("%s Data Start: ", p); 404 if (ret) { 405 printf("unavailable\n"); 406 } else { 407 void *vdata = (void *)data; 408 409 printf("0x%08lx\n", (ulong)map_to_sysmem(vdata)); 410 } 411 #endif 412 413 printf("%s Data Size: ", p); 414 if (ret) 415 printf("unavailable\n"); 416 else 417 genimg_print_size(size); 418 419 /* Remaining, type dependent properties */ 420 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 421 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) || 422 (type == IH_TYPE_FLATDT)) { 423 fit_image_get_arch(fit, image_noffset, &arch); 424 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch)); 425 } 426 427 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) { 428 fit_image_get_os(fit, image_noffset, &os); 429 printf("%s OS: %s\n", p, genimg_get_os_name(os)); 430 } 431 432 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 433 (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) || 434 (type == IH_TYPE_FPGA)) { 435 ret = fit_image_get_load(fit, image_noffset, &load); 436 printf("%s Load Address: ", p); 437 if (ret) 438 printf("unavailable\n"); 439 else 440 printf("0x%08lx\n", load); 441 } 442 443 /* optional load address for FDT */ 444 if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load)) 445 printf("%s Load Address: 0x%08lx\n", p, load); 446 447 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) || 448 (type == IH_TYPE_RAMDISK)) { 449 ret = fit_image_get_entry(fit, image_noffset, &entry); 450 printf("%s Entry Point: ", p); 451 if (ret) 452 printf("unavailable\n"); 453 else 454 printf("0x%08lx\n", entry); 455 } 456 457 /* Process all hash subnodes of the component image node */ 458 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth); 459 (noffset >= 0) && (ndepth > 0); 460 noffset = fdt_next_node(fit, noffset, &ndepth)) { 461 if (ndepth == 1) { 462 /* Direct child node of the component image node */ 463 fit_image_print_verification_data(fit, noffset, p); 464 } 465 } 466 } 467 468 #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */ 469 470 /** 471 * fit_get_desc - get node description property 472 * @fit: pointer to the FIT format image header 473 * @noffset: node offset 474 * @desc: double pointer to the char, will hold pointer to the description 475 * 476 * fit_get_desc() reads description property from a given node, if 477 * description is found pointer to it is returned in third call argument. 478 * 479 * returns: 480 * 0, on success 481 * -1, on failure 482 */ 483 int fit_get_desc(const void *fit, int noffset, char **desc) 484 { 485 int len; 486 487 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len); 488 if (*desc == NULL) { 489 fit_get_debug(fit, noffset, FIT_DESC_PROP, len); 490 return -1; 491 } 492 493 return 0; 494 } 495 496 /** 497 * fit_get_timestamp - get node timestamp property 498 * @fit: pointer to the FIT format image header 499 * @noffset: node offset 500 * @timestamp: pointer to the time_t, will hold read timestamp 501 * 502 * fit_get_timestamp() reads timestamp property from given node, if timestamp 503 * is found and has a correct size its value is returned in third call 504 * argument. 505 * 506 * returns: 507 * 0, on success 508 * -1, on property read failure 509 * -2, on wrong timestamp size 510 */ 511 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp) 512 { 513 int len; 514 const void *data; 515 516 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len); 517 if (data == NULL) { 518 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len); 519 return -1; 520 } 521 if (len != sizeof(uint32_t)) { 522 debug("FIT timestamp with incorrect size of (%u)\n", len); 523 return -2; 524 } 525 526 *timestamp = uimage_to_cpu(*((uint32_t *)data)); 527 return 0; 528 } 529 530 /** 531 * fit_image_get_node - get node offset for component image of a given unit name 532 * @fit: pointer to the FIT format image header 533 * @image_uname: component image node unit name 534 * 535 * fit_image_get_node() finds a component image (within the '/images' 536 * node) of a provided unit name. If image is found its node offset is 537 * returned to the caller. 538 * 539 * returns: 540 * image node offset when found (>=0) 541 * negative number on failure (FDT_ERR_* code) 542 */ 543 int fit_image_get_node(const void *fit, const char *image_uname) 544 { 545 int noffset, images_noffset; 546 547 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 548 if (images_noffset < 0) { 549 debug("Can't find images parent node '%s' (%s)\n", 550 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 551 return images_noffset; 552 } 553 554 noffset = fdt_subnode_offset(fit, images_noffset, image_uname); 555 if (noffset < 0) { 556 debug("Can't get node offset for image unit name: '%s' (%s)\n", 557 image_uname, fdt_strerror(noffset)); 558 } 559 560 return noffset; 561 } 562 563 /** 564 * fit_image_get_os - get os id for a given component image node 565 * @fit: pointer to the FIT format image header 566 * @noffset: component image node offset 567 * @os: pointer to the uint8_t, will hold os numeric id 568 * 569 * fit_image_get_os() finds os property in a given component image node. 570 * If the property is found, its (string) value is translated to the numeric 571 * id which is returned to the caller. 572 * 573 * returns: 574 * 0, on success 575 * -1, on failure 576 */ 577 int fit_image_get_os(const void *fit, int noffset, uint8_t *os) 578 { 579 int len; 580 const void *data; 581 582 /* Get OS name from property data */ 583 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len); 584 if (data == NULL) { 585 fit_get_debug(fit, noffset, FIT_OS_PROP, len); 586 *os = -1; 587 return -1; 588 } 589 590 /* Translate OS name to id */ 591 *os = genimg_get_os_id(data); 592 return 0; 593 } 594 595 /** 596 * fit_image_get_arch - get arch id for a given component image node 597 * @fit: pointer to the FIT format image header 598 * @noffset: component image node offset 599 * @arch: pointer to the uint8_t, will hold arch numeric id 600 * 601 * fit_image_get_arch() finds arch property in a given component image node. 602 * If the property is found, its (string) value is translated to the numeric 603 * id which is returned to the caller. 604 * 605 * returns: 606 * 0, on success 607 * -1, on failure 608 */ 609 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch) 610 { 611 int len; 612 const void *data; 613 614 /* Get architecture name from property data */ 615 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len); 616 if (data == NULL) { 617 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len); 618 *arch = -1; 619 return -1; 620 } 621 622 /* Translate architecture name to id */ 623 *arch = genimg_get_arch_id(data); 624 return 0; 625 } 626 627 /** 628 * fit_image_get_type - get type id for a given component image node 629 * @fit: pointer to the FIT format image header 630 * @noffset: component image node offset 631 * @type: pointer to the uint8_t, will hold type numeric id 632 * 633 * fit_image_get_type() finds type property in a given component image node. 634 * If the property is found, its (string) value is translated to the numeric 635 * id which is returned to the caller. 636 * 637 * returns: 638 * 0, on success 639 * -1, on failure 640 */ 641 int fit_image_get_type(const void *fit, int noffset, uint8_t *type) 642 { 643 int len; 644 const void *data; 645 646 /* Get image type name from property data */ 647 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len); 648 if (data == NULL) { 649 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len); 650 *type = -1; 651 return -1; 652 } 653 654 /* Translate image type name to id */ 655 *type = genimg_get_type_id(data); 656 return 0; 657 } 658 659 /** 660 * fit_image_get_comp - get comp id for a given component image node 661 * @fit: pointer to the FIT format image header 662 * @noffset: component image node offset 663 * @comp: pointer to the uint8_t, will hold comp numeric id 664 * 665 * fit_image_get_comp() finds comp property in a given component image node. 666 * If the property is found, its (string) value is translated to the numeric 667 * id which is returned to the caller. 668 * 669 * returns: 670 * 0, on success 671 * -1, on failure 672 */ 673 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp) 674 { 675 int len; 676 const void *data; 677 678 /* Get compression name from property data */ 679 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len); 680 if (data == NULL) { 681 fit_get_debug(fit, noffset, FIT_COMP_PROP, len); 682 *comp = -1; 683 return -1; 684 } 685 686 /* Translate compression name to id */ 687 *comp = genimg_get_comp_id(data); 688 return 0; 689 } 690 691 static int fit_image_get_address(const void *fit, int noffset, char *name, 692 ulong *load) 693 { 694 int len, cell_len; 695 const fdt32_t *cell; 696 uint64_t load64 = 0; 697 698 cell = fdt_getprop(fit, noffset, name, &len); 699 if (cell == NULL) { 700 fit_get_debug(fit, noffset, name, len); 701 return -1; 702 } 703 704 if (len > sizeof(ulong)) { 705 printf("Unsupported %s address size\n", name); 706 return -1; 707 } 708 709 cell_len = len >> 2; 710 /* Use load64 to avoid compiling warning for 32-bit target */ 711 while (cell_len--) { 712 load64 = (load64 << 32) | uimage_to_cpu(*cell); 713 cell++; 714 } 715 *load = (ulong)load64; 716 717 return 0; 718 } 719 720 static int fit_image_set_address(const void *fit, int noffset, char *name, 721 ulong addr) 722 { 723 int len, cell_len; 724 const fdt32_t *cell; 725 726 cell = fdt_getprop(fit, noffset, name, &len); 727 if (cell == NULL) { 728 fit_get_debug(fit, noffset, name, len); 729 return -1; 730 } 731 732 if (len > sizeof(ulong)) { 733 printf("Unsupported %s address size\n", name); 734 return -1; 735 } 736 737 cell_len = len >> 2; 738 /* Use load64 to avoid compiling warning for 32-bit target */ 739 while (cell_len--) { 740 *(fdt32_t *)cell = cpu_to_uimage(addr >> (32 * cell_len)); 741 cell++; 742 } 743 744 return 0; 745 } 746 747 /** 748 * fit_image_get_load() - get load addr property for given component image node 749 * @fit: pointer to the FIT format image header 750 * @noffset: component image node offset 751 * @load: pointer to the uint32_t, will hold load address 752 * 753 * fit_image_get_load() finds load address property in a given component 754 * image node. If the property is found, its value is returned to the caller. 755 * 756 * returns: 757 * 0, on success 758 * -1, on failure 759 */ 760 int fit_image_get_load(const void *fit, int noffset, ulong *load) 761 { 762 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load); 763 } 764 765 /** 766 * fit_image_set_load() - set load addr property for given component image node 767 * @fit: pointer to the FIT format image header 768 * @noffset: component image node offset 769 * @load: uint32_t value, will hold load address 770 * 771 * fit_image_set_load() finds and set load address property in a given component 772 * image node. If the property is found, its value is returned to the caller. 773 * 774 * returns: 775 * 0, on success 776 * -1, on failure 777 */ 778 int fit_image_set_load(const void *fit, int noffset, ulong load) 779 { 780 return fit_image_set_address(fit, noffset, FIT_LOAD_PROP, load); 781 } 782 783 /** 784 * fit_image_get_entry() - get entry point address property 785 * @fit: pointer to the FIT format image header 786 * @noffset: component image node offset 787 * @entry: pointer to the uint32_t, will hold entry point address 788 * 789 * This gets the entry point address property for a given component image 790 * node. 791 * 792 * fit_image_get_entry() finds entry point address property in a given 793 * component image node. If the property is found, its value is returned 794 * to the caller. 795 * 796 * returns: 797 * 0, on success 798 * -1, on failure 799 */ 800 int fit_image_get_entry(const void *fit, int noffset, ulong *entry) 801 { 802 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry); 803 } 804 805 /** 806 * fit_image_set_entry() - set entry point address property 807 * @fit: pointer to the FIT format image header 808 * @noffset: component image node offset 809 * @entry: uint32_t value, will hold entry point address 810 * 811 * This sets the entry point address property for a given component image 812 * node. 813 * 814 * fit_image_set_entry() finds and set entry point address property in a given 815 * component image node. If the property is found, its value is returned 816 * to the caller. 817 * 818 * returns: 819 * 0, on success 820 * -1, on failure 821 */ 822 int fit_image_set_entry(const void *fit, int noffset, ulong entry) 823 { 824 return fit_image_set_address(fit, noffset, FIT_ENTRY_PROP, entry); 825 } 826 827 /** 828 * fit_image_get_data - get data property and its size for a given component image node 829 * @fit: pointer to the FIT format image header 830 * @noffset: component image node offset 831 * @data: double pointer to void, will hold data property's data address 832 * @size: pointer to size_t, will hold data property's data size 833 * 834 * fit_image_get_data() finds data property in a given component image node. 835 * If the property is found its data start address and size are returned to 836 * the caller. 837 * 838 * returns: 839 * 0, on success 840 * -1, on failure 841 */ 842 int fit_image_get_data(const void *fit, int noffset, 843 const void **data, size_t *size) 844 { 845 ulong data_off = 0; 846 ulong data_pos = 0; 847 int len; 848 849 /* data */ 850 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len); 851 if (*data) { 852 *size = len; 853 return 0; 854 } 855 856 /* data-size */ 857 if (fit_image_get_data_size(fit, noffset, &len)) 858 return -ENOENT; 859 860 /* data-offset */ 861 if (!fit_image_get_data_offset(fit, noffset, (int *)&data_off)) { 862 data_off += (ulong)fit + FIT_ALIGN(fdt_totalsize(fit)); 863 *data = (void *)data_off; 864 *size = len; 865 return 0; 866 } 867 868 /* data-position */ 869 if (!fit_image_get_data_position(fit, noffset, (int *)&data_pos)) { 870 *data = (void *)(data_pos + (ulong)fit); 871 *size = len; 872 return 0; 873 } 874 875 *size = 0; 876 return -1; 877 } 878 879 /** 880 * Get 'data-offset' property from a given image node. 881 * 882 * @fit: pointer to the FIT image header 883 * @noffset: component image node offset 884 * @data_offset: holds the data-offset property 885 * 886 * returns: 887 * 0, on success 888 * -ENOENT if the property could not be found 889 */ 890 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset) 891 { 892 const fdt32_t *val; 893 894 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL); 895 if (!val) 896 return -ENOENT; 897 898 *data_offset = fdt32_to_cpu(*val); 899 900 return 0; 901 } 902 903 /** 904 * Get 'data-position' property from a given image node. 905 * 906 * @fit: pointer to the FIT image header 907 * @noffset: component image node offset 908 * @data_position: holds the data-position property 909 * 910 * returns: 911 * 0, on success 912 * -ENOENT if the property could not be found 913 */ 914 int fit_image_get_data_position(const void *fit, int noffset, 915 int *data_position) 916 { 917 const fdt32_t *val; 918 919 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL); 920 if (!val) 921 return -ENOENT; 922 923 *data_position = fdt32_to_cpu(*val); 924 925 return 0; 926 } 927 928 /** 929 * Get 'data-size' property from a given image node. 930 * 931 * @fit: pointer to the FIT image header 932 * @noffset: component image node offset 933 * @data_size: holds the data-size property 934 * 935 * returns: 936 * 0, on success 937 * -ENOENT if the property could not be found 938 */ 939 int fit_image_get_data_size(const void *fit, int noffset, int *data_size) 940 { 941 const fdt32_t *val; 942 943 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL); 944 if (!val) 945 return -ENOENT; 946 947 *data_size = fdt32_to_cpu(*val); 948 949 return 0; 950 } 951 952 /** 953 * Get 'rollback-index' property from a given image node. 954 * 955 * @fit: pointer to the FIT image header 956 * @noffset: component image node offset 957 * @index: holds the rollback-index property 958 * 959 * returns: 960 * 0, on success 961 * -ENOENT if the property could not be found 962 */ 963 int fit_image_get_rollback_index(const void *fit, int noffset, uint32_t *index) 964 { 965 const fdt32_t *val; 966 967 val = fdt_getprop(fit, noffset, FIT_ROLLBACK_PROP, NULL); 968 if (!val) 969 return -ENOENT; 970 971 *index = fdt32_to_cpu(*val); 972 973 return 0; 974 } 975 976 /** 977 * fit_image_hash_get_algo - get hash algorithm name 978 * @fit: pointer to the FIT format image header 979 * @noffset: hash node offset 980 * @algo: double pointer to char, will hold pointer to the algorithm name 981 * 982 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. 983 * If the property is found its data start address is returned to the caller. 984 * 985 * returns: 986 * 0, on success 987 * -1, on failure 988 */ 989 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo) 990 { 991 int len; 992 993 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len); 994 if (*algo == NULL) { 995 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len); 996 return -1; 997 } 998 999 return 0; 1000 } 1001 1002 /** 1003 * fit_image_hash_get_value - get hash value and length 1004 * @fit: pointer to the FIT format image header 1005 * @noffset: hash node offset 1006 * @value: double pointer to uint8_t, will hold address of a hash value data 1007 * @value_len: pointer to an int, will hold hash data length 1008 * 1009 * fit_image_hash_get_value() finds hash value property in a given hash node. 1010 * If the property is found its data start address and size are returned to 1011 * the caller. 1012 * 1013 * returns: 1014 * 0, on success 1015 * -1, on failure 1016 */ 1017 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, 1018 int *value_len) 1019 { 1020 int len; 1021 1022 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len); 1023 if (*value == NULL) { 1024 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len); 1025 *value_len = 0; 1026 return -1; 1027 } 1028 1029 *value_len = len; 1030 return 0; 1031 } 1032 1033 /** 1034 * fit_image_hash_get_ignore - get hash ignore flag 1035 * @fit: pointer to the FIT format image header 1036 * @noffset: hash node offset 1037 * @ignore: pointer to an int, will hold hash ignore flag 1038 * 1039 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node. 1040 * If the property is found and non-zero, the hash algorithm is not verified by 1041 * u-boot automatically. 1042 * 1043 * returns: 1044 * 0, on ignore not found 1045 * value, on ignore found 1046 */ 1047 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) 1048 { 1049 int len; 1050 int *value; 1051 1052 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len); 1053 if (value == NULL || len != sizeof(int)) 1054 *ignore = 0; 1055 else 1056 *ignore = *value; 1057 1058 return 0; 1059 } 1060 1061 ulong fit_get_end(const void *fit) 1062 { 1063 return map_to_sysmem((void *)(fit + fdt_totalsize(fit))); 1064 } 1065 1066 /** 1067 * fit_set_timestamp - set node timestamp property 1068 * @fit: pointer to the FIT format image header 1069 * @noffset: node offset 1070 * @timestamp: timestamp value to be set 1071 * 1072 * fit_set_timestamp() attempts to set timestamp property in the requested 1073 * node and returns operation status to the caller. 1074 * 1075 * returns: 1076 * 0, on success 1077 * -ENOSPC if no space in device tree, -1 for other error 1078 */ 1079 int fit_set_timestamp(void *fit, int noffset, time_t timestamp) 1080 { 1081 uint32_t t; 1082 int ret; 1083 1084 t = cpu_to_uimage(timestamp); 1085 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, 1086 sizeof(uint32_t)); 1087 if (ret) { 1088 debug("Can't set '%s' property for '%s' node (%s)\n", 1089 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL), 1090 fdt_strerror(ret)); 1091 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1; 1092 } 1093 1094 return 0; 1095 } 1096 1097 int fit_set_totalsize(void *fit, int noffset, int totalsize) 1098 { 1099 uint32_t t; 1100 int ret; 1101 1102 t = cpu_to_uimage(totalsize); 1103 ret = fdt_setprop(fit, noffset, FIT_TOTALSIZE_PROP, &t, 1104 sizeof(uint32_t)); 1105 if (ret) 1106 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1; 1107 1108 return 0; 1109 } 1110 1111 /** 1112 * calculate_hash - calculate and return hash for provided input data 1113 * @data: pointer to the input data 1114 * @data_len: data length 1115 * @algo: requested hash algorithm 1116 * @value: pointer to the char, will hold hash value data (caller must 1117 * allocate enough free space) 1118 * value_len: length of the calculated hash 1119 * 1120 * calculate_hash() computes input data hash according to the requested 1121 * algorithm. 1122 * Resulting hash value is placed in caller provided 'value' buffer, length 1123 * of the calculated hash is returned via value_len pointer argument. 1124 * 1125 * returns: 1126 * 0, on success 1127 * -1, when algo is unsupported 1128 */ 1129 int calculate_hash_software(const void *data, int data_len, 1130 const char *algo, uint8_t *value, 1131 int *value_len) 1132 { 1133 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { 1134 *((uint32_t *)value) = crc32_wd(0, data, data_len, 1135 CHUNKSZ_CRC32); 1136 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); 1137 *value_len = 4; 1138 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) { 1139 sha1_csum_wd((unsigned char *)data, data_len, 1140 (unsigned char *)value, CHUNKSZ_SHA1); 1141 *value_len = 20; 1142 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) { 1143 sha256_csum_wd((unsigned char *)data, data_len, 1144 (unsigned char *)value, CHUNKSZ_SHA256); 1145 *value_len = SHA256_SUM_LEN; 1146 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { 1147 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); 1148 *value_len = 16; 1149 } else { 1150 debug("Unsupported hash alogrithm\n"); 1151 return -1; 1152 } 1153 return 0; 1154 } 1155 1156 #ifdef USE_HOSTCC 1157 int calculate_hash(const void *data, int data_len, const char *algo, 1158 uint8_t *value, int *value_len) 1159 { 1160 return calculate_hash_software(data, data_len, algo, value, value_len); 1161 } 1162 #else 1163 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 1164 static int crypto_csum(u32 cap, const char *data, int len, u8 *output) 1165 { 1166 struct udevice *dev; 1167 sha_context csha_ctx; 1168 1169 dev = crypto_get_device(cap); 1170 if (!dev) { 1171 printf("Can't find expected crypto device\n"); 1172 return -ENODEV; 1173 } 1174 1175 csha_ctx.algo = cap; 1176 csha_ctx.length = len; 1177 1178 return crypto_sha_csum(dev, &csha_ctx, (char *)data, len, output); 1179 } 1180 1181 int calculate_hash(const void *data, int data_len, const char *algo, 1182 uint8_t *value, int *value_len) 1183 { 1184 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { 1185 *((uint32_t *)value) = crc32_wd(0, data, data_len, 1186 CHUNKSZ_CRC32); 1187 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); 1188 *value_len = 4; 1189 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) { 1190 crypto_csum(CRYPTO_SHA1, data, data_len, value); 1191 *value_len = 20; 1192 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) { 1193 crypto_csum(CRYPTO_SHA256, data, data_len, value); 1194 *value_len = SHA256_SUM_LEN; 1195 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { 1196 crypto_csum(CRYPTO_MD5, data, data_len, value); 1197 *value_len = 16; 1198 } else { 1199 debug("Unsupported hash alogrithm\n"); 1200 return -1; 1201 } 1202 return 0; 1203 } 1204 #else 1205 int calculate_hash(const void *data, int data_len, const char *algo, 1206 uint8_t *value, int *value_len) 1207 { 1208 return calculate_hash_software(data, data_len, algo, value, value_len); 1209 } 1210 #endif 1211 #endif 1212 1213 int fit_image_check_hash(const void *fit, int noffset, const void *data, 1214 size_t size, char **err_msgp) 1215 { 1216 uint8_t value[FIT_MAX_HASH_LEN]; 1217 int value_len; 1218 char *algo; 1219 uint8_t *fit_value; 1220 int fit_value_len; 1221 int ignore; 1222 1223 *err_msgp = NULL; 1224 1225 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 1226 *err_msgp = "Can't get hash algo property"; 1227 return -1; 1228 } 1229 printf("%s", algo); 1230 1231 if (IMAGE_ENABLE_IGNORE) { 1232 fit_image_hash_get_ignore(fit, noffset, &ignore); 1233 if (ignore) { 1234 printf("-skipped "); 1235 return 0; 1236 } 1237 } 1238 1239 if (fit_image_hash_get_value(fit, noffset, &fit_value, 1240 &fit_value_len)) { 1241 *err_msgp = "Can't get hash value property"; 1242 return -1; 1243 } 1244 1245 if (calculate_hash(data, size, algo, value, &value_len)) { 1246 *err_msgp = "Unsupported hash algorithm"; 1247 return -1; 1248 } 1249 1250 if (value_len != fit_value_len) { 1251 *err_msgp = "Bad hash value len"; 1252 return -1; 1253 } else if (memcmp(value, fit_value, value_len) != 0) { 1254 *err_msgp = "Bad hash value"; 1255 return -1; 1256 } 1257 1258 return 0; 1259 } 1260 1261 int fit_image_verify_with_data(const void *fit, int image_noffset, 1262 const void *data, size_t size) 1263 { 1264 int noffset = 0; 1265 char *err_msg = ""; 1266 int verify_all = 1; 1267 int ret; 1268 1269 /* Verify all required signatures */ 1270 if (IMAGE_ENABLE_VERIFY && 1271 fit_image_verify_required_sigs(fit, image_noffset, data, size, 1272 gd_fdt_blob(), &verify_all)) { 1273 err_msg = "Unable to verify required signature"; 1274 goto error; 1275 } 1276 1277 /* Process all hash subnodes of the component image node */ 1278 fdt_for_each_subnode(noffset, fit, image_noffset) { 1279 const char *name = fit_get_name(fit, noffset, NULL); 1280 1281 /* 1282 * Check subnode name, must be equal to "hash". 1283 * Multiple hash nodes require unique unit node 1284 * names, e.g. hash@1, hash@2, etc. 1285 */ 1286 if (!strncmp(name, FIT_HASH_NODENAME, 1287 strlen(FIT_HASH_NODENAME))) { 1288 if (fit_image_check_hash(fit, noffset, data, size, 1289 &err_msg)) 1290 goto error; 1291 puts("+ "); 1292 } else if (IMAGE_ENABLE_VERIFY && verify_all && 1293 !strncmp(name, FIT_SIG_NODENAME, 1294 strlen(FIT_SIG_NODENAME))) { 1295 ret = fit_image_check_sig(fit, noffset, data, 1296 size, -1, &err_msg); 1297 1298 /* 1299 * Show an indication on failure, but do not return 1300 * an error. Only keys marked 'required' can cause 1301 * an image validation failure. See the call to 1302 * fit_image_verify_required_sigs() above. 1303 */ 1304 if (ret) 1305 puts("- "); 1306 else 1307 puts("+ "); 1308 } 1309 } 1310 1311 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { 1312 err_msg = "Corrupted or truncated tree"; 1313 goto error; 1314 } 1315 1316 return 1; /* success */ 1317 1318 error: 1319 printf(" error!\n%s for '%s' hash node in '%s' image node\n", 1320 err_msg, fit_get_name(fit, noffset, NULL), 1321 fit_get_name(fit, image_noffset, NULL)); 1322 return 0; 1323 } 1324 1325 /** 1326 * fit_image_verify - verify data integrity 1327 * @fit: pointer to the FIT format image header 1328 * @image_noffset: component image node offset 1329 * 1330 * fit_image_verify() goes over component image hash nodes, 1331 * re-calculates each data hash and compares with the value stored in hash 1332 * node. 1333 * 1334 * returns: 1335 * 1, if all hashes are valid 1336 * 0, otherwise (or on error) 1337 */ 1338 int fit_image_verify(const void *fit, int image_noffset) 1339 { 1340 const void *data; 1341 size_t size; 1342 int noffset = 0; 1343 char *err_msg = ""; 1344 1345 /* Get image data and data length */ 1346 if (fit_image_get_data(fit, image_noffset, &data, &size)) { 1347 err_msg = "Can't get image data/size"; 1348 printf("error!\n%s for '%s' hash node in '%s' image node\n", 1349 err_msg, fit_get_name(fit, noffset, NULL), 1350 fit_get_name(fit, image_noffset, NULL)); 1351 return 0; 1352 } 1353 1354 return fit_image_verify_with_data(fit, image_noffset, data, size); 1355 } 1356 1357 /** 1358 * fit_all_image_verify - verify data integrity for all images 1359 * @fit: pointer to the FIT format image header 1360 * 1361 * fit_all_image_verify() goes over all images in the FIT and 1362 * for every images checks if all it's hashes are valid. 1363 * 1364 * returns: 1365 * 1, if all hashes of all images are valid 1366 * 0, otherwise (or on error) 1367 */ 1368 int fit_all_image_verify(const void *fit) 1369 { 1370 int images_noffset; 1371 int noffset; 1372 int ndepth; 1373 int count; 1374 1375 /* Find images parent node offset */ 1376 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 1377 if (images_noffset < 0) { 1378 printf("Can't find images parent node '%s' (%s)\n", 1379 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 1380 return 0; 1381 } 1382 1383 /* Process all image subnodes, check hashes for each */ 1384 printf("## Checking hash(es) for FIT Image at %08lx ...\n", 1385 (ulong)fit); 1386 for (ndepth = 0, count = 0, 1387 noffset = fdt_next_node(fit, images_noffset, &ndepth); 1388 (noffset >= 0) && (ndepth > 0); 1389 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1390 if (ndepth == 1) { 1391 /* 1392 * Direct child node of the images parent node, 1393 * i.e. component image node. 1394 */ 1395 printf(" Hash(es) for Image %u (%s): ", count, 1396 fit_get_name(fit, noffset, NULL)); 1397 count++; 1398 1399 if (!fit_image_verify(fit, noffset)) 1400 return 0; 1401 printf("\n"); 1402 } 1403 } 1404 return 1; 1405 } 1406 1407 /** 1408 * fit_image_check_os - check whether image node is of a given os type 1409 * @fit: pointer to the FIT format image header 1410 * @noffset: component image node offset 1411 * @os: requested image os 1412 * 1413 * fit_image_check_os() reads image os property and compares its numeric 1414 * id with the requested os. Comparison result is returned to the caller. 1415 * 1416 * returns: 1417 * 1 if image is of given os type 1418 * 0 otherwise (or on error) 1419 */ 1420 int fit_image_check_os(const void *fit, int noffset, uint8_t os) 1421 { 1422 uint8_t image_os; 1423 1424 if (fit_image_get_os(fit, noffset, &image_os)) 1425 return 0; 1426 return (os == image_os); 1427 } 1428 1429 /** 1430 * fit_image_check_arch - check whether image node is of a given arch 1431 * @fit: pointer to the FIT format image header 1432 * @noffset: component image node offset 1433 * @arch: requested imagearch 1434 * 1435 * fit_image_check_arch() reads image arch property and compares its numeric 1436 * id with the requested arch. Comparison result is returned to the caller. 1437 * 1438 * returns: 1439 * 1 if image is of given arch 1440 * 0 otherwise (or on error) 1441 */ 1442 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch) 1443 { 1444 uint8_t image_arch; 1445 int aarch32_support = 0; 1446 1447 #ifdef CONFIG_ARM64_SUPPORT_AARCH32 1448 aarch32_support = 1; 1449 #endif 1450 1451 if (fit_image_get_arch(fit, noffset, &image_arch)) 1452 return 0; 1453 return (arch == image_arch) || 1454 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) || 1455 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM && 1456 aarch32_support); 1457 } 1458 1459 /** 1460 * fit_image_check_type - check whether image node is of a given type 1461 * @fit: pointer to the FIT format image header 1462 * @noffset: component image node offset 1463 * @type: requested image type 1464 * 1465 * fit_image_check_type() reads image type property and compares its numeric 1466 * id with the requested type. Comparison result is returned to the caller. 1467 * 1468 * returns: 1469 * 1 if image is of given type 1470 * 0 otherwise (or on error) 1471 */ 1472 int fit_image_check_type(const void *fit, int noffset, uint8_t type) 1473 { 1474 uint8_t image_type; 1475 1476 if (fit_image_get_type(fit, noffset, &image_type)) 1477 return 0; 1478 return (type == image_type); 1479 } 1480 1481 /** 1482 * fit_image_check_comp - check whether image node uses given compression 1483 * @fit: pointer to the FIT format image header 1484 * @noffset: component image node offset 1485 * @comp: requested image compression type 1486 * 1487 * fit_image_check_comp() reads image compression property and compares its 1488 * numeric id with the requested compression type. Comparison result is 1489 * returned to the caller. 1490 * 1491 * returns: 1492 * 1 if image uses requested compression 1493 * 0 otherwise (or on error) 1494 */ 1495 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp) 1496 { 1497 uint8_t image_comp; 1498 1499 if (fit_image_get_comp(fit, noffset, &image_comp)) 1500 return 0; 1501 return (comp == image_comp); 1502 } 1503 1504 /** 1505 * fit_check_format - sanity check FIT image format 1506 * @fit: pointer to the FIT format image header 1507 * 1508 * fit_check_format() runs a basic sanity FIT image verification. 1509 * Routine checks for mandatory properties, nodes, etc. 1510 * 1511 * returns: 1512 * 1, on success 1513 * 0, on failure 1514 */ 1515 int fit_check_format(const void *fit) 1516 { 1517 /* mandatory / node 'description' property */ 1518 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) { 1519 debug("Wrong FIT format: no description\n"); 1520 return 0; 1521 } 1522 1523 if (IMAGE_ENABLE_TIMESTAMP) { 1524 /* mandatory / node 'timestamp' property */ 1525 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { 1526 debug("Wrong FIT format: no timestamp\n"); 1527 return 0; 1528 } 1529 } 1530 1531 /* mandatory subimages parent '/images' node */ 1532 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) { 1533 debug("Wrong FIT format: no images parent node\n"); 1534 return 0; 1535 } 1536 1537 return 1; 1538 } 1539 1540 1541 /** 1542 * fit_conf_find_compat 1543 * @fit: pointer to the FIT format image header 1544 * @fdt: pointer to the device tree to compare against 1545 * 1546 * fit_conf_find_compat() attempts to find the configuration whose fdt is the 1547 * most compatible with the passed in device tree. 1548 * 1549 * Example: 1550 * 1551 * / o image-tree 1552 * |-o images 1553 * | |-o fdt@1 1554 * | |-o fdt@2 1555 * | 1556 * |-o configurations 1557 * |-o config@1 1558 * | |-fdt = fdt@1 1559 * | 1560 * |-o config@2 1561 * |-fdt = fdt@2 1562 * 1563 * / o U-Boot fdt 1564 * |-compatible = "foo,bar", "bim,bam" 1565 * 1566 * / o kernel fdt1 1567 * |-compatible = "foo,bar", 1568 * 1569 * / o kernel fdt2 1570 * |-compatible = "bim,bam", "baz,biz" 1571 * 1572 * Configuration 1 would be picked because the first string in U-Boot's 1573 * compatible list, "foo,bar", matches a compatible string in the root of fdt1. 1574 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1. 1575 * 1576 * returns: 1577 * offset to the configuration to use if one was found 1578 * -1 otherwise 1579 */ 1580 int fit_conf_find_compat(const void *fit, const void *fdt) 1581 { 1582 int ndepth = 0; 1583 int noffset, confs_noffset, images_noffset; 1584 const void *fdt_compat; 1585 int fdt_compat_len; 1586 int best_match_offset = 0; 1587 int best_match_pos = 0; 1588 1589 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 1590 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 1591 if (confs_noffset < 0 || images_noffset < 0) { 1592 debug("Can't find configurations or images nodes.\n"); 1593 return -1; 1594 } 1595 1596 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len); 1597 if (!fdt_compat) { 1598 debug("Fdt for comparison has no \"compatible\" property.\n"); 1599 return -1; 1600 } 1601 1602 /* 1603 * Loop over the configurations in the FIT image. 1604 */ 1605 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth); 1606 (noffset >= 0) && (ndepth > 0); 1607 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1608 const void *kfdt; 1609 const char *kfdt_name; 1610 int kfdt_noffset; 1611 const char *cur_fdt_compat; 1612 int len; 1613 size_t size; 1614 int i; 1615 1616 if (ndepth > 1) 1617 continue; 1618 1619 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len); 1620 if (!kfdt_name) { 1621 debug("No fdt property found.\n"); 1622 continue; 1623 } 1624 kfdt_noffset = fdt_subnode_offset(fit, images_noffset, 1625 kfdt_name); 1626 if (kfdt_noffset < 0) { 1627 debug("No image node named \"%s\" found.\n", 1628 kfdt_name); 1629 continue; 1630 } 1631 /* 1632 * Get a pointer to this configuration's fdt. 1633 */ 1634 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) { 1635 debug("Failed to get fdt \"%s\".\n", kfdt_name); 1636 continue; 1637 } 1638 1639 len = fdt_compat_len; 1640 cur_fdt_compat = fdt_compat; 1641 /* 1642 * Look for a match for each U-Boot compatibility string in 1643 * turn in this configuration's fdt. 1644 */ 1645 for (i = 0; len > 0 && 1646 (!best_match_offset || best_match_pos > i); i++) { 1647 int cur_len = strlen(cur_fdt_compat) + 1; 1648 1649 if (!fdt_node_check_compatible(kfdt, 0, 1650 cur_fdt_compat)) { 1651 best_match_offset = noffset; 1652 best_match_pos = i; 1653 break; 1654 } 1655 len -= cur_len; 1656 cur_fdt_compat += cur_len; 1657 } 1658 } 1659 if (!best_match_offset) { 1660 debug("No match found.\n"); 1661 return -1; 1662 } 1663 1664 return best_match_offset; 1665 } 1666 1667 /** 1668 * fit_conf_get_node - get node offset for configuration of a given unit name 1669 * @fit: pointer to the FIT format image header 1670 * @conf_uname: configuration node unit name 1671 * 1672 * fit_conf_get_node() finds a configuration (within the '/configurations' 1673 * parent node) of a provided unit name. If configuration is found its node 1674 * offset is returned to the caller. 1675 * 1676 * When NULL is provided in second argument fit_conf_get_node() will search 1677 * for a default configuration node instead. Default configuration node unit 1678 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations' 1679 * node. 1680 * 1681 * returns: 1682 * configuration node offset when found (>=0) 1683 * negative number on failure (FDT_ERR_* code) 1684 */ 1685 int fit_conf_get_node(const void *fit, const char *conf_uname) 1686 { 1687 int noffset, confs_noffset; 1688 int len; 1689 const char *s; 1690 char *conf_uname_copy = NULL; 1691 1692 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 1693 if (confs_noffset < 0) { 1694 debug("Can't find configurations parent node '%s' (%s)\n", 1695 FIT_CONFS_PATH, fdt_strerror(confs_noffset)); 1696 return confs_noffset; 1697 } 1698 1699 if (conf_uname == NULL) { 1700 /* get configuration unit name from the default property */ 1701 debug("No configuration specified, trying default...\n"); 1702 conf_uname = (char *)fdt_getprop(fit, confs_noffset, 1703 FIT_DEFAULT_PROP, &len); 1704 if (conf_uname == NULL) { 1705 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP, 1706 len); 1707 return len; 1708 } 1709 debug("Found default configuration: '%s'\n", conf_uname); 1710 } 1711 1712 s = strchr(conf_uname, '#'); 1713 if (s) { 1714 len = s - conf_uname; 1715 conf_uname_copy = malloc(len + 1); 1716 if (!conf_uname_copy) { 1717 debug("Can't allocate uname copy: '%s'\n", 1718 conf_uname); 1719 return -ENOMEM; 1720 } 1721 memcpy(conf_uname_copy, conf_uname, len); 1722 conf_uname_copy[len] = '\0'; 1723 conf_uname = conf_uname_copy; 1724 } 1725 1726 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname); 1727 if (noffset < 0) { 1728 debug("Can't get node offset for configuration unit name: '%s' (%s)\n", 1729 conf_uname, fdt_strerror(noffset)); 1730 } 1731 1732 if (conf_uname_copy) 1733 free(conf_uname_copy); 1734 1735 return noffset; 1736 } 1737 1738 int fit_conf_get_prop_node_count(const void *fit, int noffset, 1739 const char *prop_name) 1740 { 1741 return fdt_stringlist_count(fit, noffset, prop_name); 1742 } 1743 1744 int fit_conf_get_prop_node_index(const void *fit, int noffset, 1745 const char *prop_name, int index) 1746 { 1747 const char *uname; 1748 int len; 1749 1750 /* get kernel image unit name from configuration kernel property */ 1751 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len); 1752 if (uname == NULL) 1753 return len; 1754 1755 return fit_image_get_node(fit, uname); 1756 } 1757 1758 int fit_conf_get_prop_node(const void *fit, int noffset, 1759 const char *prop_name) 1760 { 1761 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0); 1762 } 1763 1764 /** 1765 * fit_conf_print - prints out the FIT configuration details 1766 * @fit: pointer to the FIT format image header 1767 * @noffset: offset of the configuration node 1768 * @p: pointer to prefix string 1769 * 1770 * fit_conf_print() lists all mandatory properties for the processed 1771 * configuration node. 1772 * 1773 * returns: 1774 * no returned results 1775 */ 1776 void fit_conf_print(const void *fit, int noffset, const char *p) 1777 { 1778 char *desc; 1779 const char *uname; 1780 int ret; 1781 int fdt_index, loadables_index; 1782 1783 /* Mandatory properties */ 1784 ret = fit_get_desc(fit, noffset, &desc); 1785 printf("%s Description: ", p); 1786 if (ret) 1787 printf("unavailable\n"); 1788 else 1789 printf("%s\n", desc); 1790 1791 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL); 1792 printf("%s Kernel: ", p); 1793 if (uname == NULL) 1794 printf("unavailable\n"); 1795 else 1796 printf("%s\n", uname); 1797 1798 /* Optional properties */ 1799 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL); 1800 if (uname) 1801 printf("%s Init Ramdisk: %s\n", p, uname); 1802 1803 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL); 1804 if (uname) 1805 printf("%s Firmware: %s\n", p, uname); 1806 1807 for (fdt_index = 0; 1808 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP, 1809 fdt_index, NULL), uname; 1810 fdt_index++) { 1811 1812 if (fdt_index == 0) 1813 printf("%s FDT: ", p); 1814 else 1815 printf("%s ", p); 1816 printf("%s\n", uname); 1817 } 1818 1819 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL); 1820 if (uname) 1821 printf("%s FPGA: %s\n", p, uname); 1822 1823 /* Print out all of the specified loadables */ 1824 for (loadables_index = 0; 1825 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP, 1826 loadables_index, NULL), uname; 1827 loadables_index++) { 1828 if (loadables_index == 0) { 1829 printf("%s Loadables: ", p); 1830 } else { 1831 printf("%s ", p); 1832 } 1833 printf("%s\n", uname); 1834 } 1835 } 1836 1837 static int fit_image_select(const void *fit, int rd_noffset, int verify) 1838 { 1839 fit_image_print(fit, rd_noffset, " "); 1840 1841 if (verify) { 1842 puts(" Verifying Hash Integrity ... "); 1843 if (!fit_image_verify(fit, rd_noffset)) { 1844 puts("Bad Data Hash\n"); 1845 return -EACCES; 1846 } 1847 puts("OK\n"); 1848 } 1849 1850 return 0; 1851 } 1852 1853 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name, 1854 ulong addr) 1855 { 1856 int cfg_noffset; 1857 void *fit_hdr; 1858 int noffset; 1859 1860 debug("* %s: using config '%s' from image at 0x%08lx\n", 1861 prop_name, images->fit_uname_cfg, addr); 1862 1863 /* Check whether configuration has this property defined */ 1864 fit_hdr = map_sysmem(addr, 0); 1865 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg); 1866 if (cfg_noffset < 0) { 1867 debug("* %s: no such config\n", prop_name); 1868 return -EINVAL; 1869 } 1870 1871 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name); 1872 if (noffset < 0) { 1873 debug("* %s: no '%s' in config\n", prop_name, prop_name); 1874 return -ENOENT; 1875 } 1876 1877 return noffset; 1878 } 1879 1880 /** 1881 * fit_get_image_type_property() - get property name for IH_TYPE_... 1882 * 1883 * @return the properly name where we expect to find the image in the 1884 * config node 1885 */ 1886 static const char *fit_get_image_type_property(int type) 1887 { 1888 /* 1889 * This is sort-of available in the uimage_type[] table in image.c 1890 * but we don't have access to the short name, and "fdt" is different 1891 * anyway. So let's just keep it here. 1892 */ 1893 switch (type) { 1894 case IH_TYPE_FLATDT: 1895 return FIT_FDT_PROP; 1896 case IH_TYPE_KERNEL: 1897 return FIT_KERNEL_PROP; 1898 case IH_TYPE_RAMDISK: 1899 return FIT_RAMDISK_PROP; 1900 case IH_TYPE_FIRMWARE: 1901 return FIT_FIRMWARE_PROP; 1902 case IH_TYPE_X86_SETUP: 1903 return FIT_SETUP_PROP; 1904 case IH_TYPE_LOADABLE: 1905 return FIT_LOADABLE_PROP; 1906 case IH_TYPE_FPGA: 1907 return FIT_FPGA_PROP; 1908 case IH_TYPE_STANDALONE: 1909 return FIT_STANDALONE_PROP; 1910 } 1911 1912 return "unknown"; 1913 } 1914 1915 #ifndef USE_HOSTCC 1916 __weak int fit_board_verify_required_sigs(void) 1917 { 1918 return 0; 1919 } 1920 #endif 1921 1922 int fit_image_load_index(bootm_headers_t *images, ulong addr, 1923 const char **fit_unamep, const char **fit_uname_configp, 1924 int arch, int image_type, int image_index, int bootstage_id, 1925 enum fit_load_op load_op, ulong *datap, ulong *lenp) 1926 { 1927 int cfg_noffset, noffset; 1928 const char *fit_uname; 1929 const char *fit_uname_config; 1930 const char *fit_base_uname_config; 1931 const void *fit; 1932 const void *buf; 1933 size_t size; 1934 int type_ok, os_ok; 1935 ulong load, data, len; 1936 uint8_t os; 1937 #ifndef USE_HOSTCC 1938 uint8_t os_arch; 1939 #endif 1940 const char *prop_name; 1941 int ret; 1942 1943 fit = map_sysmem(addr, 0); 1944 fit_uname = fit_unamep ? *fit_unamep : NULL; 1945 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL; 1946 fit_base_uname_config = NULL; 1947 prop_name = fit_get_image_type_property(image_type); 1948 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr); 1949 1950 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT); 1951 if (!fit_check_format(fit)) { 1952 printf("Bad FIT %s image format!\n", prop_name); 1953 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT); 1954 return -ENOEXEC; 1955 } 1956 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK); 1957 if (fit_uname) { 1958 /* get FIT component image node offset */ 1959 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME); 1960 noffset = fit_image_get_node(fit, fit_uname); 1961 } else { 1962 /* 1963 * no image node unit name, try to get config 1964 * node first. If config unit node name is NULL 1965 * fit_conf_get_node() will try to find default config node 1966 */ 1967 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME); 1968 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) { 1969 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob()); 1970 } else { 1971 cfg_noffset = fit_conf_get_node(fit, 1972 fit_uname_config); 1973 } 1974 if (cfg_noffset < 0) { 1975 puts("Could not find configuration node\n"); 1976 bootstage_error(bootstage_id + 1977 BOOTSTAGE_SUB_NO_UNIT_NAME); 1978 return -ENOENT; 1979 } 1980 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL); 1981 printf(" Using '%s' configuration\n", fit_base_uname_config); 1982 if (image_type == IH_TYPE_KERNEL) { 1983 #ifndef USE_HOSTCC 1984 /* If board required sigs, check self */ 1985 if (fit_board_verify_required_sigs() && 1986 !IS_ENABLED(CONFIG_FIT_SIGNATURE)) { 1987 printf("Verified-boot requires CONFIG_FIT_SIGNATURE enabled\n"); 1988 hang(); 1989 } 1990 #endif 1991 /* Remember (and possibly verify) this config */ 1992 images->fit_uname_cfg = fit_base_uname_config; 1993 if (IMAGE_ENABLE_VERIFY) { 1994 puts(" Verifying Hash Integrity ... "); 1995 if (fit_config_verify(fit, cfg_noffset)) { 1996 puts("Bad Data Hash\n"); 1997 bootstage_error(bootstage_id + 1998 BOOTSTAGE_SUB_HASH); 1999 return -EACCES; 2000 } 2001 puts("OK\n"); 2002 2003 #ifdef CONFIG_FIT_ROLLBACK_PROTECT 2004 uint32_t this_index, min_index; 2005 2006 puts(" Verifying Rollback-index ... "); 2007 if (fit_rollback_index_verify(fit, 2008 FIT_ROLLBACK_INDEX, 2009 &this_index, &min_index)) { 2010 puts("Failed to get index\n"); 2011 return ret; 2012 } else if (this_index < min_index) { 2013 printf("Reject index %d < %d(min)\n", 2014 this_index, min_index); 2015 return -EINVAL; 2016 } 2017 2018 printf("%d >= %d, OK\n", this_index, min_index); 2019 #endif 2020 } 2021 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG); 2022 } 2023 2024 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset, 2025 prop_name, image_index); 2026 fit_uname = fit_get_name(fit, noffset, NULL); 2027 } 2028 if (noffset < 0) { 2029 puts("Could not find subimage node\n"); 2030 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE); 2031 return -ENOENT; 2032 } 2033 2034 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name); 2035 2036 ret = fit_image_select(fit, noffset, images->verify); 2037 if (ret) { 2038 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH); 2039 return ret; 2040 } 2041 2042 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); 2043 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX) 2044 if (!fit_image_check_target_arch(fit, noffset)) { 2045 puts("Unsupported Architecture\n"); 2046 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); 2047 return -ENOEXEC; 2048 } 2049 #endif 2050 2051 #ifndef USE_HOSTCC 2052 fit_image_get_arch(fit, noffset, &os_arch); 2053 images->os.arch = os_arch; 2054 #endif 2055 2056 if (image_type == IH_TYPE_FLATDT && 2057 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) { 2058 puts("FDT image is compressed"); 2059 return -EPROTONOSUPPORT; 2060 } 2061 2062 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); 2063 type_ok = fit_image_check_type(fit, noffset, image_type) || 2064 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) || 2065 (image_type == IH_TYPE_KERNEL && 2066 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD)); 2067 2068 os_ok = image_type == IH_TYPE_FLATDT || 2069 image_type == IH_TYPE_FPGA || 2070 fit_image_check_os(fit, noffset, IH_OS_LINUX) || 2071 fit_image_check_os(fit, noffset, IH_OS_ARM_TRUSTED_FIRMWARE) || 2072 fit_image_check_os(fit, noffset, IH_OS_OP_TEE) || 2073 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) || 2074 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS); 2075 2076 /* 2077 * If either of the checks fail, we should report an error, but 2078 * if the image type is coming from the "loadables" field, we 2079 * don't care what it is 2080 */ 2081 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) { 2082 fit_image_get_os(fit, noffset, &os); 2083 printf("No %s %s %s Image\n", 2084 genimg_get_os_name(os), 2085 genimg_get_arch_name(arch), 2086 genimg_get_type_name(image_type)); 2087 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); 2088 return -EIO; 2089 } 2090 2091 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK); 2092 2093 /* get image data address and length */ 2094 if (fit_image_get_data(fit, noffset, &buf, &size)) { 2095 printf("Could not find %s subimage data!\n", prop_name); 2096 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA); 2097 return -ENOENT; 2098 } 2099 2100 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS) 2101 /* perform any post-processing on the image data */ 2102 board_fit_image_post_process((void **)&buf, &size); 2103 #endif 2104 2105 len = (ulong)size; 2106 2107 /* verify that image data is a proper FDT blob */ 2108 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) { 2109 puts("Subimage data is not a FDT"); 2110 return -ENOEXEC; 2111 } 2112 2113 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK); 2114 2115 /* 2116 * Work-around for eldk-4.2 which gives this warning if we try to 2117 * cast in the unmap_sysmem() call: 2118 * warning: initialization discards qualifiers from pointer target type 2119 */ 2120 { 2121 void *vbuf = (void *)buf; 2122 2123 data = map_to_sysmem(vbuf); 2124 } 2125 2126 if (load_op == FIT_LOAD_IGNORED) { 2127 /* Don't load */ 2128 } else if (fit_image_get_load(fit, noffset, &load)) { 2129 if (load_op == FIT_LOAD_REQUIRED) { 2130 printf("Can't get %s subimage load address!\n", 2131 prop_name); 2132 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD); 2133 return -EBADF; 2134 } 2135 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) { 2136 ulong image_start, image_end; 2137 ulong load_end; 2138 void *dst; 2139 2140 /* 2141 * move image data to the load address, 2142 * make sure we don't overwrite initial image 2143 */ 2144 image_start = addr; 2145 image_end = addr + fit_get_size(fit); 2146 2147 load_end = load + len; 2148 if (image_type != IH_TYPE_KERNEL && 2149 load < image_end && load_end > image_start) { 2150 printf("Error: %s overwritten\n", prop_name); 2151 return -EXDEV; 2152 } 2153 2154 printf(" Loading %s from 0x%08lx to 0x%08lx\n", 2155 prop_name, data, load); 2156 2157 dst = map_sysmem(load, len); 2158 memmove(dst, buf, len); 2159 data = load; 2160 } 2161 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD); 2162 2163 *datap = data; 2164 *lenp = len; 2165 if (fit_unamep) 2166 *fit_unamep = (char *)fit_uname; 2167 if (fit_uname_configp) 2168 *fit_uname_configp = (char *)(fit_uname_config ? : 2169 fit_base_uname_config); 2170 2171 return noffset; 2172 } 2173 2174 int fit_image_load(bootm_headers_t *images, ulong addr, 2175 const char **fit_unamep, const char **fit_uname_configp, 2176 int arch, int image_type, int bootstage_id, 2177 enum fit_load_op load_op, ulong *datap, ulong *lenp) 2178 { 2179 return fit_image_load_index(images, addr,fit_unamep, fit_uname_configp, 2180 arch, image_type, 0, bootstage_id, 2181 load_op, datap, lenp); 2182 } 2183 2184 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch, 2185 ulong *setup_start, ulong *setup_len) 2186 { 2187 int noffset; 2188 ulong addr; 2189 ulong len; 2190 int ret; 2191 2192 addr = map_to_sysmem(images->fit_hdr_os); 2193 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr); 2194 if (noffset < 0) 2195 return noffset; 2196 2197 ret = fit_image_load(images, addr, NULL, NULL, arch, 2198 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START, 2199 FIT_LOAD_REQUIRED, setup_start, &len); 2200 2201 return ret; 2202 } 2203 2204 #ifndef USE_HOSTCC 2205 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr, 2206 const char **fit_unamep, const char **fit_uname_configp, 2207 int arch, ulong *datap, ulong *lenp) 2208 { 2209 int fdt_noffset, cfg_noffset, count; 2210 const void *fit; 2211 const char *fit_uname = NULL; 2212 const char *fit_uname_config = NULL; 2213 char *fit_uname_config_copy = NULL; 2214 char *next_config = NULL; 2215 ulong load, len; 2216 #ifdef CONFIG_OF_LIBFDT_OVERLAY 2217 ulong image_start, image_end; 2218 ulong ovload, ovlen; 2219 const char *uconfig; 2220 const char *uname; 2221 void *base, *ov; 2222 int i, err, noffset, ov_noffset; 2223 #endif 2224 2225 fit_uname = fit_unamep ? *fit_unamep : NULL; 2226 2227 if (fit_uname_configp && *fit_uname_configp) { 2228 fit_uname_config_copy = strdup(*fit_uname_configp); 2229 if (!fit_uname_config_copy) 2230 return -ENOMEM; 2231 2232 next_config = strchr(fit_uname_config_copy, '#'); 2233 if (next_config) 2234 *next_config++ = '\0'; 2235 if (next_config - 1 > fit_uname_config_copy) 2236 fit_uname_config = fit_uname_config_copy; 2237 } 2238 2239 fdt_noffset = fit_image_load(images, 2240 addr, &fit_uname, &fit_uname_config, 2241 arch, IH_TYPE_FLATDT, 2242 BOOTSTAGE_ID_FIT_FDT_START, 2243 FIT_LOAD_OPTIONAL, &load, &len); 2244 2245 if (fdt_noffset < 0) 2246 goto out; 2247 2248 debug("fit_uname=%s, fit_uname_config=%s\n", 2249 fit_uname ? fit_uname : "<NULL>", 2250 fit_uname_config ? fit_uname_config : "<NULL>"); 2251 2252 fit = map_sysmem(addr, 0); 2253 2254 cfg_noffset = fit_conf_get_node(fit, fit_uname_config); 2255 2256 /* single blob, or error just return as well */ 2257 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP); 2258 if (count <= 1 && !next_config) 2259 goto out; 2260 2261 /* we need to apply overlays */ 2262 2263 #ifdef CONFIG_OF_LIBFDT_OVERLAY 2264 image_start = addr; 2265 image_end = addr + fit_get_size(fit); 2266 /* verify that relocation took place by load address not being in fit */ 2267 if (load >= image_start && load < image_end) { 2268 /* check is simplified; fit load checks for overlaps */ 2269 printf("Overlayed FDT requires relocation\n"); 2270 fdt_noffset = -EBADF; 2271 goto out; 2272 } 2273 2274 base = map_sysmem(load, len); 2275 2276 /* apply extra configs in FIT first, followed by args */ 2277 for (i = 1; ; i++) { 2278 if (i < count) { 2279 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset, 2280 FIT_FDT_PROP, i); 2281 uname = fit_get_name(fit, noffset, NULL); 2282 uconfig = NULL; 2283 } else { 2284 if (!next_config) 2285 break; 2286 uconfig = next_config; 2287 next_config = strchr(next_config, '#'); 2288 if (next_config) 2289 *next_config++ = '\0'; 2290 uname = NULL; 2291 } 2292 2293 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig); 2294 2295 ov_noffset = fit_image_load(images, 2296 addr, &uname, &uconfig, 2297 arch, IH_TYPE_FLATDT, 2298 BOOTSTAGE_ID_FIT_FDT_START, 2299 FIT_LOAD_REQUIRED, &ovload, &ovlen); 2300 if (ov_noffset < 0) { 2301 printf("load of %s failed\n", uname); 2302 continue; 2303 } 2304 debug("%s loaded at 0x%08lx len=0x%08lx\n", 2305 uname, ovload, ovlen); 2306 ov = map_sysmem(ovload, ovlen); 2307 2308 base = map_sysmem(load, len + ovlen); 2309 err = fdt_open_into(base, base, len + ovlen); 2310 if (err < 0) { 2311 printf("failed on fdt_open_into\n"); 2312 fdt_noffset = err; 2313 goto out; 2314 } 2315 /* the verbose method prints out messages on error */ 2316 err = fdt_overlay_apply_verbose(base, ov); 2317 if (err < 0) { 2318 fdt_noffset = err; 2319 goto out; 2320 } 2321 fdt_pack(base); 2322 len = fdt_totalsize(base); 2323 } 2324 #else 2325 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n"); 2326 fdt_noffset = -EBADF; 2327 #endif 2328 2329 out: 2330 if (datap) 2331 *datap = load; 2332 if (lenp) 2333 *lenp = len; 2334 if (fit_unamep) 2335 *fit_unamep = fit_uname; 2336 if (fit_uname_configp) 2337 *fit_uname_configp = fit_uname_config; 2338 2339 if (fit_uname_config_copy) 2340 free(fit_uname_config_copy); 2341 return fdt_noffset; 2342 } 2343 #endif 2344