blob: 0245dcadb416e43bd2f5863adf318174f6fd3fd5 [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>
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +020012#include <malloc.h>
Simon Glassf1dcee52016-02-22 22:55:56 -070013#include <spl.h>
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +020014#include <linux/libfdt.h>
Simon Glassf1dcee52016-02-22 22:55:56 -070015
Lukas Auerb83edfb2019-08-21 21:14:42 +020016DECLARE_GLOBAL_DATA_PTR;
17
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +020018#ifndef CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ
19#define CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ (64 * 1024)
20#endif
21
York Sun7264f292017-08-15 11:14:43 -070022#ifndef CONFIG_SYS_BOOTM_LEN
23#define CONFIG_SYS_BOOTM_LEN (64 << 20)
24#endif
25
Ye Lie246bfc2018-11-17 09:10:25 +000026__weak void board_spl_fit_post_load(ulong load_addr, size_t length)
27{
28}
29
30__weak ulong board_spl_fit_size_align(ulong size)
31{
32 return size;
33}
34
Andre Przywara736806f2017-04-26 01:32:34 +010035/**
Philipp Tomsicha616c782017-09-13 21:29:34 +020036 * spl_fit_get_image_name(): By using the matching configuration subnode,
Andre Przywara736806f2017-04-26 01:32:34 +010037 * retrieve the name of an image, specified by a property name and an index
38 * into that.
39 * @fit: Pointer to the FDT blob.
40 * @images: Offset of the /images subnode.
41 * @type: Name of the property within the configuration subnode.
42 * @index: Index into the list of strings in this property.
Philipp Tomsicha616c782017-09-13 21:29:34 +020043 * @outname: Name of the image
Andre Przywara736806f2017-04-26 01:32:34 +010044 *
Philipp Tomsicha616c782017-09-13 21:29:34 +020045 * Return: 0 on success, or a negative error number
Andre Przywara736806f2017-04-26 01:32:34 +010046 */
Philipp Tomsicha616c782017-09-13 21:29:34 +020047static int spl_fit_get_image_name(const void *fit, int images,
48 const char *type, int index,
49 char **outname)
Andre Przywara4b9340a2017-04-26 01:32:33 +010050{
51 const char *name, *str;
Philipp Tomsicha616c782017-09-13 21:29:34 +020052 __maybe_unused int node;
53 int conf_node;
Andre Przywara4b9340a2017-04-26 01:32:33 +010054 int len, i;
55
Cooper Jr., Franklin3863f842017-06-16 17:25:05 -050056 conf_node = fit_find_config_node(fit);
Andre Przywara4b9340a2017-04-26 01:32:33 +010057 if (conf_node < 0) {
58#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
59 printf("No matching DT out of these options:\n");
60 for (node = fdt_first_subnode(fit, conf_node);
61 node >= 0;
62 node = fdt_next_subnode(fit, node)) {
63 name = fdt_getprop(fit, node, "description", &len);
64 printf(" %s\n", name);
65 }
66#endif
67 return conf_node;
68 }
69
70 name = fdt_getprop(fit, conf_node, type, &len);
71 if (!name) {
72 debug("cannot find property '%s': %d\n", type, len);
73 return -EINVAL;
74 }
75
76 str = name;
77 for (i = 0; i < index; i++) {
78 str = strchr(str, '\0') + 1;
79 if (!str || (str - name >= len)) {
80 debug("no string for index %d\n", index);
81 return -E2BIG;
82 }
83 }
84
Philipp Tomsicha616c782017-09-13 21:29:34 +020085 *outname = (char *)str;
86 return 0;
87}
88
89/**
90 * spl_fit_get_image_node(): By using the matching configuration subnode,
91 * retrieve the name of an image, specified by a property name and an index
92 * into that.
93 * @fit: Pointer to the FDT blob.
94 * @images: Offset of the /images subnode.
95 * @type: Name of the property within the configuration subnode.
96 * @index: Index into the list of strings in this property.
97 *
98 * Return: the node offset of the respective image node or a negative
99 * error number.
100 */
101static int spl_fit_get_image_node(const void *fit, int images,
102 const char *type, int index)
103{
104 char *str;
105 int err;
106 int node;
107
108 err = spl_fit_get_image_name(fit, images, type, index, &str);
109 if (err)
110 return err;
111
Andre Przywara4b9340a2017-04-26 01:32:33 +0100112 debug("%s: '%s'\n", type, str);
Philipp Tomsicha616c782017-09-13 21:29:34 +0200113
Andre Przywara4b9340a2017-04-26 01:32:33 +0100114 node = fdt_subnode_offset(fit, images, str);
115 if (node < 0) {
116 debug("cannot find image node '%s': %d\n", str, node);
117 return -EINVAL;
118 }
119
Andre Przywara736806f2017-04-26 01:32:34 +0100120 return node;
Andre Przywara4b9340a2017-04-26 01:32:33 +0100121}
122
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530123static int get_aligned_image_offset(struct spl_load_info *info, int offset)
124{
125 /*
126 * If it is a FS read, get the first address before offset which is
127 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
128 * block number to which offset belongs.
129 */
130 if (info->filename)
131 return offset & ~(ARCH_DMA_MINALIGN - 1);
132
133 return offset / info->bl_len;
134}
135
136static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
137{
138 /*
139 * If it is a FS read, get the difference between the offset and
140 * the first address before offset which is aligned to
141 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
142 * block.
143 */
144 if (info->filename)
145 return offset & (ARCH_DMA_MINALIGN - 1);
146
147 return offset % info->bl_len;
148}
149
150static int get_aligned_image_size(struct spl_load_info *info, int data_size,
151 int offset)
152{
Lokesh Vutla3cc1f382016-07-19 14:56:14 +0530153 data_size = data_size + get_aligned_image_overhead(info, offset);
154
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530155 if (info->filename)
Lokesh Vutla3cc1f382016-07-19 14:56:14 +0530156 return data_size;
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530157
158 return (data_size + info->bl_len - 1) / info->bl_len;
159}
160
Andre Przywara8baa3812017-04-26 01:32:36 +0100161/**
162 * spl_load_fit_image(): load the image described in a certain FIT node
163 * @info: points to information about the device to load data from
164 * @sector: the start sector of the FIT image on the device
165 * @fit: points to the flattened device tree blob describing the FIT
Philipp Tomsicha616c782017-09-13 21:29:34 +0200166 * image
Andre Przywara8baa3812017-04-26 01:32:36 +0100167 * @base_offset: the beginning of the data area containing the actual
168 * image data, relative to the beginning of the FIT
169 * @node: offset of the DT node describing the image to load (relative
Philipp Tomsicha616c782017-09-13 21:29:34 +0200170 * to @fit)
Andre Przywara8baa3812017-04-26 01:32:36 +0100171 * @image_info: will be filled with information about the loaded image
Philipp Tomsicha616c782017-09-13 21:29:34 +0200172 * If the FIT node does not contain a "load" (address) property,
173 * the image gets loaded to the address pointed to by the
174 * load_addr member in this struct.
Andre Przywara8baa3812017-04-26 01:32:36 +0100175 *
176 * Return: 0 on success or a negative error number.
177 */
178static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
179 void *fit, ulong base_offset, int node,
180 struct spl_image_info *image_info)
181{
Michal Simek3313ae62018-07-18 14:33:15 +0200182 int offset;
Andre Przywara8baa3812017-04-26 01:32:36 +0100183 size_t length;
York Sun5fd13d92017-08-15 11:14:44 -0700184 int len;
York Sun933f67a2017-09-15 08:21:13 -0700185 ulong size;
Andre Przywara8baa3812017-04-26 01:32:36 +0100186 ulong load_addr, load_ptr;
187 void *src;
188 ulong overhead;
189 int nr_sectors;
190 int align_len = ARCH_DMA_MINALIGN - 1;
York Sun7264f292017-08-15 11:14:43 -0700191 uint8_t image_comp = -1, type = -1;
York Sun5fd13d92017-08-15 11:14:44 -0700192 const void *data;
Peng Fana1be94b2017-12-05 13:20:59 +0800193 bool external_data = false;
York Sun7264f292017-08-15 11:14:43 -0700194
Marek Vasut56419ea2018-06-01 23:19:29 +0200195 if (IS_ENABLED(CONFIG_SPL_FPGA_SUPPORT) ||
196 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
197 if (fit_image_get_type(fit, node, &type))
198 puts("Cannot get image type.\n");
199 else
200 debug("%s ", genimg_get_type_name(type));
201 }
202
York Sun7264f292017-08-15 11:14:43 -0700203 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
204 if (fit_image_get_comp(fit, node, &image_comp))
205 puts("Cannot get image compression format.\n");
206 else
207 debug("%s ", genimg_get_comp_name(image_comp));
York Sun7264f292017-08-15 11:14:43 -0700208 }
Andre Przywara8baa3812017-04-26 01:32:36 +0100209
York Sun5fd13d92017-08-15 11:14:44 -0700210 if (fit_image_get_load(fit, node, &load_addr))
Andre Przywara8baa3812017-04-26 01:32:36 +0100211 load_addr = image_info->load_addr;
Andre Przywara8baa3812017-04-26 01:32:36 +0100212
Peng Fana1be94b2017-12-05 13:20:59 +0800213 if (!fit_image_get_data_position(fit, node, &offset)) {
214 external_data = true;
215 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
York Sun5fd13d92017-08-15 11:14:44 -0700216 offset += base_offset;
Peng Fana1be94b2017-12-05 13:20:59 +0800217 external_data = true;
218 }
219
220 if (external_data) {
221 /* External data */
York Sun5fd13d92017-08-15 11:14:44 -0700222 if (fit_image_get_data_size(fit, node, &len))
223 return -ENOENT;
Andre Przywara8baa3812017-04-26 01:32:36 +0100224
York Sun5fd13d92017-08-15 11:14:44 -0700225 load_ptr = (load_addr + align_len) & ~align_len;
226 length = len;
Andre Przywara8baa3812017-04-26 01:32:36 +0100227
York Sun5fd13d92017-08-15 11:14:44 -0700228 overhead = get_aligned_image_overhead(info, offset);
229 nr_sectors = get_aligned_image_size(info, length, offset);
230
Michal Simek3313ae62018-07-18 14:33:15 +0200231 if (info->read(info,
232 sector + get_aligned_image_offset(info, offset),
York Sun5fd13d92017-08-15 11:14:44 -0700233 nr_sectors, (void *)load_ptr) != nr_sectors)
234 return -EIO;
235
236 debug("External data: dst=%lx, offset=%x, size=%lx\n",
237 load_ptr, offset, (unsigned long)length);
238 src = (void *)load_ptr + overhead;
239 } else {
240 /* Embedded data */
241 if (fit_image_get_data(fit, node, &data, &length)) {
242 puts("Cannot get image data/size\n");
243 return -ENOENT;
244 }
245 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
246 (unsigned long)length);
247 src = (void *)data;
248 }
249
Ben Whittend154ca62018-06-07 11:37:27 +0100250#ifdef CONFIG_SPL_FIT_SIGNATURE
251 printf("## Checking hash(es) for Image %s ... ",
252 fit_get_name(fit, node, NULL));
253 if (!fit_image_verify_with_data(fit, node,
254 src, length))
255 return -EPERM;
256 puts("OK\n");
257#endif
258
Andre Przywara8baa3812017-04-26 01:32:36 +0100259#ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
260 board_fit_image_post_process(&src, &length);
261#endif
262
Michal Simek975e7892018-07-24 15:05:00 +0200263 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
York Sun933f67a2017-09-15 08:21:13 -0700264 size = length;
York Sun7264f292017-08-15 11:14:43 -0700265 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
York Sun933f67a2017-09-15 08:21:13 -0700266 src, &size)) {
York Sun7264f292017-08-15 11:14:43 -0700267 puts("Uncompressing error\n");
268 return -EIO;
269 }
York Sun933f67a2017-09-15 08:21:13 -0700270 length = size;
York Sun7264f292017-08-15 11:14:43 -0700271 } else {
272 memcpy((void *)load_addr, src, length);
273 }
Andre Przywara8baa3812017-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 Tomsichd8796162017-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;
Michal Simek9d13b872019-10-22 16:39:11 +0200289 int node, ret = 0, index = 0;
Lukas Auerb83edfb2019-08-21 21:14:42 +0200290
291 /*
292 * Use the address following the image as target address for the
293 * device tree.
294 */
295 image_info.load_addr = spl_image->load_addr + spl_image->size;
Philipp Tomsichd8796162017-09-13 21:29:32 +0200296
297 /* Figure out which device tree the board wants to use */
Michal Simek9d13b872019-10-22 16:39:11 +0200298 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, index++);
Philipp Tomsichd8796162017-09-13 21:29:32 +0200299 if (node < 0) {
300 debug("%s: cannot find FDT node\n", __func__);
Lukas Auerb83edfb2019-08-21 21:14:42 +0200301
302 /*
303 * U-Boot did not find a device tree inside the FIT image. Use
304 * the U-Boot device tree instead.
305 */
306 if (gd->fdt_blob)
307 memcpy((void *)image_info.load_addr, gd->fdt_blob,
308 fdt_totalsize(gd->fdt_blob));
309 else
310 return node;
311 } else {
312 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
313 &image_info);
314 if (ret < 0)
315 return ret;
Philipp Tomsichd8796162017-09-13 21:29:32 +0200316 }
317
Philipp Tomsicha616c782017-09-13 21:29:34 +0200318 /* Make the load-address of the FDT available for the SPL framework */
319 spl_image->fdt_addr = (void *)image_info.load_addr;
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100320#if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
Michal Simek9d13b872019-10-22 16:39:11 +0200321 if (CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)) {
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200322 void *tmpbuffer = NULL;
323
Michal Simek9d13b872019-10-22 16:39:11 +0200324 for (; ; index++) {
325 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP,
326 index);
Jean-Jacques Hiblot24bf44c2019-10-22 16:39:14 +0200327 if (node == -E2BIG) {
Michal Simek9d13b872019-10-22 16:39:11 +0200328 debug("%s: No additional FDT node\n", __func__);
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200329 break;
Jean-Jacques Hiblot24bf44c2019-10-22 16:39:14 +0200330 } else if (node < 0) {
331 debug("%s: unable to find FDT node %d\n",
332 __func__, index);
333 continue;
Michal Simek9d13b872019-10-22 16:39:11 +0200334 }
Philipp Tomsicha616c782017-09-13 21:29:34 +0200335
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200336 if (!tmpbuffer) {
337 /*
338 * allocate memory to store the DT overlay
339 * before it is applied. It may not be used
340 * depending on how the overlay is stored, so
341 * don't fail yet if the allocation failed.
342 */
343 tmpbuffer = malloc(CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ);
344 if (!tmpbuffer)
345 debug("%s: unable to allocate space for overlays\n",
346 __func__);
347 }
348 image_info.load_addr = (ulong)tmpbuffer;
Michal Simek9d13b872019-10-22 16:39:11 +0200349 ret = spl_load_fit_image(info, sector, fit, base_offset,
350 node, &image_info);
351 if (ret < 0)
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200352 break;
Michal Simek9d13b872019-10-22 16:39:11 +0200353
Jean-Jacques Hiblot99329be2019-10-22 16:39:12 +0200354 /* Make room in FDT for changes from the overlay */
355 ret = fdt_increase_size(spl_image->fdt_addr,
356 image_info.size);
357 if (ret < 0)
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200358 break;
Jean-Jacques Hiblot99329be2019-10-22 16:39:12 +0200359
Michal Simek9d13b872019-10-22 16:39:11 +0200360 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
361 (void *)image_info.load_addr);
362 if (ret)
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200363 break;
Michal Simek9d13b872019-10-22 16:39:11 +0200364
365 debug("%s: DT overlay %s applied\n", __func__,
366 fit_get_name(fit, node, NULL));
367 }
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200368 if (tmpbuffer)
369 free(tmpbuffer);
370 if (ret)
371 return ret;
Michal Simek9d13b872019-10-22 16:39:11 +0200372 }
Jean-Jacques Hiblot99329be2019-10-22 16:39:12 +0200373 /* Try to make space, so we can inject details on the loadables */
374 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
375 if (ret < 0)
376 return ret;
377#endif
378
Philipp Tomsicha616c782017-09-13 21:29:34 +0200379 return ret;
380}
381
382static int spl_fit_record_loadable(const void *fit, int images, int index,
383 void *blob, struct spl_image_info *image)
384{
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100385 int ret = 0;
386#if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
Philipp Tomsicha616c782017-09-13 21:29:34 +0200387 char *name;
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100388 int node;
Philipp Tomsicha616c782017-09-13 21:29:34 +0200389
390 ret = spl_fit_get_image_name(fit, images, "loadables",
391 index, &name);
392 if (ret < 0)
393 return ret;
394
395 node = spl_fit_get_image_node(fit, images, "loadables", index);
396
397 ret = fdt_record_loadable(blob, index, name, image->load_addr,
398 image->size, image->entry_point,
399 fdt_getprop(fit, node, "type", NULL),
400 fdt_getprop(fit, node, "os", NULL));
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100401#endif
Philipp Tomsichd8796162017-09-13 21:29:32 +0200402 return ret;
403}
404
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100405static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
406{
Jean-Jacques Hiblot7296a332019-04-26 15:21:25 +0200407#if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) && !defined(CONFIG_SPL_OS_BOOT)
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100408 return -ENOTSUPP;
409#else
410 return fit_image_get_os(fit, noffset, os);
411#endif
412}
413
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500414/*
415 * Weak default function to allow customizing SPL fit loading for load-only
416 * use cases by allowing to skip the parsing/processing of the FIT contents
417 * (so that this can be done separately in a more customized fashion)
418 */
419__weak bool spl_load_simple_fit_skip_processing(void)
420{
421 return false;
422}
423
Simon Glassf4d7d852016-09-24 18:20:16 -0600424int spl_load_simple_fit(struct spl_image_info *spl_image,
425 struct spl_load_info *info, ulong sector, void *fit)
Simon Glassf1dcee52016-02-22 22:55:56 -0700426{
427 int sectors;
Andre Przywara8baa3812017-04-26 01:32:36 +0100428 ulong size;
Simon Glassf1dcee52016-02-22 22:55:56 -0700429 unsigned long count;
Andre Przywara8baa3812017-04-26 01:32:36 +0100430 struct spl_image_info image_info;
York Sunc8bc3c02017-08-15 11:14:45 -0700431 int node = -1;
432 int images, ret;
Marek Vasut04ce5422018-08-14 11:27:02 +0200433 int base_offset, hsize, align_len = ARCH_DMA_MINALIGN - 1;
Andre Przywara411cf322017-04-26 01:32:37 +0100434 int index = 0;
Jean-Jacques Hiblot6b8b98d2019-10-22 16:39:10 +0200435 int firmware_node;
Simon Glassf1dcee52016-02-22 22:55:56 -0700436
437 /*
York Sunc8bc3c02017-08-15 11:14:45 -0700438 * For FIT with external data, figure out where the external images
439 * start. This is the base for the data-offset properties in each
440 * image.
Simon Glassf1dcee52016-02-22 22:55:56 -0700441 */
442 size = fdt_totalsize(fit);
443 size = (size + 3) & ~3;
Ye Lie246bfc2018-11-17 09:10:25 +0000444 size = board_spl_fit_size_align(size);
Simon Glassf1dcee52016-02-22 22:55:56 -0700445 base_offset = (size + 3) & ~3;
446
447 /*
448 * So far we only have one block of data from the FIT. Read the entire
449 * thing, including that first block, placing it so it finishes before
450 * where we will load the image.
451 *
452 * Note that we will load the image such that its first byte will be
453 * at the load address. Since that byte may be part-way through a
454 * block, we may load the image up to one block before the load
455 * address. So take account of that here by subtracting an addition
456 * block length from the FIT start position.
457 *
458 * In fact the FIT has its own load address, but we assume it cannot
459 * be before CONFIG_SYS_TEXT_BASE.
York Sunc8bc3c02017-08-15 11:14:45 -0700460 *
461 * For FIT with data embedded, data is loaded as part of FIT image.
462 * For FIT with external data, data is not loaded in this step.
Simon Glassf1dcee52016-02-22 22:55:56 -0700463 */
Marek Vasut04ce5422018-08-14 11:27:02 +0200464 hsize = (size + info->bl_len + align_len) & ~align_len;
465 fit = spl_get_load_buffer(-hsize, hsize);
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530466 sectors = get_aligned_image_size(info, size, 0);
Simon Glassf1dcee52016-02-22 22:55:56 -0700467 count = info->read(info, sector, sectors, fit);
Ye Lie246bfc2018-11-17 09:10:25 +0000468 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
469 sector, sectors, fit, count, size);
470
Simon Glassf1dcee52016-02-22 22:55:56 -0700471 if (count == 0)
472 return -EIO;
473
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500474 /* skip further processing if requested to enable load-only use cases */
475 if (spl_load_simple_fit_skip_processing())
476 return 0;
477
Andre Przywara736806f2017-04-26 01:32:34 +0100478 /* find the node holding the images information */
Simon Glassf1dcee52016-02-22 22:55:56 -0700479 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
480 if (images < 0) {
481 debug("%s: Cannot find /images node: %d\n", __func__, images);
482 return -1;
483 }
Andre Przywara736806f2017-04-26 01:32:34 +0100484
Marek Vasut26a64222018-05-12 22:25:28 +0200485#ifdef CONFIG_SPL_FPGA_SUPPORT
486 node = spl_fit_get_image_node(fit, images, "fpga", 0);
487 if (node >= 0) {
488 /* Load the image and set up the spl_image structure */
489 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
490 spl_image);
491 if (ret) {
492 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
493 return ret;
494 }
Michal Simek3313ae62018-07-18 14:33:15 +0200495
496 debug("FPGA bitstream at: %x, size: %x\n",
497 (u32)spl_image->load_addr, spl_image->size);
498
499 ret = fpga_load(0, (const void *)spl_image->load_addr,
500 spl_image->size, BIT_FULL);
501 if (ret) {
502 printf("%s: Cannot load the image to the FPGA\n",
503 __func__);
504 return ret;
505 }
506
Luis Araneda3907eef2018-07-19 03:10:16 -0400507 puts("FPGA image loaded from FIT\n");
Marek Vasut26a64222018-05-12 22:25:28 +0200508 node = -1;
509 }
510#endif
511
Philipp Tomsichd8796162017-09-13 21:29:32 +0200512 /*
513 * Find the U-Boot image using the following search order:
514 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
515 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
516 * - fall back to using the first 'loadables' entry
517 */
York Sunc8bc3c02017-08-15 11:14:45 -0700518 if (node < 0)
Michal Simek1f8e4bf2018-03-26 16:31:26 +0200519 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
520 0);
Philipp Tomsichd8796162017-09-13 21:29:32 +0200521#ifdef CONFIG_SPL_OS_BOOT
522 if (node < 0)
523 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
524#endif
Simon Glassf1dcee52016-02-22 22:55:56 -0700525 if (node < 0) {
Andre Przywara736806f2017-04-26 01:32:34 +0100526 debug("could not find firmware image, trying loadables...\n");
527 node = spl_fit_get_image_node(fit, images, "loadables", 0);
Andre Przywara411cf322017-04-26 01:32:37 +0100528 /*
529 * If we pick the U-Boot image from "loadables", start at
530 * the second image when later loading additional images.
531 */
532 index = 1;
Andre Przywara736806f2017-04-26 01:32:34 +0100533 }
534 if (node < 0) {
535 debug("%s: Cannot find u-boot image node: %d\n",
536 __func__, node);
Simon Glassf1dcee52016-02-22 22:55:56 -0700537 return -1;
538 }
539
Andre Przywara8baa3812017-04-26 01:32:36 +0100540 /* Load the image and set up the spl_image structure */
541 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
542 spl_image);
543 if (ret)
544 return ret;
545
Philipp Tomsichd8796162017-09-13 21:29:32 +0200546 /*
547 * For backward compatibility, we treat the first node that is
548 * as a U-Boot image, if no OS-type has been declared.
549 */
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100550 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
York Sunc8bc3c02017-08-15 11:14:45 -0700551 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
Philipp Tomsichd8796162017-09-13 21:29:32 +0200552#if !defined(CONFIG_SPL_OS_BOOT)
553 else
554 spl_image->os = IH_OS_U_BOOT;
York Sunc8bc3c02017-08-15 11:14:45 -0700555#endif
Simon Glassf1dcee52016-02-22 22:55:56 -0700556
Philipp Tomsichd8796162017-09-13 21:29:32 +0200557 /*
558 * Booting a next-stage U-Boot may require us to append the FDT.
559 * We allow this to fail, as the U-Boot image might embed its FDT.
560 */
561 if (spl_image->os == IH_OS_U_BOOT)
562 spl_fit_append_fdt(spl_image, info, sector, fit,
563 images, base_offset);
Simon Glassf1dcee52016-02-22 22:55:56 -0700564
Jean-Jacques Hiblot6b8b98d2019-10-22 16:39:10 +0200565 firmware_node = node;
Andre Przywara411cf322017-04-26 01:32:37 +0100566 /* Now check if there are more images for us to load */
567 for (; ; index++) {
Philipp Tomsichd8796162017-09-13 21:29:32 +0200568 uint8_t os_type = IH_OS_INVALID;
569
Andre Przywara411cf322017-04-26 01:32:37 +0100570 node = spl_fit_get_image_node(fit, images, "loadables", index);
571 if (node < 0)
572 break;
573
Jean-Jacques Hiblot6b8b98d2019-10-22 16:39:10 +0200574 /*
575 * if the firmware is also a loadable, skip it because
576 * it already has been loaded. This is typically the case with
577 * u-boot.img generated by mkimage.
578 */
579 if (firmware_node == node)
580 continue;
581
Andre Przywara411cf322017-04-26 01:32:37 +0100582 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
583 &image_info);
584 if (ret < 0)
585 continue;
586
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100587 if (!spl_fit_image_get_os(fit, node, &os_type))
Philipp Tomsichd8796162017-09-13 21:29:32 +0200588 debug("Loadable is %s\n", genimg_get_os_name(os_type));
Abel Vesacf8dcc52019-03-12 08:34:31 +0000589#if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
590 else
591 os_type = IH_OS_U_BOOT;
592#endif
Philipp Tomsichd8796162017-09-13 21:29:32 +0200593
Philipp Tomsicha616c782017-09-13 21:29:34 +0200594 if (os_type == IH_OS_U_BOOT) {
595 spl_fit_append_fdt(&image_info, info, sector,
Philipp Tomsichd8796162017-09-13 21:29:32 +0200596 fit, images, base_offset);
Philipp Tomsicha616c782017-09-13 21:29:34 +0200597 spl_image->fdt_addr = image_info.fdt_addr;
598 }
Philipp Tomsichd8796162017-09-13 21:29:32 +0200599
Andre Przywara411cf322017-04-26 01:32:37 +0100600 /*
601 * If the "firmware" image did not provide an entry point,
602 * use the first valid entry point from the loadables.
603 */
604 if (spl_image->entry_point == FDT_ERROR &&
605 image_info.entry_point != FDT_ERROR)
606 spl_image->entry_point = image_info.entry_point;
Philipp Tomsicha616c782017-09-13 21:29:34 +0200607
608 /* Record our loadables into the FDT */
609 if (spl_image->fdt_addr)
610 spl_fit_record_loadable(fit, images, index,
611 spl_image->fdt_addr,
612 &image_info);
Andre Przywara411cf322017-04-26 01:32:37 +0100613 }
614
615 /*
616 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
617 * Makefile will set it to 0 and it will end up as the entry point
618 * here. What it actually means is: use the load address.
619 */
620 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
621 spl_image->entry_point = spl_image->load_addr;
622
Ye Lie246bfc2018-11-17 09:10:25 +0000623 spl_image->flags |= SPL_FIT_FOUND;
624
Stefano Babicd714a752019-09-20 08:47:53 +0200625#ifdef CONFIG_IMX_HAB
Ye Lie246bfc2018-11-17 09:10:25 +0000626 board_spl_fit_post_load((ulong)fit, size);
627#endif
628
Andre Przywara411cf322017-04-26 01:32:37 +0100629 return 0;
Simon Glassf1dcee52016-02-22 22:55:56 -0700630}