xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision dc66eaaf753bf604452926b863f0647c75be148e)
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 	board_fit_image_post_process(fit, node, (ulong *)&load_addr,
370 				     (ulong **)&src, &length, info);
371 #endif
372 	puts("OK\n");
373 
374 	if (IS_ENABLED(CONFIG_SPL_OS_BOOT)	&&
375 	    IS_ENABLED(CONFIG_SPL_GZIP)		&&
376 	    image_comp == IH_COMP_GZIP		&&
377 	    type == IH_TYPE_KERNEL) {
378 		size = length;
379 		if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
380 			   src, &size)) {
381 			puts("Uncompressing error\n");
382 			return -EIO;
383 		}
384 		length = size;
385 	} else {
386 		memcpy((void *)load_addr, src, length);
387 	}
388 
389 	if (image_info) {
390 		image_info->load_addr = load_addr;
391 		image_info->size = length;
392 		image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
393 	}
394 
395 	return 0;
396 }
397 
398 static int spl_fit_append_fdt(struct spl_image_info *spl_image,
399 			      struct spl_load_info *info, ulong sector,
400 			      void *fit, int images, ulong base_offset)
401 {
402 	struct spl_image_info image_info;
403 	int node, ret;
404 
405 	/* Figure out which device tree the board wants to use */
406 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
407 	if (node < 0) {
408 		debug("%s: cannot find FDT node\n", __func__);
409 		return node;
410 	}
411 
412 	/*
413 	 * Read the device tree and place it after the image.
414 	 * Align the destination address to ARCH_DMA_MINALIGN.
415 	 */
416 	image_info.load_addr = spl_image->load_addr + spl_image->size;
417 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
418 				 &image_info);
419 
420 	if (ret < 0)
421 		return ret;
422 
423 	/* Make the load-address of the FDT available for the SPL framework */
424 	spl_image->fdt_addr = (void *)image_info.load_addr;
425 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
426 	/* Try to make space, so we can inject details on the loadables */
427 	fdt_shrink_to_minimum(spl_image->fdt_addr, 8192);
428 #endif
429 
430 	/*
431 	 * If need, load kernel FDT right after U-Boot FDT.
432 	 *
433 	 * kernel FDT is for U-Boot if there is not valid one
434 	 * from images, ie: resource.img, boot.img or recovery.img.
435 	 */
436 	node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 1);
437 	if (node < 0) {
438 		debug("%s: cannot find kernel FDT node\n", __func__);
439 		/* attention: here return ret but not node */
440 		return ret;
441 	}
442 
443 	image_info.load_addr =
444 		(ulong)spl_image->fdt_addr + fdt_totalsize(spl_image->fdt_addr);
445 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
446 				 &image_info);
447 
448 	return ret;
449 }
450 
451 static int spl_fit_record_loadable(const void *fit, int images, int index,
452 				   void *blob, struct spl_image_info *image)
453 {
454 	int ret = 0;
455 #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
456 	char *name;
457 	int node;
458 
459 	ret = spl_fit_get_image_name(fit, images, "loadables",
460 				     index, &name);
461 	if (ret < 0)
462 		return ret;
463 
464 	node = spl_fit_get_image_node(fit, images, "loadables", index);
465 
466 	ret = fdt_record_loadable(blob, index, name, image->load_addr,
467 				  image->size, image->entry_point,
468 				  fdt_getprop(fit, node, "type", NULL),
469 				  fdt_getprop(fit, node, "os", NULL));
470 #endif
471 	return ret;
472 }
473 
474 static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os)
475 {
476 #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY)
477 	return -ENOTSUPP;
478 #else
479 	return fit_image_get_os(fit, noffset, os);
480 #endif
481 }
482 
483 __weak int spl_fit_standalone_release(char *id, uintptr_t entry_point)
484 {
485 	return 0;
486 }
487 
488 static void *spl_fit_load_blob(struct spl_load_info *info,
489 			       ulong sector, void *fit_header,
490 			       int *base_offset)
491 {
492 	int align_len = ARCH_DMA_MINALIGN - 1;
493 	ulong count;
494 	ulong size;
495 	int sectors;
496 	void *fit;
497 
498 	/*
499 	 * For FIT with external data, figure out where the external images
500 	 * start. This is the base for the data-offset properties in each
501 	 * image.
502 	 */
503 	size = fdt_totalsize(fit_header);
504 	size = FIT_ALIGN(size);
505 	*base_offset = FIT_ALIGN(size);
506 
507 	/*
508 	 * So far we only have one block of data from the FIT. Read the entire
509 	 * thing, including that first block, placing it so it finishes before
510 	 * where we will load the image.
511 	 *
512 	 * Note that we will load the image such that its first byte will be
513 	 * at the load address. Since that byte may be part-way through a
514 	 * block, we may load the image up to one block before the load
515 	 * address. So take account of that here by subtracting an addition
516 	 * block length from the FIT start position.
517 	 *
518 	 * In fact the FIT has its own load address, but we assume it cannot
519 	 * be before CONFIG_SYS_TEXT_BASE.
520 	 *
521 	 * For FIT with data embedded, data is loaded as part of FIT image.
522 	 * For FIT with external data, data is not loaded in this step.
523 	 */
524 	fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
525 			align_len) & ~align_len);
526 	sectors = get_aligned_image_size(info, size, 0);
527 	count = info->read(info, sector, sectors, fit);
528 #if defined(CONFIG_SPL_MTD_SUPPORT) && !defined(CONFIG_FPGA_RAM)
529 	mtd_blk_map_fit(info->dev, sector, fit);
530 #endif
531 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
532 	      sector, sectors, fit, count);
533 	if (count == 0)
534 		return NULL;
535 
536 	return fit;
537 }
538 
539 #ifdef CONFIG_SPL_KERNEL_BOOT
540 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
541 __weak const char *spl_kernel_partition(struct spl_image_info *spl,
542 					struct spl_load_info *info)
543 {
544 	return PART_BOOT;
545 }
546 #endif
547 
548 static int spl_fit_get_kernel_dtb(const void *fit, int images_noffset)
549 {
550 	const char *name = NULL;
551 	int node, index = 0;
552 
553 	for (; ; index++) {
554 		node = spl_fit_get_image_node(fit, images_noffset,
555 					      FIT_FDT_PROP, index);
556 		if (node < 0)
557 			break;
558 		name = fdt_get_name(fit, node, NULL);
559 		if(!strcmp(name, "fdt"))
560 			return node;
561 #if defined(CONFIG_SPL_ROCKCHIP_HWID_DTB)
562 		if (spl_find_hwid_dtb(name)) {
563 			printf("HWID DTB: %s\n", name);
564 			break;
565 		}
566 #endif
567 	}
568 
569 	return node;
570 }
571 
572 static int spl_load_kernel_fit(struct spl_image_info *spl_image,
573 			       struct spl_load_info *info)
574 {
575 	/*
576 	 * Never change the image order.
577 	 *
578 	 * Considering thunder-boot feature, there maybe asynchronous
579 	 * loading operation of these images and ramdisk is usually to
580 	 * be the last one.
581 	 *
582 	 * The .its content rule of kernel fit image follows U-Boot proper.
583 	 */
584 	const char *images[] = { FIT_FDT_PROP, FIT_KERNEL_PROP, FIT_RAMDISK_PROP, };
585 	struct spl_image_info image_info;
586 	char fit_header[info->bl_len];
587 	int images_noffset;
588 	int base_offset;
589 	int sector;
590 	int node, ret, i;
591 	void *fit;
592 
593 	if (spl_image->next_stage != SPL_NEXT_STAGE_KERNEL)
594 		return 0;
595 
596 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
597 	const char *part_name = PART_BOOT;
598 	disk_partition_t part_info;
599 
600 	part_name = spl_kernel_partition(spl_image, info);
601 	if (part_get_info_by_name(info->dev, part_name, &part_info) <= 0) {
602 		printf("%s: no partition\n", __func__);
603 		return -EINVAL;
604 	}
605 	sector = part_info.start;
606 #else
607 	sector = CONFIG_SPL_KERNEL_BOOT_SECTOR;
608 #endif
609 	printf("Trying kernel at 0x%x sector from '%s' part\n", sector, part_name);
610 
611 	if (info->read(info, sector, 1, &fit_header) != 1) {
612 		debug("%s: Failed to read header\n", __func__);
613 		return -EIO;
614 	}
615 
616 	if (image_get_magic((void *)&fit_header) != FDT_MAGIC) {
617 		printf("%s: Not fit magic\n", __func__);
618 		return -EINVAL;
619 	}
620 
621 	fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
622 	if (!fit) {
623 		debug("%s: Cannot load blob\n", __func__);
624 		return -ENODEV;
625 	}
626 
627 	/* verify the configure node by keys, if required */
628 #ifdef CONFIG_SPL_FIT_SIGNATURE
629 	int conf_noffset;
630 
631 	conf_noffset = fit_conf_get_node(fit, NULL);
632 	if (conf_noffset <= 0) {
633 		printf("No default config node\n");
634 		return -EINVAL;
635 	}
636 
637 	ret = fit_config_verify(fit, conf_noffset);
638 	if (ret) {
639 		printf("fit verify configure failed, ret=%d\n", ret);
640 		return ret;
641 	}
642 	printf("\n");
643 #endif
644 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
645 	if (images_noffset < 0) {
646 		debug("%s: Cannot find /images node: %d\n",
647 		      __func__, images_noffset);
648 		return images_noffset;
649 	}
650 
651 	for (i = 0; i < ARRAY_SIZE(images); i++) {
652 		if (!strcmp(images[i], FIT_FDT_PROP))
653 			node = spl_fit_get_kernel_dtb(fit, images_noffset);
654 		else
655 			node = spl_fit_get_image_node(fit, images_noffset,
656 						      images[i], 0);
657 		if (node < 0) {
658 			debug("No image: %s\n", images[i]);
659 			continue;
660 		}
661 
662 		ret = spl_load_fit_image(info, sector, fit, base_offset,
663 					 node, &image_info);
664 		if (ret)
665 			return ret;
666 
667 		/* initial addr or entry point */
668 		if (!strcmp(images[i], FIT_FDT_PROP)) {
669 			spl_image->fdt_addr = (void *)image_info.load_addr;
670 #ifdef CONFIG_SPL_AB
671 			char slot_suffix[3] = {0};
672 
673 			if (!spl_get_current_slot(info->dev, "misc", slot_suffix))
674 				spl_ab_bootargs_append_slot((void *)image_info.load_addr, slot_suffix);
675 #endif
676 
677 #ifdef CONFIG_SPL_MTD_SUPPORT
678 			struct blk_desc *desc = info->dev;
679 
680 			if (desc->devnum == BLK_MTD_SPI_NAND)
681 				fdt_bootargs_append((void *)image_info.load_addr, mtd_part_parse(desc));
682 #endif
683 		} else if (!strcmp(images[i], FIT_KERNEL_PROP)) {
684 #if CONFIG_IS_ENABLED(OPTEE)
685 			spl_image->entry_point_os = image_info.load_addr;
686 #endif
687 #if CONFIG_IS_ENABLED(ATF)
688 			spl_image->entry_point_bl33 = image_info.load_addr;
689 #endif
690 		} else if (!strcmp(images[i], FIT_RAMDISK_PROP)) {
691 			fdt_initrd(spl_image->fdt_addr, image_info.load_addr,
692 				   image_info.load_addr + image_info.size);
693 		}
694 	}
695 
696 	debug("fdt_addr=0x%08lx, entry_point=0x%08lx, entry_point_os=0x%08lx\n",
697 	      (ulong)spl_image->fdt_addr,
698 	      spl_image->entry_point,
699 #if CONFIG_IS_ENABLED(OPTEE)
700 	      spl_image->entry_point_os);
701 #endif
702 #if CONFIG_IS_ENABLED(ATF)
703 	      spl_image->entry_point_bl33);
704 #endif
705 
706 	return 0;
707 }
708 #endif
709 
710 static int spl_internal_load_simple_fit(struct spl_image_info *spl_image,
711 					struct spl_load_info *info,
712 					ulong sector, void *fit_header)
713 {
714 	struct spl_image_info image_info;
715 	char *desc;
716 #if CONFIG_IS_ENABLED(ATF)
717 	uint8_t ih_arch;
718 #endif
719 	int base_offset;
720 	int images, ret;
721 	int index = 0;
722 	int node = -1;
723 	void *fit;
724 
725 	fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
726 	if (!fit) {
727 		debug("%s: Cannot load blob\n", __func__);
728 		return -1;
729 	}
730 
731 	/* find the node holding the images information */
732 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
733 	if (images < 0) {
734 		debug("%s: Cannot find /images node: %d\n", __func__, images);
735 		return -1;
736 	}
737 
738 	/* if board sigs verify required, check self */
739 	if (fit_board_verify_required_sigs() &&
740 	    !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
741 		printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n");
742 		hang();
743 	}
744 
745 	/* verify the configure node by keys, if required */
746 #ifdef CONFIG_SPL_FIT_SIGNATURE
747 	int conf_noffset;
748 
749 	conf_noffset = fit_conf_get_node(fit, NULL);
750 	if (conf_noffset <= 0) {
751 		printf("No default config node\n");
752 		return -EINVAL;
753 	}
754 
755 	ret = fit_config_verify(fit, conf_noffset);
756 	if (ret) {
757 		printf("fit verify configure failed, ret=%d\n", ret);
758 		return ret;
759 	}
760 	printf("\n");
761 
762 #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT
763 	uint32_t this_index, min_index;
764 
765 	ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL,
766 					&this_index, &min_index);
767 	if (ret) {
768 		printf("fit failed to get rollback index, ret=%d\n", ret);
769 		return ret;
770 	} else if (this_index < min_index) {
771 		printf("fit reject rollback: %d < %d(min)\n",
772 		       this_index, min_index);
773 		return -EINVAL;
774 	}
775 
776 	printf("rollback index: %d >= %d(min), OK\n", this_index, min_index);
777 #endif
778 #endif
779 
780 	/*
781 	 * If required to start the other core before load "loadables"
782 	 * firmwares, use the config "standalone" to load the other core's
783 	 * firmware, then start it.
784 	 * Normally, different cores' firmware is attach to the config
785 	 * "loadables" and load them together.
786 	 */
787 	for (; ; index++) {
788 		node = spl_fit_get_image_node(fit, images,
789 					      FIT_STANDALONE_PROP, index);
790 		if (node < 0)
791 			break;
792 
793 		ret = spl_load_fit_image(info, sector, fit, base_offset,
794 					 node, &image_info);
795 		if (ret)
796 			return ret;
797 
798 		ret = fit_get_desc(fit, node, &desc);
799 		if (ret)
800 			return ret;
801 
802 		if (image_info.entry_point == FDT_ERROR)
803 			image_info.entry_point = image_info.load_addr;
804 
805 		flush_dcache_range(image_info.load_addr,
806 				   image_info.load_addr + image_info.size);
807 		ret = spl_fit_standalone_release(desc, image_info.entry_point);
808 		if (ret)
809 			printf("%s: start standalone fail, ret=%d\n", desc, ret);
810 	}
811 
812 	/* standalone is special one, continue to find others */
813 	node = -1;
814 	index = 0;
815 
816 	/*
817 	 * Find the U-Boot image using the following search order:
818 	 *   - start at 'firmware' (e.g. an ARM Trusted Firmware)
819 	 *   - fall back 'kernel' (e.g. a Falcon-mode OS boot
820 	 *   - fall back to using the first 'loadables' entry
821 	 */
822 	if (node < 0)
823 		node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
824 					      0);
825 #ifdef CONFIG_SPL_OS_BOOT
826 	if (node < 0)
827 		node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
828 #endif
829 	if (node < 0) {
830 		debug("could not find firmware image, trying loadables...\n");
831 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
832 		/*
833 		 * If we pick the U-Boot image from "loadables", start at
834 		 * the second image when later loading additional images.
835 		 */
836 		index = 1;
837 	}
838 	if (node < 0) {
839 		debug("%s: Cannot find u-boot image node: %d\n",
840 		      __func__, node);
841 		return -1;
842 	}
843 
844 	/* Load the image and set up the spl_image structure */
845 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
846 				 spl_image);
847 	if (ret)
848 		return ret;
849 
850 	/*
851 	 * For backward compatibility, we treat the first node that is
852 	 * as a U-Boot image, if no OS-type has been declared.
853 	 */
854 	if (!spl_fit_image_get_os(fit, node, &spl_image->os))
855 		debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
856 #if !defined(CONFIG_SPL_OS_BOOT)
857 	else
858 		spl_image->os = IH_OS_U_BOOT;
859 #endif
860 
861 	/* Booting a next-stage U-Boot may require us to append the FDT. */
862 	if (spl_image->os == IH_OS_U_BOOT) {
863 		ret = spl_fit_append_fdt(spl_image, info, sector, fit,
864 					 images, base_offset);
865 		if (ret < 0)
866 			return ret;
867 	}
868 
869 	/* Now check if there are more images for us to load */
870 	for (; ; index++) {
871 		uint8_t os_type = IH_OS_INVALID;
872 
873 		node = spl_fit_get_image_node(fit, images, "loadables", index);
874 		if (node < 0)
875 			break;
876 
877 		if (!spl_fit_image_get_os(fit, node, &os_type))
878 			debug("Loadable is %s\n", genimg_get_os_name(os_type));
879 
880 		/* skip U-Boot ? */
881 		if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL &&
882 		    os_type == IH_OS_U_BOOT)
883 		    continue;
884 
885 		ret = spl_load_fit_image(info, sector, fit, base_offset, node,
886 					 &image_info);
887 		if (ret < 0)
888 			return ret;
889 
890 		if (os_type == IH_OS_U_BOOT) {
891 #if CONFIG_IS_ENABLED(ATF)
892 			fit_image_get_arch(fit, node, &ih_arch);
893 			debug("Image ARCH is %s\n", genimg_get_arch_name(ih_arch));
894 			if (ih_arch == IH_ARCH_ARM)
895 				spl_image->flags |= SPL_ATF_AARCH32_BL33;
896 			spl_image->entry_point_bl33 = image_info.load_addr;
897 #elif CONFIG_IS_ENABLED(OPTEE)
898 			spl_image->entry_point_os = image_info.load_addr;
899 #endif
900 			ret = spl_fit_append_fdt(&image_info, info, sector,
901 						 fit, images, base_offset);
902 			if (ret < 0)
903 				return ret;
904 			spl_image->fdt_addr = image_info.fdt_addr;
905 		}
906 
907 		/*
908 		 * If the "firmware" image did not provide an entry point,
909 		 * use the first valid entry point from the loadables.
910 		 */
911 		if (spl_image->entry_point == FDT_ERROR &&
912 		    image_info.entry_point != FDT_ERROR)
913 			spl_image->entry_point = image_info.entry_point;
914 
915 		/* Record our loadables into the FDT */
916 		if (spl_image->fdt_addr && spl_image->next_stage == SPL_NEXT_STAGE_UBOOT)
917 			spl_fit_record_loadable(fit, images, index,
918 						spl_image->fdt_addr,
919 						&image_info);
920 #if CONFIG_IS_ENABLED(ATF)
921 		else if (os_type == IH_OS_OP_TEE)
922 			spl_image->entry_point_bl32 = image_info.load_addr;
923 #endif
924 	}
925 
926 	/*
927 	 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
928 	 * Makefile will set it to 0 and it will end up as the entry point
929 	 * here. What it actually means is: use the load address.
930 	 */
931 	if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
932 		spl_image->entry_point = spl_image->load_addr;
933 
934 	return 0;
935 }
936 
937 int spl_load_simple_fit(struct spl_image_info *spl_image,
938 			struct spl_load_info *info, ulong sector, void *fit)
939 {
940 	ulong sector_offs = sector;
941 	int ret = -EINVAL;
942 	int i;
943 
944 #ifdef CONFIG_MP_BOOT
945 	mpb_init_1(*info);
946 #endif
947 
948 	printf("Trying fit image at 0x%lx sector\n", sector_offs);
949 	for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) {
950 		if (i > 0) {
951 			sector_offs +=
952 			   i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len);
953 			printf("Trying fit image at 0x%lx sector\n", sector_offs);
954 			if (info->read(info, sector_offs, 1, fit) != 1) {
955 				printf("IO error\n");
956 				continue;
957 			}
958 		}
959 
960 		if (image_get_magic(fit) != FDT_MAGIC) {
961 			printf("Not fit magic\n");
962 			continue;
963 		}
964 
965 		ret = spl_internal_load_simple_fit(spl_image, info,
966 						   sector_offs, fit);
967 		if (!ret) {
968 #ifdef CONFIG_SPL_KERNEL_BOOT
969 			ret = spl_load_kernel_fit(spl_image, info);
970 #endif
971 			break;
972 		}
973 	}
974 #ifdef CONFIG_SPL_AB
975 	/* If boot fail in spl, spl must decrease 1 and do_reset. */
976 	if (ret)
977 		return spl_ab_decrease_reset(info->dev);
978 	/*
979 	 * If boot successfully, it is no need to do decrease
980 	 * and U-boot will always decrease 1.
981 	 * If in thunderboot process, always need to decrease 1.
982 	 */
983 	if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL)
984 		spl_ab_decrease_tries(info->dev);
985 #endif
986 
987 	return ret;
988 }
989 
990