xref: /rk3399_rockchip-uboot/common/spl/spl_fit.c (revision f779c0ec41124e0777fa1fe97bed9dad769e0c50)
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 			spl_image->fdt_addr = (void *)image_info.load_addr;
671 #ifdef CONFIG_SPL_AB
672 			char slot_suffix[3] = {0};
673 
674 			if (!spl_get_current_slot(info->dev, "misc", slot_suffix))
675 				spl_ab_bootargs_append_slot((void *)image_info.load_addr, slot_suffix);
676 #endif
677 
678 #ifdef CONFIG_SPL_MTD_SUPPORT
679 			struct blk_desc *desc = info->dev;
680 
681 			if (desc->devnum == BLK_MTD_SPI_NAND)
682 				fdt_bootargs_append((void *)image_info.load_addr, mtd_part_parse(desc));
683 #endif
684 		} else if (!strcmp(images[i], FIT_KERNEL_PROP)) {
685 #if CONFIG_IS_ENABLED(OPTEE)
686 			spl_image->entry_point_os = image_info.load_addr;
687 #endif
688 #if CONFIG_IS_ENABLED(ATF)
689 			spl_image->entry_point_bl33 = image_info.load_addr;
690 #endif
691 		} else if (!strcmp(images[i], FIT_RAMDISK_PROP)) {
692 			fdt_initrd(spl_image->fdt_addr, image_info.load_addr,
693 				   image_info.load_addr + image_info.size);
694 		}
695 	}
696 
697 	debug("fdt_addr=0x%08lx, entry_point=0x%08lx, entry_point_os=0x%08lx\n",
698 	      (ulong)spl_image->fdt_addr,
699 	      spl_image->entry_point,
700 #if CONFIG_IS_ENABLED(OPTEE)
701 	      spl_image->entry_point_os);
702 #endif
703 #if CONFIG_IS_ENABLED(ATF)
704 	      spl_image->entry_point_bl33);
705 #endif
706 
707 	return 0;
708 }
709 #endif
710 
711 static int spl_internal_load_simple_fit(struct spl_image_info *spl_image,
712 					struct spl_load_info *info,
713 					ulong sector, void *fit_header)
714 {
715 	struct spl_image_info image_info;
716 	char *desc;
717 #if CONFIG_IS_ENABLED(ATF)
718 	uint8_t ih_arch;
719 #endif
720 	int base_offset;
721 	int images, ret;
722 	int index = 0;
723 	int node = -1;
724 	void *fit;
725 
726 	fit = spl_fit_load_blob(info, sector, fit_header, &base_offset);
727 	if (!fit) {
728 		debug("%s: Cannot load blob\n", __func__);
729 		return -1;
730 	}
731 
732 	/* find the node holding the images information */
733 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
734 	if (images < 0) {
735 		debug("%s: Cannot find /images node: %d\n", __func__, images);
736 		return -1;
737 	}
738 
739 	/* if board sigs verify required, check self */
740 	if (fit_board_verify_required_sigs() &&
741 	    !IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) {
742 		printf("Verified-boot requires CONFIG_SPL_FIT_SIGNATURE enabled\n");
743 		hang();
744 	}
745 
746 	/* verify the configure node by keys, if required */
747 #ifdef CONFIG_SPL_FIT_SIGNATURE
748 	int conf_noffset;
749 
750 	conf_noffset = fit_conf_get_node(fit, NULL);
751 	if (conf_noffset <= 0) {
752 		printf("No default config node\n");
753 		return -EINVAL;
754 	}
755 
756 	ret = fit_config_verify(fit, conf_noffset);
757 	if (ret) {
758 		printf("fit verify configure failed, ret=%d\n", ret);
759 		return ret;
760 	}
761 	printf("\n");
762 
763 #ifdef CONFIG_SPL_FIT_ROLLBACK_PROTECT
764 	uint32_t this_index, min_index;
765 
766 	ret = fit_rollback_index_verify(fit, FIT_ROLLBACK_INDEX_SPL,
767 					&this_index, &min_index);
768 	if (ret) {
769 		printf("fit failed to get rollback index, ret=%d\n", ret);
770 		return ret;
771 	} else if (this_index < min_index) {
772 		printf("fit reject rollback: %d < %d(min)\n",
773 		       this_index, min_index);
774 		return -EINVAL;
775 	}
776 
777 	printf("rollback index: %d >= %d(min), OK\n", this_index, min_index);
778 #endif
779 #endif
780 
781 	/*
782 	 * If required to start the other core before load "loadables"
783 	 * firmwares, use the config "standalone" to load the other core's
784 	 * firmware, then start it.
785 	 * Normally, different cores' firmware is attach to the config
786 	 * "loadables" and load them together.
787 	 */
788 	for (; ; index++) {
789 		node = spl_fit_get_image_node(fit, images,
790 					      FIT_STANDALONE_PROP, index);
791 		if (node < 0)
792 			break;
793 
794 		ret = spl_load_fit_image(info, sector, fit, base_offset,
795 					 node, &image_info);
796 		if (ret)
797 			return ret;
798 
799 		ret = fit_get_desc(fit, node, &desc);
800 		if (ret)
801 			return ret;
802 
803 		if (image_info.entry_point == FDT_ERROR)
804 			image_info.entry_point = image_info.load_addr;
805 
806 		flush_dcache_range(image_info.load_addr,
807 				   image_info.load_addr + image_info.size);
808 		ret = spl_fit_standalone_release(desc, image_info.entry_point);
809 		if (ret)
810 			printf("%s: start standalone fail, ret=%d\n", desc, ret);
811 	}
812 
813 	/* standalone is special one, continue to find others */
814 	node = -1;
815 	index = 0;
816 
817 	/*
818 	 * Find the U-Boot image using the following search order:
819 	 *   - start at 'firmware' (e.g. an ARM Trusted Firmware)
820 	 *   - fall back 'kernel' (e.g. a Falcon-mode OS boot
821 	 *   - fall back to using the first 'loadables' entry
822 	 */
823 	if (node < 0)
824 		node = spl_fit_get_image_node(fit, images, FIT_FIRMWARE_PROP,
825 					      0);
826 #ifdef CONFIG_SPL_OS_BOOT
827 	if (node < 0)
828 		node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
829 #endif
830 	if (node < 0) {
831 		debug("could not find firmware image, trying loadables...\n");
832 		node = spl_fit_get_image_node(fit, images, "loadables", 0);
833 		/*
834 		 * If we pick the U-Boot image from "loadables", start at
835 		 * the second image when later loading additional images.
836 		 */
837 		index = 1;
838 	}
839 	if (node < 0) {
840 		debug("%s: Cannot find u-boot image node: %d\n",
841 		      __func__, node);
842 		return -1;
843 	}
844 
845 	/* Load the image and set up the spl_image structure */
846 	ret = spl_load_fit_image(info, sector, fit, base_offset, node,
847 				 spl_image);
848 	if (ret)
849 		return ret;
850 
851 	/*
852 	 * For backward compatibility, we treat the first node that is
853 	 * as a U-Boot image, if no OS-type has been declared.
854 	 */
855 	if (!spl_fit_image_get_os(fit, node, &spl_image->os))
856 		debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
857 #if !defined(CONFIG_SPL_OS_BOOT)
858 	else
859 		spl_image->os = IH_OS_U_BOOT;
860 #endif
861 
862 	/* Booting a next-stage U-Boot may require us to append the FDT. */
863 	if (spl_image->os == IH_OS_U_BOOT) {
864 		ret = spl_fit_append_fdt(spl_image, info, sector, fit,
865 					 images, base_offset);
866 		if (ret < 0)
867 			return ret;
868 	}
869 
870 	/* Now check if there are more images for us to load */
871 	for (; ; index++) {
872 		uint8_t os_type = IH_OS_INVALID;
873 
874 		node = spl_fit_get_image_node(fit, images, "loadables", index);
875 		if (node < 0)
876 			break;
877 
878 		if (!spl_fit_image_get_os(fit, node, &os_type))
879 			debug("Loadable is %s\n", genimg_get_os_name(os_type));
880 
881 		/* skip U-Boot ? */
882 		if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL &&
883 		    os_type == IH_OS_U_BOOT)
884 		    continue;
885 
886 		ret = spl_load_fit_image(info, sector, fit, base_offset, node,
887 					 &image_info);
888 		if (ret < 0)
889 			return ret;
890 
891 		if (os_type == IH_OS_U_BOOT) {
892 #if CONFIG_IS_ENABLED(ATF)
893 			fit_image_get_arch(fit, node, &ih_arch);
894 			debug("Image ARCH is %s\n", genimg_get_arch_name(ih_arch));
895 			if (ih_arch == IH_ARCH_ARM)
896 				spl_image->flags |= SPL_ATF_AARCH32_BL33;
897 			spl_image->entry_point_bl33 = image_info.load_addr;
898 #elif CONFIG_IS_ENABLED(OPTEE)
899 			spl_image->entry_point_os = image_info.load_addr;
900 #endif
901 			ret = spl_fit_append_fdt(&image_info, info, sector,
902 						 fit, images, base_offset);
903 			if (ret < 0)
904 				return ret;
905 			spl_image->fdt_addr = image_info.fdt_addr;
906 		}
907 
908 		/*
909 		 * If the "firmware" image did not provide an entry point,
910 		 * use the first valid entry point from the loadables.
911 		 */
912 		if (spl_image->entry_point == FDT_ERROR &&
913 		    image_info.entry_point != FDT_ERROR)
914 			spl_image->entry_point = image_info.entry_point;
915 
916 		/* Record our loadables into the FDT */
917 		if (spl_image->fdt_addr && spl_image->next_stage == SPL_NEXT_STAGE_UBOOT)
918 			spl_fit_record_loadable(fit, images, index,
919 						spl_image->fdt_addr,
920 						&image_info);
921 #if CONFIG_IS_ENABLED(ATF)
922 		else if (os_type == IH_OS_OP_TEE)
923 			spl_image->entry_point_bl32 = image_info.load_addr;
924 #endif
925 	}
926 
927 	/*
928 	 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
929 	 * Makefile will set it to 0 and it will end up as the entry point
930 	 * here. What it actually means is: use the load address.
931 	 */
932 	if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
933 		spl_image->entry_point = spl_image->load_addr;
934 
935 	return 0;
936 }
937 
938 int spl_load_simple_fit(struct spl_image_info *spl_image,
939 			struct spl_load_info *info, ulong sector, void *fit)
940 {
941 	ulong sector_offs = sector;
942 	int ret = -EINVAL;
943 	int i;
944 
945 #ifdef CONFIG_MP_BOOT
946 	mpb_init_1(*info);
947 #endif
948 
949 	printf("Trying fit image at 0x%lx sector\n", sector_offs);
950 	for (i = 0; i < CONFIG_SPL_FIT_IMAGE_MULTIPLE; i++) {
951 		if (i > 0) {
952 			sector_offs +=
953 			   i * ((CONFIG_SPL_FIT_IMAGE_KB << 10) / info->bl_len);
954 			printf("Trying fit image at 0x%lx sector\n", sector_offs);
955 			if (info->read(info, sector_offs, 1, fit) != 1) {
956 				printf("IO error\n");
957 				continue;
958 			}
959 		}
960 
961 		if (image_get_magic(fit) != FDT_MAGIC) {
962 			printf("Not fit magic\n");
963 			continue;
964 		}
965 
966 		ret = spl_internal_load_simple_fit(spl_image, info,
967 						   sector_offs, fit);
968 		if (!ret) {
969 #ifdef CONFIG_SPL_KERNEL_BOOT
970 			ret = spl_load_kernel_fit(spl_image, info);
971 #endif
972 			break;
973 		}
974 	}
975 #ifdef CONFIG_SPL_AB
976 	/* If boot fail in spl, spl must decrease 1 and do_reset. */
977 	if (ret)
978 		return spl_ab_decrease_reset(info->dev);
979 	/*
980 	 * If boot successfully, it is no need to do decrease
981 	 * and U-boot will always decrease 1.
982 	 * If in thunderboot process, always need to decrease 1.
983 	 */
984 	if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL)
985 		spl_ab_decrease_tries(info->dev);
986 #endif
987 
988 	return ret;
989 }
990 
991