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