1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7 #include <errno.h>
8 #include <fpga.h>
9 #include <gzip.h>
10 #include <image.h>
11 #include <log.h>
12 #include <memalign.h>
13 #include <mapmem.h>
14 #include <spl.h>
15 #include <upl.h>
16 #include <sysinfo.h>
17 #include <asm/global_data.h>
18 #include <asm/io.h>
19 #include <linux/libfdt.h>
20 #include <linux/printk.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 struct 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 */
28 int conf_node; /* FDT offset to selected configuration node */
29 };
30
board_spl_fit_size_align(ulong size)31 __weak ulong board_spl_fit_size_align(ulong size)
32 {
33 return size;
34 }
35
find_node_from_desc(const void * fit,int node,const char * str)36 static 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;
47 const char *desc = fdt_getprop(fit, child, FIT_DESC_PROP, &len);
48
49 if (!desc)
50 continue;
51
52 if (!strcmp(desc, str))
53 return child;
54 }
55
56 return -ENOENT;
57 }
58
59 /**
60 * spl_fit_get_image_name(): By using the matching configuration subnode,
61 * 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.
67 * @outname: Name of the image
68 *
69 * Return: 0 on success, or a negative error number
70 */
spl_fit_get_image_name(const struct spl_fit_info * ctx,const char * type,int index,const char ** outname)71 static int spl_fit_get_image_name(const struct spl_fit_info *ctx,
72 const char *type, int index,
73 const char **outname)
74 {
75 struct udevice *sysinfo;
76 const char *name, *str, *end;
77 __maybe_unused int node;
78 int len, i;
79 bool found = true;
80
81 name = fdt_getprop(ctx->fit, ctx->conf_node, type, &len);
82 if (!name) {
83 debug("cannot find property '%s': %d\n", type, len);
84 return -EINVAL;
85 }
86 /* 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 }
92
93 str = name;
94 for (i = 0; i < index; i++) {
95 str = strchr(str, '\0') + 1;
96 if (str > end) {
97 found = false;
98 break;
99 }
100 }
101
102 if (!found && CONFIG_IS_ENABLED(SYSINFO) && !sysinfo_get(&sysinfo)) {
103 int rc;
104 /*
105 * no string in the property for this index. Check if the
106 * sysinfo-level code can supply one.
107 */
108 rc = sysinfo_detect(sysinfo);
109 if (rc)
110 return rc;
111
112 rc = sysinfo_get_fit_loadable(sysinfo, index - i - 1, type,
113 &str);
114 if (rc && rc != -ENOENT)
115 return rc;
116
117 if (!rc) {
118 /*
119 * The sysinfo provided a name for a loadable.
120 * 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;
125 int images = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH);
126
127 node = find_node_from_desc(ctx->fit, images, str);
128 if (node > 0)
129 str = fdt_get_name(ctx->fit, node, NULL);
130
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;
141 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 */
spl_fit_get_image_node(const struct spl_fit_info * ctx,const char * type,int index)156 static int spl_fit_get_image_node(const struct spl_fit_info *ctx,
157 const char *type, int index)
158 {
159 const char *str;
160 int err;
161 int node;
162
163 err = spl_fit_get_image_name(ctx, type, index, &str);
164 if (err)
165 return err;
166
167 debug("%s: '%s'\n", type, str);
168
169 node = fdt_subnode_offset(ctx->fit, ctx->images_node, str);
170 if (node < 0) {
171 pr_err("cannot find image node '%s': %d\n", str, node);
172 return -EINVAL;
173 }
174
175 return node;
176 }
177
get_aligned_image_offset(struct spl_load_info * info,int offset)178 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
179 {
180 return ALIGN_DOWN(offset, spl_get_bl_len(info));
181 }
182
get_aligned_image_overhead(struct spl_load_info * info,int offset)183 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
184 {
185 return offset & (spl_get_bl_len(info) - 1);
186 }
187
get_aligned_image_size(struct spl_load_info * info,int data_size,int offset)188 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
189 int offset)
190 {
191 data_size = data_size + get_aligned_image_overhead(info, offset);
192
193 return ALIGN(data_size, spl_get_bl_len(info));
194 }
195
196 /**
197 * load_simple_fit(): load the image described in a certain FIT node
198 * @info: points to information about the device to load data from
199 * @fit_offset: the offset of the FIT image on the device
200 * @ctx: points to the FIT context structure
201 * @node: offset of the DT node describing the image to load (relative
202 * to @fit)
203 * @image_info: will be filled with information about the loaded image
204 * If the FIT node does not contain a "load" (address) property,
205 * the image gets loaded to the address pointed to by the
206 * load_addr member in this struct, if load_addr is not 0
207 *
208 * Return: 0 on success, -EBADSLT if this image is not the correct phase
209 * (for CONFIG_BOOTMETH_VBE_SIMPLE_FW), or another negative error number on
210 * other error.
211 */
load_simple_fit(struct spl_load_info * info,ulong fit_offset,const struct spl_fit_info * ctx,int node,struct spl_image_info * image_info)212 static int load_simple_fit(struct spl_load_info *info, ulong fit_offset,
213 const struct spl_fit_info *ctx, int node,
214 struct spl_image_info *image_info)
215 {
216 int offset;
217 size_t length;
218 int len;
219 ulong size;
220 ulong load_addr;
221 void *load_ptr;
222 void *src;
223 ulong overhead;
224 uint8_t image_comp = -1, type = -1;
225 const void *data;
226 const void *fit = ctx->fit;
227 bool external_data = false;
228
229 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");
244 return -EBADSLT;
245 }
246 }
247
248 if (IS_ENABLED(CONFIG_SPL_FPGA) ||
249 (IS_ENABLED(CONFIG_SPL_OS_BOOT) && spl_decompression_enabled())) {
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
256 if (spl_decompression_enabled()) {
257 fit_image_get_comp(fit, node, &image_comp);
258 debug("%s ", genimg_get_comp_name(image_comp));
259 }
260
261 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 }
267 load_addr = image_info->load_addr;
268 }
269
270 if (!fit_image_get_data_position(fit, node, &offset)) {
271 external_data = true;
272 } else if (!fit_image_get_data_offset(fit, node, &offset)) {
273 log_debug("read offset %x = offset from fit %lx\n",
274 offset, (ulong)offset + ctx->ext_data_offset);
275 offset += ctx->ext_data_offset;
276 external_data = true;
277 }
278
279 if (external_data) {
280 ulong read_offset;
281 void *src_ptr;
282
283 /* External data */
284 if (fit_image_get_data_size(fit, node, &len))
285 return -ENOENT;
286
287 /* 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
294 if (spl_decompression_enabled() &&
295 (image_comp == IH_COMP_GZIP || image_comp == IH_COMP_LZMA))
296 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);
299 length = len;
300
301 overhead = get_aligned_image_overhead(info, offset);
302 size = get_aligned_image_size(info, length, offset);
303 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);
307
308 if (info->read(info, read_offset, size, src_ptr) < length)
309 return -EIO;
310
311 debug("External data: dst=%p, offset=%x, size=%lx\n",
312 src_ptr, offset, (unsigned long)length);
313 src = src_ptr + overhead;
314 } else {
315 /* Embedded data */
316 if (fit_image_get_emb_data(fit, node, &data, &length)) {
317 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);
322 src = (void *)data; /* cast away const */
323 }
324
325 if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
326 printf("## Checking hash(es) for Image %s ... ",
327 fit_get_name(fit, node, NULL));
328 if (!fit_image_verify_with_data(fit, node, gd_fdt_blob(), src,
329 length))
330 return -EPERM;
331 puts("OK\n");
332 }
333
334 if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS))
335 board_fit_image_post_process(fit, node, &src, &length);
336
337 load_ptr = map_sysmem(load_addr, length);
338 if (IS_ENABLED(CONFIG_SPL_GZIP) && image_comp == IH_COMP_GZIP) {
339 size = length;
340 if (gunzip(load_ptr, CONFIG_SYS_BOOTM_LEN, src, &size)) {
341 puts("Uncompressing error\n");
342 return -EIO;
343 }
344 length = size;
345 } 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;
355 } else {
356 memcpy(load_ptr, src, length);
357 }
358
359 if (image_info) {
360 ulong entry_point;
361
362 image_info->load_addr = load_addr;
363 image_info->size = length;
364
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;
369 }
370 log_debug("- done loading\n");
371
372 upl_add_image(fit, node, load_addr, length);
373
374 return 0;
375 }
376
os_takes_devicetree(uint8_t os)377 static 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:
383 return IS_ENABLED(CONFIG_SPL_OS_BOOT) ||
384 IS_ENABLED(CONFIG_SPL_OPENSBI);
385 default:
386 return false;
387 }
388 }
389
board_spl_fit_append_fdt_skip(const char * name)390 __weak int board_spl_fit_append_fdt_skip(const char *name)
391 {
392 return 0; /* Do not skip */
393 }
394
spl_fit_append_fdt(struct spl_image_info * spl_image,struct spl_load_info * info,ulong offset,const struct spl_fit_info * ctx)395 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
396 struct spl_load_info *info, ulong offset,
397 const struct spl_fit_info *ctx)
398 {
399 struct spl_image_info image_info;
400 int node, ret = 0, index = 0;
401
402 /*
403 * Use the address following the image as target address for the
404 * device tree.
405 */
406 image_info.load_addr = ALIGN(spl_image->load_addr + spl_image->size, 8);
407
408 /* Figure out which device tree the board wants to use */
409 node = spl_fit_get_image_node(ctx, FIT_FDT_PROP, index++);
410 if (node < 0) {
411 size_t size;
412
413 debug("%s: cannot find FDT node\n", __func__);
414
415 /*
416 * U-Boot did not find a device tree inside the FIT image. Use
417 * the U-Boot device tree instead.
418 */
419 if (!gd->fdt_blob)
420 return node;
421
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);
429 } else {
430 ret = load_simple_fit(info, offset, ctx, node, &image_info);
431 if (ret < 0)
432 return ret;
433
434 spl_image->fdt_addr = phys_to_virt(image_info.load_addr);
435 }
436
437 if (CONFIG_IS_ENABLED(FIT_IMAGE_TINY))
438 return 0;
439
440 #if CONFIG_IS_ENABLED(LOAD_FIT_APPLY_OVERLAY)
441 void *tmpbuffer = NULL;
442
443 for (; ; index++) {
444 const char *str;
445
446 ret = spl_fit_get_image_name(ctx, FIT_FDT_PROP, index, &str);
447 if (ret == -E2BIG) {
448 debug("%s: No additional FDT node\n", __func__);
449 ret = 0;
450 break;
451 } 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) {
461 debug("%s: unable to find FDT node %d\n",
462 __func__, index);
463 continue;
464 }
465
466 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 */
473 size_t size = CONFIG_SPL_LOAD_FIT_APPLY_OVERLAY_BUF_SZ;
474
475 tmpbuffer = malloc_cache_aligned(size);
476 if (!tmpbuffer)
477 debug("%s: unable to allocate space for overlays\n",
478 __func__);
479 }
480 image_info.load_addr = (ulong)tmpbuffer;
481 ret = load_simple_fit(info, offset, ctx, node,
482 &image_info);
483 if (ret == -EBADSLT)
484 continue;
485 else if (ret < 0)
486 break;
487
488 /* 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)
492 break;
493
494 ret = fdt_overlay_apply_verbose(spl_image->fdt_addr,
495 (void *)image_info.load_addr);
496 if (ret) {
497 pr_err("failed to apply DT overlay %s\n",
498 fit_get_name(ctx->fit, node, NULL));
499 break;
500 }
501
502 debug("%s: DT overlay %s applied\n", __func__,
503 fit_get_name(ctx->fit, node, NULL));
504 }
505 free(tmpbuffer);
506 if (ret)
507 return ret;
508 #endif
509 /* 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;
513
514 return ret;
515 }
516
spl_fit_record_loadable(const struct spl_fit_info * ctx,int index,void * blob,struct spl_image_info * image)517 static int spl_fit_record_loadable(const struct spl_fit_info *ctx, int index,
518 void *blob, struct spl_image_info *image)
519 {
520 int ret = 0;
521 const char *name;
522 int node;
523
524 ret = spl_fit_get_image_name(ctx, "loadables", index, &name);
525 if (ret < 0)
526 return ret;
527
528 node = spl_fit_get_image_node(ctx, "loadables", index);
529
530 ret = fdt_record_loadable(blob, index, name, image->load_addr,
531 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
536 return ret;
537 }
538
spl_fit_image_is_fpga(const void * fit,int node)539 static 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
spl_fit_image_get_os(const void * fit,int noffset,uint8_t * os)553 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
554 {
555 if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) || CONFIG_IS_ENABLED(OS_BOOT))
556 return fit_image_get_os(fit, noffset, os);
557
558 const char *name = fdt_getprop(fit, noffset, FIT_OS_PROP, NULL);
559 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;
573 }
574
575 /*
576 * 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 */
spl_get_fit_load_buffer(size_t size)579 static void *spl_get_fit_load_buffer(size_t size)
580 {
581 void *buf;
582
583 buf = malloc_cache_aligned(size);
584 if (!buf) {
585 pr_err("Could not get FIT buffer of %lu bytes\n", (ulong)size);
586
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
592 buf = spl_get_load_buffer(0, size);
593 }
594 return buf;
595 }
596
board_spl_fit_buffer_addr(ulong fit_size,int sectors,int bl_len)597 __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
602 /*
603 * 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 */
spl_load_simple_fit_skip_processing(void)607 __weak bool spl_load_simple_fit_skip_processing(void)
608 {
609 return false;
610 }
611
612 /*
613 * Weak default function to allow fixes after fit header
614 * is loaded.
615 */
spl_load_simple_fit_fix_load(const void * fit)616 __weak void *spl_load_simple_fit_fix_load(const void *fit)
617 {
618 return (void *)fit;
619 }
620
warn_deprecated(const char * msg)621 static void warn_deprecated(const char *msg)
622 {
623 printf("DEPRECATED: %s\n", msg);
624 printf("\tSee https://fitspec.osfw.foundation/\n");
625 }
626
spl_fit_upload_fpga(struct spl_fit_info * ctx,int node,struct spl_image_info * fpga_image)627 static int spl_fit_upload_fpga(struct spl_fit_info *ctx, int node,
628 struct spl_image_info *fpga_image)
629 {
630 const char *compatible;
631 int ret;
632 int devnum = 0;
633 int flags = 0;
634
635 debug("FPGA bitstream at: %x, size: %x\n",
636 (u32)fpga_image->load_addr, fpga_image->size);
637
638 compatible = fdt_getprop(ctx->fit, node, "compatible", NULL);
639 if (!compatible) {
640 warn_deprecated("'fpga' image without 'compatible' property");
641 } 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 }
648
649 ret = fpga_load(devnum, (void *)fpga_image->load_addr,
650 fpga_image->size, BIT_FULL, flags);
651 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
spl_fit_load_fpga(struct spl_fit_info * ctx,struct spl_load_info * info,ulong offset)661 static int spl_fit_load_fpga(struct spl_fit_info *ctx,
662 struct spl_load_info *info, ulong offset)
663 {
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
674 warn_deprecated("'fpga' property in config node. Use 'loadables'");
675
676 /* Load the image and set up the fpga_image structure */
677 ret = load_simple_fit(info, offset, ctx, node, &fpga_image);
678 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
spl_simple_fit_read(struct spl_fit_info * ctx,struct spl_load_info * info,ulong offset,const void * fit_header)686 static int spl_simple_fit_read(struct spl_fit_info *ctx,
687 struct spl_load_info *info, ulong offset,
688 const void *fit_header)
689 {
690 unsigned long count, size;
691 void *buf;
692
693 /*
694 * 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.
697 */
698 size = ALIGN(fdt_totalsize(fit_header), 4);
699 size = board_spl_fit_size_align(size);
700 ctx->ext_data_offset = ALIGN(size, 4);
701
702 /*
703 * So far we only have one block of data from the FIT. Read the entire
704 * thing, including that first block.
705 *
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.
708 */
709 size = get_aligned_image_size(info, size, 0);
710 buf = board_spl_fit_buffer_addr(size, size, 1);
711 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 }
719
720 count = info->read(info, offset, size, buf);
721 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
751 ctx->fit = buf;
752 debug("fit read offset %lx, size=%lu, dst=%p, count=%lu\n",
753 offset, size, buf, count);
754
755 return 0;
756 }
757
spl_simple_fit_parse(struct spl_fit_info * ctx)758 static int spl_simple_fit_parse(struct spl_fit_info *ctx)
759 {
760 /* 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;
764
765 if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
766 printf("## Checking hash(es) for config %s ... ",
767 fit_get_name(ctx->fit, ctx->conf_node, NULL));
768 if (fit_config_verify(ctx->fit, ctx->conf_node))
769 return -EPERM;
770 puts("OK\n");
771 }
772
773 /* find the node holding the images information */
774 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;
779 }
780
781 return 0;
782 }
783
spl_load_simple_fit(struct spl_image_info * spl_image,struct spl_load_info * info,ulong offset,void * fit)784 int spl_load_simple_fit(struct spl_image_info *spl_image,
785 struct spl_load_info *info, ulong offset, void *fit)
786 {
787 struct spl_image_info image_info;
788 struct spl_fit_info ctx;
789 int node = -1;
790 int ret;
791 int index = 0;
792 int firmware_node;
793
794 ret = spl_simple_fit_read(&ctx, info, offset, fit);
795 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
802 ctx.fit = spl_load_simple_fit_fix_load(ctx.fit);
803
804 ret = spl_simple_fit_parse(&ctx);
805 if (ret < 0)
806 return ret;
807
808 if (IS_ENABLED(CONFIG_SPL_FPGA))
809 spl_fit_load_fpga(&ctx, info, offset);
810
811 /*
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)
818 node = spl_fit_get_image_node(&ctx, FIT_FIRMWARE_PROP, 0);
819
820 if (node < 0 && IS_ENABLED(CONFIG_SPL_OS_BOOT))
821 node = spl_fit_get_image_node(&ctx, FIT_KERNEL_PROP, 0);
822
823 if (node < 0) {
824 debug("could not find firmware image, trying loadables...\n");
825 node = spl_fit_get_image_node(&ctx, "loadables", 0);
826 /*
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;
831 }
832 if (node < 0) {
833 debug("%s: Cannot find u-boot image node: %d\n",
834 __func__, node);
835 return -1;
836 }
837
838 /* Load the image and set up the spl_image structure */
839 ret = load_simple_fit(info, offset, &ctx, node, spl_image);
840 if (ret)
841 return ret;
842
843 /*
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 */
847 if (!spl_fit_image_get_os(ctx.fit, node, &spl_image->os))
848 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
849 else if (!IS_ENABLED(CONFIG_SPL_OS_BOOT))
850 spl_image->os = IH_OS_U_BOOT;
851
852 /*
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 */
856 if (os_takes_devicetree(spl_image->os)) {
857 ret = spl_fit_append_fdt(spl_image, info, offset, &ctx);
858 if (ret < 0 && spl_image->os != IH_OS_U_BOOT)
859 return ret;
860 }
861
862 firmware_node = node;
863 /* Now check if there are more images for us to load */
864 for (; ; index++) {
865 uint8_t os_type = IH_OS_INVALID;
866
867 node = spl_fit_get_image_node(&ctx, "loadables", index);
868 if (node < 0)
869 break;
870
871 /*
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
879 image_info.load_addr = 0;
880 ret = load_simple_fit(info, offset, &ctx, node, &image_info);
881 if (ret < 0 && ret != -EBADSLT) {
882 printf("%s: can't load image loadables index %d (ret = %d)\n",
883 __func__, index, ret);
884 return ret;
885 }
886
887 if (spl_fit_image_is_fpga(ctx.fit, node))
888 spl_fit_upload_fpga(&ctx, node, &image_info);
889
890 if (!spl_fit_image_get_os(ctx.fit, node, &os_type))
891 debug("Loadable is %s\n", genimg_get_os_name(os_type));
892
893 if (os_takes_devicetree(os_type)) {
894 spl_fit_append_fdt(&image_info, info, offset, &ctx);
895 spl_image->fdt_addr = image_info.fdt_addr;
896 }
897
898 /*
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;
905
906 /* Record our loadables into the FDT */
907 if (!CONFIG_IS_ENABLED(FIT_IMAGE_TINY) &&
908 xpl_get_fdt_update(info) && spl_image->fdt_addr)
909 spl_fit_record_loadable(&ctx, index,
910 spl_image->fdt_addr,
911 &image_info);
912 }
913
914 /*
915 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
916 * 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
922 spl_image->flags |= SPL_FIT_FOUND;
923 upl_set_fit_info(map_to_sysmem(ctx.fit), ctx.conf_node,
924 spl_image->entry_point);
925
926 return 0;
927 }
928
929 /* Parse and load full fitImage in SPL */
spl_load_fit_image(struct spl_image_info * spl_image,const struct legacy_img_hdr * header)930 int 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;
935 ulong fdt_hack;
936 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
945 ret = fit_image_load(&images, virt_to_phys((void *)header),
946 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 {
953 ret = fit_image_load(&images, virt_to_phys((void *)header),
954 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
955 IH_TYPE_FIRMWARE, -1, FIT_LOAD_OPTIONAL,
956 &fw_data, &fw_len);
957 }
958
959 if (ret < 0) {
960 ret = fit_image_load(&images, virt_to_phys((void *)header),
961 NULL, &fit_uname_config, IH_ARCH_DEFAULT,
962 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;
970 spl_image->load_addr = fw_data;
971 if (fit_image_get_entry(header, ret, &spl_image->entry_point))
972 spl_image->entry_point = fw_data;
973 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
977 debug(PHASE_PROMPT "payload image: %32s load addr: 0x%lx size: %d\n",
978 spl_image->name, spl_image->load_addr, spl_image->size);
979
980 #ifdef CONFIG_SPL_FIT_SIGNATURE
981 images.verify = 1;
982 #endif
983 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);
986 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 }
1019 spl_image->flags |= SPL_FIT_FOUND;
1020
1021 upl_set_fit_info(map_to_sysmem(header), conf_noffset,
1022 spl_image->entry_point);
1023
1024 return 0;
1025 }
1026