blob: 65dd835eccd4bb3e3b9eec857d65435b85e37822 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassf1dcee52016-02-22 22:55:56 -07002/*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassf1dcee52016-02-22 22:55:56 -07005 */
6
7#include <common.h>
8#include <errno.h>
Michal Simek3313ae62018-07-18 14:33:15 +02009#include <fpga.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060010#include <gzip.h>
Simon Glassf1dcee52016-02-22 22:55:56 -070011#include <image.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Simon Glassf1dcee52016-02-22 22:55:56 -070013#include <spl.h>
14
Lukas Auerb83edfb2019-08-21 21:14:42 +020015DECLARE_GLOBAL_DATA_PTR;
16
York Sun7264f292017-08-15 11:14:43 -070017#ifndef CONFIG_SYS_BOOTM_LEN
18#define CONFIG_SYS_BOOTM_LEN (64 << 20)
19#endif
20
Ye Lie246bfc2018-11-17 09:10:25 +000021__weak void board_spl_fit_post_load(ulong load_addr, size_t length)
22{
23}
24
25__weak ulong board_spl_fit_size_align(ulong size)
26{
27 return size;
28}
29
Andre Przywara736806f2017-04-26 01:32:34 +010030/**
Philipp Tomsicha616c782017-09-13 21:29:34 +020031 * spl_fit_get_image_name(): By using the matching configuration subnode,
Andre Przywara736806f2017-04-26 01:32:34 +010032 * retrieve the name of an image, specified by a property name and an index
33 * into that.
34 * @fit: Pointer to the FDT blob.
35 * @images: Offset of the /images subnode.
36 * @type: Name of the property within the configuration subnode.
37 * @index: Index into the list of strings in this property.
Philipp Tomsicha616c782017-09-13 21:29:34 +020038 * @outname: Name of the image
Andre Przywara736806f2017-04-26 01:32:34 +010039 *
Philipp Tomsicha616c782017-09-13 21:29:34 +020040 * Return: 0 on success, or a negative error number
Andre Przywara736806f2017-04-26 01:32:34 +010041 */
Philipp Tomsicha616c782017-09-13 21:29:34 +020042static int spl_fit_get_image_name(const void *fit, int images,
43 const char *type, int index,
44 char **outname)
Andre Przywara4b9340a2017-04-26 01:32:33 +010045{
46 const char *name, *str;
Philipp Tomsicha616c782017-09-13 21:29:34 +020047 __maybe_unused int node;
48 int conf_node;
Andre Przywara4b9340a2017-04-26 01:32:33 +010049 int len, i;
50
Cooper Jr., Franklin3863f842017-06-16 17:25:05 -050051 conf_node = fit_find_config_node(fit);
Andre Przywara4b9340a2017-04-26 01:32:33 +010052 if (conf_node < 0) {
53#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
54 printf("No matching DT out of these options:\n");
55 for (node = fdt_first_subnode(fit, conf_node);
56 node >= 0;
57 node = fdt_next_subnode(fit, node)) {
58 name = fdt_getprop(fit, node, "description", &len);
59 printf(" %s\n", name);
60 }
61#endif
62 return conf_node;
63 }
64
65 name = fdt_getprop(fit, conf_node, type, &len);
66 if (!name) {
67 debug("cannot find property '%s': %d\n", type, len);
68 return -EINVAL;
69 }
70
71 str = name;
72 for (i = 0; i < index; i++) {
73 str = strchr(str, '\0') + 1;
74 if (!str || (str - name >= len)) {
75 debug("no string for index %d\n", index);
76 return -E2BIG;
77 }
78 }
79
Philipp Tomsicha616c782017-09-13 21:29:34 +020080 *outname = (char *)str;
81 return 0;
82}
83
84/**
85 * spl_fit_get_image_node(): By using the matching configuration subnode,
86 * retrieve the name of an image, specified by a property name and an index
87 * into that.
88 * @fit: Pointer to the FDT blob.
89 * @images: Offset of the /images subnode.
90 * @type: Name of the property within the configuration subnode.
91 * @index: Index into the list of strings in this property.
92 *
93 * Return: the node offset of the respective image node or a negative
94 * error number.
95 */
96static int spl_fit_get_image_node(const void *fit, int images,
97 const char *type, int index)
98{
99 char *str;
100 int err;
101 int node;
102
103 err = spl_fit_get_image_name(fit, images, type, index, &str);
104 if (err)
105 return err;
106
Andre Przywara4b9340a2017-04-26 01:32:33 +0100107 debug("%s: '%s'\n", type, str);
Philipp Tomsicha616c782017-09-13 21:29:34 +0200108
Andre Przywara4b9340a2017-04-26 01:32:33 +0100109 node = fdt_subnode_offset(fit, images, str);
110 if (node < 0) {
111 debug("cannot find image node '%s': %d\n", str, node);
112 return -EINVAL;
113 }
114
Andre Przywara736806f2017-04-26 01:32:34 +0100115 return node;
Andre Przywara4b9340a2017-04-26 01:32:33 +0100116}
117
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530118static int get_aligned_image_offset(struct spl_load_info *info, int offset)
119{
120 /*
121 * If it is a FS read, get the first address before offset which is
122 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
123 * block number to which offset belongs.
124 */
125 if (info->filename)
126 return offset & ~(ARCH_DMA_MINALIGN - 1);
127
128 return offset / info->bl_len;
129}
130
131static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
132{
133 /*
134 * If it is a FS read, get the difference between the offset and
135 * the first address before offset which is aligned to
136 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
137 * block.
138 */
139 if (info->filename)
140 return offset & (ARCH_DMA_MINALIGN - 1);
141
142 return offset % info->bl_len;
143}
144
145static int get_aligned_image_size(struct spl_load_info *info, int data_size,
146 int offset)
147{
Lokesh Vutla3cc1f382016-07-19 14:56:14 +0530148 data_size = data_size + get_aligned_image_overhead(info, offset);
149
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530150 if (info->filename)
Lokesh Vutla3cc1f382016-07-19 14:56:14 +0530151 return data_size;
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530152
153 return (data_size + info->bl_len - 1) / info->bl_len;
154}
155
Andre Przywara8baa3812017-04-26 01:32:36 +0100156/**
157 * spl_load_fit_image(): load the image described in a certain FIT node
158 * @info: points to information about the device to load data from
159 * @sector: the start sector of the FIT image on the device
160 * @fit: points to the flattened device tree blob describing the FIT
Philipp Tomsicha616c782017-09-13 21:29:34 +0200161 * image
Andre Przywara8baa3812017-04-26 01:32:36 +0100162 * @base_offset: the beginning of the data area containing the actual
163 * image data, relative to the beginning of the FIT
164 * @node: offset of the DT node describing the image to load (relative
Philipp Tomsicha616c782017-09-13 21:29:34 +0200165 * to @fit)
Andre Przywara8baa3812017-04-26 01:32:36 +0100166 * @image_info: will be filled with information about the loaded image
Philipp Tomsicha616c782017-09-13 21:29:34 +0200167 * If the FIT node does not contain a "load" (address) property,
168 * the image gets loaded to the address pointed to by the
169 * load_addr member in this struct.
Andre Przywara8baa3812017-04-26 01:32:36 +0100170 *
171 * Return: 0 on success or a negative error number.
172 */
173static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
174 void *fit, ulong base_offset, int node,
175 struct spl_image_info *image_info)
176{
Michal Simek3313ae62018-07-18 14:33:15 +0200177 int offset;
Andre Przywara8baa3812017-04-26 01:32:36 +0100178 size_t length;
York Sun5fd13d92017-08-15 11:14:44 -0700179 int len;
York Sun933f67a2017-09-15 08:21:13 -0700180 ulong size;
Andre Przywara8baa3812017-04-26 01:32:36 +0100181 ulong load_addr, load_ptr;
182 void *src;
183 ulong overhead;
184 int nr_sectors;
185 int align_len = ARCH_DMA_MINALIGN - 1;
York Sun7264f292017-08-15 11:14:43 -0700186 uint8_t image_comp = -1, type = -1;
York Sun5fd13d92017-08-15 11:14:44 -0700187 const void *data;
Peng Fana1be94b2017-12-05 13:20:59 +0800188 bool external_data = false;
York Sun7264f292017-08-15 11:14:43 -0700189
Marek Vasut56419ea2018-06-01 23:19:29 +0200190 if (IS_ENABLED(CONFIG_SPL_FPGA_SUPPORT) ||
191 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
192 if (fit_image_get_type(fit, node, &type))
193 puts("Cannot get image type.\n");
194 else
195 debug("%s ", genimg_get_type_name(type));
196 }
197
York Sun7264f292017-08-15 11:14:43 -0700198 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
199 if (fit_image_get_comp(fit, node, &image_comp))
200 puts("Cannot get image compression format.\n");
201 else
202 debug("%s ", genimg_get_comp_name(image_comp));
York Sun7264f292017-08-15 11:14:43 -0700203 }
Andre Przywara8baa3812017-04-26 01:32:36 +0100204
York Sun5fd13d92017-08-15 11:14:44 -0700205 if (fit_image_get_load(fit, node, &load_addr))
Andre Przywara8baa3812017-04-26 01:32:36 +0100206 load_addr = image_info->load_addr;
Andre Przywara8baa3812017-04-26 01:32:36 +0100207
Peng Fana1be94b2017-12-05 13:20:59 +0800208 if (!fit_image_get_data_position(fit, node, &offset)) {
209 external_data = true;
210 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
York Sun5fd13d92017-08-15 11:14:44 -0700211 offset += base_offset;
Peng Fana1be94b2017-12-05 13:20:59 +0800212 external_data = true;
213 }
214
215 if (external_data) {
216 /* External data */
York Sun5fd13d92017-08-15 11:14:44 -0700217 if (fit_image_get_data_size(fit, node, &len))
218 return -ENOENT;
Andre Przywara8baa3812017-04-26 01:32:36 +0100219
York Sun5fd13d92017-08-15 11:14:44 -0700220 load_ptr = (load_addr + align_len) & ~align_len;
221 length = len;
Andre Przywara8baa3812017-04-26 01:32:36 +0100222
York Sun5fd13d92017-08-15 11:14:44 -0700223 overhead = get_aligned_image_overhead(info, offset);
224 nr_sectors = get_aligned_image_size(info, length, offset);
225
Michal Simek3313ae62018-07-18 14:33:15 +0200226 if (info->read(info,
227 sector + get_aligned_image_offset(info, offset),
York Sun5fd13d92017-08-15 11:14:44 -0700228 nr_sectors, (void *)load_ptr) != nr_sectors)
229 return -EIO;
230
231 debug("External data: dst=%lx, offset=%x, size=%lx\n",
232 load_ptr, offset, (unsigned long)length);
233 src = (void *)load_ptr + overhead;
234 } else {
235 /* Embedded data */
236 if (fit_image_get_data(fit, node, &data, &length)) {
237 puts("Cannot get image data/size\n");
238 return -ENOENT;
239 }
240 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
241 (unsigned long)length);
242 src = (void *)data;
243 }
244
Ben Whittend154ca62018-06-07 11:37:27 +0100245#ifdef CONFIG_SPL_FIT_SIGNATURE
246 printf("## Checking hash(es) for Image %s ... ",
247 fit_get_name(fit, node, NULL));
248 if (!fit_image_verify_with_data(fit, node,
249 src, length))
250 return -EPERM;
251 puts("OK\n");
252#endif
253
Andre Przywara8baa3812017-04-26 01:32:36 +0100254#ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
255 board_fit_image_post_process(&src, &length);
256#endif
257
Michal Simek975e7892018-07-24 15:05:00 +0200258 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
York Sun933f67a2017-09-15 08:21:13 -0700259 size = length;
York Sun7264f292017-08-15 11:14:43 -0700260 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
York Sun933f67a2017-09-15 08:21:13 -0700261 src, &size)) {
York Sun7264f292017-08-15 11:14:43 -0700262 puts("Uncompressing error\n");
263 return -EIO;
264 }
York Sun933f67a2017-09-15 08:21:13 -0700265 length = size;
York Sun7264f292017-08-15 11:14:43 -0700266 } else {
267 memcpy((void *)load_addr, src, length);
268 }
Andre Przywara8baa3812017-04-26 01:32:36 +0100269
270 if (image_info) {
271 image_info->load_addr = load_addr;
272 image_info->size = length;
273 image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
274 }
275
276 return 0;
277}
278
Philipp Tomsichd8796162017-09-13 21:29:32 +0200279static int spl_fit_append_fdt(struct spl_image_info *spl_image,
280 struct spl_load_info *info, ulong sector,
281 void *fit, int images, ulong base_offset)
282{
283 struct spl_image_info image_info;
Michal Simek9d13b872019-10-22 16:39:11 +0200284 int node, ret = 0, index = 0;
Lukas Auerb83edfb2019-08-21 21:14:42 +0200285
286 /*
287 * Use the address following the image as target address for the
288 * device tree.
289 */
290 image_info.load_addr = spl_image->load_addr + spl_image->size;
Philipp Tomsichd8796162017-09-13 21:29:32 +0200291
292 /* Figure out which device tree the board wants to use */
Michal Simek9d13b872019-10-22 16:39:11 +0200293 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, index++);
Philipp Tomsichd8796162017-09-13 21:29:32 +0200294 if (node < 0) {
295 debug("%s: cannot find FDT node\n", __func__);
Lukas Auerb83edfb2019-08-21 21:14:42 +0200296
297 /*
298 * U-Boot did not find a device tree inside the FIT image. Use
299 * the U-Boot device tree instead.
300 */
301 if (gd->fdt_blob)
302 memcpy((void *)image_info.load_addr, gd->fdt_blob,
303 fdt_totalsize(gd->fdt_blob));
304 else
305 return node;
306 } else {
307 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
308 &image_info);
309 if (ret < 0)
310 return ret;
Philipp Tomsichd8796162017-09-13 21:29:32 +0200311 }
312
Philipp Tomsicha616c782017-09-13 21:29:34 +0200313 /* Make the load-address of the FDT available for the SPL framework */
314 spl_image->fdt_addr = (void *)image_info.load_addr;
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100315#if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
Michal Simek9d13b872019-10-22 16:39:11 +0200316 if (CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)) {
317 for (; ; index++) {
318 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP,
319 index);
320 if (node < 0) {
321 debug("%s: No additional FDT node\n", __func__);
322 return 0;
323 }
Philipp Tomsicha616c782017-09-13 21:29:34 +0200324
Michal Simek9d13b872019-10-22 16:39:11 +0200325 ret = spl_load_fit_image(info, sector, fit, base_offset,
326 node, &image_info);
327 if (ret < 0)
328 return ret;
329
Jean-Jacques Hiblot99329be2019-10-22 16:39:12 +0200330 /* Make room in FDT for changes from the overlay */
331 ret = fdt_increase_size(spl_image->fdt_addr,
332 image_info.size);
333 if (ret < 0)
334 return ret;
335
Michal Simek9d13b872019-10-22 16:39:11 +0200336 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
337 (void *)image_info.load_addr);
338 if (ret)
339 return ret;
340
341 debug("%s: DT overlay %s applied\n", __func__,
342 fit_get_name(fit, node, NULL));
343 }
344 }
Jean-Jacques Hiblot99329be2019-10-22 16:39:12 +0200345 /* Try to make space, so we can inject details on the loadables */
346 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
347 if (ret < 0)
348 return ret;
349#endif
350
Philipp Tomsicha616c782017-09-13 21:29:34 +0200351 return ret;
352}
353
354static int spl_fit_record_loadable(const void *fit, int images, int index,
355 void *blob, struct spl_image_info *image)
356{
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100357 int ret = 0;
358#if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
Philipp Tomsicha616c782017-09-13 21:29:34 +0200359 char *name;
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100360 int node;
Philipp Tomsicha616c782017-09-13 21:29:34 +0200361
362 ret = spl_fit_get_image_name(fit, images, "loadables",
363 index, &name);
364 if (ret < 0)
365 return ret;
366
367 node = spl_fit_get_image_node(fit, images, "loadables", index);
368
369 ret = fdt_record_loadable(blob, index, name, image->load_addr,
370 image->size, image->entry_point,
371 fdt_getprop(fit, node, "type", NULL),
372 fdt_getprop(fit, node, "os", NULL));
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100373#endif
Philipp Tomsichd8796162017-09-13 21:29:32 +0200374 return ret;
375}
376
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100377static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
378{
Jean-Jacques Hiblot7296a332019-04-26 15:21:25 +0200379#if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) && !defined(CONFIG_SPL_OS_BOOT)
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100380 return -ENOTSUPP;
381#else
382 return fit_image_get_os(fit, noffset, os);
383#endif
384}
385
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500386/*
387 * Weak default function to allow customizing SPL fit loading for load-only
388 * use cases by allowing to skip the parsing/processing of the FIT contents
389 * (so that this can be done separately in a more customized fashion)
390 */
391__weak bool spl_load_simple_fit_skip_processing(void)
392{
393 return false;
394}
395
Simon Glassf4d7d852016-09-24 18:20:16 -0600396int spl_load_simple_fit(struct spl_image_info *spl_image,
397 struct spl_load_info *info, ulong sector, void *fit)
Simon Glassf1dcee52016-02-22 22:55:56 -0700398{
399 int sectors;
Andre Przywara8baa3812017-04-26 01:32:36 +0100400 ulong size;
Simon Glassf1dcee52016-02-22 22:55:56 -0700401 unsigned long count;
Andre Przywara8baa3812017-04-26 01:32:36 +0100402 struct spl_image_info image_info;
York Sunc8bc3c02017-08-15 11:14:45 -0700403 int node = -1;
404 int images, ret;
Marek Vasut04ce5422018-08-14 11:27:02 +0200405 int base_offset, hsize, align_len = ARCH_DMA_MINALIGN - 1;
Andre Przywara411cf322017-04-26 01:32:37 +0100406 int index = 0;
Jean-Jacques Hiblot6b8b98d2019-10-22 16:39:10 +0200407 int firmware_node;
Simon Glassf1dcee52016-02-22 22:55:56 -0700408
409 /*
York Sunc8bc3c02017-08-15 11:14:45 -0700410 * For FIT with external data, figure out where the external images
411 * start. This is the base for the data-offset properties in each
412 * image.
Simon Glassf1dcee52016-02-22 22:55:56 -0700413 */
414 size = fdt_totalsize(fit);
415 size = (size + 3) & ~3;
Ye Lie246bfc2018-11-17 09:10:25 +0000416 size = board_spl_fit_size_align(size);
Simon Glassf1dcee52016-02-22 22:55:56 -0700417 base_offset = (size + 3) & ~3;
418
419 /*
420 * So far we only have one block of data from the FIT. Read the entire
421 * thing, including that first block, placing it so it finishes before
422 * where we will load the image.
423 *
424 * Note that we will load the image such that its first byte will be
425 * at the load address. Since that byte may be part-way through a
426 * block, we may load the image up to one block before the load
427 * address. So take account of that here by subtracting an addition
428 * block length from the FIT start position.
429 *
430 * In fact the FIT has its own load address, but we assume it cannot
431 * be before CONFIG_SYS_TEXT_BASE.
York Sunc8bc3c02017-08-15 11:14:45 -0700432 *
433 * For FIT with data embedded, data is loaded as part of FIT image.
434 * For FIT with external data, data is not loaded in this step.
Simon Glassf1dcee52016-02-22 22:55:56 -0700435 */
Marek Vasut04ce5422018-08-14 11:27:02 +0200436 hsize = (size + info->bl_len + align_len) & ~align_len;
437 fit = spl_get_load_buffer(-hsize, hsize);
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530438 sectors = get_aligned_image_size(info, size, 0);
Simon Glassf1dcee52016-02-22 22:55:56 -0700439 count = info->read(info, sector, sectors, fit);
Ye Lie246bfc2018-11-17 09:10:25 +0000440 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
441 sector, sectors, fit, count, size);
442
Simon Glassf1dcee52016-02-22 22:55:56 -0700443 if (count == 0)
444 return -EIO;
445
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500446 /* skip further processing if requested to enable load-only use cases */
447 if (spl_load_simple_fit_skip_processing())
448 return 0;
449
Andre Przywara736806f2017-04-26 01:32:34 +0100450 /* find the node holding the images information */
Simon Glassf1dcee52016-02-22 22:55:56 -0700451 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
452 if (images < 0) {
453 debug("%s: Cannot find /images node: %d\n", __func__, images);
454 return -1;
455 }
Andre Przywara736806f2017-04-26 01:32:34 +0100456
Marek Vasut26a64222018-05-12 22:25:28 +0200457#ifdef CONFIG_SPL_FPGA_SUPPORT
458 node = spl_fit_get_image_node(fit, images, "fpga", 0);
459 if (node >= 0) {
460 /* Load the image and set up the spl_image structure */
461 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
462 spl_image);
463 if (ret) {
464 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
465 return ret;
466 }
Michal Simek3313ae62018-07-18 14:33:15 +0200467
468 debug("FPGA bitstream at: %x, size: %x\n",
469 (u32)spl_image->load_addr, spl_image->size);
470
471 ret = fpga_load(0, (const void *)spl_image->load_addr,
472 spl_image->size, BIT_FULL);
473 if (ret) {
474 printf("%s: Cannot load the image to the FPGA\n",
475 __func__);
476 return ret;
477 }
478
Luis Araneda3907eef2018-07-19 03:10:16 -0400479 puts("FPGA image loaded from FIT\n");
Marek Vasut26a64222018-05-12 22:25:28 +0200480 node = -1;
481 }
482#endif
483
Philipp Tomsichd8796162017-09-13 21:29:32 +0200484 /*
485 * Find the U-Boot image using the following search order:
486 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
487 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
488 * - fall back to using the first 'loadables' entry
489 */
York Sunc8bc3c02017-08-15 11:14:45 -0700490 if (node < 0)
Michal Simek1f8e4bf2018-03-26 16:31:26 +0200491 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
492 0);
Philipp Tomsichd8796162017-09-13 21:29:32 +0200493#ifdef CONFIG_SPL_OS_BOOT
494 if (node < 0)
495 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
496#endif
Simon Glassf1dcee52016-02-22 22:55:56 -0700497 if (node < 0) {
Andre Przywara736806f2017-04-26 01:32:34 +0100498 debug("could not find firmware image, trying loadables...\n");
499 node = spl_fit_get_image_node(fit, images, "loadables", 0);
Andre Przywara411cf322017-04-26 01:32:37 +0100500 /*
501 * If we pick the U-Boot image from "loadables", start at
502 * the second image when later loading additional images.
503 */
504 index = 1;
Andre Przywara736806f2017-04-26 01:32:34 +0100505 }
506 if (node < 0) {
507 debug("%s: Cannot find u-boot image node: %d\n",
508 __func__, node);
Simon Glassf1dcee52016-02-22 22:55:56 -0700509 return -1;
510 }
511
Andre Przywara8baa3812017-04-26 01:32:36 +0100512 /* Load the image and set up the spl_image structure */
513 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
514 spl_image);
515 if (ret)
516 return ret;
517
Philipp Tomsichd8796162017-09-13 21:29:32 +0200518 /*
519 * For backward compatibility, we treat the first node that is
520 * as a U-Boot image, if no OS-type has been declared.
521 */
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100522 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
York Sunc8bc3c02017-08-15 11:14:45 -0700523 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
Philipp Tomsichd8796162017-09-13 21:29:32 +0200524#if !defined(CONFIG_SPL_OS_BOOT)
525 else
526 spl_image->os = IH_OS_U_BOOT;
York Sunc8bc3c02017-08-15 11:14:45 -0700527#endif
Simon Glassf1dcee52016-02-22 22:55:56 -0700528
Philipp Tomsichd8796162017-09-13 21:29:32 +0200529 /*
530 * Booting a next-stage U-Boot may require us to append the FDT.
531 * We allow this to fail, as the U-Boot image might embed its FDT.
532 */
533 if (spl_image->os == IH_OS_U_BOOT)
534 spl_fit_append_fdt(spl_image, info, sector, fit,
535 images, base_offset);
Simon Glassf1dcee52016-02-22 22:55:56 -0700536
Jean-Jacques Hiblot6b8b98d2019-10-22 16:39:10 +0200537 firmware_node = node;
Andre Przywara411cf322017-04-26 01:32:37 +0100538 /* Now check if there are more images for us to load */
539 for (; ; index++) {
Philipp Tomsichd8796162017-09-13 21:29:32 +0200540 uint8_t os_type = IH_OS_INVALID;
541
Andre Przywara411cf322017-04-26 01:32:37 +0100542 node = spl_fit_get_image_node(fit, images, "loadables", index);
543 if (node < 0)
544 break;
545
Jean-Jacques Hiblot6b8b98d2019-10-22 16:39:10 +0200546 /*
547 * if the firmware is also a loadable, skip it because
548 * it already has been loaded. This is typically the case with
549 * u-boot.img generated by mkimage.
550 */
551 if (firmware_node == node)
552 continue;
553
Andre Przywara411cf322017-04-26 01:32:37 +0100554 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
555 &image_info);
556 if (ret < 0)
557 continue;
558
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100559 if (!spl_fit_image_get_os(fit, node, &os_type))
Philipp Tomsichd8796162017-09-13 21:29:32 +0200560 debug("Loadable is %s\n", genimg_get_os_name(os_type));
Abel Vesacf8dcc52019-03-12 08:34:31 +0000561#if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
562 else
563 os_type = IH_OS_U_BOOT;
564#endif
Philipp Tomsichd8796162017-09-13 21:29:32 +0200565
Philipp Tomsicha616c782017-09-13 21:29:34 +0200566 if (os_type == IH_OS_U_BOOT) {
567 spl_fit_append_fdt(&image_info, info, sector,
Philipp Tomsichd8796162017-09-13 21:29:32 +0200568 fit, images, base_offset);
Philipp Tomsicha616c782017-09-13 21:29:34 +0200569 spl_image->fdt_addr = image_info.fdt_addr;
570 }
Philipp Tomsichd8796162017-09-13 21:29:32 +0200571
Andre Przywara411cf322017-04-26 01:32:37 +0100572 /*
573 * If the "firmware" image did not provide an entry point,
574 * use the first valid entry point from the loadables.
575 */
576 if (spl_image->entry_point == FDT_ERROR &&
577 image_info.entry_point != FDT_ERROR)
578 spl_image->entry_point = image_info.entry_point;
Philipp Tomsicha616c782017-09-13 21:29:34 +0200579
580 /* Record our loadables into the FDT */
581 if (spl_image->fdt_addr)
582 spl_fit_record_loadable(fit, images, index,
583 spl_image->fdt_addr,
584 &image_info);
Andre Przywara411cf322017-04-26 01:32:37 +0100585 }
586
587 /*
588 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
589 * Makefile will set it to 0 and it will end up as the entry point
590 * here. What it actually means is: use the load address.
591 */
592 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
593 spl_image->entry_point = spl_image->load_addr;
594
Ye Lie246bfc2018-11-17 09:10:25 +0000595 spl_image->flags |= SPL_FIT_FOUND;
596
Stefano Babicd714a752019-09-20 08:47:53 +0200597#ifdef CONFIG_IMX_HAB
Ye Lie246bfc2018-11-17 09:10:25 +0000598 board_spl_fit_post_load((ulong)fit, size);
599#endif
600
Andre Przywara411cf322017-04-26 01:32:37 +0100601 return 0;
Simon Glassf1dcee52016-02-22 22:55:56 -0700602}