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, time_t 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 printf("Can't set '%s' property for '%s' node (%s)\n", 1107 FIT_TOTALSIZE_PROP, fit_get_name(fit, noffset, NULL), 1108 fdt_strerror(ret)); 1109 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1; 1110 } 1111 1112 return 0; 1113 } 1114 1115 int fit_get_image_defconf_node(const void *fit, int *images_noffset, int *def_noffset) 1116 { 1117 int images_node, confs_node, defconf_node; 1118 const char *def_name; 1119 1120 images_node = fdt_path_offset(fit, FIT_IMAGES_PATH); 1121 if (images_node < 0) 1122 return images_node; 1123 1124 confs_node = fdt_path_offset(fit, FIT_CONFS_PATH); 1125 if (confs_node < 0) 1126 return confs_node; 1127 1128 def_name = fdt_getprop(fit, confs_node, FIT_DEFAULT_PROP, NULL); 1129 if (!def_name) 1130 return -ENOENT; 1131 1132 defconf_node = fdt_subnode_offset(fit, confs_node, def_name); 1133 if (defconf_node < 0) 1134 return defconf_node; 1135 1136 *images_noffset = images_node; 1137 *def_noffset = defconf_node; 1138 1139 return 0; 1140 } 1141 1142 /** 1143 * calculate_hash - calculate and return hash for provided input data 1144 * @data: pointer to the input data 1145 * @data_len: data length 1146 * @algo: requested hash algorithm 1147 * @value: pointer to the char, will hold hash value data (caller must 1148 * allocate enough free space) 1149 * value_len: length of the calculated hash 1150 * 1151 * calculate_hash() computes input data hash according to the requested 1152 * algorithm. 1153 * Resulting hash value is placed in caller provided 'value' buffer, length 1154 * of the calculated hash is returned via value_len pointer argument. 1155 * 1156 * returns: 1157 * 0, on success 1158 * -1, when algo is unsupported 1159 */ 1160 int calculate_hash_software(const void *data, int data_len, 1161 const char *algo, uint8_t *value, 1162 int *value_len) 1163 { 1164 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { 1165 *((uint32_t *)value) = crc32_wd(0, data, data_len, 1166 CHUNKSZ_CRC32); 1167 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); 1168 *value_len = 4; 1169 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) { 1170 sha1_csum_wd((unsigned char *)data, data_len, 1171 (unsigned char *)value, CHUNKSZ_SHA1); 1172 *value_len = 20; 1173 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) { 1174 sha256_csum_wd((unsigned char *)data, data_len, 1175 (unsigned char *)value, CHUNKSZ_SHA256); 1176 *value_len = SHA256_SUM_LEN; 1177 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { 1178 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); 1179 *value_len = 16; 1180 } else { 1181 debug("Unsupported hash alogrithm\n"); 1182 return -1; 1183 } 1184 return 0; 1185 } 1186 1187 #ifdef USE_HOSTCC 1188 int calculate_hash(const void *data, int data_len, const char *algo, 1189 uint8_t *value, int *value_len) 1190 { 1191 return calculate_hash_software(data, data_len, algo, value, value_len); 1192 } 1193 #else 1194 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 1195 static int crypto_csum(u32 cap, const char *data, int len, u8 *output) 1196 { 1197 struct udevice *dev; 1198 sha_context csha_ctx; 1199 1200 dev = crypto_get_device(cap); 1201 if (!dev) { 1202 printf("Can't find expected crypto device\n"); 1203 return -ENODEV; 1204 } 1205 1206 csha_ctx.algo = cap; 1207 csha_ctx.length = len; 1208 1209 return crypto_sha_csum(dev, &csha_ctx, (char *)data, len, output); 1210 } 1211 1212 int calculate_hash(const void *data, int data_len, const char *algo, 1213 uint8_t *value, int *value_len) 1214 { 1215 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { 1216 *((uint32_t *)value) = crc32_wd(0, data, data_len, 1217 CHUNKSZ_CRC32); 1218 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); 1219 *value_len = 4; 1220 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) { 1221 crypto_csum(CRYPTO_SHA1, data, data_len, value); 1222 *value_len = 20; 1223 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) { 1224 crypto_csum(CRYPTO_SHA256, data, data_len, value); 1225 *value_len = SHA256_SUM_LEN; 1226 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { 1227 crypto_csum(CRYPTO_MD5, data, data_len, value); 1228 *value_len = 16; 1229 } else { 1230 debug("Unsupported hash alogrithm\n"); 1231 return -1; 1232 } 1233 return 0; 1234 } 1235 #else 1236 int calculate_hash(const void *data, int data_len, const char *algo, 1237 uint8_t *value, int *value_len) 1238 { 1239 return calculate_hash_software(data, data_len, algo, value, value_len); 1240 } 1241 #endif 1242 #endif 1243 1244 int fit_image_check_hash(const void *fit, int noffset, const void *data, 1245 size_t size, char **err_msgp) 1246 { 1247 uint8_t value[FIT_MAX_HASH_LEN]; 1248 int value_len; 1249 char *algo; 1250 uint8_t *fit_value; 1251 int fit_value_len; 1252 int ignore; 1253 1254 *err_msgp = NULL; 1255 1256 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 1257 *err_msgp = "Can't get hash algo property"; 1258 return -1; 1259 } 1260 printf("%s", algo); 1261 1262 if (IMAGE_ENABLE_IGNORE) { 1263 fit_image_hash_get_ignore(fit, noffset, &ignore); 1264 if (ignore) { 1265 printf("-skipped "); 1266 return 0; 1267 } 1268 } 1269 1270 if (fit_image_hash_get_value(fit, noffset, &fit_value, 1271 &fit_value_len)) { 1272 *err_msgp = "Can't get hash value property"; 1273 return -1; 1274 } 1275 1276 if (calculate_hash(data, size, algo, value, &value_len)) { 1277 *err_msgp = "Unsupported hash algorithm"; 1278 return -1; 1279 } 1280 1281 if (value_len != fit_value_len) { 1282 *err_msgp = "Bad hash value len"; 1283 return -1; 1284 } else if (memcmp(value, fit_value, value_len) != 0) { 1285 *err_msgp = "Bad hash value"; 1286 return -1; 1287 } 1288 1289 return 0; 1290 } 1291 1292 int fit_image_verify_with_data(const void *fit, int image_noffset, 1293 const void *data, size_t size) 1294 { 1295 int noffset = 0; 1296 char *err_msg = ""; 1297 int verify_all = 1; 1298 int ret; 1299 1300 /* Verify all required signatures */ 1301 if (IMAGE_ENABLE_VERIFY && 1302 fit_image_verify_required_sigs(fit, image_noffset, data, size, 1303 gd_fdt_blob(), &verify_all)) { 1304 err_msg = "Unable to verify required signature"; 1305 goto error; 1306 } 1307 1308 /* Process all hash subnodes of the component image node */ 1309 fdt_for_each_subnode(noffset, fit, image_noffset) { 1310 const char *name = fit_get_name(fit, noffset, NULL); 1311 1312 /* 1313 * Check subnode name, must be equal to "hash". 1314 * Multiple hash nodes require unique unit node 1315 * names, e.g. hash@1, hash@2, etc. 1316 */ 1317 if (!strncmp(name, FIT_HASH_NODENAME, 1318 strlen(FIT_HASH_NODENAME))) { 1319 if (fit_image_check_hash(fit, noffset, data, size, 1320 &err_msg)) 1321 goto error; 1322 puts("+ "); 1323 } else if (IMAGE_ENABLE_VERIFY && verify_all && 1324 !strncmp(name, FIT_SIG_NODENAME, 1325 strlen(FIT_SIG_NODENAME))) { 1326 ret = fit_image_check_sig(fit, noffset, data, 1327 size, -1, &err_msg); 1328 1329 /* 1330 * Show an indication on failure, but do not return 1331 * an error. Only keys marked 'required' can cause 1332 * an image validation failure. See the call to 1333 * fit_image_verify_required_sigs() above. 1334 */ 1335 if (ret) 1336 puts("- "); 1337 else 1338 puts("+ "); 1339 } 1340 } 1341 1342 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { 1343 err_msg = "Corrupted or truncated tree"; 1344 goto error; 1345 } 1346 1347 return 1; /* success */ 1348 1349 error: 1350 printf(" error!\n%s for '%s' hash node in '%s' image node\n", 1351 err_msg, fit_get_name(fit, noffset, NULL), 1352 fit_get_name(fit, image_noffset, NULL)); 1353 return 0; 1354 } 1355 1356 /** 1357 * fit_image_verify - verify data integrity 1358 * @fit: pointer to the FIT format image header 1359 * @image_noffset: component image node offset 1360 * 1361 * fit_image_verify() goes over component image hash nodes, 1362 * re-calculates each data hash and compares with the value stored in hash 1363 * node. 1364 * 1365 * returns: 1366 * 1, if all hashes are valid 1367 * 0, otherwise (or on error) 1368 */ 1369 int fit_image_verify(const void *fit, int image_noffset) 1370 { 1371 const void *data; 1372 size_t size; 1373 int noffset = 0; 1374 char *err_msg = ""; 1375 1376 /* Get image data and data length */ 1377 if (fit_image_get_data(fit, image_noffset, &data, &size)) { 1378 err_msg = "Can't get image data/size"; 1379 printf("error!\n%s for '%s' hash node in '%s' image node\n", 1380 err_msg, fit_get_name(fit, noffset, NULL), 1381 fit_get_name(fit, image_noffset, NULL)); 1382 return 0; 1383 } 1384 1385 return fit_image_verify_with_data(fit, image_noffset, data, size); 1386 } 1387 1388 /** 1389 * fit_all_image_verify - verify data integrity for all images 1390 * @fit: pointer to the FIT format image header 1391 * 1392 * fit_all_image_verify() goes over all images in the FIT and 1393 * for every images checks if all it's hashes are valid. 1394 * 1395 * returns: 1396 * 1, if all hashes of all images are valid 1397 * 0, otherwise (or on error) 1398 */ 1399 int fit_all_image_verify(const void *fit) 1400 { 1401 int images_noffset; 1402 int noffset; 1403 int ndepth; 1404 int count; 1405 1406 /* Find images parent node offset */ 1407 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 1408 if (images_noffset < 0) { 1409 printf("Can't find images parent node '%s' (%s)\n", 1410 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 1411 return 0; 1412 } 1413 1414 /* Process all image subnodes, check hashes for each */ 1415 printf("## Checking hash(es) for FIT Image at %08lx ...\n", 1416 (ulong)fit); 1417 for (ndepth = 0, count = 0, 1418 noffset = fdt_next_node(fit, images_noffset, &ndepth); 1419 (noffset >= 0) && (ndepth > 0); 1420 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1421 if (ndepth == 1) { 1422 /* 1423 * Direct child node of the images parent node, 1424 * i.e. component image node. 1425 */ 1426 printf(" Hash(es) for Image %u (%s): ", count, 1427 fit_get_name(fit, noffset, NULL)); 1428 count++; 1429 1430 if (!fit_image_verify(fit, noffset)) 1431 return 0; 1432 printf("\n"); 1433 } 1434 } 1435 return 1; 1436 } 1437 1438 /** 1439 * fit_image_check_os - check whether image node is of a given os type 1440 * @fit: pointer to the FIT format image header 1441 * @noffset: component image node offset 1442 * @os: requested image os 1443 * 1444 * fit_image_check_os() reads image os property and compares its numeric 1445 * id with the requested os. Comparison result is returned to the caller. 1446 * 1447 * returns: 1448 * 1 if image is of given os type 1449 * 0 otherwise (or on error) 1450 */ 1451 int fit_image_check_os(const void *fit, int noffset, uint8_t os) 1452 { 1453 uint8_t image_os; 1454 1455 if (fit_image_get_os(fit, noffset, &image_os)) 1456 return 0; 1457 return (os == image_os); 1458 } 1459 1460 /** 1461 * fit_image_check_arch - check whether image node is of a given arch 1462 * @fit: pointer to the FIT format image header 1463 * @noffset: component image node offset 1464 * @arch: requested imagearch 1465 * 1466 * fit_image_check_arch() reads image arch property and compares its numeric 1467 * id with the requested arch. Comparison result is returned to the caller. 1468 * 1469 * returns: 1470 * 1 if image is of given arch 1471 * 0 otherwise (or on error) 1472 */ 1473 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch) 1474 { 1475 uint8_t image_arch; 1476 int aarch32_support = 0; 1477 1478 #ifdef CONFIG_ARM64_SUPPORT_AARCH32 1479 aarch32_support = 1; 1480 #endif 1481 1482 if (fit_image_get_arch(fit, noffset, &image_arch)) 1483 return 0; 1484 return (arch == image_arch) || 1485 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) || 1486 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM && 1487 aarch32_support); 1488 } 1489 1490 /** 1491 * fit_image_check_type - check whether image node is of a given type 1492 * @fit: pointer to the FIT format image header 1493 * @noffset: component image node offset 1494 * @type: requested image type 1495 * 1496 * fit_image_check_type() reads image type property and compares its numeric 1497 * id with the requested type. Comparison result is returned to the caller. 1498 * 1499 * returns: 1500 * 1 if image is of given type 1501 * 0 otherwise (or on error) 1502 */ 1503 int fit_image_check_type(const void *fit, int noffset, uint8_t type) 1504 { 1505 uint8_t image_type; 1506 1507 if (fit_image_get_type(fit, noffset, &image_type)) 1508 return 0; 1509 return (type == image_type); 1510 } 1511 1512 /** 1513 * fit_image_check_comp - check whether image node uses given compression 1514 * @fit: pointer to the FIT format image header 1515 * @noffset: component image node offset 1516 * @comp: requested image compression type 1517 * 1518 * fit_image_check_comp() reads image compression property and compares its 1519 * numeric id with the requested compression type. Comparison result is 1520 * returned to the caller. 1521 * 1522 * returns: 1523 * 1 if image uses requested compression 1524 * 0 otherwise (or on error) 1525 */ 1526 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp) 1527 { 1528 uint8_t image_comp; 1529 1530 if (fit_image_get_comp(fit, noffset, &image_comp)) 1531 return 0; 1532 return (comp == image_comp); 1533 } 1534 1535 /** 1536 * fit_check_format - sanity check FIT image format 1537 * @fit: pointer to the FIT format image header 1538 * 1539 * fit_check_format() runs a basic sanity FIT image verification. 1540 * Routine checks for mandatory properties, nodes, etc. 1541 * 1542 * returns: 1543 * 1, on success 1544 * 0, on failure 1545 */ 1546 int fit_check_format(const void *fit) 1547 { 1548 /* mandatory / node 'description' property */ 1549 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) { 1550 debug("Wrong FIT format: no description\n"); 1551 return 0; 1552 } 1553 1554 if (IMAGE_ENABLE_TIMESTAMP) { 1555 /* mandatory / node 'timestamp' property */ 1556 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { 1557 debug("Wrong FIT format: no timestamp\n"); 1558 return 0; 1559 } 1560 } 1561 1562 /* mandatory subimages parent '/images' node */ 1563 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) { 1564 debug("Wrong FIT format: no images parent node\n"); 1565 return 0; 1566 } 1567 1568 return 1; 1569 } 1570 1571 1572 /** 1573 * fit_conf_find_compat 1574 * @fit: pointer to the FIT format image header 1575 * @fdt: pointer to the device tree to compare against 1576 * 1577 * fit_conf_find_compat() attempts to find the configuration whose fdt is the 1578 * most compatible with the passed in device tree. 1579 * 1580 * Example: 1581 * 1582 * / o image-tree 1583 * |-o images 1584 * | |-o fdt@1 1585 * | |-o fdt@2 1586 * | 1587 * |-o configurations 1588 * |-o config@1 1589 * | |-fdt = fdt@1 1590 * | 1591 * |-o config@2 1592 * |-fdt = fdt@2 1593 * 1594 * / o U-Boot fdt 1595 * |-compatible = "foo,bar", "bim,bam" 1596 * 1597 * / o kernel fdt1 1598 * |-compatible = "foo,bar", 1599 * 1600 * / o kernel fdt2 1601 * |-compatible = "bim,bam", "baz,biz" 1602 * 1603 * Configuration 1 would be picked because the first string in U-Boot's 1604 * compatible list, "foo,bar", matches a compatible string in the root of fdt1. 1605 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1. 1606 * 1607 * returns: 1608 * offset to the configuration to use if one was found 1609 * -1 otherwise 1610 */ 1611 int fit_conf_find_compat(const void *fit, const void *fdt) 1612 { 1613 int ndepth = 0; 1614 int noffset, confs_noffset, images_noffset; 1615 const void *fdt_compat; 1616 int fdt_compat_len; 1617 int best_match_offset = 0; 1618 int best_match_pos = 0; 1619 1620 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 1621 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 1622 if (confs_noffset < 0 || images_noffset < 0) { 1623 debug("Can't find configurations or images nodes.\n"); 1624 return -1; 1625 } 1626 1627 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len); 1628 if (!fdt_compat) { 1629 debug("Fdt for comparison has no \"compatible\" property.\n"); 1630 return -1; 1631 } 1632 1633 /* 1634 * Loop over the configurations in the FIT image. 1635 */ 1636 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth); 1637 (noffset >= 0) && (ndepth > 0); 1638 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1639 const void *kfdt; 1640 const char *kfdt_name; 1641 int kfdt_noffset; 1642 const char *cur_fdt_compat; 1643 int len; 1644 size_t size; 1645 int i; 1646 1647 if (ndepth > 1) 1648 continue; 1649 1650 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len); 1651 if (!kfdt_name) { 1652 debug("No fdt property found.\n"); 1653 continue; 1654 } 1655 kfdt_noffset = fdt_subnode_offset(fit, images_noffset, 1656 kfdt_name); 1657 if (kfdt_noffset < 0) { 1658 debug("No image node named \"%s\" found.\n", 1659 kfdt_name); 1660 continue; 1661 } 1662 /* 1663 * Get a pointer to this configuration's fdt. 1664 */ 1665 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) { 1666 debug("Failed to get fdt \"%s\".\n", kfdt_name); 1667 continue; 1668 } 1669 1670 len = fdt_compat_len; 1671 cur_fdt_compat = fdt_compat; 1672 /* 1673 * Look for a match for each U-Boot compatibility string in 1674 * turn in this configuration's fdt. 1675 */ 1676 for (i = 0; len > 0 && 1677 (!best_match_offset || best_match_pos > i); i++) { 1678 int cur_len = strlen(cur_fdt_compat) + 1; 1679 1680 if (!fdt_node_check_compatible(kfdt, 0, 1681 cur_fdt_compat)) { 1682 best_match_offset = noffset; 1683 best_match_pos = i; 1684 break; 1685 } 1686 len -= cur_len; 1687 cur_fdt_compat += cur_len; 1688 } 1689 } 1690 if (!best_match_offset) { 1691 debug("No match found.\n"); 1692 return -1; 1693 } 1694 1695 return best_match_offset; 1696 } 1697 1698 /** 1699 * fit_conf_get_node - get node offset for configuration of a given unit name 1700 * @fit: pointer to the FIT format image header 1701 * @conf_uname: configuration node unit name 1702 * 1703 * fit_conf_get_node() finds a configuration (within the '/configurations' 1704 * parent node) of a provided unit name. If configuration is found its node 1705 * offset is returned to the caller. 1706 * 1707 * When NULL is provided in second argument fit_conf_get_node() will search 1708 * for a default configuration node instead. Default configuration node unit 1709 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations' 1710 * node. 1711 * 1712 * returns: 1713 * configuration node offset when found (>=0) 1714 * negative number on failure (FDT_ERR_* code) 1715 */ 1716 int fit_conf_get_node(const void *fit, const char *conf_uname) 1717 { 1718 int noffset, confs_noffset; 1719 int len; 1720 const char *s; 1721 char *conf_uname_copy = NULL; 1722 1723 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 1724 if (confs_noffset < 0) { 1725 debug("Can't find configurations parent node '%s' (%s)\n", 1726 FIT_CONFS_PATH, fdt_strerror(confs_noffset)); 1727 return confs_noffset; 1728 } 1729 1730 if (conf_uname == NULL) { 1731 /* get configuration unit name from the default property */ 1732 debug("No configuration specified, trying default...\n"); 1733 conf_uname = (char *)fdt_getprop(fit, confs_noffset, 1734 FIT_DEFAULT_PROP, &len); 1735 if (conf_uname == NULL) { 1736 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP, 1737 len); 1738 return len; 1739 } 1740 debug("Found default configuration: '%s'\n", conf_uname); 1741 } 1742 1743 s = strchr(conf_uname, '#'); 1744 if (s) { 1745 len = s - conf_uname; 1746 conf_uname_copy = malloc(len + 1); 1747 if (!conf_uname_copy) { 1748 debug("Can't allocate uname copy: '%s'\n", 1749 conf_uname); 1750 return -ENOMEM; 1751 } 1752 memcpy(conf_uname_copy, conf_uname, len); 1753 conf_uname_copy[len] = '\0'; 1754 conf_uname = conf_uname_copy; 1755 } 1756 1757 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname); 1758 if (noffset < 0) { 1759 debug("Can't get node offset for configuration unit name: '%s' (%s)\n", 1760 conf_uname, fdt_strerror(noffset)); 1761 } 1762 1763 if (conf_uname_copy) 1764 free(conf_uname_copy); 1765 1766 return noffset; 1767 } 1768 1769 int fit_conf_get_prop_node_count(const void *fit, int noffset, 1770 const char *prop_name) 1771 { 1772 return fdt_stringlist_count(fit, noffset, prop_name); 1773 } 1774 1775 int fit_conf_get_prop_node_index(const void *fit, int noffset, 1776 const char *prop_name, int index) 1777 { 1778 const char *uname; 1779 int len; 1780 1781 /* get kernel image unit name from configuration kernel property */ 1782 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len); 1783 if (uname == NULL) 1784 return len; 1785 1786 return fit_image_get_node(fit, uname); 1787 } 1788 1789 int fit_conf_get_prop_node(const void *fit, int noffset, 1790 const char *prop_name) 1791 { 1792 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0); 1793 } 1794 1795 /** 1796 * fit_conf_print - prints out the FIT configuration details 1797 * @fit: pointer to the FIT format image header 1798 * @noffset: offset of the configuration node 1799 * @p: pointer to prefix string 1800 * 1801 * fit_conf_print() lists all mandatory properties for the processed 1802 * configuration node. 1803 * 1804 * returns: 1805 * no returned results 1806 */ 1807 void fit_conf_print(const void *fit, int noffset, const char *p) 1808 { 1809 char *desc; 1810 const char *uname; 1811 int ret; 1812 int fdt_index, loadables_index; 1813 1814 /* Mandatory properties */ 1815 ret = fit_get_desc(fit, noffset, &desc); 1816 printf("%s Description: ", p); 1817 if (ret) 1818 printf("unavailable\n"); 1819 else 1820 printf("%s\n", desc); 1821 1822 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL); 1823 printf("%s Kernel: ", p); 1824 if (uname == NULL) 1825 printf("unavailable\n"); 1826 else 1827 printf("%s\n", uname); 1828 1829 /* Optional properties */ 1830 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL); 1831 if (uname) 1832 printf("%s Init Ramdisk: %s\n", p, uname); 1833 1834 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL); 1835 if (uname) 1836 printf("%s Firmware: %s\n", p, uname); 1837 1838 for (fdt_index = 0; 1839 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP, 1840 fdt_index, NULL), uname; 1841 fdt_index++) { 1842 1843 if (fdt_index == 0) 1844 printf("%s FDT: ", p); 1845 else 1846 printf("%s ", p); 1847 printf("%s\n", uname); 1848 } 1849 1850 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL); 1851 if (uname) 1852 printf("%s FPGA: %s\n", p, uname); 1853 1854 /* Print out all of the specified loadables */ 1855 for (loadables_index = 0; 1856 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP, 1857 loadables_index, NULL), uname; 1858 loadables_index++) { 1859 if (loadables_index == 0) { 1860 printf("%s Loadables: ", p); 1861 } else { 1862 printf("%s ", p); 1863 } 1864 printf("%s\n", uname); 1865 } 1866 } 1867 1868 static int fit_image_select(const void *fit, int rd_noffset, int verify) 1869 { 1870 fit_image_print(fit, rd_noffset, " "); 1871 1872 if (verify) { 1873 puts(" Verifying Hash Integrity ... "); 1874 if (!fit_image_verify(fit, rd_noffset)) { 1875 puts("Bad Data Hash\n"); 1876 return -EACCES; 1877 } 1878 puts("OK\n"); 1879 } 1880 1881 return 0; 1882 } 1883 1884 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name, 1885 ulong addr) 1886 { 1887 int cfg_noffset; 1888 void *fit_hdr; 1889 int noffset; 1890 1891 debug("* %s: using config '%s' from image at 0x%08lx\n", 1892 prop_name, images->fit_uname_cfg, addr); 1893 1894 /* Check whether configuration has this property defined */ 1895 fit_hdr = map_sysmem(addr, 0); 1896 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg); 1897 if (cfg_noffset < 0) { 1898 debug("* %s: no such config\n", prop_name); 1899 return -EINVAL; 1900 } 1901 1902 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name); 1903 if (noffset < 0) { 1904 debug("* %s: no '%s' in config\n", prop_name, prop_name); 1905 return -ENOENT; 1906 } 1907 1908 return noffset; 1909 } 1910 1911 /** 1912 * fit_get_image_type_property() - get property name for IH_TYPE_... 1913 * 1914 * @return the properly name where we expect to find the image in the 1915 * config node 1916 */ 1917 static const char *fit_get_image_type_property(int type) 1918 { 1919 /* 1920 * This is sort-of available in the uimage_type[] table in image.c 1921 * but we don't have access to the short name, and "fdt" is different 1922 * anyway. So let's just keep it here. 1923 */ 1924 switch (type) { 1925 case IH_TYPE_FLATDT: 1926 return FIT_FDT_PROP; 1927 case IH_TYPE_KERNEL: 1928 return FIT_KERNEL_PROP; 1929 case IH_TYPE_RAMDISK: 1930 return FIT_RAMDISK_PROP; 1931 case IH_TYPE_FIRMWARE: 1932 return FIT_FIRMWARE_PROP; 1933 case IH_TYPE_X86_SETUP: 1934 return FIT_SETUP_PROP; 1935 case IH_TYPE_LOADABLE: 1936 return FIT_LOADABLE_PROP; 1937 case IH_TYPE_FPGA: 1938 return FIT_FPGA_PROP; 1939 case IH_TYPE_STANDALONE: 1940 return FIT_STANDALONE_PROP; 1941 } 1942 1943 return "unknown"; 1944 } 1945 1946 #ifndef USE_HOSTCC 1947 __weak int fit_board_verify_required_sigs(void) 1948 { 1949 return 0; 1950 } 1951 #endif 1952 1953 int fit_image_load_index(bootm_headers_t *images, ulong addr, 1954 const char **fit_unamep, const char **fit_uname_configp, 1955 int arch, int image_type, int image_index, int bootstage_id, 1956 enum fit_load_op load_op, ulong *datap, ulong *lenp) 1957 { 1958 int cfg_noffset, noffset; 1959 const char *fit_uname; 1960 const char *fit_uname_config; 1961 const char *fit_base_uname_config; 1962 const void *fit; 1963 const void *buf; 1964 size_t size; 1965 int type_ok, os_ok; 1966 ulong load, data, len; 1967 uint8_t os; 1968 #ifndef USE_HOSTCC 1969 uint8_t os_arch; 1970 #endif 1971 const char *prop_name; 1972 int ret; 1973 1974 #ifndef USE_HOSTCC 1975 /* If board required sigs, check self */ 1976 if (fit_board_verify_required_sigs() && 1977 !IS_ENABLED(CONFIG_FIT_SIGNATURE)) { 1978 printf("Verified-boot requires CONFIG_FIT_SIGNATURE enabled\n"); 1979 hang(); 1980 } 1981 #endif 1982 1983 fit = map_sysmem(addr, 0); 1984 fit_uname = fit_unamep ? *fit_unamep : NULL; 1985 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL; 1986 fit_base_uname_config = NULL; 1987 prop_name = fit_get_image_type_property(image_type); 1988 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr); 1989 1990 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT); 1991 if (!fit_check_format(fit)) { 1992 printf("Bad FIT %s image format!\n", prop_name); 1993 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT); 1994 return -ENOEXEC; 1995 } 1996 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK); 1997 if (fit_uname) { 1998 /* get FIT component image node offset */ 1999 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME); 2000 noffset = fit_image_get_node(fit, fit_uname); 2001 } else { 2002 /* 2003 * no image node unit name, try to get config 2004 * node first. If config unit node name is NULL 2005 * fit_conf_get_node() will try to find default config node 2006 */ 2007 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME); 2008 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) { 2009 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob()); 2010 } else { 2011 cfg_noffset = fit_conf_get_node(fit, 2012 fit_uname_config); 2013 } 2014 if (cfg_noffset < 0) { 2015 puts("Could not find configuration node\n"); 2016 bootstage_error(bootstage_id + 2017 BOOTSTAGE_SUB_NO_UNIT_NAME); 2018 return -ENOENT; 2019 } 2020 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL); 2021 printf(" Using '%s' configuration\n", fit_base_uname_config); 2022 if (image_type == IH_TYPE_KERNEL) { 2023 /* Remember (and possibly verify) this config */ 2024 images->fit_uname_cfg = fit_base_uname_config; 2025 if (IMAGE_ENABLE_VERIFY) { 2026 puts(" Verifying Hash Integrity ... "); 2027 if (fit_config_verify(fit, cfg_noffset)) { 2028 puts("Bad Data Hash\n"); 2029 bootstage_error(bootstage_id + 2030 BOOTSTAGE_SUB_HASH); 2031 return -EACCES; 2032 } 2033 puts("OK\n"); 2034 2035 #ifdef CONFIG_FIT_ROLLBACK_PROTECT 2036 uint32_t this_index, min_index; 2037 2038 puts(" Verifying Rollback-index ... "); 2039 if (fit_rollback_index_verify(fit, 2040 FIT_ROLLBACK_INDEX, 2041 &this_index, &min_index)) { 2042 puts("Failed to get index\n"); 2043 return ret; 2044 } else if (this_index < min_index) { 2045 printf("Reject index %d < %d(min)\n", 2046 this_index, min_index); 2047 return -EINVAL; 2048 } 2049 2050 printf("%d >= %d, OK\n", this_index, min_index); 2051 #endif 2052 } 2053 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG); 2054 } 2055 2056 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset, 2057 prop_name, image_index); 2058 fit_uname = fit_get_name(fit, noffset, NULL); 2059 } 2060 if (noffset < 0) { 2061 puts("Could not find subimage node\n"); 2062 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE); 2063 return -ENOENT; 2064 } 2065 2066 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name); 2067 2068 ret = fit_image_select(fit, noffset, images->verify); 2069 if (ret) { 2070 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH); 2071 return ret; 2072 } 2073 2074 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); 2075 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX) 2076 if (!fit_image_check_target_arch(fit, noffset)) { 2077 puts("Unsupported Architecture\n"); 2078 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); 2079 return -ENOEXEC; 2080 } 2081 #endif 2082 2083 #ifndef USE_HOSTCC 2084 fit_image_get_arch(fit, noffset, &os_arch); 2085 images->os.arch = os_arch; 2086 #endif 2087 2088 if (image_type == IH_TYPE_FLATDT && 2089 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) { 2090 puts("FDT image is compressed"); 2091 return -EPROTONOSUPPORT; 2092 } 2093 2094 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); 2095 type_ok = fit_image_check_type(fit, noffset, image_type) || 2096 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) || 2097 (image_type == IH_TYPE_KERNEL && 2098 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD)); 2099 2100 os_ok = image_type == IH_TYPE_FLATDT || 2101 image_type == IH_TYPE_FPGA || 2102 fit_image_check_os(fit, noffset, IH_OS_LINUX) || 2103 fit_image_check_os(fit, noffset, IH_OS_ARM_TRUSTED_FIRMWARE) || 2104 fit_image_check_os(fit, noffset, IH_OS_OP_TEE) || 2105 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) || 2106 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS); 2107 2108 /* 2109 * If either of the checks fail, we should report an error, but 2110 * if the image type is coming from the "loadables" field, we 2111 * don't care what it is 2112 */ 2113 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) { 2114 fit_image_get_os(fit, noffset, &os); 2115 printf("No %s %s %s Image\n", 2116 genimg_get_os_name(os), 2117 genimg_get_arch_name(arch), 2118 genimg_get_type_name(image_type)); 2119 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); 2120 return -EIO; 2121 } 2122 2123 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK); 2124 2125 /* get image data address and length */ 2126 if (fit_image_get_data(fit, noffset, &buf, &size)) { 2127 printf("Could not find %s subimage data!\n", prop_name); 2128 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA); 2129 return -ENOENT; 2130 } 2131 2132 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS) 2133 /* perform any post-processing on the image data */ 2134 board_fit_image_post_process((void **)&buf, &size); 2135 #endif 2136 2137 len = (ulong)size; 2138 2139 /* verify that image data is a proper FDT blob */ 2140 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) { 2141 puts("Subimage data is not a FDT"); 2142 return -ENOEXEC; 2143 } 2144 2145 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK); 2146 2147 /* 2148 * Work-around for eldk-4.2 which gives this warning if we try to 2149 * cast in the unmap_sysmem() call: 2150 * warning: initialization discards qualifiers from pointer target type 2151 */ 2152 { 2153 void *vbuf = (void *)buf; 2154 2155 data = map_to_sysmem(vbuf); 2156 } 2157 2158 if (load_op == FIT_LOAD_IGNORED) { 2159 /* Don't load */ 2160 } else if (fit_image_get_load(fit, noffset, &load)) { 2161 if (load_op == FIT_LOAD_REQUIRED) { 2162 printf("Can't get %s subimage load address!\n", 2163 prop_name); 2164 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD); 2165 return -EBADF; 2166 } 2167 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) { 2168 ulong image_start, image_end; 2169 ulong load_end; 2170 void *dst; 2171 2172 /* 2173 * move image data to the load address, 2174 * make sure we don't overwrite initial image 2175 */ 2176 image_start = addr; 2177 image_end = addr + fit_get_size(fit); 2178 2179 load_end = load + len; 2180 if (image_type != IH_TYPE_KERNEL && 2181 load < image_end && load_end > image_start) { 2182 printf("Error: %s overwritten\n", prop_name); 2183 return -EXDEV; 2184 } 2185 2186 printf(" Loading %s from 0x%08lx to 0x%08lx\n", 2187 prop_name, data, load); 2188 2189 dst = map_sysmem(load, len); 2190 memmove(dst, buf, len); 2191 data = load; 2192 } 2193 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD); 2194 2195 *datap = data; 2196 *lenp = len; 2197 if (fit_unamep) 2198 *fit_unamep = (char *)fit_uname; 2199 if (fit_uname_configp) 2200 *fit_uname_configp = (char *)(fit_uname_config ? : 2201 fit_base_uname_config); 2202 2203 return noffset; 2204 } 2205 2206 int fit_image_load(bootm_headers_t *images, ulong addr, 2207 const char **fit_unamep, const char **fit_uname_configp, 2208 int arch, int image_type, int bootstage_id, 2209 enum fit_load_op load_op, ulong *datap, ulong *lenp) 2210 { 2211 return fit_image_load_index(images, addr,fit_unamep, fit_uname_configp, 2212 arch, image_type, 0, bootstage_id, 2213 load_op, datap, lenp); 2214 } 2215 2216 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch, 2217 ulong *setup_start, ulong *setup_len) 2218 { 2219 int noffset; 2220 ulong addr; 2221 ulong len; 2222 int ret; 2223 2224 addr = map_to_sysmem(images->fit_hdr_os); 2225 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr); 2226 if (noffset < 0) 2227 return noffset; 2228 2229 ret = fit_image_load(images, addr, NULL, NULL, arch, 2230 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START, 2231 FIT_LOAD_REQUIRED, setup_start, &len); 2232 2233 return ret; 2234 } 2235 2236 #ifndef USE_HOSTCC 2237 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr, 2238 const char **fit_unamep, const char **fit_uname_configp, 2239 int arch, ulong *datap, ulong *lenp) 2240 { 2241 int fdt_noffset, cfg_noffset, count; 2242 const void *fit; 2243 const char *fit_uname = NULL; 2244 const char *fit_uname_config = NULL; 2245 char *fit_uname_config_copy = NULL; 2246 char *next_config = NULL; 2247 ulong load, len; 2248 #ifdef CONFIG_OF_LIBFDT_OVERLAY 2249 ulong image_start, image_end; 2250 ulong ovload, ovlen; 2251 const char *uconfig; 2252 const char *uname; 2253 void *base, *ov; 2254 int i, err, noffset, ov_noffset; 2255 #endif 2256 2257 fit_uname = fit_unamep ? *fit_unamep : NULL; 2258 2259 if (fit_uname_configp && *fit_uname_configp) { 2260 fit_uname_config_copy = strdup(*fit_uname_configp); 2261 if (!fit_uname_config_copy) 2262 return -ENOMEM; 2263 2264 next_config = strchr(fit_uname_config_copy, '#'); 2265 if (next_config) 2266 *next_config++ = '\0'; 2267 if (next_config - 1 > fit_uname_config_copy) 2268 fit_uname_config = fit_uname_config_copy; 2269 } 2270 2271 fdt_noffset = fit_image_load(images, 2272 addr, &fit_uname, &fit_uname_config, 2273 arch, IH_TYPE_FLATDT, 2274 BOOTSTAGE_ID_FIT_FDT_START, 2275 FIT_LOAD_OPTIONAL, &load, &len); 2276 2277 if (fdt_noffset < 0) 2278 goto out; 2279 2280 debug("fit_uname=%s, fit_uname_config=%s\n", 2281 fit_uname ? fit_uname : "<NULL>", 2282 fit_uname_config ? fit_uname_config : "<NULL>"); 2283 2284 fit = map_sysmem(addr, 0); 2285 2286 cfg_noffset = fit_conf_get_node(fit, fit_uname_config); 2287 2288 /* single blob, or error just return as well */ 2289 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP); 2290 if (count <= 1 && !next_config) 2291 goto out; 2292 2293 /* we need to apply overlays */ 2294 2295 #ifdef CONFIG_OF_LIBFDT_OVERLAY 2296 image_start = addr; 2297 image_end = addr + fit_get_size(fit); 2298 /* verify that relocation took place by load address not being in fit */ 2299 if (load >= image_start && load < image_end) { 2300 /* check is simplified; fit load checks for overlaps */ 2301 printf("Overlayed FDT requires relocation\n"); 2302 fdt_noffset = -EBADF; 2303 goto out; 2304 } 2305 2306 base = map_sysmem(load, len); 2307 2308 /* apply extra configs in FIT first, followed by args */ 2309 for (i = 1; ; i++) { 2310 if (i < count) { 2311 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset, 2312 FIT_FDT_PROP, i); 2313 uname = fit_get_name(fit, noffset, NULL); 2314 uconfig = NULL; 2315 } else { 2316 if (!next_config) 2317 break; 2318 uconfig = next_config; 2319 next_config = strchr(next_config, '#'); 2320 if (next_config) 2321 *next_config++ = '\0'; 2322 uname = NULL; 2323 } 2324 2325 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig); 2326 2327 ov_noffset = fit_image_load(images, 2328 addr, &uname, &uconfig, 2329 arch, IH_TYPE_FLATDT, 2330 BOOTSTAGE_ID_FIT_FDT_START, 2331 FIT_LOAD_REQUIRED, &ovload, &ovlen); 2332 if (ov_noffset < 0) { 2333 printf("load of %s failed\n", uname); 2334 continue; 2335 } 2336 debug("%s loaded at 0x%08lx len=0x%08lx\n", 2337 uname, ovload, ovlen); 2338 ov = map_sysmem(ovload, ovlen); 2339 2340 base = map_sysmem(load, len + ovlen); 2341 err = fdt_open_into(base, base, len + ovlen); 2342 if (err < 0) { 2343 printf("failed on fdt_open_into\n"); 2344 fdt_noffset = err; 2345 goto out; 2346 } 2347 /* the verbose method prints out messages on error */ 2348 err = fdt_overlay_apply_verbose(base, ov); 2349 if (err < 0) { 2350 fdt_noffset = err; 2351 goto out; 2352 } 2353 fdt_pack(base); 2354 len = fdt_totalsize(base); 2355 } 2356 #else 2357 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n"); 2358 fdt_noffset = -EBADF; 2359 #endif 2360 2361 out: 2362 if (datap) 2363 *datap = load; 2364 if (lenp) 2365 *lenp = len; 2366 if (fit_unamep) 2367 *fit_unamep = fit_uname; 2368 if (fit_uname_configp) 2369 *fit_uname_configp = fit_uname_config; 2370 2371 if (fit_uname_config_copy) 2372 free(fit_uname_config_copy); 2373 return fdt_noffset; 2374 } 2375 #endif 2376