blob: 988125be008d56d68fd1ac7da5c1d36ca07db406 [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
Simon Glassa6131a22016-02-22 22:55:56 -07007#include <errno.h>
Michal Simekf707b5a2018-07-18 14:33:15 +02008#include <fpga.h>
Simon Glass1a974af2019-08-01 09:46:36 -06009#include <gzip.h>
Simon Glassa6131a22016-02-22 22:55:56 -070010#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +020012#include <memalign.h>
Simon Glass2192e982021-03-07 17:35:14 -070013#include <mapmem.h>
Simon Glassa6131a22016-02-22 22:55:56 -070014#include <spl.h>
Simon Glass458b66a2020-11-05 06:32:05 -070015#include <sysinfo.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040017#include <asm/io.h>
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +020018#include <linux/libfdt.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060019#include <linux/printk.h>
Simon Glassa6131a22016-02-22 22:55:56 -070020
Lukas Auer80afee72019-08-21 21:14:42 +020021DECLARE_GLOBAL_DATA_PTR;
22
Alexandru Gagniuc23243c92021-01-20 10:46:50 -060023struct spl_fit_info {
24 const void *fit; /* Pointer to a valid FIT blob */
25 size_t ext_data_offset; /* Offset to FIT external data (end of FIT) */
26 int images_node; /* FDT offset to "/images" node */
Alexandru Gagniucfa11a442021-01-20 10:46:53 -060027 int conf_node; /* FDT offset to selected configuration node */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -060028};
29
Ye Li9d5b0f42018-11-17 09:10:25 +000030__weak ulong board_spl_fit_size_align(ulong size)
31{
32 return size;
33}
34
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020035static int find_node_from_desc(const void *fit, int node, const char *str)
36{
37 int child;
38
39 if (node < 0)
40 return -EINVAL;
41
42 /* iterate the FIT nodes and find a matching description */
43 for (child = fdt_first_subnode(fit, node); child >= 0;
44 child = fdt_next_subnode(fit, child)) {
45 int len;
Simon Glassdff90432023-09-26 08:14:35 -060046 const char *desc = fdt_getprop(fit, child, FIT_DESC_PROP, &len);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020047
48 if (!desc)
49 continue;
50
51 if (!strcmp(desc, str))
52 return child;
53 }
54
55 return -ENOENT;
56}
57
Andre Przywara525e9c92017-04-26 01:32:34 +010058/**
Philipp Tomsiche61eff92017-09-13 21:29:34 +020059 * spl_fit_get_image_name(): By using the matching configuration subnode,
Andre Przywara525e9c92017-04-26 01:32:34 +010060 * retrieve the name of an image, specified by a property name and an index
61 * into that.
62 * @fit: Pointer to the FDT blob.
63 * @images: Offset of the /images subnode.
64 * @type: Name of the property within the configuration subnode.
65 * @index: Index into the list of strings in this property.
Philipp Tomsiche61eff92017-09-13 21:29:34 +020066 * @outname: Name of the image
Andre Przywara525e9c92017-04-26 01:32:34 +010067 *
Philipp Tomsiche61eff92017-09-13 21:29:34 +020068 * Return: 0 on success, or a negative error number
Andre Przywara525e9c92017-04-26 01:32:34 +010069 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -060070static int spl_fit_get_image_name(const struct spl_fit_info *ctx,
Philipp Tomsiche61eff92017-09-13 21:29:34 +020071 const char *type, int index,
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +020072 const char **outname)
Andre Przywara1e329b62017-04-26 01:32:33 +010073{
Simon Glass458b66a2020-11-05 06:32:05 -070074 struct udevice *sysinfo;
Andre Przywara1e329b62017-04-26 01:32:33 +010075 const char *name, *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +020076 __maybe_unused int node;
Andre Przywara1e329b62017-04-26 01:32:33 +010077 int len, i;
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020078 bool found = true;
Andre Przywara1e329b62017-04-26 01:32:33 +010079
Alexandru Gagniucfa11a442021-01-20 10:46:53 -060080 name = fdt_getprop(ctx->fit, ctx->conf_node, type, &len);
Andre Przywara1e329b62017-04-26 01:32:33 +010081 if (!name) {
82 debug("cannot find property '%s': %d\n", type, len);
83 return -EINVAL;
84 }
Simon Glassa6131a22016-02-22 22:55:56 -070085
Andre Przywara1e329b62017-04-26 01:32:33 +010086 str = name;
87 for (i = 0; i < index; i++) {
88 str = strchr(str, '\0') + 1;
89 if (!str || (str - name >= len)) {
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020090 found = false;
91 break;
Andre Przywara1e329b62017-04-26 01:32:33 +010092 }
Simon Glassa6131a22016-02-22 22:55:56 -070093 }
94
Simon Glass458b66a2020-11-05 06:32:05 -070095 if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) {
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020096 int rc;
97 /*
Simon Glass458b66a2020-11-05 06:32:05 -070098 * no string in the property for this index. Check if the
99 * sysinfo-level code can supply one.
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200100 */
Sean Anderson6ccf10a2021-04-20 10:50:56 -0400101 rc = sysinfo_detect(sysinfo);
102 if (rc)
103 return rc;
104
Simon Glass458b66a2020-11-05 06:32:05 -0700105 rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
106 &str);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200107 if (rc && rc != -ENOENT)
108 return rc;
109
110 if (!rc) {
111 /*
Simon Glass458b66a2020-11-05 06:32:05 -0700112 * The sysinfo provided a name for a loadable.
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200113 * Try to match it against the description properties
114 * first. If no matching node is found, use it as a
115 * node name.
116 */
117 int node;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600118 int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200119
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600120 node = find_node_from_desc(ctx->fit, images, str);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200121 if (node > 0)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600122 str = fdt_get_name(ctx->fit, node, NULL);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200123
124 found = true;
125 }
126 }
127
128 if (!found) {
129 debug("no string for index %d\n", index);
130 return -E2BIG;
131 }
132
133 *outname = str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200134 return 0;
135}
136
137/**
138 * spl_fit_get_image_node(): By using the matching configuration subnode,
139 * retrieve the name of an image, specified by a property name and an index
140 * into that.
141 * @fit: Pointer to the FDT blob.
142 * @images: Offset of the /images subnode.
143 * @type: Name of the property within the configuration subnode.
144 * @index: Index into the list of strings in this property.
145 *
146 * Return: the node offset of the respective image node or a negative
147 * error number.
148 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600149static int spl_fit_get_image_node(const struct spl_fit_info *ctx,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200150 const char *type, int index)
151{
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200152 const char *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200153 int err;
154 int node;
155
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600156 err = spl_fit_get_image_name(ctx, type, index, &str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200157 if (err)
158 return err;
159
Andre Przywara1e329b62017-04-26 01:32:33 +0100160 debug("%s: '%s'\n", type, str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200161
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600162 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
Andre Przywara1e329b62017-04-26 01:32:33 +0100163 if (node < 0) {
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200164 pr_err("cannot find image node '%s': %d\n", str, node);
Andre Przywara1e329b62017-04-26 01:32:33 +0100165 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700166 }
Simon Glassa6131a22016-02-22 22:55:56 -0700167
Andre Przywara525e9c92017-04-26 01:32:34 +0100168 return node;
Simon Glassa6131a22016-02-22 22:55:56 -0700169}
170
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530171static int get_aligned_image_offset(struct spl_load_info *info, int offset)
172{
Sean Anderson35f15fe2023-11-08 11:48:43 -0500173 return ALIGN_DOWN(offset, spl_get_bl_len(info));
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530174}
175
176static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
177{
Sean Anderson35f15fe2023-11-08 11:48:43 -0500178 return offset & (spl_get_bl_len(info) - 1);
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530179}
180
181static int get_aligned_image_size(struct spl_load_info *info, int data_size,
182 int offset)
183{
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530184 data_size = data_size + get_aligned_image_overhead(info, offset);
185
Sean Anderson35f15fe2023-11-08 11:48:43 -0500186 return ALIGN(data_size, spl_get_bl_len(info));
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530187}
188
Andre Przywara29053e92017-04-26 01:32:36 +0100189/**
Simon Glass205ff7b2023-09-26 08:14:33 -0600190 * load_simple_fit(): load the image described in a certain FIT node
Andre Przywara29053e92017-04-26 01:32:36 +0100191 * @info: points to information about the device to load data from
192 * @sector: the start sector of the FIT image on the device
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600193 * @ctx: points to the FIT context structure
Andre Przywara29053e92017-04-26 01:32:36 +0100194 * @node: offset of the DT node describing the image to load (relative
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200195 * to @fit)
Andre Przywara29053e92017-04-26 01:32:36 +0100196 * @image_info: will be filled with information about the loaded image
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200197 * If the FIT node does not contain a "load" (address) property,
198 * the image gets loaded to the address pointed to by the
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500199 * load_addr member in this struct, if load_addr is not 0
Andre Przywara29053e92017-04-26 01:32:36 +0100200 *
201 * Return: 0 on success or a negative error number.
202 */
Sean Anderson7d8d6132023-11-08 11:48:40 -0500203static int load_simple_fit(struct spl_load_info *info, ulong fit_offset,
Simon Glass205ff7b2023-09-26 08:14:33 -0600204 const struct spl_fit_info *ctx, int node,
205 struct spl_image_info *image_info)
Andre Przywara29053e92017-04-26 01:32:36 +0100206{
Michal Simekf707b5a2018-07-18 14:33:15 +0200207 int offset;
Andre Przywara29053e92017-04-26 01:32:36 +0100208 size_t length;
York Sunadf99fa2017-08-15 11:14:44 -0700209 int len;
York Sun25d65f12017-09-15 08:21:13 -0700210 ulong size;
Simon Glass2192e982021-03-07 17:35:14 -0700211 ulong load_addr;
212 void *load_ptr;
Andre Przywara29053e92017-04-26 01:32:36 +0100213 void *src;
214 ulong overhead;
York Suna6945fe2017-08-15 11:14:43 -0700215 uint8_t image_comp = -1, type = -1;
York Sunadf99fa2017-08-15 11:14:44 -0700216 const void *data;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600217 const void *fit = ctx->fit;
Peng Fan8876c7e2017-12-05 13:20:59 +0800218 bool external_data = false;
York Suna6945fe2017-08-15 11:14:43 -0700219
Michal Simek1aab1142020-09-09 14:41:56 +0200220 if (IS_ENABLED(CONFIG_SPL_FPGA) ||
Manoj Sai2b2e6282023-09-18 00:56:25 +0530221 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && spl_decompression_enabled())) {
Marek Vasut72bc5d52018-06-01 23:19:29 +0200222 if (fit_image_get_type(fit, node, &type))
223 puts("Cannot get image type.\n");
224 else
225 debug("%s ", genimg_get_type_name(type));
226 }
227
Manoj Sai2b2e6282023-09-18 00:56:25 +0530228 if (spl_decompression_enabled()) {
Klaus H. Sorensen4a9a0422019-12-11 11:03:33 +0000229 fit_image_get_comp(fit, node, &image_comp);
230 debug("%s ", genimg_get_comp_name(image_comp));
York Suna6945fe2017-08-15 11:14:43 -0700231 }
Andre Przywara29053e92017-04-26 01:32:36 +0100232
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500233 if (fit_image_get_load(fit, node, &load_addr)) {
234 if (!image_info->load_addr) {
235 printf("Can't load %s: No load address and no buffer\n",
236 fit_get_name(fit, node, NULL));
237 return -ENOBUFS;
238 }
Andre Przywara29053e92017-04-26 01:32:36 +0100239 load_addr = image_info->load_addr;
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500240 }
Andre Przywara29053e92017-04-26 01:32:36 +0100241
Peng Fan8876c7e2017-12-05 13:20:59 +0800242 if (!fit_image_get_data_position(fit, node, &offset)) {
243 external_data = true;
244 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600245 offset += ctx->ext_data_offset;
Peng Fan8876c7e2017-12-05 13:20:59 +0800246 external_data = true;
247 }
248
249 if (external_data) {
Simon Glass2192e982021-03-07 17:35:14 -0700250 void *src_ptr;
251
Peng Fan8876c7e2017-12-05 13:20:59 +0800252 /* External data */
York Sunadf99fa2017-08-15 11:14:44 -0700253 if (fit_image_get_data_size(fit, node, &len))
254 return -ENOENT;
Andre Przywara29053e92017-04-26 01:32:36 +0100255
Nishanth Menone4cb6452021-10-19 12:32:29 -0500256 /* Dont bother to copy 0 byte data, but warn, though */
257 if (!len) {
258 log_warning("%s: Skip load '%s': image size is 0!\n",
259 __func__, fit_get_name(fit, node, NULL));
260 return 0;
261 }
262
Manoj Saia8560992023-09-18 00:56:26 +0530263 if (spl_decompression_enabled() &&
264 (image_comp == IH_COMP_GZIP || image_comp == IH_COMP_LZMA))
Manoj Sai2b2e6282023-09-18 00:56:25 +0530265 src_ptr = map_sysmem(ALIGN(CONFIG_SYS_LOAD_ADDR, ARCH_DMA_MINALIGN), len);
266 else
267 src_ptr = map_sysmem(ALIGN(load_addr, ARCH_DMA_MINALIGN), len);
York Sunadf99fa2017-08-15 11:14:44 -0700268 length = len;
269
270 overhead = get_aligned_image_overhead(info, offset);
Sean Anderson7d8d6132023-11-08 11:48:40 -0500271 size = get_aligned_image_size(info, length, offset);
York Sunadf99fa2017-08-15 11:14:44 -0700272
Michal Simekf707b5a2018-07-18 14:33:15 +0200273 if (info->read(info,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500274 fit_offset +
275 get_aligned_image_offset(info, offset), size,
Sean Andersonb27c5f82023-11-08 11:48:41 -0500276 src_ptr) < length)
York Sunadf99fa2017-08-15 11:14:44 -0700277 return -EIO;
278
Simon Glass2192e982021-03-07 17:35:14 -0700279 debug("External data: dst=%p, offset=%x, size=%lx\n",
280 src_ptr, offset, (unsigned long)length);
281 src = src_ptr + overhead;
York Sunadf99fa2017-08-15 11:14:44 -0700282 } else {
283 /* Embedded data */
284 if (fit_image_get_data(fit, node, &data, &length)) {
285 puts("Cannot get image data/size\n");
286 return -ENOENT;
287 }
288 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
289 (unsigned long)length);
Simon Glass2192e982021-03-07 17:35:14 -0700290 src = (void *)data; /* cast away const */
York Sunadf99fa2017-08-15 11:14:44 -0700291 }
Andre Przywara29053e92017-04-26 01:32:36 +0100292
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600293 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
294 printf("## Checking hash(es) for Image %s ... ",
295 fit_get_name(fit, node, NULL));
Simon Glasse10e2ee2021-11-12 12:28:10 -0700296 if (!fit_image_verify_with_data(fit, node, gd_fdt_blob(), src,
297 length))
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600298 return -EPERM;
299 puts("OK\n");
300 }
Ben Whitten54a91192018-06-07 11:37:27 +0100301
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600302 if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS))
Lokesh Vutlab36dd3e2021-06-11 11:45:05 +0300303 board_fit_image_post_process(fit, node, &src, &length);
Andre Przywara29053e92017-04-26 01:32:36 +0100304
Simon Glass2192e982021-03-07 17:35:14 -0700305 load_ptr = map_sysmem(load_addr, length);
Michal Simekf354c3f2018-07-24 15:05:00 +0200306 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
York Sun25d65f12017-09-15 08:21:13 -0700307 size = length;
Simon Glass2192e982021-03-07 17:35:14 -0700308 if (gunzip(load_ptr, CONFIG_SYS_BOOTM_LEN, src, &size)) {
York Suna6945fe2017-08-15 11:14:43 -0700309 puts("Uncompressing error\n");
310 return -EIO;
311 }
York Sun25d65f12017-09-15 08:21:13 -0700312 length = size;
Manoj Saia8560992023-09-18 00:56:26 +0530313 } else if (IS_ENABLED(CONFIG_SPL_LZMA) && image_comp == IH_COMP_LZMA) {
314 size = CONFIG_SYS_BOOTM_LEN;
315 ulong loadEnd;
316
317 if (image_decomp(IH_COMP_LZMA, CONFIG_SYS_LOAD_ADDR, 0, 0,
318 load_ptr, src, length, size, &loadEnd)) {
319 puts("Uncompressing error\n");
320 return -EIO;
321 }
322 length = loadEnd - CONFIG_SYS_LOAD_ADDR;
York Suna6945fe2017-08-15 11:14:43 -0700323 } else {
Simon Glass2192e982021-03-07 17:35:14 -0700324 memcpy(load_ptr, src, length);
York Suna6945fe2017-08-15 11:14:43 -0700325 }
Andre Przywara29053e92017-04-26 01:32:36 +0100326
327 if (image_info) {
Michal Simek9e64a552020-09-03 11:24:28 +0200328 ulong entry_point;
329
Andre Przywara29053e92017-04-26 01:32:36 +0100330 image_info->load_addr = load_addr;
331 image_info->size = length;
Michal Simek9e64a552020-09-03 11:24:28 +0200332
333 if (!fit_image_get_entry(fit, node, &entry_point))
334 image_info->entry_point = entry_point;
335 else
336 image_info->entry_point = FDT_ERROR;
Andre Przywara29053e92017-04-26 01:32:36 +0100337 }
338
339 return 0;
340}
341
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600342static bool os_takes_devicetree(uint8_t os)
343{
344 switch (os) {
345 case IH_OS_U_BOOT:
346 return true;
347 case IH_OS_LINUX:
Randolph263e2ae2023-10-12 14:35:07 +0800348 return IS_ENABLED(CONFIG_SPL_OS_BOOT) ||
349 IS_ENABLED(CONFIG_SPL_OPENSBI);
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600350 default:
351 return false;
352 }
353}
354
Marek Vasuta0494272023-09-21 20:44:16 +0200355__weak int board_spl_fit_append_fdt_skip(const char *name)
356{
357 return 0; /* Do not skip */
358}
359
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200360static int spl_fit_append_fdt(struct spl_image_info *spl_image,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500361 struct spl_load_info *info, ulong offset,
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600362 const struct spl_fit_info *ctx)
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200363{
364 struct spl_image_info image_info;
Michal Simekbf703df2019-10-22 16:39:11 +0200365 int node, ret = 0, index = 0;
Lukas Auer80afee72019-08-21 21:14:42 +0200366
367 /*
368 * Use the address following the image as target address for the
Marek Vasutc27cbe82020-10-19 23:40:26 +0200369 * device tree.
Lukas Auer80afee72019-08-21 21:14:42 +0200370 */
Marek Vasutc27cbe82020-10-19 23:40:26 +0200371 image_info.load_addr = spl_image->load_addr + spl_image->size;
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200372
373 /* Figure out which device tree the board wants to use */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600374 node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200375 if (node < 0) {
Sean Anderson5ff77722023-10-14 16:47:55 -0400376 size_t size;
377
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200378 debug("%s: cannot find FDT node\n", __func__);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200379
Lukas Auer80afee72019-08-21 21:14:42 +0200380 /*
381 * U-Boot did not find a device tree inside the FIT image. Use
382 * the U-Boot device tree instead.
383 */
Sean Anderson5ff77722023-10-14 16:47:55 -0400384 if (!gd->fdt_blob)
Lukas Auer80afee72019-08-21 21:14:42 +0200385 return node;
Sean Anderson5ff77722023-10-14 16:47:55 -0400386
387 /*
388 * Make the load-address of the FDT available for the SPL
389 * framework
390 */
391 size = fdt_totalsize(gd->fdt_blob);
392 spl_image->fdt_addr = map_sysmem(image_info.load_addr, size);
393 memcpy(spl_image->fdt_addr, gd->fdt_blob, size);
Lukas Auer80afee72019-08-21 21:14:42 +0200394 } else {
Sean Anderson7d8d6132023-11-08 11:48:40 -0500395 ret = load_simple_fit(info, offset, ctx, node, &image_info);
Lukas Auer80afee72019-08-21 21:14:42 +0200396 if (ret < 0)
397 return ret;
Sean Anderson5ff77722023-10-14 16:47:55 -0400398
399 spl_image->fdt_addr = phys_to_virt(image_info.load_addr);
Lukas Auer80afee72019-08-21 21:14:42 +0200400 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200401
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600402 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
403 return 0;
404
Tom Rinid259d9c2023-01-10 11:19:28 -0500405#if CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200406 void *tmpbuffer = NULL;
407
Michal Simekbf703df2019-10-22 16:39:11 +0200408 for (; ; index++) {
Marek Vasuta0494272023-09-21 20:44:16 +0200409 const char *str;
410
411 ret = spl_fit_get_image_name(ctx, FIT_FDT_PROP, index, &str);
412 if (ret == -E2BIG) {
Michal Simekbf703df2019-10-22 16:39:11 +0200413 debug("%s: No additional FDT node\n", __func__);
Marek Vasuta0494272023-09-21 20:44:16 +0200414 ret = 0;
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200415 break;
Marek Vasuta0494272023-09-21 20:44:16 +0200416 } else if (ret < 0) {
417 continue;
418 }
419
420 ret = board_spl_fit_append_fdt_skip(str);
421 if (ret)
422 continue;
423
424 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
425 if (node < 0) {
Jean-Jacques Hiblotc6ab6a12019-10-22 16:39:14 +0200426 debug("%s: unable to find FDT node %d\n",
427 __func__, index);
428 continue;
Michal Simekbf703df2019-10-22 16:39:11 +0200429 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200430
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200431 if (!tmpbuffer) {
432 /*
433 * allocate memory to store the DT overlay
434 * before it is applied. It may not be used
435 * depending on how the overlay is stored, so
436 * don't fail yet if the allocation failed.
437 */
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +0200438 size_t size = CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ;
439
440 tmpbuffer = malloc_cache_aligned(size);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200441 if (!tmpbuffer)
442 debug("%s: unable to allocate space for overlays\n",
443 __func__);
444 }
445 image_info.load_addr = (ulong)tmpbuffer;
Sean Anderson7d8d6132023-11-08 11:48:40 -0500446 ret = load_simple_fit(info, offset, ctx, node,
Simon Glass205ff7b2023-09-26 08:14:33 -0600447 &image_info);
Michal Simekbf703df2019-10-22 16:39:11 +0200448 if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200449 break;
Michal Simekbf703df2019-10-22 16:39:11 +0200450
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200451 /* Make room in FDT for changes from the overlay */
452 ret = fdt_increase_size(spl_image->fdt_addr,
453 image_info.size);
454 if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200455 break;
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200456
Michal Simekbf703df2019-10-22 16:39:11 +0200457 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
458 (void *)image_info.load_addr);
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200459 if (ret) {
460 pr_err("failed to apply DT overlay %s\n",
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600461 fit_get_name(ctx->fit, node, NULL));
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200462 break;
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200463 }
Michal Simekbf703df2019-10-22 16:39:11 +0200464
465 debug("%s: DT overlay %s applied\n", __func__,
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600466 fit_get_name(ctx->fit, node, NULL));
Michal Simekbf703df2019-10-22 16:39:11 +0200467 }
Heinrich Schuchardt939a9612020-04-20 12:44:01 +0200468 free(tmpbuffer);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200469 if (ret)
470 return ret;
Tom Rinid259d9c2023-01-10 11:19:28 -0500471#endif
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200472 /* Try to make space, so we can inject details on the loadables */
473 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
474 if (ret < 0)
475 return ret;
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200476
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200477 return ret;
478}
479
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600480static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200481 void *blob, struct spl_image_info *image)
482{
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100483 int ret = 0;
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200484 const char *name;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100485 int node;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200486
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600487 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
488 return 0;
489
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600490 ret = spl_fit_get_image_name(ctx, "loadables", index, &name);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200491 if (ret < 0)
492 return ret;
493
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600494 node = spl_fit_get_image_node(ctx, "loadables", index);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200495
496 ret = fdt_record_loadable(blob, index, name, image->load_addr,
Simon Glassdff90432023-09-26 08:14:35 -0600497 image->size, image->entry_point,
498 fdt_getprop(ctx->fit, node, FIT_TYPE_PROP, NULL),
499 fdt_getprop(ctx->fit, node, FIT_OS_PROP, NULL),
500 fdt_getprop(ctx->fit, node, FIT_ARCH_PROP, NULL));
501
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200502 return ret;
503}
504
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500505static int spl_fit_image_is_fpga(const void *fit, int node)
506{
507 const char *type;
508
509 if (!IS_ENABLED(CONFIG_SPL_FPGA))
510 return 0;
511
512 type = fdt_getprop(fit, node, FIT_TYPE_PROP, NULL);
513 if (!type)
514 return 0;
515
516 return !strcmp(type, "fpga");
517}
518
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100519static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
520{
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600521 if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT))
522 return fit_image_get_os(fit, noffset, os);
Samuel Hollande646f512020-10-21 21:12:13 -0500523
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600524 const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
Samuel Hollande646f512020-10-21 21:12:13 -0500525 if (!name)
526 return -ENOENT;
527
528 /*
529 * We don't care what the type of the image actually is,
530 * only whether or not it is U-Boot. This saves some
531 * space by omitting the large table of OS types.
532 */
533 if (!strcmp(name, "u-boot"))
534 *os = IH_OS_U_BOOT;
535 else
536 *os = IH_OS_INVALID;
537
538 return 0;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100539}
540
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500541/*
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500542 * The purpose of the FIT load buffer is to provide a memory location that is
543 * independent of the load address of any FIT component.
544 */
545static void *spl_get_fit_load_buffer(size_t size)
546{
547 void *buf;
548
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +0200549 buf = malloc_cache_aligned(size);
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500550 if (!buf) {
551 pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size);
Leo Yu-Chi Liangecb7eaa2024-03-13 14:53:15 +0800552
553 if (IS_ENABLED(CONFIG_SPL_SYS_MALLOC))
554 pr_err("\tcheck CONFIG_SPL_SYS_MALLOC_SIZE\n");
555 else
556 pr_err("\tcheck CONFIG_SPL_SYS_MALLOC_F_LEN\n");
557
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500558 buf = spl_get_load_buffer(0, size);
559 }
560 return buf;
561}
562
Heiko Schocher7d7c3672021-08-17 08:17:18 +0200563__weak void *board_spl_fit_buffer_addr(ulong fit_size, int sectors, int bl_len)
564{
565 return spl_get_fit_load_buffer(sectors * bl_len);
566}
567
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500568/*
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500569 * Weak default function to allow customizing SPL fit loading for load-only
570 * use cases by allowing to skip the parsing/processing of the FIT contents
571 * (so that this can be done separately in a more customized fashion)
572 */
573__weak bool spl_load_simple_fit_skip_processing(void)
574{
575 return false;
576}
577
Heiko Schocher67e8f5d2021-08-06 06:44:26 +0200578/*
579 * Weak default function to allow fixes after fit header
580 * is loaded.
581 */
582__weak void *spl_load_simple_fit_fix_load(const void *fit)
583{
584 return (void *)fit;
585}
586
Alexandru Gagniuc69a99662021-03-29 12:05:13 -0500587static void warn_deprecated(const char *msg)
588{
589 printf("DEPRECATED: %s\n", msg);
590 printf("\tSee doc/uImage.FIT/source_file_format.txt\n");
591}
592
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500593static int spl_fit_upload_fpga(struct spl_fit_info *ctx, int node,
594 struct spl_image_info *fpga_image)
595{
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500596 const char *compatible;
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500597 int ret;
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300598 int devnum = 0;
599 int flags = 0;
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500600
601 debug("FPGA bitstream at: %x, size: %x\n",
602 (u32)fpga_image->load_addr, fpga_image->size);
603
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500604 compatible = fdt_getprop(ctx->fit, node, "compatible", NULL);
Oleksandr Suvorov2fddf3e2022-07-22 17:16:09 +0300605 if (!compatible) {
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500606 warn_deprecated("'fpga' image without 'compatible' property");
Oleksandr Suvorov2fddf3e2022-07-22 17:16:09 +0300607 } else {
608 if (CONFIG_IS_ENABLED(FPGA_LOAD_SECURE))
609 flags = fpga_compatible2flag(devnum, compatible);
610 if (strcmp(compatible, "u-boot,fpga-legacy"))
611 debug("Ignoring compatible = %s property\n",
612 compatible);
613 }
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500614
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300615 ret = fpga_load(devnum, (void *)fpga_image->load_addr,
616 fpga_image->size, BIT_FULL, flags);
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500617 if (ret) {
618 printf("%s: Cannot load the image to the FPGA\n", __func__);
619 return ret;
620 }
621
622 puts("FPGA image loaded from FIT\n");
623
624 return 0;
625}
626
627static int spl_fit_load_fpga(struct spl_fit_info *ctx,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500628 struct spl_load_info *info, ulong offset)
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500629{
630 int node, ret;
631
632 struct spl_image_info fpga_image = {
633 .load_addr = 0,
634 };
635
636 node = spl_fit_get_image_node(ctx, "fpga", 0);
637 if (node < 0)
638 return node;
639
Alexandru Gagniuc69a99662021-03-29 12:05:13 -0500640 warn_deprecated("'fpga' property in config node. Use 'loadables'");
641
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500642 /* Load the image and set up the fpga_image structure */
Sean Anderson7d8d6132023-11-08 11:48:40 -0500643 ret = load_simple_fit(info, offset, ctx, node, &fpga_image);
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500644 if (ret) {
645 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
646 return ret;
647 }
648
649 return spl_fit_upload_fpga(ctx, node, &fpga_image);
650}
651
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600652static int spl_simple_fit_read(struct spl_fit_info *ctx,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500653 struct spl_load_info *info, ulong offset,
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600654 const void *fit_header)
Simon Glassa6131a22016-02-22 22:55:56 -0700655{
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600656 unsigned long count, size;
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600657 void *buf;
Simon Glassa6131a22016-02-22 22:55:56 -0700658
659 /*
York Suna058b8a2017-08-15 11:14:45 -0700660 * For FIT with external data, figure out where the external images
661 * start. This is the base for the data-offset properties in each
662 * image.
Simon Glassa6131a22016-02-22 22:55:56 -0700663 */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600664 size = ALIGN(fdt_totalsize(fit_header), 4);
Ye Li9d5b0f42018-11-17 09:10:25 +0000665 size = board_spl_fit_size_align(size);
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600666 ctx->ext_data_offset = ALIGN(size, 4);
Simon Glassa6131a22016-02-22 22:55:56 -0700667
668 /*
669 * So far we only have one block of data from the FIT. Read the entire
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500670 * thing, including that first block.
York Suna058b8a2017-08-15 11:14:45 -0700671 *
672 * For FIT with data embedded, data is loaded as part of FIT image.
673 * For FIT with external data, data is not loaded in this step.
Simon Glassa6131a22016-02-22 22:55:56 -0700674 */
Sean Anderson7d8d6132023-11-08 11:48:40 -0500675 size = get_aligned_image_size(info, size, 0);
676 buf = board_spl_fit_buffer_addr(size, size, 1);
Ye Li9d5b0f42018-11-17 09:10:25 +0000677
Sean Anderson7d8d6132023-11-08 11:48:40 -0500678 count = info->read(info, offset, size, buf);
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600679 ctx->fit = buf;
Sean Anderson7d8d6132023-11-08 11:48:40 -0500680 debug("fit read offset %lx, size=%lu, dst=%p, count=%lu\n",
681 offset, size, buf, count);
Simon Glassa6131a22016-02-22 22:55:56 -0700682
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600683 return (count == 0) ? -EIO : 0;
684}
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500685
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600686static int spl_simple_fit_parse(struct spl_fit_info *ctx)
687{
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600688 /* Find the correct subnode under "/configurations" */
689 ctx->conf_node = fit_find_config_node(ctx->fit);
690 if (ctx->conf_node < 0)
691 return -EINVAL;
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100692
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600693 if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100694 printf("## Checking hash(es) for config %s ... ",
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600695 fit_get_name(ctx->fit, ctx->conf_node, NULL));
696 if (fit_config_verify(ctx->fit, ctx->conf_node))
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100697 return -EPERM;
698 puts("OK\n");
699 }
700
Andre Przywara525e9c92017-04-26 01:32:34 +0100701 /* find the node holding the images information */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600702 ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
703 if (ctx->images_node < 0) {
704 debug("%s: Cannot find /images node: %d\n", __func__,
705 ctx->images_node);
706 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700707 }
Marek Vasut33dd00e2018-05-12 22:25:28 +0200708
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600709 return 0;
710}
711
712int spl_load_simple_fit(struct spl_image_info *spl_image,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500713 struct spl_load_info *info, ulong offset, void *fit)
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600714{
715 struct spl_image_info image_info;
716 struct spl_fit_info ctx;
717 int node = -1;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600718 int ret;
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600719 int index = 0;
720 int firmware_node;
721
Sean Anderson7d8d6132023-11-08 11:48:40 -0500722 ret = spl_simple_fit_read(&ctx, info, offset, fit);
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600723 if (ret < 0)
724 return ret;
725
726 /* skip further processing if requested to enable load-only use cases */
727 if (spl_load_simple_fit_skip_processing())
728 return 0;
729
Heiko Schocher67e8f5d2021-08-06 06:44:26 +0200730 ctx.fit = spl_load_simple_fit_fix_load(ctx.fit);
731
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600732 ret = spl_simple_fit_parse(&ctx);
733 if (ret < 0)
734 return ret;
735
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500736 if (IS_ENABLED(CONFIG_SPL_FPGA))
Sean Anderson7d8d6132023-11-08 11:48:40 -0500737 spl_fit_load_fpga(&ctx, info, offset);
Andre Przywara525e9c92017-04-26 01:32:34 +0100738
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200739 /*
740 * Find the U-Boot image using the following search order:
741 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
742 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
743 * - fall back to using the first 'loadables' entry
744 */
745 if (node < 0)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600746 node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600747
748 if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT))
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600749 node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600750
Andre Przywara525e9c92017-04-26 01:32:34 +0100751 if (node < 0) {
752 debug("could not find firmware image, trying loadables...\n");
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600753 node = spl_fit_get_image_node(&ctx, "loadables", 0);
Andre Przywarae7c58562017-04-26 01:32:37 +0100754 /*
755 * If we pick the U-Boot image from "loadables", start at
756 * the second image when later loading additional images.
757 */
758 index = 1;
Andre Przywara525e9c92017-04-26 01:32:34 +0100759 }
Simon Glassa6131a22016-02-22 22:55:56 -0700760 if (node < 0) {
Andre Przywara525e9c92017-04-26 01:32:34 +0100761 debug("%s: Cannot find u-boot image node: %d\n",
762 __func__, node);
Simon Glassa6131a22016-02-22 22:55:56 -0700763 return -1;
764 }
765
Andre Przywara29053e92017-04-26 01:32:36 +0100766 /* Load the image and set up the spl_image structure */
Sean Anderson7d8d6132023-11-08 11:48:40 -0500767 ret = load_simple_fit(info, offset, &ctx, node, spl_image);
Andre Przywara29053e92017-04-26 01:32:36 +0100768 if (ret)
769 return ret;
Daniel Allredcf839bd2016-06-27 09:19:21 -0500770
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200771 /*
772 * For backward compatibility, we treat the first node that is
773 * as a U-Boot image, if no OS-type has been declared.
774 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600775 if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
York Suna058b8a2017-08-15 11:14:45 -0700776 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600777 else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200778 spl_image->os = IH_OS_U_BOOT;
Simon Glassa6131a22016-02-22 22:55:56 -0700779
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200780 /*
781 * Booting a next-stage U-Boot may require us to append the FDT.
782 * We allow this to fail, as the U-Boot image might embed its FDT.
783 */
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600784 if (os_takes_devicetree(spl_image->os)) {
Sean Anderson7d8d6132023-11-08 11:48:40 -0500785 ret = spl_fit_append_fdt(spl_image, info, offset, &ctx);
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600786 if (ret < 0 && spl_image->os != IH_OS_U_BOOT)
Dario Binacchifaf8d142020-05-27 13:56:19 +0200787 return ret;
788 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100789
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200790 firmware_node = node;
Andre Przywarae7c58562017-04-26 01:32:37 +0100791 /* Now check if there are more images for us to load */
792 for (; ; index++) {
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200793 uint8_t os_type = IH_OS_INVALID;
794
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600795 node = spl_fit_get_image_node(&ctx, "loadables", index);
Andre Przywarae7c58562017-04-26 01:32:37 +0100796 if (node < 0)
797 break;
798
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200799 /*
800 * if the firmware is also a loadable, skip it because
801 * it already has been loaded. This is typically the case with
802 * u-boot.img generated by mkimage.
803 */
804 if (firmware_node == node)
805 continue;
806
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500807 image_info.load_addr = 0;
Sean Anderson7d8d6132023-11-08 11:48:40 -0500808 ret = load_simple_fit(info, offset, &ctx, node, &image_info);
Philippe Reynesba87da42020-11-24 16:15:05 +0100809 if (ret < 0) {
810 printf("%s: can't load image loadables index %d (ret = %d)\n",
811 __func__, index, ret);
812 return ret;
813 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100814
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500815 if (spl_fit_image_is_fpga(ctx.fit, node))
816 spl_fit_upload_fpga(&ctx, node, &image_info);
817
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600818 if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200819 debug("Loadable is %s\n", genimg_get_os_name(os_type));
820
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600821 if (os_takes_devicetree(os_type)) {
Sean Anderson7d8d6132023-11-08 11:48:40 -0500822 spl_fit_append_fdt(&image_info, info, offset, &ctx);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200823 spl_image->fdt_addr = image_info.fdt_addr;
824 }
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200825
Andre Przywarae7c58562017-04-26 01:32:37 +0100826 /*
827 * If the "firmware" image did not provide an entry point,
828 * use the first valid entry point from the loadables.
829 */
830 if (spl_image->entry_point == FDT_ERROR &&
831 image_info.entry_point != FDT_ERROR)
832 spl_image->entry_point = image_info.entry_point;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200833
834 /* Record our loadables into the FDT */
835 if (spl_image->fdt_addr)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600836 spl_fit_record_loadable(&ctx, index,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200837 spl_image->fdt_addr,
838 &image_info);
Andre Przywarae7c58562017-04-26 01:32:37 +0100839 }
840
841 /*
Jesse Taube93ee5c82023-08-24 21:59:48 -0400842 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
Andre Przywarae7c58562017-04-26 01:32:37 +0100843 * Makefile will set it to 0 and it will end up as the entry point
844 * here. What it actually means is: use the load address.
845 */
846 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
847 spl_image->entry_point = spl_image->load_addr;
848
Ye Li9d5b0f42018-11-17 09:10:25 +0000849 spl_image->flags |= SPL_FIT_FOUND;
850
Andre Przywarae7c58562017-04-26 01:32:37 +0100851 return 0;
Simon Glassa6131a22016-02-22 22:55:56 -0700852}
Simon Glassc0bd55e2023-09-26 08:14:34 -0600853
854/* Parse and load full fitImage in SPL */
855int spl_load_fit_image(struct spl_image_info *spl_image,
856 const struct legacy_img_hdr *header)
857{
858 struct bootm_headers images;
859 const char *fit_uname_config = NULL;
860 uintptr_t fdt_hack;
861 const char *uname;
862 ulong fw_data = 0, dt_data = 0, img_data = 0;
863 ulong fw_len = 0, dt_len = 0, img_len = 0;
864 int idx, conf_noffset;
865 int ret;
866
867#ifdef CONFIG_SPL_FIT_SIGNATURE
868 images.verify = 1;
869#endif
Sean Anderson5ff77722023-10-14 16:47:55 -0400870 ret = fit_image_load(&images, virt_to_phys((void *)header),
Simon Glassc0bd55e2023-09-26 08:14:34 -0600871 NULL, &fit_uname_config,
872 IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
873 FIT_LOAD_OPTIONAL, &fw_data, &fw_len);
874 if (ret >= 0) {
875 printf("DEPRECATED: 'standalone = ' property.");
876 printf("Please use either 'firmware =' or 'kernel ='\n");
877 } else {
Sean Anderson5ff77722023-10-14 16:47:55 -0400878 ret = fit_image_load(&images, virt_to_phys((void *)header),
879 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
Simon Glassc0bd55e2023-09-26 08:14:34 -0600880 IH_TYPE_FIRMWARE, -1, FIT_LOAD_OPTIONAL,
881 &fw_data, &fw_len);
882 }
883
884 if (ret < 0) {
Sean Anderson5ff77722023-10-14 16:47:55 -0400885 ret = fit_image_load(&images, virt_to_phys((void *)header),
886 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
Simon Glassc0bd55e2023-09-26 08:14:34 -0600887 IH_TYPE_KERNEL, -1, FIT_LOAD_OPTIONAL,
888 &fw_data, &fw_len);
889 }
890
891 if (ret < 0)
892 return ret;
893
894 spl_image->size = fw_len;
Simon Glassc0bd55e2023-09-26 08:14:34 -0600895 spl_image->load_addr = fw_data;
Sean Andersone99b2cd2023-10-14 16:47:39 -0400896 if (fit_image_get_entry(header, ret, &spl_image->entry_point))
897 spl_image->entry_point = fw_data;
Simon Glassc0bd55e2023-09-26 08:14:34 -0600898 if (fit_image_get_os(header, ret, &spl_image->os))
899 spl_image->os = IH_OS_INVALID;
900 spl_image->name = genimg_get_os_name(spl_image->os);
901
902 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
903 spl_image->name, spl_image->load_addr, spl_image->size);
904
905#ifdef CONFIG_SPL_FIT_SIGNATURE
906 images.verify = 1;
907#endif
Sean Anderson5ff77722023-10-14 16:47:55 -0400908 ret = fit_image_load(&images, virt_to_phys((void *)header), NULL,
909 &fit_uname_config, IH_ARCH_DEFAULT, IH_TYPE_FLATDT,
910 -1, FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
Simon Glassc0bd55e2023-09-26 08:14:34 -0600911 if (ret >= 0) {
912 spl_image->fdt_addr = (void *)dt_data;
913
914 if (spl_image->os == IH_OS_U_BOOT) {
915 /* HACK: U-Boot expects FDT at a specific address */
916 fdt_hack = spl_image->load_addr + spl_image->size;
917 fdt_hack = (fdt_hack + 3) & ~3;
918 debug("Relocating FDT to %p\n", spl_image->fdt_addr);
919 memcpy((void *)fdt_hack, spl_image->fdt_addr, dt_len);
920 }
921 }
922
923 conf_noffset = fit_conf_get_node((const void *)header,
924 fit_uname_config);
925 if (conf_noffset < 0)
926 return 0;
927
928 for (idx = 0;
929 uname = fdt_stringlist_get((const void *)header, conf_noffset,
930 FIT_LOADABLE_PROP, idx,
931 NULL), uname;
932 idx++) {
933#ifdef CONFIG_SPL_FIT_SIGNATURE
934 images.verify = 1;
935#endif
936 ret = fit_image_load(&images, (ulong)header,
937 &uname, &fit_uname_config,
938 IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
939 FIT_LOAD_OPTIONAL_NON_ZERO,
940 &img_data, &img_len);
941 if (ret < 0)
942 return ret;
943 }
944
945 return 0;
946}