1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
15 #include <linux/libfdt.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #ifndef CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ
20 #define CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ (64 * 1024)
23 #ifndef CONFIG_SYS_BOOTM_LEN
24 #define CONFIG_SYS_BOOTM_LEN (64 << 20)
27 __weak void board_spl_fit_post_load(ulong load_addr, size_t length)
31 __weak ulong board_spl_fit_size_align(ulong size)
36 static int find_node_from_desc(const void *fit, int node, const char *str)
43 /* iterate the FIT nodes and find a matching description */
44 for (child = fdt_first_subnode(fit, node); child >= 0;
45 child = fdt_next_subnode(fit, child)) {
47 const char *desc = fdt_getprop(fit, child, "description", &len);
52 if (!strcmp(desc, str))
60 * spl_fit_get_image_name(): By using the matching configuration subnode,
61 * retrieve the name of an image, specified by a property name and an index
63 * @fit: Pointer to the FDT blob.
64 * @images: Offset of the /images subnode.
65 * @type: Name of the property within the configuration subnode.
66 * @index: Index into the list of strings in this property.
67 * @outname: Name of the image
69 * Return: 0 on success, or a negative error number
71 static int spl_fit_get_image_name(const void *fit, int images,
72 const char *type, int index,
75 struct udevice *board;
76 const char *name, *str;
77 __maybe_unused int node;
82 conf_node = fit_find_config_node(fit);
84 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
85 printf("No matching DT out of these options:\n");
86 for (node = fdt_first_subnode(fit, conf_node);
88 node = fdt_next_subnode(fit, node)) {
89 name = fdt_getprop(fit, node, "description", &len);
90 printf(" %s\n", name);
96 name = fdt_getprop(fit, conf_node, type, &len);
98 debug("cannot find property '%s': %d\n", type, len);
103 for (i = 0; i < index; i++) {
104 str = strchr(str, '\0') + 1;
105 if (!str || (str - name >= len)) {
111 if (!found && !board_get(&board)) {
114 * no string in the property for this index. Check if the board
115 * level code can supply one.
117 rc = board_get_fit_loadable(board, index - i - 1, type, &str);
118 if (rc && rc != -ENOENT)
123 * The board provided a name for a loadable.
124 * Try to match it against the description properties
125 * first. If no matching node is found, use it as a
129 int images = fdt_path_offset(fit, FIT_IMAGES_PATH);
131 node = find_node_from_desc(fit, images, str);
133 str = fdt_get_name(fit, node, NULL);
140 debug("no string for index %d\n", index);
149 * spl_fit_get_image_node(): By using the matching configuration subnode,
150 * retrieve the name of an image, specified by a property name and an index
152 * @fit: Pointer to the FDT blob.
153 * @images: Offset of the /images subnode.
154 * @type: Name of the property within the configuration subnode.
155 * @index: Index into the list of strings in this property.
157 * Return: the node offset of the respective image node or a negative
160 static int spl_fit_get_image_node(const void *fit, int images,
161 const char *type, int index)
167 err = spl_fit_get_image_name(fit, images, type, index, &str);
171 debug("%s: '%s'\n", type, str);
173 node = fdt_subnode_offset(fit, images, str);
175 pr_err("cannot find image node '%s': %d\n", str, node);
182 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
185 * If it is a FS read, get the first address before offset which is
186 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
187 * block number to which offset belongs.
190 return offset & ~(ARCH_DMA_MINALIGN - 1);
192 return offset / info->bl_len;
195 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
198 * If it is a FS read, get the difference between the offset and
199 * the first address before offset which is aligned to
200 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
204 return offset & (ARCH_DMA_MINALIGN - 1);
206 return offset % info->bl_len;
209 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
212 data_size = data_size + get_aligned_image_overhead(info, offset);
217 return (data_size + info->bl_len - 1) / info->bl_len;
221 * spl_load_fit_image(): load the image described in a certain FIT node
222 * @info: points to information about the device to load data from
223 * @sector: the start sector of the FIT image on the device
224 * @fit: points to the flattened device tree blob describing the FIT
226 * @base_offset: the beginning of the data area containing the actual
227 * image data, relative to the beginning of the FIT
228 * @node: offset of the DT node describing the image to load (relative
230 * @image_info: will be filled with information about the loaded image
231 * If the FIT node does not contain a "load" (address) property,
232 * the image gets loaded to the address pointed to by the
233 * load_addr member in this struct.
235 * Return: 0 on success or a negative error number.
237 static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
238 void *fit, ulong base_offset, int node,
239 struct spl_image_info *image_info)
245 ulong load_addr, load_ptr;
249 int align_len = ARCH_DMA_MINALIGN - 1;
250 uint8_t image_comp = -1, type = -1;
252 bool external_data = false;
254 if (IS_ENABLED(CONFIG_SPL_FPGA_SUPPORT) ||
255 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
256 if (fit_image_get_type(fit, node, &type))
257 puts("Cannot get image type.\n");
259 debug("%s ", genimg_get_type_name(type));
262 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
263 if (fit_image_get_comp(fit, node, &image_comp))
264 puts("Cannot get image compression format.\n");
266 debug("%s ", genimg_get_comp_name(image_comp));
269 if (fit_image_get_load(fit, node, &load_addr))
270 load_addr = image_info->load_addr;
272 if (!fit_image_get_data_position(fit, node, &offset)) {
273 external_data = true;
274 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
275 offset += base_offset;
276 external_data = true;
281 if (fit_image_get_data_size(fit, node, &len))
284 load_ptr = (load_addr + align_len) & ~align_len;
287 overhead = get_aligned_image_overhead(info, offset);
288 nr_sectors = get_aligned_image_size(info, length, offset);
291 sector + get_aligned_image_offset(info, offset),
292 nr_sectors, (void *)load_ptr) != nr_sectors)
295 debug("External data: dst=%lx, offset=%x, size=%lx\n",
296 load_ptr, offset, (unsigned long)length);
297 src = (void *)load_ptr + overhead;
300 if (fit_image_get_data(fit, node, &data, &length)) {
301 puts("Cannot get image data/size\n");
304 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
305 (unsigned long)length);
309 #ifdef CONFIG_SPL_FIT_SIGNATURE
310 printf("## Checking hash(es) for Image %s ... ",
311 fit_get_name(fit, node, NULL));
312 if (!fit_image_verify_with_data(fit, node,
318 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
319 board_fit_image_post_process(&src, &length);
322 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
324 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
326 puts("Uncompressing error\n");
331 memcpy((void *)load_addr, src, length);
335 image_info->load_addr = load_addr;
336 image_info->size = length;
337 image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
343 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
344 struct spl_load_info *info, ulong sector,
345 void *fit, int images, ulong base_offset)
347 struct spl_image_info image_info;
348 int node, ret = 0, index = 0;
351 * Use the address following the image as target address for the
354 image_info.load_addr = spl_image->load_addr + spl_image->size;
356 /* Figure out which device tree the board wants to use */
357 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, index++);
359 debug("%s: cannot find FDT node\n", __func__);
362 * U-Boot did not find a device tree inside the FIT image. Use
363 * the U-Boot device tree instead.
366 memcpy((void *)image_info.load_addr, gd->fdt_blob,
367 fdt_totalsize(gd->fdt_blob));
371 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
377 /* Make the load-address of the FDT available for the SPL framework */
378 spl_image->fdt_addr = (void *)image_info.load_addr;
379 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
380 if (CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)) {
381 void *tmpbuffer = NULL;
384 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP,
386 if (node == -E2BIG) {
387 debug("%s: No additional FDT node\n", __func__);
389 } else if (node < 0) {
390 debug("%s: unable to find FDT node %d\n",
397 * allocate memory to store the DT overlay
398 * before it is applied. It may not be used
399 * depending on how the overlay is stored, so
400 * don't fail yet if the allocation failed.
402 tmpbuffer = malloc(CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ);
404 debug("%s: unable to allocate space for overlays\n",
407 image_info.load_addr = (ulong)tmpbuffer;
408 ret = spl_load_fit_image(info, sector, fit, base_offset,
413 /* Make room in FDT for changes from the overlay */
414 ret = fdt_increase_size(spl_image->fdt_addr,
419 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
420 (void *)image_info.load_addr);
422 pr_err("failed to apply DT overlay %s\n",
423 fit_get_name(fit, node, NULL));
427 debug("%s: DT overlay %s applied\n", __func__,
428 fit_get_name(fit, node, NULL));
435 /* Try to make space, so we can inject details on the loadables */
436 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
444 static int spl_fit_record_loadable(const void *fit, int images, int index,
445 void *blob, struct spl_image_info *image)
448 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
452 ret = spl_fit_get_image_name(fit, images, "loadables",
457 node = spl_fit_get_image_node(fit, images, "loadables", index);
459 ret = fdt_record_loadable(blob, index, name, image->load_addr,
460 image->size, image->entry_point,
461 fdt_getprop(fit, node, "type", NULL),
462 fdt_getprop(fit, node, "os", NULL));
467 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
469 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) && !defined(CONFIG_SPL_OS_BOOT)
472 return fit_image_get_os(fit, noffset, os);
477 * Weak default function to allow customizing SPL fit loading for load-only
478 * use cases by allowing to skip the parsing/processing of the FIT contents
479 * (so that this can be done separately in a more customized fashion)
481 __weak bool spl_load_simple_fit_skip_processing(void)
486 int spl_load_simple_fit(struct spl_image_info *spl_image,
487 struct spl_load_info *info, ulong sector, void *fit)
492 struct spl_image_info image_info;
495 int base_offset, hsize, align_len = ARCH_DMA_MINALIGN - 1;
500 * For FIT with external data, figure out where the external images
501 * start. This is the base for the data-offset properties in each
504 size = fdt_totalsize(fit);
505 size = (size + 3) & ~3;
506 size = board_spl_fit_size_align(size);
507 base_offset = (size + 3) & ~3;
510 * So far we only have one block of data from the FIT. Read the entire
511 * thing, including that first block, placing it so it finishes before
512 * where we will load the image.
514 * Note that we will load the image such that its first byte will be
515 * at the load address. Since that byte may be part-way through a
516 * block, we may load the image up to one block before the load
517 * address. So take account of that here by subtracting an addition
518 * block length from the FIT start position.
520 * In fact the FIT has its own load address, but we assume it cannot
521 * be before CONFIG_SYS_TEXT_BASE.
523 * For FIT with data embedded, data is loaded as part of FIT image.
524 * For FIT with external data, data is not loaded in this step.
526 hsize = (size + info->bl_len + align_len) & ~align_len;
527 fit = spl_get_load_buffer(-hsize, hsize);
528 sectors = get_aligned_image_size(info, size, 0);
529 count = info->read(info, sector, sectors, fit);
530 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
531 sector, sectors, fit, count, size);
536 /* skip further processing if requested to enable load-only use cases */
537 if (spl_load_simple_fit_skip_processing())
540 /* find the node holding the images information */
541 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
543 debug("%s: Cannot find /images node: %d\n", __func__, images);
547 #ifdef CONFIG_SPL_FPGA_SUPPORT
548 node = spl_fit_get_image_node(fit, images, "fpga", 0);
550 /* Load the image and set up the spl_image structure */
551 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
554 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
558 debug("FPGA bitstream at: %x, size: %x\n",
559 (u32)spl_image->load_addr, spl_image->size);
561 ret = fpga_load(0, (const void *)spl_image->load_addr,
562 spl_image->size, BIT_FULL);
564 printf("%s: Cannot load the image to the FPGA\n",
569 puts("FPGA image loaded from FIT\n");
575 * Find the U-Boot image using the following search order:
576 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
577 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
578 * - fall back to using the first 'loadables' entry
581 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
583 #ifdef CONFIG_SPL_OS_BOOT
585 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
588 debug("could not find firmware image, trying loadables...\n");
589 node = spl_fit_get_image_node(fit, images, "loadables", 0);
591 * If we pick the U-Boot image from "loadables", start at
592 * the second image when later loading additional images.
597 debug("%s: Cannot find u-boot image node: %d\n",
602 /* Load the image and set up the spl_image structure */
603 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
609 * For backward compatibility, we treat the first node that is
610 * as a U-Boot image, if no OS-type has been declared.
612 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
613 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
614 #if !defined(CONFIG_SPL_OS_BOOT)
616 spl_image->os = IH_OS_U_BOOT;
620 * Booting a next-stage U-Boot may require us to append the FDT.
621 * We allow this to fail, as the U-Boot image might embed its FDT.
623 if (spl_image->os == IH_OS_U_BOOT)
624 spl_fit_append_fdt(spl_image, info, sector, fit,
625 images, base_offset);
627 firmware_node = node;
628 /* Now check if there are more images for us to load */
630 uint8_t os_type = IH_OS_INVALID;
632 node = spl_fit_get_image_node(fit, images, "loadables", index);
637 * if the firmware is also a loadable, skip it because
638 * it already has been loaded. This is typically the case with
639 * u-boot.img generated by mkimage.
641 if (firmware_node == node)
644 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
649 if (!spl_fit_image_get_os(fit, node, &os_type))
650 debug("Loadable is %s\n", genimg_get_os_name(os_type));
651 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
653 os_type = IH_OS_U_BOOT;
656 if (os_type == IH_OS_U_BOOT) {
657 spl_fit_append_fdt(&image_info, info, sector,
658 fit, images, base_offset);
659 spl_image->fdt_addr = image_info.fdt_addr;
663 * If the "firmware" image did not provide an entry point,
664 * use the first valid entry point from the loadables.
666 if (spl_image->entry_point == FDT_ERROR &&
667 image_info.entry_point != FDT_ERROR)
668 spl_image->entry_point = image_info.entry_point;
670 /* Record our loadables into the FDT */
671 if (spl_image->fdt_addr)
672 spl_fit_record_loadable(fit, images, index,
678 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
679 * Makefile will set it to 0 and it will end up as the entry point
680 * here. What it actually means is: use the load address.
682 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
683 spl_image->entry_point = spl_image->load_addr;
685 spl_image->flags |= SPL_FIT_FOUND;
687 #ifdef CONFIG_IMX_HAB
688 board_spl_fit_post_load((ulong)fit, size);