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_get_totalsize - get node totalsize property. 532 * 533 * @fit: pointer to the FIT image header 534 * @totalsize: holds the /totalsize property 535 * 536 * returns: 537 * 0, on success 538 * -ENOENT if the property could not be found 539 */ 540 int fit_get_totalsize(const void *fit, int *totalsize) 541 { 542 const fdt32_t *val; 543 544 val = fdt_getprop(fit, 0, FIT_TOTALSIZE_PROP, NULL); 545 if (!val) 546 return -ENOENT; 547 548 *totalsize = fdt32_to_cpu(*val); 549 550 return 0; 551 } 552 553 /** 554 * fit_image_get_node - get node offset for component image of a given unit name 555 * @fit: pointer to the FIT format image header 556 * @image_uname: component image node unit name 557 * 558 * fit_image_get_node() finds a component image (within the '/images' 559 * node) of a provided unit name. If image is found its node offset is 560 * returned to the caller. 561 * 562 * returns: 563 * image node offset when found (>=0) 564 * negative number on failure (FDT_ERR_* code) 565 */ 566 int fit_image_get_node(const void *fit, const char *image_uname) 567 { 568 int noffset, images_noffset; 569 570 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 571 if (images_noffset < 0) { 572 debug("Can't find images parent node '%s' (%s)\n", 573 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 574 return images_noffset; 575 } 576 577 noffset = fdt_subnode_offset(fit, images_noffset, image_uname); 578 if (noffset < 0) { 579 debug("Can't get node offset for image unit name: '%s' (%s)\n", 580 image_uname, fdt_strerror(noffset)); 581 } 582 583 return noffset; 584 } 585 586 /** 587 * fit_image_get_os - get os id for a given component image node 588 * @fit: pointer to the FIT format image header 589 * @noffset: component image node offset 590 * @os: pointer to the uint8_t, will hold os numeric id 591 * 592 * fit_image_get_os() finds os property in a given component image node. 593 * If the property is found, its (string) value is translated to the numeric 594 * id which is returned to the caller. 595 * 596 * returns: 597 * 0, on success 598 * -1, on failure 599 */ 600 int fit_image_get_os(const void *fit, int noffset, uint8_t *os) 601 { 602 int len; 603 const void *data; 604 605 /* Get OS name from property data */ 606 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len); 607 if (data == NULL) { 608 fit_get_debug(fit, noffset, FIT_OS_PROP, len); 609 *os = -1; 610 return -1; 611 } 612 613 /* Translate OS name to id */ 614 *os = genimg_get_os_id(data); 615 return 0; 616 } 617 618 /** 619 * fit_image_get_arch - get arch id for a given component image node 620 * @fit: pointer to the FIT format image header 621 * @noffset: component image node offset 622 * @arch: pointer to the uint8_t, will hold arch numeric id 623 * 624 * fit_image_get_arch() finds arch property in a given component image node. 625 * If the property is found, its (string) value is translated to the numeric 626 * id which is returned to the caller. 627 * 628 * returns: 629 * 0, on success 630 * -1, on failure 631 */ 632 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch) 633 { 634 int len; 635 const void *data; 636 637 /* Get architecture name from property data */ 638 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len); 639 if (data == NULL) { 640 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len); 641 *arch = -1; 642 return -1; 643 } 644 645 /* Translate architecture name to id */ 646 *arch = genimg_get_arch_id(data); 647 return 0; 648 } 649 650 /** 651 * fit_image_get_type - get type id for a given component image node 652 * @fit: pointer to the FIT format image header 653 * @noffset: component image node offset 654 * @type: pointer to the uint8_t, will hold type numeric id 655 * 656 * fit_image_get_type() finds type property in a given component image node. 657 * If the property is found, its (string) value is translated to the numeric 658 * id which is returned to the caller. 659 * 660 * returns: 661 * 0, on success 662 * -1, on failure 663 */ 664 int fit_image_get_type(const void *fit, int noffset, uint8_t *type) 665 { 666 int len; 667 const void *data; 668 669 /* Get image type name from property data */ 670 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len); 671 if (data == NULL) { 672 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len); 673 *type = -1; 674 return -1; 675 } 676 677 /* Translate image type name to id */ 678 *type = genimg_get_type_id(data); 679 return 0; 680 } 681 682 /** 683 * fit_image_get_comp - get comp id for a given component image node 684 * @fit: pointer to the FIT format image header 685 * @noffset: component image node offset 686 * @comp: pointer to the uint8_t, will hold comp numeric id 687 * 688 * fit_image_get_comp() finds comp property in a given component image node. 689 * If the property is found, its (string) value is translated to the numeric 690 * id which is returned to the caller. 691 * 692 * returns: 693 * 0, on success 694 * -1, on failure 695 */ 696 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp) 697 { 698 int len; 699 const void *data; 700 701 /* Get compression name from property data */ 702 data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len); 703 if (data == NULL) { 704 fit_get_debug(fit, noffset, FIT_COMP_PROP, len); 705 *comp = -1; 706 return -1; 707 } 708 709 /* Translate compression name to id */ 710 *comp = genimg_get_comp_id(data); 711 return 0; 712 } 713 714 static int fit_image_get_address(const void *fit, int noffset, char *name, 715 ulong *load) 716 { 717 int len, cell_len; 718 const fdt32_t *cell; 719 uint64_t load64 = 0; 720 721 cell = fdt_getprop(fit, noffset, name, &len); 722 if (cell == NULL) { 723 fit_get_debug(fit, noffset, name, len); 724 return -1; 725 } 726 727 if (len > sizeof(ulong)) { 728 printf("Unsupported %s address size\n", name); 729 return -1; 730 } 731 732 cell_len = len >> 2; 733 /* Use load64 to avoid compiling warning for 32-bit target */ 734 while (cell_len--) { 735 load64 = (load64 << 32) | uimage_to_cpu(*cell); 736 cell++; 737 } 738 *load = (ulong)load64; 739 740 return 0; 741 } 742 743 static int fit_image_set_address(const void *fit, int noffset, char *name, 744 ulong addr) 745 { 746 int len, cell_len; 747 const fdt32_t *cell; 748 749 cell = fdt_getprop(fit, noffset, name, &len); 750 if (cell == NULL) { 751 fit_get_debug(fit, noffset, name, len); 752 return -1; 753 } 754 755 if (len > sizeof(ulong)) { 756 printf("Unsupported %s address size\n", name); 757 return -1; 758 } 759 760 cell_len = len >> 2; 761 /* Use load64 to avoid compiling warning for 32-bit target */ 762 while (cell_len--) { 763 *(fdt32_t *)cell = cpu_to_uimage(addr >> (32 * cell_len)); 764 cell++; 765 } 766 767 return 0; 768 } 769 770 /** 771 * fit_image_get_load() - get load addr property for given component image node 772 * @fit: pointer to the FIT format image header 773 * @noffset: component image node offset 774 * @load: pointer to the uint32_t, will hold load address 775 * 776 * fit_image_get_load() finds load address property in a given component 777 * image node. If the property is found, its value is returned to the caller. 778 * 779 * returns: 780 * 0, on success 781 * -1, on failure 782 */ 783 int fit_image_get_load(const void *fit, int noffset, ulong *load) 784 { 785 return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load); 786 } 787 788 /** 789 * fit_image_get_comp_addr() - get compress addr property for given component image node 790 * @fit: pointer to the FIT format image header 791 * @noffset: component image node offset 792 * @comp: pointer to the uint32_t, will hold load address 793 * 794 * fit_image_get_comp_addr() finds compress address property in a given component 795 * image node. If the property is found, its value is returned to the caller. 796 * 797 * returns: 798 * 0, on success 799 * -1, on failure 800 */ 801 int fit_image_get_comp_addr(const void *fit, int noffset, ulong *comp) 802 { 803 return fit_image_get_address(fit, noffset, FIT_COMP_ADDR_PROP, comp); 804 } 805 806 /** 807 * fit_image_set_load() - set load addr property for given component image node 808 * @fit: pointer to the FIT format image header 809 * @noffset: component image node offset 810 * @load: uint32_t value, will hold load address 811 * 812 * fit_image_set_load() finds and set load address property in a given component 813 * image node. If the property is found, its value is returned to the caller. 814 * 815 * returns: 816 * 0, on success 817 * -1, on failure 818 */ 819 int fit_image_set_load(const void *fit, int noffset, ulong load) 820 { 821 return fit_image_set_address(fit, noffset, FIT_LOAD_PROP, load); 822 } 823 824 /** 825 * fit_image_get_entry() - get entry point address property 826 * @fit: pointer to the FIT format image header 827 * @noffset: component image node offset 828 * @entry: pointer to the uint32_t, will hold entry point address 829 * 830 * This gets the entry point address property for a given component image 831 * node. 832 * 833 * fit_image_get_entry() finds entry point address property in a given 834 * component image node. If the property is found, its value is returned 835 * to the caller. 836 * 837 * returns: 838 * 0, on success 839 * -1, on failure 840 */ 841 int fit_image_get_entry(const void *fit, int noffset, ulong *entry) 842 { 843 return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry); 844 } 845 846 /** 847 * fit_image_set_entry() - set entry point address property 848 * @fit: pointer to the FIT format image header 849 * @noffset: component image node offset 850 * @entry: uint32_t value, will hold entry point address 851 * 852 * This sets the entry point address property for a given component image 853 * node. 854 * 855 * fit_image_set_entry() finds and set entry point address property in a given 856 * component image node. If the property is found, its value is returned 857 * to the caller. 858 * 859 * returns: 860 * 0, on success 861 * -1, on failure 862 */ 863 int fit_image_set_entry(const void *fit, int noffset, ulong entry) 864 { 865 return fit_image_set_address(fit, noffset, FIT_ENTRY_PROP, entry); 866 } 867 868 /** 869 * fit_image_get_data - get data property and its size for a given component image node 870 * @fit: pointer to the FIT format image header 871 * @noffset: component image node offset 872 * @data: double pointer to void, will hold data property's data address 873 * @size: pointer to size_t, will hold data property's data size 874 * 875 * fit_image_get_data() finds data property in a given component image node. 876 * If the property is found its data start address and size are returned to 877 * the caller. 878 * 879 * returns: 880 * 0, on success 881 * -1, on failure 882 */ 883 int fit_image_get_data(const void *fit, int noffset, 884 const void **data, size_t *size) 885 { 886 ulong data_off = 0; 887 ulong data_pos = 0; 888 int len; 889 890 /* data */ 891 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len); 892 if (*data) { 893 *size = len; 894 return 0; 895 } 896 897 /* data-size */ 898 if (fit_image_get_data_size(fit, noffset, &len)) 899 return -ENOENT; 900 901 /* data-offset */ 902 if (!fit_image_get_data_offset(fit, noffset, (int *)&data_off)) { 903 data_off += (ulong)fit + FIT_ALIGN(fdt_totalsize(fit)); 904 *data = (void *)data_off; 905 *size = len; 906 return 0; 907 } 908 909 /* data-position */ 910 if (!fit_image_get_data_position(fit, noffset, (int *)&data_pos)) { 911 *data = (void *)(data_pos + (ulong)fit); 912 *size = len; 913 return 0; 914 } 915 916 *size = 0; 917 return -1; 918 } 919 920 /** 921 * Get 'data-offset' property from a given image node. 922 * 923 * @fit: pointer to the FIT image header 924 * @noffset: component image node offset 925 * @data_offset: holds the data-offset property 926 * 927 * returns: 928 * 0, on success 929 * -ENOENT if the property could not be found 930 */ 931 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset) 932 { 933 const fdt32_t *val; 934 935 val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL); 936 if (!val) 937 return -ENOENT; 938 939 *data_offset = fdt32_to_cpu(*val); 940 941 return 0; 942 } 943 944 /** 945 * Get 'data-position' property from a given image node. 946 * 947 * @fit: pointer to the FIT image header 948 * @noffset: component image node offset 949 * @data_position: holds the data-position property 950 * 951 * returns: 952 * 0, on success 953 * -ENOENT if the property could not be found 954 */ 955 int fit_image_get_data_position(const void *fit, int noffset, 956 int *data_position) 957 { 958 const fdt32_t *val; 959 960 val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL); 961 if (!val) 962 return -ENOENT; 963 964 *data_position = fdt32_to_cpu(*val); 965 966 return 0; 967 } 968 969 /** 970 * Get 'data-size' property from a given image node. 971 * 972 * @fit: pointer to the FIT image header 973 * @noffset: component image node offset 974 * @data_size: holds the data-size property 975 * 976 * returns: 977 * 0, on success 978 * -ENOENT if the property could not be found 979 */ 980 int fit_image_get_data_size(const void *fit, int noffset, int *data_size) 981 { 982 const fdt32_t *val; 983 984 val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL); 985 if (!val) 986 return -ENOENT; 987 988 *data_size = fdt32_to_cpu(*val); 989 990 return 0; 991 } 992 993 /** 994 * Get 'rollback-index' property from a given image node. 995 * 996 * @fit: pointer to the FIT image header 997 * @noffset: component image node offset 998 * @index: holds the rollback-index property 999 * 1000 * returns: 1001 * 0, on success 1002 * -ENOENT if the property could not be found 1003 */ 1004 int fit_image_get_rollback_index(const void *fit, int noffset, uint32_t *index) 1005 { 1006 const fdt32_t *val; 1007 1008 val = fdt_getprop(fit, noffset, FIT_ROLLBACK_PROP, NULL); 1009 if (!val) 1010 return -ENOENT; 1011 1012 *index = fdt32_to_cpu(*val); 1013 1014 return 0; 1015 } 1016 1017 /** 1018 * fit_image_hash_get_algo - get hash algorithm name 1019 * @fit: pointer to the FIT format image header 1020 * @noffset: hash node offset 1021 * @algo: double pointer to char, will hold pointer to the algorithm name 1022 * 1023 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node. 1024 * If the property is found its data start address is returned to the caller. 1025 * 1026 * returns: 1027 * 0, on success 1028 * -1, on failure 1029 */ 1030 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo) 1031 { 1032 int len; 1033 1034 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len); 1035 if (*algo == NULL) { 1036 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len); 1037 return -1; 1038 } 1039 1040 return 0; 1041 } 1042 1043 /** 1044 * fit_image_hash_get_value - get hash value and length 1045 * @fit: pointer to the FIT format image header 1046 * @noffset: hash node offset 1047 * @value: double pointer to uint8_t, will hold address of a hash value data 1048 * @value_len: pointer to an int, will hold hash data length 1049 * 1050 * fit_image_hash_get_value() finds hash value property in a given hash node. 1051 * If the property is found its data start address and size are returned to 1052 * the caller. 1053 * 1054 * returns: 1055 * 0, on success 1056 * -1, on failure 1057 */ 1058 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value, 1059 int *value_len) 1060 { 1061 int len; 1062 1063 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len); 1064 if (*value == NULL) { 1065 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len); 1066 *value_len = 0; 1067 return -1; 1068 } 1069 1070 *value_len = len; 1071 return 0; 1072 } 1073 1074 /** 1075 * fit_image_hash_get_ignore - get hash ignore flag 1076 * @fit: pointer to the FIT format image header 1077 * @noffset: hash node offset 1078 * @ignore: pointer to an int, will hold hash ignore flag 1079 * 1080 * fit_image_hash_get_ignore() finds hash ignore property in a given hash node. 1081 * If the property is found and non-zero, the hash algorithm is not verified by 1082 * u-boot automatically. 1083 * 1084 * returns: 1085 * 0, on ignore not found 1086 * value, on ignore found 1087 */ 1088 static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore) 1089 { 1090 int len; 1091 int *value; 1092 1093 value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len); 1094 if (value == NULL || len != sizeof(int)) 1095 *ignore = 0; 1096 else 1097 *ignore = *value; 1098 1099 return 0; 1100 } 1101 1102 ulong fit_get_end(const void *fit) 1103 { 1104 return map_to_sysmem((void *)(fit + fdt_totalsize(fit))); 1105 } 1106 1107 /** 1108 * fit_set_timestamp - set node timestamp property 1109 * @fit: pointer to the FIT format image header 1110 * @noffset: node offset 1111 * @timestamp: timestamp value to be set 1112 * 1113 * fit_set_timestamp() attempts to set timestamp property in the requested 1114 * node and returns operation status to the caller. 1115 * 1116 * returns: 1117 * 0, on success 1118 * -ENOSPC if no space in device tree, -1 for other error 1119 */ 1120 int fit_set_timestamp(void *fit, int noffset, time_t timestamp) 1121 { 1122 uint32_t t; 1123 int ret; 1124 1125 t = cpu_to_uimage(timestamp); 1126 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t, 1127 sizeof(uint32_t)); 1128 if (ret) { 1129 debug("Can't set '%s' property for '%s' node (%s)\n", 1130 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL), 1131 fdt_strerror(ret)); 1132 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1; 1133 } 1134 1135 return 0; 1136 } 1137 1138 int fit_set_totalsize(void *fit, int noffset, int totalsize) 1139 { 1140 uint32_t t; 1141 int ret; 1142 1143 t = cpu_to_uimage(totalsize); 1144 ret = fdt_setprop(fit, noffset, FIT_TOTALSIZE_PROP, &t, 1145 sizeof(uint32_t)); 1146 if (ret) 1147 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1; 1148 1149 return 0; 1150 } 1151 1152 int fit_set_version(void *fit, int noffset, int version) 1153 { 1154 uint32_t v; 1155 int ret; 1156 1157 v = cpu_to_uimage(version); 1158 ret = fdt_setprop(fit, noffset, FIT_VERSION_PROP, &v, sizeof(uint32_t)); 1159 if (ret) 1160 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1; 1161 1162 return 0; 1163 } 1164 1165 int fit_calculate_hash(const void *data, int data_len, 1166 const char *algo, uint8_t *value, 1167 int *value_len) 1168 { 1169 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { 1170 *((uint32_t *)value) = crc32_wd(0, data, data_len, 1171 CHUNKSZ_CRC32); 1172 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); 1173 *value_len = 4; 1174 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) { 1175 sha1_csum_wd((unsigned char *)data, data_len, 1176 (unsigned char *)value, CHUNKSZ_SHA1); 1177 *value_len = 20; 1178 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) { 1179 sha256_csum_wd((unsigned char *)data, data_len, 1180 (unsigned char *)value, CHUNKSZ_SHA256); 1181 *value_len = SHA256_SUM_LEN; 1182 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { 1183 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); 1184 *value_len = 16; 1185 } else { 1186 debug("Unsupported hash alogrithm\n"); 1187 return -1; 1188 } 1189 return 0; 1190 } 1191 1192 #ifndef USE_HOSTCC 1193 #if CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 1194 static int crypto_csum(u32 cap, const char *data, int len, u8 *output) 1195 { 1196 struct udevice *dev; 1197 sha_context csha_ctx; 1198 1199 dev = crypto_get_device(cap); 1200 if (!dev) { 1201 printf("Can't find expected crypto device\n"); 1202 return -ENODEV; 1203 } 1204 1205 csha_ctx.algo = cap; 1206 csha_ctx.length = len; 1207 1208 return crypto_sha_csum(dev, &csha_ctx, (char *)data, len, output); 1209 } 1210 1211 static int hw_fit_calculate_hash(const void *data, int data_len, 1212 const char *algo, uint8_t *value, 1213 int *value_len) 1214 { 1215 int ret = 0; 1216 1217 if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { 1218 *((uint32_t *)value) = crc32_wd(0, data, data_len, 1219 CHUNKSZ_CRC32); 1220 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); 1221 *value_len = 4; 1222 } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) { 1223 ret = crypto_csum(CRYPTO_SHA1, data, data_len, value); 1224 *value_len = 20; 1225 } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) { 1226 ret = crypto_csum(CRYPTO_SHA256, data, data_len, value); 1227 *value_len = SHA256_SUM_LEN; 1228 } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { 1229 ret = crypto_csum(CRYPTO_MD5, data, data_len, value); 1230 *value_len = 16; 1231 } else { 1232 debug("Unsupported hash alogrithm\n"); 1233 return -1; 1234 } 1235 1236 if (ret) 1237 printf("%s: algo %s failed, ret=%d\n", __func__, algo, ret); 1238 1239 return ret; 1240 } 1241 #endif 1242 #endif 1243 1244 /** 1245 * calculate_hash - calculate and return hash for provided input data 1246 * @data: pointer to the input data 1247 * @data_len: data length 1248 * @algo: requested hash algorithm 1249 * @value: pointer to the char, will hold hash value data (caller must 1250 * allocate enough free space) 1251 * value_len: length of the calculated hash 1252 * 1253 * calculate_hash() computes input data hash according to the requested 1254 * algorithm. 1255 * Resulting hash value is placed in caller provided 'value' buffer, length 1256 * of the calculated hash is returned via value_len pointer argument. 1257 * 1258 * returns: 1259 * 0, on success 1260 * -1, when algo is unsupported 1261 */ 1262 int calculate_hash(const void *data, int data_len, const char *algo, 1263 uint8_t *value, int *value_len) 1264 { 1265 #if defined(USE_HOSTCC) 1266 return fit_calculate_hash(data, data_len, algo, value, value_len); 1267 #else 1268 #if !CONFIG_IS_ENABLED(FIT_HW_CRYPTO) 1269 return fit_calculate_hash(data, data_len, algo, value, value_len); 1270 #else 1271 return hw_fit_calculate_hash(data, data_len, algo, value, value_len); 1272 #endif 1273 #endif 1274 } 1275 1276 int fit_image_check_hash(const void *fit, int noffset, const void *data, 1277 size_t size, char **err_msgp) 1278 { 1279 uint8_t value[FIT_MAX_HASH_LEN]; 1280 int value_len; 1281 char *algo; 1282 uint8_t *fit_value; 1283 int fit_value_len; 1284 int ignore; 1285 1286 *err_msgp = NULL; 1287 1288 if (fit_image_hash_get_algo(fit, noffset, &algo)) { 1289 *err_msgp = "Can't get hash algo property"; 1290 return -1; 1291 } 1292 printf("%s", algo); 1293 1294 if (IMAGE_ENABLE_IGNORE) { 1295 fit_image_hash_get_ignore(fit, noffset, &ignore); 1296 if (ignore) { 1297 printf("-skipped "); 1298 return 0; 1299 } 1300 } 1301 1302 if (fit_image_hash_get_value(fit, noffset, &fit_value, 1303 &fit_value_len)) { 1304 *err_msgp = "Can't get hash value property"; 1305 return -1; 1306 } 1307 1308 if (calculate_hash(data, size, algo, value, &value_len)) { 1309 *err_msgp = "Unsupported hash algorithm"; 1310 return -1; 1311 } 1312 1313 if (value_len != fit_value_len) { 1314 *err_msgp = "Bad hash value len"; 1315 return -1; 1316 } else if (memcmp(value, fit_value, value_len) != 0) { 1317 int i; 1318 1319 printf(" Bad hash: "); 1320 for (i = 0; i < value_len; i++) 1321 printf("%02x", value[i]); 1322 printf("\n"); 1323 1324 *err_msgp = "Bad hash value"; 1325 return -1; 1326 } 1327 1328 return 0; 1329 } 1330 1331 int fit_image_verify_with_data(const void *fit, int image_noffset, 1332 const void *data, size_t size) 1333 { 1334 int noffset = 0; 1335 char *err_msg = ""; 1336 int verify_all = 1; 1337 int ret; 1338 1339 /* Verify all required signatures */ 1340 if (IMAGE_ENABLE_VERIFY && 1341 fit_image_verify_required_sigs(fit, image_noffset, data, size, 1342 gd_fdt_blob(), &verify_all)) { 1343 err_msg = "Unable to verify required signature"; 1344 goto error; 1345 } 1346 1347 /* Process all hash subnodes of the component image node */ 1348 fdt_for_each_subnode(noffset, fit, image_noffset) { 1349 const char *name = fit_get_name(fit, noffset, NULL); 1350 1351 /* 1352 * Check subnode name, must be equal to "hash". 1353 * Multiple hash nodes require unique unit node 1354 * names, e.g. hash@1, hash@2, etc. 1355 */ 1356 if (!strncmp(name, FIT_HASH_NODENAME, 1357 strlen(FIT_HASH_NODENAME))) { 1358 if (fit_image_check_hash(fit, noffset, data, size, 1359 &err_msg)) 1360 goto error; 1361 puts("+ "); 1362 } else if (IMAGE_ENABLE_VERIFY && verify_all && 1363 !strncmp(name, FIT_SIG_NODENAME, 1364 strlen(FIT_SIG_NODENAME))) { 1365 ret = fit_image_check_sig(fit, noffset, data, 1366 size, -1, &err_msg); 1367 1368 /* 1369 * Show an indication on failure, but do not return 1370 * an error. Only keys marked 'required' can cause 1371 * an image validation failure. See the call to 1372 * fit_image_verify_required_sigs() above. 1373 */ 1374 if (ret) 1375 puts("- "); 1376 else 1377 puts("+ "); 1378 } 1379 } 1380 1381 if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) { 1382 err_msg = "Corrupted or truncated tree"; 1383 goto error; 1384 } 1385 1386 return 1; /* success */ 1387 1388 error: 1389 printf(" error!\n%s for '%s' hash node in '%s' image node\n", 1390 err_msg, fit_get_name(fit, noffset, NULL), 1391 fit_get_name(fit, image_noffset, NULL)); 1392 return 0; 1393 } 1394 1395 /** 1396 * fit_image_verify - verify data integrity 1397 * @fit: pointer to the FIT format image header 1398 * @image_noffset: component image node offset 1399 * 1400 * fit_image_verify() goes over component image hash nodes, 1401 * re-calculates each data hash and compares with the value stored in hash 1402 * node. 1403 * 1404 * returns: 1405 * 1, if all hashes are valid 1406 * 0, otherwise (or on error) 1407 */ 1408 int fit_image_verify(const void *fit, int image_noffset) 1409 { 1410 const void *data; 1411 size_t size; 1412 int noffset = 0; 1413 char *err_msg = ""; 1414 1415 /* Get image data and data length */ 1416 if (fit_image_get_data(fit, image_noffset, &data, &size)) { 1417 err_msg = "Can't get image data/size"; 1418 printf("error!\n%s for '%s' hash node in '%s' image node\n", 1419 err_msg, fit_get_name(fit, noffset, NULL), 1420 fit_get_name(fit, image_noffset, NULL)); 1421 return 0; 1422 } 1423 1424 return fit_image_verify_with_data(fit, image_noffset, data, size); 1425 } 1426 1427 /** 1428 * fit_all_image_verify - verify data integrity for all images 1429 * @fit: pointer to the FIT format image header 1430 * 1431 * fit_all_image_verify() goes over all images in the FIT and 1432 * for every images checks if all it's hashes are valid. 1433 * 1434 * returns: 1435 * 1, if all hashes of all images are valid 1436 * 0, otherwise (or on error) 1437 */ 1438 int fit_all_image_verify(const void *fit) 1439 { 1440 int images_noffset; 1441 int noffset; 1442 int ndepth; 1443 int count; 1444 1445 /* Find images parent node offset */ 1446 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 1447 if (images_noffset < 0) { 1448 printf("Can't find images parent node '%s' (%s)\n", 1449 FIT_IMAGES_PATH, fdt_strerror(images_noffset)); 1450 return 0; 1451 } 1452 1453 /* Process all image subnodes, check hashes for each */ 1454 printf("## Checking hash(es) for FIT Image at %08lx ...\n", 1455 (ulong)fit); 1456 for (ndepth = 0, count = 0, 1457 noffset = fdt_next_node(fit, images_noffset, &ndepth); 1458 (noffset >= 0) && (ndepth > 0); 1459 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1460 if (ndepth == 1) { 1461 /* 1462 * Direct child node of the images parent node, 1463 * i.e. component image node. 1464 */ 1465 printf(" Hash(es) for Image %u (%s): ", count, 1466 fit_get_name(fit, noffset, NULL)); 1467 count++; 1468 1469 if (!fit_image_verify(fit, noffset)) 1470 return 0; 1471 printf("\n"); 1472 } 1473 } 1474 return 1; 1475 } 1476 1477 /** 1478 * fit_image_check_os - check whether image node is of a given os type 1479 * @fit: pointer to the FIT format image header 1480 * @noffset: component image node offset 1481 * @os: requested image os 1482 * 1483 * fit_image_check_os() reads image os property and compares its numeric 1484 * id with the requested os. Comparison result is returned to the caller. 1485 * 1486 * returns: 1487 * 1 if image is of given os type 1488 * 0 otherwise (or on error) 1489 */ 1490 int fit_image_check_os(const void *fit, int noffset, uint8_t os) 1491 { 1492 uint8_t image_os; 1493 1494 if (fit_image_get_os(fit, noffset, &image_os)) 1495 return 0; 1496 return (os == image_os); 1497 } 1498 1499 /** 1500 * fit_image_check_arch - check whether image node is of a given arch 1501 * @fit: pointer to the FIT format image header 1502 * @noffset: component image node offset 1503 * @arch: requested imagearch 1504 * 1505 * fit_image_check_arch() reads image arch property and compares its numeric 1506 * id with the requested arch. Comparison result is returned to the caller. 1507 * 1508 * returns: 1509 * 1 if image is of given arch 1510 * 0 otherwise (or on error) 1511 */ 1512 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch) 1513 { 1514 uint8_t image_arch; 1515 int aarch32_support = 0; 1516 1517 #ifdef CONFIG_ARM64_SUPPORT_AARCH32 1518 aarch32_support = 1; 1519 #endif 1520 1521 if (fit_image_get_arch(fit, noffset, &image_arch)) 1522 return 0; 1523 return (arch == image_arch) || 1524 (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) || 1525 (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM && 1526 aarch32_support); 1527 } 1528 1529 /** 1530 * fit_image_check_type - check whether image node is of a given type 1531 * @fit: pointer to the FIT format image header 1532 * @noffset: component image node offset 1533 * @type: requested image type 1534 * 1535 * fit_image_check_type() reads image type property and compares its numeric 1536 * id with the requested type. Comparison result is returned to the caller. 1537 * 1538 * returns: 1539 * 1 if image is of given type 1540 * 0 otherwise (or on error) 1541 */ 1542 int fit_image_check_type(const void *fit, int noffset, uint8_t type) 1543 { 1544 uint8_t image_type; 1545 1546 if (fit_image_get_type(fit, noffset, &image_type)) 1547 return 0; 1548 return (type == image_type); 1549 } 1550 1551 /** 1552 * fit_image_check_comp - check whether image node uses given compression 1553 * @fit: pointer to the FIT format image header 1554 * @noffset: component image node offset 1555 * @comp: requested image compression type 1556 * 1557 * fit_image_check_comp() reads image compression property and compares its 1558 * numeric id with the requested compression type. Comparison result is 1559 * returned to the caller. 1560 * 1561 * returns: 1562 * 1 if image uses requested compression 1563 * 0 otherwise (or on error) 1564 */ 1565 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp) 1566 { 1567 uint8_t image_comp; 1568 1569 if (fit_image_get_comp(fit, noffset, &image_comp)) 1570 return 0; 1571 return (comp == image_comp); 1572 } 1573 1574 /** 1575 * fit_check_format - sanity check FIT image format 1576 * @fit: pointer to the FIT format image header 1577 * 1578 * fit_check_format() runs a basic sanity FIT image verification. 1579 * Routine checks for mandatory properties, nodes, etc. 1580 * 1581 * returns: 1582 * 1, on success 1583 * 0, on failure 1584 */ 1585 int fit_check_format(const void *fit) 1586 { 1587 /* mandatory / node 'description' property */ 1588 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) { 1589 debug("Wrong FIT format: no description\n"); 1590 return 0; 1591 } 1592 1593 if (IMAGE_ENABLE_TIMESTAMP) { 1594 /* mandatory / node 'timestamp' property */ 1595 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) { 1596 debug("Wrong FIT format: no timestamp\n"); 1597 return 0; 1598 } 1599 } 1600 1601 /* mandatory subimages parent '/images' node */ 1602 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) { 1603 debug("Wrong FIT format: no images parent node\n"); 1604 return 0; 1605 } 1606 1607 return 1; 1608 } 1609 1610 1611 /** 1612 * fit_conf_find_compat 1613 * @fit: pointer to the FIT format image header 1614 * @fdt: pointer to the device tree to compare against 1615 * 1616 * fit_conf_find_compat() attempts to find the configuration whose fdt is the 1617 * most compatible with the passed in device tree. 1618 * 1619 * Example: 1620 * 1621 * / o image-tree 1622 * |-o images 1623 * | |-o fdt@1 1624 * | |-o fdt@2 1625 * | 1626 * |-o configurations 1627 * |-o config@1 1628 * | |-fdt = fdt@1 1629 * | 1630 * |-o config@2 1631 * |-fdt = fdt@2 1632 * 1633 * / o U-Boot fdt 1634 * |-compatible = "foo,bar", "bim,bam" 1635 * 1636 * / o kernel fdt1 1637 * |-compatible = "foo,bar", 1638 * 1639 * / o kernel fdt2 1640 * |-compatible = "bim,bam", "baz,biz" 1641 * 1642 * Configuration 1 would be picked because the first string in U-Boot's 1643 * compatible list, "foo,bar", matches a compatible string in the root of fdt1. 1644 * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1. 1645 * 1646 * returns: 1647 * offset to the configuration to use if one was found 1648 * -1 otherwise 1649 */ 1650 int fit_conf_find_compat(const void *fit, const void *fdt) 1651 { 1652 int ndepth = 0; 1653 int noffset, confs_noffset, images_noffset; 1654 const void *fdt_compat; 1655 int fdt_compat_len; 1656 int best_match_offset = 0; 1657 int best_match_pos = 0; 1658 1659 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 1660 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); 1661 if (confs_noffset < 0 || images_noffset < 0) { 1662 debug("Can't find configurations or images nodes.\n"); 1663 return -1; 1664 } 1665 1666 fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len); 1667 if (!fdt_compat) { 1668 debug("Fdt for comparison has no \"compatible\" property.\n"); 1669 return -1; 1670 } 1671 1672 /* 1673 * Loop over the configurations in the FIT image. 1674 */ 1675 for (noffset = fdt_next_node(fit, confs_noffset, &ndepth); 1676 (noffset >= 0) && (ndepth > 0); 1677 noffset = fdt_next_node(fit, noffset, &ndepth)) { 1678 const void *kfdt; 1679 const char *kfdt_name; 1680 int kfdt_noffset; 1681 const char *cur_fdt_compat; 1682 int len; 1683 size_t size; 1684 int i; 1685 1686 if (ndepth > 1) 1687 continue; 1688 1689 kfdt_name = fdt_getprop(fit, noffset, "fdt", &len); 1690 if (!kfdt_name) { 1691 debug("No fdt property found.\n"); 1692 continue; 1693 } 1694 kfdt_noffset = fdt_subnode_offset(fit, images_noffset, 1695 kfdt_name); 1696 if (kfdt_noffset < 0) { 1697 debug("No image node named \"%s\" found.\n", 1698 kfdt_name); 1699 continue; 1700 } 1701 /* 1702 * Get a pointer to this configuration's fdt. 1703 */ 1704 if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) { 1705 debug("Failed to get fdt \"%s\".\n", kfdt_name); 1706 continue; 1707 } 1708 1709 len = fdt_compat_len; 1710 cur_fdt_compat = fdt_compat; 1711 /* 1712 * Look for a match for each U-Boot compatibility string in 1713 * turn in this configuration's fdt. 1714 */ 1715 for (i = 0; len > 0 && 1716 (!best_match_offset || best_match_pos > i); i++) { 1717 int cur_len = strlen(cur_fdt_compat) + 1; 1718 1719 if (!fdt_node_check_compatible(kfdt, 0, 1720 cur_fdt_compat)) { 1721 best_match_offset = noffset; 1722 best_match_pos = i; 1723 break; 1724 } 1725 len -= cur_len; 1726 cur_fdt_compat += cur_len; 1727 } 1728 } 1729 if (!best_match_offset) { 1730 debug("No match found.\n"); 1731 return -1; 1732 } 1733 1734 return best_match_offset; 1735 } 1736 1737 /** 1738 * fit_conf_get_node - get node offset for configuration of a given unit name 1739 * @fit: pointer to the FIT format image header 1740 * @conf_uname: configuration node unit name 1741 * 1742 * fit_conf_get_node() finds a configuration (within the '/configurations' 1743 * parent node) of a provided unit name. If configuration is found its node 1744 * offset is returned to the caller. 1745 * 1746 * When NULL is provided in second argument fit_conf_get_node() will search 1747 * for a default configuration node instead. Default configuration node unit 1748 * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations' 1749 * node. 1750 * 1751 * returns: 1752 * configuration node offset when found (>=0) 1753 * negative number on failure (FDT_ERR_* code) 1754 */ 1755 int fit_conf_get_node(const void *fit, const char *conf_uname) 1756 { 1757 int noffset, confs_noffset; 1758 int len; 1759 const char *s; 1760 char *conf_uname_copy = NULL; 1761 1762 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH); 1763 if (confs_noffset < 0) { 1764 debug("Can't find configurations parent node '%s' (%s)\n", 1765 FIT_CONFS_PATH, fdt_strerror(confs_noffset)); 1766 return confs_noffset; 1767 } 1768 1769 if (conf_uname == NULL) { 1770 /* get configuration unit name from the default property */ 1771 debug("No configuration specified, trying default...\n"); 1772 conf_uname = (char *)fdt_getprop(fit, confs_noffset, 1773 FIT_DEFAULT_PROP, &len); 1774 if (conf_uname == NULL) { 1775 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP, 1776 len); 1777 return len; 1778 } 1779 debug("Found default configuration: '%s'\n", conf_uname); 1780 } 1781 1782 s = strchr(conf_uname, '#'); 1783 if (s) { 1784 len = s - conf_uname; 1785 conf_uname_copy = malloc(len + 1); 1786 if (!conf_uname_copy) { 1787 debug("Can't allocate uname copy: '%s'\n", 1788 conf_uname); 1789 return -ENOMEM; 1790 } 1791 memcpy(conf_uname_copy, conf_uname, len); 1792 conf_uname_copy[len] = '\0'; 1793 conf_uname = conf_uname_copy; 1794 } 1795 1796 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname); 1797 if (noffset < 0) { 1798 debug("Can't get node offset for configuration unit name: '%s' (%s)\n", 1799 conf_uname, fdt_strerror(noffset)); 1800 } 1801 1802 if (conf_uname_copy) 1803 free(conf_uname_copy); 1804 1805 return noffset; 1806 } 1807 1808 int fit_conf_get_prop_node_count(const void *fit, int noffset, 1809 const char *prop_name) 1810 { 1811 return fdt_stringlist_count(fit, noffset, prop_name); 1812 } 1813 1814 int fit_conf_get_prop_node_index(const void *fit, int noffset, 1815 const char *prop_name, int index) 1816 { 1817 const char *uname; 1818 int len; 1819 1820 /* get kernel image unit name from configuration kernel property */ 1821 uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len); 1822 if (uname == NULL) 1823 return len; 1824 1825 return fit_image_get_node(fit, uname); 1826 } 1827 1828 int fit_conf_get_prop_node(const void *fit, int noffset, 1829 const char *prop_name) 1830 { 1831 return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0); 1832 } 1833 1834 /** 1835 * fit_conf_print - prints out the FIT configuration details 1836 * @fit: pointer to the FIT format image header 1837 * @noffset: offset of the configuration node 1838 * @p: pointer to prefix string 1839 * 1840 * fit_conf_print() lists all mandatory properties for the processed 1841 * configuration node. 1842 * 1843 * returns: 1844 * no returned results 1845 */ 1846 void fit_conf_print(const void *fit, int noffset, const char *p) 1847 { 1848 char *desc; 1849 const char *uname; 1850 int ret; 1851 int fdt_index, loadables_index; 1852 1853 /* Mandatory properties */ 1854 ret = fit_get_desc(fit, noffset, &desc); 1855 printf("%s Description: ", p); 1856 if (ret) 1857 printf("unavailable\n"); 1858 else 1859 printf("%s\n", desc); 1860 1861 uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL); 1862 printf("%s Kernel: ", p); 1863 if (uname == NULL) 1864 printf("unavailable\n"); 1865 else 1866 printf("%s\n", uname); 1867 1868 /* Optional properties */ 1869 uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL); 1870 if (uname) 1871 printf("%s Init Ramdisk: %s\n", p, uname); 1872 1873 uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL); 1874 if (uname) 1875 printf("%s Firmware: %s\n", p, uname); 1876 1877 for (fdt_index = 0; 1878 uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP, 1879 fdt_index, NULL), uname; 1880 fdt_index++) { 1881 1882 if (fdt_index == 0) 1883 printf("%s FDT: ", p); 1884 else 1885 printf("%s ", p); 1886 printf("%s\n", uname); 1887 } 1888 1889 uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL); 1890 if (uname) 1891 printf("%s FPGA: %s\n", p, uname); 1892 1893 /* Print out all of the specified loadables */ 1894 for (loadables_index = 0; 1895 uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP, 1896 loadables_index, NULL), uname; 1897 loadables_index++) { 1898 if (loadables_index == 0) { 1899 printf("%s Loadables: ", p); 1900 } else { 1901 printf("%s ", p); 1902 } 1903 printf("%s\n", uname); 1904 } 1905 } 1906 1907 static int fit_image_select(const void *fit, int rd_noffset, int verify) 1908 { 1909 fit_image_print(fit, rd_noffset, " "); 1910 1911 if (verify) { 1912 puts(" Verifying Hash Integrity ... "); 1913 if (!fit_image_verify(fit, rd_noffset)) { 1914 puts("Bad Data Hash\n"); 1915 return -EACCES; 1916 } 1917 puts("OK\n"); 1918 } 1919 1920 return 0; 1921 } 1922 1923 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name, 1924 ulong addr) 1925 { 1926 int cfg_noffset; 1927 void *fit_hdr; 1928 int noffset; 1929 1930 debug("* %s: using config '%s' from image at 0x%08lx\n", 1931 prop_name, images->fit_uname_cfg, addr); 1932 1933 /* Check whether configuration has this property defined */ 1934 fit_hdr = map_sysmem(addr, 0); 1935 cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg); 1936 if (cfg_noffset < 0) { 1937 debug("* %s: no such config\n", prop_name); 1938 return -EINVAL; 1939 } 1940 1941 noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name); 1942 if (noffset < 0) { 1943 debug("* %s: no '%s' in config\n", prop_name, prop_name); 1944 return -ENOENT; 1945 } 1946 1947 return noffset; 1948 } 1949 1950 /** 1951 * fit_get_image_type_property() - get property name for IH_TYPE_... 1952 * 1953 * @return the properly name where we expect to find the image in the 1954 * config node 1955 */ 1956 static const char *fit_get_image_type_property(int type) 1957 { 1958 /* 1959 * This is sort-of available in the uimage_type[] table in image.c 1960 * but we don't have access to the short name, and "fdt" is different 1961 * anyway. So let's just keep it here. 1962 */ 1963 switch (type) { 1964 case IH_TYPE_FLATDT: 1965 return FIT_FDT_PROP; 1966 case IH_TYPE_KERNEL: 1967 return FIT_KERNEL_PROP; 1968 case IH_TYPE_RAMDISK: 1969 return FIT_RAMDISK_PROP; 1970 case IH_TYPE_FIRMWARE: 1971 return FIT_FIRMWARE_PROP; 1972 case IH_TYPE_X86_SETUP: 1973 return FIT_SETUP_PROP; 1974 case IH_TYPE_LOADABLE: 1975 return FIT_LOADABLE_PROP; 1976 case IH_TYPE_FPGA: 1977 return FIT_FPGA_PROP; 1978 case IH_TYPE_STANDALONE: 1979 return FIT_STANDALONE_PROP; 1980 } 1981 1982 return "unknown"; 1983 } 1984 1985 #ifndef USE_HOSTCC 1986 __weak int fit_board_verify_required_sigs(void) 1987 { 1988 return 0; 1989 } 1990 #endif 1991 1992 int fit_image_load_index(bootm_headers_t *images, ulong addr, 1993 const char **fit_unamep, const char **fit_uname_configp, 1994 int arch, int image_type, int image_index, int bootstage_id, 1995 enum fit_load_op load_op, ulong *datap, ulong *lenp) 1996 { 1997 int cfg_noffset, noffset; 1998 const char *fit_uname; 1999 const char *fit_uname_config; 2000 const char *fit_base_uname_config; 2001 const void *fit; 2002 const void *buf; 2003 size_t size; 2004 int type_ok, os_ok; 2005 ulong load, data, len; 2006 uint8_t os; 2007 #ifndef USE_HOSTCC 2008 uint8_t os_arch; 2009 #endif 2010 const char *prop_name; 2011 int ret; 2012 2013 fit = map_sysmem(addr, 0); 2014 fit_uname = fit_unamep ? *fit_unamep : NULL; 2015 fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL; 2016 fit_base_uname_config = NULL; 2017 prop_name = fit_get_image_type_property(image_type); 2018 printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr); 2019 2020 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT); 2021 if (!fit_check_format(fit)) { 2022 printf("Bad FIT %s image format!\n", prop_name); 2023 bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT); 2024 return -ENOEXEC; 2025 } 2026 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK); 2027 if (fit_uname) { 2028 /* get FIT component image node offset */ 2029 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME); 2030 noffset = fit_image_get_node(fit, fit_uname); 2031 } else { 2032 /* 2033 * no image node unit name, try to get config 2034 * node first. If config unit node name is NULL 2035 * fit_conf_get_node() will try to find default config node 2036 */ 2037 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME); 2038 if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) { 2039 cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob()); 2040 } else { 2041 cfg_noffset = fit_conf_get_node(fit, 2042 fit_uname_config); 2043 } 2044 if (cfg_noffset < 0) { 2045 puts("Could not find configuration node\n"); 2046 bootstage_error(bootstage_id + 2047 BOOTSTAGE_SUB_NO_UNIT_NAME); 2048 return -ENOENT; 2049 } 2050 fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL); 2051 printf(" Using '%s' configuration\n", fit_base_uname_config); 2052 if (image_type == IH_TYPE_KERNEL) { 2053 #ifndef USE_HOSTCC 2054 /* If board required sigs, check self */ 2055 if (fit_board_verify_required_sigs() && 2056 !IS_ENABLED(CONFIG_FIT_SIGNATURE)) { 2057 printf("Verified-boot requires CONFIG_FIT_SIGNATURE enabled\n"); 2058 hang(); 2059 } 2060 #endif 2061 /* Remember (and possibly verify) this config */ 2062 images->fit_uname_cfg = fit_base_uname_config; 2063 if (IMAGE_ENABLE_VERIFY) { 2064 puts(" Verifying Hash Integrity ... "); 2065 if (fit_config_verify(fit, cfg_noffset)) { 2066 puts("Bad Data Hash\n"); 2067 bootstage_error(bootstage_id + 2068 BOOTSTAGE_SUB_HASH); 2069 return -EACCES; 2070 } 2071 puts("OK\n"); 2072 2073 #ifdef CONFIG_FIT_ROLLBACK_PROTECT 2074 uint32_t this_index, min_index; 2075 2076 puts(" Verifying Rollback-index ... "); 2077 if (fit_rollback_index_verify(fit, 2078 FIT_ROLLBACK_INDEX, 2079 &this_index, &min_index)) { 2080 puts("Failed to get index\n"); 2081 return ret; 2082 } else if (this_index < min_index) { 2083 printf("Reject index %d < %d(min)\n", 2084 this_index, min_index); 2085 return -EINVAL; 2086 } 2087 2088 printf("%d >= %d(min), OK\n", this_index, min_index); 2089 #endif 2090 } 2091 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG); 2092 } 2093 2094 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset, 2095 prop_name, image_index); 2096 fit_uname = fit_get_name(fit, noffset, NULL); 2097 } 2098 if (noffset < 0) { 2099 puts("Could not find subimage node\n"); 2100 bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE); 2101 return -ENOENT; 2102 } 2103 2104 printf(" Trying '%s' %s subimage\n", fit_uname, prop_name); 2105 2106 ret = fit_image_select(fit, noffset, images->verify); 2107 if (ret) { 2108 bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH); 2109 return ret; 2110 } 2111 2112 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); 2113 #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX) 2114 if (!fit_image_check_target_arch(fit, noffset)) { 2115 puts("Unsupported Architecture\n"); 2116 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH); 2117 return -ENOEXEC; 2118 } 2119 #endif 2120 2121 #ifndef USE_HOSTCC 2122 fit_image_get_arch(fit, noffset, &os_arch); 2123 images->os.arch = os_arch; 2124 #endif 2125 2126 if (image_type == IH_TYPE_FLATDT && 2127 !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) { 2128 puts("FDT image is compressed"); 2129 return -EPROTONOSUPPORT; 2130 } 2131 2132 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); 2133 type_ok = fit_image_check_type(fit, noffset, image_type) || 2134 fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) || 2135 (image_type == IH_TYPE_KERNEL && 2136 fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD)); 2137 2138 os_ok = image_type == IH_TYPE_FLATDT || 2139 image_type == IH_TYPE_FPGA || 2140 fit_image_check_os(fit, noffset, IH_OS_LINUX) || 2141 fit_image_check_os(fit, noffset, IH_OS_ARM_TRUSTED_FIRMWARE) || 2142 fit_image_check_os(fit, noffset, IH_OS_OP_TEE) || 2143 fit_image_check_os(fit, noffset, IH_OS_U_BOOT) || 2144 fit_image_check_os(fit, noffset, IH_OS_OPENRTOS); 2145 2146 /* 2147 * If either of the checks fail, we should report an error, but 2148 * if the image type is coming from the "loadables" field, we 2149 * don't care what it is 2150 */ 2151 if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) { 2152 fit_image_get_os(fit, noffset, &os); 2153 printf("No %s %s %s Image\n", 2154 genimg_get_os_name(os), 2155 genimg_get_arch_name(arch), 2156 genimg_get_type_name(image_type)); 2157 bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL); 2158 return -EIO; 2159 } 2160 2161 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK); 2162 2163 /* get image data address and length */ 2164 if (fit_image_get_data(fit, noffset, &buf, &size)) { 2165 printf("Could not find %s subimage data!\n", prop_name); 2166 bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA); 2167 return -ENOENT; 2168 } 2169 2170 #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS) 2171 ret = fit_image_get_load(fit, noffset, &load); 2172 if (ret < 0) 2173 return ret; 2174 2175 /* perform any post-processing on the image data */ 2176 board_fit_image_post_process((void *)fit, noffset, 2177 &load, (ulong **)&buf, &size); 2178 #endif 2179 2180 len = (ulong)size; 2181 2182 /* verify that image data is a proper FDT blob */ 2183 if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) { 2184 puts("Subimage data is not a FDT"); 2185 return -ENOEXEC; 2186 } 2187 2188 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK); 2189 2190 /* 2191 * Work-around for eldk-4.2 which gives this warning if we try to 2192 * cast in the unmap_sysmem() call: 2193 * warning: initialization discards qualifiers from pointer target type 2194 */ 2195 { 2196 void *vbuf = (void *)buf; 2197 2198 data = map_to_sysmem(vbuf); 2199 } 2200 2201 if (load_op == FIT_LOAD_IGNORED) { 2202 /* Don't load */ 2203 } else if (fit_image_get_load(fit, noffset, &load)) { 2204 if (load_op == FIT_LOAD_REQUIRED) { 2205 printf("Can't get %s subimage load address!\n", 2206 prop_name); 2207 bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD); 2208 return -EBADF; 2209 } 2210 } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) { 2211 ulong image_start, image_end; 2212 ulong load_end; 2213 void *dst; 2214 2215 /* 2216 * move image data to the load address, 2217 * make sure we don't overwrite initial image 2218 */ 2219 image_start = addr; 2220 image_end = addr + fit_get_size(fit); 2221 2222 load_end = load + len; 2223 if (image_type != IH_TYPE_KERNEL && 2224 load < image_end && load_end > image_start) { 2225 printf("Error: %s overwritten\n", prop_name); 2226 return -EXDEV; 2227 } 2228 2229 printf(" Loading %s from 0x%08lx to 0x%08lx\n", 2230 prop_name, data, load); 2231 2232 dst = map_sysmem(load, len); 2233 memmove(dst, buf, len); 2234 data = load; 2235 } 2236 bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD); 2237 2238 *datap = data; 2239 *lenp = len; 2240 if (fit_unamep) 2241 *fit_unamep = (char *)fit_uname; 2242 if (fit_uname_configp) 2243 *fit_uname_configp = (char *)(fit_uname_config ? : 2244 fit_base_uname_config); 2245 2246 return noffset; 2247 } 2248 2249 int fit_image_load(bootm_headers_t *images, ulong addr, 2250 const char **fit_unamep, const char **fit_uname_configp, 2251 int arch, int image_type, int bootstage_id, 2252 enum fit_load_op load_op, ulong *datap, ulong *lenp) 2253 { 2254 return fit_image_load_index(images, addr,fit_unamep, fit_uname_configp, 2255 arch, image_type, 0, bootstage_id, 2256 load_op, datap, lenp); 2257 } 2258 2259 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch, 2260 ulong *setup_start, ulong *setup_len) 2261 { 2262 int noffset; 2263 ulong addr; 2264 ulong len; 2265 int ret; 2266 2267 addr = map_to_sysmem(images->fit_hdr_os); 2268 noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr); 2269 if (noffset < 0) 2270 return noffset; 2271 2272 ret = fit_image_load(images, addr, NULL, NULL, arch, 2273 IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START, 2274 FIT_LOAD_REQUIRED, setup_start, &len); 2275 2276 return ret; 2277 } 2278 2279 #ifndef USE_HOSTCC 2280 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr, 2281 const char **fit_unamep, const char **fit_uname_configp, 2282 int arch, ulong *datap, ulong *lenp) 2283 { 2284 int fdt_noffset, cfg_noffset, count; 2285 const void *fit; 2286 const char *fit_uname = NULL; 2287 const char *fit_uname_config = NULL; 2288 char *fit_uname_config_copy = NULL; 2289 char *next_config = NULL; 2290 ulong load, len; 2291 #ifdef CONFIG_OF_LIBFDT_OVERLAY 2292 ulong image_start, image_end; 2293 ulong ovload, ovlen; 2294 const char *uconfig; 2295 const char *uname; 2296 void *base, *ov; 2297 int i, err, noffset, ov_noffset; 2298 #endif 2299 2300 fit_uname = fit_unamep ? *fit_unamep : NULL; 2301 2302 if (fit_uname_configp && *fit_uname_configp) { 2303 fit_uname_config_copy = strdup(*fit_uname_configp); 2304 if (!fit_uname_config_copy) 2305 return -ENOMEM; 2306 2307 next_config = strchr(fit_uname_config_copy, '#'); 2308 if (next_config) 2309 *next_config++ = '\0'; 2310 if (next_config - 1 > fit_uname_config_copy) 2311 fit_uname_config = fit_uname_config_copy; 2312 } 2313 2314 fdt_noffset = fit_image_load(images, 2315 addr, &fit_uname, &fit_uname_config, 2316 arch, IH_TYPE_FLATDT, 2317 BOOTSTAGE_ID_FIT_FDT_START, 2318 FIT_LOAD_OPTIONAL, &load, &len); 2319 2320 if (fdt_noffset < 0) 2321 goto out; 2322 2323 debug("fit_uname=%s, fit_uname_config=%s\n", 2324 fit_uname ? fit_uname : "<NULL>", 2325 fit_uname_config ? fit_uname_config : "<NULL>"); 2326 2327 fit = map_sysmem(addr, 0); 2328 2329 cfg_noffset = fit_conf_get_node(fit, fit_uname_config); 2330 2331 /* single blob, or error just return as well */ 2332 count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP); 2333 if (count <= 1 && !next_config) 2334 goto out; 2335 2336 /* we need to apply overlays */ 2337 2338 #ifdef CONFIG_OF_LIBFDT_OVERLAY 2339 image_start = addr; 2340 image_end = addr + fit_get_size(fit); 2341 /* verify that relocation took place by load address not being in fit */ 2342 if (load >= image_start && load < image_end) { 2343 /* check is simplified; fit load checks for overlaps */ 2344 printf("Overlayed FDT requires relocation\n"); 2345 fdt_noffset = -EBADF; 2346 goto out; 2347 } 2348 2349 base = map_sysmem(load, len); 2350 2351 /* apply extra configs in FIT first, followed by args */ 2352 for (i = 1; ; i++) { 2353 if (i < count) { 2354 noffset = fit_conf_get_prop_node_index(fit, cfg_noffset, 2355 FIT_FDT_PROP, i); 2356 uname = fit_get_name(fit, noffset, NULL); 2357 uconfig = NULL; 2358 } else { 2359 if (!next_config) 2360 break; 2361 uconfig = next_config; 2362 next_config = strchr(next_config, '#'); 2363 if (next_config) 2364 *next_config++ = '\0'; 2365 uname = NULL; 2366 } 2367 2368 debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig); 2369 2370 ov_noffset = fit_image_load(images, 2371 addr, &uname, &uconfig, 2372 arch, IH_TYPE_FLATDT, 2373 BOOTSTAGE_ID_FIT_FDT_START, 2374 FIT_LOAD_REQUIRED, &ovload, &ovlen); 2375 if (ov_noffset < 0) { 2376 printf("load of %s failed\n", uname); 2377 continue; 2378 } 2379 debug("%s loaded at 0x%08lx len=0x%08lx\n", 2380 uname, ovload, ovlen); 2381 ov = map_sysmem(ovload, ovlen); 2382 2383 base = map_sysmem(load, len + ovlen); 2384 err = fdt_open_into(base, base, len + ovlen); 2385 if (err < 0) { 2386 printf("failed on fdt_open_into\n"); 2387 fdt_noffset = err; 2388 goto out; 2389 } 2390 /* the verbose method prints out messages on error */ 2391 err = fdt_overlay_apply_verbose(base, ov); 2392 if (err < 0) { 2393 fdt_noffset = err; 2394 goto out; 2395 } 2396 fdt_pack(base); 2397 len = fdt_totalsize(base); 2398 } 2399 #else 2400 printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n"); 2401 fdt_noffset = -EBADF; 2402 #endif 2403 2404 out: 2405 if (datap) 2406 *datap = load; 2407 if (lenp) 2408 *lenp = len; 2409 if (fit_unamep) 2410 *fit_unamep = fit_uname; 2411 if (fit_uname_configp) 2412 *fit_uname_configp = fit_uname_config; 2413 2414 if (fit_uname_config_copy) 2415 free(fit_uname_config_copy); 2416 return fdt_noffset; 2417 } 2418 #endif 2419