blob: fd6086a65c30f1fd427e4b66b4b05c360e777932 [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>
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +02009#include <board.h>
Michal Simekf707b5a2018-07-18 14:33:15 +020010#include <fpga.h>
Simon Glass1a974af2019-08-01 09:46:36 -060011#include <gzip.h>
Simon Glassa6131a22016-02-22 22:55:56 -070012#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +020014#include <malloc.h>
Simon Glassa6131a22016-02-22 22:55:56 -070015#include <spl.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <asm/cache.h>
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +020017#include <linux/libfdt.h>
Simon Glassa6131a22016-02-22 22:55:56 -070018
Lukas Auer80afee72019-08-21 21:14:42 +020019DECLARE_GLOBAL_DATA_PTR;
20
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +020021#ifndef CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ
22#define CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ (64 * 1024)
23#endif
24
York Suna6945fe2017-08-15 11:14:43 -070025#ifndef CONFIG_SYS_BOOTM_LEN
26#define CONFIG_SYS_BOOTM_LEN (64 << 20)
27#endif
28
Ye Li9d5b0f42018-11-17 09:10:25 +000029__weak void board_spl_fit_post_load(ulong load_addr, size_t length)
30{
31}
32
33__weak ulong board_spl_fit_size_align(ulong size)
34{
35 return size;
36}
37
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020038static int find_node_from_desc(const void *fit, int node, const char *str)
39{
40 int child;
41
42 if (node < 0)
43 return -EINVAL;
44
45 /* iterate the FIT nodes and find a matching description */
46 for (child = fdt_first_subnode(fit, node); child >= 0;
47 child = fdt_next_subnode(fit, child)) {
48 int len;
49 const char *desc = fdt_getprop(fit, child, "description", &len);
50
51 if (!desc)
52 continue;
53
54 if (!strcmp(desc, str))
55 return child;
56 }
57
58 return -ENOENT;
59}
60
Andre Przywara525e9c92017-04-26 01:32:34 +010061/**
Philipp Tomsiche61eff92017-09-13 21:29:34 +020062 * spl_fit_get_image_name(): By using the matching configuration subnode,
Andre Przywara525e9c92017-04-26 01:32:34 +010063 * retrieve the name of an image, specified by a property name and an index
64 * into that.
65 * @fit: Pointer to the FDT blob.
66 * @images: Offset of the /images subnode.
67 * @type: Name of the property within the configuration subnode.
68 * @index: Index into the list of strings in this property.
Philipp Tomsiche61eff92017-09-13 21:29:34 +020069 * @outname: Name of the image
Andre Przywara525e9c92017-04-26 01:32:34 +010070 *
Philipp Tomsiche61eff92017-09-13 21:29:34 +020071 * Return: 0 on success, or a negative error number
Andre Przywara525e9c92017-04-26 01:32:34 +010072 */
Philipp Tomsiche61eff92017-09-13 21:29:34 +020073static int spl_fit_get_image_name(const void *fit, int images,
74 const char *type, int index,
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +020075 const char **outname)
Andre Przywara1e329b62017-04-26 01:32:33 +010076{
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020077 struct udevice *board;
Andre Przywara1e329b62017-04-26 01:32:33 +010078 const char *name, *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +020079 __maybe_unused int node;
80 int conf_node;
Andre Przywara1e329b62017-04-26 01:32:33 +010081 int len, i;
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020082 bool found = true;
Andre Przywara1e329b62017-04-26 01:32:33 +010083
Cooper Jr., Frankline6792402017-06-16 17:25:05 -050084 conf_node = fit_find_config_node(fit);
Andre Przywara1e329b62017-04-26 01:32:33 +010085 if (conf_node < 0) {
86#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
87 printf("No matching DT out of these options:\n");
88 for (node = fdt_first_subnode(fit, conf_node);
89 node >= 0;
90 node = fdt_next_subnode(fit, node)) {
91 name = fdt_getprop(fit, node, "description", &len);
92 printf(" %s\n", name);
Simon Glassa6131a22016-02-22 22:55:56 -070093 }
Andre Przywara1e329b62017-04-26 01:32:33 +010094#endif
95 return conf_node;
96 }
Simon Glassa6131a22016-02-22 22:55:56 -070097
Andre Przywara1e329b62017-04-26 01:32:33 +010098 name = fdt_getprop(fit, conf_node, type, &len);
99 if (!name) {
100 debug("cannot find property '%s': %d\n", type, len);
101 return -EINVAL;
102 }
Simon Glassa6131a22016-02-22 22:55:56 -0700103
Andre Przywara1e329b62017-04-26 01:32:33 +0100104 str = name;
105 for (i = 0; i < index; i++) {
106 str = strchr(str, '\0') + 1;
107 if (!str || (str - name >= len)) {
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200108 found = false;
109 break;
Andre Przywara1e329b62017-04-26 01:32:33 +0100110 }
Simon Glassa6131a22016-02-22 22:55:56 -0700111 }
112
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200113 if (!found && !board_get(&board)) {
114 int rc;
115 /*
116 * no string in the property for this index. Check if the board
117 * level code can supply one.
118 */
119 rc = board_get_fit_loadable(board, index - i - 1, type, &str);
120 if (rc && rc != -ENOENT)
121 return rc;
122
123 if (!rc) {
124 /*
125 * The board provided a name for a loadable.
126 * Try to match it against the description properties
127 * first. If no matching node is found, use it as a
128 * node name.
129 */
130 int node;
131 int images = fdt_path_offset(fit, FIT_IMAGES_PATH);
132
133 node = find_node_from_desc(fit, images, str);
134 if (node > 0)
135 str = fdt_get_name(fit, node, NULL);
136
137 found = true;
138 }
139 }
140
141 if (!found) {
142 debug("no string for index %d\n", index);
143 return -E2BIG;
144 }
145
146 *outname = str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200147 return 0;
148}
149
150/**
151 * spl_fit_get_image_node(): By using the matching configuration subnode,
152 * retrieve the name of an image, specified by a property name and an index
153 * into that.
154 * @fit: Pointer to the FDT blob.
155 * @images: Offset of the /images subnode.
156 * @type: Name of the property within the configuration subnode.
157 * @index: Index into the list of strings in this property.
158 *
159 * Return: the node offset of the respective image node or a negative
160 * error number.
161 */
162static int spl_fit_get_image_node(const void *fit, int images,
163 const char *type, int index)
164{
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200165 const char *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200166 int err;
167 int node;
168
169 err = spl_fit_get_image_name(fit, images, type, index, &str);
170 if (err)
171 return err;
172
Andre Przywara1e329b62017-04-26 01:32:33 +0100173 debug("%s: '%s'\n", type, str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200174
Andre Przywara1e329b62017-04-26 01:32:33 +0100175 node = fdt_subnode_offset(fit, images, str);
176 if (node < 0) {
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200177 pr_err("cannot find image node '%s': %d\n", str, node);
Andre Przywara1e329b62017-04-26 01:32:33 +0100178 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700179 }
Simon Glassa6131a22016-02-22 22:55:56 -0700180
Andre Przywara525e9c92017-04-26 01:32:34 +0100181 return node;
Simon Glassa6131a22016-02-22 22:55:56 -0700182}
183
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530184static int get_aligned_image_offset(struct spl_load_info *info, int offset)
185{
186 /*
187 * If it is a FS read, get the first address before offset which is
188 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
189 * block number to which offset belongs.
190 */
191 if (info->filename)
192 return offset & ~(ARCH_DMA_MINALIGN - 1);
193
194 return offset / info->bl_len;
195}
196
197static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
198{
199 /*
200 * If it is a FS read, get the difference between the offset and
201 * the first address before offset which is aligned to
202 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
203 * block.
204 */
205 if (info->filename)
206 return offset & (ARCH_DMA_MINALIGN - 1);
207
208 return offset % info->bl_len;
209}
210
211static int get_aligned_image_size(struct spl_load_info *info, int data_size,
212 int offset)
213{
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530214 data_size = data_size + get_aligned_image_overhead(info, offset);
215
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530216 if (info->filename)
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530217 return data_size;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530218
219 return (data_size + info->bl_len - 1) / info->bl_len;
220}
221
Andre Przywara29053e92017-04-26 01:32:36 +0100222/**
223 * spl_load_fit_image(): load the image described in a certain FIT node
224 * @info: points to information about the device to load data from
225 * @sector: the start sector of the FIT image on the device
226 * @fit: points to the flattened device tree blob describing the FIT
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200227 * image
Andre Przywara29053e92017-04-26 01:32:36 +0100228 * @base_offset: the beginning of the data area containing the actual
229 * image data, relative to the beginning of the FIT
230 * @node: offset of the DT node describing the image to load (relative
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200231 * to @fit)
Andre Przywara29053e92017-04-26 01:32:36 +0100232 * @image_info: will be filled with information about the loaded image
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200233 * If the FIT node does not contain a "load" (address) property,
234 * the image gets loaded to the address pointed to by the
235 * load_addr member in this struct.
Andre Przywara29053e92017-04-26 01:32:36 +0100236 *
237 * Return: 0 on success or a negative error number.
238 */
239static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
240 void *fit, ulong base_offset, int node,
241 struct spl_image_info *image_info)
242{
Michal Simekf707b5a2018-07-18 14:33:15 +0200243 int offset;
Andre Przywara29053e92017-04-26 01:32:36 +0100244 size_t length;
York Sunadf99fa2017-08-15 11:14:44 -0700245 int len;
York Sun25d65f12017-09-15 08:21:13 -0700246 ulong size;
Andre Przywara29053e92017-04-26 01:32:36 +0100247 ulong load_addr, load_ptr;
248 void *src;
249 ulong overhead;
250 int nr_sectors;
251 int align_len = ARCH_DMA_MINALIGN - 1;
York Suna6945fe2017-08-15 11:14:43 -0700252 uint8_t image_comp = -1, type = -1;
York Sunadf99fa2017-08-15 11:14:44 -0700253 const void *data;
Peng Fan8876c7e2017-12-05 13:20:59 +0800254 bool external_data = false;
York Suna6945fe2017-08-15 11:14:43 -0700255
Michal Simek1aab1142020-09-09 14:41:56 +0200256 if (IS_ENABLED(CONFIG_SPL_FPGA) ||
Marek Vasut72bc5d52018-06-01 23:19:29 +0200257 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
258 if (fit_image_get_type(fit, node, &type))
259 puts("Cannot get image type.\n");
260 else
261 debug("%s ", genimg_get_type_name(type));
262 }
263
Klaus H. Sorensen4a9a0422019-12-11 11:03:33 +0000264 if (IS_ENABLED(CONFIG_SPL_GZIP)) {
265 fit_image_get_comp(fit, node, &image_comp);
266 debug("%s ", genimg_get_comp_name(image_comp));
York Suna6945fe2017-08-15 11:14:43 -0700267 }
Andre Przywara29053e92017-04-26 01:32:36 +0100268
York Sunadf99fa2017-08-15 11:14:44 -0700269 if (fit_image_get_load(fit, node, &load_addr))
Andre Przywara29053e92017-04-26 01:32:36 +0100270 load_addr = image_info->load_addr;
Andre Przywara29053e92017-04-26 01:32:36 +0100271
Peng Fan8876c7e2017-12-05 13:20:59 +0800272 if (!fit_image_get_data_position(fit, node, &offset)) {
273 external_data = true;
274 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
York Sunadf99fa2017-08-15 11:14:44 -0700275 offset += base_offset;
Peng Fan8876c7e2017-12-05 13:20:59 +0800276 external_data = true;
277 }
278
279 if (external_data) {
280 /* External data */
York Sunadf99fa2017-08-15 11:14:44 -0700281 if (fit_image_get_data_size(fit, node, &len))
282 return -ENOENT;
Andre Przywara29053e92017-04-26 01:32:36 +0100283
York Sunadf99fa2017-08-15 11:14:44 -0700284 load_ptr = (load_addr + align_len) & ~align_len;
285 length = len;
286
287 overhead = get_aligned_image_overhead(info, offset);
288 nr_sectors = get_aligned_image_size(info, length, offset);
289
Michal Simekf707b5a2018-07-18 14:33:15 +0200290 if (info->read(info,
291 sector + get_aligned_image_offset(info, offset),
York Sunadf99fa2017-08-15 11:14:44 -0700292 nr_sectors, (void *)load_ptr) != nr_sectors)
293 return -EIO;
294
295 debug("External data: dst=%lx, offset=%x, size=%lx\n",
296 load_ptr, offset, (unsigned long)length);
297 src = (void *)load_ptr + overhead;
298 } else {
299 /* Embedded data */
300 if (fit_image_get_data(fit, node, &data, &length)) {
301 puts("Cannot get image data/size\n");
302 return -ENOENT;
303 }
304 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
305 (unsigned long)length);
306 src = (void *)data;
307 }
Andre Przywara29053e92017-04-26 01:32:36 +0100308
Ben Whitten54a91192018-06-07 11:37:27 +0100309#ifdef CONFIG_SPL_FIT_SIGNATURE
310 printf("## Checking hash(es) for Image %s ... ",
311 fit_get_name(fit, node, NULL));
312 if (!fit_image_verify_with_data(fit, node,
313 src, length))
314 return -EPERM;
315 puts("OK\n");
316#endif
317
Andre Przywara29053e92017-04-26 01:32:36 +0100318#ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
319 board_fit_image_post_process(&src, &length);
320#endif
321
Michal Simekf354c3f2018-07-24 15:05:00 +0200322 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
York Sun25d65f12017-09-15 08:21:13 -0700323 size = length;
York Suna6945fe2017-08-15 11:14:43 -0700324 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
York Sun25d65f12017-09-15 08:21:13 -0700325 src, &size)) {
York Suna6945fe2017-08-15 11:14:43 -0700326 puts("Uncompressing error\n");
327 return -EIO;
328 }
York Sun25d65f12017-09-15 08:21:13 -0700329 length = size;
York Suna6945fe2017-08-15 11:14:43 -0700330 } else {
331 memcpy((void *)load_addr, src, length);
332 }
Andre Przywara29053e92017-04-26 01:32:36 +0100333
334 if (image_info) {
335 image_info->load_addr = load_addr;
336 image_info->size = length;
337 image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
338 }
339
340 return 0;
341}
342
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200343static int spl_fit_append_fdt(struct spl_image_info *spl_image,
344 struct spl_load_info *info, ulong sector,
345 void *fit, int images, ulong base_offset)
346{
347 struct spl_image_info image_info;
Michal Simekbf703df2019-10-22 16:39:11 +0200348 int node, ret = 0, index = 0;
Lukas Auer80afee72019-08-21 21:14:42 +0200349
350 /*
351 * Use the address following the image as target address for the
Marek Vasutc27cbe82020-10-19 23:40:26 +0200352 * device tree.
Lukas Auer80afee72019-08-21 21:14:42 +0200353 */
Marek Vasutc27cbe82020-10-19 23:40:26 +0200354 image_info.load_addr = spl_image->load_addr + spl_image->size;
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200355
356 /* Figure out which device tree the board wants to use */
Michal Simekbf703df2019-10-22 16:39:11 +0200357 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, index++);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200358 if (node < 0) {
359 debug("%s: cannot find FDT node\n", __func__);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200360
Lukas Auer80afee72019-08-21 21:14:42 +0200361 /*
362 * U-Boot did not find a device tree inside the FIT image. Use
363 * the U-Boot device tree instead.
364 */
365 if (gd->fdt_blob)
366 memcpy((void *)image_info.load_addr, gd->fdt_blob,
367 fdt_totalsize(gd->fdt_blob));
368 else
369 return node;
370 } else {
371 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
372 &image_info);
373 if (ret < 0)
374 return ret;
375 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200376
377 /* Make the load-address of the FDT available for the SPL framework */
378 spl_image->fdt_addr = (void *)image_info.load_addr;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100379#if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
Michal Simekbf703df2019-10-22 16:39:11 +0200380 if (CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)) {
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200381 void *tmpbuffer = NULL;
382
Michal Simekbf703df2019-10-22 16:39:11 +0200383 for (; ; index++) {
384 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP,
385 index);
Jean-Jacques Hiblotc6ab6a12019-10-22 16:39:14 +0200386 if (node == -E2BIG) {
Michal Simekbf703df2019-10-22 16:39:11 +0200387 debug("%s: No additional FDT node\n", __func__);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200388 break;
Jean-Jacques Hiblotc6ab6a12019-10-22 16:39:14 +0200389 } else if (node < 0) {
390 debug("%s: unable to find FDT node %d\n",
391 __func__, index);
392 continue;
Michal Simekbf703df2019-10-22 16:39:11 +0200393 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200394
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200395 if (!tmpbuffer) {
396 /*
397 * allocate memory to store the DT overlay
398 * before it is applied. It may not be used
399 * depending on how the overlay is stored, so
400 * don't fail yet if the allocation failed.
401 */
402 tmpbuffer = malloc(CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ);
403 if (!tmpbuffer)
404 debug("%s: unable to allocate space for overlays\n",
405 __func__);
406 }
407 image_info.load_addr = (ulong)tmpbuffer;
Michal Simekbf703df2019-10-22 16:39:11 +0200408 ret = spl_load_fit_image(info, sector, fit, base_offset,
409 node, &image_info);
410 if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200411 break;
Michal Simekbf703df2019-10-22 16:39:11 +0200412
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200413 /* Make room in FDT for changes from the overlay */
414 ret = fdt_increase_size(spl_image->fdt_addr,
415 image_info.size);
416 if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200417 break;
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200418
Michal Simekbf703df2019-10-22 16:39:11 +0200419 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
420 (void *)image_info.load_addr);
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200421 if (ret) {
422 pr_err("failed to apply DT overlay %s\n",
423 fit_get_name(fit, node, NULL));
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200424 break;
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200425 }
Michal Simekbf703df2019-10-22 16:39:11 +0200426
427 debug("%s: DT overlay %s applied\n", __func__,
428 fit_get_name(fit, node, NULL));
429 }
Heinrich Schuchardt939a9612020-04-20 12:44:01 +0200430 free(tmpbuffer);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200431 if (ret)
432 return ret;
Michal Simekbf703df2019-10-22 16:39:11 +0200433 }
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200434 /* Try to make space, so we can inject details on the loadables */
435 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
436 if (ret < 0)
437 return ret;
438#endif
439
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200440 return ret;
441}
442
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200443static int spl_fit_record_loadable(const void *fit, int images, int index,
444 void *blob, struct spl_image_info *image)
445{
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100446 int ret = 0;
447#if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200448 const char *name;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100449 int node;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200450
451 ret = spl_fit_get_image_name(fit, images, "loadables",
452 index, &name);
453 if (ret < 0)
454 return ret;
455
456 node = spl_fit_get_image_node(fit, images, "loadables", index);
457
458 ret = fdt_record_loadable(blob, index, name, image->load_addr,
459 image->size, image->entry_point,
460 fdt_getprop(fit, node, "type", NULL),
461 fdt_getprop(fit, node, "os", NULL));
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100462#endif
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200463 return ret;
464}
465
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100466static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
467{
Jean-Jacques Hiblot943ec252019-04-26 15:21:25 +0200468#if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) && !defined(CONFIG_SPL_OS_BOOT)
Samuel Hollande646f512020-10-21 21:12:13 -0500469 const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
470
471 if (!name)
472 return -ENOENT;
473
474 /*
475 * We don't care what the type of the image actually is,
476 * only whether or not it is U-Boot. This saves some
477 * space by omitting the large table of OS types.
478 */
479 if (!strcmp(name, "u-boot"))
480 *os = IH_OS_U_BOOT;
481 else
482 *os = IH_OS_INVALID;
483
484 return 0;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100485#else
486 return fit_image_get_os(fit, noffset, os);
487#endif
488}
489
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500490/*
491 * Weak default function to allow customizing SPL fit loading for load-only
492 * use cases by allowing to skip the parsing/processing of the FIT contents
493 * (so that this can be done separately in a more customized fashion)
494 */
495__weak bool spl_load_simple_fit_skip_processing(void)
496{
497 return false;
498}
499
Simon Glass43a734f2016-09-24 18:20:16 -0600500int spl_load_simple_fit(struct spl_image_info *spl_image,
501 struct spl_load_info *info, ulong sector, void *fit)
Simon Glassa6131a22016-02-22 22:55:56 -0700502{
503 int sectors;
Andre Przywara29053e92017-04-26 01:32:36 +0100504 ulong size;
Simon Glassa6131a22016-02-22 22:55:56 -0700505 unsigned long count;
Andre Przywara29053e92017-04-26 01:32:36 +0100506 struct spl_image_info image_info;
York Suna058b8a2017-08-15 11:14:45 -0700507 int node = -1;
508 int images, ret;
Marek Vasut1dda46f2018-08-14 11:27:02 +0200509 int base_offset, hsize, align_len = ARCH_DMA_MINALIGN - 1;
Andre Przywarae7c58562017-04-26 01:32:37 +0100510 int index = 0;
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200511 int firmware_node;
Simon Glassa6131a22016-02-22 22:55:56 -0700512
513 /*
York Suna058b8a2017-08-15 11:14:45 -0700514 * For FIT with external data, figure out where the external images
515 * start. This is the base for the data-offset properties in each
516 * image.
Simon Glassa6131a22016-02-22 22:55:56 -0700517 */
518 size = fdt_totalsize(fit);
519 size = (size + 3) & ~3;
Ye Li9d5b0f42018-11-17 09:10:25 +0000520 size = board_spl_fit_size_align(size);
Simon Glassa6131a22016-02-22 22:55:56 -0700521 base_offset = (size + 3) & ~3;
522
523 /*
524 * So far we only have one block of data from the FIT. Read the entire
525 * thing, including that first block, placing it so it finishes before
526 * where we will load the image.
527 *
528 * Note that we will load the image such that its first byte will be
529 * at the load address. Since that byte may be part-way through a
530 * block, we may load the image up to one block before the load
531 * address. So take account of that here by subtracting an addition
532 * block length from the FIT start position.
533 *
534 * In fact the FIT has its own load address, but we assume it cannot
535 * be before CONFIG_SYS_TEXT_BASE.
York Suna058b8a2017-08-15 11:14:45 -0700536 *
537 * For FIT with data embedded, data is loaded as part of FIT image.
538 * For FIT with external data, data is not loaded in this step.
Simon Glassa6131a22016-02-22 22:55:56 -0700539 */
Marek Vasut1dda46f2018-08-14 11:27:02 +0200540 hsize = (size + info->bl_len + align_len) & ~align_len;
541 fit = spl_get_load_buffer(-hsize, hsize);
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530542 sectors = get_aligned_image_size(info, size, 0);
Simon Glassa6131a22016-02-22 22:55:56 -0700543 count = info->read(info, sector, sectors, fit);
Ye Li9d5b0f42018-11-17 09:10:25 +0000544 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
545 sector, sectors, fit, count, size);
546
Simon Glassa6131a22016-02-22 22:55:56 -0700547 if (count == 0)
548 return -EIO;
549
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500550 /* skip further processing if requested to enable load-only use cases */
551 if (spl_load_simple_fit_skip_processing())
552 return 0;
553
Andre Przywara525e9c92017-04-26 01:32:34 +0100554 /* find the node holding the images information */
Simon Glassa6131a22016-02-22 22:55:56 -0700555 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
556 if (images < 0) {
557 debug("%s: Cannot find /images node: %d\n", __func__, images);
558 return -1;
559 }
Marek Vasut33dd00e2018-05-12 22:25:28 +0200560
Michal Simek1aab1142020-09-09 14:41:56 +0200561#ifdef CONFIG_SPL_FPGA
Marek Vasut33dd00e2018-05-12 22:25:28 +0200562 node = spl_fit_get_image_node(fit, images, "fpga", 0);
563 if (node >= 0) {
564 /* Load the image and set up the spl_image structure */
565 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
566 spl_image);
567 if (ret) {
568 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
569 return ret;
570 }
Michal Simekf707b5a2018-07-18 14:33:15 +0200571
572 debug("FPGA bitstream at: %x, size: %x\n",
573 (u32)spl_image->load_addr, spl_image->size);
574
575 ret = fpga_load(0, (const void *)spl_image->load_addr,
576 spl_image->size, BIT_FULL);
577 if (ret) {
578 printf("%s: Cannot load the image to the FPGA\n",
579 __func__);
580 return ret;
581 }
582
Luis Aranedaeece6232018-07-19 03:10:16 -0400583 puts("FPGA image loaded from FIT\n");
Marek Vasut33dd00e2018-05-12 22:25:28 +0200584 node = -1;
585 }
586#endif
Andre Przywara525e9c92017-04-26 01:32:34 +0100587
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200588 /*
589 * Find the U-Boot image using the following search order:
590 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
591 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
592 * - fall back to using the first 'loadables' entry
593 */
594 if (node < 0)
Michal Simekb766e8f2018-03-26 16:31:26 +0200595 node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
596 0);
York Suna058b8a2017-08-15 11:14:45 -0700597#ifdef CONFIG_SPL_OS_BOOT
York Suna058b8a2017-08-15 11:14:45 -0700598 if (node < 0)
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200599 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
York Suna058b8a2017-08-15 11:14:45 -0700600#endif
Andre Przywara525e9c92017-04-26 01:32:34 +0100601 if (node < 0) {
602 debug("could not find firmware image, trying loadables...\n");
603 node = spl_fit_get_image_node(fit, images, "loadables", 0);
Andre Przywarae7c58562017-04-26 01:32:37 +0100604 /*
605 * If we pick the U-Boot image from "loadables", start at
606 * the second image when later loading additional images.
607 */
608 index = 1;
Andre Przywara525e9c92017-04-26 01:32:34 +0100609 }
Simon Glassa6131a22016-02-22 22:55:56 -0700610 if (node < 0) {
Andre Przywara525e9c92017-04-26 01:32:34 +0100611 debug("%s: Cannot find u-boot image node: %d\n",
612 __func__, node);
Simon Glassa6131a22016-02-22 22:55:56 -0700613 return -1;
614 }
615
Andre Przywara29053e92017-04-26 01:32:36 +0100616 /* Load the image and set up the spl_image structure */
617 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
618 spl_image);
619 if (ret)
620 return ret;
Daniel Allredcf839bd2016-06-27 09:19:21 -0500621
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200622 /*
623 * For backward compatibility, we treat the first node that is
624 * as a U-Boot image, if no OS-type has been declared.
625 */
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100626 if (!spl_fit_image_get_os(fit, node, &spl_image->os))
York Suna058b8a2017-08-15 11:14:45 -0700627 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200628#if !defined(CONFIG_SPL_OS_BOOT)
629 else
630 spl_image->os = IH_OS_U_BOOT;
York Suna058b8a2017-08-15 11:14:45 -0700631#endif
Simon Glassa6131a22016-02-22 22:55:56 -0700632
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200633 /*
634 * Booting a next-stage U-Boot may require us to append the FDT.
635 * We allow this to fail, as the U-Boot image might embed its FDT.
636 */
Dario Binacchifaf8d142020-05-27 13:56:19 +0200637 if (spl_image->os == IH_OS_U_BOOT) {
638 ret = spl_fit_append_fdt(spl_image, info, sector, fit,
639 images, base_offset);
640 if (!IS_ENABLED(CONFIG_OF_EMBED) && ret < 0)
641 return ret;
642 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100643
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200644 firmware_node = node;
Andre Przywarae7c58562017-04-26 01:32:37 +0100645 /* Now check if there are more images for us to load */
646 for (; ; index++) {
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200647 uint8_t os_type = IH_OS_INVALID;
648
Andre Przywarae7c58562017-04-26 01:32:37 +0100649 node = spl_fit_get_image_node(fit, images, "loadables", index);
650 if (node < 0)
651 break;
652
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200653 /*
654 * if the firmware is also a loadable, skip it because
655 * it already has been loaded. This is typically the case with
656 * u-boot.img generated by mkimage.
657 */
658 if (firmware_node == node)
659 continue;
660
Andre Przywarae7c58562017-04-26 01:32:37 +0100661 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
662 &image_info);
663 if (ret < 0)
664 continue;
665
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100666 if (!spl_fit_image_get_os(fit, node, &os_type))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200667 debug("Loadable is %s\n", genimg_get_os_name(os_type));
668
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200669 if (os_type == IH_OS_U_BOOT) {
670 spl_fit_append_fdt(&image_info, info, sector,
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200671 fit, images, base_offset);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200672 spl_image->fdt_addr = image_info.fdt_addr;
673 }
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200674
Andre Przywarae7c58562017-04-26 01:32:37 +0100675 /*
676 * If the "firmware" image did not provide an entry point,
677 * use the first valid entry point from the loadables.
678 */
679 if (spl_image->entry_point == FDT_ERROR &&
680 image_info.entry_point != FDT_ERROR)
681 spl_image->entry_point = image_info.entry_point;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200682
683 /* Record our loadables into the FDT */
684 if (spl_image->fdt_addr)
685 spl_fit_record_loadable(fit, images, index,
686 spl_image->fdt_addr,
687 &image_info);
Andre Przywarae7c58562017-04-26 01:32:37 +0100688 }
689
690 /*
691 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
692 * Makefile will set it to 0 and it will end up as the entry point
693 * here. What it actually means is: use the load address.
694 */
695 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
696 spl_image->entry_point = spl_image->load_addr;
697
Ye Li9d5b0f42018-11-17 09:10:25 +0000698 spl_image->flags |= SPL_FIT_FOUND;
699
Stefano Babicf8b509b2019-09-20 08:47:53 +0200700#ifdef CONFIG_IMX_HAB
Ye Li9d5b0f42018-11-17 09:10:25 +0000701 board_spl_fit_post_load((ulong)fit, size);
702#endif
703
Andre Przywarae7c58562017-04-26 01:32:37 +0100704 return 0;
Simon Glassa6131a22016-02-22 22:55:56 -0700705}