blob: 50670bfc768b5c78c990bb58cb22d4ef3f8104b9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassf1dcee52016-02-22 22:55:56 -07002/*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassf1dcee52016-02-22 22:55:56 -07005 */
6
7#include <common.h>
8#include <errno.h>
Michal Simek3313ae62018-07-18 14:33:15 +02009#include <fpga.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060010#include <gzip.h>
Simon Glassf1dcee52016-02-22 22:55:56 -070011#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +020013#include <malloc.h>
Simon Glassf1dcee52016-02-22 22:55:56 -070014#include <spl.h>
Simon Glass3a8ee3d2020-11-05 06:32:05 -070015#include <sysinfo.h>
Simon Glass90526e92020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +020018#include <linux/libfdt.h>
Simon Glassf1dcee52016-02-22 22:55:56 -070019
Lukas Auerb83edfb2019-08-21 21:14:42 +020020DECLARE_GLOBAL_DATA_PTR;
21
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +020022#ifndef CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ
23#define CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ (64 * 1024)
24#endif
25
York Sun7264f292017-08-15 11:14:43 -070026#ifndef CONFIG_SYS_BOOTM_LEN
27#define CONFIG_SYS_BOOTM_LEN (64 << 20)
28#endif
29
Alexandru Gagniuc917fa362021-01-20 10:46:50 -060030struct spl_fit_info {
31 const void *fit; /* Pointer to a valid FIT blob */
32 size_t ext_data_offset; /* Offset to FIT external data (end of FIT) */
33 int images_node; /* FDT offset to "/images" node */
Alexandru Gagniuc9e9aa0b2021-01-20 10:46:53 -060034 int conf_node; /* FDT offset to selected configuration node */
Alexandru Gagniuc917fa362021-01-20 10:46:50 -060035};
36
Alexandru Gagniucefc4ad02021-01-20 10:46:49 -060037__weak void board_spl_fit_post_load(const void *fit)
Ye Lie246bfc2018-11-17 09:10:25 +000038{
39}
40
41__weak ulong board_spl_fit_size_align(ulong size)
42{
43 return size;
44}
45
Jean-Jacques Hiblot152781d2019-10-22 16:39:22 +020046static int find_node_from_desc(const void *fit, int node, const char *str)
47{
48 int child;
49
50 if (node < 0)
51 return -EINVAL;
52
53 /* iterate the FIT nodes and find a matching description */
54 for (child = fdt_first_subnode(fit, node); child >= 0;
55 child = fdt_next_subnode(fit, child)) {
56 int len;
57 const char *desc = fdt_getprop(fit, child, "description", &len);
58
59 if (!desc)
60 continue;
61
62 if (!strcmp(desc, str))
63 return child;
64 }
65
66 return -ENOENT;
67}
68
Andre Przywara736806f2017-04-26 01:32:34 +010069/**
Philipp Tomsicha616c782017-09-13 21:29:34 +020070 * spl_fit_get_image_name(): By using the matching configuration subnode,
Andre Przywara736806f2017-04-26 01:32:34 +010071 * retrieve the name of an image, specified by a property name and an index
72 * into that.
73 * @fit: Pointer to the FDT blob.
74 * @images: Offset of the /images subnode.
75 * @type: Name of the property within the configuration subnode.
76 * @index: Index into the list of strings in this property.
Philipp Tomsicha616c782017-09-13 21:29:34 +020077 * @outname: Name of the image
Andre Przywara736806f2017-04-26 01:32:34 +010078 *
Philipp Tomsicha616c782017-09-13 21:29:34 +020079 * Return: 0 on success, or a negative error number
Andre Przywara736806f2017-04-26 01:32:34 +010080 */
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -060081static int spl_fit_get_image_name(const struct spl_fit_info *ctx,
Philipp Tomsicha616c782017-09-13 21:29:34 +020082 const char *type, int index,
Jean-Jacques Hiblotc1648d02019-10-22 16:39:17 +020083 const char **outname)
Andre Przywara4b9340a2017-04-26 01:32:33 +010084{
Simon Glass3a8ee3d2020-11-05 06:32:05 -070085 struct udevice *sysinfo;
Andre Przywara4b9340a2017-04-26 01:32:33 +010086 const char *name, *str;
Philipp Tomsicha616c782017-09-13 21:29:34 +020087 __maybe_unused int node;
Andre Przywara4b9340a2017-04-26 01:32:33 +010088 int len, i;
Jean-Jacques Hiblot152781d2019-10-22 16:39:22 +020089 bool found = true;
Andre Przywara4b9340a2017-04-26 01:32:33 +010090
Alexandru Gagniuc9e9aa0b2021-01-20 10:46:53 -060091 name = fdt_getprop(ctx->fit, ctx->conf_node, type, &len);
Andre Przywara4b9340a2017-04-26 01:32:33 +010092 if (!name) {
93 debug("cannot find property '%s': %d\n", type, len);
94 return -EINVAL;
95 }
96
97 str = name;
98 for (i = 0; i < index; i++) {
99 str = strchr(str, '\0') + 1;
100 if (!str || (str - name >= len)) {
Jean-Jacques Hiblot152781d2019-10-22 16:39:22 +0200101 found = false;
102 break;
Andre Przywara4b9340a2017-04-26 01:32:33 +0100103 }
104 }
105
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700106 if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) {
Jean-Jacques Hiblot152781d2019-10-22 16:39:22 +0200107 int rc;
108 /*
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700109 * no string in the property for this index. Check if the
110 * sysinfo-level code can supply one.
Jean-Jacques Hiblot152781d2019-10-22 16:39:22 +0200111 */
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700112 rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
113 &str);
Jean-Jacques Hiblot152781d2019-10-22 16:39:22 +0200114 if (rc && rc != -ENOENT)
115 return rc;
116
117 if (!rc) {
118 /*
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700119 * The sysinfo provided a name for a loadable.
Jean-Jacques Hiblot152781d2019-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 Gagniuc3dc20792021-01-20 10:46:51 -0600125 int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
Jean-Jacques Hiblot152781d2019-10-22 16:39:22 +0200126
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600127 node = find_node_from_desc(ctx->fit, images, str);
Jean-Jacques Hiblot152781d2019-10-22 16:39:22 +0200128 if (node > 0)
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600129 str = fdt_get_name(ctx->fit, node, NULL);
Jean-Jacques Hiblot152781d2019-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 Tomsicha616c782017-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 Gagniuc3dc20792021-01-20 10:46:51 -0600156static int spl_fit_get_image_node(const struct spl_fit_info *ctx,
Philipp Tomsicha616c782017-09-13 21:29:34 +0200157 const char *type, int index)
158{
Jean-Jacques Hiblotc1648d02019-10-22 16:39:17 +0200159 const char *str;
Philipp Tomsicha616c782017-09-13 21:29:34 +0200160 int err;
161 int node;
162
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600163 err = spl_fit_get_image_name(ctx, type, index, &str);
Philipp Tomsicha616c782017-09-13 21:29:34 +0200164 if (err)
165 return err;
166
Andre Przywara4b9340a2017-04-26 01:32:33 +0100167 debug("%s: '%s'\n", type, str);
Philipp Tomsicha616c782017-09-13 21:29:34 +0200168
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600169 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
Andre Przywara4b9340a2017-04-26 01:32:33 +0100170 if (node < 0) {
Jean-Jacques Hiblot19141d62019-10-22 16:39:15 +0200171 pr_err("cannot find image node '%s': %d\n", str, node);
Andre Przywara4b9340a2017-04-26 01:32:33 +0100172 return -EINVAL;
173 }
174
Andre Przywara736806f2017-04-26 01:32:34 +0100175 return node;
Andre Przywara4b9340a2017-04-26 01:32:33 +0100176}
177
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530178static int get_aligned_image_offset(struct spl_load_info *info, int offset)
179{
180 /*
181 * If it is a FS read, get the first address before offset which is
182 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
183 * block number to which offset belongs.
184 */
185 if (info->filename)
186 return offset & ~(ARCH_DMA_MINALIGN - 1);
187
188 return offset / info->bl_len;
189}
190
191static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
192{
193 /*
194 * If it is a FS read, get the difference between the offset and
195 * the first address before offset which is aligned to
196 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
197 * block.
198 */
199 if (info->filename)
200 return offset & (ARCH_DMA_MINALIGN - 1);
201
202 return offset % info->bl_len;
203}
204
205static int get_aligned_image_size(struct spl_load_info *info, int data_size,
206 int offset)
207{
Lokesh Vutla3cc1f382016-07-19 14:56:14 +0530208 data_size = data_size + get_aligned_image_overhead(info, offset);
209
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530210 if (info->filename)
Lokesh Vutla3cc1f382016-07-19 14:56:14 +0530211 return data_size;
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530212
213 return (data_size + info->bl_len - 1) / info->bl_len;
214}
215
Andre Przywara8baa3812017-04-26 01:32:36 +0100216/**
217 * spl_load_fit_image(): load the image described in a certain FIT node
218 * @info: points to information about the device to load data from
219 * @sector: the start sector of the FIT image on the device
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600220 * @ctx: points to the FIT context structure
Andre Przywara8baa3812017-04-26 01:32:36 +0100221 * @node: offset of the DT node describing the image to load (relative
Philipp Tomsicha616c782017-09-13 21:29:34 +0200222 * to @fit)
Andre Przywara8baa3812017-04-26 01:32:36 +0100223 * @image_info: will be filled with information about the loaded image
Philipp Tomsicha616c782017-09-13 21:29:34 +0200224 * If the FIT node does not contain a "load" (address) property,
225 * the image gets loaded to the address pointed to by the
226 * load_addr member in this struct.
Andre Przywara8baa3812017-04-26 01:32:36 +0100227 *
228 * Return: 0 on success or a negative error number.
229 */
230static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600231 const struct spl_fit_info *ctx, int node,
Andre Przywara8baa3812017-04-26 01:32:36 +0100232 struct spl_image_info *image_info)
233{
Michal Simek3313ae62018-07-18 14:33:15 +0200234 int offset;
Andre Przywara8baa3812017-04-26 01:32:36 +0100235 size_t length;
York Sun5fd13d92017-08-15 11:14:44 -0700236 int len;
York Sun933f67a2017-09-15 08:21:13 -0700237 ulong size;
Andre Przywara8baa3812017-04-26 01:32:36 +0100238 ulong load_addr, load_ptr;
239 void *src;
240 ulong overhead;
241 int nr_sectors;
242 int align_len = ARCH_DMA_MINALIGN - 1;
York Sun7264f292017-08-15 11:14:43 -0700243 uint8_t image_comp = -1, type = -1;
York Sun5fd13d92017-08-15 11:14:44 -0700244 const void *data;
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600245 const void *fit = ctx->fit;
Peng Fana1be94b2017-12-05 13:20:59 +0800246 bool external_data = false;
York Sun7264f292017-08-15 11:14:43 -0700247
Michal Simek29bd8ad2020-09-09 14:41:56 +0200248 if (IS_ENABLED(CONFIG_SPL_FPGA) ||
Marek Vasut56419ea2018-06-01 23:19:29 +0200249 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP))) {
250 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
Klaus H. Sorensen602ce1d2019-12-11 11:03:33 +0000256 if (IS_ENABLED(CONFIG_SPL_GZIP)) {
257 fit_image_get_comp(fit, node, &image_comp);
258 debug("%s ", genimg_get_comp_name(image_comp));
York Sun7264f292017-08-15 11:14:43 -0700259 }
Andre Przywara8baa3812017-04-26 01:32:36 +0100260
York Sun5fd13d92017-08-15 11:14:44 -0700261 if (fit_image_get_load(fit, node, &load_addr))
Andre Przywara8baa3812017-04-26 01:32:36 +0100262 load_addr = image_info->load_addr;
Andre Przywara8baa3812017-04-26 01:32:36 +0100263
Peng Fana1be94b2017-12-05 13:20:59 +0800264 if (!fit_image_get_data_position(fit, node, &offset)) {
265 external_data = true;
266 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600267 offset += ctx->ext_data_offset;
Peng Fana1be94b2017-12-05 13:20:59 +0800268 external_data = true;
269 }
270
271 if (external_data) {
272 /* External data */
York Sun5fd13d92017-08-15 11:14:44 -0700273 if (fit_image_get_data_size(fit, node, &len))
274 return -ENOENT;
Andre Przywara8baa3812017-04-26 01:32:36 +0100275
York Sun5fd13d92017-08-15 11:14:44 -0700276 load_ptr = (load_addr + align_len) & ~align_len;
277 length = len;
Andre Przywara8baa3812017-04-26 01:32:36 +0100278
York Sun5fd13d92017-08-15 11:14:44 -0700279 overhead = get_aligned_image_overhead(info, offset);
280 nr_sectors = get_aligned_image_size(info, length, offset);
281
Michal Simek3313ae62018-07-18 14:33:15 +0200282 if (info->read(info,
283 sector + get_aligned_image_offset(info, offset),
York Sun5fd13d92017-08-15 11:14:44 -0700284 nr_sectors, (void *)load_ptr) != nr_sectors)
285 return -EIO;
286
287 debug("External data: dst=%lx, offset=%x, size=%lx\n",
288 load_ptr, offset, (unsigned long)length);
289 src = (void *)load_ptr + overhead;
290 } else {
291 /* Embedded data */
292 if (fit_image_get_data(fit, node, &data, &length)) {
293 puts("Cannot get image data/size\n");
294 return -ENOENT;
295 }
296 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
297 (unsigned long)length);
298 src = (void *)data;
299 }
300
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600301 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
302 printf("## Checking hash(es) for Image %s ... ",
303 fit_get_name(fit, node, NULL));
304 if (!fit_image_verify_with_data(fit, node, src, length))
305 return -EPERM;
306 puts("OK\n");
307 }
Ben Whittend154ca62018-06-07 11:37:27 +0100308
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600309 if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS))
310 board_fit_image_post_process(&src, &length);
Andre Przywara8baa3812017-04-26 01:32:36 +0100311
Michal Simek975e7892018-07-24 15:05:00 +0200312 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
York Sun933f67a2017-09-15 08:21:13 -0700313 size = length;
York Sun7264f292017-08-15 11:14:43 -0700314 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
York Sun933f67a2017-09-15 08:21:13 -0700315 src, &size)) {
York Sun7264f292017-08-15 11:14:43 -0700316 puts("Uncompressing error\n");
317 return -EIO;
318 }
York Sun933f67a2017-09-15 08:21:13 -0700319 length = size;
York Sun7264f292017-08-15 11:14:43 -0700320 } else {
321 memcpy((void *)load_addr, src, length);
322 }
Andre Przywara8baa3812017-04-26 01:32:36 +0100323
324 if (image_info) {
Michal Simekcaa7fc22020-09-03 11:24:28 +0200325 ulong entry_point;
326
Andre Przywara8baa3812017-04-26 01:32:36 +0100327 image_info->load_addr = load_addr;
328 image_info->size = length;
Michal Simekcaa7fc22020-09-03 11:24:28 +0200329
330 if (!fit_image_get_entry(fit, node, &entry_point))
331 image_info->entry_point = entry_point;
332 else
333 image_info->entry_point = FDT_ERROR;
Andre Przywara8baa3812017-04-26 01:32:36 +0100334 }
335
336 return 0;
337}
338
Philipp Tomsichd8796162017-09-13 21:29:32 +0200339static int spl_fit_append_fdt(struct spl_image_info *spl_image,
340 struct spl_load_info *info, ulong sector,
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600341 const struct spl_fit_info *ctx)
Philipp Tomsichd8796162017-09-13 21:29:32 +0200342{
343 struct spl_image_info image_info;
Michal Simek9d13b872019-10-22 16:39:11 +0200344 int node, ret = 0, index = 0;
Lukas Auerb83edfb2019-08-21 21:14:42 +0200345
346 /*
347 * Use the address following the image as target address for the
Marek Vasut5675ed72020-10-19 23:40:26 +0200348 * device tree.
Lukas Auerb83edfb2019-08-21 21:14:42 +0200349 */
Marek Vasut5675ed72020-10-19 23:40:26 +0200350 image_info.load_addr = spl_image->load_addr + spl_image->size;
Philipp Tomsichd8796162017-09-13 21:29:32 +0200351
352 /* Figure out which device tree the board wants to use */
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600353 node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
Philipp Tomsichd8796162017-09-13 21:29:32 +0200354 if (node < 0) {
355 debug("%s: cannot find FDT node\n", __func__);
Lukas Auerb83edfb2019-08-21 21:14:42 +0200356
357 /*
358 * U-Boot did not find a device tree inside the FIT image. Use
359 * the U-Boot device tree instead.
360 */
361 if (gd->fdt_blob)
362 memcpy((void *)image_info.load_addr, gd->fdt_blob,
363 fdt_totalsize(gd->fdt_blob));
364 else
365 return node;
366 } else {
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600367 ret = spl_load_fit_image(info, sector, ctx, node,
Lukas Auerb83edfb2019-08-21 21:14:42 +0200368 &image_info);
369 if (ret < 0)
370 return ret;
Philipp Tomsichd8796162017-09-13 21:29:32 +0200371 }
372
Philipp Tomsicha616c782017-09-13 21:29:34 +0200373 /* Make the load-address of the FDT available for the SPL framework */
374 spl_image->fdt_addr = (void *)image_info.load_addr;
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600375 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
376 return 0;
377
Michal Simek9d13b872019-10-22 16:39:11 +0200378 if (CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)) {
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200379 void *tmpbuffer = NULL;
380
Michal Simek9d13b872019-10-22 16:39:11 +0200381 for (; ; index++) {
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600382 node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index);
Jean-Jacques Hiblot24bf44c2019-10-22 16:39:14 +0200383 if (node == -E2BIG) {
Michal Simek9d13b872019-10-22 16:39:11 +0200384 debug("%s: No additional FDT node\n", __func__);
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200385 break;
Jean-Jacques Hiblot24bf44c2019-10-22 16:39:14 +0200386 } else if (node < 0) {
387 debug("%s: unable to find FDT node %d\n",
388 __func__, index);
389 continue;
Michal Simek9d13b872019-10-22 16:39:11 +0200390 }
Philipp Tomsicha616c782017-09-13 21:29:34 +0200391
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200392 if (!tmpbuffer) {
393 /*
394 * allocate memory to store the DT overlay
395 * before it is applied. It may not be used
396 * depending on how the overlay is stored, so
397 * don't fail yet if the allocation failed.
398 */
399 tmpbuffer = malloc(CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ);
400 if (!tmpbuffer)
401 debug("%s: unable to allocate space for overlays\n",
402 __func__);
403 }
404 image_info.load_addr = (ulong)tmpbuffer;
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600405 ret = spl_load_fit_image(info, sector, ctx,
Michal Simek9d13b872019-10-22 16:39:11 +0200406 node, &image_info);
407 if (ret < 0)
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200408 break;
Michal Simek9d13b872019-10-22 16:39:11 +0200409
Jean-Jacques Hiblot99329be2019-10-22 16:39:12 +0200410 /* Make room in FDT for changes from the overlay */
411 ret = fdt_increase_size(spl_image->fdt_addr,
412 image_info.size);
413 if (ret < 0)
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200414 break;
Jean-Jacques Hiblot99329be2019-10-22 16:39:12 +0200415
Michal Simek9d13b872019-10-22 16:39:11 +0200416 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
417 (void *)image_info.load_addr);
Jean-Jacques Hiblot19141d62019-10-22 16:39:15 +0200418 if (ret) {
419 pr_err("failed to apply DT overlay %s\n",
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600420 fit_get_name(ctx->fit, node, NULL));
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200421 break;
Jean-Jacques Hiblot19141d62019-10-22 16:39:15 +0200422 }
Michal Simek9d13b872019-10-22 16:39:11 +0200423
424 debug("%s: DT overlay %s applied\n", __func__,
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600425 fit_get_name(ctx->fit, node, NULL));
Michal Simek9d13b872019-10-22 16:39:11 +0200426 }
Heinrich Schuchardt077e72c2020-04-20 12:44:01 +0200427 free(tmpbuffer);
Jean-Jacques Hiblotea376eb2019-10-22 16:39:13 +0200428 if (ret)
429 return ret;
Michal Simek9d13b872019-10-22 16:39:11 +0200430 }
Jean-Jacques Hiblot99329be2019-10-22 16:39:12 +0200431 /* Try to make space, so we can inject details on the loadables */
432 ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
433 if (ret < 0)
434 return ret;
Jean-Jacques Hiblot99329be2019-10-22 16:39:12 +0200435
Philipp Tomsicha616c782017-09-13 21:29:34 +0200436 return ret;
437}
438
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600439static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index,
Philipp Tomsicha616c782017-09-13 21:29:34 +0200440 void *blob, struct spl_image_info *image)
441{
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100442 int ret = 0;
Jean-Jacques Hiblotc1648d02019-10-22 16:39:17 +0200443 const char *name;
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100444 int node;
Philipp Tomsicha616c782017-09-13 21:29:34 +0200445
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600446 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
447 return 0;
448
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600449 ret = spl_fit_get_image_name(ctx, "loadables", index, &name);
Philipp Tomsicha616c782017-09-13 21:29:34 +0200450 if (ret < 0)
451 return ret;
452
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600453 node = spl_fit_get_image_node(ctx, "loadables", index);
Philipp Tomsicha616c782017-09-13 21:29:34 +0200454
455 ret = fdt_record_loadable(blob, index, name, image->load_addr,
456 image->size, image->entry_point,
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600457 fdt_getprop(ctx->fit, node, "type", NULL),
458 fdt_getprop(ctx->fit, node, "os", NULL));
Philipp Tomsichd8796162017-09-13 21:29:32 +0200459 return ret;
460}
461
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100462static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
463{
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600464 if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT))
465 return fit_image_get_os(fit, noffset, os);
Samuel Hollandcf705532020-10-21 21:12:13 -0500466
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600467 const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
Samuel Hollandcf705532020-10-21 21:12:13 -0500468 if (!name)
469 return -ENOENT;
470
471 /*
472 * We don't care what the type of the image actually is,
473 * only whether or not it is U-Boot. This saves some
474 * space by omitting the large table of OS types.
475 */
476 if (!strcmp(name, "u-boot"))
477 *os = IH_OS_U_BOOT;
478 else
479 *os = IH_OS_INVALID;
480
481 return 0;
Philipp Tomsich337bbb62017-11-24 13:26:03 +0100482}
483
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500484/*
Alexandru Gagniuc03f1f782020-10-21 18:32:58 -0500485 * The purpose of the FIT load buffer is to provide a memory location that is
486 * independent of the load address of any FIT component.
487 */
488static void *spl_get_fit_load_buffer(size_t size)
489{
490 void *buf;
491
492 buf = malloc(size);
493 if (!buf) {
494 pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size);
495 pr_err("\tcheck CONFIG_SYS_SPL_MALLOC_SIZE\n");
496 buf = spl_get_load_buffer(0, size);
497 }
498 return buf;
499}
500
501/*
Andreas Dannenberge1eb6ad2019-06-04 17:55:46 -0500502 * Weak default function to allow customizing SPL fit loading for load-only
503 * use cases by allowing to skip the parsing/processing of the FIT contents
504 * (so that this can be done separately in a more customized fashion)
505 */
506__weak bool spl_load_simple_fit_skip_processing(void)
507{
508 return false;
509}
510
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600511static int spl_simple_fit_read(struct spl_fit_info *ctx,
512 struct spl_load_info *info, ulong sector,
513 const void *fit_header)
Simon Glassf1dcee52016-02-22 22:55:56 -0700514{
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600515 unsigned long count, size;
Simon Glassf1dcee52016-02-22 22:55:56 -0700516 int sectors;
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600517 void *buf;
Simon Glassf1dcee52016-02-22 22:55:56 -0700518
519 /*
York Sunc8bc3c02017-08-15 11:14:45 -0700520 * For FIT with external data, figure out where the external images
521 * start. This is the base for the data-offset properties in each
522 * image.
Simon Glassf1dcee52016-02-22 22:55:56 -0700523 */
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600524 size = ALIGN(fdt_totalsize(fit_header), 4);
Ye Lie246bfc2018-11-17 09:10:25 +0000525 size = board_spl_fit_size_align(size);
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600526 ctx->ext_data_offset = ALIGN(size, 4);
Simon Glassf1dcee52016-02-22 22:55:56 -0700527
528 /*
529 * So far we only have one block of data from the FIT. Read the entire
Alexandru Gagniuc03f1f782020-10-21 18:32:58 -0500530 * thing, including that first block.
York Sunc8bc3c02017-08-15 11:14:45 -0700531 *
532 * For FIT with data embedded, data is loaded as part of FIT image.
533 * For FIT with external data, data is not loaded in this step.
Simon Glassf1dcee52016-02-22 22:55:56 -0700534 */
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530535 sectors = get_aligned_image_size(info, size, 0);
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600536 buf = spl_get_fit_load_buffer(sectors * info->bl_len);
537
538 count = info->read(info, sector, sectors, buf);
539 ctx->fit = buf;
Ye Lie246bfc2018-11-17 09:10:25 +0000540 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n",
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600541 sector, sectors, buf, count, size);
Ye Lie246bfc2018-11-17 09:10:25 +0000542
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600543 return (count == 0) ? -EIO : 0;
544}
Simon Glassf1dcee52016-02-22 22:55:56 -0700545
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600546static int spl_simple_fit_parse(struct spl_fit_info *ctx)
547{
Alexandru Gagniuc9e9aa0b2021-01-20 10:46:53 -0600548 /* Find the correct subnode under "/configurations" */
549 ctx->conf_node = fit_find_config_node(ctx->fit);
550 if (ctx->conf_node < 0)
551 return -EINVAL;
Philippe Reynes7d5b1bf2020-10-29 18:50:29 +0100552
Alexandru Gagniuc9e9aa0b2021-01-20 10:46:53 -0600553 if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
Philippe Reynes7d5b1bf2020-10-29 18:50:29 +0100554 printf("## Checking hash(es) for config %s ... ",
Alexandru Gagniuc9e9aa0b2021-01-20 10:46:53 -0600555 fit_get_name(ctx->fit, ctx->conf_node, NULL));
556 if (fit_config_verify(ctx->fit, ctx->conf_node))
Philippe Reynes7d5b1bf2020-10-29 18:50:29 +0100557 return -EPERM;
558 puts("OK\n");
559 }
560
Andre Przywara736806f2017-04-26 01:32:34 +0100561 /* find the node holding the images information */
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600562 ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
563 if (ctx->images_node < 0) {
564 debug("%s: Cannot find /images node: %d\n", __func__,
565 ctx->images_node);
566 return -EINVAL;
Simon Glassf1dcee52016-02-22 22:55:56 -0700567 }
Andre Przywara736806f2017-04-26 01:32:34 +0100568
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600569 return 0;
570}
571
572int spl_load_simple_fit(struct spl_image_info *spl_image,
573 struct spl_load_info *info, ulong sector, void *fit)
574{
575 struct spl_image_info image_info;
576 struct spl_fit_info ctx;
577 int node = -1;
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600578 int ret;
Alexandru Gagniuc917fa362021-01-20 10:46:50 -0600579 int index = 0;
580 int firmware_node;
581
582 ret = spl_simple_fit_read(&ctx, info, sector, fit);
583 if (ret < 0)
584 return ret;
585
586 /* skip further processing if requested to enable load-only use cases */
587 if (spl_load_simple_fit_skip_processing())
588 return 0;
589
590 ret = spl_simple_fit_parse(&ctx);
591 if (ret < 0)
592 return ret;
593
Michal Simek29bd8ad2020-09-09 14:41:56 +0200594#ifdef CONFIG_SPL_FPGA
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600595 node = spl_fit_get_image_node(&ctx, "fpga", 0);
Marek Vasut26a64222018-05-12 22:25:28 +0200596 if (node >= 0) {
597 /* Load the image and set up the spl_image structure */
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600598 ret = spl_load_fit_image(info, sector, &ctx, node, spl_image);
Marek Vasut26a64222018-05-12 22:25:28 +0200599 if (ret) {
600 printf("%s: Cannot load the FPGA: %i\n", __func__, ret);
601 return ret;
602 }
Michal Simek3313ae62018-07-18 14:33:15 +0200603
604 debug("FPGA bitstream at: %x, size: %x\n",
605 (u32)spl_image->load_addr, spl_image->size);
606
607 ret = fpga_load(0, (const void *)spl_image->load_addr,
608 spl_image->size, BIT_FULL);
609 if (ret) {
610 printf("%s: Cannot load the image to the FPGA\n",
611 __func__);
612 return ret;
613 }
614
Luis Araneda3907eef2018-07-19 03:10:16 -0400615 puts("FPGA image loaded from FIT\n");
Marek Vasut26a64222018-05-12 22:25:28 +0200616 node = -1;
617 }
618#endif
619
Philipp Tomsichd8796162017-09-13 21:29:32 +0200620 /*
621 * Find the U-Boot image using the following search order:
622 * - start at 'firmware' (e.g. an ARM Trusted Firmware)
623 * - fall back 'kernel' (e.g. a Falcon-mode OS boot
624 * - fall back to using the first 'loadables' entry
625 */
York Sunc8bc3c02017-08-15 11:14:45 -0700626 if (node < 0)
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600627 node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600628
629 if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT))
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600630 node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600631
Simon Glassf1dcee52016-02-22 22:55:56 -0700632 if (node < 0) {
Andre Przywara736806f2017-04-26 01:32:34 +0100633 debug("could not find firmware image, trying loadables...\n");
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600634 node = spl_fit_get_image_node(&ctx, "loadables", 0);
Andre Przywara411cf322017-04-26 01:32:37 +0100635 /*
636 * If we pick the U-Boot image from "loadables", start at
637 * the second image when later loading additional images.
638 */
639 index = 1;
Andre Przywara736806f2017-04-26 01:32:34 +0100640 }
641 if (node < 0) {
642 debug("%s: Cannot find u-boot image node: %d\n",
643 __func__, node);
Simon Glassf1dcee52016-02-22 22:55:56 -0700644 return -1;
645 }
646
Andre Przywara8baa3812017-04-26 01:32:36 +0100647 /* Load the image and set up the spl_image structure */
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600648 ret = spl_load_fit_image(info, sector, &ctx, node, spl_image);
Andre Przywara8baa3812017-04-26 01:32:36 +0100649 if (ret)
650 return ret;
651
Philipp Tomsichd8796162017-09-13 21:29:32 +0200652 /*
653 * For backward compatibility, we treat the first node that is
654 * as a U-Boot image, if no OS-type has been declared.
655 */
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600656 if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
York Sunc8bc3c02017-08-15 11:14:45 -0700657 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600658 else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT))
Philipp Tomsichd8796162017-09-13 21:29:32 +0200659 spl_image->os = IH_OS_U_BOOT;
Simon Glassf1dcee52016-02-22 22:55:56 -0700660
Philipp Tomsichd8796162017-09-13 21:29:32 +0200661 /*
662 * Booting a next-stage U-Boot may require us to append the FDT.
663 * We allow this to fail, as the U-Boot image might embed its FDT.
664 */
Dario Binacchi585b4682020-05-27 13:56:19 +0200665 if (spl_image->os == IH_OS_U_BOOT) {
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600666 ret = spl_fit_append_fdt(spl_image, info, sector, &ctx);
Dario Binacchi585b4682020-05-27 13:56:19 +0200667 if (!IS_ENABLED(CONFIG_OF_EMBED) && ret < 0)
668 return ret;
669 }
Simon Glassf1dcee52016-02-22 22:55:56 -0700670
Jean-Jacques Hiblot6b8b98d2019-10-22 16:39:10 +0200671 firmware_node = node;
Andre Przywara411cf322017-04-26 01:32:37 +0100672 /* Now check if there are more images for us to load */
673 for (; ; index++) {
Philipp Tomsichd8796162017-09-13 21:29:32 +0200674 uint8_t os_type = IH_OS_INVALID;
675
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600676 node = spl_fit_get_image_node(&ctx, "loadables", index);
Andre Przywara411cf322017-04-26 01:32:37 +0100677 if (node < 0)
678 break;
679
Jean-Jacques Hiblot6b8b98d2019-10-22 16:39:10 +0200680 /*
681 * if the firmware is also a loadable, skip it because
682 * it already has been loaded. This is typically the case with
683 * u-boot.img generated by mkimage.
684 */
685 if (firmware_node == node)
686 continue;
687
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600688 ret = spl_load_fit_image(info, sector, &ctx, node, &image_info);
Philippe Reynesc61b2bf2020-11-24 16:15:05 +0100689 if (ret < 0) {
690 printf("%s: can't load image loadables index %d (ret = %d)\n",
691 __func__, index, ret);
692 return ret;
693 }
Andre Przywara411cf322017-04-26 01:32:37 +0100694
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600695 if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
Philipp Tomsichd8796162017-09-13 21:29:32 +0200696 debug("Loadable is %s\n", genimg_get_os_name(os_type));
697
Philipp Tomsicha616c782017-09-13 21:29:34 +0200698 if (os_type == IH_OS_U_BOOT) {
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600699 spl_fit_append_fdt(&image_info, info, sector, &ctx);
Philipp Tomsicha616c782017-09-13 21:29:34 +0200700 spl_image->fdt_addr = image_info.fdt_addr;
701 }
Philipp Tomsichd8796162017-09-13 21:29:32 +0200702
Andre Przywara411cf322017-04-26 01:32:37 +0100703 /*
704 * If the "firmware" image did not provide an entry point,
705 * use the first valid entry point from the loadables.
706 */
707 if (spl_image->entry_point == FDT_ERROR &&
708 image_info.entry_point != FDT_ERROR)
709 spl_image->entry_point = image_info.entry_point;
Philipp Tomsicha616c782017-09-13 21:29:34 +0200710
711 /* Record our loadables into the FDT */
712 if (spl_image->fdt_addr)
Alexandru Gagniuc3dc20792021-01-20 10:46:51 -0600713 spl_fit_record_loadable(&ctx, index,
Philipp Tomsicha616c782017-09-13 21:29:34 +0200714 spl_image->fdt_addr,
715 &image_info);
Andre Przywara411cf322017-04-26 01:32:37 +0100716 }
717
718 /*
719 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
720 * Makefile will set it to 0 and it will end up as the entry point
721 * here. What it actually means is: use the load address.
722 */
723 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
724 spl_image->entry_point = spl_image->load_addr;
725
Ye Lie246bfc2018-11-17 09:10:25 +0000726 spl_image->flags |= SPL_FIT_FOUND;
727
Alexandru Gagniucaeedeae2021-01-20 10:46:55 -0600728 if (IS_ENABLED(CONFIG_IMX_HAB))
729 board_spl_fit_post_load(ctx.fit);
Ye Lie246bfc2018-11-17 09:10:25 +0000730
Andre Przywara411cf322017-04-26 01:32:37 +0100731 return 0;
Simon Glassf1dcee52016-02-22 22:55:56 -0700732}