1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
17 #include <asm/cache.h>
18 #include <asm/global_data.h>
19 #include <linux/libfdt.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #ifndef CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ
24 #define CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ (64 * 1024)
27 #ifndef CONFIG_SYS_BOOTM_LEN
28 #define CONFIG_SYS_BOOTM_LEN (64 << 20)
32 const void *fit; /* Pointer to a valid FIT blob */
33 size_t ext_data_offset; /* Offset to FIT external data (end of FIT) */
34 int images_node; /* FDT offset to "/images" node */
35 int conf_node; /* FDT offset to selected configuration node */
38 __weak void board_spl_fit_post_load(const void *fit)
42 __weak ulong board_spl_fit_size_align(ulong size)
47 static int find_node_from_desc(const void *fit, int node, const char *str)
54 /* iterate the FIT nodes and find a matching description */
55 for (child = fdt_first_subnode(fit, node); child >= 0;
56 child = fdt_next_subnode(fit, child)) {
58 const char *desc = fdt_getprop(fit, child, "description", &len);
63 if (!strcmp(desc, str))
71 * spl_fit_get_image_name(): By using the matching configuration subnode,
72 * retrieve the name of an image, specified by a property name and an index
74 * @fit: Pointer to the FDT blob.
75 * @images: Offset of the /images subnode.
76 * @type: Name of the property within the configuration subnode.
77 * @index: Index into the list of strings in this property.
78 * @outname: Name of the image
80 * Return: 0 on success, or a negative error number
82 static int spl_fit_get_image_name(const struct spl_fit_info *ctx,
83 const char *type, int index,
86 struct udevice *sysinfo;
87 const char *name, *str;
88 __maybe_unused int node;
92 name = fdt_getprop(ctx->fit, ctx->conf_node, type, &len);
94 debug("cannot find property '%s': %d\n", type, len);
99 for (i = 0; i < index; i++) {
100 str = strchr(str, '\0') + 1;
101 if (!str || (str - name >= len)) {
107 if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) {
110 * no string in the property for this index. Check if the
111 * sysinfo-level code can supply one.
113 rc = sysinfo_detect(sysinfo);
117 rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
119 if (rc && rc != -ENOENT)
124 * The sysinfo provided a name for a loadable.
125 * Try to match it against the description properties
126 * first. If no matching node is found, use it as a
130 int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
132 node = find_node_from_desc(ctx->fit, images, str);
134 str = fdt_get_name(ctx->fit, node, NULL);
141 debug("no string for index %d\n", index);
150 * spl_fit_get_image_node(): By using the matching configuration subnode,
151 * retrieve the name of an image, specified by a property name and an index
153 * @fit: Pointer to the FDT blob.
154 * @images: Offset of the /images subnode.
155 * @type: Name of the property within the configuration subnode.
156 * @index: Index into the list of strings in this property.
158 * Return: the node offset of the respective image node or a negative
161 static int spl_fit_get_image_node(const struct spl_fit_info *ctx,
162 const char *type, int index)
168 err = spl_fit_get_image_name(ctx, type, index, &str);
172 debug("%s: '%s'\n", type, str);
174 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
176 pr_err("cannot find image node '%s': %d\n", str, node);
183 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
186 * If it is a FS read, get the first address before offset which is
187 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
188 * block number to which offset belongs.
191 return offset & ~(ARCH_DMA_MINALIGN - 1);
193 return offset / info->bl_len;
196 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
199 * If it is a FS read, get the difference between the offset and
200 * the first address before offset which is aligned to
201 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
205 return offset & (ARCH_DMA_MINALIGN - 1);
207 return offset % info->bl_len;
210 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
213 data_size = data_size + get_aligned_image_overhead(info, offset);
218 return (data_size + info->bl_len - 1) / info->bl_len;
222 * spl_load_fit_image(): load the image described in a certain FIT node
223 * @info: points to information about the device to load data from
224 * @sector: the start sector of the FIT image on the device
225 * @ctx: points to the FIT context structure
226 * @node: offset of the DT node describing the image to load (relative
228 * @image_info: will be filled with information about the loaded image
229 * If the FIT node does not contain a "load" (address) property,
230 * the image gets loaded to the address pointed to by the
231 * load_addr member in this struct, if load_addr is not 0
233 * Return: 0 on success or a negative error number.
235 static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
236 const struct spl_fit_info *ctx, int node,
237 struct spl_image_info *image_info)
248 uint8_t image_comp = -1, type = -1;
250 const void *fit = ctx->fit;
251 bool external_data = false;
253 if (IS_ENABLED(CONFIG_SPL_FPGA) ||
254 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
255 if (fit_image_get_type(fit, node, &type))
256 puts("Cannot get image type.\n");
258 debug("%s ", genimg_get_type_name(type));
261 if (IS_ENABLED(CONFIG_SPL_GZIP)) {
262 fit_image_get_comp(fit, node, &image_comp);
263 debug("%s ", genimg_get_comp_name(image_comp));
266 if (fit_image_get_load(fit, node, &load_addr)) {
267 if (!image_info->load_addr) {
268 printf("Can't load %s: No load address and no buffer\n",
269 fit_get_name(fit, node, NULL));
272 load_addr = image_info->load_addr;
275 if (!fit_image_get_data_position(fit, node, &offset)) {
276 external_data = true;
277 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
278 offset += ctx->ext_data_offset;
279 external_data = true;
286 if (fit_image_get_data_size(fit, node, &len))
289 /* Dont bother to copy 0 byte data, but warn, though */
291 log_warning("%s: Skip load '%s': image size is 0!\n",
292 __func__, fit_get_name(fit, node, NULL));
296 src_ptr = map_sysmem(ALIGN(load_addr, ARCH_DMA_MINALIGN), len);
299 overhead = get_aligned_image_overhead(info, offset);
300 nr_sectors = get_aligned_image_size(info, length, offset);
303 sector + get_aligned_image_offset(info, offset),
304 nr_sectors, src_ptr) != nr_sectors)
307 debug("External data: dst=%p, offset=%x, size=%lx\n",
308 src_ptr, offset, (unsigned long)length);
309 src = src_ptr + overhead;
312 if (fit_image_get_data(fit, node, &data, &length)) {
313 puts("Cannot get image data/size\n");
316 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
317 (unsigned long)length);
318 src = (void *)data; /* cast away const */
321 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
322 printf("## Checking hash(es) for Image %s ... ",
323 fit_get_name(fit, node, NULL));
324 if (!fit_image_verify_with_data(fit, node, src, length))
329 if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS))
330 board_fit_image_post_process(fit, node, &src, &length);
332 load_ptr = map_sysmem(load_addr, length);
333 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
335 if (gunzip(load_ptr, CONFIG_SYS_BOOTM_LEN, src, &size)) {
336 puts("Uncompressing error\n");
341 memcpy(load_ptr, src, length);
347 image_info->load_addr = load_addr;
348 image_info->size = length;
350 if (!fit_image_get_entry(fit, node, &entry_point))
351 image_info->entry_point = entry_point;
353 image_info->entry_point = FDT_ERROR;
359 static bool os_takes_devicetree(uint8_t os)
365 return IS_ENABLED(CONFIG_SPL_OS_BOOT);
371 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
372 struct spl_load_info *info, ulong sector,
373 const struct spl_fit_info *ctx)
375 struct spl_image_info image_info;
376 int node, ret = 0, index = 0;
379 * Use the address following the image as target address for the
382 image_info.load_addr = spl_image->load_addr + spl_image->size;
384 /* Figure out which device tree the board wants to use */
385 node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
387 debug("%s: cannot find FDT node\n", __func__);
390 * U-Boot did not find a device tree inside the FIT image. Use
391 * the U-Boot device tree instead.
394 memcpy((void *)image_info.load_addr, gd->fdt_blob,
395 fdt_totalsize(gd->fdt_blob));
399 ret = spl_load_fit_image(info, sector, ctx, node,
405 /* Make the load-address of the FDT available for the SPL framework */
406 spl_image->fdt_addr = map_sysmem(image_info.load_addr, 0);
407 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
410 if (CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)) {
411 void *tmpbuffer = NULL;
414 node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index);
415 if (node == -E2BIG) {
416 debug("%s: No additional FDT node\n", __func__);
418 } else if (node < 0) {
419 debug("%s: unable to find FDT node %d\n",
426 * allocate memory to store the DT overlay
427 * before it is applied. It may not be used
428 * depending on how the overlay is stored, so
429 * don't fail yet if the allocation failed.
431 tmpbuffer = malloc(CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ);
433 debug("%s: unable to allocate space for overlays\n",
436 image_info.load_addr = (ulong)tmpbuffer;
437 ret = spl_load_fit_image(info, sector, ctx,
442 /* Make room in FDT for changes from the overlay */
443 ret = fdt_increase_size(spl_image->fdt_addr,
448 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
449 (void *)image_info.load_addr);
451 pr_err("failed to apply DT overlay %s\n",
452 fit_get_name(ctx->fit, node, NULL));
456 debug("%s: DT overlay %s applied\n", __func__,
457 fit_get_name(ctx->fit, node, NULL));
463 /* Try to make space, so we can inject details on the loadables */
464 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
471 static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index,
472 void *blob, struct spl_image_info *image)
478 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
481 ret = spl_fit_get_image_name(ctx, "loadables", index, &name);
485 node = spl_fit_get_image_node(ctx, "loadables", index);
487 ret = fdt_record_loadable(blob, index, name, image->load_addr,
488 image->size, image->entry_point,
489 fdt_getprop(ctx->fit, node, "type", NULL),
490 fdt_getprop(ctx->fit, node, "os", NULL),
491 fdt_getprop(ctx->fit, node, "arch", NULL));
495 static int spl_fit_image_is_fpga(const void *fit, int node)
499 if (!IS_ENABLED(CONFIG_SPL_FPGA))
502 type = fdt_getprop(fit, node, FIT_TYPE_PROP, NULL);
506 return !strcmp(type, "fpga");
509 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
511 if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT))
512 return fit_image_get_os(fit, noffset, os);
514 const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
519 * We don't care what the type of the image actually is,
520 * only whether or not it is U-Boot. This saves some
521 * space by omitting the large table of OS types.
523 if (!strcmp(name, "u-boot"))
532 * The purpose of the FIT load buffer is to provide a memory location that is
533 * independent of the load address of any FIT component.
535 static void *spl_get_fit_load_buffer(size_t size)
541 pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size);
542 pr_err("\tcheck CONFIG_SYS_SPL_MALLOC_SIZE\n");
543 buf = spl_get_load_buffer(0, size);
548 __weak void *board_spl_fit_buffer_addr(ulong fit_size, int sectors, int bl_len)
550 return spl_get_fit_load_buffer(sectors * bl_len);
554 * Weak default function to allow customizing SPL fit loading for load-only
555 * use cases by allowing to skip the parsing/processing of the FIT contents
556 * (so that this can be done separately in a more customized fashion)
558 __weak bool spl_load_simple_fit_skip_processing(void)
564 * Weak default function to allow fixes after fit header
567 __weak void *spl_load_simple_fit_fix_load(const void *fit)
572 static void warn_deprecated(const char *msg)
574 printf("DEPRECATED: %s\n", msg);
575 printf("\tSee doc/uImage.FIT/source_file_format.txt\n");
578 static int spl_fit_upload_fpga(struct spl_fit_info *ctx, int node,
579 struct spl_image_info *fpga_image)
581 const char *compatible;
584 debug("FPGA bitstream at: %x, size: %x\n",
585 (u32)fpga_image->load_addr, fpga_image->size);
587 compatible = fdt_getprop(ctx->fit, node, "compatible", NULL);
589 warn_deprecated("'fpga' image without 'compatible' property");
590 else if (strcmp(compatible, "u-boot,fpga-legacy"))
591 printf("Ignoring compatible = %s property\n", compatible);
593 ret = fpga_load(0, (void *)fpga_image->load_addr, fpga_image->size,
596 printf("%s: Cannot load the image to the FPGA\n", __func__);
600 puts("FPGA image loaded from FIT\n");
605 static int spl_fit_load_fpga(struct spl_fit_info *ctx,
606 struct spl_load_info *info, ulong sector)
610 struct spl_image_info fpga_image = {
614 node = spl_fit_get_image_node(ctx, "fpga", 0);
618 warn_deprecated("'fpga' property in config node. Use 'loadables'");
620 /* Load the image and set up the fpga_image structure */
621 ret = spl_load_fit_image(info, sector, ctx, node, &fpga_image);
623 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
627 return spl_fit_upload_fpga(ctx, node, &fpga_image);
630 static int spl_simple_fit_read(struct spl_fit_info *ctx,
631 struct spl_load_info *info, ulong sector,
632 const void *fit_header)
634 unsigned long count, size;
639 * For FIT with external data, figure out where the external images
640 * start. This is the base for the data-offset properties in each
643 size = ALIGN(fdt_totalsize(fit_header), 4);
644 size = board_spl_fit_size_align(size);
645 ctx->ext_data_offset = ALIGN(size, 4);
648 * So far we only have one block of data from the FIT. Read the entire
649 * thing, including that first block.
651 * For FIT with data embedded, data is loaded as part of FIT image.
652 * For FIT with external data, data is not loaded in this step.
654 sectors = get_aligned_image_size(info, size, 0);
655 buf = board_spl_fit_buffer_addr(size, sectors, info->bl_len);
657 count = info->read(info, sector, sectors, buf);
659 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
660 sector, sectors, buf, count, size);
662 return (count == 0) ? -EIO : 0;
665 static int spl_simple_fit_parse(struct spl_fit_info *ctx)
667 /* Find the correct subnode under "/configurations" */
668 ctx->conf_node = fit_find_config_node(ctx->fit);
669 if (ctx->conf_node < 0)
672 if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
673 printf("## Checking hash(es) for config %s ... ",
674 fit_get_name(ctx->fit, ctx->conf_node, NULL));
675 if (fit_config_verify(ctx->fit, ctx->conf_node))
680 /* find the node holding the images information */
681 ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
682 if (ctx->images_node < 0) {
683 debug("%s: Cannot find /images node: %d\n", __func__,
691 int spl_load_simple_fit(struct spl_image_info *spl_image,
692 struct spl_load_info *info, ulong sector, void *fit)
694 struct spl_image_info image_info;
695 struct spl_fit_info ctx;
701 ret = spl_simple_fit_read(&ctx, info, sector, fit);
705 /* skip further processing if requested to enable load-only use cases */
706 if (spl_load_simple_fit_skip_processing())
709 ctx.fit = spl_load_simple_fit_fix_load(ctx.fit);
711 ret = spl_simple_fit_parse(&ctx);
715 if (IS_ENABLED(CONFIG_SPL_FPGA))
716 spl_fit_load_fpga(&ctx, info, sector);
719 * Find the U-Boot image using the following search order:
720 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
721 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
722 * - fall back to using the first 'loadables' entry
725 node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
727 if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT))
728 node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
731 debug("could not find firmware image, trying loadables...\n");
732 node = spl_fit_get_image_node(&ctx, "loadables", 0);
734 * If we pick the U-Boot image from "loadables", start at
735 * the second image when later loading additional images.
740 debug("%s: Cannot find u-boot image node: %d\n",
745 /* Load the image and set up the spl_image structure */
746 ret = spl_load_fit_image(info, sector, &ctx, node, spl_image);
751 * For backward compatibility, we treat the first node that is
752 * as a U-Boot image, if no OS-type has been declared.
754 if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
755 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
756 else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT))
757 spl_image->os = IH_OS_U_BOOT;
760 * Booting a next-stage U-Boot may require us to append the FDT.
761 * We allow this to fail, as the U-Boot image might embed its FDT.
763 if (os_takes_devicetree(spl_image->os)) {
764 ret = spl_fit_append_fdt(spl_image, info, sector, &ctx);
765 if (ret < 0 && spl_image->os != IH_OS_U_BOOT)
769 firmware_node = node;
770 /* Now check if there are more images for us to load */
772 uint8_t os_type = IH_OS_INVALID;
774 node = spl_fit_get_image_node(&ctx, "loadables", index);
779 * if the firmware is also a loadable, skip it because
780 * it already has been loaded. This is typically the case with
781 * u-boot.img generated by mkimage.
783 if (firmware_node == node)
786 image_info.load_addr = 0;
787 ret = spl_load_fit_image(info, sector, &ctx, node, &image_info);
789 printf("%s: can't load image loadables index %d (ret = %d)\n",
790 __func__, index, ret);
794 if (spl_fit_image_is_fpga(ctx.fit, node))
795 spl_fit_upload_fpga(&ctx, node, &image_info);
797 if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
798 debug("Loadable is %s\n", genimg_get_os_name(os_type));
800 if (os_takes_devicetree(os_type)) {
801 spl_fit_append_fdt(&image_info, info, sector, &ctx);
802 spl_image->fdt_addr = image_info.fdt_addr;
806 * If the "firmware" image did not provide an entry point,
807 * use the first valid entry point from the loadables.
809 if (spl_image->entry_point == FDT_ERROR &&
810 image_info.entry_point != FDT_ERROR)
811 spl_image->entry_point = image_info.entry_point;
813 /* Record our loadables into the FDT */
814 if (spl_image->fdt_addr)
815 spl_fit_record_loadable(&ctx, index,
821 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
822 * Makefile will set it to 0 and it will end up as the entry point
823 * here. What it actually means is: use the load address.
825 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
826 spl_image->entry_point = spl_image->load_addr;
828 spl_image->flags |= SPL_FIT_FOUND;
830 if (IS_ENABLED(CONFIG_IMX_HAB))
831 board_spl_fit_post_load(ctx.fit);