blob: 9eabb1c1058b3df670bfaf9925b14e15a19623c3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassa6131a22016-02-22 22:55:56 -07002/*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassa6131a22016-02-22 22:55:56 -07005 */
6
7#include <common.h>
8#include <errno.h>
9#include <image.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Simon Glassa6131a22016-02-22 22:55:56 -070011#include <spl.h>
12
York Suna6945fe2017-08-15 11:14:43 -070013#ifndef CONFIG_SYS_BOOTM_LEN
14#define CONFIG_SYS_BOOTM_LEN (64 << 20)
15#endif
16
Andre Przywara525e9c92017-04-26 01:32:34 +010017/**
Philipp Tomsiche61eff92017-09-13 21:29:34 +020018 * spl_fit_get_image_name(): By using the matching configuration subnode,
Andre Przywara525e9c92017-04-26 01:32:34 +010019 * retrieve the name of an image, specified by a property name and an index
20 * into that.
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.
Philipp Tomsiche61eff92017-09-13 21:29:34 +020025 * @outname: Name of the image
Andre Przywara525e9c92017-04-26 01:32:34 +010026 *
Philipp Tomsiche61eff92017-09-13 21:29:34 +020027 * Return: 0 on success, or a negative error number
Andre Przywara525e9c92017-04-26 01:32:34 +010028 */
Philipp Tomsiche61eff92017-09-13 21:29:34 +020029static int spl_fit_get_image_name(const void *fit, int images,
30 const char *type, int index,
31 char **outname)
Andre Przywara1e329b62017-04-26 01:32:33 +010032{
33 const char *name, *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +020034 __maybe_unused int node;
35 int conf_node;
Andre Przywara1e329b62017-04-26 01:32:33 +010036 int len, i;
37
Cooper Jr., Frankline6792402017-06-16 17:25:05 -050038 conf_node = fit_find_config_node(fit);
Andre Przywara1e329b62017-04-26 01:32:33 +010039 if (conf_node < 0) {
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);
43 node >= 0;
44 node = fdt_next_subnode(fit, node)) {
45 name = fdt_getprop(fit, node, "description", &len);
46 printf(" %s\n", name);
Simon Glassa6131a22016-02-22 22:55:56 -070047 }
Andre Przywara1e329b62017-04-26 01:32:33 +010048#endif
49 return conf_node;
50 }
Simon Glassa6131a22016-02-22 22:55:56 -070051
Andre Przywara1e329b62017-04-26 01:32:33 +010052 name = fdt_getprop(fit, conf_node, type, &len);
53 if (!name) {
54 debug("cannot find property '%s': %d\n", type, len);
55 return -EINVAL;
56 }
Simon Glassa6131a22016-02-22 22:55:56 -070057
Andre Przywara1e329b62017-04-26 01:32:33 +010058 str = name;
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);
63 return -E2BIG;
64 }
Simon Glassa6131a22016-02-22 22:55:56 -070065 }
66
Philipp Tomsiche61eff92017-09-13 21:29:34 +020067 *outname = (char *)str;
68 return 0;
69}
70
71/**
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
74 * into that.
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.
79 *
80 * Return: the node offset of the respective image node or a negative
81 * error number.
82 */
83static int spl_fit_get_image_node(const void *fit, int images,
84 const char *type, int index)
85{
86 char *str;
87 int err;
88 int node;
89
90 err = spl_fit_get_image_name(fit, images, type, index, &str);
91 if (err)
92 return err;
93
Andre Przywara1e329b62017-04-26 01:32:33 +010094 debug("%s: '%s'\n", type, str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +020095
Andre Przywara1e329b62017-04-26 01:32:33 +010096 node = fdt_subnode_offset(fit, images, str);
97 if (node < 0) {
98 debug("cannot find image node '%s': %d\n", str, node);
99 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700100 }
Simon Glassa6131a22016-02-22 22:55:56 -0700101
Andre Przywara525e9c92017-04-26 01:32:34 +0100102 return node;
Simon Glassa6131a22016-02-22 22:55:56 -0700103}
104
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530105static int get_aligned_image_offset(struct spl_load_info *info, int offset)
106{
107 /*
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.
111 */
112 if (info->filename)
113 return offset & ~(ARCH_DMA_MINALIGN - 1);
114
115 return offset / info->bl_len;
116}
117
118static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
119{
120 /*
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
124 * block.
125 */
126 if (info->filename)
127 return offset & (ARCH_DMA_MINALIGN - 1);
128
129 return offset % info->bl_len;
130}
131
132static int get_aligned_image_size(struct spl_load_info *info, int data_size,
133 int offset)
134{
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530135 data_size = data_size + get_aligned_image_overhead(info, offset);
136
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530137 if (info->filename)
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530138 return data_size;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530139
140 return (data_size + info->bl_len - 1) / info->bl_len;
141}
142
Marek Vasut33dd00e2018-05-12 22:25:28 +0200143#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)
146{
147 return 0;
148}
149#endif
150
Andre Przywara29053e92017-04-26 01:32:36 +0100151/**
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
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200156 * image
Andre Przywara29053e92017-04-26 01:32:36 +0100157 * @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
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200160 * to @fit)
Andre Przywara29053e92017-04-26 01:32:36 +0100161 * @image_info: will be filled with information about the loaded image
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200162 * 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.
Andre Przywara29053e92017-04-26 01:32:36 +0100165 *
166 * Return: 0 on success or a negative error number.
167 */
168static 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)
171{
Marek Vasut33dd00e2018-05-12 22:25:28 +0200172 int offset, sector_offset;
Andre Przywara29053e92017-04-26 01:32:36 +0100173 size_t length;
York Sunadf99fa2017-08-15 11:14:44 -0700174 int len;
York Sun25d65f12017-09-15 08:21:13 -0700175 ulong size;
Andre Przywara29053e92017-04-26 01:32:36 +0100176 ulong load_addr, load_ptr;
177 void *src;
178 ulong overhead;
179 int nr_sectors;
180 int align_len = ARCH_DMA_MINALIGN - 1;
York Suna6945fe2017-08-15 11:14:43 -0700181 uint8_t image_comp = -1, type = -1;
York Sunadf99fa2017-08-15 11:14:44 -0700182 const void *data;
Peng Fan8876c7e2017-12-05 13:20:59 +0800183 bool external_data = false;
York Suna6945fe2017-08-15 11:14:43 -0700184
Marek Vasut72bc5d52018-06-01 23:19:29 +0200185 if (IS_ENABLED(CONFIG_SPL_FPGA_SUPPORT) ||
186 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
187 if (fit_image_get_type(fit, node, &type))
188 puts("Cannot get image type.\n");
189 else
190 debug("%s ", genimg_get_type_name(type));
191 }
192
York Suna6945fe2017-08-15 11:14:43 -0700193 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
194 if (fit_image_get_comp(fit, node, &image_comp))
195 puts("Cannot get image compression format.\n");
196 else
197 debug("%s ", genimg_get_comp_name(image_comp));
York Suna6945fe2017-08-15 11:14:43 -0700198 }
Andre Przywara29053e92017-04-26 01:32:36 +0100199
York Sunadf99fa2017-08-15 11:14:44 -0700200 if (fit_image_get_load(fit, node, &load_addr))
Andre Przywara29053e92017-04-26 01:32:36 +0100201 load_addr = image_info->load_addr;
Andre Przywara29053e92017-04-26 01:32:36 +0100202
Peng Fan8876c7e2017-12-05 13:20:59 +0800203 if (!fit_image_get_data_position(fit, node, &offset)) {
204 external_data = true;
205 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
York Sunadf99fa2017-08-15 11:14:44 -0700206 offset += base_offset;
Peng Fan8876c7e2017-12-05 13:20:59 +0800207 external_data = true;
208 }
209
210 if (external_data) {
211 /* External data */
York Sunadf99fa2017-08-15 11:14:44 -0700212 if (fit_image_get_data_size(fit, node, &len))
213 return -ENOENT;
Andre Przywara29053e92017-04-26 01:32:36 +0100214
York Sunadf99fa2017-08-15 11:14:44 -0700215 load_ptr = (load_addr + align_len) & ~align_len;
216 length = len;
217
218 overhead = get_aligned_image_overhead(info, offset);
219 nr_sectors = get_aligned_image_size(info, length, offset);
Marek Vasut33dd00e2018-05-12 22:25:28 +0200220 sector_offset = sector + get_aligned_image_offset(info, offset);
221
222#ifdef CONFIG_SPL_FPGA_SUPPORT
223 if (type == IH_TYPE_FPGA) {
224 return spl_load_fpga_image(info, length, nr_sectors,
225 sector_offset);
226 }
227#endif
York Sunadf99fa2017-08-15 11:14:44 -0700228
Marek Vasut33dd00e2018-05-12 22:25:28 +0200229 if (info->read(info, sector_offset,
York Sunadf99fa2017-08-15 11:14:44 -0700230 nr_sectors, (void *)load_ptr) != nr_sectors)
231 return -EIO;
232
233 debug("External data: dst=%lx, offset=%x, size=%lx\n",
234 load_ptr, offset, (unsigned long)length);
235 src = (void *)load_ptr + overhead;
236 } else {
237 /* Embedded data */
238 if (fit_image_get_data(fit, node, &data, &length)) {
239 puts("Cannot get image data/size\n");
240 return -ENOENT;
241 }
242 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
243 (unsigned long)length);
244 src = (void *)data;
245 }
Andre Przywara29053e92017-04-26 01:32:36 +0100246
Ben Whitten54a91192018-06-07 11:37:27 +0100247#ifdef CONFIG_SPL_FIT_SIGNATURE
248 printf("## Checking hash(es) for Image %s ... ",
249 fit_get_name(fit, node, NULL));
250 if (!fit_image_verify_with_data(fit, node,
251 src, length))
252 return -EPERM;
253 puts("OK\n");
254#endif
255
Andre Przywara29053e92017-04-26 01:32:36 +0100256#ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
257 board_fit_image_post_process(&src, &length);
258#endif
259
York Suna6945fe2017-08-15 11:14:43 -0700260 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) &&
261 IS_ENABLED(CONFIG_SPL_GZIP) &&
262 image_comp == IH_COMP_GZIP &&
263 type == IH_TYPE_KERNEL) {
York Sun25d65f12017-09-15 08:21:13 -0700264 size = length;
York Suna6945fe2017-08-15 11:14:43 -0700265 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
York Sun25d65f12017-09-15 08:21:13 -0700266 src, &size)) {
York Suna6945fe2017-08-15 11:14:43 -0700267 puts("Uncompressing error\n");
268 return -EIO;
269 }
York Sun25d65f12017-09-15 08:21:13 -0700270 length = size;
York Suna6945fe2017-08-15 11:14:43 -0700271 } else {
272 memcpy((void *)load_addr, src, length);
273 }
Andre Przywara29053e92017-04-26 01:32:36 +0100274
275 if (image_info) {
276 image_info->load_addr = load_addr;
277 image_info->size = length;
278 image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
279 }
280
281 return 0;
282}
283
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200284static 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)
287{
288 struct spl_image_info image_info;
289 int node, ret;
290
291 /* Figure out which device tree the board wants to use */
292 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
293 if (node < 0) {
294 debug("%s: cannot find FDT node\n", __func__);
295 return node;
296 }
297
298 /*
299 * Read the device tree and place it after the image.
300 * Align the destination address to ARCH_DMA_MINALIGN.
301 */
302 image_info.load_addr = spl_image->load_addr + spl_image->size;
303 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
304 &image_info);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200305
306 if (ret < 0)
307 return ret;
308
309 /* Make the load-address of the FDT available for the SPL framework */
310 spl_image->fdt_addr = (void *)image_info.load_addr;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100311#if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200312 /* Try to make space, so we can inject details on the loadables */
313 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100314#endif
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200315
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200316 return ret;
317}
318
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200319static int spl_fit_record_loadable(const void *fit, int images, int index,
320 void *blob, struct spl_image_info *image)
321{
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100322 int ret = 0;
323#if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200324 char *name;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100325 int node;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200326
327 ret = spl_fit_get_image_name(fit, images, "loadables",
328 index, &name);
329 if (ret < 0)
330 return ret;
331
332 node = spl_fit_get_image_node(fit, images, "loadables", index);
333
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));
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100338#endif
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200339 return ret;
340}
341
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100342static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
343{
344#if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
345 return -ENOTSUPP;
346#else
347 return fit_image_get_os(fit, noffset, os);
348#endif
349}
350
Simon Glass43a734f2016-09-24 18:20:16 -0600351int spl_load_simple_fit(struct spl_image_info *spl_image,
352 struct spl_load_info *info, ulong sector, void *fit)
Simon Glassa6131a22016-02-22 22:55:56 -0700353{
354 int sectors;
Andre Przywara29053e92017-04-26 01:32:36 +0100355 ulong size;
Simon Glassa6131a22016-02-22 22:55:56 -0700356 unsigned long count;
Andre Przywara29053e92017-04-26 01:32:36 +0100357 struct spl_image_info image_info;
York Suna058b8a2017-08-15 11:14:45 -0700358 int node = -1;
359 int images, ret;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530360 int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
Andre Przywarae7c58562017-04-26 01:32:37 +0100361 int index = 0;
Simon Glassa6131a22016-02-22 22:55:56 -0700362
363 /*
York Suna058b8a2017-08-15 11:14:45 -0700364 * For FIT with external data, figure out where the external images
365 * start. This is the base for the data-offset properties in each
366 * image.
Simon Glassa6131a22016-02-22 22:55:56 -0700367 */
368 size = fdt_totalsize(fit);
369 size = (size + 3) & ~3;
370 base_offset = (size + 3) & ~3;
371
372 /*
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.
376 *
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.
382 *
383 * In fact the FIT has its own load address, but we assume it cannot
384 * be before CONFIG_SYS_TEXT_BASE.
York Suna058b8a2017-08-15 11:14:45 -0700385 *
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.
Simon Glassa6131a22016-02-22 22:55:56 -0700388 */
Lokesh Vutlac8405852016-06-01 10:28:31 +0530389 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
390 align_len) & ~align_len);
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530391 sectors = get_aligned_image_size(info, size, 0);
Simon Glassa6131a22016-02-22 22:55:56 -0700392 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);
395 if (count == 0)
396 return -EIO;
397
Andre Przywara525e9c92017-04-26 01:32:34 +0100398 /* find the node holding the images information */
Simon Glassa6131a22016-02-22 22:55:56 -0700399 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
400 if (images < 0) {
401 debug("%s: Cannot find /images node: %d\n", __func__, images);
402 return -1;
403 }
Marek Vasut33dd00e2018-05-12 22:25:28 +0200404
405#ifdef CONFIG_SPL_FPGA_SUPPORT
406 node = spl_fit_get_image_node(fit, images, "fpga", 0);
407 if (node >= 0) {
408 /* Load the image and set up the spl_image structure */
409 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
410 spl_image);
411 if (ret) {
412 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
413 return ret;
414 }
Luis Aranedaeece6232018-07-19 03:10:16 -0400415 puts("FPGA image loaded from FIT\n");
Marek Vasut33dd00e2018-05-12 22:25:28 +0200416 node = -1;
417 }
418#endif
Andre Przywara525e9c92017-04-26 01:32:34 +0100419
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200420 /*
421 * Find the U-Boot image using the following search order:
422 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
423 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
424 * - fall back to using the first 'loadables' entry
425 */
426 if (node < 0)
Michal Simekb766e8f2018-03-26 16:31:26 +0200427 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
428 0);
York Suna058b8a2017-08-15 11:14:45 -0700429#ifdef CONFIG_SPL_OS_BOOT
York Suna058b8a2017-08-15 11:14:45 -0700430 if (node < 0)
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200431 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
York Suna058b8a2017-08-15 11:14:45 -0700432#endif
Andre Przywara525e9c92017-04-26 01:32:34 +0100433 if (node < 0) {
434 debug("could not find firmware image, trying loadables...\n");
435 node = spl_fit_get_image_node(fit, images, "loadables", 0);
Andre Przywarae7c58562017-04-26 01:32:37 +0100436 /*
437 * If we pick the U-Boot image from "loadables", start at
438 * the second image when later loading additional images.
439 */
440 index = 1;
Andre Przywara525e9c92017-04-26 01:32:34 +0100441 }
Simon Glassa6131a22016-02-22 22:55:56 -0700442 if (node < 0) {
Andre Przywara525e9c92017-04-26 01:32:34 +0100443 debug("%s: Cannot find u-boot image node: %d\n",
444 __func__, node);
Simon Glassa6131a22016-02-22 22:55:56 -0700445 return -1;
446 }
447
Andre Przywara29053e92017-04-26 01:32:36 +0100448 /* Load the image and set up the spl_image structure */
449 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
450 spl_image);
451 if (ret)
452 return ret;
Daniel Allredcf839bd2016-06-27 09:19:21 -0500453
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200454 /*
455 * For backward compatibility, we treat the first node that is
456 * as a U-Boot image, if no OS-type has been declared.
457 */
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100458 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
York Suna058b8a2017-08-15 11:14:45 -0700459 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200460#if !defined(CONFIG_SPL_OS_BOOT)
461 else
462 spl_image->os = IH_OS_U_BOOT;
York Suna058b8a2017-08-15 11:14:45 -0700463#endif
Simon Glassa6131a22016-02-22 22:55:56 -0700464
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200465 /*
466 * Booting a next-stage U-Boot may require us to append the FDT.
467 * We allow this to fail, as the U-Boot image might embed its FDT.
468 */
469 if (spl_image->os == IH_OS_U_BOOT)
470 spl_fit_append_fdt(spl_image, info, sector, fit,
471 images, base_offset);
Andre Przywarae7c58562017-04-26 01:32:37 +0100472
473 /* Now check if there are more images for us to load */
474 for (; ; index++) {
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200475 uint8_t os_type = IH_OS_INVALID;
476
Andre Przywarae7c58562017-04-26 01:32:37 +0100477 node = spl_fit_get_image_node(fit, images, "loadables", index);
478 if (node < 0)
479 break;
480
481 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
482 &image_info);
483 if (ret < 0)
484 continue;
485
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100486 if (!spl_fit_image_get_os(fit, node, &os_type))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200487 debug("Loadable is %s\n", genimg_get_os_name(os_type));
488
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200489 if (os_type == IH_OS_U_BOOT) {
490 spl_fit_append_fdt(&image_info, info, sector,
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200491 fit, images, base_offset);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200492 spl_image->fdt_addr = image_info.fdt_addr;
493 }
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200494
Andre Przywarae7c58562017-04-26 01:32:37 +0100495 /*
496 * If the "firmware" image did not provide an entry point,
497 * use the first valid entry point from the loadables.
498 */
499 if (spl_image->entry_point == FDT_ERROR &&
500 image_info.entry_point != FDT_ERROR)
501 spl_image->entry_point = image_info.entry_point;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200502
503 /* Record our loadables into the FDT */
504 if (spl_image->fdt_addr)
505 spl_fit_record_loadable(fit, images, index,
506 spl_image->fdt_addr,
507 &image_info);
Andre Przywarae7c58562017-04-26 01:32:37 +0100508 }
509
510 /*
511 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
512 * Makefile will set it to 0 and it will end up as the entry point
513 * here. What it actually means is: use the load address.
514 */
515 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
516 spl_image->entry_point = spl_image->load_addr;
517
518 return 0;
Simon Glassa6131a22016-02-22 22:55:56 -0700519}