1 /* 2 * Copyright (C) 2016 Google, Inc 3 * Written by Simon Glass <sjg@chromium.org> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <errno.h> 10 #include <image.h> 11 #include <linux/libfdt.h> 12 #include <spl.h> 13 #include <malloc.h> 14 #include <optee_include/OpteeClientInterface.h> 15 16 #ifndef CONFIG_SYS_BOOTM_LEN 17 #define CONFIG_SYS_BOOTM_LEN (64 << 20) 18 #endif 19 20 /** 21 * spl_fit_get_image_name(): By using the matching configuration subnode, 22 * retrieve the name of an image, specified by a property name and an index 23 * into that. 24 * @fit: Pointer to the FDT blob. 25 * @images: Offset of the /images subnode. 26 * @type: Name of the property within the configuration subnode. 27 * @index: Index into the list of strings in this property. 28 * @outname: Name of the image 29 * 30 * Return: 0 on success, or a negative error number 31 */ 32 static int spl_fit_get_image_name(const void *fit, int images, 33 const char *type, int index, 34 char **outname) 35 { 36 const char *name, *str; 37 __maybe_unused int node; 38 int conf_node; 39 int len, i; 40 41 conf_node = fit_find_config_node(fit); 42 if (conf_node < 0) { 43 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 44 printf("No matching DT out of these options:\n"); 45 for (node = fdt_first_subnode(fit, conf_node); 46 node >= 0; 47 node = fdt_next_subnode(fit, node)) { 48 name = fdt_getprop(fit, node, "description", &len); 49 printf(" %s\n", name); 50 } 51 #endif 52 return conf_node; 53 } 54 55 name = fdt_getprop(fit, conf_node, type, &len); 56 if (!name) { 57 debug("cannot find property '%s': %d\n", type, len); 58 return -EINVAL; 59 } 60 61 str = name; 62 for (i = 0; i < index; i++) { 63 str = strchr(str, '\0') + 1; 64 if (!str || (str - name >= len)) { 65 debug("no string for index %d\n", index); 66 return -E2BIG; 67 } 68 } 69 70 *outname = (char *)str; 71 return 0; 72 } 73 74 /** 75 * spl_fit_get_image_node(): By using the matching configuration subnode, 76 * retrieve the name of an image, specified by a property name and an index 77 * into that. 78 * @fit: Pointer to the FDT blob. 79 * @images: Offset of the /images subnode. 80 * @type: Name of the property within the configuration subnode. 81 * @index: Index into the list of strings in this property. 82 * 83 * Return: the node offset of the respective image node or a negative 84 * error number. 85 */ 86 static int spl_fit_get_image_node(const void *fit, int images, 87 const char *type, int index) 88 { 89 char *str; 90 int err; 91 int node; 92 93 err = spl_fit_get_image_name(fit, images, type, index, &str); 94 if (err) 95 return err; 96 97 debug("%s: '%s'\n", type, str); 98 99 node = fdt_subnode_offset(fit, images, str); 100 if (node < 0) { 101 debug("cannot find image node '%s': %d\n", str, node); 102 return -EINVAL; 103 } 104 105 return node; 106 } 107 108 static int get_aligned_image_offset(struct spl_load_info *info, int offset) 109 { 110 /* 111 * If it is a FS read, get the first address before offset which is 112 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the 113 * block number to which offset belongs. 114 */ 115 if (info->filename) 116 return offset & ~(ARCH_DMA_MINALIGN - 1); 117 118 return offset / info->bl_len; 119 } 120 121 static int get_aligned_image_overhead(struct spl_load_info *info, int offset) 122 { 123 /* 124 * If it is a FS read, get the difference between the offset and 125 * the first address before offset which is aligned to 126 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the 127 * block. 128 */ 129 if (info->filename) 130 return offset & (ARCH_DMA_MINALIGN - 1); 131 132 return offset % info->bl_len; 133 } 134 135 static int get_aligned_image_size(struct spl_load_info *info, int data_size, 136 int offset) 137 { 138 data_size = data_size + get_aligned_image_overhead(info, offset); 139 140 if (info->filename) 141 return data_size; 142 143 return (data_size + info->bl_len - 1) / info->bl_len; 144 } 145 146 /** 147 * spl_load_fit_image(): load the image described in a certain FIT node 148 * @info: points to information about the device to load data from 149 * @sector: the start sector of the FIT image on the device 150 * @fit: points to the flattened device tree blob describing the FIT 151 * image 152 * @base_offset: the beginning of the data area containing the actual 153 * image data, relative to the beginning of the FIT 154 * @node: offset of the DT node describing the image to load (relative 155 * to @fit) 156 * @image_info: will be filled with information about the loaded image 157 * If the FIT node does not contain a "load" (address) property, 158 * the image gets loaded to the address pointed to by the 159 * load_addr member in this struct. 160 * 161 * Return: 0 on success or a negative error number. 162 */ 163 static int spl_load_fit_image(struct spl_load_info *info, ulong sector, 164 void *fit, ulong base_offset, int node, 165 struct spl_image_info *image_info) 166 { 167 int offset; 168 size_t length; 169 int len; 170 ulong size; 171 ulong load_addr, load_ptr; 172 void *src; 173 ulong overhead; 174 int nr_sectors; 175 int align_len = ARCH_DMA_MINALIGN - 1; 176 uint8_t image_comp = -1, type = -1; 177 const void *data; 178 bool external_data = false; 179 180 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { 181 if (fit_image_get_comp(fit, node, &image_comp)) 182 puts("Cannot get image compression format.\n"); 183 else 184 debug("%s ", genimg_get_comp_name(image_comp)); 185 186 if (fit_image_get_type(fit, node, &type)) 187 puts("Cannot get image type.\n"); 188 else 189 debug("%s ", genimg_get_type_name(type)); 190 } 191 192 if (fit_image_get_load(fit, node, &load_addr)) 193 load_addr = image_info->load_addr; 194 195 if (!fit_image_get_data_position(fit, node, &offset)) { 196 external_data = true; 197 } else if (!fit_image_get_data_offset(fit, node, &offset)) { 198 offset += base_offset; 199 external_data = true; 200 } 201 202 if (external_data) { 203 /* External data */ 204 if (fit_image_get_data_size(fit, node, &len)) 205 return -ENOENT; 206 207 if (!len) 208 return 0; 209 210 load_ptr = (load_addr + align_len) & ~align_len; 211 #if defined(CONFIG_ARCH_ROCKCHIP) 212 if ((load_ptr < CONFIG_SYS_SDRAM_BASE) || 213 (load_ptr >= CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE)) 214 load_ptr = (ulong)memalign(ARCH_DMA_MINALIGN, len); 215 #endif 216 length = len; 217 218 overhead = get_aligned_image_overhead(info, offset); 219 nr_sectors = get_aligned_image_size(info, length, offset); 220 221 if (info->read(info, 222 sector + get_aligned_image_offset(info, offset), 223 nr_sectors, (void *)load_ptr) != nr_sectors) 224 return -EIO; 225 226 debug("External data: dst=%lx, offset=%x, size=%lx\n", 227 load_ptr, offset, (unsigned long)length); 228 src = (void *)load_ptr + overhead; 229 } else { 230 /* Embedded data */ 231 if (fit_image_get_data(fit, node, &data, &length)) { 232 puts("Cannot get image data/size\n"); 233 return -ENOENT; 234 } 235 debug("Embedded data: dst=%lx, size=%lx\n", load_addr, 236 (unsigned long)length); 237 src = (void *)data; 238 239 if (!length) 240 return 0; 241 } 242 243 /* Check hashes and signature */ 244 printf("## Checking %s ... ", 245 fit_get_name(fit, node, NULL)); 246 #ifdef CONFIG_FIT_SPL_PRINT 247 printf("\n"); 248 fit_image_print(fit, node, ""); 249 #endif 250 if (!fit_image_verify_with_data(fit, node, 251 src, length)) 252 return -EPERM; 253 puts("OK\n"); 254 255 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS 256 board_fit_image_post_process(&src, &length); 257 #endif 258 259 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && 260 IS_ENABLED(CONFIG_SPL_GZIP) && 261 image_comp == IH_COMP_GZIP && 262 type == IH_TYPE_KERNEL) { 263 size = length; 264 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, 265 src, &size)) { 266 puts("Uncompressing error\n"); 267 return -EIO; 268 } 269 length = size; 270 } else { 271 memcpy((void *)load_addr, src, length); 272 } 273 274 if (image_info) { 275 image_info->load_addr = load_addr; 276 image_info->size = length; 277 image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); 278 } 279 280 return 0; 281 } 282 283 static int spl_fit_append_fdt(struct spl_image_info *spl_image, 284 struct spl_load_info *info, ulong sector, 285 void *fit, int images, ulong base_offset) 286 { 287 struct spl_image_info image_info; 288 int node, ret; 289 290 /* Figure out which device tree the board wants to use */ 291 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0); 292 if (node < 0) { 293 debug("%s: cannot find FDT node\n", __func__); 294 return node; 295 } 296 297 /* 298 * Read the device tree and place it after the image. 299 * Align the destination address to ARCH_DMA_MINALIGN. 300 */ 301 image_info.load_addr = spl_image->load_addr + spl_image->size; 302 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 303 &image_info); 304 305 if (ret < 0) 306 return ret; 307 308 /* Make the load-address of the FDT available for the SPL framework */ 309 spl_image->fdt_addr = (void *)image_info.load_addr; 310 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) 311 /* Try to make space, so we can inject details on the loadables */ 312 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192); 313 #endif 314 315 return ret; 316 } 317 318 static int spl_fit_record_loadable(const void *fit, int images, int index, 319 void *blob, struct spl_image_info *image) 320 { 321 int ret = 0; 322 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) 323 char *name; 324 int node; 325 326 ret = spl_fit_get_image_name(fit, images, "loadables", 327 index, &name); 328 if (ret < 0) 329 return ret; 330 331 node = spl_fit_get_image_node(fit, images, "loadables", index); 332 333 ret = fdt_record_loadable(blob, index, name, image->load_addr, 334 image->size, image->entry_point, 335 fdt_getprop(fit, node, "type", NULL), 336 fdt_getprop(fit, node, "os", NULL)); 337 #endif 338 return ret; 339 } 340 341 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os) 342 { 343 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) 344 return -ENOTSUPP; 345 #else 346 return fit_image_get_os(fit, noffset, os); 347 #endif 348 } 349 350 __weak int spl_fit_standalone_release(void) 351 { 352 return 0; 353 } 354 355 static int spl_internal_load_simple_fit(struct spl_image_info *spl_image, 356 struct spl_load_info *info, 357 ulong sector, void *fit) 358 { 359 int sectors; 360 ulong size; 361 unsigned long count; 362 struct spl_image_info image_info; 363 int node = -1; 364 int images, ret; 365 int base_offset, align_len = ARCH_DMA_MINALIGN - 1; 366 int index = 0; 367 368 /* 369 * For FIT with external data, figure out where the external images 370 * start. This is the base for the data-offset properties in each 371 * image. 372 */ 373 size = fdt_totalsize(fit); 374 size = FIT_ALIGN(size); 375 base_offset = FIT_ALIGN(size); 376 377 /* 378 * So far we only have one block of data from the FIT. Read the entire 379 * thing, including that first block, placing it so it finishes before 380 * where we will load the image. 381 * 382 * Note that we will load the image such that its first byte will be 383 * at the load address. Since that byte may be part-way through a 384 * block, we may load the image up to one block before the load 385 * address. So take account of that here by subtracting an addition 386 * block length from the FIT start position. 387 * 388 * In fact the FIT has its own load address, but we assume it cannot 389 * be before CONFIG_SYS_TEXT_BASE. 390 * 391 * For FIT with data embedded, data is loaded as part of FIT image. 392 * For FIT with external data, data is not loaded in this step. 393 */ 394 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - 395 align_len) & ~align_len); 396 sectors = get_aligned_image_size(info, size, 0); 397 count = info->read(info, sector, sectors, fit); 398 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", 399 sector, sectors, fit, count); 400 if (count == 0) 401 return -EIO; 402 403 /* find the node holding the images information */ 404 images = fdt_path_offset(fit, FIT_IMAGES_PATH); 405 if (images < 0) { 406 debug("%s: Cannot find /images node: %d\n", __func__, images); 407 return -1; 408 } 409 410 /* if board sigs verify required, check self */ 411 if (fit_board_verify_required_sigs() && 412 !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) { 413 printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n"); 414 hang(); 415 } 416 417 /* verify the configure node by keys, if required */ 418 #ifdef CONFIG_SPL_FIT_SIGNATURE 419 int conf_noffset; 420 421 conf_noffset = fit_conf_get_node(fit, NULL); 422 if (conf_noffset > 0) { 423 ret = fit_config_verify(fit, conf_noffset); 424 if (ret) { 425 printf("fit verify configure failed, ret=%d\n", ret); 426 return ret; 427 } 428 printf("\n"); 429 } 430 431 #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT 432 uint32_t this_index, min_index; 433 434 ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL, 435 &this_index, &min_index); 436 if (ret) { 437 printf("fit failed to get rollback index, ret=%d\n", ret); 438 return ret; 439 } else if (this_index < min_index) { 440 printf("fit reject rollback: %d < %d(min)\n", 441 this_index, min_index); 442 return -EINVAL; 443 } 444 445 spl_image->rollback_index = this_index; 446 printf("rollback index: %d >= %d, OK\n", this_index, min_index); 447 #endif 448 #endif 449 450 /* 451 * If required to start the other core before load "loadables" 452 * firmwares, use the config "standalone" to load the other core's 453 * firmware, then start it. 454 * Normally, different cores' firmware is attach to the config 455 * "loadables" and load them together. 456 */ 457 if (node < 0) 458 node = spl_fit_get_image_node(fit, images, FIT_STANDALONE_PROP, 459 0); 460 if (node > 0) { 461 /* Load the image and set up the spl_image structure */ 462 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 463 spl_image); 464 if (!ret) { 465 ret = spl_fit_standalone_release(); 466 if (ret) 467 printf("Start standalone fail, ret = %d\n", 468 ret); 469 } 470 471 node = -1; 472 } 473 474 /* 475 * Find the U-Boot image using the following search order: 476 * - start at 'firmware' (e.g. an ARM Trusted Firmware) 477 * - fall back 'kernel' (e.g. a Falcon-mode OS boot 478 * - fall back to using the first 'loadables' entry 479 */ 480 if (node < 0) 481 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP, 482 0); 483 #ifdef CONFIG_SPL_OS_BOOT 484 if (node < 0) 485 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0); 486 #endif 487 if (node < 0) { 488 debug("could not find firmware image, trying loadables...\n"); 489 node = spl_fit_get_image_node(fit, images, "loadables", 0); 490 /* 491 * If we pick the U-Boot image from "loadables", start at 492 * the second image when later loading additional images. 493 */ 494 index = 1; 495 } 496 if (node < 0) { 497 debug("%s: Cannot find u-boot image node: %d\n", 498 __func__, node); 499 return -1; 500 } 501 502 /* Load the image and set up the spl_image structure */ 503 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 504 spl_image); 505 if (ret) 506 return ret; 507 508 /* 509 * For backward compatibility, we treat the first node that is 510 * as a U-Boot image, if no OS-type has been declared. 511 */ 512 if (!spl_fit_image_get_os(fit, node, &spl_image->os)) 513 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os)); 514 #if !defined(CONFIG_SPL_OS_BOOT) 515 else 516 spl_image->os = IH_OS_U_BOOT; 517 #endif 518 519 /* 520 * Booting a next-stage U-Boot may require us to append the FDT. 521 * We allow this to fail, as the U-Boot image might embed its FDT. 522 */ 523 if (spl_image->os == IH_OS_U_BOOT) 524 spl_fit_append_fdt(spl_image, info, sector, fit, 525 images, base_offset); 526 527 /* Now check if there are more images for us to load */ 528 for (; ; index++) { 529 uint8_t os_type = IH_OS_INVALID; 530 531 node = spl_fit_get_image_node(fit, images, "loadables", index); 532 if (node < 0) 533 break; 534 535 ret = spl_load_fit_image(info, sector, fit, base_offset, node, 536 &image_info); 537 if (ret < 0) 538 continue; 539 540 if (!spl_fit_image_get_os(fit, node, &os_type)) 541 debug("Loadable is %s\n", genimg_get_os_name(os_type)); 542 543 if (os_type == IH_OS_U_BOOT) { 544 spl_fit_append_fdt(&image_info, info, sector, 545 fit, images, base_offset); 546 spl_image->fdt_addr = image_info.fdt_addr; 547 } 548 549 /* 550 * If the "firmware" image did not provide an entry point, 551 * use the first valid entry point from the loadables. 552 */ 553 if (spl_image->entry_point == FDT_ERROR && 554 image_info.entry_point != FDT_ERROR) 555 spl_image->entry_point = image_info.entry_point; 556 557 /* Record our loadables into the FDT */ 558 if (spl_image->fdt_addr) 559 spl_fit_record_loadable(fit, images, index, 560 spl_image->fdt_addr, 561 &image_info); 562 } 563 564 /* 565 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's 566 * Makefile will set it to 0 and it will end up as the entry point 567 * here. What it actually means is: use the load address. 568 */ 569 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) 570 spl_image->entry_point = spl_image->load_addr; 571 572 return 0; 573 } 574 575 int spl_load_simple_fit(struct spl_image_info *spl_image, 576 struct spl_load_info *info, ulong sector, void *fit) 577 { 578 ulong sector_offs = sector; 579 int i; 580 581 for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) { 582 if (i > 0) { 583 sector_offs += 584 i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len); 585 printf("Trying fit image at 0x%lx sector\n", sector_offs); 586 if (info->read(info, sector_offs, 1, fit) != 1) { 587 printf("IO error\n"); 588 continue; 589 } 590 } 591 592 if (image_get_magic(fit) != FDT_MAGIC) { 593 printf("Bad fit magic\n"); 594 continue; 595 } 596 597 if (!spl_internal_load_simple_fit(spl_image, info, 598 sector_offs, fit)) 599 return 0; 600 } 601 602 return -EINVAL; 603 } 604 605