blob: c026e1a194571e47c41417165e349ce6d41437ac [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassa6131a22016-02-22 22:55:56 -07002/*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassa6131a22016-02-22 22:55:56 -07005 */
6
7#include <common.h>
8#include <errno.h>
Michal Simekf707b5a2018-07-18 14:33:15 +02009#include <fpga.h>
Simon Glass1a974af2019-08-01 09:46:36 -060010#include <gzip.h>
Simon Glassa6131a22016-02-22 22:55:56 -070011#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +020013#include <memalign.h>
Simon Glass2192e982021-03-07 17:35:14 -070014#include <mapmem.h>
Simon Glassa6131a22016-02-22 22:55:56 -070015#include <spl.h>
Simon Glass458b66a2020-11-05 06:32:05 -070016#include <sysinfo.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040019#include <asm/io.h>
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +020020#include <linux/libfdt.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Simon Glassa6131a22016-02-22 22:55:56 -070022
Lukas Auer80afee72019-08-21 21:14:42 +020023DECLARE_GLOBAL_DATA_PTR;
24
Alexandru Gagniuc23243c92021-01-20 10:46:50 -060025struct spl_fit_info {
26 const void *fit; /* Pointer to a valid FIT blob */
27 size_t ext_data_offset; /* Offset to FIT external data (end of FIT) */
28 int images_node; /* FDT offset to "/images" node */
Alexandru Gagniucfa11a442021-01-20 10:46:53 -060029 int conf_node; /* FDT offset to selected configuration node */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -060030};
31
Ye Li9d5b0f42018-11-17 09:10:25 +000032__weak ulong board_spl_fit_size_align(ulong size)
33{
34 return size;
35}
36
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020037static int find_node_from_desc(const void *fit, int node, const char *str)
38{
39 int child;
40
41 if (node < 0)
42 return -EINVAL;
43
44 /* iterate the FIT nodes and find a matching description */
45 for (child = fdt_first_subnode(fit, node); child >= 0;
46 child = fdt_next_subnode(fit, child)) {
47 int len;
Simon Glassdff90432023-09-26 08:14:35 -060048 const char *desc = fdt_getprop(fit, child, FIT_DESC_PROP, &len);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020049
50 if (!desc)
51 continue;
52
53 if (!strcmp(desc, str))
54 return child;
55 }
56
57 return -ENOENT;
58}
59
Andre Przywara525e9c92017-04-26 01:32:34 +010060/**
Philipp Tomsiche61eff92017-09-13 21:29:34 +020061 * spl_fit_get_image_name(): By using the matching configuration subnode,
Andre Przywara525e9c92017-04-26 01:32:34 +010062 * retrieve the name of an image, specified by a property name and an index
63 * into that.
64 * @fit: Pointer to the FDT blob.
65 * @images: Offset of the /images subnode.
66 * @type: Name of the property within the configuration subnode.
67 * @index: Index into the list of strings in this property.
Philipp Tomsiche61eff92017-09-13 21:29:34 +020068 * @outname: Name of the image
Andre Przywara525e9c92017-04-26 01:32:34 +010069 *
Philipp Tomsiche61eff92017-09-13 21:29:34 +020070 * Return: 0 on success, or a negative error number
Andre Przywara525e9c92017-04-26 01:32:34 +010071 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -060072static int spl_fit_get_image_name(const struct spl_fit_info *ctx,
Philipp Tomsiche61eff92017-09-13 21:29:34 +020073 const char *type, int index,
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +020074 const char **outname)
Andre Przywara1e329b62017-04-26 01:32:33 +010075{
Simon Glass458b66a2020-11-05 06:32:05 -070076 struct udevice *sysinfo;
Andre Przywara1e329b62017-04-26 01:32:33 +010077 const char *name, *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +020078 __maybe_unused int node;
Andre Przywara1e329b62017-04-26 01:32:33 +010079 int len, i;
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020080 bool found = true;
Andre Przywara1e329b62017-04-26 01:32:33 +010081
Alexandru Gagniucfa11a442021-01-20 10:46:53 -060082 name = fdt_getprop(ctx->fit, ctx->conf_node, type, &len);
Andre Przywara1e329b62017-04-26 01:32:33 +010083 if (!name) {
84 debug("cannot find property '%s': %d\n", type, len);
85 return -EINVAL;
86 }
Simon Glassa6131a22016-02-22 22:55:56 -070087
Andre Przywara1e329b62017-04-26 01:32:33 +010088 str = name;
89 for (i = 0; i < index; i++) {
90 str = strchr(str, '\0') + 1;
91 if (!str || (str - name >= len)) {
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020092 found = false;
93 break;
Andre Przywara1e329b62017-04-26 01:32:33 +010094 }
Simon Glassa6131a22016-02-22 22:55:56 -070095 }
96
Simon Glass458b66a2020-11-05 06:32:05 -070097 if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) {
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +020098 int rc;
99 /*
Simon Glass458b66a2020-11-05 06:32:05 -0700100 * no string in the property for this index. Check if the
101 * sysinfo-level code can supply one.
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200102 */
Sean Anderson6ccf10a2021-04-20 10:50:56 -0400103 rc = sysinfo_detect(sysinfo);
104 if (rc)
105 return rc;
106
Simon Glass458b66a2020-11-05 06:32:05 -0700107 rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
108 &str);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200109 if (rc && rc != -ENOENT)
110 return rc;
111
112 if (!rc) {
113 /*
Simon Glass458b66a2020-11-05 06:32:05 -0700114 * The sysinfo provided a name for a loadable.
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200115 * Try to match it against the description properties
116 * first. If no matching node is found, use it as a
117 * node name.
118 */
119 int node;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600120 int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200121
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600122 node = find_node_from_desc(ctx->fit, images, str);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200123 if (node > 0)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600124 str = fdt_get_name(ctx->fit, node, NULL);
Jean-Jacques Hiblot9037b7f2019-10-22 16:39:22 +0200125
126 found = true;
127 }
128 }
129
130 if (!found) {
131 debug("no string for index %d\n", index);
132 return -E2BIG;
133 }
134
135 *outname = str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200136 return 0;
137}
138
139/**
140 * spl_fit_get_image_node(): By using the matching configuration subnode,
141 * retrieve the name of an image, specified by a property name and an index
142 * into that.
143 * @fit: Pointer to the FDT blob.
144 * @images: Offset of the /images subnode.
145 * @type: Name of the property within the configuration subnode.
146 * @index: Index into the list of strings in this property.
147 *
148 * Return: the node offset of the respective image node or a negative
149 * error number.
150 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600151static int spl_fit_get_image_node(const struct spl_fit_info *ctx,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200152 const char *type, int index)
153{
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200154 const char *str;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200155 int err;
156 int node;
157
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600158 err = spl_fit_get_image_name(ctx, type, index, &str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200159 if (err)
160 return err;
161
Andre Przywara1e329b62017-04-26 01:32:33 +0100162 debug("%s: '%s'\n", type, str);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200163
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600164 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
Andre Przywara1e329b62017-04-26 01:32:33 +0100165 if (node < 0) {
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200166 pr_err("cannot find image node '%s': %d\n", str, node);
Andre Przywara1e329b62017-04-26 01:32:33 +0100167 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700168 }
Simon Glassa6131a22016-02-22 22:55:56 -0700169
Andre Przywara525e9c92017-04-26 01:32:34 +0100170 return node;
Simon Glassa6131a22016-02-22 22:55:56 -0700171}
172
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530173static int get_aligned_image_offset(struct spl_load_info *info, int offset)
174{
175 /*
176 * If it is a FS read, get the first address before offset which is
177 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
178 * block number to which offset belongs.
179 */
180 if (info->filename)
181 return offset & ~(ARCH_DMA_MINALIGN - 1);
182
183 return offset / info->bl_len;
184}
185
186static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
187{
188 /*
189 * If it is a FS read, get the difference between the offset and
190 * the first address before offset which is aligned to
191 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
192 * block.
193 */
194 if (info->filename)
195 return offset & (ARCH_DMA_MINALIGN - 1);
196
197 return offset % info->bl_len;
198}
199
200static int get_aligned_image_size(struct spl_load_info *info, int data_size,
201 int offset)
202{
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530203 data_size = data_size + get_aligned_image_overhead(info, offset);
204
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530205 if (info->filename)
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530206 return data_size;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530207
208 return (data_size + info->bl_len - 1) / info->bl_len;
209}
210
Andre Przywara29053e92017-04-26 01:32:36 +0100211/**
Simon Glass205ff7b2023-09-26 08:14:33 -0600212 * load_simple_fit(): load the image described in a certain FIT node
Andre Przywara29053e92017-04-26 01:32:36 +0100213 * @info: points to information about the device to load data from
214 * @sector: the start sector of the FIT image on the device
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600215 * @ctx: points to the FIT context structure
Andre Przywara29053e92017-04-26 01:32:36 +0100216 * @node: offset of the DT node describing the image to load (relative
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200217 * to @fit)
Andre Przywara29053e92017-04-26 01:32:36 +0100218 * @image_info: will be filled with information about the loaded image
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200219 * If the FIT node does not contain a "load" (address) property,
220 * the image gets loaded to the address pointed to by the
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500221 * load_addr member in this struct, if load_addr is not 0
Andre Przywara29053e92017-04-26 01:32:36 +0100222 *
223 * Return: 0 on success or a negative error number.
224 */
Simon Glass205ff7b2023-09-26 08:14:33 -0600225static int load_simple_fit(struct spl_load_info *info, ulong sector,
226 const struct spl_fit_info *ctx, int node,
227 struct spl_image_info *image_info)
Andre Przywara29053e92017-04-26 01:32:36 +0100228{
Michal Simekf707b5a2018-07-18 14:33:15 +0200229 int offset;
Andre Przywara29053e92017-04-26 01:32:36 +0100230 size_t length;
York Sunadf99fa2017-08-15 11:14:44 -0700231 int len;
York Sun25d65f12017-09-15 08:21:13 -0700232 ulong size;
Simon Glass2192e982021-03-07 17:35:14 -0700233 ulong load_addr;
234 void *load_ptr;
Andre Przywara29053e92017-04-26 01:32:36 +0100235 void *src;
236 ulong overhead;
237 int nr_sectors;
York Suna6945fe2017-08-15 11:14:43 -0700238 uint8_t image_comp = -1, type = -1;
York Sunadf99fa2017-08-15 11:14:44 -0700239 const void *data;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600240 const void *fit = ctx->fit;
Peng Fan8876c7e2017-12-05 13:20:59 +0800241 bool external_data = false;
York Suna6945fe2017-08-15 11:14:43 -0700242
Michal Simek1aab1142020-09-09 14:41:56 +0200243 if (IS_ENABLED(CONFIG_SPL_FPGA) ||
Manoj Sai2b2e6282023-09-18 00:56:25 +0530244 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && spl_decompression_enabled())) {
Marek Vasut72bc5d52018-06-01 23:19:29 +0200245 if (fit_image_get_type(fit, node, &type))
246 puts("Cannot get image type.\n");
247 else
248 debug("%s ", genimg_get_type_name(type));
249 }
250
Manoj Sai2b2e6282023-09-18 00:56:25 +0530251 if (spl_decompression_enabled()) {
Klaus H. Sorensen4a9a0422019-12-11 11:03:33 +0000252 fit_image_get_comp(fit, node, &image_comp);
253 debug("%s ", genimg_get_comp_name(image_comp));
York Suna6945fe2017-08-15 11:14:43 -0700254 }
Andre Przywara29053e92017-04-26 01:32:36 +0100255
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500256 if (fit_image_get_load(fit, node, &load_addr)) {
257 if (!image_info->load_addr) {
258 printf("Can't load %s: No load address and no buffer\n",
259 fit_get_name(fit, node, NULL));
260 return -ENOBUFS;
261 }
Andre Przywara29053e92017-04-26 01:32:36 +0100262 load_addr = image_info->load_addr;
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500263 }
Andre Przywara29053e92017-04-26 01:32:36 +0100264
Peng Fan8876c7e2017-12-05 13:20:59 +0800265 if (!fit_image_get_data_position(fit, node, &offset)) {
266 external_data = true;
267 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600268 offset += ctx->ext_data_offset;
Peng Fan8876c7e2017-12-05 13:20:59 +0800269 external_data = true;
270 }
271
272 if (external_data) {
Simon Glass2192e982021-03-07 17:35:14 -0700273 void *src_ptr;
274
Peng Fan8876c7e2017-12-05 13:20:59 +0800275 /* External data */
York Sunadf99fa2017-08-15 11:14:44 -0700276 if (fit_image_get_data_size(fit, node, &len))
277 return -ENOENT;
Andre Przywara29053e92017-04-26 01:32:36 +0100278
Nishanth Menone4cb6452021-10-19 12:32:29 -0500279 /* Dont bother to copy 0 byte data, but warn, though */
280 if (!len) {
281 log_warning("%s: Skip load '%s': image size is 0!\n",
282 __func__, fit_get_name(fit, node, NULL));
283 return 0;
284 }
285
Manoj Saia8560992023-09-18 00:56:26 +0530286 if (spl_decompression_enabled() &&
287 (image_comp == IH_COMP_GZIP || image_comp == IH_COMP_LZMA))
Manoj Sai2b2e6282023-09-18 00:56:25 +0530288 src_ptr = map_sysmem(ALIGN(CONFIG_SYS_LOAD_ADDR, ARCH_DMA_MINALIGN), len);
289 else
290 src_ptr = map_sysmem(ALIGN(load_addr, ARCH_DMA_MINALIGN), len);
York Sunadf99fa2017-08-15 11:14:44 -0700291 length = len;
292
293 overhead = get_aligned_image_overhead(info, offset);
294 nr_sectors = get_aligned_image_size(info, length, offset);
295
Michal Simekf707b5a2018-07-18 14:33:15 +0200296 if (info->read(info,
297 sector + get_aligned_image_offset(info, offset),
Simon Glass2192e982021-03-07 17:35:14 -0700298 nr_sectors, src_ptr) != nr_sectors)
York Sunadf99fa2017-08-15 11:14:44 -0700299 return -EIO;
300
Simon Glass2192e982021-03-07 17:35:14 -0700301 debug("External data: dst=%p, offset=%x, size=%lx\n",
302 src_ptr, offset, (unsigned long)length);
303 src = src_ptr + overhead;
York Sunadf99fa2017-08-15 11:14:44 -0700304 } else {
305 /* Embedded data */
306 if (fit_image_get_data(fit, node, &data, &length)) {
307 puts("Cannot get image data/size\n");
308 return -ENOENT;
309 }
310 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
311 (unsigned long)length);
Simon Glass2192e982021-03-07 17:35:14 -0700312 src = (void *)data; /* cast away const */
York Sunadf99fa2017-08-15 11:14:44 -0700313 }
Andre Przywara29053e92017-04-26 01:32:36 +0100314
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600315 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
316 printf("## Checking hash(es) for Image %s ... ",
317 fit_get_name(fit, node, NULL));
Simon Glasse10e2ee2021-11-12 12:28:10 -0700318 if (!fit_image_verify_with_data(fit, node, gd_fdt_blob(), src,
319 length))
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600320 return -EPERM;
321 puts("OK\n");
322 }
Ben Whitten54a91192018-06-07 11:37:27 +0100323
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600324 if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS))
Lokesh Vutlab36dd3e2021-06-11 11:45:05 +0300325 board_fit_image_post_process(fit, node, &src, &length);
Andre Przywara29053e92017-04-26 01:32:36 +0100326
Simon Glass2192e982021-03-07 17:35:14 -0700327 load_ptr = map_sysmem(load_addr, length);
Michal Simekf354c3f2018-07-24 15:05:00 +0200328 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
York Sun25d65f12017-09-15 08:21:13 -0700329 size = length;
Simon Glass2192e982021-03-07 17:35:14 -0700330 if (gunzip(load_ptr, CONFIG_SYS_BOOTM_LEN, src, &size)) {
York Suna6945fe2017-08-15 11:14:43 -0700331 puts("Uncompressing error\n");
332 return -EIO;
333 }
York Sun25d65f12017-09-15 08:21:13 -0700334 length = size;
Manoj Saia8560992023-09-18 00:56:26 +0530335 } else if (IS_ENABLED(CONFIG_SPL_LZMA) && image_comp == IH_COMP_LZMA) {
336 size = CONFIG_SYS_BOOTM_LEN;
337 ulong loadEnd;
338
339 if (image_decomp(IH_COMP_LZMA, CONFIG_SYS_LOAD_ADDR, 0, 0,
340 load_ptr, src, length, size, &loadEnd)) {
341 puts("Uncompressing error\n");
342 return -EIO;
343 }
344 length = loadEnd - CONFIG_SYS_LOAD_ADDR;
York Suna6945fe2017-08-15 11:14:43 -0700345 } else {
Simon Glass2192e982021-03-07 17:35:14 -0700346 memcpy(load_ptr, src, length);
York Suna6945fe2017-08-15 11:14:43 -0700347 }
Andre Przywara29053e92017-04-26 01:32:36 +0100348
349 if (image_info) {
Michal Simek9e64a552020-09-03 11:24:28 +0200350 ulong entry_point;
351
Andre Przywara29053e92017-04-26 01:32:36 +0100352 image_info->load_addr = load_addr;
353 image_info->size = length;
Michal Simek9e64a552020-09-03 11:24:28 +0200354
355 if (!fit_image_get_entry(fit, node, &entry_point))
356 image_info->entry_point = entry_point;
357 else
358 image_info->entry_point = FDT_ERROR;
Andre Przywara29053e92017-04-26 01:32:36 +0100359 }
360
361 return 0;
362}
363
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600364static bool os_takes_devicetree(uint8_t os)
365{
366 switch (os) {
367 case IH_OS_U_BOOT:
368 return true;
369 case IH_OS_LINUX:
370 return IS_ENABLED(CONFIG_SPL_OS_BOOT);
371 default:
372 return false;
373 }
374}
375
Marek Vasuta0494272023-09-21 20:44:16 +0200376__weak int board_spl_fit_append_fdt_skip(const char *name)
377{
378 return 0; /* Do not skip */
379}
380
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200381static int spl_fit_append_fdt(struct spl_image_info *spl_image,
382 struct spl_load_info *info, ulong sector,
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600383 const struct spl_fit_info *ctx)
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200384{
385 struct spl_image_info image_info;
Michal Simekbf703df2019-10-22 16:39:11 +0200386 int node, ret = 0, index = 0;
Lukas Auer80afee72019-08-21 21:14:42 +0200387
388 /*
389 * Use the address following the image as target address for the
Marek Vasutc27cbe82020-10-19 23:40:26 +0200390 * device tree.
Lukas Auer80afee72019-08-21 21:14:42 +0200391 */
Marek Vasutc27cbe82020-10-19 23:40:26 +0200392 image_info.load_addr = spl_image->load_addr + spl_image->size;
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200393
394 /* Figure out which device tree the board wants to use */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600395 node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200396 if (node < 0) {
Sean Anderson5ff77722023-10-14 16:47:55 -0400397 size_t size;
398
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200399 debug("%s: cannot find FDT node\n", __func__);
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200400
Lukas Auer80afee72019-08-21 21:14:42 +0200401 /*
402 * U-Boot did not find a device tree inside the FIT image. Use
403 * the U-Boot device tree instead.
404 */
Sean Anderson5ff77722023-10-14 16:47:55 -0400405 if (!gd->fdt_blob)
Lukas Auer80afee72019-08-21 21:14:42 +0200406 return node;
Sean Anderson5ff77722023-10-14 16:47:55 -0400407
408 /*
409 * Make the load-address of the FDT available for the SPL
410 * framework
411 */
412 size = fdt_totalsize(gd->fdt_blob);
413 spl_image->fdt_addr = map_sysmem(image_info.load_addr, size);
414 memcpy(spl_image->fdt_addr, gd->fdt_blob, size);
Lukas Auer80afee72019-08-21 21:14:42 +0200415 } else {
Simon Glass205ff7b2023-09-26 08:14:33 -0600416 ret = load_simple_fit(info, sector, ctx, node, &image_info);
Lukas Auer80afee72019-08-21 21:14:42 +0200417 if (ret < 0)
418 return ret;
Sean Anderson5ff77722023-10-14 16:47:55 -0400419
420 spl_image->fdt_addr = phys_to_virt(image_info.load_addr);
Lukas Auer80afee72019-08-21 21:14:42 +0200421 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200422
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600423 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
424 return 0;
425
Tom Rinid259d9c2023-01-10 11:19:28 -0500426#if CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200427 void *tmpbuffer = NULL;
428
Michal Simekbf703df2019-10-22 16:39:11 +0200429 for (; ; index++) {
Marek Vasuta0494272023-09-21 20:44:16 +0200430 const char *str;
431
432 ret = spl_fit_get_image_name(ctx, FIT_FDT_PROP, index, &str);
433 if (ret == -E2BIG) {
Michal Simekbf703df2019-10-22 16:39:11 +0200434 debug("%s: No additional FDT node\n", __func__);
Marek Vasuta0494272023-09-21 20:44:16 +0200435 ret = 0;
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200436 break;
Marek Vasuta0494272023-09-21 20:44:16 +0200437 } else if (ret < 0) {
438 continue;
439 }
440
441 ret = board_spl_fit_append_fdt_skip(str);
442 if (ret)
443 continue;
444
445 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
446 if (node < 0) {
Jean-Jacques Hiblotc6ab6a12019-10-22 16:39:14 +0200447 debug("%s: unable to find FDT node %d\n",
448 __func__, index);
449 continue;
Michal Simekbf703df2019-10-22 16:39:11 +0200450 }
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200451
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200452 if (!tmpbuffer) {
453 /*
454 * allocate memory to store the DT overlay
455 * before it is applied. It may not be used
456 * depending on how the overlay is stored, so
457 * don't fail yet if the allocation failed.
458 */
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +0200459 size_t size = CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ;
460
461 tmpbuffer = malloc_cache_aligned(size);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200462 if (!tmpbuffer)
463 debug("%s: unable to allocate space for overlays\n",
464 __func__);
465 }
466 image_info.load_addr = (ulong)tmpbuffer;
Simon Glass205ff7b2023-09-26 08:14:33 -0600467 ret = load_simple_fit(info, sector, ctx, node,
468 &image_info);
Michal Simekbf703df2019-10-22 16:39:11 +0200469 if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200470 break;
Michal Simekbf703df2019-10-22 16:39:11 +0200471
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200472 /* Make room in FDT for changes from the overlay */
473 ret = fdt_increase_size(spl_image->fdt_addr,
474 image_info.size);
475 if (ret < 0)
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200476 break;
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200477
Michal Simekbf703df2019-10-22 16:39:11 +0200478 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
479 (void *)image_info.load_addr);
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200480 if (ret) {
481 pr_err("failed to apply DT overlay %s\n",
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600482 fit_get_name(ctx->fit, node, NULL));
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200483 break;
Jean-Jacques Hiblot9443a732019-10-22 16:39:15 +0200484 }
Michal Simekbf703df2019-10-22 16:39:11 +0200485
486 debug("%s: DT overlay %s applied\n", __func__,
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600487 fit_get_name(ctx->fit, node, NULL));
Michal Simekbf703df2019-10-22 16:39:11 +0200488 }
Heinrich Schuchardt939a9612020-04-20 12:44:01 +0200489 free(tmpbuffer);
Jean-Jacques Hiblot0da58152019-10-22 16:39:13 +0200490 if (ret)
491 return ret;
Tom Rinid259d9c2023-01-10 11:19:28 -0500492#endif
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200493 /* Try to make space, so we can inject details on the loadables */
494 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
495 if (ret < 0)
496 return ret;
Jean-Jacques Hiblot74addb62019-10-22 16:39:12 +0200497
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200498 return ret;
499}
500
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600501static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200502 void *blob, struct spl_image_info *image)
503{
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100504 int ret = 0;
Jean-Jacques Hiblot64e82dc2019-10-22 16:39:17 +0200505 const char *name;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100506 int node;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200507
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600508 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
509 return 0;
510
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600511 ret = spl_fit_get_image_name(ctx, "loadables", index, &name);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200512 if (ret < 0)
513 return ret;
514
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600515 node = spl_fit_get_image_node(ctx, "loadables", index);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200516
517 ret = fdt_record_loadable(blob, index, name, image->load_addr,
Simon Glassdff90432023-09-26 08:14:35 -0600518 image->size, image->entry_point,
519 fdt_getprop(ctx->fit, node, FIT_TYPE_PROP, NULL),
520 fdt_getprop(ctx->fit, node, FIT_OS_PROP, NULL),
521 fdt_getprop(ctx->fit, node, FIT_ARCH_PROP, NULL));
522
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200523 return ret;
524}
525
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500526static int spl_fit_image_is_fpga(const void *fit, int node)
527{
528 const char *type;
529
530 if (!IS_ENABLED(CONFIG_SPL_FPGA))
531 return 0;
532
533 type = fdt_getprop(fit, node, FIT_TYPE_PROP, NULL);
534 if (!type)
535 return 0;
536
537 return !strcmp(type, "fpga");
538}
539
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100540static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
541{
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600542 if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT))
543 return fit_image_get_os(fit, noffset, os);
Samuel Hollande646f512020-10-21 21:12:13 -0500544
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600545 const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
Samuel Hollande646f512020-10-21 21:12:13 -0500546 if (!name)
547 return -ENOENT;
548
549 /*
550 * We don't care what the type of the image actually is,
551 * only whether or not it is U-Boot. This saves some
552 * space by omitting the large table of OS types.
553 */
554 if (!strcmp(name, "u-boot"))
555 *os = IH_OS_U_BOOT;
556 else
557 *os = IH_OS_INVALID;
558
559 return 0;
Philipp Tomsich4faa0112017-11-24 13:26:03 +0100560}
561
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500562/*
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500563 * The purpose of the FIT load buffer is to provide a memory location that is
564 * independent of the load address of any FIT component.
565 */
566static void *spl_get_fit_load_buffer(size_t size)
567{
568 void *buf;
569
Stefan Herbrechtsmeier66249102022-06-14 16:12:00 +0200570 buf = malloc_cache_aligned(size);
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500571 if (!buf) {
572 pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size);
Simon Glass67e3fca2023-09-26 08:14:16 -0600573 pr_err("\tcheck CONFIG_SPL_SYS_MALLOC_SIZE\n");
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500574 buf = spl_get_load_buffer(0, size);
575 }
576 return buf;
577}
578
Heiko Schocher7d7c3672021-08-17 08:17:18 +0200579__weak void *board_spl_fit_buffer_addr(ulong fit_size, int sectors, int bl_len)
580{
581 return spl_get_fit_load_buffer(sectors * bl_len);
582}
583
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500584/*
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500585 * Weak default function to allow customizing SPL fit loading for load-only
586 * use cases by allowing to skip the parsing/processing of the FIT contents
587 * (so that this can be done separately in a more customized fashion)
588 */
589__weak bool spl_load_simple_fit_skip_processing(void)
590{
591 return false;
592}
593
Heiko Schocher67e8f5d2021-08-06 06:44:26 +0200594/*
595 * Weak default function to allow fixes after fit header
596 * is loaded.
597 */
598__weak void *spl_load_simple_fit_fix_load(const void *fit)
599{
600 return (void *)fit;
601}
602
Alexandru Gagniuc69a99662021-03-29 12:05:13 -0500603static void warn_deprecated(const char *msg)
604{
605 printf("DEPRECATED: %s\n", msg);
606 printf("\tSee doc/uImage.FIT/source_file_format.txt\n");
607}
608
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500609static int spl_fit_upload_fpga(struct spl_fit_info *ctx, int node,
610 struct spl_image_info *fpga_image)
611{
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500612 const char *compatible;
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500613 int ret;
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300614 int devnum = 0;
615 int flags = 0;
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500616
617 debug("FPGA bitstream at: %x, size: %x\n",
618 (u32)fpga_image->load_addr, fpga_image->size);
619
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500620 compatible = fdt_getprop(ctx->fit, node, "compatible", NULL);
Oleksandr Suvorov2fddf3e2022-07-22 17:16:09 +0300621 if (!compatible) {
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500622 warn_deprecated("'fpga' image without 'compatible' property");
Oleksandr Suvorov2fddf3e2022-07-22 17:16:09 +0300623 } else {
624 if (CONFIG_IS_ENABLED(FPGA_LOAD_SECURE))
625 flags = fpga_compatible2flag(devnum, compatible);
626 if (strcmp(compatible, "u-boot,fpga-legacy"))
627 debug("Ignoring compatible = %s property\n",
628 compatible);
629 }
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500630
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300631 ret = fpga_load(devnum, (void *)fpga_image->load_addr,
632 fpga_image->size, BIT_FULL, flags);
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500633 if (ret) {
634 printf("%s: Cannot load the image to the FPGA\n", __func__);
635 return ret;
636 }
637
638 puts("FPGA image loaded from FIT\n");
639
640 return 0;
641}
642
643static int spl_fit_load_fpga(struct spl_fit_info *ctx,
644 struct spl_load_info *info, ulong sector)
645{
646 int node, ret;
647
648 struct spl_image_info fpga_image = {
649 .load_addr = 0,
650 };
651
652 node = spl_fit_get_image_node(ctx, "fpga", 0);
653 if (node < 0)
654 return node;
655
Alexandru Gagniuc69a99662021-03-29 12:05:13 -0500656 warn_deprecated("'fpga' property in config node. Use 'loadables'");
657
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500658 /* Load the image and set up the fpga_image structure */
Simon Glass205ff7b2023-09-26 08:14:33 -0600659 ret = load_simple_fit(info, sector, ctx, node, &fpga_image);
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500660 if (ret) {
661 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
662 return ret;
663 }
664
665 return spl_fit_upload_fpga(ctx, node, &fpga_image);
666}
667
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600668static int spl_simple_fit_read(struct spl_fit_info *ctx,
669 struct spl_load_info *info, ulong sector,
670 const void *fit_header)
Simon Glassa6131a22016-02-22 22:55:56 -0700671{
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600672 unsigned long count, size;
Simon Glassa6131a22016-02-22 22:55:56 -0700673 int sectors;
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600674 void *buf;
Simon Glassa6131a22016-02-22 22:55:56 -0700675
676 /*
York Suna058b8a2017-08-15 11:14:45 -0700677 * For FIT with external data, figure out where the external images
678 * start. This is the base for the data-offset properties in each
679 * image.
Simon Glassa6131a22016-02-22 22:55:56 -0700680 */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600681 size = ALIGN(fdt_totalsize(fit_header), 4);
Ye Li9d5b0f42018-11-17 09:10:25 +0000682 size = board_spl_fit_size_align(size);
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600683 ctx->ext_data_offset = ALIGN(size, 4);
Simon Glassa6131a22016-02-22 22:55:56 -0700684
685 /*
686 * So far we only have one block of data from the FIT. Read the entire
Alexandru Gagniuc02560172020-10-21 18:32:58 -0500687 * thing, including that first block.
York Suna058b8a2017-08-15 11:14:45 -0700688 *
689 * For FIT with data embedded, data is loaded as part of FIT image.
690 * For FIT with external data, data is not loaded in this step.
Simon Glassa6131a22016-02-22 22:55:56 -0700691 */
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530692 sectors = get_aligned_image_size(info, size, 0);
Heiko Schocher7d7c3672021-08-17 08:17:18 +0200693 buf = board_spl_fit_buffer_addr(size, sectors, info->bl_len);
Ye Li9d5b0f42018-11-17 09:10:25 +0000694
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600695 count = info->read(info, sector, sectors, buf);
696 ctx->fit = buf;
697 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
698 sector, sectors, buf, count, size);
Simon Glassa6131a22016-02-22 22:55:56 -0700699
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600700 return (count == 0) ? -EIO : 0;
701}
Andreas Dannenberg8d9f7f12019-06-04 17:55:46 -0500702
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600703static int spl_simple_fit_parse(struct spl_fit_info *ctx)
704{
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600705 /* Find the correct subnode under "/configurations" */
706 ctx->conf_node = fit_find_config_node(ctx->fit);
707 if (ctx->conf_node < 0)
708 return -EINVAL;
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100709
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600710 if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100711 printf("## Checking hash(es) for config %s ... ",
Alexandru Gagniucfa11a442021-01-20 10:46:53 -0600712 fit_get_name(ctx->fit, ctx->conf_node, NULL));
713 if (fit_config_verify(ctx->fit, ctx->conf_node))
Philippe Reynes6c5c03c2020-10-29 18:50:29 +0100714 return -EPERM;
715 puts("OK\n");
716 }
717
Andre Przywara525e9c92017-04-26 01:32:34 +0100718 /* find the node holding the images information */
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600719 ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
720 if (ctx->images_node < 0) {
721 debug("%s: Cannot find /images node: %d\n", __func__,
722 ctx->images_node);
723 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -0700724 }
Marek Vasut33dd00e2018-05-12 22:25:28 +0200725
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600726 return 0;
727}
728
729int spl_load_simple_fit(struct spl_image_info *spl_image,
730 struct spl_load_info *info, ulong sector, void *fit)
731{
732 struct spl_image_info image_info;
733 struct spl_fit_info ctx;
734 int node = -1;
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600735 int ret;
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600736 int index = 0;
737 int firmware_node;
738
739 ret = spl_simple_fit_read(&ctx, info, sector, fit);
740 if (ret < 0)
741 return ret;
742
743 /* skip further processing if requested to enable load-only use cases */
744 if (spl_load_simple_fit_skip_processing())
745 return 0;
746
Heiko Schocher67e8f5d2021-08-06 06:44:26 +0200747 ctx.fit = spl_load_simple_fit_fix_load(ctx.fit);
748
Alexandru Gagniuc23243c92021-01-20 10:46:50 -0600749 ret = spl_simple_fit_parse(&ctx);
750 if (ret < 0)
751 return ret;
752
Alexandru Gagniucec2a5742021-03-29 12:05:12 -0500753 if (IS_ENABLED(CONFIG_SPL_FPGA))
754 spl_fit_load_fpga(&ctx, info, sector);
Andre Przywara525e9c92017-04-26 01:32:34 +0100755
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200756 /*
757 * Find the U-Boot image using the following search order:
758 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
759 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
760 * - fall back to using the first 'loadables' entry
761 */
762 if (node < 0)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600763 node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600764
765 if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT))
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600766 node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600767
Andre Przywara525e9c92017-04-26 01:32:34 +0100768 if (node < 0) {
769 debug("could not find firmware image, trying loadables...\n");
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600770 node = spl_fit_get_image_node(&ctx, "loadables", 0);
Andre Przywarae7c58562017-04-26 01:32:37 +0100771 /*
772 * If we pick the U-Boot image from "loadables", start at
773 * the second image when later loading additional images.
774 */
775 index = 1;
Andre Przywara525e9c92017-04-26 01:32:34 +0100776 }
Simon Glassa6131a22016-02-22 22:55:56 -0700777 if (node < 0) {
Andre Przywara525e9c92017-04-26 01:32:34 +0100778 debug("%s: Cannot find u-boot image node: %d\n",
779 __func__, node);
Simon Glassa6131a22016-02-22 22:55:56 -0700780 return -1;
781 }
782
Andre Przywara29053e92017-04-26 01:32:36 +0100783 /* Load the image and set up the spl_image structure */
Simon Glass205ff7b2023-09-26 08:14:33 -0600784 ret = load_simple_fit(info, sector, &ctx, node, spl_image);
Andre Przywara29053e92017-04-26 01:32:36 +0100785 if (ret)
786 return ret;
Daniel Allredcf839bd2016-06-27 09:19:21 -0500787
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200788 /*
789 * For backward compatibility, we treat the first node that is
790 * as a U-Boot image, if no OS-type has been declared.
791 */
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600792 if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
York Suna058b8a2017-08-15 11:14:45 -0700793 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
Alexandru Gagniuca4fed792021-01-20 10:46:55 -0600794 else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200795 spl_image->os = IH_OS_U_BOOT;
Simon Glassa6131a22016-02-22 22:55:56 -0700796
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200797 /*
798 * Booting a next-stage U-Boot may require us to append the FDT.
799 * We allow this to fail, as the U-Boot image might embed its FDT.
800 */
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600801 if (os_takes_devicetree(spl_image->os)) {
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600802 ret = spl_fit_append_fdt(spl_image, info, sector, &ctx);
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600803 if (ret < 0 && spl_image->os != IH_OS_U_BOOT)
Dario Binacchifaf8d142020-05-27 13:56:19 +0200804 return ret;
805 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100806
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200807 firmware_node = node;
Andre Przywarae7c58562017-04-26 01:32:37 +0100808 /* Now check if there are more images for us to load */
809 for (; ; index++) {
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200810 uint8_t os_type = IH_OS_INVALID;
811
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600812 node = spl_fit_get_image_node(&ctx, "loadables", index);
Andre Przywarae7c58562017-04-26 01:32:37 +0100813 if (node < 0)
814 break;
815
Jean-Jacques Hiblot776ce602019-10-22 16:39:10 +0200816 /*
817 * if the firmware is also a loadable, skip it because
818 * it already has been loaded. This is typically the case with
819 * u-boot.img generated by mkimage.
820 */
821 if (firmware_node == node)
822 continue;
823
Alexandru Gagniucc5236c32021-03-29 12:05:10 -0500824 image_info.load_addr = 0;
Simon Glass205ff7b2023-09-26 08:14:33 -0600825 ret = load_simple_fit(info, sector, &ctx, node, &image_info);
Philippe Reynesba87da42020-11-24 16:15:05 +0100826 if (ret < 0) {
827 printf("%s: can't load image loadables index %d (ret = %d)\n",
828 __func__, index, ret);
829 return ret;
830 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100831
Alexandru Gagniuc0df07592021-03-29 12:05:14 -0500832 if (spl_fit_image_is_fpga(ctx.fit, node))
833 spl_fit_upload_fpga(&ctx, node, &image_info);
834
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600835 if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200836 debug("Loadable is %s\n", genimg_get_os_name(os_type));
837
Alexandru Gagniuc769ed252021-01-20 10:46:56 -0600838 if (os_takes_devicetree(os_type)) {
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600839 spl_fit_append_fdt(&image_info, info, sector, &ctx);
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200840 spl_image->fdt_addr = image_info.fdt_addr;
841 }
Philipp Tomsich1345ffa2017-09-13 21:29:32 +0200842
Andre Przywarae7c58562017-04-26 01:32:37 +0100843 /*
844 * If the "firmware" image did not provide an entry point,
845 * use the first valid entry point from the loadables.
846 */
847 if (spl_image->entry_point == FDT_ERROR &&
848 image_info.entry_point != FDT_ERROR)
849 spl_image->entry_point = image_info.entry_point;
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200850
851 /* Record our loadables into the FDT */
852 if (spl_image->fdt_addr)
Alexandru Gagniuc93d15572021-01-20 10:46:51 -0600853 spl_fit_record_loadable(&ctx, index,
Philipp Tomsiche61eff92017-09-13 21:29:34 +0200854 spl_image->fdt_addr,
855 &image_info);
Andre Przywarae7c58562017-04-26 01:32:37 +0100856 }
857
858 /*
Jesse Taube93ee5c82023-08-24 21:59:48 -0400859 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
Andre Przywarae7c58562017-04-26 01:32:37 +0100860 * Makefile will set it to 0 and it will end up as the entry point
861 * here. What it actually means is: use the load address.
862 */
863 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
864 spl_image->entry_point = spl_image->load_addr;
865
Ye Li9d5b0f42018-11-17 09:10:25 +0000866 spl_image->flags |= SPL_FIT_FOUND;
867
Andre Przywarae7c58562017-04-26 01:32:37 +0100868 return 0;
Simon Glassa6131a22016-02-22 22:55:56 -0700869}
Simon Glassc0bd55e2023-09-26 08:14:34 -0600870
871/* Parse and load full fitImage in SPL */
872int spl_load_fit_image(struct spl_image_info *spl_image,
873 const struct legacy_img_hdr *header)
874{
875 struct bootm_headers images;
876 const char *fit_uname_config = NULL;
877 uintptr_t fdt_hack;
878 const char *uname;
879 ulong fw_data = 0, dt_data = 0, img_data = 0;
880 ulong fw_len = 0, dt_len = 0, img_len = 0;
881 int idx, conf_noffset;
882 int ret;
883
884#ifdef CONFIG_SPL_FIT_SIGNATURE
885 images.verify = 1;
886#endif
Sean Anderson5ff77722023-10-14 16:47:55 -0400887 ret = fit_image_load(&images, virt_to_phys((void *)header),
Simon Glassc0bd55e2023-09-26 08:14:34 -0600888 NULL, &fit_uname_config,
889 IH_ARCH_DEFAULT, IH_TYPE_STANDALONE, -1,
890 FIT_LOAD_OPTIONAL, &fw_data, &fw_len);
891 if (ret >= 0) {
892 printf("DEPRECATED: 'standalone = ' property.");
893 printf("Please use either 'firmware =' or 'kernel ='\n");
894 } else {
Sean Anderson5ff77722023-10-14 16:47:55 -0400895 ret = fit_image_load(&images, virt_to_phys((void *)header),
896 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
Simon Glassc0bd55e2023-09-26 08:14:34 -0600897 IH_TYPE_FIRMWARE, -1, FIT_LOAD_OPTIONAL,
898 &fw_data, &fw_len);
899 }
900
901 if (ret < 0) {
Sean Anderson5ff77722023-10-14 16:47:55 -0400902 ret = fit_image_load(&images, virt_to_phys((void *)header),
903 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
Simon Glassc0bd55e2023-09-26 08:14:34 -0600904 IH_TYPE_KERNEL, -1, FIT_LOAD_OPTIONAL,
905 &fw_data, &fw_len);
906 }
907
908 if (ret < 0)
909 return ret;
910
911 spl_image->size = fw_len;
Simon Glassc0bd55e2023-09-26 08:14:34 -0600912 spl_image->load_addr = fw_data;
Sean Andersone99b2cd2023-10-14 16:47:39 -0400913 if (fit_image_get_entry(header, ret, &spl_image->entry_point))
914 spl_image->entry_point = fw_data;
Simon Glassc0bd55e2023-09-26 08:14:34 -0600915 if (fit_image_get_os(header, ret, &spl_image->os))
916 spl_image->os = IH_OS_INVALID;
917 spl_image->name = genimg_get_os_name(spl_image->os);
918
919 debug(SPL_TPL_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
920 spl_image->name, spl_image->load_addr, spl_image->size);
921
922#ifdef CONFIG_SPL_FIT_SIGNATURE
923 images.verify = 1;
924#endif
Sean Anderson5ff77722023-10-14 16:47:55 -0400925 ret = fit_image_load(&images, virt_to_phys((void *)header), NULL,
926 &fit_uname_config, IH_ARCH_DEFAULT, IH_TYPE_FLATDT,
927 -1, FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
Simon Glassc0bd55e2023-09-26 08:14:34 -0600928 if (ret >= 0) {
929 spl_image->fdt_addr = (void *)dt_data;
930
931 if (spl_image->os == IH_OS_U_BOOT) {
932 /* HACK: U-Boot expects FDT at a specific address */
933 fdt_hack = spl_image->load_addr + spl_image->size;
934 fdt_hack = (fdt_hack + 3) & ~3;
935 debug("Relocating FDT to %p\n", spl_image->fdt_addr);
936 memcpy((void *)fdt_hack, spl_image->fdt_addr, dt_len);
937 }
938 }
939
940 conf_noffset = fit_conf_get_node((const void *)header,
941 fit_uname_config);
942 if (conf_noffset < 0)
943 return 0;
944
945 for (idx = 0;
946 uname = fdt_stringlist_get((const void *)header, conf_noffset,
947 FIT_LOADABLE_PROP, idx,
948 NULL), uname;
949 idx++) {
950#ifdef CONFIG_SPL_FIT_SIGNATURE
951 images.verify = 1;
952#endif
953 ret = fit_image_load(&images, (ulong)header,
954 &uname, &fit_uname_config,
955 IH_ARCH_DEFAULT, IH_TYPE_LOADABLE, -1,
956 FIT_LOAD_OPTIONAL_NON_ZERO,
957 &img_data, &img_len);
958 if (ret < 0)
959 return ret;
960 }
961
962 return 0;
963}