1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
10 #include <linux/libfdt.h>
13 #ifndef CONFIG_SYS_BOOTM_LEN
14 #define CONFIG_SYS_BOOTM_LEN (64 << 20)
18 * spl_fit_get_image_name(): By using the matching configuration subnode,
19 * retrieve the name of an image, specified by a property name and an index
21 * @fit: Pointer to the FDT blob.
22 * @images: Offset of the /images subnode.
23 * @type: Name of the property within the configuration subnode.
24 * @index: Index into the list of strings in this property.
25 * @outname: Name of the image
27 * Return: 0 on success, or a negative error number
29 static int spl_fit_get_image_name(const void *fit, int images,
30 const char *type, int index,
33 const char *name, *str;
34 __maybe_unused int node;
38 conf_node = fit_find_config_node(fit);
40 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
41 printf("No matching DT out of these options:\n");
42 for (node = fdt_first_subnode(fit, conf_node);
44 node = fdt_next_subnode(fit, node)) {
45 name = fdt_getprop(fit, node, "description", &len);
46 printf(" %s\n", name);
52 name = fdt_getprop(fit, conf_node, type, &len);
54 debug("cannot find property '%s': %d\n", type, len);
59 for (i = 0; i < index; i++) {
60 str = strchr(str, '\0') + 1;
61 if (!str || (str - name >= len)) {
62 debug("no string for index %d\n", index);
67 *outname = (char *)str;
72 * spl_fit_get_image_node(): By using the matching configuration subnode,
73 * retrieve the name of an image, specified by a property name and an index
75 * @fit: Pointer to the FDT blob.
76 * @images: Offset of the /images subnode.
77 * @type: Name of the property within the configuration subnode.
78 * @index: Index into the list of strings in this property.
80 * Return: the node offset of the respective image node or a negative
83 static int spl_fit_get_image_node(const void *fit, int images,
84 const char *type, int index)
90 err = spl_fit_get_image_name(fit, images, type, index, &str);
94 debug("%s: '%s'\n", type, str);
96 node = fdt_subnode_offset(fit, images, str);
98 debug("cannot find image node '%s': %d\n", str, node);
105 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
108 * If it is a FS read, get the first address before offset which is
109 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
110 * block number to which offset belongs.
113 return offset & ~(ARCH_DMA_MINALIGN - 1);
115 return offset / info->bl_len;
118 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
121 * If it is a FS read, get the difference between the offset and
122 * the first address before offset which is aligned to
123 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
127 return offset & (ARCH_DMA_MINALIGN - 1);
129 return offset % info->bl_len;
132 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
135 data_size = data_size + get_aligned_image_overhead(info, offset);
140 return (data_size + info->bl_len - 1) / info->bl_len;
143 #ifdef CONFIG_SPL_FPGA_SUPPORT
144 __weak int spl_load_fpga_image(struct spl_load_info *info, size_t length,
145 int nr_sectors, int sector_offset)
152 * spl_load_fit_image(): load the image described in a certain FIT node
153 * @info: points to information about the device to load data from
154 * @sector: the start sector of the FIT image on the device
155 * @fit: points to the flattened device tree blob describing the FIT
157 * @base_offset: the beginning of the data area containing the actual
158 * image data, relative to the beginning of the FIT
159 * @node: offset of the DT node describing the image to load (relative
161 * @image_info: will be filled with information about the loaded image
162 * If the FIT node does not contain a "load" (address) property,
163 * the image gets loaded to the address pointed to by the
164 * load_addr member in this struct.
166 * Return: 0 on success or a negative error number.
168 static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
169 void *fit, ulong base_offset, int node,
170 struct spl_image_info *image_info)
172 int offset, sector_offset;
176 ulong load_addr, load_ptr;
180 int align_len = ARCH_DMA_MINALIGN - 1;
181 uint8_t image_comp = -1, type = -1;
183 bool external_data = false;
184 #ifdef CONFIG_SPL_FIT_SIGNATURE
188 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
189 if (fit_image_get_comp(fit, node, &image_comp))
190 puts("Cannot get image compression format.\n");
192 debug("%s ", genimg_get_comp_name(image_comp));
194 if (fit_image_get_type(fit, node, &type))
195 puts("Cannot get image type.\n");
197 debug("%s ", genimg_get_type_name(type));
200 if (fit_image_get_load(fit, node, &load_addr))
201 load_addr = image_info->load_addr;
203 if (!fit_image_get_data_position(fit, node, &offset)) {
204 external_data = true;
205 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
206 offset += base_offset;
207 external_data = true;
212 if (fit_image_get_data_size(fit, node, &len))
215 load_ptr = (load_addr + align_len) & ~align_len;
218 overhead = get_aligned_image_overhead(info, offset);
219 nr_sectors = get_aligned_image_size(info, length, offset);
220 sector_offset = sector + get_aligned_image_offset(info, offset);
222 #ifdef CONFIG_SPL_FPGA_SUPPORT
223 if (type == IH_TYPE_FPGA) {
224 return spl_load_fpga_image(info, length, nr_sectors,
229 if (info->read(info, sector_offset,
230 nr_sectors, (void *)load_ptr) != nr_sectors)
233 debug("External data: dst=%lx, offset=%x, size=%lx\n",
234 load_ptr, offset, (unsigned long)length);
235 src = (void *)load_ptr + overhead;
238 if (fit_image_get_data(fit, node, &data, &length)) {
239 puts("Cannot get image data/size\n");
242 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
243 (unsigned long)length);
247 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
248 board_fit_image_post_process(&src, &length);
251 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) &&
252 IS_ENABLED(CONFIG_SPL_GZIP) &&
253 image_comp == IH_COMP_GZIP &&
254 type == IH_TYPE_KERNEL) {
256 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
258 puts("Uncompressing error\n");
263 memcpy((void *)load_addr, src, length);
267 image_info->load_addr = load_addr;
268 image_info->size = length;
269 image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
272 #ifdef CONFIG_SPL_FIT_SIGNATURE
273 printf("## Checking hash(es) for Image %s ...\n",
274 fit_get_name(fit, node, NULL));
275 ret = fit_image_verify_with_data(fit, node,
276 (const void *)load_addr, length);
284 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
285 struct spl_load_info *info, ulong sector,
286 void *fit, int images, ulong base_offset)
288 struct spl_image_info image_info;
291 /* Figure out which device tree the board wants to use */
292 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
294 debug("%s: cannot find FDT node\n", __func__);
299 * Read the device tree and place it after the image.
300 * Align the destination address to ARCH_DMA_MINALIGN.
302 image_info.load_addr = spl_image->load_addr + spl_image->size;
303 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
309 /* Make the load-address of the FDT available for the SPL framework */
310 spl_image->fdt_addr = (void *)image_info.load_addr;
311 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
312 /* Try to make space, so we can inject details on the loadables */
313 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
319 static int spl_fit_record_loadable(const void *fit, int images, int index,
320 void *blob, struct spl_image_info *image)
323 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
327 ret = spl_fit_get_image_name(fit, images, "loadables",
332 node = spl_fit_get_image_node(fit, images, "loadables", index);
334 ret = fdt_record_loadable(blob, index, name, image->load_addr,
335 image->size, image->entry_point,
336 fdt_getprop(fit, node, "type", NULL),
337 fdt_getprop(fit, node, "os", NULL));
342 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
344 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
347 return fit_image_get_os(fit, noffset, os);
351 int spl_load_simple_fit(struct spl_image_info *spl_image,
352 struct spl_load_info *info, ulong sector, void *fit)
357 struct spl_image_info image_info;
360 int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
364 * For FIT with external data, figure out where the external images
365 * start. This is the base for the data-offset properties in each
368 size = fdt_totalsize(fit);
369 size = (size + 3) & ~3;
370 base_offset = (size + 3) & ~3;
373 * So far we only have one block of data from the FIT. Read the entire
374 * thing, including that first block, placing it so it finishes before
375 * where we will load the image.
377 * Note that we will load the image such that its first byte will be
378 * at the load address. Since that byte may be part-way through a
379 * block, we may load the image up to one block before the load
380 * address. So take account of that here by subtracting an addition
381 * block length from the FIT start position.
383 * In fact the FIT has its own load address, but we assume it cannot
384 * be before CONFIG_SYS_TEXT_BASE.
386 * For FIT with data embedded, data is loaded as part of FIT image.
387 * For FIT with external data, data is not loaded in this step.
389 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
390 align_len) & ~align_len);
391 sectors = get_aligned_image_size(info, size, 0);
392 count = info->read(info, sector, sectors, fit);
393 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
394 sector, sectors, fit, count);
398 /* find the node holding the images information */
399 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
401 debug("%s: Cannot find /images node: %d\n", __func__, images);
405 #ifdef CONFIG_SPL_FPGA_SUPPORT
406 node = spl_fit_get_image_node(fit, images, "fpga", 0);
408 /* Load the image and set up the spl_image structure */
409 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
412 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
420 * Find the U-Boot image using the following search order:
421 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
422 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
423 * - fall back to using the first 'loadables' entry
426 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
428 #ifdef CONFIG_SPL_OS_BOOT
430 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
433 debug("could not find firmware image, trying loadables...\n");
434 node = spl_fit_get_image_node(fit, images, "loadables", 0);
436 * If we pick the U-Boot image from "loadables", start at
437 * the second image when later loading additional images.
442 debug("%s: Cannot find u-boot image node: %d\n",
447 /* Load the image and set up the spl_image structure */
448 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
454 * For backward compatibility, we treat the first node that is
455 * as a U-Boot image, if no OS-type has been declared.
457 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
458 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
459 #if !defined(CONFIG_SPL_OS_BOOT)
461 spl_image->os = IH_OS_U_BOOT;
465 * Booting a next-stage U-Boot may require us to append the FDT.
466 * We allow this to fail, as the U-Boot image might embed its FDT.
468 if (spl_image->os == IH_OS_U_BOOT)
469 spl_fit_append_fdt(spl_image, info, sector, fit,
470 images, base_offset);
472 /* Now check if there are more images for us to load */
474 uint8_t os_type = IH_OS_INVALID;
476 node = spl_fit_get_image_node(fit, images, "loadables", index);
480 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
485 if (!spl_fit_image_get_os(fit, node, &os_type))
486 debug("Loadable is %s\n", genimg_get_os_name(os_type));
488 if (os_type == IH_OS_U_BOOT) {
489 spl_fit_append_fdt(&image_info, info, sector,
490 fit, images, base_offset);
491 spl_image->fdt_addr = image_info.fdt_addr;
495 * If the "firmware" image did not provide an entry point,
496 * use the first valid entry point from the loadables.
498 if (spl_image->entry_point == FDT_ERROR &&
499 image_info.entry_point != FDT_ERROR)
500 spl_image->entry_point = image_info.entry_point;
502 /* Record our loadables into the FDT */
503 if (spl_image->fdt_addr)
504 spl_fit_record_loadable(fit, images, index,
510 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
511 * Makefile will set it to 0 and it will end up as the entry point
512 * here. What it actually means is: use the load address.
514 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
515 spl_image->entry_point = spl_image->load_addr;