blob: 70d8d5942d96c6e6017007405a1fac2a2846aaa1 [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>
Michal Simekf707b5a2018-07-18 14:33:15 +02009#include <fpga.h>
Simon Glass1a974af2019-08-01 09:46:36 -060010#include <gzip.h>
Simon Glassa6131a22016-02-22 22:55:56 -070011#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +020013#include <memalign.h>
Simon Glass2192e982021-03-07 17:35:14 -070014#include <mapmem.h>
Simon Glassa6131a22016-02-22 22:55:56 -070015#include <spl.h>
Simon Glass458b66a2020-11-05 06:32:05 -070016#include <sysinfo.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040019#include <asm/io.h>
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +020020#include <linux/libfdt.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Simon Glassa6131a22016-02-22 22:55:56 -070022
Lukas Auer80afee72019-08-21 21:14:42 +020023DECLARE_GLOBAL_DATA_PTR;
24
Alexandru Gagniuc23243c92021-01-20 10:46:50 -060025struct spl_fit_info {
26 const void *fit; /* Pointer to a valid FIT blob */
27 size_t ext_data_offset; /* Offset to FIT external data (end of FIT) */
28 int images_node; /* FDT offset to "/images" node */
Alexandru Gagniucfa11a442021-01-20 10:46:53 -060029 int conf_node; /* FDT offset to selected configuration node */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -060030};
31
Ye Li9d5b0f42018-11-17 09:10:25 +000032__weak ulong board_spl_fit_size_align(ulong size)
33{
34 return size;
35}
36
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020037static int find_node_from_desc(const void *fit, int node, const char *str)
38{
39 int child;
40
41 if (node < 0)
42 return -EINVAL;
43
44 /* iterate the FIT nodes and find a matching description */
45 for (child = fdt_first_subnode(fit, node); child >= 0;
46 child = fdt_next_subnode(fit, child)) {
47 int len;
Simon Glassdff90432023-09-26 08:14:35 -060048 const char *desc = fdt_getprop(fit, child, FIT_DESC_PROP, &len);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020049
50 if (!desc)
51 continue;
52
53 if (!strcmp(desc, str))
54 return child;
55 }
56
57 return -ENOENT;
58}
59
Andre Przywara525e9c92017-04-26 01:32:34 +010060/**
Philipp Tomsiche61eff92017-09-13 21:29:34 +020061 * spl_fit_get_image_name(): By using the matching configuration subnode,
Andre Przywara525e9c92017-04-26 01:32:34 +010062 * retrieve the name of an image, specified by a property name and an index
63 * into that.
64 * @fit: Pointer to the FDT blob.
65 * @images: Offset of the /images subnode.
66 * @type: Name of the property within the configuration subnode.
67 * @index: Index into the list of strings in this property.
Philipp Tomsiche61eff92017-09-13 21:29:34 +020068 * @outname: Name of the image
Andre Przywara525e9c92017-04-26 01:32:34 +010069 *
Philipp Tomsiche61eff92017-09-13 21:29:34 +020070 * Return: 0 on success, or a negative error number
Andre Przywara525e9c92017-04-26 01:32:34 +010071 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -060072static int spl_fit_get_image_name(const struct spl_fit_info *ctx,
Philipp Tomsiche61eff92017-09-13 21:29:34 +020073 const char *type, int index,
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +020074 const char **outname)
Andre Przywara1e329b62017-04-26 01:32:33 +010075{
Simon Glass458b66a2020-11-05 06:32:05 -070076 struct udevice *sysinfo;
Andre Przywara1e329b62017-04-26 01:32:33 +010077 const char *name, *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +020078 __maybe_unused int node;
Andre Przywara1e329b62017-04-26 01:32:33 +010079 int len, i;
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020080 bool found = true;
Andre Przywara1e329b62017-04-26 01:32:33 +010081
Alexandru Gagniucfa11a442021-01-20 10:46:53 -060082 name = fdt_getprop(ctx->fit, ctx->conf_node, type, &len);
Andre Przywara1e329b62017-04-26 01:32:33 +010083 if (!name) {
84 debug("cannot find property '%s': %d\n", type, len);
85 return -EINVAL;
86 }
Simon Glassa6131a22016-02-22 22:55:56 -070087
Andre Przywara1e329b62017-04-26 01:32:33 +010088 str = name;
89 for (i = 0; i < index; i++) {
90 str = strchr(str, '\0') + 1;
91 if (!str || (str - name >= len)) {
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020092 found = false;
93 break;
Andre Przywara1e329b62017-04-26 01:32:33 +010094 }
Simon Glassa6131a22016-02-22 22:55:56 -070095 }
96
Simon Glass458b66a2020-11-05 06:32:05 -070097 if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) {
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020098 int rc;
99 /*
Simon Glass458b66a2020-11-05 06:32:05 -0700100 * no string in the property for this index. Check if the
101 * sysinfo-level code can supply one.
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200102 */
Sean Anderson6ccf10a2021-04-20 10:50:56 -0400103 rc = sysinfo_detect(sysinfo);
104 if (rc)
105 return rc;
106
Simon Glass458b66a2020-11-05 06:32:05 -0700107 rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
108 &str);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200109 if (rc && rc != -ENOENT)
110 return rc;
111
112 if (!rc) {
113 /*
Simon Glass458b66a2020-11-05 06:32:05 -0700114 * The sysinfo provided a name for a loadable.
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200115 * Try to match it against the description properties
116 * first. If no matching node is found, use it as a
117 * node name.
118 */
119 int node;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600120 int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200121
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600122 node = find_node_from_desc(ctx->fit, images, str);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200123 if (node > 0)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600124 str = fdt_get_name(ctx->fit, node, NULL);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200125
126 found = true;
127 }
128 }
129
130 if (!found) {
131 debug("no string for index %d\n", index);
132 return -E2BIG;
133 }
134
135 *outname = str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200136 return 0;
137}
138
139/**
140 * spl_fit_get_image_node(): By using the matching configuration subnode,
141 * retrieve the name of an image, specified by a property name and an index
142 * into that.
143 * @fit: Pointer to the FDT blob.
144 * @images: Offset of the /images subnode.
145 * @type: Name of the property within the configuration subnode.
146 * @index: Index into the list of strings in this property.
147 *
148 * Return: the node offset of the respective image node or a negative
149 * error number.
150 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600151static int spl_fit_get_image_node(const struct spl_fit_info *ctx,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200152 const char *type, int index)
153{
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200154 const char *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200155 int err;
156 int node;
157
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600158 err = spl_fit_get_image_name(ctx, type, index, &str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200159 if (err)
160 return err;
161
Andre Przywara1e329b62017-04-26 01:32:33 +0100162 debug("%s: '%s'\n", type, str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200163
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600164 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
Andre Przywara1e329b62017-04-26 01:32:33 +0100165 if (node < 0) {
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200166 pr_err("cannot find image node '%s': %d\n", str, node);
Andre Przywara1e329b62017-04-26 01:32:33 +0100167 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700168 }
Simon Glassa6131a22016-02-22 22:55:56 -0700169
Andre Przywara525e9c92017-04-26 01:32:34 +0100170 return node;
Simon Glassa6131a22016-02-22 22:55:56 -0700171}
172
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530173static int get_aligned_image_offset(struct spl_load_info *info, int offset)
174{
175 /*
176 * If it is a FS read, get the first address before offset which is
177 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
178 * block number to which offset belongs.
179 */
180 if (info->filename)
181 return offset & ~(ARCH_DMA_MINALIGN - 1);
182
183 return offset / info->bl_len;
184}
185
186static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
187{
188 /*
189 * If it is a FS read, get the difference between the offset and
190 * the first address before offset which is aligned to
191 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
192 * block.
193 */
194 if (info->filename)
195 return offset & (ARCH_DMA_MINALIGN - 1);
196
197 return offset % info->bl_len;
198}
199
200static int get_aligned_image_size(struct spl_load_info *info, int data_size,
201 int offset)
202{
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530203 data_size = data_size + get_aligned_image_overhead(info, offset);
204
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530205 if (info->filename)
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530206 return data_size;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530207
208 return (data_size + info->bl_len - 1) / info->bl_len;
209}
210
Andre Przywara29053e92017-04-26 01:32:36 +0100211/**
Simon Glass205ff7b2023-09-26 08:14:33 -0600212 * load_simple_fit(): load the image described in a certain FIT node
Andre Przywara29053e92017-04-26 01:32:36 +0100213 * @info: points to information about the device to load data from
214 * @sector: the start sector of the FIT image on the device
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600215 * @ctx: points to the FIT context structure
Andre Przywara29053e92017-04-26 01:32:36 +0100216 * @node: offset of the DT node describing the image to load (relative
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200217 * to @fit)
Andre Przywara29053e92017-04-26 01:32:36 +0100218 * @image_info: will be filled with information about the loaded image
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200219 * If the FIT node does not contain a "load" (address) property,
220 * the image gets loaded to the address pointed to by the
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500221 * load_addr member in this struct, if load_addr is not 0
Andre Przywara29053e92017-04-26 01:32:36 +0100222 *
223 * Return: 0 on success or a negative error number.
224 */
Simon Glass205ff7b2023-09-26 08:14:33 -0600225static int load_simple_fit(struct spl_load_info *info, ulong sector,
226 const struct spl_fit_info *ctx, int node,
227 struct spl_image_info *image_info)
Andre Przywara29053e92017-04-26 01:32:36 +0100228{
Michal Simekf707b5a2018-07-18 14:33:15 +0200229 int offset;
Andre Przywara29053e92017-04-26 01:32:36 +0100230 size_t length;
York Sunadf99fa2017-08-15 11:14:44 -0700231 int len;
York Sun25d65f12017-09-15 08:21:13 -0700232 ulong size;
Simon Glass2192e982021-03-07 17:35:14 -0700233 ulong load_addr;
234 void *load_ptr;
Andre Przywara29053e92017-04-26 01:32:36 +0100235 void *src;
236 ulong overhead;
237 int nr_sectors;
York Suna6945fe2017-08-15 11:14:43 -0700238 uint8_t image_comp = -1, type = -1;
York Sunadf99fa2017-08-15 11:14:44 -0700239 const void *data;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600240 const void *fit = ctx->fit;
Peng Fan8876c7e2017-12-05 13:20:59 +0800241 bool external_data = false;
York Suna6945fe2017-08-15 11:14:43 -0700242
Michal Simek1aab1142020-09-09 14:41:56 +0200243 if (IS_ENABLED(CONFIG_SPL_FPGA) ||
Manoj Sai2b2e6282023-09-18 00:56:25 +0530244 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && spl_decompression_enabled())) {
Marek Vasut72bc5d52018-06-01 23:19:29 +0200245 if (fit_image_get_type(fit, node, &type))
246 puts("Cannot get image type.\n");
247 else
248 debug("%s ", genimg_get_type_name(type));
249 }
250
Manoj Sai2b2e6282023-09-18 00:56:25 +0530251 if (spl_decompression_enabled()) {
Klaus H. Sorensen4a9a0422019-12-11 11:03:33 +0000252 fit_image_get_comp(fit, node, &image_comp);
253 debug("%s ", genimg_get_comp_name(image_comp));
York Suna6945fe2017-08-15 11:14:43 -0700254 }
Andre Przywara29053e92017-04-26 01:32:36 +0100255
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500256 if (fit_image_get_load(fit, node, &load_addr)) {
257 if (!image_info->load_addr) {
258 printf("Can't load %s: No load address and no buffer\n",
259 fit_get_name(fit, node, NULL));
260 return -ENOBUFS;
261 }
Andre Przywara29053e92017-04-26 01:32:36 +0100262 load_addr = image_info->load_addr;
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500263 }
Andre Przywara29053e92017-04-26 01:32:36 +0100264
Peng Fan8876c7e2017-12-05 13:20:59 +0800265 if (!fit_image_get_data_position(fit, node, &offset)) {
266 external_data = true;
267 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600268 offset += ctx->ext_data_offset;
Peng Fan8876c7e2017-12-05 13:20:59 +0800269 external_data = true;
270 }
271
272 if (external_data) {
Simon Glass2192e982021-03-07 17:35:14 -0700273 void *src_ptr;
274
Peng Fan8876c7e2017-12-05 13:20:59 +0800275 /* External data */
York Sunadf99fa2017-08-15 11:14:44 -0700276 if (fit_image_get_data_size(fit, node, &len))
277 return -ENOENT;
Andre Przywara29053e92017-04-26 01:32:36 +0100278
Nishanth Menone4cb6452021-10-19 12:32:29 -0500279 /* Dont bother to copy 0 byte data, but warn, though */
280 if (!len) {
281 log_warning("%s: Skip load '%s': image size is 0!\n",
282 __func__, fit_get_name(fit, node, NULL));
283 return 0;
284 }
285
Manoj Saia8560992023-09-18 00:56:26 +0530286 if (spl_decompression_enabled() &&
287 (image_comp == IH_COMP_GZIP || image_comp == IH_COMP_LZMA))
Manoj Sai2b2e6282023-09-18 00:56:25 +0530288 src_ptr = map_sysmem(ALIGN(CONFIG_SYS_LOAD_ADDR, ARCH_DMA_MINALIGN), len);
289 else
290 src_ptr = map_sysmem(ALIGN(load_addr, ARCH_DMA_MINALIGN), len);
York Sunadf99fa2017-08-15 11:14:44 -0700291 length = len;
292
293 overhead = get_aligned_image_overhead(info, offset);
294 nr_sectors = get_aligned_image_size(info, length, offset);
295
Michal Simekf707b5a2018-07-18 14:33:15 +0200296 if (info->read(info,
297 sector + get_aligned_image_offset(info, offset),
Simon Glass2192e982021-03-07 17:35:14 -0700298 nr_sectors, src_ptr) != nr_sectors)
York Sunadf99fa2017-08-15 11:14:44 -0700299 return -EIO;
300
Simon Glass2192e982021-03-07 17:35:14 -0700301 debug("External data: dst=%p, offset=%x, size=%lx\n",
302 src_ptr, offset, (unsigned long)length);
303 src = src_ptr + overhead;
York Sunadf99fa2017-08-15 11:14:44 -0700304 } else {
305 /* Embedded data */
306 if (fit_image_get_data(fit, node, &data, &length)) {
307 puts("Cannot get image data/size\n");
308 return -ENOENT;
309 }
310 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
311 (unsigned long)length);
Simon Glass2192e982021-03-07 17:35:14 -0700312 src = (void *)data; /* cast away const */
York Sunadf99fa2017-08-15 11:14:44 -0700313 }
Andre Przywara29053e92017-04-26 01:32:36 +0100314
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600315 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
316 printf("## Checking hash(es) for Image %s ... ",
317 fit_get_name(fit, node, NULL));
Simon Glasse10e2ee2021-11-12 12:28:10 -0700318 if (!fit_image_verify_with_data(fit, node, gd_fdt_blob(), src,
319 length))
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600320 return -EPERM;
321 puts("OK\n");
322 }
Ben Whitten54a91192018-06-07 11:37:27 +0100323
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600324 if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS))
Lokesh Vutlab36dd3e2021-06-11 11:45:05 +0300325 board_fit_image_post_process(fit, node, &src, &length);
Andre Przywara29053e92017-04-26 01:32:36 +0100326
Simon Glass2192e982021-03-07 17:35:14 -0700327 load_ptr = map_sysmem(load_addr, length);
Michal Simekf354c3f2018-07-24 15:05:00 +0200328 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
York Sun25d65f12017-09-15 08:21:13 -0700329 size = length;
Simon Glass2192e982021-03-07 17:35:14 -0700330 if (gunzip(load_ptr, CONFIG_SYS_BOOTM_LEN, src, &size)) {
York Suna6945fe2017-08-15 11:14:43 -0700331 puts("Uncompressing error\n");
332 return -EIO;
333 }
York Sun25d65f12017-09-15 08:21:13 -0700334 length = size;
Manoj Saia8560992023-09-18 00:56:26 +0530335 } else if (IS_ENABLED(CONFIG_SPL_LZMA) && image_comp == IH_COMP_LZMA) {
336 size = CONFIG_SYS_BOOTM_LEN;
337 ulong loadEnd;
338
339 if (image_decomp(IH_COMP_LZMA, CONFIG_SYS_LOAD_ADDR, 0, 0,
340 load_ptr, src, length, size, &loadEnd)) {
341 puts("Uncompressing error\n");
342 return -EIO;
343 }
344 length = loadEnd - CONFIG_SYS_LOAD_ADDR;
York Suna6945fe2017-08-15 11:14:43 -0700345 } else {
Simon Glass2192e982021-03-07 17:35:14 -0700346 memcpy(load_ptr, src, length);
York Suna6945fe2017-08-15 11:14:43 -0700347 }
Andre Przywara29053e92017-04-26 01:32:36 +0100348
349 if (image_info) {
Michal Simek9e64a552020-09-03 11:24:28 +0200350 ulong entry_point;
351
Andre Przywara29053e92017-04-26 01:32:36 +0100352 image_info->load_addr = load_addr;
353 image_info->size = length;
Michal Simek9e64a552020-09-03 11:24:28 +0200354
355 if (!fit_image_get_entry(fit, node, &entry_point))
356 image_info->entry_point = entry_point;
357 else
358 image_info->entry_point = FDT_ERROR;
Andre Przywara29053e92017-04-26 01:32:36 +0100359 }
360
361 return 0;
362}
363
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600364static bool os_takes_devicetree(uint8_t os)
365{
366 switch (os) {
367 case IH_OS_U_BOOT:
368 return true;
369 case IH_OS_LINUX:
Randolph263e2ae2023-10-12 14:35:07 +0800370 return IS_ENABLED(CONFIG_SPL_OS_BOOT) ||
371 IS_ENABLED(CONFIG_SPL_OPENSBI);
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600372 default:
373 return false;
374 }
375}
376
Marek Vasuta0494272023-09-21 20:44:16 +0200377__weak int board_spl_fit_append_fdt_skip(const char *name)
378{
379 return 0; /* Do not skip */
380}
381
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200382static int spl_fit_append_fdt(struct spl_image_info *spl_image,
383 struct spl_load_info *info, ulong sector,
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600384 const struct spl_fit_info *ctx)
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200385{
386 struct spl_image_info image_info;
Michal Simekbf703df2019-10-22 16:39:11 +0200387 int node, ret = 0, index = 0;
Lukas Auer80afee72019-08-21 21:14:42 +0200388
389 /*
390 * Use the address following the image as target address for the
Marek Vasutc27cbe82020-10-19 23:40:26 +0200391 * device tree.
Lukas Auer80afee72019-08-21 21:14:42 +0200392 */
Marek Vasutc27cbe82020-10-19 23:40:26 +0200393 image_info.load_addr = spl_image->load_addr + spl_image->size;
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200394
395 /* Figure out which device tree the board wants to use */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600396 node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200397 if (node < 0) {
Sean Anderson5ff77722023-10-14 16:47:55 -0400398 size_t size;
399
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200400 debug("%s: cannot find FDT node\n", __func__);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200401
Lukas Auer80afee72019-08-21 21:14:42 +0200402 /*
403 * U-Boot did not find a device tree inside the FIT image. Use
404 * the U-Boot device tree instead.
405 */
Sean Anderson5ff77722023-10-14 16:47:55 -0400406 if (!gd->fdt_blob)
Lukas Auer80afee72019-08-21 21:14:42 +0200407 return node;
Sean Anderson5ff77722023-10-14 16:47:55 -0400408
409 /*
410 * Make the load-address of the FDT available for the SPL
411 * framework
412 */
413 size = fdt_totalsize(gd->fdt_blob);
414 spl_image->fdt_addr = map_sysmem(image_info.load_addr, size);
415 memcpy(spl_image->fdt_addr, gd->fdt_blob, size);
Lukas Auer80afee72019-08-21 21:14:42 +0200416 } else {
Simon Glass205ff7b2023-09-26 08:14:33 -0600417 ret = load_simple_fit(info, sector, ctx, node, &image_info);
Lukas Auer80afee72019-08-21 21:14:42 +0200418 if (ret < 0)
419 return ret;
Sean Anderson5ff77722023-10-14 16:47:55 -0400420
421 spl_image->fdt_addr = phys_to_virt(image_info.load_addr);
Lukas Auer80afee72019-08-21 21:14:42 +0200422 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200423
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600424 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
425 return 0;
426
Tom Rinid259d9c2023-01-10 11:19:28 -0500427#if CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200428 void *tmpbuffer = NULL;
429
Michal Simekbf703df2019-10-22 16:39:11 +0200430 for (; ; index++) {
Marek Vasuta0494272023-09-21 20:44:16 +0200431 const char *str;
432
433 ret = spl_fit_get_image_name(ctx, FIT_FDT_PROP, index, &str);
434 if (ret == -E2BIG) {
Michal Simekbf703df2019-10-22 16:39:11 +0200435 debug("%s: No additional FDT node\n", __func__);
Marek Vasuta0494272023-09-21 20:44:16 +0200436 ret = 0;
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200437 break;
Marek Vasuta0494272023-09-21 20:44:16 +0200438 } else if (ret < 0) {
439 continue;
440 }
441
442 ret = board_spl_fit_append_fdt_skip(str);
443 if (ret)
444 continue;
445
446 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
447 if (node < 0) {
Jean-Jacques Hiblotc6ab6a12019-10-22 16:39:14 +0200448 debug("%s: unable to find FDT node %d\n",
449 __func__, index);
450 continue;
Michal Simekbf703df2019-10-22 16:39:11 +0200451 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200452
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200453 if (!tmpbuffer) {
454 /*
455 * allocate memory to store the DT overlay
456 * before it is applied. It may not be used
457 * depending on how the overlay is stored, so
458 * don't fail yet if the allocation failed.
459 */
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +0200460 size_t size = CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ;
461
462 tmpbuffer = malloc_cache_aligned(size);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200463 if (!tmpbuffer)
464 debug("%s: unable to allocate space for overlays\n",
465 __func__);
466 }
467 image_info.load_addr = (ulong)tmpbuffer;
Simon Glass205ff7b2023-09-26 08:14:33 -0600468 ret = load_simple_fit(info, sector, ctx, node,
469 &image_info);
Michal Simekbf703df2019-10-22 16:39:11 +0200470 if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200471 break;
Michal Simekbf703df2019-10-22 16:39:11 +0200472
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200473 /* Make room in FDT for changes from the overlay */
474 ret = fdt_increase_size(spl_image->fdt_addr,
475 image_info.size);
476 if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200477 break;
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200478
Michal Simekbf703df2019-10-22 16:39:11 +0200479 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
480 (void *)image_info.load_addr);
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200481 if (ret) {
482 pr_err("failed to apply DT overlay %s\n",
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600483 fit_get_name(ctx->fit, node, NULL));
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200484 break;
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200485 }
Michal Simekbf703df2019-10-22 16:39:11 +0200486
487 debug("%s: DT overlay %s applied\n", __func__,
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600488 fit_get_name(ctx->fit, node, NULL));
Michal Simekbf703df2019-10-22 16:39:11 +0200489 }
Heinrich Schuchardt939a9612020-04-20 12:44:01 +0200490 free(tmpbuffer);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200491 if (ret)
492 return ret;
Tom Rinid259d9c2023-01-10 11:19:28 -0500493#endif
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200494 /* Try to make space, so we can inject details on the loadables */
495 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
496 if (ret < 0)
497 return ret;
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200498
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200499 return ret;
500}
501
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600502static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200503 void *blob, struct spl_image_info *image)
504{
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100505 int ret = 0;
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200506 const char *name;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100507 int node;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200508
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600509 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
510 return 0;
511
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600512 ret = spl_fit_get_image_name(ctx, "loadables", index, &name);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200513 if (ret < 0)
514 return ret;
515
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600516 node = spl_fit_get_image_node(ctx, "loadables", index);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200517
518 ret = fdt_record_loadable(blob, index, name, image->load_addr,
Simon Glassdff90432023-09-26 08:14:35 -0600519 image->size, image->entry_point,
520 fdt_getprop(ctx->fit, node, FIT_TYPE_PROP, NULL),
521 fdt_getprop(ctx->fit, node, FIT_OS_PROP, NULL),
522 fdt_getprop(ctx->fit, node, FIT_ARCH_PROP, NULL));
523
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200524 return ret;
525}
526
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500527static int spl_fit_image_is_fpga(const void *fit, int node)
528{
529 const char *type;
530
531 if (!IS_ENABLED(CONFIG_SPL_FPGA))
532 return 0;
533
534 type = fdt_getprop(fit, node, FIT_TYPE_PROP, NULL);
535 if (!type)
536 return 0;
537
538 return !strcmp(type, "fpga");
539}
540
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100541static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
542{
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600543 if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT))
544 return fit_image_get_os(fit, noffset, os);
Samuel Hollande646f512020-10-21 21:12:13 -0500545
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600546 const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
Samuel Hollande646f512020-10-21 21:12:13 -0500547 if (!name)
548 return -ENOENT;
549
550 /*
551 * We don't care what the type of the image actually is,
552 * only whether or not it is U-Boot. This saves some
553 * space by omitting the large table of OS types.
554 */
555 if (!strcmp(name, "u-boot"))
556 *os = IH_OS_U_BOOT;
557 else
558 *os = IH_OS_INVALID;
559
560 return 0;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100561}
562
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500563/*
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500564 * The purpose of the FIT load buffer is to provide a memory location that is
565 * independent of the load address of any FIT component.
566 */
567static void *spl_get_fit_load_buffer(size_t size)
568{
569 void *buf;
570
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +0200571 buf = malloc_cache_aligned(size);
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500572 if (!buf) {
573 pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size);
Simon Glass67e3fca2023-09-26 08:14:16 -0600574 pr_err("\tcheck CONFIG_SPL_SYS_MALLOC_SIZE\n");
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500575 buf = spl_get_load_buffer(0, size);
576 }
577 return buf;
578}
579
Heiko Schocher7d7c3672021-08-17 08:17:18 +0200580__weak void *board_spl_fit_buffer_addr(ulong fit_size, int sectors, int bl_len)
581{
582 return spl_get_fit_load_buffer(sectors * bl_len);
583}
584
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500585/*
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500586 * Weak default function to allow customizing SPL fit loading for load-only
587 * use cases by allowing to skip the parsing/processing of the FIT contents
588 * (so that this can be done separately in a more customized fashion)
589 */
590__weak bool spl_load_simple_fit_skip_processing(void)
591{
592 return false;
593}
594
Heiko Schocher67e8f5d2021-08-06 06:44:26 +0200595/*
596 * Weak default function to allow fixes after fit header
597 * is loaded.
598 */
599__weak void *spl_load_simple_fit_fix_load(const void *fit)
600{
601 return (void *)fit;
602}
603
Alexandru Gagniuc69a99662021-03-29 12:05:13 -0500604static void warn_deprecated(const char *msg)
605{
606 printf("DEPRECATED: %s\n", msg);
607 printf("\tSee doc/uImage.FIT/source_file_format.txt\n");
608}
609
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500610static int spl_fit_upload_fpga(struct spl_fit_info *ctx, int node,
611 struct spl_image_info *fpga_image)
612{
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500613 const char *compatible;
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500614 int ret;
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300615 int devnum = 0;
616 int flags = 0;
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500617
618 debug("FPGA bitstream at: %x, size: %x\n",
619 (u32)fpga_image->load_addr, fpga_image->size);
620
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500621 compatible = fdt_getprop(ctx->fit, node, "compatible", NULL);
Oleksandr Suvorov2fddf3e2022-07-22 17:16:09 +0300622 if (!compatible) {
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500623 warn_deprecated("'fpga' image without 'compatible' property");
Oleksandr Suvorov2fddf3e2022-07-22 17:16:09 +0300624 } else {
625 if (CONFIG_IS_ENABLED(FPGA_LOAD_SECURE))
626 flags = fpga_compatible2flag(devnum, compatible);
627 if (strcmp(compatible, "u-boot,fpga-legacy"))
628 debug("Ignoring compatible = %s property\n",
629 compatible);
630 }
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500631
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300632 ret = fpga_load(devnum, (void *)fpga_image->load_addr,
633 fpga_image->size, BIT_FULL, flags);
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500634 if (ret) {
635 printf("%s: Cannot load the image to the FPGA\n", __func__);
636 return ret;
637 }
638
639 puts("FPGA image loaded from FIT\n");
640
641 return 0;
642}
643
644static int spl_fit_load_fpga(struct spl_fit_info *ctx,
645 struct spl_load_info *info, ulong sector)
646{
647 int node, ret;
648
649 struct spl_image_info fpga_image = {
650 .load_addr = 0,
651 };
652
653 node = spl_fit_get_image_node(ctx, "fpga", 0);
654 if (node < 0)
655 return node;
656
Alexandru Gagniuc69a99662021-03-29 12:05:13 -0500657 warn_deprecated("'fpga' property in config node. Use 'loadables'");
658
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500659 /* Load the image and set up the fpga_image structure */
Simon Glass205ff7b2023-09-26 08:14:33 -0600660 ret = load_simple_fit(info, sector, ctx, node, &fpga_image);
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500661 if (ret) {
662 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
663 return ret;
664 }
665
666 return spl_fit_upload_fpga(ctx, node, &fpga_image);
667}
668
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600669static int spl_simple_fit_read(struct spl_fit_info *ctx,
670 struct spl_load_info *info, ulong sector,
671 const void *fit_header)
Simon Glassa6131a22016-02-22 22:55:56 -0700672{
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600673 unsigned long count, size;
Simon Glassa6131a22016-02-22 22:55:56 -0700674 int sectors;
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600675 void *buf;
Simon Glassa6131a22016-02-22 22:55:56 -0700676
677 /*
York Suna058b8a2017-08-15 11:14:45 -0700678 * For FIT with external data, figure out where the external images
679 * start. This is the base for the data-offset properties in each
680 * image.
Simon Glassa6131a22016-02-22 22:55:56 -0700681 */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600682 size = ALIGN(fdt_totalsize(fit_header), 4);
Ye Li9d5b0f42018-11-17 09:10:25 +0000683 size = board_spl_fit_size_align(size);
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600684 ctx->ext_data_offset = ALIGN(size, 4);
Simon Glassa6131a22016-02-22 22:55:56 -0700685
686 /*
687 * So far we only have one block of data from the FIT. Read the entire
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500688 * thing, including that first block.
York Suna058b8a2017-08-15 11:14:45 -0700689 *
690 * For FIT with data embedded, data is loaded as part of FIT image.
691 * For FIT with external data, data is not loaded in this step.
Simon Glassa6131a22016-02-22 22:55:56 -0700692 */
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530693 sectors = get_aligned_image_size(info, size, 0);
Heiko Schocher7d7c3672021-08-17 08:17:18 +0200694 buf = board_spl_fit_buffer_addr(size, sectors, info->bl_len);
Ye Li9d5b0f42018-11-17 09:10:25 +0000695
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600696 count = info->read(info, sector, sectors, buf);
697 ctx->fit = buf;
698 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
699 sector, sectors, buf, count, size);
Simon Glassa6131a22016-02-22 22:55:56 -0700700
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600701 return (count == 0) ? -EIO : 0;
702}
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500703
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600704static int spl_simple_fit_parse(struct spl_fit_info *ctx)
705{
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600706 /* Find the correct subnode under "/configurations" */
707 ctx->conf_node = fit_find_config_node(ctx->fit);
708 if (ctx->conf_node < 0)
709 return -EINVAL;
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100710
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600711 if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100712 printf("## Checking hash(es) for config %s ... ",
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600713 fit_get_name(ctx->fit, ctx->conf_node, NULL));
714 if (fit_config_verify(ctx->fit, ctx->conf_node))
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100715 return -EPERM;
716 puts("OK\n");
717 }
718
Andre Przywara525e9c92017-04-26 01:32:34 +0100719 /* find the node holding the images information */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600720 ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
721 if (ctx->images_node < 0) {
722 debug("%s: Cannot find /images node: %d\n", __func__,
723 ctx->images_node);
724 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700725 }
Marek Vasut33dd00e2018-05-12 22:25:28 +0200726
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600727 return 0;
728}
729
730int spl_load_simple_fit(struct spl_image_info *spl_image,
731 struct spl_load_info *info, ulong sector, void *fit)
732{
733 struct spl_image_info image_info;
734 struct spl_fit_info ctx;
735 int node = -1;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600736 int ret;
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600737 int index = 0;
738 int firmware_node;
739
740 ret = spl_simple_fit_read(&ctx, info, sector, fit);
741 if (ret < 0)
742 return ret;
743
744 /* skip further processing if requested to enable load-only use cases */
745 if (spl_load_simple_fit_skip_processing())
746 return 0;
747
Heiko Schocher67e8f5d2021-08-06 06:44:26 +0200748 ctx.fit = spl_load_simple_fit_fix_load(ctx.fit);
749
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600750 ret = spl_simple_fit_parse(&ctx);
751 if (ret < 0)
752 return ret;
753
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500754 if (IS_ENABLED(CONFIG_SPL_FPGA))
755 spl_fit_load_fpga(&ctx, info, sector);
Andre Przywara525e9c92017-04-26 01:32:34 +0100756
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200757 /*
758 * Find the U-Boot image using the following search order:
759 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
760 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
761 * - fall back to using the first 'loadables' entry
762 */
763 if (node < 0)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600764 node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600765
766 if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT))
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600767 node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600768
Andre Przywara525e9c92017-04-26 01:32:34 +0100769 if (node < 0) {
770 debug("could not find firmware image, trying loadables...\n");
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600771 node = spl_fit_get_image_node(&ctx, "loadables", 0);
Andre Przywarae7c58562017-04-26 01:32:37 +0100772 /*
773 * If we pick the U-Boot image from "loadables", start at
774 * the second image when later loading additional images.
775 */
776 index = 1;
Andre Przywara525e9c92017-04-26 01:32:34 +0100777 }
Simon Glassa6131a22016-02-22 22:55:56 -0700778 if (node < 0) {
Andre Przywara525e9c92017-04-26 01:32:34 +0100779 debug("%s: Cannot find u-boot image node: %d\n",
780 __func__, node);
Simon Glassa6131a22016-02-22 22:55:56 -0700781 return -1;
782 }
783
Andre Przywara29053e92017-04-26 01:32:36 +0100784 /* Load the image and set up the spl_image structure */
Simon Glass205ff7b2023-09-26 08:14:33 -0600785 ret = load_simple_fit(info, sector, &ctx, node, spl_image);
Andre Przywara29053e92017-04-26 01:32:36 +0100786 if (ret)
787 return ret;
Daniel Allredcf839bd2016-06-27 09:19:21 -0500788
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200789 /*
790 * For backward compatibility, we treat the first node that is
791 * as a U-Boot image, if no OS-type has been declared.
792 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600793 if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
York Suna058b8a2017-08-15 11:14:45 -0700794 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600795 else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200796 spl_image->os = IH_OS_U_BOOT;
Simon Glassa6131a22016-02-22 22:55:56 -0700797
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200798 /*
799 * Booting a next-stage U-Boot may require us to append the FDT.
800 * We allow this to fail, as the U-Boot image might embed its FDT.
801 */
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600802 if (os_takes_devicetree(spl_image->os)) {
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600803 ret = spl_fit_append_fdt(spl_image, info, sector, &ctx);
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600804 if (ret < 0 && spl_image->os != IH_OS_U_BOOT)
Dario Binacchifaf8d142020-05-27 13:56:19 +0200805 return ret;
806 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100807
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200808 firmware_node = node;
Andre Przywarae7c58562017-04-26 01:32:37 +0100809 /* Now check if there are more images for us to load */
810 for (; ; index++) {
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200811 uint8_t os_type = IH_OS_INVALID;
812
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600813 node = spl_fit_get_image_node(&ctx, "loadables", index);
Andre Przywarae7c58562017-04-26 01:32:37 +0100814 if (node < 0)
815 break;
816
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200817 /*
818 * if the firmware is also a loadable, skip it because
819 * it already has been loaded. This is typically the case with
820 * u-boot.img generated by mkimage.
821 */
822 if (firmware_node == node)
823 continue;
824
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500825 image_info.load_addr = 0;
Simon Glass205ff7b2023-09-26 08:14:33 -0600826 ret = load_simple_fit(info, sector, &ctx, node, &image_info);
Philippe Reynesba87da42020-11-24 16:15:05 +0100827 if (ret < 0) {
828 printf("%s: can't load image loadables index %d (ret = %d)\n",
829 __func__, index, ret);
830 return ret;
831 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100832
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500833 if (spl_fit_image_is_fpga(ctx.fit, node))
834 spl_fit_upload_fpga(&ctx, node, &image_info);
835
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600836 if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200837 debug("Loadable is %s\n", genimg_get_os_name(os_type));
838
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600839 if (os_takes_devicetree(os_type)) {
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600840 spl_fit_append_fdt(&image_info, info, sector, &ctx);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200841 spl_image->fdt_addr = image_info.fdt_addr;
842 }
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200843
Andre Przywarae7c58562017-04-26 01:32:37 +0100844 /*
845 * If the "firmware" image did not provide an entry point,
846 * use the first valid entry point from the loadables.
847 */
848 if (spl_image->entry_point == FDT_ERROR &&
849 image_info.entry_point != FDT_ERROR)
850 spl_image->entry_point = image_info.entry_point;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200851
852 /* Record our loadables into the FDT */
853 if (spl_image->fdt_addr)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600854 spl_fit_record_loadable(&ctx, index,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200855 spl_image->fdt_addr,
856 &image_info);
Andre Przywarae7c58562017-04-26 01:32:37 +0100857 }
858
859 /*
Jesse Taube93ee5c82023-08-24 21:59:48 -0400860 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
Andre Przywarae7c58562017-04-26 01:32:37 +0100861 * Makefile will set it to 0 and it will end up as the entry point
862 * here. What it actually means is: use the load address.
863 */
864 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
865 spl_image->entry_point = spl_image->load_addr;
866
Ye Li9d5b0f42018-11-17 09:10:25 +0000867 spl_image->flags |= SPL_FIT_FOUND;
868
Andre Przywarae7c58562017-04-26 01:32:37 +0100869 return 0;
Simon Glassa6131a22016-02-22 22:55:56 -0700870}
Simon Glassc0bd55e2023-09-26 08:14:34 -0600871
872/* Parse and load full fitImage in SPL */
873int spl_load_fit_image(struct spl_image_info *spl_image,
874 const struct legacy_img_hdr *header)
875{
876 struct bootm_headers images;
877 const char *fit_uname_config = NULL;
878 uintptr_t fdt_hack;
879 const char *uname;
880 ulong fw_data = 0, dt_data = 0, img_data = 0;
881 ulong fw_len = 0, dt_len = 0, img_len = 0;
882 int idx, conf_noffset;
883 int ret;
884
885#ifdef CONFIG_SPL_FIT_SIGNATURE
886 images.verify = 1;
887#endif
Sean Anderson5ff77722023-10-14 16:47:55 -0400888 ret = fit_image_load(&images, virt_to_phys((void *)header),
Simon Glassc0bd55e2023-09-26 08:14:34 -0600889 NULL, &fit_uname_config,
890 IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
891 FIT_LOAD_OPTIONAL, &fw_data, &fw_len);
892 if (ret >= 0) {
893 printf("DEPRECATED: 'standalone = ' property.");
894 printf("Please use either 'firmware =' or 'kernel ='\n");
895 } else {
Sean Anderson5ff77722023-10-14 16:47:55 -0400896 ret = fit_image_load(&images, virt_to_phys((void *)header),
897 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
Simon Glassc0bd55e2023-09-26 08:14:34 -0600898 IH_TYPE_FIRMWARE, -1, FIT_LOAD_OPTIONAL,
899 &fw_data, &fw_len);
900 }
901
902 if (ret < 0) {
Sean Anderson5ff77722023-10-14 16:47:55 -0400903 ret = fit_image_load(&images, virt_to_phys((void *)header),
904 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
Simon Glassc0bd55e2023-09-26 08:14:34 -0600905 IH_TYPE_KERNEL, -1, FIT_LOAD_OPTIONAL,
906 &fw_data, &fw_len);
907 }
908
909 if (ret < 0)
910 return ret;
911
912 spl_image->size = fw_len;
Simon Glassc0bd55e2023-09-26 08:14:34 -0600913 spl_image->load_addr = fw_data;
Sean Andersone99b2cd2023-10-14 16:47:39 -0400914 if (fit_image_get_entry(header, ret, &spl_image->entry_point))
915 spl_image->entry_point = fw_data;
Simon Glassc0bd55e2023-09-26 08:14:34 -0600916 if (fit_image_get_os(header, ret, &spl_image->os))
917 spl_image->os = IH_OS_INVALID;
918 spl_image->name = genimg_get_os_name(spl_image->os);
919
920 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
921 spl_image->name, spl_image->load_addr, spl_image->size);
922
923#ifdef CONFIG_SPL_FIT_SIGNATURE
924 images.verify = 1;
925#endif
Sean Anderson5ff77722023-10-14 16:47:55 -0400926 ret = fit_image_load(&images, virt_to_phys((void *)header), NULL,
927 &fit_uname_config, IH_ARCH_DEFAULT, IH_TYPE_FLATDT,
928 -1, FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
Simon Glassc0bd55e2023-09-26 08:14:34 -0600929 if (ret >= 0) {
930 spl_image->fdt_addr = (void *)dt_data;
931
932 if (spl_image->os == IH_OS_U_BOOT) {
933 /* HACK: U-Boot expects FDT at a specific address */
934 fdt_hack = spl_image->load_addr + spl_image->size;
935 fdt_hack = (fdt_hack + 3) & ~3;
936 debug("Relocating FDT to %p\n", spl_image->fdt_addr);
937 memcpy((void *)fdt_hack, spl_image->fdt_addr, dt_len);
938 }
939 }
940
941 conf_noffset = fit_conf_get_node((const void *)header,
942 fit_uname_config);
943 if (conf_noffset < 0)
944 return 0;
945
946 for (idx = 0;
947 uname = fdt_stringlist_get((const void *)header, conf_noffset,
948 FIT_LOADABLE_PROP, idx,
949 NULL), uname;
950 idx++) {
951#ifdef CONFIG_SPL_FIT_SIGNATURE
952 images.verify = 1;
953#endif
954 ret = fit_image_load(&images, (ulong)header,
955 &uname, &fit_uname_config,
956 IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
957 FIT_LOAD_OPTIONAL_NON_ZERO,
958 &img_data, &img_len);
959 if (ret < 0)
960 return ret;
961 }
962
963 return 0;
964}