1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 #include <linux/libfdt.h>
14 #ifndef CONFIG_SYS_BOOTM_LEN
15 #define CONFIG_SYS_BOOTM_LEN (64 << 20)
18 __weak void board_spl_fit_post_load(ulong load_addr, size_t length)
22 __weak ulong board_spl_fit_size_align(ulong size)
28 * spl_fit_get_image_name(): By using the matching configuration subnode,
29 * retrieve the name of an image, specified by a property name and an index
31 * @fit: Pointer to the FDT blob.
32 * @images: Offset of the /images subnode.
33 * @type: Name of the property within the configuration subnode.
34 * @index: Index into the list of strings in this property.
35 * @outname: Name of the image
37 * Return: 0 on success, or a negative error number
39 static int spl_fit_get_image_name(const void *fit, int images,
40 const char *type, int index,
43 const char *name, *str;
44 __maybe_unused int node;
48 conf_node = fit_find_config_node(fit);
50 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
51 printf("No matching DT out of these options:\n");
52 for (node = fdt_first_subnode(fit, conf_node);
54 node = fdt_next_subnode(fit, node)) {
55 name = fdt_getprop(fit, node, "description", &len);
56 printf(" %s\n", name);
62 name = fdt_getprop(fit, conf_node, type, &len);
64 debug("cannot find property '%s': %d\n", type, len);
69 for (i = 0; i < index; i++) {
70 str = strchr(str, '\0') + 1;
71 if (!str || (str - name >= len)) {
72 debug("no string for index %d\n", index);
77 *outname = (char *)str;
82 * spl_fit_get_image_node(): By using the matching configuration subnode,
83 * retrieve the name of an image, specified by a property name and an index
85 * @fit: Pointer to the FDT blob.
86 * @images: Offset of the /images subnode.
87 * @type: Name of the property within the configuration subnode.
88 * @index: Index into the list of strings in this property.
90 * Return: the node offset of the respective image node or a negative
93 static int spl_fit_get_image_node(const void *fit, int images,
94 const char *type, int index)
100 err = spl_fit_get_image_name(fit, images, type, index, &str);
104 debug("%s: '%s'\n", type, str);
106 node = fdt_subnode_offset(fit, images, str);
108 debug("cannot find image node '%s': %d\n", str, node);
115 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
118 * If it is a FS read, get the first address before offset which is
119 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
120 * block number to which offset belongs.
123 return offset & ~(ARCH_DMA_MINALIGN - 1);
125 return offset / info->bl_len;
128 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
131 * If it is a FS read, get the difference between the offset and
132 * the first address before offset which is aligned to
133 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
137 return offset & (ARCH_DMA_MINALIGN - 1);
139 return offset % info->bl_len;
142 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
145 data_size = data_size + get_aligned_image_overhead(info, offset);
150 return (data_size + info->bl_len - 1) / info->bl_len;
154 * spl_load_fit_image(): load the image described in a certain FIT node
155 * @info: points to information about the device to load data from
156 * @sector: the start sector of the FIT image on the device
157 * @fit: points to the flattened device tree blob describing the FIT
159 * @base_offset: the beginning of the data area containing the actual
160 * image data, relative to the beginning of the FIT
161 * @node: offset of the DT node describing the image to load (relative
163 * @image_info: will be filled with information about the loaded image
164 * If the FIT node does not contain a "load" (address) property,
165 * the image gets loaded to the address pointed to by the
166 * load_addr member in this struct.
168 * Return: 0 on success or a negative error number.
170 static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
171 void *fit, ulong base_offset, int node,
172 struct spl_image_info *image_info)
178 ulong load_addr, load_ptr;
182 int align_len = ARCH_DMA_MINALIGN - 1;
183 uint8_t image_comp = -1, type = -1;
185 bool external_data = false;
187 if (IS_ENABLED(CONFIG_SPL_FPGA_SUPPORT) ||
188 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
189 if (fit_image_get_type(fit, node, &type))
190 puts("Cannot get image type.\n");
192 debug("%s ", genimg_get_type_name(type));
195 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
196 if (fit_image_get_comp(fit, node, &image_comp))
197 puts("Cannot get image compression format.\n");
199 debug("%s ", genimg_get_comp_name(image_comp));
202 if (fit_image_get_load(fit, node, &load_addr))
203 load_addr = image_info->load_addr;
205 if (!fit_image_get_data_position(fit, node, &offset)) {
206 external_data = true;
207 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
208 offset += base_offset;
209 external_data = true;
214 if (fit_image_get_data_size(fit, node, &len))
217 load_ptr = (load_addr + align_len) & ~align_len;
220 overhead = get_aligned_image_overhead(info, offset);
221 nr_sectors = get_aligned_image_size(info, length, offset);
224 sector + get_aligned_image_offset(info, offset),
225 nr_sectors, (void *)load_ptr) != nr_sectors)
228 debug("External data: dst=%lx, offset=%x, size=%lx\n",
229 load_ptr, offset, (unsigned long)length);
230 src = (void *)load_ptr + overhead;
233 if (fit_image_get_data(fit, node, &data, &length)) {
234 puts("Cannot get image data/size\n");
237 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
238 (unsigned long)length);
242 #ifdef CONFIG_SPL_FIT_SIGNATURE
243 printf("## Checking hash(es) for Image %s ... ",
244 fit_get_name(fit, node, NULL));
245 if (!fit_image_verify_with_data(fit, node,
251 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
252 board_fit_image_post_process(&src, &length);
255 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
257 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
259 puts("Uncompressing error\n");
264 memcpy((void *)load_addr, src, length);
268 image_info->load_addr = load_addr;
269 image_info->size = length;
270 image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
276 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
277 struct spl_load_info *info, ulong sector,
278 void *fit, int images, ulong base_offset)
280 struct spl_image_info image_info;
283 /* Figure out which device tree the board wants to use */
284 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
286 debug("%s: cannot find FDT node\n", __func__);
291 * Read the device tree and place it after the image.
292 * Align the destination address to ARCH_DMA_MINALIGN.
294 image_info.load_addr = spl_image->load_addr + spl_image->size;
295 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
301 /* Make the load-address of the FDT available for the SPL framework */
302 spl_image->fdt_addr = (void *)image_info.load_addr;
303 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
304 /* Try to make space, so we can inject details on the loadables */
305 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
311 static int spl_fit_record_loadable(const void *fit, int images, int index,
312 void *blob, struct spl_image_info *image)
315 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
319 ret = spl_fit_get_image_name(fit, images, "loadables",
324 node = spl_fit_get_image_node(fit, images, "loadables", index);
326 ret = fdt_record_loadable(blob, index, name, image->load_addr,
327 image->size, image->entry_point,
328 fdt_getprop(fit, node, "type", NULL),
329 fdt_getprop(fit, node, "os", NULL));
334 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
336 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
339 return fit_image_get_os(fit, noffset, os);
343 int spl_load_simple_fit(struct spl_image_info *spl_image,
344 struct spl_load_info *info, ulong sector, void *fit)
349 struct spl_image_info image_info;
352 int base_offset, hsize, align_len = ARCH_DMA_MINALIGN - 1;
356 * For FIT with external data, figure out where the external images
357 * start. This is the base for the data-offset properties in each
360 size = fdt_totalsize(fit);
361 size = (size + 3) & ~3;
362 size = board_spl_fit_size_align(size);
363 base_offset = (size + 3) & ~3;
366 * So far we only have one block of data from the FIT. Read the entire
367 * thing, including that first block, placing it so it finishes before
368 * where we will load the image.
370 * Note that we will load the image such that its first byte will be
371 * at the load address. Since that byte may be part-way through a
372 * block, we may load the image up to one block before the load
373 * address. So take account of that here by subtracting an addition
374 * block length from the FIT start position.
376 * In fact the FIT has its own load address, but we assume it cannot
377 * be before CONFIG_SYS_TEXT_BASE.
379 * For FIT with data embedded, data is loaded as part of FIT image.
380 * For FIT with external data, data is not loaded in this step.
382 hsize = (size + info->bl_len + align_len) & ~align_len;
383 fit = spl_get_load_buffer(-hsize, hsize);
384 sectors = get_aligned_image_size(info, size, 0);
385 count = info->read(info, sector, sectors, fit);
386 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
387 sector, sectors, fit, count, size);
392 /* find the node holding the images information */
393 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
395 debug("%s: Cannot find /images node: %d\n", __func__, images);
399 #ifdef CONFIG_SPL_FPGA_SUPPORT
400 node = spl_fit_get_image_node(fit, images, "fpga", 0);
402 /* Load the image and set up the spl_image structure */
403 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
406 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
410 debug("FPGA bitstream at: %x, size: %x\n",
411 (u32)spl_image->load_addr, spl_image->size);
413 ret = fpga_load(0, (const void *)spl_image->load_addr,
414 spl_image->size, BIT_FULL);
416 printf("%s: Cannot load the image to the FPGA\n",
421 puts("FPGA image loaded from FIT\n");
427 * Find the U-Boot image using the following search order:
428 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
429 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
430 * - fall back to using the first 'loadables' entry
433 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
435 #ifdef CONFIG_SPL_OS_BOOT
437 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
440 debug("could not find firmware image, trying loadables...\n");
441 node = spl_fit_get_image_node(fit, images, "loadables", 0);
443 * If we pick the U-Boot image from "loadables", start at
444 * the second image when later loading additional images.
449 debug("%s: Cannot find u-boot image node: %d\n",
454 /* Load the image and set up the spl_image structure */
455 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
461 * For backward compatibility, we treat the first node that is
462 * as a U-Boot image, if no OS-type has been declared.
464 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
465 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
466 #if !defined(CONFIG_SPL_OS_BOOT)
468 spl_image->os = IH_OS_U_BOOT;
472 * Booting a next-stage U-Boot may require us to append the FDT.
473 * We allow this to fail, as the U-Boot image might embed its FDT.
475 if (spl_image->os == IH_OS_U_BOOT)
476 spl_fit_append_fdt(spl_image, info, sector, fit,
477 images, base_offset);
479 /* Now check if there are more images for us to load */
481 uint8_t os_type = IH_OS_INVALID;
483 node = spl_fit_get_image_node(fit, images, "loadables", index);
487 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
492 if (!spl_fit_image_get_os(fit, node, &os_type))
493 debug("Loadable is %s\n", genimg_get_os_name(os_type));
495 if (os_type == IH_OS_U_BOOT) {
496 spl_fit_append_fdt(&image_info, info, sector,
497 fit, images, base_offset);
498 spl_image->fdt_addr = image_info.fdt_addr;
502 * If the "firmware" image did not provide an entry point,
503 * use the first valid entry point from the loadables.
505 if (spl_image->entry_point == FDT_ERROR &&
506 image_info.entry_point != FDT_ERROR)
507 spl_image->entry_point = image_info.entry_point;
509 /* Record our loadables into the FDT */
510 if (spl_image->fdt_addr)
511 spl_fit_record_loadable(fit, images, index,
517 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
518 * Makefile will set it to 0 and it will end up as the entry point
519 * here. What it actually means is: use the load address.
521 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
522 spl_image->entry_point = spl_image->load_addr;
524 spl_image->flags |= SPL_FIT_FOUND;
526 #ifdef CONFIG_SECURE_BOOT
527 board_spl_fit_post_load((ulong)fit, size);