2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
14 #ifndef CONFIG_SYS_BOOTM_LEN
15 #define CONFIG_SYS_BOOTM_LEN (64 << 20)
19 * spl_fit_get_image_name(): By using the matching configuration subnode,
20 * retrieve the name of an image, specified by a property name and an index
22 * @fit: Pointer to the FDT blob.
23 * @images: Offset of the /images subnode.
24 * @type: Name of the property within the configuration subnode.
25 * @index: Index into the list of strings in this property.
26 * @outname: Name of the image
28 * Return: 0 on success, or a negative error number
30 static int spl_fit_get_image_name(const void *fit
, int images
,
31 const char *type
, int index
,
34 const char *name
, *str
;
35 __maybe_unused
int node
;
39 conf_node
= fit_find_config_node(fit
);
41 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
42 printf("No matching DT out of these options:\n");
43 for (node
= fdt_first_subnode(fit
, conf_node
);
45 node
= fdt_next_subnode(fit
, node
)) {
46 name
= fdt_getprop(fit
, node
, "description", &len
);
47 printf(" %s\n", name
);
53 name
= fdt_getprop(fit
, conf_node
, type
, &len
);
55 debug("cannot find property '%s': %d\n", type
, len
);
60 for (i
= 0; i
< index
; i
++) {
61 str
= strchr(str
, '\0') + 1;
62 if (!str
|| (str
- name
>= len
)) {
63 debug("no string for index %d\n", index
);
68 *outname
= (char *)str
;
73 * spl_fit_get_image_node(): By using the matching configuration subnode,
74 * retrieve the name of an image, specified by a property name and an index
76 * @fit: Pointer to the FDT blob.
77 * @images: Offset of the /images subnode.
78 * @type: Name of the property within the configuration subnode.
79 * @index: Index into the list of strings in this property.
81 * Return: the node offset of the respective image node or a negative
84 static int spl_fit_get_image_node(const void *fit
, int images
,
85 const char *type
, int index
)
91 err
= spl_fit_get_image_name(fit
, images
, type
, index
, &str
);
95 debug("%s: '%s'\n", type
, str
);
97 node
= fdt_subnode_offset(fit
, images
, str
);
99 debug("cannot find image node '%s': %d\n", str
, node
);
106 static int get_aligned_image_offset(struct spl_load_info
*info
, int offset
)
109 * If it is a FS read, get the first address before offset which is
110 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
111 * block number to which offset belongs.
114 return offset
& ~(ARCH_DMA_MINALIGN
- 1);
116 return offset
/ info
->bl_len
;
119 static int get_aligned_image_overhead(struct spl_load_info
*info
, int offset
)
122 * If it is a FS read, get the difference between the offset and
123 * the first address before offset which is aligned to
124 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
128 return offset
& (ARCH_DMA_MINALIGN
- 1);
130 return offset
% info
->bl_len
;
133 static int get_aligned_image_size(struct spl_load_info
*info
, int data_size
,
136 data_size
= data_size
+ get_aligned_image_overhead(info
, offset
);
141 return (data_size
+ info
->bl_len
- 1) / info
->bl_len
;
145 * spl_load_fit_image(): load the image described in a certain FIT node
146 * @info: points to information about the device to load data from
147 * @sector: the start sector of the FIT image on the device
148 * @fit: points to the flattened device tree blob describing the FIT
150 * @base_offset: the beginning of the data area containing the actual
151 * image data, relative to the beginning of the FIT
152 * @node: offset of the DT node describing the image to load (relative
154 * @image_info: will be filled with information about the loaded image
155 * If the FIT node does not contain a "load" (address) property,
156 * the image gets loaded to the address pointed to by the
157 * load_addr member in this struct.
159 * Return: 0 on success or a negative error number.
161 static int spl_load_fit_image(struct spl_load_info
*info
, ulong sector
,
162 void *fit
, ulong base_offset
, int node
,
163 struct spl_image_info
*image_info
)
169 ulong load_addr
, load_ptr
;
173 int align_len
= ARCH_DMA_MINALIGN
- 1;
174 uint8_t image_comp
= -1, type
= -1;
177 if (IS_ENABLED(CONFIG_SPL_OS_BOOT
) && IS_ENABLED(CONFIG_SPL_GZIP
)) {
178 if (fit_image_get_comp(fit
, node
, &image_comp
))
179 puts("Cannot get image compression format.\n");
181 debug("%s ", genimg_get_comp_name(image_comp
));
183 if (fit_image_get_type(fit
, node
, &type
))
184 puts("Cannot get image type.\n");
186 debug("%s ", genimg_get_type_name(type
));
189 if (fit_image_get_load(fit
, node
, &load_addr
))
190 load_addr
= image_info
->load_addr
;
192 if (!fit_image_get_data_offset(fit
, node
, &offset
)) {
194 offset
+= base_offset
;
195 if (fit_image_get_data_size(fit
, node
, &len
))
198 load_ptr
= (load_addr
+ align_len
) & ~align_len
;
201 overhead
= get_aligned_image_overhead(info
, offset
);
202 nr_sectors
= get_aligned_image_size(info
, length
, offset
);
205 sector
+ get_aligned_image_offset(info
, offset
),
206 nr_sectors
, (void *)load_ptr
) != nr_sectors
)
209 debug("External data: dst=%lx, offset=%x, size=%lx\n",
210 load_ptr
, offset
, (unsigned long)length
);
211 src
= (void *)load_ptr
+ overhead
;
214 if (fit_image_get_data(fit
, node
, &data
, &length
)) {
215 puts("Cannot get image data/size\n");
218 debug("Embedded data: dst=%lx, size=%lx\n", load_addr
,
219 (unsigned long)length
);
223 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
224 board_fit_image_post_process(&src
, &length
);
227 if (IS_ENABLED(CONFIG_SPL_OS_BOOT
) &&
228 IS_ENABLED(CONFIG_SPL_GZIP
) &&
229 image_comp
== IH_COMP_GZIP
&&
230 type
== IH_TYPE_KERNEL
) {
232 if (gunzip((void *)load_addr
, CONFIG_SYS_BOOTM_LEN
,
234 puts("Uncompressing error\n");
239 memcpy((void *)load_addr
, src
, length
);
243 image_info
->load_addr
= load_addr
;
244 image_info
->size
= length
;
245 image_info
->entry_point
= fdt_getprop_u32(fit
, node
, "entry");
251 static int spl_fit_append_fdt(struct spl_image_info
*spl_image
,
252 struct spl_load_info
*info
, ulong sector
,
253 void *fit
, int images
, ulong base_offset
)
255 struct spl_image_info image_info
;
258 /* Figure out which device tree the board wants to use */
259 node
= spl_fit_get_image_node(fit
, images
, FIT_FDT_PROP
, 0);
261 debug("%s: cannot find FDT node\n", __func__
);
266 * Read the device tree and place it after the image.
267 * Align the destination address to ARCH_DMA_MINALIGN.
269 image_info
.load_addr
= spl_image
->load_addr
+ spl_image
->size
;
270 ret
= spl_load_fit_image(info
, sector
, fit
, base_offset
, node
,
276 /* Make the load-address of the FDT available for the SPL framework */
277 spl_image
->fdt_addr
= (void *)image_info
.load_addr
;
278 /* Try to make space, so we can inject details on the loadables */
279 ret
= fdt_shrink_to_minimum(spl_image
->fdt_addr
, 8192);
284 static int spl_fit_record_loadable(const void *fit
, int images
, int index
,
285 void *blob
, struct spl_image_info
*image
)
290 ret
= spl_fit_get_image_name(fit
, images
, "loadables",
295 node
= spl_fit_get_image_node(fit
, images
, "loadables", index
);
297 ret
= fdt_record_loadable(blob
, index
, name
, image
->load_addr
,
298 image
->size
, image
->entry_point
,
299 fdt_getprop(fit
, node
, "type", NULL
),
300 fdt_getprop(fit
, node
, "os", NULL
));
304 int spl_load_simple_fit(struct spl_image_info
*spl_image
,
305 struct spl_load_info
*info
, ulong sector
, void *fit
)
310 struct spl_image_info image_info
;
313 int base_offset
, align_len
= ARCH_DMA_MINALIGN
- 1;
317 * For FIT with external data, figure out where the external images
318 * start. This is the base for the data-offset properties in each
321 size
= fdt_totalsize(fit
);
322 size
= (size
+ 3) & ~3;
323 base_offset
= (size
+ 3) & ~3;
326 * So far we only have one block of data from the FIT. Read the entire
327 * thing, including that first block, placing it so it finishes before
328 * where we will load the image.
330 * Note that we will load the image such that its first byte will be
331 * at the load address. Since that byte may be part-way through a
332 * block, we may load the image up to one block before the load
333 * address. So take account of that here by subtracting an addition
334 * block length from the FIT start position.
336 * In fact the FIT has its own load address, but we assume it cannot
337 * be before CONFIG_SYS_TEXT_BASE.
339 * For FIT with data embedded, data is loaded as part of FIT image.
340 * For FIT with external data, data is not loaded in this step.
342 fit
= (void *)((CONFIG_SYS_TEXT_BASE
- size
- info
->bl_len
-
343 align_len
) & ~align_len
);
344 sectors
= get_aligned_image_size(info
, size
, 0);
345 count
= info
->read(info
, sector
, sectors
, fit
);
346 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
347 sector
, sectors
, fit
, count
);
351 /* find the node holding the images information */
352 images
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
354 debug("%s: Cannot find /images node: %d\n", __func__
, images
);
359 * Find the U-Boot image using the following search order:
360 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
361 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
362 * - fall back to using the first 'loadables' entry
365 node
= spl_fit_get_image_node(fit
, images
, "firmware", 0);
366 #ifdef CONFIG_SPL_OS_BOOT
368 node
= spl_fit_get_image_node(fit
, images
, FIT_KERNEL_PROP
, 0);
371 debug("could not find firmware image, trying loadables...\n");
372 node
= spl_fit_get_image_node(fit
, images
, "loadables", 0);
374 * If we pick the U-Boot image from "loadables", start at
375 * the second image when later loading additional images.
380 debug("%s: Cannot find u-boot image node: %d\n",
385 /* Load the image and set up the spl_image structure */
386 ret
= spl_load_fit_image(info
, sector
, fit
, base_offset
, node
,
392 * For backward compatibility, we treat the first node that is
393 * as a U-Boot image, if no OS-type has been declared.
395 if (!fit_image_get_os(fit
, node
, &spl_image
->os
))
396 debug("Image OS is %s\n", genimg_get_os_name(spl_image
->os
));
397 #if !defined(CONFIG_SPL_OS_BOOT)
399 spl_image
->os
= IH_OS_U_BOOT
;
403 * Booting a next-stage U-Boot may require us to append the FDT.
404 * We allow this to fail, as the U-Boot image might embed its FDT.
406 if (spl_image
->os
== IH_OS_U_BOOT
)
407 spl_fit_append_fdt(spl_image
, info
, sector
, fit
,
408 images
, base_offset
);
410 /* Now check if there are more images for us to load */
412 uint8_t os_type
= IH_OS_INVALID
;
414 node
= spl_fit_get_image_node(fit
, images
, "loadables", index
);
418 ret
= spl_load_fit_image(info
, sector
, fit
, base_offset
, node
,
423 if (!fit_image_get_os(fit
, node
, &os_type
))
424 debug("Loadable is %s\n", genimg_get_os_name(os_type
));
426 if (os_type
== IH_OS_U_BOOT
) {
427 spl_fit_append_fdt(&image_info
, info
, sector
,
428 fit
, images
, base_offset
);
429 spl_image
->fdt_addr
= image_info
.fdt_addr
;
433 * If the "firmware" image did not provide an entry point,
434 * use the first valid entry point from the loadables.
436 if (spl_image
->entry_point
== FDT_ERROR
&&
437 image_info
.entry_point
!= FDT_ERROR
)
438 spl_image
->entry_point
= image_info
.entry_point
;
440 /* Record our loadables into the FDT */
441 if (spl_image
->fdt_addr
)
442 spl_fit_record_loadable(fit
, images
, index
,
448 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
449 * Makefile will set it to 0 and it will end up as the entry point
450 * here. What it actually means is: use the load address.
452 if (spl_image
->entry_point
== FDT_ERROR
|| spl_image
->entry_point
== 0)
453 spl_image
->entry_point
= spl_image
->load_addr
;