1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
16 #include <asm/cache.h>
17 #include <linux/libfdt.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #ifndef CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ
22 #define CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ (64 * 1024)
25 #ifndef CONFIG_SYS_BOOTM_LEN
26 #define CONFIG_SYS_BOOTM_LEN (64 << 20)
29 __weak void board_spl_fit_post_load(ulong load_addr, size_t length)
33 __weak ulong board_spl_fit_size_align(ulong size)
38 static int find_node_from_desc(const void *fit, int node, const char *str)
45 /* iterate the FIT nodes and find a matching description */
46 for (child = fdt_first_subnode(fit, node); child >= 0;
47 child = fdt_next_subnode(fit, child)) {
49 const char *desc = fdt_getprop(fit, child, "description", &len);
54 if (!strcmp(desc, str))
62 * spl_fit_get_image_name(): By using the matching configuration subnode,
63 * retrieve the name of an image, specified by a property name and an index
65 * @fit: Pointer to the FDT blob.
66 * @images: Offset of the /images subnode.
67 * @type: Name of the property within the configuration subnode.
68 * @index: Index into the list of strings in this property.
69 * @outname: Name of the image
71 * Return: 0 on success, or a negative error number
73 static int spl_fit_get_image_name(const void *fit, int images,
74 const char *type, int index,
77 struct udevice *sysinfo;
78 const char *name, *str;
79 __maybe_unused int node;
84 conf_node = fit_find_config_node(fit);
86 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
87 printf("No matching DT out of these options:\n");
88 for (node = fdt_first_subnode(fit, conf_node);
90 node = fdt_next_subnode(fit, node)) {
91 name = fdt_getprop(fit, node, "description", &len);
92 printf(" %s\n", name);
98 name = fdt_getprop(fit, conf_node, type, &len);
100 debug("cannot find property '%s': %d\n", type, len);
105 for (i = 0; i < index; i++) {
106 str = strchr(str, '\0') + 1;
107 if (!str || (str - name >= len)) {
113 if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) {
116 * no string in the property for this index. Check if the
117 * sysinfo-level code can supply one.
119 rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
121 if (rc && rc != -ENOENT)
126 * The sysinfo provided a name for a loadable.
127 * Try to match it against the description properties
128 * first. If no matching node is found, use it as a
132 int images = fdt_path_offset(fit, FIT_IMAGES_PATH);
134 node = find_node_from_desc(fit, images, str);
136 str = fdt_get_name(fit, node, NULL);
143 debug("no string for index %d\n", index);
152 * spl_fit_get_image_node(): By using the matching configuration subnode,
153 * retrieve the name of an image, specified by a property name and an index
155 * @fit: Pointer to the FDT blob.
156 * @images: Offset of the /images subnode.
157 * @type: Name of the property within the configuration subnode.
158 * @index: Index into the list of strings in this property.
160 * Return: the node offset of the respective image node or a negative
163 static int spl_fit_get_image_node(const void *fit, int images,
164 const char *type, int index)
170 err = spl_fit_get_image_name(fit, images, type, index, &str);
174 debug("%s: '%s'\n", type, str);
176 node = fdt_subnode_offset(fit, images, str);
178 pr_err("cannot find image node '%s': %d\n", str, node);
185 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
188 * If it is a FS read, get the first address before offset which is
189 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
190 * block number to which offset belongs.
193 return offset & ~(ARCH_DMA_MINALIGN - 1);
195 return offset / info->bl_len;
198 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
201 * If it is a FS read, get the difference between the offset and
202 * the first address before offset which is aligned to
203 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
207 return offset & (ARCH_DMA_MINALIGN - 1);
209 return offset % info->bl_len;
212 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
215 data_size = data_size + get_aligned_image_overhead(info, offset);
220 return (data_size + info->bl_len - 1) / info->bl_len;
224 * spl_load_fit_image(): load the image described in a certain FIT node
225 * @info: points to information about the device to load data from
226 * @sector: the start sector of the FIT image on the device
227 * @fit: points to the flattened device tree blob describing the FIT
229 * @base_offset: the beginning of the data area containing the actual
230 * image data, relative to the beginning of the FIT
231 * @node: offset of the DT node describing the image to load (relative
233 * @image_info: will be filled with information about the loaded image
234 * If the FIT node does not contain a "load" (address) property,
235 * the image gets loaded to the address pointed to by the
236 * load_addr member in this struct.
238 * Return: 0 on success or a negative error number.
240 static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
241 void *fit, ulong base_offset, int node,
242 struct spl_image_info *image_info)
248 ulong load_addr, load_ptr;
252 int align_len = ARCH_DMA_MINALIGN - 1;
253 uint8_t image_comp = -1, type = -1;
255 bool external_data = false;
257 if (IS_ENABLED(CONFIG_SPL_FPGA) ||
258 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
259 if (fit_image_get_type(fit, node, &type))
260 puts("Cannot get image type.\n");
262 debug("%s ", genimg_get_type_name(type));
265 if (IS_ENABLED(CONFIG_SPL_GZIP)) {
266 fit_image_get_comp(fit, node, &image_comp);
267 debug("%s ", genimg_get_comp_name(image_comp));
270 if (fit_image_get_load(fit, node, &load_addr))
271 load_addr = image_info->load_addr;
273 if (!fit_image_get_data_position(fit, node, &offset)) {
274 external_data = true;
275 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
276 offset += base_offset;
277 external_data = true;
282 if (fit_image_get_data_size(fit, node, &len))
285 load_ptr = (load_addr + align_len) & ~align_len;
288 overhead = get_aligned_image_overhead(info, offset);
289 nr_sectors = get_aligned_image_size(info, length, offset);
292 sector + get_aligned_image_offset(info, offset),
293 nr_sectors, (void *)load_ptr) != nr_sectors)
296 debug("External data: dst=%lx, offset=%x, size=%lx\n",
297 load_ptr, offset, (unsigned long)length);
298 src = (void *)load_ptr + overhead;
301 if (fit_image_get_data(fit, node, &data, &length)) {
302 puts("Cannot get image data/size\n");
305 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
306 (unsigned long)length);
310 #ifdef CONFIG_SPL_FIT_SIGNATURE
311 printf("## Checking hash(es) for Image %s ... ",
312 fit_get_name(fit, node, NULL));
313 if (!fit_image_verify_with_data(fit, node,
319 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
320 board_fit_image_post_process(&src, &length);
323 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
325 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
327 puts("Uncompressing error\n");
332 memcpy((void *)load_addr, src, length);
338 image_info->load_addr = load_addr;
339 image_info->size = length;
341 if (!fit_image_get_entry(fit, node, &entry_point))
342 image_info->entry_point = entry_point;
344 image_info->entry_point = FDT_ERROR;
350 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
351 struct spl_load_info *info, ulong sector,
352 void *fit, int images, ulong base_offset)
354 struct spl_image_info image_info;
355 int node, ret = 0, index = 0;
358 * Use the address following the image as target address for the
361 image_info.load_addr = spl_image->load_addr + spl_image->size;
363 /* Figure out which device tree the board wants to use */
364 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, index++);
366 debug("%s: cannot find FDT node\n", __func__);
369 * U-Boot did not find a device tree inside the FIT image. Use
370 * the U-Boot device tree instead.
373 memcpy((void *)image_info.load_addr, gd->fdt_blob,
374 fdt_totalsize(gd->fdt_blob));
378 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
384 /* Make the load-address of the FDT available for the SPL framework */
385 spl_image->fdt_addr = (void *)image_info.load_addr;
386 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
387 if (CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)) {
388 void *tmpbuffer = NULL;
391 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP,
393 if (node == -E2BIG) {
394 debug("%s: No additional FDT node\n", __func__);
396 } else if (node < 0) {
397 debug("%s: unable to find FDT node %d\n",
404 * allocate memory to store the DT overlay
405 * before it is applied. It may not be used
406 * depending on how the overlay is stored, so
407 * don't fail yet if the allocation failed.
409 tmpbuffer = malloc(CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ);
411 debug("%s: unable to allocate space for overlays\n",
414 image_info.load_addr = (ulong)tmpbuffer;
415 ret = spl_load_fit_image(info, sector, fit, base_offset,
420 /* Make room in FDT for changes from the overlay */
421 ret = fdt_increase_size(spl_image->fdt_addr,
426 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
427 (void *)image_info.load_addr);
429 pr_err("failed to apply DT overlay %s\n",
430 fit_get_name(fit, node, NULL));
434 debug("%s: DT overlay %s applied\n", __func__,
435 fit_get_name(fit, node, NULL));
441 /* Try to make space, so we can inject details on the loadables */
442 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
450 static int spl_fit_record_loadable(const void *fit, int images, int index,
451 void *blob, struct spl_image_info *image)
454 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
458 ret = spl_fit_get_image_name(fit, images, "loadables",
463 node = spl_fit_get_image_node(fit, images, "loadables", index);
465 ret = fdt_record_loadable(blob, index, name, image->load_addr,
466 image->size, image->entry_point,
467 fdt_getprop(fit, node, "type", NULL),
468 fdt_getprop(fit, node, "os", NULL));
473 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
475 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) && !defined(CONFIG_SPL_OS_BOOT)
476 const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
482 * We don't care what the type of the image actually is,
483 * only whether or not it is U-Boot. This saves some
484 * space by omitting the large table of OS types.
486 if (!strcmp(name, "u-boot"))
493 return fit_image_get_os(fit, noffset, os);
498 * Weak default function to allow customizing SPL fit loading for load-only
499 * use cases by allowing to skip the parsing/processing of the FIT contents
500 * (so that this can be done separately in a more customized fashion)
502 __weak bool spl_load_simple_fit_skip_processing(void)
507 int spl_load_simple_fit(struct spl_image_info *spl_image,
508 struct spl_load_info *info, ulong sector, void *fit)
513 struct spl_image_info image_info;
516 int base_offset, hsize, align_len = ARCH_DMA_MINALIGN - 1;
521 * For FIT with external data, figure out where the external images
522 * start. This is the base for the data-offset properties in each
525 size = fdt_totalsize(fit);
526 size = (size + 3) & ~3;
527 size = board_spl_fit_size_align(size);
528 base_offset = (size + 3) & ~3;
531 * So far we only have one block of data from the FIT. Read the entire
532 * thing, including that first block, placing it so it finishes before
533 * where we will load the image.
535 * Note that we will load the image such that its first byte will be
536 * at the load address. Since that byte may be part-way through a
537 * block, we may load the image up to one block before the load
538 * address. So take account of that here by subtracting an addition
539 * block length from the FIT start position.
541 * In fact the FIT has its own load address, but we assume it cannot
542 * be before CONFIG_SYS_TEXT_BASE.
544 * For FIT with data embedded, data is loaded as part of FIT image.
545 * For FIT with external data, data is not loaded in this step.
547 hsize = (size + info->bl_len + align_len) & ~align_len;
548 fit = spl_get_load_buffer(-hsize, hsize);
549 sectors = get_aligned_image_size(info, size, 0);
550 count = info->read(info, sector, sectors, fit);
551 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
552 sector, sectors, fit, count, size);
557 /* skip further processing if requested to enable load-only use cases */
558 if (spl_load_simple_fit_skip_processing())
561 if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
562 int conf_offset = fit_find_config_node(fit);
564 printf("## Checking hash(es) for config %s ... ",
565 fit_get_name(fit, conf_offset, NULL));
566 if (fit_config_verify(fit, conf_offset))
571 /* find the node holding the images information */
572 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
574 debug("%s: Cannot find /images node: %d\n", __func__, images);
578 #ifdef CONFIG_SPL_FPGA
579 node = spl_fit_get_image_node(fit, images, "fpga", 0);
581 /* Load the image and set up the spl_image structure */
582 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
585 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
589 debug("FPGA bitstream at: %x, size: %x\n",
590 (u32)spl_image->load_addr, spl_image->size);
592 ret = fpga_load(0, (const void *)spl_image->load_addr,
593 spl_image->size, BIT_FULL);
595 printf("%s: Cannot load the image to the FPGA\n",
600 puts("FPGA image loaded from FIT\n");
606 * Find the U-Boot image using the following search order:
607 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
608 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
609 * - fall back to using the first 'loadables' entry
612 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
614 #ifdef CONFIG_SPL_OS_BOOT
616 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
619 debug("could not find firmware image, trying loadables...\n");
620 node = spl_fit_get_image_node(fit, images, "loadables", 0);
622 * If we pick the U-Boot image from "loadables", start at
623 * the second image when later loading additional images.
628 debug("%s: Cannot find u-boot image node: %d\n",
633 /* Load the image and set up the spl_image structure */
634 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
640 * For backward compatibility, we treat the first node that is
641 * as a U-Boot image, if no OS-type has been declared.
643 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
644 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
645 #if !defined(CONFIG_SPL_OS_BOOT)
647 spl_image->os = IH_OS_U_BOOT;
651 * Booting a next-stage U-Boot may require us to append the FDT.
652 * We allow this to fail, as the U-Boot image might embed its FDT.
654 if (spl_image->os == IH_OS_U_BOOT) {
655 ret = spl_fit_append_fdt(spl_image, info, sector, fit,
656 images, base_offset);
657 if (!IS_ENABLED(CONFIG_OF_EMBED) && ret < 0)
661 firmware_node = node;
662 /* Now check if there are more images for us to load */
664 uint8_t os_type = IH_OS_INVALID;
666 node = spl_fit_get_image_node(fit, images, "loadables", index);
671 * if the firmware is also a loadable, skip it because
672 * it already has been loaded. This is typically the case with
673 * u-boot.img generated by mkimage.
675 if (firmware_node == node)
678 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
683 if (!spl_fit_image_get_os(fit, node, &os_type))
684 debug("Loadable is %s\n", genimg_get_os_name(os_type));
686 if (os_type == IH_OS_U_BOOT) {
687 spl_fit_append_fdt(&image_info, info, sector,
688 fit, images, base_offset);
689 spl_image->fdt_addr = image_info.fdt_addr;
693 * If the "firmware" image did not provide an entry point,
694 * use the first valid entry point from the loadables.
696 if (spl_image->entry_point == FDT_ERROR &&
697 image_info.entry_point != FDT_ERROR)
698 spl_image->entry_point = image_info.entry_point;
700 /* Record our loadables into the FDT */
701 if (spl_image->fdt_addr)
702 spl_fit_record_loadable(fit, images, index,
708 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
709 * Makefile will set it to 0 and it will end up as the entry point
710 * here. What it actually means is: use the load address.
712 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
713 spl_image->entry_point = spl_image->load_addr;
715 spl_image->flags |= SPL_FIT_FOUND;
717 #ifdef CONFIG_IMX_HAB
718 board_spl_fit_post_load((ulong)fit, size);