blob: 25f3c822a498f14b551d76322670a17d2b4942c2 [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 Glass9b5bfba2024-08-07 16:47:33 -060015#include <upl.h>
Simon Glass458b66a2020-11-05 06:32:05 -070016#include <sysinfo.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040018#include <asm/io.h>
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +020019#include <linux/libfdt.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060020#include <linux/printk.h>
Simon Glassa6131a22016-02-22 22:55:56 -070021
Lukas Auer80afee72019-08-21 21:14:42 +020022DECLARE_GLOBAL_DATA_PTR;
23
Alexandru Gagniuc23243c92021-01-20 10:46:50 -060024struct spl_fit_info {
25 const void *fit; /* Pointer to a valid FIT blob */
26 size_t ext_data_offset; /* Offset to FIT external data (end of FIT) */
27 int images_node; /* FDT offset to "/images" node */
Alexandru Gagniucfa11a442021-01-20 10:46:53 -060028 int conf_node; /* FDT offset to selected configuration node */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -060029};
30
Ye Li9d5b0f42018-11-17 09:10:25 +000031__weak ulong board_spl_fit_size_align(ulong size)
32{
33 return size;
34}
35
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020036static int find_node_from_desc(const void *fit, int node, const char *str)
37{
38 int child;
39
40 if (node < 0)
41 return -EINVAL;
42
43 /* iterate the FIT nodes and find a matching description */
44 for (child = fdt_first_subnode(fit, node); child >= 0;
45 child = fdt_next_subnode(fit, child)) {
46 int len;
Simon Glassdff90432023-09-26 08:14:35 -060047 const char *desc = fdt_getprop(fit, child, FIT_DESC_PROP, &len);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020048
49 if (!desc)
50 continue;
51
52 if (!strcmp(desc, str))
53 return child;
54 }
55
56 return -ENOENT;
57}
58
Andre Przywara525e9c92017-04-26 01:32:34 +010059/**
Philipp Tomsiche61eff92017-09-13 21:29:34 +020060 * spl_fit_get_image_name(): By using the matching configuration subnode,
Andre Przywara525e9c92017-04-26 01:32:34 +010061 * retrieve the name of an image, specified by a property name and an index
62 * into that.
63 * @fit: Pointer to the FDT blob.
64 * @images: Offset of the /images subnode.
65 * @type: Name of the property within the configuration subnode.
66 * @index: Index into the list of strings in this property.
Philipp Tomsiche61eff92017-09-13 21:29:34 +020067 * @outname: Name of the image
Andre Przywara525e9c92017-04-26 01:32:34 +010068 *
Philipp Tomsiche61eff92017-09-13 21:29:34 +020069 * Return: 0 on success, or a negative error number
Andre Przywara525e9c92017-04-26 01:32:34 +010070 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -060071static int spl_fit_get_image_name(const struct spl_fit_info *ctx,
Philipp Tomsiche61eff92017-09-13 21:29:34 +020072 const char *type, int index,
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +020073 const char **outname)
Andre Przywara1e329b62017-04-26 01:32:33 +010074{
Simon Glass458b66a2020-11-05 06:32:05 -070075 struct udevice *sysinfo;
Heinrich Schuchardt57894fb2025-06-24 17:34:31 +020076 const char *name, *str, *end;
Philipp Tomsiche61eff92017-09-13 21:29:34 +020077 __maybe_unused int node;
Andre Przywara1e329b62017-04-26 01:32:33 +010078 int len, i;
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020079 bool found = true;
Andre Przywara1e329b62017-04-26 01:32:33 +010080
Alexandru Gagniucfa11a442021-01-20 10:46:53 -060081 name = fdt_getprop(ctx->fit, ctx->conf_node, type, &len);
Andre Przywara1e329b62017-04-26 01:32:33 +010082 if (!name) {
83 debug("cannot find property '%s': %d\n", type, len);
84 return -EINVAL;
85 }
Heinrich Schuchardt57894fb2025-06-24 17:34:31 +020086 /* A string property should be NUL terminated */
87 end = name + len - 1;
88 if (!len || *end) {
89 debug("malformed property '%s'\n", type);
90 return -EINVAL;
91 }
Simon Glassa6131a22016-02-22 22:55:56 -070092
Andre Przywara1e329b62017-04-26 01:32:33 +010093 str = name;
94 for (i = 0; i < index; i++) {
Heinrich Schuchardtd682bf92025-06-24 17:34:30 +020095 str = strchr(str, '\0') + 1;
Heinrich Schuchardt57894fb2025-06-24 17:34:31 +020096 if (str > end) {
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020097 found = false;
98 break;
Andre Przywara1e329b62017-04-26 01:32:33 +010099 }
Simon Glassa6131a22016-02-22 22:55:56 -0700100 }
101
Simon Glass458b66a2020-11-05 06:32:05 -0700102 if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) {
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200103 int rc;
104 /*
Simon Glass458b66a2020-11-05 06:32:05 -0700105 * no string in the property for this index. Check if the
106 * sysinfo-level code can supply one.
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200107 */
Sean Anderson6ccf10a2021-04-20 10:50:56 -0400108 rc = sysinfo_detect(sysinfo);
109 if (rc)
110 return rc;
111
Simon Glass458b66a2020-11-05 06:32:05 -0700112 rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
113 &str);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200114 if (rc && rc != -ENOENT)
115 return rc;
116
117 if (!rc) {
118 /*
Simon Glass458b66a2020-11-05 06:32:05 -0700119 * The sysinfo provided a name for a loadable.
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200120 * Try to match it against the description properties
121 * first. If no matching node is found, use it as a
122 * node name.
123 */
124 int node;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600125 int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200126
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600127 node = find_node_from_desc(ctx->fit, images, str);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200128 if (node > 0)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600129 str = fdt_get_name(ctx->fit, node, NULL);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200130
131 found = true;
132 }
133 }
134
135 if (!found) {
136 debug("no string for index %d\n", index);
137 return -E2BIG;
138 }
139
140 *outname = str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200141 return 0;
142}
143
144/**
145 * spl_fit_get_image_node(): By using the matching configuration subnode,
146 * retrieve the name of an image, specified by a property name and an index
147 * into that.
148 * @fit: Pointer to the FDT blob.
149 * @images: Offset of the /images subnode.
150 * @type: Name of the property within the configuration subnode.
151 * @index: Index into the list of strings in this property.
152 *
153 * Return: the node offset of the respective image node or a negative
154 * error number.
155 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600156static int spl_fit_get_image_node(const struct spl_fit_info *ctx,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200157 const char *type, int index)
158{
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200159 const char *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200160 int err;
161 int node;
162
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600163 err = spl_fit_get_image_name(ctx, type, index, &str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200164 if (err)
165 return err;
166
Andre Przywara1e329b62017-04-26 01:32:33 +0100167 debug("%s: '%s'\n", type, str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200168
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600169 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
Andre Przywara1e329b62017-04-26 01:32:33 +0100170 if (node < 0) {
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200171 pr_err("cannot find image node '%s': %d\n", str, node);
Andre Przywara1e329b62017-04-26 01:32:33 +0100172 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700173 }
Simon Glassa6131a22016-02-22 22:55:56 -0700174
Andre Przywara525e9c92017-04-26 01:32:34 +0100175 return node;
Simon Glassa6131a22016-02-22 22:55:56 -0700176}
177
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530178static int get_aligned_image_offset(struct spl_load_info *info, int offset)
179{
Sean Anderson35f15fe2023-11-08 11:48:43 -0500180 return ALIGN_DOWN(offset, spl_get_bl_len(info));
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530181}
182
183static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
184{
Sean Anderson35f15fe2023-11-08 11:48:43 -0500185 return offset & (spl_get_bl_len(info) - 1);
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530186}
187
188static int get_aligned_image_size(struct spl_load_info *info, int data_size,
189 int offset)
190{
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530191 data_size = data_size + get_aligned_image_overhead(info, offset);
192
Sean Anderson35f15fe2023-11-08 11:48:43 -0500193 return ALIGN(data_size, spl_get_bl_len(info));
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530194}
195
Andre Przywara29053e92017-04-26 01:32:36 +0100196/**
Simon Glass205ff7b2023-09-26 08:14:33 -0600197 * load_simple_fit(): load the image described in a certain FIT node
Andre Przywara29053e92017-04-26 01:32:36 +0100198 * @info: points to information about the device to load data from
Simon Glass97c434b2024-12-19 11:29:02 -0700199 * @fit_offset: the offset of the FIT image on the device
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600200 * @ctx: points to the FIT context structure
Andre Przywara29053e92017-04-26 01:32:36 +0100201 * @node: offset of the DT node describing the image to load (relative
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200202 * to @fit)
Andre Przywara29053e92017-04-26 01:32:36 +0100203 * @image_info: will be filled with information about the loaded image
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200204 * If the FIT node does not contain a "load" (address) property,
205 * the image gets loaded to the address pointed to by the
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500206 * load_addr member in this struct, if load_addr is not 0
Andre Przywara29053e92017-04-26 01:32:36 +0100207 *
Mikhail Kshevetskiyaa17b392025-06-10 12:56:31 +0300208 * Return: 0 on success, -EBADSLT if this image is not the correct phase
Simon Glassc811ef92025-01-26 11:43:17 -0700209 * (for CONFIG_BOOTMETH_VBE_SIMPLE_FW), or another negative error number on
210 * other error.
Andre Przywara29053e92017-04-26 01:32:36 +0100211 */
Sean Anderson7d8d6132023-11-08 11:48:40 -0500212static int load_simple_fit(struct spl_load_info *info, ulong fit_offset,
Simon Glass205ff7b2023-09-26 08:14:33 -0600213 const struct spl_fit_info *ctx, int node,
214 struct spl_image_info *image_info)
Andre Przywara29053e92017-04-26 01:32:36 +0100215{
Michal Simekf707b5a2018-07-18 14:33:15 +0200216 int offset;
Andre Przywara29053e92017-04-26 01:32:36 +0100217 size_t length;
York Sunadf99fa2017-08-15 11:14:44 -0700218 int len;
York Sun25d65f12017-09-15 08:21:13 -0700219 ulong size;
Simon Glass2192e982021-03-07 17:35:14 -0700220 ulong load_addr;
221 void *load_ptr;
Andre Przywara29053e92017-04-26 01:32:36 +0100222 void *src;
223 ulong overhead;
York Suna6945fe2017-08-15 11:14:43 -0700224 uint8_t image_comp = -1, type = -1;
York Sunadf99fa2017-08-15 11:14:44 -0700225 const void *data;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600226 const void *fit = ctx->fit;
Peng Fan8876c7e2017-12-05 13:20:59 +0800227 bool external_data = false;
York Suna6945fe2017-08-15 11:14:43 -0700228
Simon Glassc811ef92025-01-26 11:43:17 -0700229 log_debug("starting\n");
230 if (CONFIG_IS_ENABLED(BOOTMETH_VBE) &&
231 xpl_get_phase(info) != IH_PHASE_NONE) {
232 enum image_phase_t phase;
233 int ret;
234
235 ret = fit_image_get_phase(fit, node, &phase);
236 /* if the image is for any phase, let's use it */
237 if (ret == -ENOENT || phase == xpl_get_phase(info)) {
238 log_debug("found\n");
239 } else if (ret < 0) {
240 log_debug("err=%d\n", ret);
241 return ret;
242 } else {
243 log_debug("- phase mismatch, skipping this image\n");
Mikhail Kshevetskiyaa17b392025-06-10 12:56:31 +0300244 return -EBADSLT;
Simon Glassc811ef92025-01-26 11:43:17 -0700245 }
246 }
247
Michal Simek1aab1142020-09-09 14:41:56 +0200248 if (IS_ENABLED(CONFIG_SPL_FPGA) ||
Manoj Sai2b2e6282023-09-18 00:56:25 +0530249 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && spl_decompression_enabled())) {
Marek Vasut72bc5d52018-06-01 23:19:29 +0200250 if (fit_image_get_type(fit, node, &type))
251 puts("Cannot get image type.\n");
252 else
253 debug("%s ", genimg_get_type_name(type));
254 }
255
Manoj Sai2b2e6282023-09-18 00:56:25 +0530256 if (spl_decompression_enabled()) {
Klaus H. Sorensen4a9a0422019-12-11 11:03:33 +0000257 fit_image_get_comp(fit, node, &image_comp);
258 debug("%s ", genimg_get_comp_name(image_comp));
York Suna6945fe2017-08-15 11:14:43 -0700259 }
Andre Przywara29053e92017-04-26 01:32:36 +0100260
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500261 if (fit_image_get_load(fit, node, &load_addr)) {
262 if (!image_info->load_addr) {
263 printf("Can't load %s: No load address and no buffer\n",
264 fit_get_name(fit, node, NULL));
265 return -ENOBUFS;
266 }
Andre Przywara29053e92017-04-26 01:32:36 +0100267 load_addr = image_info->load_addr;
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500268 }
Andre Przywara29053e92017-04-26 01:32:36 +0100269
Peng Fan8876c7e2017-12-05 13:20:59 +0800270 if (!fit_image_get_data_position(fit, node, &offset)) {
271 external_data = true;
272 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
Simon Glass97c434b2024-12-19 11:29:02 -0700273 log_debug("read offset %x = offset from fit %lx\n",
274 offset, (ulong)offset + ctx->ext_data_offset);
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600275 offset += ctx->ext_data_offset;
Peng Fan8876c7e2017-12-05 13:20:59 +0800276 external_data = true;
277 }
278
279 if (external_data) {
Simon Glass97c434b2024-12-19 11:29:02 -0700280 ulong read_offset;
Simon Glass2192e982021-03-07 17:35:14 -0700281 void *src_ptr;
282
Peng Fan8876c7e2017-12-05 13:20:59 +0800283 /* External data */
York Sunadf99fa2017-08-15 11:14:44 -0700284 if (fit_image_get_data_size(fit, node, &len))
285 return -ENOENT;
Andre Przywara29053e92017-04-26 01:32:36 +0100286
Nishanth Menone4cb6452021-10-19 12:32:29 -0500287 /* Dont bother to copy 0 byte data, but warn, though */
288 if (!len) {
289 log_warning("%s: Skip load '%s': image size is 0!\n",
290 __func__, fit_get_name(fit, node, NULL));
291 return 0;
292 }
293
Manoj Saia8560992023-09-18 00:56:26 +0530294 if (spl_decompression_enabled() &&
295 (image_comp == IH_COMP_GZIP || image_comp == IH_COMP_LZMA))
Manoj Sai2b2e6282023-09-18 00:56:25 +0530296 src_ptr = map_sysmem(ALIGN(CONFIG_SYS_LOAD_ADDR, ARCH_DMA_MINALIGN), len);
297 else
298 src_ptr = map_sysmem(ALIGN(load_addr, ARCH_DMA_MINALIGN), len);
York Sunadf99fa2017-08-15 11:14:44 -0700299 length = len;
300
301 overhead = get_aligned_image_overhead(info, offset);
Sean Anderson7d8d6132023-11-08 11:48:40 -0500302 size = get_aligned_image_size(info, length, offset);
Simon Glass97c434b2024-12-19 11:29:02 -0700303 read_offset = fit_offset + get_aligned_image_offset(info,
304 offset);
305 log_debug("reading from offset %x / %lx size %lx to %p: ",
306 offset, read_offset, size, src_ptr);
York Sunadf99fa2017-08-15 11:14:44 -0700307
Simon Glassc811ef92025-01-26 11:43:17 -0700308 if (info->read(info, read_offset, size, src_ptr) < length)
York Sunadf99fa2017-08-15 11:14:44 -0700309 return -EIO;
310
Simon Glass2192e982021-03-07 17:35:14 -0700311 debug("External data: dst=%p, offset=%x, size=%lx\n",
312 src_ptr, offset, (unsigned long)length);
313 src = src_ptr + overhead;
York Sunadf99fa2017-08-15 11:14:44 -0700314 } else {
315 /* Embedded data */
Simon Glassd3ca4012025-01-10 17:00:12 -0700316 if (fit_image_get_emb_data(fit, node, &data, &length)) {
York Sunadf99fa2017-08-15 11:14:44 -0700317 puts("Cannot get image data/size\n");
318 return -ENOENT;
319 }
320 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
321 (unsigned long)length);
Simon Glass2192e982021-03-07 17:35:14 -0700322 src = (void *)data; /* cast away const */
York Sunadf99fa2017-08-15 11:14:44 -0700323 }
Andre Przywara29053e92017-04-26 01:32:36 +0100324
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600325 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
326 printf("## Checking hash(es) for Image %s ... ",
327 fit_get_name(fit, node, NULL));
Simon Glasse10e2ee2021-11-12 12:28:10 -0700328 if (!fit_image_verify_with_data(fit, node, gd_fdt_blob(), src,
329 length))
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600330 return -EPERM;
331 puts("OK\n");
332 }
Ben Whitten54a91192018-06-07 11:37:27 +0100333
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600334 if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS))
Lokesh Vutlab36dd3e2021-06-11 11:45:05 +0300335 board_fit_image_post_process(fit, node, &src, &length);
Andre Przywara29053e92017-04-26 01:32:36 +0100336
Simon Glass2192e982021-03-07 17:35:14 -0700337 load_ptr = map_sysmem(load_addr, length);
Michal Simekf354c3f2018-07-24 15:05:00 +0200338 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
York Sun25d65f12017-09-15 08:21:13 -0700339 size = length;
Simon Glass2192e982021-03-07 17:35:14 -0700340 if (gunzip(load_ptr, CONFIG_SYS_BOOTM_LEN, src, &size)) {
York Suna6945fe2017-08-15 11:14:43 -0700341 puts("Uncompressing error\n");
342 return -EIO;
343 }
York Sun25d65f12017-09-15 08:21:13 -0700344 length = size;
Manoj Saia8560992023-09-18 00:56:26 +0530345 } else if (IS_ENABLED(CONFIG_SPL_LZMA) && image_comp == IH_COMP_LZMA) {
346 size = CONFIG_SYS_BOOTM_LEN;
347 ulong loadEnd;
348
349 if (image_decomp(IH_COMP_LZMA, CONFIG_SYS_LOAD_ADDR, 0, 0,
350 load_ptr, src, length, size, &loadEnd)) {
351 puts("Uncompressing error\n");
352 return -EIO;
353 }
354 length = loadEnd - CONFIG_SYS_LOAD_ADDR;
York Suna6945fe2017-08-15 11:14:43 -0700355 } else {
Simon Glass2192e982021-03-07 17:35:14 -0700356 memcpy(load_ptr, src, length);
York Suna6945fe2017-08-15 11:14:43 -0700357 }
Andre Przywara29053e92017-04-26 01:32:36 +0100358
359 if (image_info) {
Michal Simek9e64a552020-09-03 11:24:28 +0200360 ulong entry_point;
361
Andre Przywara29053e92017-04-26 01:32:36 +0100362 image_info->load_addr = load_addr;
363 image_info->size = length;
Michal Simek9e64a552020-09-03 11:24:28 +0200364
365 if (!fit_image_get_entry(fit, node, &entry_point))
366 image_info->entry_point = entry_point;
367 else
368 image_info->entry_point = FDT_ERROR;
Andre Przywara29053e92017-04-26 01:32:36 +0100369 }
Simon Glass97c434b2024-12-19 11:29:02 -0700370 log_debug("- done loading\n");
Andre Przywara29053e92017-04-26 01:32:36 +0100371
Simon Glass9b5bfba2024-08-07 16:47:33 -0600372 upl_add_image(fit, node, load_addr, length);
373
Andre Przywara29053e92017-04-26 01:32:36 +0100374 return 0;
375}
376
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600377static bool os_takes_devicetree(uint8_t os)
378{
379 switch (os) {
380 case IH_OS_U_BOOT:
381 return true;
382 case IH_OS_LINUX:
Randolph263e2ae2023-10-12 14:35:07 +0800383 return IS_ENABLED(CONFIG_SPL_OS_BOOT) ||
384 IS_ENABLED(CONFIG_SPL_OPENSBI);
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600385 default:
386 return false;
387 }
388}
389
Marek Vasuta0494272023-09-21 20:44:16 +0200390__weak int board_spl_fit_append_fdt_skip(const char *name)
391{
392 return 0; /* Do not skip */
393}
394
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200395static int spl_fit_append_fdt(struct spl_image_info *spl_image,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500396 struct spl_load_info *info, ulong offset,
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600397 const struct spl_fit_info *ctx)
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200398{
399 struct spl_image_info image_info;
Michal Simekbf703df2019-10-22 16:39:11 +0200400 int node, ret = 0, index = 0;
Lukas Auer80afee72019-08-21 21:14:42 +0200401
402 /*
403 * Use the address following the image as target address for the
Marek Vasutc27cbe82020-10-19 23:40:26 +0200404 * device tree.
Lukas Auer80afee72019-08-21 21:14:42 +0200405 */
Sam Edwards02d708d2025-03-15 15:18:13 -0700406 image_info.load_addr = ALIGN(spl_image->load_addr + spl_image->size, 8);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200407
408 /* Figure out which device tree the board wants to use */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600409 node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200410 if (node < 0) {
Sean Anderson5ff77722023-10-14 16:47:55 -0400411 size_t size;
412
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200413 debug("%s: cannot find FDT node\n", __func__);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200414
Lukas Auer80afee72019-08-21 21:14:42 +0200415 /*
416 * U-Boot did not find a device tree inside the FIT image. Use
417 * the U-Boot device tree instead.
418 */
Sean Anderson5ff77722023-10-14 16:47:55 -0400419 if (!gd->fdt_blob)
Lukas Auer80afee72019-08-21 21:14:42 +0200420 return node;
Sean Anderson5ff77722023-10-14 16:47:55 -0400421
422 /*
423 * Make the load-address of the FDT available for the SPL
424 * framework
425 */
426 size = fdt_totalsize(gd->fdt_blob);
427 spl_image->fdt_addr = map_sysmem(image_info.load_addr, size);
428 memcpy(spl_image->fdt_addr, gd->fdt_blob, size);
Lukas Auer80afee72019-08-21 21:14:42 +0200429 } else {
Sean Anderson7d8d6132023-11-08 11:48:40 -0500430 ret = load_simple_fit(info, offset, ctx, node, &image_info);
Lukas Auer80afee72019-08-21 21:14:42 +0200431 if (ret < 0)
432 return ret;
Sean Anderson5ff77722023-10-14 16:47:55 -0400433
434 spl_image->fdt_addr = phys_to_virt(image_info.load_addr);
Lukas Auer80afee72019-08-21 21:14:42 +0200435 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200436
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600437 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
438 return 0;
439
Tom Rinid259d9c2023-01-10 11:19:28 -0500440#if CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200441 void *tmpbuffer = NULL;
442
Michal Simekbf703df2019-10-22 16:39:11 +0200443 for (; ; index++) {
Marek Vasuta0494272023-09-21 20:44:16 +0200444 const char *str;
445
446 ret = spl_fit_get_image_name(ctx, FIT_FDT_PROP, index, &str);
447 if (ret == -E2BIG) {
Michal Simekbf703df2019-10-22 16:39:11 +0200448 debug("%s: No additional FDT node\n", __func__);
Marek Vasuta0494272023-09-21 20:44:16 +0200449 ret = 0;
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200450 break;
Marek Vasuta0494272023-09-21 20:44:16 +0200451 } else if (ret < 0) {
452 continue;
453 }
454
455 ret = board_spl_fit_append_fdt_skip(str);
456 if (ret)
457 continue;
458
459 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
460 if (node < 0) {
Jean-Jacques Hiblotc6ab6a12019-10-22 16:39:14 +0200461 debug("%s: unable to find FDT node %d\n",
462 __func__, index);
463 continue;
Michal Simekbf703df2019-10-22 16:39:11 +0200464 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200465
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200466 if (!tmpbuffer) {
467 /*
468 * allocate memory to store the DT overlay
469 * before it is applied. It may not be used
470 * depending on how the overlay is stored, so
471 * don't fail yet if the allocation failed.
472 */
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +0200473 size_t size = CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ;
474
475 tmpbuffer = malloc_cache_aligned(size);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200476 if (!tmpbuffer)
477 debug("%s: unable to allocate space for overlays\n",
478 __func__);
479 }
480 image_info.load_addr = (ulong)tmpbuffer;
Sean Anderson7d8d6132023-11-08 11:48:40 -0500481 ret = load_simple_fit(info, offset, ctx, node,
Simon Glass205ff7b2023-09-26 08:14:33 -0600482 &image_info);
Mikhail Kshevetskiyaa17b392025-06-10 12:56:31 +0300483 if (ret == -EBADSLT)
Simon Glassc811ef92025-01-26 11:43:17 -0700484 continue;
485 else if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200486 break;
Michal Simekbf703df2019-10-22 16:39:11 +0200487
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200488 /* Make room in FDT for changes from the overlay */
489 ret = fdt_increase_size(spl_image->fdt_addr,
490 image_info.size);
491 if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200492 break;
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200493
Michal Simekbf703df2019-10-22 16:39:11 +0200494 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
495 (void *)image_info.load_addr);
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200496 if (ret) {
497 pr_err("failed to apply DT overlay %s\n",
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600498 fit_get_name(ctx->fit, node, NULL));
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200499 break;
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200500 }
Michal Simekbf703df2019-10-22 16:39:11 +0200501
502 debug("%s: DT overlay %s applied\n", __func__,
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600503 fit_get_name(ctx->fit, node, NULL));
Michal Simekbf703df2019-10-22 16:39:11 +0200504 }
Heinrich Schuchardt939a9612020-04-20 12:44:01 +0200505 free(tmpbuffer);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200506 if (ret)
507 return ret;
Tom Rinid259d9c2023-01-10 11:19:28 -0500508#endif
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200509 /* Try to make space, so we can inject details on the loadables */
510 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
511 if (ret < 0)
512 return ret;
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200513
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200514 return ret;
515}
516
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600517static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200518 void *blob, struct spl_image_info *image)
519{
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100520 int ret = 0;
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200521 const char *name;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100522 int node;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200523
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600524 ret = spl_fit_get_image_name(ctx, "loadables", index, &name);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200525 if (ret < 0)
526 return ret;
527
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600528 node = spl_fit_get_image_node(ctx, "loadables", index);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200529
530 ret = fdt_record_loadable(blob, index, name, image->load_addr,
Simon Glassdff90432023-09-26 08:14:35 -0600531 image->size, image->entry_point,
532 fdt_getprop(ctx->fit, node, FIT_TYPE_PROP, NULL),
533 fdt_getprop(ctx->fit, node, FIT_OS_PROP, NULL),
534 fdt_getprop(ctx->fit, node, FIT_ARCH_PROP, NULL));
535
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200536 return ret;
537}
538
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500539static int spl_fit_image_is_fpga(const void *fit, int node)
540{
541 const char *type;
542
543 if (!IS_ENABLED(CONFIG_SPL_FPGA))
544 return 0;
545
546 type = fdt_getprop(fit, node, FIT_TYPE_PROP, NULL);
547 if (!type)
548 return 0;
549
550 return !strcmp(type, "fpga");
551}
552
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100553static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
554{
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600555 if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT))
556 return fit_image_get_os(fit, noffset, os);
Samuel Hollande646f512020-10-21 21:12:13 -0500557
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600558 const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
Samuel Hollande646f512020-10-21 21:12:13 -0500559 if (!name)
560 return -ENOENT;
561
562 /*
563 * We don't care what the type of the image actually is,
564 * only whether or not it is U-Boot. This saves some
565 * space by omitting the large table of OS types.
566 */
567 if (!strcmp(name, "u-boot"))
568 *os = IH_OS_U_BOOT;
569 else
570 *os = IH_OS_INVALID;
571
572 return 0;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100573}
574
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500575/*
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500576 * The purpose of the FIT load buffer is to provide a memory location that is
577 * independent of the load address of any FIT component.
578 */
579static void *spl_get_fit_load_buffer(size_t size)
580{
581 void *buf;
582
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +0200583 buf = malloc_cache_aligned(size);
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500584 if (!buf) {
585 pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size);
Leo Yu-Chi Liangecb7eaa2024-03-13 14:53:15 +0800586
587 if (IS_ENABLED(CONFIG_SPL_SYS_MALLOC))
588 pr_err("\tcheck CONFIG_SPL_SYS_MALLOC_SIZE\n");
589 else
590 pr_err("\tcheck CONFIG_SPL_SYS_MALLOC_F_LEN\n");
591
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500592 buf = spl_get_load_buffer(0, size);
593 }
594 return buf;
595}
596
Heiko Schocher7d7c3672021-08-17 08:17:18 +0200597__weak void *board_spl_fit_buffer_addr(ulong fit_size, int sectors, int bl_len)
598{
599 return spl_get_fit_load_buffer(sectors * bl_len);
600}
601
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500602/*
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500603 * Weak default function to allow customizing SPL fit loading for load-only
604 * use cases by allowing to skip the parsing/processing of the FIT contents
605 * (so that this can be done separately in a more customized fashion)
606 */
607__weak bool spl_load_simple_fit_skip_processing(void)
608{
609 return false;
610}
611
Heiko Schocher67e8f5d2021-08-06 06:44:26 +0200612/*
613 * Weak default function to allow fixes after fit header
614 * is loaded.
615 */
616__weak void *spl_load_simple_fit_fix_load(const void *fit)
617{
618 return (void *)fit;
619}
620
Alexandru Gagniuc69a99662021-03-29 12:05:13 -0500621static void warn_deprecated(const char *msg)
622{
623 printf("DEPRECATED: %s\n", msg);
Heinrich Schuchardt01e836c2024-06-18 08:32:30 +0200624 printf("\tSee https://fitspec.osfw.foundation/\n");
Alexandru Gagniuc69a99662021-03-29 12:05:13 -0500625}
626
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500627static int spl_fit_upload_fpga(struct spl_fit_info *ctx, int node,
628 struct spl_image_info *fpga_image)
629{
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500630 const char *compatible;
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500631 int ret;
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300632 int devnum = 0;
633 int flags = 0;
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500634
635 debug("FPGA bitstream at: %x, size: %x\n",
636 (u32)fpga_image->load_addr, fpga_image->size);
637
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500638 compatible = fdt_getprop(ctx->fit, node, "compatible", NULL);
Oleksandr Suvorov2fddf3e2022-07-22 17:16:09 +0300639 if (!compatible) {
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500640 warn_deprecated("'fpga' image without 'compatible' property");
Oleksandr Suvorov2fddf3e2022-07-22 17:16:09 +0300641 } else {
642 if (CONFIG_IS_ENABLED(FPGA_LOAD_SECURE))
643 flags = fpga_compatible2flag(devnum, compatible);
644 if (strcmp(compatible, "u-boot,fpga-legacy"))
645 debug("Ignoring compatible = %s property\n",
646 compatible);
647 }
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500648
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300649 ret = fpga_load(devnum, (void *)fpga_image->load_addr,
650 fpga_image->size, BIT_FULL, flags);
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500651 if (ret) {
652 printf("%s: Cannot load the image to the FPGA\n", __func__);
653 return ret;
654 }
655
656 puts("FPGA image loaded from FIT\n");
657
658 return 0;
659}
660
661static int spl_fit_load_fpga(struct spl_fit_info *ctx,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500662 struct spl_load_info *info, ulong offset)
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500663{
664 int node, ret;
665
666 struct spl_image_info fpga_image = {
667 .load_addr = 0,
668 };
669
670 node = spl_fit_get_image_node(ctx, "fpga", 0);
671 if (node < 0)
672 return node;
673
Alexandru Gagniuc69a99662021-03-29 12:05:13 -0500674 warn_deprecated("'fpga' property in config node. Use 'loadables'");
675
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500676 /* Load the image and set up the fpga_image structure */
Sean Anderson7d8d6132023-11-08 11:48:40 -0500677 ret = load_simple_fit(info, offset, ctx, node, &fpga_image);
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500678 if (ret) {
679 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
680 return ret;
681 }
682
683 return spl_fit_upload_fpga(ctx, node, &fpga_image);
684}
685
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600686static int spl_simple_fit_read(struct spl_fit_info *ctx,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500687 struct spl_load_info *info, ulong offset,
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600688 const void *fit_header)
Simon Glassa6131a22016-02-22 22:55:56 -0700689{
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600690 unsigned long count, size;
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600691 void *buf;
Simon Glassa6131a22016-02-22 22:55:56 -0700692
693 /*
York Suna058b8a2017-08-15 11:14:45 -0700694 * For FIT with external data, figure out where the external images
695 * start. This is the base for the data-offset properties in each
696 * image.
Simon Glassa6131a22016-02-22 22:55:56 -0700697 */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600698 size = ALIGN(fdt_totalsize(fit_header), 4);
Ye Li9d5b0f42018-11-17 09:10:25 +0000699 size = board_spl_fit_size_align(size);
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600700 ctx->ext_data_offset = ALIGN(size, 4);
Simon Glassa6131a22016-02-22 22:55:56 -0700701
702 /*
703 * So far we only have one block of data from the FIT. Read the entire
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500704 * thing, including that first block.
York Suna058b8a2017-08-15 11:14:45 -0700705 *
706 * For FIT with data embedded, data is loaded as part of FIT image.
707 * For FIT with external data, data is not loaded in this step.
Simon Glassa6131a22016-02-22 22:55:56 -0700708 */
Sean Anderson7d8d6132023-11-08 11:48:40 -0500709 size = get_aligned_image_size(info, size, 0);
710 buf = board_spl_fit_buffer_addr(size, size, 1);
Mikhail Kshevetskiy9035ecf2025-06-10 12:56:32 +0300711 if (!buf) {
712 /*
713 * We assume that none of the board will ever use 0x0 as a
714 * valid load address. Theoretically some board could use it,
715 * but this is extremely unlikely.
716 */
717 return -EIO;
718 }
Ye Li9d5b0f42018-11-17 09:10:25 +0000719
Sean Anderson7d8d6132023-11-08 11:48:40 -0500720 count = info->read(info, offset, size, buf);
Mikhail Kshevetskiy9035ecf2025-06-10 12:56:32 +0300721 if (!count) {
722 /*
723 * FIT could not be read. This means we should free the
724 * memory allocated by board_spl_fit_buffer_addr().
725 * Unfortunately, we don't know what memory allocation
726 * mechanism was used:
727 * - For the SPL_SYS_MALLOC_SIMPLE case nothing could
728 * be done. The memory just could not be freed.
729 * - For statically allocated memory buffer we can try
730 * to reuse previously allocated memory (example:
731 * board_spl_fit_buffer_addr() function from the
732 * file test/image/spl_load.c).
733 * - For normall malloc() -- memory leak can't be easily
734 * avoided. To somehow reduce memory consumption the
735 * next calls of board_spl_fit_buffer_addr() could
736 * reallocate previously allocated buffer and use
737 * them again. This is somethat similar to the approach
738 * used for statically allocated buffer.
739 *
740 * Please note:
741 * - FIT images with data placed outside of the FIT
742 * structure will cause small memory leak (several
743 * kilobytes),
744 * - FIT images with data placed inside to the FIT
745 * structure may cause huge memory leak (up to
746 * several megabytes). Do NOT use such images!
747 */
748 return -EIO;
749 }
750
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600751 ctx->fit = buf;
Sean Anderson7d8d6132023-11-08 11:48:40 -0500752 debug("fit read offset %lx, size=%lu, dst=%p, count=%lu\n",
753 offset, size, buf, count);
Simon Glassa6131a22016-02-22 22:55:56 -0700754
Mikhail Kshevetskiy9035ecf2025-06-10 12:56:32 +0300755 return 0;
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600756}
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500757
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600758static int spl_simple_fit_parse(struct spl_fit_info *ctx)
759{
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600760 /* Find the correct subnode under "/configurations" */
761 ctx->conf_node = fit_find_config_node(ctx->fit);
762 if (ctx->conf_node < 0)
763 return -EINVAL;
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100764
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600765 if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100766 printf("## Checking hash(es) for config %s ... ",
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600767 fit_get_name(ctx->fit, ctx->conf_node, NULL));
768 if (fit_config_verify(ctx->fit, ctx->conf_node))
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100769 return -EPERM;
770 puts("OK\n");
771 }
772
Andre Przywara525e9c92017-04-26 01:32:34 +0100773 /* find the node holding the images information */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600774 ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
775 if (ctx->images_node < 0) {
776 debug("%s: Cannot find /images node: %d\n", __func__,
777 ctx->images_node);
778 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700779 }
Marek Vasut33dd00e2018-05-12 22:25:28 +0200780
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600781 return 0;
782}
783
784int spl_load_simple_fit(struct spl_image_info *spl_image,
Sean Anderson7d8d6132023-11-08 11:48:40 -0500785 struct spl_load_info *info, ulong offset, void *fit)
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600786{
787 struct spl_image_info image_info;
788 struct spl_fit_info ctx;
789 int node = -1;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600790 int ret;
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600791 int index = 0;
792 int firmware_node;
793
Sean Anderson7d8d6132023-11-08 11:48:40 -0500794 ret = spl_simple_fit_read(&ctx, info, offset, fit);
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600795 if (ret < 0)
796 return ret;
797
798 /* skip further processing if requested to enable load-only use cases */
799 if (spl_load_simple_fit_skip_processing())
800 return 0;
801
Heiko Schocher67e8f5d2021-08-06 06:44:26 +0200802 ctx.fit = spl_load_simple_fit_fix_load(ctx.fit);
803
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600804 ret = spl_simple_fit_parse(&ctx);
805 if (ret < 0)
806 return ret;
807
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500808 if (IS_ENABLED(CONFIG_SPL_FPGA))
Sean Anderson7d8d6132023-11-08 11:48:40 -0500809 spl_fit_load_fpga(&ctx, info, offset);
Andre Przywara525e9c92017-04-26 01:32:34 +0100810
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200811 /*
812 * Find the U-Boot image using the following search order:
813 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
814 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
815 * - fall back to using the first 'loadables' entry
816 */
817 if (node < 0)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600818 node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600819
820 if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT))
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600821 node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600822
Andre Przywara525e9c92017-04-26 01:32:34 +0100823 if (node < 0) {
824 debug("could not find firmware image, trying loadables...\n");
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600825 node = spl_fit_get_image_node(&ctx, "loadables", 0);
Andre Przywarae7c58562017-04-26 01:32:37 +0100826 /*
827 * If we pick the U-Boot image from "loadables", start at
828 * the second image when later loading additional images.
829 */
830 index = 1;
Andre Przywara525e9c92017-04-26 01:32:34 +0100831 }
Simon Glassa6131a22016-02-22 22:55:56 -0700832 if (node < 0) {
Andre Przywara525e9c92017-04-26 01:32:34 +0100833 debug("%s: Cannot find u-boot image node: %d\n",
834 __func__, node);
Simon Glassa6131a22016-02-22 22:55:56 -0700835 return -1;
836 }
837
Andre Przywara29053e92017-04-26 01:32:36 +0100838 /* Load the image and set up the spl_image structure */
Sean Anderson7d8d6132023-11-08 11:48:40 -0500839 ret = load_simple_fit(info, offset, &ctx, node, spl_image);
Andre Przywara29053e92017-04-26 01:32:36 +0100840 if (ret)
841 return ret;
Daniel Allredcf839bd2016-06-27 09:19:21 -0500842
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200843 /*
844 * For backward compatibility, we treat the first node that is
845 * as a U-Boot image, if no OS-type has been declared.
846 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600847 if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
York Suna058b8a2017-08-15 11:14:45 -0700848 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600849 else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200850 spl_image->os = IH_OS_U_BOOT;
Simon Glassa6131a22016-02-22 22:55:56 -0700851
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200852 /*
853 * Booting a next-stage U-Boot may require us to append the FDT.
854 * We allow this to fail, as the U-Boot image might embed its FDT.
855 */
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600856 if (os_takes_devicetree(spl_image->os)) {
Sean Anderson7d8d6132023-11-08 11:48:40 -0500857 ret = spl_fit_append_fdt(spl_image, info, offset, &ctx);
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600858 if (ret < 0 && spl_image->os != IH_OS_U_BOOT)
Dario Binacchifaf8d142020-05-27 13:56:19 +0200859 return ret;
860 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100861
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200862 firmware_node = node;
Andre Przywarae7c58562017-04-26 01:32:37 +0100863 /* Now check if there are more images for us to load */
864 for (; ; index++) {
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200865 uint8_t os_type = IH_OS_INVALID;
866
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600867 node = spl_fit_get_image_node(&ctx, "loadables", index);
Andre Przywarae7c58562017-04-26 01:32:37 +0100868 if (node < 0)
869 break;
870
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200871 /*
872 * if the firmware is also a loadable, skip it because
873 * it already has been loaded. This is typically the case with
874 * u-boot.img generated by mkimage.
875 */
876 if (firmware_node == node)
877 continue;
878
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500879 image_info.load_addr = 0;
Sean Anderson7d8d6132023-11-08 11:48:40 -0500880 ret = load_simple_fit(info, offset, &ctx, node, &image_info);
Mikhail Kshevetskiyaa17b392025-06-10 12:56:31 +0300881 if (ret < 0 && ret != -EBADSLT) {
Philippe Reynesba87da42020-11-24 16:15:05 +0100882 printf("%s: can't load image loadables index %d (ret = %d)\n",
883 __func__, index, ret);
884 return ret;
885 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100886
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500887 if (spl_fit_image_is_fpga(ctx.fit, node))
888 spl_fit_upload_fpga(&ctx, node, &image_info);
889
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600890 if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200891 debug("Loadable is %s\n", genimg_get_os_name(os_type));
892
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600893 if (os_takes_devicetree(os_type)) {
Sean Anderson7d8d6132023-11-08 11:48:40 -0500894 spl_fit_append_fdt(&image_info, info, offset, &ctx);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200895 spl_image->fdt_addr = image_info.fdt_addr;
896 }
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200897
Andre Przywarae7c58562017-04-26 01:32:37 +0100898 /*
899 * If the "firmware" image did not provide an entry point,
900 * use the first valid entry point from the loadables.
901 */
902 if (spl_image->entry_point == FDT_ERROR &&
903 image_info.entry_point != FDT_ERROR)
904 spl_image->entry_point = image_info.entry_point;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200905
906 /* Record our loadables into the FDT */
Simon Glassd4315522025-01-26 11:43:28 -0700907 if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) &&
908 xpl_get_fdt_update(info) && spl_image->fdt_addr)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600909 spl_fit_record_loadable(&ctx, index,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200910 spl_image->fdt_addr,
911 &image_info);
Andre Przywarae7c58562017-04-26 01:32:37 +0100912 }
913
914 /*
Jesse Taube93ee5c82023-08-24 21:59:48 -0400915 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
Andre Przywarae7c58562017-04-26 01:32:37 +0100916 * Makefile will set it to 0 and it will end up as the entry point
917 * here. What it actually means is: use the load address.
918 */
919 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
920 spl_image->entry_point = spl_image->load_addr;
921
Ye Li9d5b0f42018-11-17 09:10:25 +0000922 spl_image->flags |= SPL_FIT_FOUND;
Simon Glass9b5bfba2024-08-07 16:47:33 -0600923 upl_set_fit_info(map_to_sysmem(ctx.fit), ctx.conf_node,
924 spl_image->entry_point);
Ye Li9d5b0f42018-11-17 09:10:25 +0000925
Andre Przywarae7c58562017-04-26 01:32:37 +0100926 return 0;
Simon Glassa6131a22016-02-22 22:55:56 -0700927}
Simon Glassc0bd55e2023-09-26 08:14:34 -0600928
929/* Parse and load full fitImage in SPL */
930int spl_load_fit_image(struct spl_image_info *spl_image,
931 const struct legacy_img_hdr *header)
932{
933 struct bootm_headers images;
934 const char *fit_uname_config = NULL;
Simon Glass9a9097f2024-12-19 11:29:00 -0700935 ulong fdt_hack;
Simon Glassc0bd55e2023-09-26 08:14:34 -0600936 const char *uname;
937 ulong fw_data = 0, dt_data = 0, img_data = 0;
938 ulong fw_len = 0, dt_len = 0, img_len = 0;
939 int idx, conf_noffset;
940 int ret;
941
942#ifdef CONFIG_SPL_FIT_SIGNATURE
943 images.verify = 1;
944#endif
Sean Anderson5ff77722023-10-14 16:47:55 -0400945 ret = fit_image_load(&images, virt_to_phys((void *)header),
Simon Glassc0bd55e2023-09-26 08:14:34 -0600946 NULL, &fit_uname_config,
947 IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
948 FIT_LOAD_OPTIONAL, &fw_data, &fw_len);
949 if (ret >= 0) {
950 printf("DEPRECATED: 'standalone = ' property.");
951 printf("Please use either 'firmware =' or 'kernel ='\n");
952 } else {
Sean Anderson5ff77722023-10-14 16:47:55 -0400953 ret = fit_image_load(&images, virt_to_phys((void *)header),
954 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
Simon Glassc0bd55e2023-09-26 08:14:34 -0600955 IH_TYPE_FIRMWARE, -1, FIT_LOAD_OPTIONAL,
956 &fw_data, &fw_len);
957 }
958
959 if (ret < 0) {
Sean Anderson5ff77722023-10-14 16:47:55 -0400960 ret = fit_image_load(&images, virt_to_phys((void *)header),
961 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
Simon Glassc0bd55e2023-09-26 08:14:34 -0600962 IH_TYPE_KERNEL, -1, FIT_LOAD_OPTIONAL,
963 &fw_data, &fw_len);
964 }
965
966 if (ret < 0)
967 return ret;
968
969 spl_image->size = fw_len;
Simon Glassc0bd55e2023-09-26 08:14:34 -0600970 spl_image->load_addr = fw_data;
Sean Andersone99b2cd2023-10-14 16:47:39 -0400971 if (fit_image_get_entry(header, ret, &spl_image->entry_point))
972 spl_image->entry_point = fw_data;
Simon Glassc0bd55e2023-09-26 08:14:34 -0600973 if (fit_image_get_os(header, ret, &spl_image->os))
974 spl_image->os = IH_OS_INVALID;
975 spl_image->name = genimg_get_os_name(spl_image->os);
976
Simon Glass8bc93262024-09-29 19:49:55 -0600977 debug(PHASE_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
Simon Glassc0bd55e2023-09-26 08:14:34 -0600978 spl_image->name, spl_image->load_addr, spl_image->size);
979
980#ifdef CONFIG_SPL_FIT_SIGNATURE
981 images.verify = 1;
982#endif
Sean Anderson5ff77722023-10-14 16:47:55 -0400983 ret = fit_image_load(&images, virt_to_phys((void *)header), NULL,
984 &fit_uname_config, IH_ARCH_DEFAULT, IH_TYPE_FLATDT,
985 -1, FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
Simon Glassc0bd55e2023-09-26 08:14:34 -0600986 if (ret >= 0) {
987 spl_image->fdt_addr = (void *)dt_data;
988
989 if (spl_image->os == IH_OS_U_BOOT) {
990 /* HACK: U-Boot expects FDT at a specific address */
991 fdt_hack = spl_image->load_addr + spl_image->size;
992 fdt_hack = (fdt_hack + 3) & ~3;
993 debug("Relocating FDT to %p\n", spl_image->fdt_addr);
994 memcpy((void *)fdt_hack, spl_image->fdt_addr, dt_len);
995 }
996 }
997
998 conf_noffset = fit_conf_get_node((const void *)header,
999 fit_uname_config);
1000 if (conf_noffset < 0)
1001 return 0;
1002
1003 for (idx = 0;
1004 uname = fdt_stringlist_get((const void *)header, conf_noffset,
1005 FIT_LOADABLE_PROP, idx,
1006 NULL), uname;
1007 idx++) {
1008#ifdef CONFIG_SPL_FIT_SIGNATURE
1009 images.verify = 1;
1010#endif
1011 ret = fit_image_load(&images, (ulong)header,
1012 &uname, &fit_uname_config,
1013 IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
1014 FIT_LOAD_OPTIONAL_NON_ZERO,
1015 &img_data, &img_len);
1016 if (ret < 0)
1017 return ret;
1018 }
Simon Glass32668282024-08-07 16:47:32 -06001019 spl_image->flags |= SPL_FIT_FOUND;
Simon Glassc0bd55e2023-09-26 08:14:34 -06001020
Simon Glass9b5bfba2024-08-07 16:47:33 -06001021 upl_set_fit_info(map_to_sysmem(header), conf_noffset,
1022 spl_image->entry_point);
1023
Simon Glassc0bd55e2023-09-26 08:14:34 -06001024 return 0;
1025}