xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision b6bda7d5f40f8df92d3195bfab2b7ecbabd3e740)
1 /*
2  * Copyright (C) 2016 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <boot_rkimg.h>
10 #include <keylad.h>
11 #include <crypto.h>
12 #include <errno.h>
13 #include <fdt_support.h>
14 #include <image.h>
15 #include <malloc.h>
16 #include <mtd_blk.h>
17 #include <mp_boot.h>
18 #include <spl.h>
19 #include <spl_ab.h>
20 #include <linux/libfdt.h>
21 
22 #ifndef CONFIG_SYS_BOOTM_LEN
23 #define CONFIG_SYS_BOOTM_LEN	(64 << 20)
24 #endif
25 
26 /**
27  * spl_fit_get_image_name(): By using the matching configuration subnode,
28  * retrieve the name of an image, specified by a property name and an index
29  * into that.
30  * @fit:	Pointer to the FDT blob.
31  * @images:	Offset of the /images subnode.
32  * @type:	Name of the property within the configuration subnode.
33  * @index:	Index into the list of strings in this property.
34  * @outname:	Name of the image
35  *
36  * Return:	0 on success, or a negative error number
37  */
38 static int spl_fit_get_image_name(const void *fit, int images,
39 				  const char *type, int index,
40 				  char **outname)
41 {
42 	const char *name, *str;
43 	__maybe_unused int node;
44 	int conf_node;
45 	int len, i;
46 
47 	conf_node = fit_find_config_node(fit);
48 	if (conf_node < 0) {
49 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
50 		printf("No matching DT out of these options:\n");
51 		for (node = fdt_first_subnode(fit, conf_node);
52 		     node >= 0;
53 		     node = fdt_next_subnode(fit, node)) {
54 			name = fdt_getprop(fit, node, "description", &len);
55 			printf("   %s\n", name);
56 		}
57 #endif
58 		return conf_node;
59 	}
60 
61 	name = fdt_getprop(fit, conf_node, type, &len);
62 	if (!name) {
63 		debug("cannot find property '%s': %d\n", type, len);
64 		return -EINVAL;
65 	}
66 
67 	str = name;
68 	for (i = 0; i < index; i++) {
69 		str = strchr(str, '\0') + 1;
70 		if (!str || (str - name >= len)) {
71 			debug("no string for index %d\n", index);
72 			return -E2BIG;
73 		}
74 	}
75 
76 	*outname = (char *)str;
77 	return 0;
78 }
79 
80 /**
81  * spl_fit_get_image_node(): By using the matching configuration subnode,
82  * retrieve the name of an image, specified by a property name and an index
83  * into that.
84  * @fit:	Pointer to the FDT blob.
85  * @images:	Offset of the /images subnode.
86  * @type:	Name of the property within the configuration subnode.
87  * @index:	Index into the list of strings in this property.
88  *
89  * Return:	the node offset of the respective image node or a negative
90  *		error number.
91  */
92 static int spl_fit_get_image_node(const void *fit, int images,
93 				  const char *type, int index)
94 {
95 	char *str;
96 	int err;
97 	int node;
98 
99 	err = spl_fit_get_image_name(fit, images, type, index, &str);
100 	if (err)
101 		return err;
102 
103 	debug("%s: '%s'\n", type, str);
104 
105 	node = fdt_subnode_offset(fit, images, str);
106 	if (node < 0) {
107 		debug("cannot find image node '%s': %d\n", str, node);
108 		return -EINVAL;
109 	}
110 
111 	return node;
112 }
113 
114 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
115 {
116 	/*
117 	 * If it is a FS read, get the first address before offset which is
118 	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
119 	 * block number to which offset belongs.
120 	 */
121 	if (info->filename)
122 		return offset & ~(ARCH_DMA_MINALIGN - 1);
123 
124 	return offset / info->bl_len;
125 }
126 
127 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
128 {
129 	/*
130 	 * If it is a FS read, get the difference between the offset and
131 	 * the first address before offset which is aligned to
132 	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
133 	 * block.
134 	 */
135 	if (info->filename)
136 		return offset & (ARCH_DMA_MINALIGN - 1);
137 
138 	return offset % info->bl_len;
139 }
140 
141 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
142 				  int offset)
143 {
144 	data_size = data_size + get_aligned_image_overhead(info, offset);
145 
146 	if (info->filename)
147 		return data_size;
148 
149 	return (data_size + info->bl_len - 1) / info->bl_len;
150 }
151 
152 #ifdef CONFIG_SPL_FIT_CIPHER
153 static int spl_fit_image_uncipher(const void *fit, int noffset,
154 				  ulong cipher_addr, size_t cipher_sz,
155 				  ulong uncipher_addr)
156 {
157 	struct udevice *dev;
158 	cipher_fw_context ctx;
159 	int cipher_noffset;
160 	const char *node_name;
161 	const void *iv;
162 	char *algo_name;
163 	int key_len = 16;
164 	int iv_len;
165 	int ret;
166 
167 	node_name = fdt_get_name(fit, noffset, NULL);
168 	if (!node_name) {
169 		printf("Can't get node name.\n");
170 		return -1;
171 	}
172 
173 	cipher_noffset = fdt_subnode_offset(fit, noffset, FIT_CIPHER_NODENAME);
174 	if (cipher_noffset < 0) {
175 		printf("Can't get cipher node offset for image '%s'\n",
176 		       node_name);
177 		return -1;
178 	}
179 
180 	if (fit_image_cipher_get_algo(fit, cipher_noffset, &algo_name)) {
181 		printf("Can't get cipher algo for image '%s'\n",
182 		       node_name);
183 		return -1;
184 	}
185 
186 	if (strcmp(algo_name, "aes128")) {
187 		printf("Invalid cipher algo '%s'\n", algo_name);
188 		return -1;
189 	}
190 
191 	iv = fdt_getprop(fit, cipher_noffset, "iv", &iv_len);
192 	if (!iv) {
193 		printf("Can't get IV for image '%s'\n", node_name);
194 		return -1;
195 	}
196 
197 	if (iv_len != key_len) {
198 		printf("Len iv(%d) != key(%d) for image '%s'\n",
199 		       iv_len, key_len, node_name);
200 		return -1;
201 	}
202 
203 	memset(&ctx, 0x00, sizeof(ctx));
204 
205 	ctx.algo    = CRYPTO_AES;
206 	ctx.mode    = RK_MODE_CTR;
207 	ctx.key_len = key_len;
208 	ctx.iv      = iv;
209 	ctx.iv_len  = iv_len;
210 	ctx.fw_keyid = RK_FW_KEY0;
211 
212 	dev = crypto_get_device(CRYPTO_AES);
213 	if (!dev) {
214 		printf("No crypto device for expected AES\n");
215 		return -ENODEV;
216 	}
217 
218 	/* uncipher */
219 	ret = crypto_fw_cipher(dev, &ctx, (void *)cipher_addr,
220 		(void *)uncipher_addr, cipher_sz, true);
221 
222 	if (ret) {
223 		printf("Uncipher data failed for image '%s', ret=%d\n",
224 		       node_name, ret);
225 		return ret;
226 	}
227 
228 	return 0;
229 }
230 #endif
231 
232 /**
233  * spl_load_fit_image(): load the image described in a certain FIT node
234  * @info:	points to information about the device to load data from
235  * @sector:	the start sector of the FIT image on the device
236  * @fit:	points to the flattened device tree blob describing the FIT
237  *		image
238  * @base_offset: the beginning of the data area containing the actual
239  *		image data, relative to the beginning of the FIT
240  * @node:	offset of the DT node describing the image to load (relative
241  *		to @fit)
242  * @image_info:	will be filled with information about the loaded image
243  *		If the FIT node does not contain a "load" (address) property,
244  *		the image gets loaded to the address pointed to by the
245  *		load_addr member in this struct.
246  *
247  * Return:	0 on success or a negative error number.
248  */
249 static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
250 			      void *fit, ulong base_offset, int node,
251 			      struct spl_image_info *image_info)
252 {
253 	int offset;
254 	size_t length;
255 	int len;
256 	ulong size;
257 	ulong comp_addr, load_addr, load_ptr;
258 	void *src;
259 	ulong overhead;
260 	int nr_sectors;
261 	int align_len = ARCH_DMA_MINALIGN - 1;
262 	uint8_t image_comp = -1, type = -1;
263 	const void *data;
264 	bool external_data = false;
265 
266 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
267 		if (fit_image_get_comp(fit, node, &image_comp))
268 			puts("Cannot get image compression format.\n");
269 		else
270 			debug("%s ", genimg_get_comp_name(image_comp));
271 
272 		if (fit_image_get_type(fit, node, &type))
273 			puts("Cannot get image type.\n");
274 		else
275 			debug("%s ", genimg_get_type_name(type));
276 	} else {
277 		fit_image_get_comp(fit, node, &image_comp);
278 	}
279 
280 	if (fit_image_get_load(fit, node, &load_addr))
281 		load_addr = image_info->load_addr;
282 
283 	if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE) {
284 		/* Empirically, 2MB is enough for U-Boot, tee and atf */
285 		if (fit_image_get_comp_addr(fit, node, &comp_addr))
286 			comp_addr = load_addr + FIT_MAX_SPL_IMAGE_SZ;
287 	} else {
288 		comp_addr = load_addr;
289 	}
290 
291 #ifdef CONFIG_SPL_FIT_CIPHER
292 	ulong cipher_addr;
293 
294 	if (fit_image_get_cipher_addr(fit, node, &cipher_addr))
295 		cipher_addr = comp_addr + FIT_MAX_SPL_IMAGE_SZ;
296 #endif
297 
298 	if (!fit_image_get_data_position(fit, node, &offset)) {
299 		external_data = true;
300 	} else if (!fit_image_get_data_offset(fit, node, &offset)) {
301 		offset += base_offset;
302 		external_data = true;
303 	}
304 
305 	if (external_data) {
306 		/* External data */
307 		if (fit_image_get_data_size(fit, node, &len))
308 			return -ENOENT;
309 
310 		load_ptr = (comp_addr + align_len) & ~align_len;
311 #if  defined(CONFIG_ARCH_ROCKCHIP)
312 		if ((load_ptr < CONFIG_SYS_SDRAM_BASE) ||
313 		     (load_ptr >= CONFIG_SYS_SDRAM_BASE + SDRAM_MAX_SIZE))
314 			load_ptr = (ulong)memalign(ARCH_DMA_MINALIGN, len);
315 #endif
316 		length = len;
317 
318 		overhead = get_aligned_image_overhead(info, offset);
319 		nr_sectors = get_aligned_image_size(info, length, offset);
320 
321 		if (info->read(info,
322 			       sector + get_aligned_image_offset(info, offset),
323 			       nr_sectors, (void *)load_ptr) != nr_sectors)
324 			return -EIO;
325 
326 		debug("External data: dst=%lx, offset=%x, size=%lx\n",
327 		      load_ptr, offset, (unsigned long)length);
328 		src = (void *)load_ptr + overhead;
329 	} else {
330 		/* Embedded data */
331 		if (fit_image_get_data(fit, node, &data, &length)) {
332 			puts("Cannot get image data/size\n");
333 			return -ENOENT;
334 		}
335 		debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
336 		      (unsigned long)length);
337 		src = (void *)data;
338 	}
339 
340 	/* Check hashes and signature */
341 	if (image_comp != IH_COMP_NONE && image_comp != IH_COMP_ZIMAGE)
342 		printf("## Checking %s 0x%08lx (%s @0x%08lx) ... ",
343 		       fit_get_name(fit, node, NULL), load_addr,
344 		       (char *)fdt_getprop(fit, node, FIT_COMP_PROP, NULL),
345 		       (long)src);
346 	else
347 		printf("## Checking %s 0x%08lx ... ",
348 		       fit_get_name(fit, node, NULL), load_addr);
349 
350 #ifdef CONFIG_FIT_SPL_PRINT
351 	printf("\n");
352 	fit_image_print(fit, node, "");
353 #endif
354 	if (!fit_image_verify_with_data(fit, node,
355 					 src, length))
356 		return -EPERM;
357 
358 #ifdef CONFIG_SPL_FIT_CIPHER
359 	if (fdt_subnode_offset(fit, node, FIT_CIPHER_NODENAME) > 0) {
360 		printf(" Decrypting Data ...");
361 		memcpy((void *)cipher_addr, src, length);
362 		if (spl_fit_image_uncipher(fit, node, cipher_addr, length, (ulong)src))
363 			return -EACCES;
364 		printf(" OK ");
365 	}
366 #endif
367 
368 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
369 	if (board_fit_image_post_process(fit, node, (ulong *)&load_addr,
370 					 (ulong **)&src, &length, info))
371 		return -EINVAL;
372 #endif
373 	puts("OK\n");
374 
375 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT)	&&
376 	    IS_ENABLED(CONFIG_SPL_GZIP)		&&
377 	    image_comp == IH_COMP_GZIP		&&
378 	    type == IH_TYPE_KERNEL) {
379 		size = length;
380 		if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
381 			   src, &size)) {
382 			puts("Uncompressing error\n");
383 			return -EIO;
384 		}
385 		length = size;
386 	} else {
387 		memcpy((void *)load_addr, src, length);
388 	}
389 
390 	if (image_info) {
391 		image_info->load_addr = load_addr;
392 		image_info->size = length;
393 		image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
394 	}
395 
396 	return 0;
397 }
398 
399 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
400 			      struct spl_load_info *info, ulong sector,
401 			      void *fit, int images, ulong base_offset)
402 {
403 	struct spl_image_info image_info;
404 	int node, ret;
405 
406 	/* Figure out which device tree the board wants to use */
407 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
408 	if (node < 0) {
409 		debug("%s: cannot find FDT node\n", __func__);
410 		return node;
411 	}
412 
413 	/*
414 	 * Read the device tree and place it after the image.
415 	 * Align the destination address to ARCH_DMA_MINALIGN.
416 	 */
417 	image_info.load_addr = spl_image->load_addr + spl_image->size;
418 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
419 				 &image_info);
420 
421 	if (ret < 0)
422 		return ret;
423 
424 	/* Make the load-address of the FDT available for the SPL framework */
425 	spl_image->fdt_addr = (void *)image_info.load_addr;
426 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
427 	/* Try to make space, so we can inject details on the loadables */
428 	fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
429 #endif
430 
431 	/*
432 	 * If need, load kernel FDT right after U-Boot FDT.
433 	 *
434 	 * kernel FDT is for U-Boot if there is not valid one
435 	 * from images, ie: resource.img, boot.img or recovery.img.
436 	 */
437 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 1);
438 	if (node < 0) {
439 		debug("%s: cannot find kernel FDT node\n", __func__);
440 		/* attention: here return ret but not node */
441 		return ret;
442 	}
443 
444 	image_info.load_addr =
445 		(ulong)spl_image->fdt_addr + fdt_totalsize(spl_image->fdt_addr);
446 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
447 				 &image_info);
448 
449 	return ret;
450 }
451 
452 static int spl_fit_record_loadable(const void *fit, int images, int index,
453 				   void *blob, struct spl_image_info *image)
454 {
455 	int ret = 0;
456 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
457 	char *name;
458 	int node;
459 
460 	ret = spl_fit_get_image_name(fit, images, "loadables",
461 				     index, &name);
462 	if (ret < 0)
463 		return ret;
464 
465 	node = spl_fit_get_image_node(fit, images, "loadables", index);
466 
467 	ret = fdt_record_loadable(blob, index, name, image->load_addr,
468 				  image->size, image->entry_point,
469 				  fdt_getprop(fit, node, "type", NULL),
470 				  fdt_getprop(fit, node, "os", NULL));
471 #endif
472 	return ret;
473 }
474 
475 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
476 {
477 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
478 	return -ENOTSUPP;
479 #else
480 	return fit_image_get_os(fit, noffset, os);
481 #endif
482 }
483 
484 __weak int spl_fit_standalone_release(char *id, uintptr_t entry_point)
485 {
486 	return 0;
487 }
488 
489 static void *spl_fit_load_blob(struct spl_load_info *info,
490 			       ulong sector, void *fit_header,
491 			       int *base_offset)
492 {
493 	int align_len = ARCH_DMA_MINALIGN - 1;
494 	ulong count;
495 	ulong size;
496 	int sectors;
497 	void *fit;
498 
499 	/*
500 	 * For FIT with external data, figure out where the external images
501 	 * start. This is the base for the data-offset properties in each
502 	 * image.
503 	 */
504 	size = fdt_totalsize(fit_header);
505 	size = FIT_ALIGN(size);
506 	*base_offset = FIT_ALIGN(size);
507 
508 	/*
509 	 * So far we only have one block of data from the FIT. Read the entire
510 	 * thing, including that first block, placing it so it finishes before
511 	 * where we will load the image.
512 	 *
513 	 * Note that we will load the image such that its first byte will be
514 	 * at the load address. Since that byte may be part-way through a
515 	 * block, we may load the image up to one block before the load
516 	 * address. So take account of that here by subtracting an addition
517 	 * block length from the FIT start position.
518 	 *
519 	 * In fact the FIT has its own load address, but we assume it cannot
520 	 * be before CONFIG_SYS_TEXT_BASE.
521 	 *
522 	 * For FIT with data embedded, data is loaded as part of FIT image.
523 	 * For FIT with external data, data is not loaded in this step.
524 	 */
525 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
526 			align_len) & ~align_len);
527 	sectors = get_aligned_image_size(info, size, 0);
528 	count = info->read(info, sector, sectors, fit);
529 #if defined(CONFIG_SPL_MTD_SUPPORT) && !defined(CONFIG_FPGA_RAM)
530 	mtd_blk_map_fit(info->dev, sector, fit);
531 #endif
532 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
533 	      sector, sectors, fit, count);
534 	if (count == 0)
535 		return NULL;
536 
537 	return fit;
538 }
539 
540 #ifdef CONFIG_SPL_KERNEL_BOOT
541 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
542 __weak const char *spl_kernel_partition(struct spl_image_info *spl,
543 					struct spl_load_info *info)
544 {
545 	return PART_BOOT;
546 }
547 #endif
548 
549 static int spl_fit_get_kernel_dtb(const void *fit, int images_noffset)
550 {
551 	const char *name = NULL;
552 	int node, index = 0;
553 
554 	for (; ; index++) {
555 		node = spl_fit_get_image_node(fit, images_noffset,
556 					      FIT_FDT_PROP, index);
557 		if (node < 0)
558 			break;
559 		name = fdt_get_name(fit, node, NULL);
560 		if(!strcmp(name, "fdt"))
561 			return node;
562 #if defined(CONFIG_SPL_ROCKCHIP_HWID_DTB)
563 		if (spl_find_hwid_dtb(name)) {
564 			printf("HWID DTB: %s\n", name);
565 			break;
566 		}
567 #endif
568 	}
569 
570 	return node;
571 }
572 
573 static int spl_load_kernel_fit(struct spl_image_info *spl_image,
574 			       struct spl_load_info *info)
575 {
576 	/*
577 	 * Never change the image order.
578 	 *
579 	 * Considering thunder-boot feature, there maybe asynchronous
580 	 * loading operation of these images and ramdisk is usually to
581 	 * be the last one.
582 	 *
583 	 * The .its content rule of kernel fit image follows U-Boot proper.
584 	 */
585 	const char *images[] = { FIT_FDT_PROP, FIT_KERNEL_PROP, FIT_RAMDISK_PROP, };
586 	struct spl_image_info image_info;
587 	char fit_header[info->bl_len];
588 	int images_noffset;
589 	int base_offset;
590 	int sector;
591 	int node, ret, i;
592 	void *fit;
593 
594 	if (spl_image->next_stage != SPL_NEXT_STAGE_KERNEL)
595 		return 0;
596 
597 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
598 	const char *part_name = PART_BOOT;
599 	disk_partition_t part_info;
600 
601 	part_name = spl_kernel_partition(spl_image, info);
602 	if (part_get_info_by_name(info->dev, part_name, &part_info) <= 0) {
603 		printf("%s: no partition\n", __func__);
604 		return -EINVAL;
605 	}
606 	sector = part_info.start;
607 #else
608 	sector = CONFIG_SPL_KERNEL_BOOT_SECTOR;
609 #endif
610 	printf("Trying kernel at 0x%x sector from '%s' part\n", sector, part_name);
611 
612 	if (info->read(info, sector, 1, &fit_header) != 1) {
613 		debug("%s: Failed to read header\n", __func__);
614 		return -EIO;
615 	}
616 
617 	if (image_get_magic((void *)&fit_header) != FDT_MAGIC) {
618 		printf("%s: Not fit magic\n", __func__);
619 		return -EINVAL;
620 	}
621 
622 	fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
623 	if (!fit) {
624 		debug("%s: Cannot load blob\n", __func__);
625 		return -ENODEV;
626 	}
627 
628 	/* verify the configure node by keys, if required */
629 #ifdef CONFIG_SPL_FIT_SIGNATURE
630 	int conf_noffset;
631 
632 	conf_noffset = fit_conf_get_node(fit, NULL);
633 	if (conf_noffset <= 0) {
634 		printf("No default config node\n");
635 		return -EINVAL;
636 	}
637 
638 	ret = fit_config_verify(fit, conf_noffset);
639 	if (ret) {
640 		printf("fit verify configure failed, ret=%d\n", ret);
641 		return ret;
642 	}
643 	printf("\n");
644 #endif
645 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
646 	if (images_noffset < 0) {
647 		debug("%s: Cannot find /images node: %d\n",
648 		      __func__, images_noffset);
649 		return images_noffset;
650 	}
651 
652 	for (i = 0; i < ARRAY_SIZE(images); i++) {
653 		if (!strcmp(images[i], FIT_FDT_PROP))
654 			node = spl_fit_get_kernel_dtb(fit, images_noffset);
655 		else
656 			node = spl_fit_get_image_node(fit, images_noffset,
657 						      images[i], 0);
658 		if (node < 0) {
659 			debug("No image: %s\n", images[i]);
660 			continue;
661 		}
662 
663 		ret = spl_load_fit_image(info, sector, fit, base_offset,
664 					 node, &image_info);
665 		if (ret)
666 			return ret;
667 
668 		/* initial addr or entry point */
669 		if (!strcmp(images[i], FIT_FDT_PROP)) {
670 			if (spl_fdt_chosen_bootargs(info, (void *)image_info.load_addr)) {
671 				printf("ERROR: Append bootargs failed\n");
672 				return -EINVAL;
673 			}
674 		} else if (!strcmp(images[i], FIT_KERNEL_PROP)) {
675 #if CONFIG_IS_ENABLED(OPTEE)
676 			spl_image->entry_point_os = image_info.load_addr;
677 #endif
678 #if CONFIG_IS_ENABLED(ATF)
679 			spl_image->entry_point_bl33 = image_info.load_addr;
680 #endif
681 		} else if (!strcmp(images[i], FIT_RAMDISK_PROP)) {
682 			fdt_initrd(spl_image->fdt_addr, image_info.load_addr,
683 				   image_info.load_addr + image_info.size);
684 		}
685 	}
686 
687 	debug("fdt_addr=0x%08lx, entry_point=0x%08lx, entry_point_os=0x%08lx\n",
688 	      (ulong)spl_image->fdt_addr,
689 	      spl_image->entry_point,
690 #if CONFIG_IS_ENABLED(OPTEE)
691 	      spl_image->entry_point_os);
692 #endif
693 #if CONFIG_IS_ENABLED(ATF)
694 	      spl_image->entry_point_bl33);
695 #endif
696 
697 	return 0;
698 }
699 #endif
700 
701 static int spl_internal_load_simple_fit(struct spl_image_info *spl_image,
702 					struct spl_load_info *info,
703 					ulong sector, void *fit_header)
704 {
705 	struct spl_image_info image_info;
706 	char *desc;
707 #if CONFIG_IS_ENABLED(ATF)
708 	uint8_t ih_arch;
709 #endif
710 	int base_offset;
711 	int images, ret;
712 	int index = 0;
713 	int node = -1;
714 	void *fit;
715 
716 	fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
717 	if (!fit) {
718 		debug("%s: Cannot load blob\n", __func__);
719 		return -1;
720 	}
721 
722 	/* find the node holding the images information */
723 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
724 	if (images < 0) {
725 		debug("%s: Cannot find /images node: %d\n", __func__, images);
726 		return -1;
727 	}
728 
729 	/* if board sigs verify required, check self */
730 	if (fit_board_verify_required_sigs() &&
731 	    !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
732 		printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n");
733 		hang();
734 	}
735 
736 	/* verify the configure node by keys, if required */
737 #ifdef CONFIG_SPL_FIT_SIGNATURE
738 	int conf_noffset;
739 
740 	conf_noffset = fit_conf_get_node(fit, NULL);
741 	if (conf_noffset <= 0) {
742 		printf("No default config node\n");
743 		return -EINVAL;
744 	}
745 
746 	ret = fit_config_verify(fit, conf_noffset);
747 	if (ret) {
748 		printf("fit verify configure failed, ret=%d\n", ret);
749 		return ret;
750 	}
751 	printf("\n");
752 
753 #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT
754 	uint32_t this_index, min_index;
755 
756 	ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL,
757 					&this_index, &min_index);
758 	if (ret) {
759 		printf("fit failed to get rollback index, ret=%d\n", ret);
760 		return ret;
761 	} else if (this_index < min_index) {
762 		printf("fit reject rollback: %d < %d(min)\n",
763 		       this_index, min_index);
764 		return -EINVAL;
765 	}
766 
767 	printf("rollback index: %d >= %d(min), OK\n", this_index, min_index);
768 #endif
769 #endif
770 
771 	/*
772 	 * If required to start the other core before load "loadables"
773 	 * firmwares, use the config "standalone" to load the other core's
774 	 * firmware, then start it.
775 	 * Normally, different cores' firmware is attach to the config
776 	 * "loadables" and load them together.
777 	 */
778 	for (; ; index++) {
779 		node = spl_fit_get_image_node(fit, images,
780 					      FIT_STANDALONE_PROP, index);
781 		if (node < 0)
782 			break;
783 
784 		ret = spl_load_fit_image(info, sector, fit, base_offset,
785 					 node, &image_info);
786 		if (ret)
787 			return ret;
788 
789 		ret = fit_get_desc(fit, node, &desc);
790 		if (ret)
791 			return ret;
792 
793 		if (image_info.entry_point == FDT_ERROR)
794 			image_info.entry_point = image_info.load_addr;
795 
796 		flush_dcache_range(image_info.load_addr,
797 				   image_info.load_addr + image_info.size);
798 		ret = spl_fit_standalone_release(desc, image_info.entry_point);
799 		if (ret)
800 			printf("%s: start standalone fail, ret=%d\n", desc, ret);
801 	}
802 
803 	/* standalone is special one, continue to find others */
804 	node = -1;
805 	index = 0;
806 
807 	/*
808 	 * Find the U-Boot image using the following search order:
809 	 *   - start at 'firmware' (e.g. an ARM Trusted Firmware)
810 	 *   - fall back 'kernel' (e.g. a Falcon-mode OS boot
811 	 *   - fall back to using the first 'loadables' entry
812 	 */
813 	if (node < 0)
814 		node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
815 					      0);
816 #ifdef CONFIG_SPL_OS_BOOT
817 	if (node < 0)
818 		node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
819 #endif
820 	if (node < 0) {
821 		debug("could not find firmware image, trying loadables...\n");
822 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
823 		/*
824 		 * If we pick the U-Boot image from "loadables", start at
825 		 * the second image when later loading additional images.
826 		 */
827 		index = 1;
828 	}
829 	if (node < 0) {
830 		debug("%s: Cannot find u-boot image node: %d\n",
831 		      __func__, node);
832 		return -1;
833 	}
834 
835 	/* Load the image and set up the spl_image structure */
836 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
837 				 spl_image);
838 	if (ret)
839 		return ret;
840 
841 	/*
842 	 * For backward compatibility, we treat the first node that is
843 	 * as a U-Boot image, if no OS-type has been declared.
844 	 */
845 	if (!spl_fit_image_get_os(fit, node, &spl_image->os))
846 		debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
847 #if !defined(CONFIG_SPL_OS_BOOT)
848 	else
849 		spl_image->os = IH_OS_U_BOOT;
850 #endif
851 
852 	/* Booting a next-stage U-Boot may require us to append the FDT. */
853 	if (spl_image->os == IH_OS_U_BOOT) {
854 		ret = spl_fit_append_fdt(spl_image, info, sector, fit,
855 					 images, base_offset);
856 		if (ret < 0)
857 			return ret;
858 	}
859 
860 	/* Now check if there are more images for us to load */
861 	for (; ; index++) {
862 		uint8_t os_type = IH_OS_INVALID;
863 
864 		node = spl_fit_get_image_node(fit, images, "loadables", index);
865 		if (node < 0)
866 			break;
867 
868 		if (!spl_fit_image_get_os(fit, node, &os_type))
869 			debug("Loadable is %s\n", genimg_get_os_name(os_type));
870 
871 		/* skip U-Boot ? */
872 		if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL &&
873 		    os_type == IH_OS_U_BOOT)
874 		    continue;
875 
876 		ret = spl_load_fit_image(info, sector, fit, base_offset, node,
877 					 &image_info);
878 		if (ret < 0)
879 			return ret;
880 
881 		if (os_type == IH_OS_U_BOOT) {
882 #if CONFIG_IS_ENABLED(ATF)
883 			fit_image_get_arch(fit, node, &ih_arch);
884 			debug("Image ARCH is %s\n", genimg_get_arch_name(ih_arch));
885 			if (ih_arch == IH_ARCH_ARM)
886 				spl_image->flags |= SPL_ATF_AARCH32_BL33;
887 			spl_image->entry_point_bl33 = image_info.load_addr;
888 #elif CONFIG_IS_ENABLED(OPTEE)
889 			spl_image->entry_point_os = image_info.load_addr;
890 #endif
891 			ret = spl_fit_append_fdt(&image_info, info, sector,
892 						 fit, images, base_offset);
893 			if (ret < 0)
894 				return ret;
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 (spl_image->fdt_addr && spl_image->next_stage == SPL_NEXT_STAGE_UBOOT)
908 			spl_fit_record_loadable(fit, images, index,
909 						spl_image->fdt_addr,
910 						&image_info);
911 #if CONFIG_IS_ENABLED(ATF)
912 		else if (os_type == IH_OS_OP_TEE)
913 			spl_image->entry_point_bl32 = image_info.load_addr;
914 #endif
915 	}
916 
917 	/*
918 	 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
919 	 * Makefile will set it to 0 and it will end up as the entry point
920 	 * here. What it actually means is: use the load address.
921 	 */
922 	if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
923 		spl_image->entry_point = spl_image->load_addr;
924 
925 	return 0;
926 }
927 
928 int spl_load_simple_fit(struct spl_image_info *spl_image,
929 			struct spl_load_info *info, ulong sector, void *fit)
930 {
931 	ulong sector_offs = sector;
932 	int ret = -EINVAL;
933 	int i;
934 
935 #ifdef CONFIG_MP_BOOT
936 	mpb_init_1(*info);
937 #endif
938 
939 	printf("Trying fit image at 0x%lx sector\n", sector_offs);
940 	for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) {
941 		if (i > 0) {
942 			sector_offs +=
943 			   i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len);
944 			printf("Trying fit image at 0x%lx sector\n", sector_offs);
945 			if (info->read(info, sector_offs, 1, fit) != 1) {
946 				printf("IO error\n");
947 				continue;
948 			}
949 		}
950 
951 		if (image_get_magic(fit) != FDT_MAGIC) {
952 			printf("Not fit magic\n");
953 			continue;
954 		}
955 
956 		ret = spl_internal_load_simple_fit(spl_image, info,
957 						   sector_offs, fit);
958 		if (!ret) {
959 #ifdef CONFIG_SPL_KERNEL_BOOT
960 			ret = spl_load_kernel_fit(spl_image, info);
961 #endif
962 			break;
963 		}
964 	}
965 #ifdef CONFIG_SPL_AB
966 	/* If boot fail in spl, spl must decrease 1 and do_reset. */
967 	if (ret)
968 		return spl_ab_decrease_reset(info->dev);
969 	/*
970 	 * If boot successfully, it is no need to do decrease
971 	 * and U-boot will always decrease 1.
972 	 * If in thunderboot process, always need to decrease 1.
973 	 */
974 	if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL)
975 		spl_ab_decrease_tries(info->dev);
976 #endif
977 
978 	return ret;
979 }
980 
981