xref: /rk3399_rockchip-uboot/common/android_bootloader.c (revision 2602b1ba1429717f5d33bbe0fe45c601c9a6c923)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #include <android_bootloader.h>
8 #include <android_bootloader_message.h>
9 #include <android_avb/avb_slot_verify.h>
10 #include <android_avb/avb_ops_user.h>
11 #include <android_avb/rk_avb_ops_user.h>
12 #include <android_image.h>
13 #include <android_ab.h>
14 #include <bootm.h>
15 #include <asm/arch/hotkey.h>
16 #include <cli.h>
17 #include <common.h>
18 #include <dt_table.h>
19 #include <image-android-dt.h>
20 #include <malloc.h>
21 #include <fdt_support.h>
22 #include <fs.h>
23 #include <boot_rkimg.h>
24 #include <attestation_key.h>
25 #include <keymaster.h>
26 #include <linux/libfdt_env.h>
27 #include <optee_include/OpteeClientInterface.h>
28 #include <bidram.h>
29 #include <console.h>
30 #include <sysmem.h>
31 
32 DECLARE_GLOBAL_DATA_PTR;
33 
34 int android_bootloader_message_load(
35 	struct blk_desc *dev_desc,
36 	const disk_partition_t *part_info,
37 	struct android_bootloader_message *message)
38 {
39 	ulong message_blocks = sizeof(struct android_bootloader_message) /
40 	    part_info->blksz;
41 	if (message_blocks > part_info->size) {
42 		printf("misc partition too small.\n");
43 		return -1;
44 	}
45 
46 	if (blk_dread(dev_desc, part_info->start + android_bcb_msg_sector_offset(),
47 	     message_blocks, message) !=
48 	    message_blocks) {
49 		printf("Could not read from misc partition\n");
50 		return -1;
51 	}
52 	debug("ANDROID: Loaded BCB, %lu blocks.\n", message_blocks);
53 	return 0;
54 }
55 
56 static int android_bootloader_message_write(
57 	struct blk_desc *dev_desc,
58 	const disk_partition_t *part_info,
59 	struct android_bootloader_message *message)
60 {
61 	ulong message_blocks = sizeof(struct android_bootloader_message) /
62 	    part_info->blksz + android_bcb_msg_sector_offset();
63 
64 	if (message_blocks > part_info->size) {
65 		printf("misc partition too small.\n");
66 		return -1;
67 	}
68 
69 	if (blk_dwrite(dev_desc, part_info->start, message_blocks, message) !=
70 	    message_blocks) {
71 		printf("Could not write to misc partition\n");
72 		return -1;
73 	}
74 	debug("ANDROID: Wrote new BCB, %lu blocks.\n", message_blocks);
75 	return 0;
76 }
77 
78 static enum android_boot_mode android_bootloader_load_and_clear_mode(
79 	struct blk_desc *dev_desc,
80 	const disk_partition_t *misc_part_info)
81 {
82 	struct android_bootloader_message bcb;
83 
84 #ifdef CONFIG_FASTBOOT
85 	char *bootloader_str;
86 
87 	/* Check for message from bootloader stored in RAM from a previous boot.
88 	 */
89 	bootloader_str = (char *)CONFIG_FASTBOOT_BUF_ADDR;
90 	if (!strcmp("reboot-bootloader", bootloader_str)) {
91 		bootloader_str[0] = '\0';
92 		return ANDROID_BOOT_MODE_BOOTLOADER;
93 	}
94 #endif
95 
96 	/* Check and update the BCB message if needed. */
97 	if (android_bootloader_message_load(dev_desc, misc_part_info, &bcb) <
98 	    0) {
99 		printf("WARNING: Unable to load the BCB.\n");
100 		return ANDROID_BOOT_MODE_NORMAL;
101 	}
102 
103 	if (!strcmp("bootonce-bootloader", bcb.command)) {
104 		/* Erase the message in the BCB since this value should be used
105 		 * only once.
106 		 */
107 		memset(bcb.command, 0, sizeof(bcb.command));
108 		android_bootloader_message_write(dev_desc, misc_part_info,
109 						 &bcb);
110 		return ANDROID_BOOT_MODE_BOOTLOADER;
111 	}
112 
113 	if (!strcmp("boot-recovery", bcb.command))
114 		return ANDROID_BOOT_MODE_RECOVERY;
115 
116 	if (!strcmp("boot-fastboot", bcb.command))
117 		return ANDROID_BOOT_MODE_RECOVERY;
118 
119 	return ANDROID_BOOT_MODE_NORMAL;
120 }
121 
122 int android_bcb_write(char *cmd)
123 {
124 	struct android_bootloader_message message = {0};
125 	disk_partition_t part_info;
126 	struct blk_desc *dev_desc;
127 	int ret;
128 
129 	if (!cmd)
130 		return -ENOMEM;
131 
132 	if (strlen(cmd) >= 32)
133 		return -ENOMEM;
134 
135 	dev_desc = rockchip_get_bootdev();
136 	if (!dev_desc) {
137 		printf("%s: dev_desc is NULL!\n", __func__);
138 		return -ENODEV;
139 	}
140 
141 	ret = part_get_info_by_name(dev_desc, ANDROID_PARTITION_MISC, &part_info);
142 	if (ret < 0) {
143 		printf("%s: Could not found misc partition, just run recovery\n",
144 		       __func__);
145 		return -ENODEV;
146 	}
147 
148 	strcpy(message.command, cmd);
149 	return android_bootloader_message_write(dev_desc, &part_info, &message);
150 }
151 
152 /**
153  * Return the reboot reason string for the passed boot mode.
154  *
155  * @param mode	The Android Boot mode.
156  * @return a pointer to the reboot reason string for mode.
157  */
158 static const char *android_boot_mode_str(enum android_boot_mode mode)
159 {
160 	switch (mode) {
161 	case ANDROID_BOOT_MODE_NORMAL:
162 		return "(none)";
163 	case ANDROID_BOOT_MODE_RECOVERY:
164 		return "recovery";
165 	case ANDROID_BOOT_MODE_BOOTLOADER:
166 		return "bootloader";
167 	}
168 	return NULL;
169 }
170 
171 static int android_bootloader_boot_bootloader(void)
172 {
173 	const char *fastboot_cmd = env_get("fastbootcmd");
174 
175 	if (fastboot_cmd == NULL) {
176 		printf("fastboot_cmd is null, run default fastboot_cmd!\n");
177 		fastboot_cmd = "fastboot usb 0";
178 	}
179 
180 	return run_command(fastboot_cmd, CMD_FLAG_ENV);
181 }
182 
183 #ifdef CONFIG_SUPPORT_OEM_DTB
184 static int android_bootloader_get_fdt(const char *part_name,
185 		const char *load_file_name)
186 {
187 	struct blk_desc *dev_desc;
188 	disk_partition_t part_info;
189 	char *fdt_addr = NULL;
190 	char dev_part[3] = {0};
191 	loff_t bytes = 0;
192 	loff_t pos = 0;
193 	loff_t len_read;
194 	unsigned long addr = 0;
195 	int part_num = -1;
196 	int ret;
197 
198 	dev_desc = rockchip_get_bootdev();
199 	if (!dev_desc) {
200 		printf("%s: dev_desc is NULL!\n", __func__);
201 		return -1;
202 	}
203 
204 	part_num = part_get_info_by_name(dev_desc, part_name, &part_info);
205 	if (part_num < 0) {
206 		printf("ANDROID: Could not find partition \"%s\"\n", part_name);
207 		return -1;
208 	}
209 
210 	snprintf(dev_part, ARRAY_SIZE(dev_part), ":%x", part_num);
211 	if (fs_set_blk_dev_with_part(dev_desc, part_num))
212 		return -1;
213 
214 	fdt_addr = env_get("fdt_addr_r");
215 	if (!fdt_addr) {
216 		printf("ANDROID: No Found FDT Load Address.\n");
217 		return -1;
218 	}
219 	addr = simple_strtoul(fdt_addr, NULL, 16);
220 
221 	ret = fs_read(load_file_name, addr, pos, bytes, &len_read);
222 	if (ret < 0)
223 		return -1;
224 
225 	return 0;
226 }
227 #endif
228 
229 /*
230  *   Test on RK3308 AARCH64 mode (Cortex A35 816 MHZ) boot with eMMC:
231  *
232  *   |-------------------------------------------------------------------|
233  *   | Format    |  Size(Byte) | Ratio | Decomp time(ms) | Boot time(ms) |
234  *   |-------------------------------------------------------------------|
235  *   | Image     | 7720968     |       |                 |     488       |
236  *   |-------------------------------------------------------------------|
237  *   | Image.lz4 | 4119448     | 53%   |       59        |     455       |
238  *   |-------------------------------------------------------------------|
239  *   | Image.lzo | 3858322     | 49%   |       141       |     536       |
240  *   |-------------------------------------------------------------------|
241  *   | Image.gz  | 3529108     | 45%   |       222       |     609       |
242  *   |-------------------------------------------------------------------|
243  *   | Image.bz2 | 3295914     | 42%   |       2940      |               |
244  *   |-------------------------------------------------------------------|
245  *   | Image.lzma| 2683750     | 34%   |                 |               |
246  *   |-------------------------------------------------------------------|
247  */
248 static int sysmem_alloc_uncomp_kernel(ulong andr_hdr,
249 				      ulong uncomp_kaddr, u32 comp)
250 {
251 	struct andr_img_hdr *hdr = (struct andr_img_hdr *)andr_hdr;
252 	ulong ksize, kaddr;
253 
254 	if (comp != IH_COMP_NONE) {
255 		/* Release compressed sysmem */
256 		kaddr = env_get_hex("kernel_addr_c", 0);
257 		if (!kaddr)
258 			kaddr = env_get_hex("kernel_addr_r", 0);
259 		kaddr -= hdr->page_size;
260 		if (sysmem_free((phys_addr_t)kaddr))
261 			return -EINVAL;
262 #ifdef CONFIG_SKIP_RELOCATE_UBOOT
263 		sysmem_free(CONFIG_SYS_TEXT_BASE);
264 #endif
265 		/*
266 		 * Use smaller Ratio to get larger estimated uncompress
267 		 * kernel size.
268 		 */
269 		if (comp == IH_COMP_ZIMAGE)
270 			ksize = hdr->kernel_size * 100 / 45;
271 		else if (comp == IH_COMP_LZ4)
272 			ksize = hdr->kernel_size * 100 / 50;
273 		else if (comp == IH_COMP_LZO)
274 			ksize = hdr->kernel_size * 100 / 45;
275 		else if (comp == IH_COMP_GZIP)
276 			ksize = hdr->kernel_size * 100 / 40;
277 		else if (comp == IH_COMP_BZIP2)
278 			ksize = hdr->kernel_size * 100 / 40;
279 		else if (comp == IH_COMP_LZMA)
280 			ksize = hdr->kernel_size * 100 / 30;
281 		else
282 			ksize = hdr->kernel_size;
283 
284 		kaddr = uncomp_kaddr;
285 		ksize = ALIGN(ksize, 512);
286 		if (!sysmem_alloc_base(MEM_UNCOMP_KERNEL,
287 				       (phys_addr_t)kaddr, ksize))
288 			return -ENOMEM;
289 	}
290 
291 	return 0;
292 }
293 
294 int android_bootloader_boot_kernel(unsigned long kernel_address)
295 {
296 	char *kernel_addr_r = env_get("kernel_addr_r");
297 	char *kernel_addr_c = env_get("kernel_addr_c");
298 	char *fdt_addr = env_get("fdt_addr_r");
299 	char kernel_addr_str[12];
300 	char comp_str[32] = {0};
301 	ulong comp_type;
302 	const char *comp_name[] = {
303 		[IH_COMP_NONE]  = "IMAGE",
304 		[IH_COMP_GZIP]  = "GZIP",
305 		[IH_COMP_BZIP2] = "BZIP2",
306 		[IH_COMP_LZMA]  = "LZMA",
307 		[IH_COMP_LZO]   = "LZO",
308 		[IH_COMP_LZ4]   = "LZ4",
309 		[IH_COMP_ZIMAGE]= "ZIMAGE",
310 	};
311 	char *bootm_args[] = {
312 		kernel_addr_str, kernel_addr_str, fdt_addr, NULL };
313 
314 	comp_type = env_get_ulong("os_comp", 10, 0);
315 	sprintf(kernel_addr_str, "0x%08lx", kernel_address);
316 
317 	if (comp_type != IH_COMP_NONE) {
318 		if (comp_type == IH_COMP_ZIMAGE &&
319 		    kernel_addr_r && !kernel_addr_c) {
320 			kernel_addr_c = kernel_addr_r;
321 			kernel_addr_r = __stringify(CONFIG_SYS_SDRAM_BASE);
322 		}
323 		snprintf(comp_str, 32, "%s%s%s",
324 			 "(Uncompress to ", kernel_addr_r, ")");
325 	}
326 
327 	printf("Booting %s kernel at %s%s with fdt at %s...\n\n\n",
328 	       comp_name[comp_type],
329 	       comp_type != IH_COMP_NONE ? kernel_addr_c : kernel_addr_r,
330 	       comp_str, fdt_addr);
331 
332 	hotkey_run(HK_SYSMEM);
333 
334 	/*
335 	 * Check whether there is enough space for uncompress kernel,
336 	 * Actually, here only gives a sysmem warning message when failed
337 	 * but never return -1.
338 	 */
339 	if (sysmem_alloc_uncomp_kernel(kernel_address,
340 				       simple_strtoul(kernel_addr_r, NULL, 16),
341 				       comp_type))
342 		return -1;
343 
344 	return do_bootm_states(NULL, 0, ARRAY_SIZE(bootm_args), bootm_args,
345 		BOOTM_STATE_START |
346 		BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
347 		BOOTM_STATE_LOADOS |
348 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
349 		BOOTM_STATE_RAMDISK |
350 #endif
351 		BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
352 		BOOTM_STATE_OS_GO, &images, 1);
353 }
354 
355 static char *strjoin(const char **chunks, char separator)
356 {
357 	int len, joined_len = 0;
358 	char *ret, *current;
359 	const char **p;
360 
361 	for (p = chunks; *p; p++)
362 		joined_len += strlen(*p) + 1;
363 
364 	if (!joined_len) {
365 		ret = malloc(1);
366 		if (ret)
367 			ret[0] = '\0';
368 		return ret;
369 	}
370 
371 	ret = malloc(joined_len);
372 	current = ret;
373 	if (!ret)
374 		return ret;
375 
376 	for (p = chunks; *p; p++) {
377 		len = strlen(*p);
378 		memcpy(current, *p, len);
379 		current += len;
380 		*current = separator;
381 		current++;
382 	}
383 	/* Replace the last separator by a \0. */
384 	current[-1] = '\0';
385 	return ret;
386 }
387 
388 /** android_assemble_cmdline - Assemble the command line to pass to the kernel
389  * @return a newly allocated string
390  */
391 char *android_assemble_cmdline(const char *slot_suffix,
392 				      const char *extra_args)
393 {
394 	const char *cmdline_chunks[16];
395 	const char **current_chunk = cmdline_chunks;
396 	char *env_cmdline, *cmdline, *rootdev_input, *serialno;
397 	char *allocated_suffix = NULL;
398 	char *allocated_serialno = NULL;
399 	char *allocated_rootdev = NULL;
400 	unsigned long rootdev_len;
401 
402 	env_cmdline = env_get("bootargs");
403 	if (env_cmdline)
404 		*(current_chunk++) = env_cmdline;
405 
406 	/* The |slot_suffix| needs to be passed to the kernel to know what
407 	 * slot to boot from.
408 	 */
409 #ifdef CONFIG_ANDROID_AB
410 	if (slot_suffix) {
411 		allocated_suffix = malloc(strlen(ANDROID_ARG_SLOT_SUFFIX) +
412 					  strlen(slot_suffix) + 1);
413 		memset(allocated_suffix, 0, strlen(ANDROID_ARG_SLOT_SUFFIX)
414 		       + strlen(slot_suffix) + 1);
415 		strcpy(allocated_suffix, ANDROID_ARG_SLOT_SUFFIX);
416 		strcat(allocated_suffix, slot_suffix);
417 		*(current_chunk++) = allocated_suffix;
418 	}
419 #endif
420 	serialno = env_get("serial#");
421 	if (serialno) {
422 		allocated_serialno = malloc(strlen(ANDROID_ARG_SERIALNO) +
423 					  strlen(serialno) + 1);
424 		memset(allocated_serialno, 0, strlen(ANDROID_ARG_SERIALNO) +
425 				strlen(serialno) + 1);
426 		strcpy(allocated_serialno, ANDROID_ARG_SERIALNO);
427 		strcat(allocated_serialno, serialno);
428 		*(current_chunk++) = allocated_serialno;
429 	}
430 
431 	rootdev_input = env_get("android_rootdev");
432 	if (rootdev_input) {
433 		rootdev_len = strlen(ANDROID_ARG_ROOT) + CONFIG_SYS_CBSIZE + 1;
434 		allocated_rootdev = malloc(rootdev_len);
435 		strcpy(allocated_rootdev, ANDROID_ARG_ROOT);
436 		cli_simple_process_macros(rootdev_input,
437 					  allocated_rootdev +
438 					  strlen(ANDROID_ARG_ROOT));
439 		/* Make sure that the string is null-terminated since the
440 		 * previous could not copy to the end of the input string if it
441 		 * is too big.
442 		 */
443 		allocated_rootdev[rootdev_len - 1] = '\0';
444 		*(current_chunk++) = allocated_rootdev;
445 	}
446 
447 	if (extra_args)
448 		*(current_chunk++) = extra_args;
449 
450 	*(current_chunk++) = NULL;
451 	cmdline = strjoin(cmdline_chunks, ' ');
452 	free(allocated_suffix);
453 	free(allocated_rootdev);
454 	return cmdline;
455 }
456 
457 #ifdef CONFIG_ANDROID_AVB
458 static void slot_set_unbootable(AvbABSlotData* slot)
459 {
460 	slot->priority = 0;
461 	slot->tries_remaining = 0;
462 	slot->successful_boot = 0;
463 }
464 
465 static char *join_str(const char *a, const char *b)
466 {
467 	size_t len = strlen(a) + strlen(b) + 1 /* null term */;
468 	char *ret = (char *)malloc(len);
469 
470 	if (!ret) {
471 		debug("failed to alloc %zu\n", len);
472 		return NULL;
473 	}
474 	strcpy(ret, a);
475 	strcat(ret, b);
476 
477 	return ret;
478 }
479 
480 static size_t get_partition_size(AvbOps *ops, char *name,
481 				 const char *slot_suffix)
482 {
483 	char *partition_name = join_str(name, slot_suffix);
484 	uint64_t size = 0;
485 	AvbIOResult res;
486 
487 	if (partition_name == NULL)
488 		goto bail;
489 
490 	res = ops->get_size_of_partition(ops, partition_name, &size);
491 	if (res != AVB_IO_RESULT_OK && res != AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION)
492 		size = 0;
493 bail:
494 	if (partition_name)
495 		free(partition_name);
496 
497 	return size;
498 }
499 
500 static int avb_image_distribute_prepare(AvbSlotVerifyData *slot_data,
501 					AvbOps *ops, char *slot_suffix)
502 {
503 	struct AvbOpsData *data = (struct AvbOpsData *)(ops->user_data);
504 	size_t vendor_boot_size;
505 	size_t init_boot_size;
506 	size_t boot_size;
507 	void *image_buf;
508 
509 	boot_size = max(get_partition_size(ops, ANDROID_PARTITION_BOOT, slot_suffix),
510 		get_partition_size(ops, ANDROID_PARTITION_RECOVERY, slot_suffix));
511 	init_boot_size = get_partition_size(ops,
512 				ANDROID_PARTITION_INIT_BOOT, slot_suffix);
513 	vendor_boot_size = get_partition_size(ops,
514 				ANDROID_PARTITION_VENDOR_BOOT, slot_suffix);
515 	image_buf = sysmem_alloc(MEM_AVB_ANDROID,
516 				 boot_size + init_boot_size + vendor_boot_size);
517 	if (!image_buf) {
518 		printf("avb: sysmem alloc failed\n");
519 		return -1;
520 	}
521 
522 	data = (struct AvbOpsData *)(ops->user_data);
523 	data->slot_suffix = slot_suffix;
524 	data->boot.addr = image_buf;
525 	data->boot.size = 0;
526 	data->vendor_boot.addr = data->boot.addr + boot_size;
527 	data->vendor_boot.size = 0;
528 	data->init_boot.addr = data->vendor_boot.addr + vendor_boot_size;
529 	data->init_boot.size = 0;
530 
531 	return 0;
532 }
533 
534 static int avb_image_distribute_finish(AvbSlotVerifyData *slot_data,
535 				       AvbSlotVerifyFlags flags,
536 				       ulong *load_address)
537 {
538 	struct andr_img_hdr *hdr;
539 	ulong load_addr = *load_address;
540 	void *vendor_boot_hdr = NULL;
541 	void *init_boot_hdr = NULL;
542 	void *boot_hdr = NULL;
543 	char *part_name;
544 	int i, ret;
545 
546 	for (i = 0; i < slot_data->num_loaded_partitions; i++) {
547 		part_name = slot_data->loaded_partitions[i].partition_name;
548 		if (!strncmp(ANDROID_PARTITION_BOOT, part_name, 4) ||
549 		    !strncmp(ANDROID_PARTITION_RECOVERY, part_name, 8)) {
550 			boot_hdr = slot_data->loaded_partitions[i].data;
551 		} else if (!strncmp(ANDROID_PARTITION_INIT_BOOT, part_name, 9)) {
552 			init_boot_hdr = slot_data->loaded_partitions[i].data;
553 		} else if (!strncmp(ANDROID_PARTITION_VENDOR_BOOT, part_name, 11)) {
554 			vendor_boot_hdr = slot_data->loaded_partitions[i].data;
555 		}
556 	}
557 
558 	/*
559 	 *		populate boot_img_hdr_v34
560 	 *
561 	 * If allow verification error: the images are loaded by
562 	 * ops->get_preloaded_partition() which auto populates
563 	 * boot_img_hdr_v34.
564 	 *
565 	 * If not allow verification error: the images are full loaded
566 	 * by ops->read_from_partition() which doesn't populate
567 	 * boot_img_hdr_v34, we need to fix it here for bootm and
568 	 */
569 
570 	hdr = boot_hdr;
571 	if (hdr->header_version >= 3 &&
572 	    !(flags & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR)) {
573 		hdr = malloc(sizeof(struct andr_img_hdr));
574 		if (!hdr)
575 			return -1;
576 
577 		ret = populate_boot_info(boot_hdr, vendor_boot_hdr,
578 					 init_boot_hdr, hdr, true);
579 		if (ret < 0) {
580 			printf("avb: populate boot info failed, ret=%d\n", ret);
581 			return -1;
582 		}
583 		memcpy(boot_hdr, hdr, sizeof(*hdr));
584 	}
585 
586 	/* distribute ! */
587 	load_addr -= hdr->page_size;
588 	if (android_image_memcpy_separate(boot_hdr, &load_addr)) {
589 		printf("Failed to separate copy android image\n");
590 		return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
591 	}
592 
593 	*load_address = load_addr;
594 
595 	return 0;
596 }
597 
598 /*
599  *		AVB Policy.
600  *
601  * == avb with unlock:
602  * Don't process hash verify.
603  * Go pre-loaded path: Loading vendor_boot and init_boot
604  * directly to where they should be, while loading the
605  * boot/recovery. The boot message tells like:
606  * ···
607  * preloaded: distribute image from 'boot_a'
608  * preloaded: distribute image from 'init_boot_a'
609  * preloaded: distribute image from 'vendor_boot_a'
610  * ···
611  *
612  * == avb with lock:
613  * Process hash verify.
614  * Go pre-loaded path: Loading full vendor_boot, init_boot and
615  * boot/recovery one by one to verify, and distributing them to
616  * where they should be by memcpy at last.
617  *
618  * The three images share a large memory buffer that allocated
619  * by sysmem_alloc(), it locate at high memory address that
620  * just lower than SP bottom. The boot message tells like:
621  * ···
622  * preloaded: full image from 'boot_a' at 0xe47f90c0 - 0xe7a4b0c0
623  * preloaded: full image from 'init_boot_a' at 0xeaff90c0 - 0xeb2950c0
624  * preloaded: full image from 'vendor_boot_a' at 0xe87f90c0 - 0xe9f6e0c0
625  * ···
626  */
627 static AvbSlotVerifyResult android_slot_verify(char *boot_partname,
628 			       unsigned long *android_load_address,
629 			       char *slot_suffix)
630 {
631 	const char *requested_partitions[] = {
632 		boot_partname,
633 		NULL,
634 		NULL,
635 		NULL,
636 	};
637 	struct blk_desc *dev_desc;
638 	struct andr_img_hdr *hdr;
639 	disk_partition_t part_info;
640 	uint8_t unlocked = true;
641 	AvbOps *ops;
642 	AvbSlotVerifyFlags flags;
643 	AvbSlotVerifyData *slot_data = {NULL};
644 	AvbSlotVerifyResult verify_result;
645 	AvbABData ab_data, ab_data_orig;
646 	size_t slot_index_to_boot = 0;
647 	char verify_state[38] = {0};
648 	char can_boot = 1;
649 	char retry_no_vbmeta_partition = 1;
650 	unsigned long load_address = *android_load_address;
651 	int ret;
652 
653 	dev_desc = rockchip_get_bootdev();
654 	if (!dev_desc)
655 		return AVB_IO_RESULT_ERROR_IO;
656 
657 	if (part_get_info_by_name(dev_desc, boot_partname, &part_info) < 0) {
658 		printf("Could not find \"%s\" partition\n", boot_partname);
659 		return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
660 	}
661 
662 	hdr = populate_andr_img_hdr(dev_desc, &part_info);
663 	if (!hdr) {
664 		printf("No valid android hdr\n");
665 		return -1;
666 	}
667 
668 	if (hdr->header_version >= 4) {
669 		requested_partitions[1] = ANDROID_PARTITION_VENDOR_BOOT;
670 		if (((hdr->os_version >> 25) & 0x7f) >= 13)
671 			requested_partitions[2] = ANDROID_PARTITION_INIT_BOOT;
672 	}
673 
674 	ops = avb_ops_user_new();
675 	if (ops == NULL) {
676 		printf("avb_ops_user_new() failed!\n");
677 		return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
678 	}
679 
680 	if (ops->read_is_device_unlocked(ops, (bool *)&unlocked) != AVB_IO_RESULT_OK)
681 		printf("Error determining whether device is unlocked.\n");
682 
683 	printf("read_is_device_unlocked() ops returned that device is %s\n",
684 	       (unlocked & LOCK_MASK)? "UNLOCKED" : "LOCKED");
685 
686 	flags = AVB_SLOT_VERIFY_FLAGS_NONE;
687 	if (unlocked & LOCK_MASK)
688 		flags |= AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR;
689 
690 	if (load_metadata(ops->ab_ops, &ab_data, &ab_data_orig)) {
691 		printf("Can not load metadata\n");
692 		return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
693 	}
694 
695 	if (!strncmp(slot_suffix, "_a", 2))
696 		slot_index_to_boot = 0;
697 	else if (!strncmp(slot_suffix, "_b", 2))
698 		slot_index_to_boot = 1;
699 	else
700 		slot_index_to_boot = 0;
701 
702 	if (strcmp(boot_partname, ANDROID_PARTITION_RECOVERY) == 0)
703 		flags |= AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION;
704 
705 	/* prepare image buffer */
706 	ret = avb_image_distribute_prepare(slot_data, ops, slot_suffix);
707 	if (ret < 0) {
708 		printf("avb image distribute prepare failed %d\n", ret);
709 		return -1;
710 	}
711 
712 retry_verify:
713 	verify_result =
714 	avb_slot_verify(ops,
715 			requested_partitions,
716 			slot_suffix,
717 			flags,
718 			AVB_HASHTREE_ERROR_MODE_RESTART,
719 			&slot_data);
720 	if (verify_result != AVB_SLOT_VERIFY_RESULT_OK &&
721 	    verify_result != AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED) {
722 		if (retry_no_vbmeta_partition && strcmp(boot_partname, ANDROID_PARTITION_RECOVERY) == 0) {
723 			printf("Verify recovery with vbmeta.\n");
724 			flags &= ~AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION;
725 			retry_no_vbmeta_partition = 0;
726 			goto retry_verify;
727 		}
728 	}
729 
730 	strcat(verify_state, ANDROID_VERIFY_STATE);
731 	switch (verify_result) {
732 	case AVB_SLOT_VERIFY_RESULT_OK:
733 		if (unlocked & LOCK_MASK)
734 			strcat(verify_state, "orange");
735 		else
736 			strcat(verify_state, "green");
737 		break;
738 	case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
739 		if (unlocked & LOCK_MASK)
740 			strcat(verify_state, "orange");
741 		else
742 			strcat(verify_state, "yellow");
743 		break;
744 	case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
745 	case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
746 	case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
747 	case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
748 	case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
749 	case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
750 	default:
751 		if (unlocked & LOCK_MASK)
752 			strcat(verify_state, "orange");
753 		else
754 			strcat(verify_state, "red");
755 		break;
756 	}
757 
758 	if (!slot_data) {
759 		can_boot = 0;
760 		goto out;
761 	}
762 
763 	if (verify_result == AVB_SLOT_VERIFY_RESULT_OK ||
764 	    verify_result == AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED ||
765 	    (unlocked & LOCK_MASK)) {
766 		int len = 0;
767 		char *bootargs, *newbootargs;
768 #ifdef CONFIG_ANDROID_AVB_ROLLBACK_INDEX
769 		if (rk_avb_update_stored_rollback_indexes_for_slot(ops, slot_data))
770 			printf("Fail to update the rollback indexes.\n");
771 #endif
772 		if (slot_data->cmdline) {
773 			debug("Kernel command line: %s\n", slot_data->cmdline);
774 			len += strlen(slot_data->cmdline);
775 		}
776 
777 		bootargs = env_get("bootargs");
778 		if (bootargs)
779 			len += strlen(bootargs);
780 
781 		newbootargs = malloc(len + 2);
782 
783 		if (!newbootargs) {
784 			puts("Error: malloc in android_slot_verify failed!\n");
785 			return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
786 		}
787 		*newbootargs = '\0';
788 
789 		if (bootargs) {
790 			strcpy(newbootargs, bootargs);
791 			strcat(newbootargs, " ");
792 		}
793 		if (slot_data->cmdline)
794 			strcat(newbootargs, slot_data->cmdline);
795 		env_set("bootargs", newbootargs);
796 
797 		/* if need, distribute full image to where they should be */
798 		ret = avb_image_distribute_finish(slot_data, flags, &load_address);
799 		if (ret < 0) {
800 			printf("avb image distribute finish failed %d\n", ret);
801 			return -1;
802 		}
803 		*android_load_address = load_address;
804 	} else {
805 		slot_set_unbootable(&ab_data.slots[slot_index_to_boot]);
806 	}
807 
808 out:
809 	env_update("bootargs", verify_state);
810 	if (save_metadata_if_changed(ops->ab_ops, &ab_data, &ab_data_orig)) {
811 		printf("Can not save metadata\n");
812 		verify_result = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
813 	}
814 
815 	if (slot_data != NULL)
816 		avb_slot_verify_data_free(slot_data);
817 
818 	if ((unlocked & LOCK_MASK) && can_boot)
819 		return 0;
820 	else
821 		return verify_result;
822 }
823 #endif
824 
825 #if defined(CONFIG_CMD_DTIMG) && defined(CONFIG_OF_LIBFDT_OVERLAY)
826 
827 /*
828  * Default return index 0.
829  */
830 __weak int board_select_fdt_index(ulong dt_table_hdr)
831 {
832 /*
833  * User can use "dt_for_each_entry(entry, hdr, idx)" to iterate
834  * over all dt entry of DT image and pick up which they want.
835  *
836  * Example:
837  *	struct dt_table_entry *entry;
838  *	int index;
839  *
840  *	dt_for_each_entry(entry, dt_table_hdr, index) {
841  *
842  *		.... (use entry)
843  *	}
844  *
845  *	return index;
846  */
847 	return 0;
848 }
849 
850 static int android_get_dtbo(ulong *fdt_dtbo,
851 			    const struct andr_img_hdr *hdr,
852 			    int *index, const char *part_dtbo)
853 {
854 	struct dt_table_header *dt_hdr = NULL;
855 	struct blk_desc *dev_desc;
856 	disk_partition_t part_info;
857 	u32 blk_offset, blk_cnt;
858 	void *buf;
859 	ulong e_addr;
860 	u32 e_size;
861 	int e_idx;
862 	int ret;
863 
864 	/* Get partition info */
865 	dev_desc = rockchip_get_bootdev();
866 	if (!dev_desc)
867 		return -ENODEV;
868 
869 	ret = part_get_info_by_name(dev_desc, part_dtbo, &part_info);
870 	if (ret < 0) {
871 		printf("No %s partition, ret=%d\n", part_dtbo, ret);
872 		return ret;
873 	}
874 
875 	/* Check dt table header */
876 	if (!strcmp(part_dtbo, PART_RECOVERY))
877 		blk_offset = part_info.start +
878 			     (hdr->recovery_dtbo_offset / part_info.blksz);
879 	else
880 		blk_offset = part_info.start;
881 
882 	dt_hdr = memalign(ARCH_DMA_MINALIGN, part_info.blksz);
883 	if (!dt_hdr)
884 		return -ENOMEM;
885 
886 	ret = blk_dread(dev_desc, blk_offset, 1, dt_hdr);
887 	if (ret != 1)
888 		goto out1;
889 
890 	if (!android_dt_check_header((ulong)dt_hdr)) {
891 		printf("DTBO: invalid dt table header: 0x%x\n", dt_hdr->magic);
892 		ret = -EINVAL;
893 		goto out1;
894 	}
895 
896 #ifdef DEBUG
897 	android_dt_print_contents((ulong)dt_hdr);
898 #endif
899 
900 	blk_cnt = DIV_ROUND_UP(fdt32_to_cpu(dt_hdr->total_size),
901 			       part_info.blksz);
902 	/* Read all DT Image */
903 	buf = memalign(ARCH_DMA_MINALIGN, part_info.blksz * blk_cnt);
904 	if (!buf) {
905 		ret = -ENOMEM;
906 		goto out1;
907 	}
908 
909 	ret = blk_dread(dev_desc, blk_offset, blk_cnt, buf);
910 	if (ret != blk_cnt)
911 		goto out2;
912 
913 	e_idx = board_select_fdt_index((ulong)buf);
914 	if (e_idx < 0) {
915 		printf("%s: failed to select board fdt index\n", __func__);
916 		ret = -EINVAL;
917 		goto out2;
918 	}
919 
920 	ret = android_dt_get_fdt_by_index((ulong)buf, e_idx, &e_addr, &e_size);
921 	if (!ret) {
922 		printf("%s: failed to get fdt, index=%d\n", __func__, e_idx);
923 		ret = -EINVAL;
924 		goto out2;
925 	}
926 
927 	if (fdt_dtbo)
928 		*fdt_dtbo = e_addr;
929 	if (index)
930 		*index = e_idx;
931 
932 	free(dt_hdr);
933 	debug("ANDROID: Loading dt entry to 0x%lx size 0x%x idx %d from \"%s\" part\n",
934 	      e_addr, e_size, e_idx, part_dtbo);
935 
936 	return 0;
937 
938 out2:
939 	free(buf);
940 out1:
941 	free(dt_hdr);
942 
943 	return ret;
944 }
945 
946 int android_fdt_overlay_apply(void *fdt_addr)
947 {
948 	struct andr_img_hdr *hdr;
949 	struct blk_desc *dev_desc;
950 	const char *part_boot = PART_BOOT;
951 	disk_partition_t part_info;
952 	char *fdt_backup;
953 	char *part_dtbo = PART_DTBO;
954 	char buf[32] = {0};
955 	ulong fdt_dtbo = -1;
956 	u32 totalsize;
957 	int index = -1;
958 	int ret;
959 
960 	if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) {
961 #ifdef CONFIG_ANDROID_AB
962 		bool can_find_recovery;
963 
964 		can_find_recovery = ab_can_find_recovery_part();
965 		part_boot = can_find_recovery ? PART_RECOVERY : PART_BOOT;
966 		part_dtbo = can_find_recovery ? PART_RECOVERY : PART_DTBO;
967 #else
968 		part_boot = PART_RECOVERY;
969 		part_dtbo = PART_RECOVERY;
970 #endif
971 	}
972 
973 	dev_desc = rockchip_get_bootdev();
974 	if (!dev_desc)
975 		return -ENODEV;
976 
977 	ret = part_get_info_by_name(dev_desc, part_boot, &part_info);
978 	if (ret < 0)
979 		return ret;
980 
981 	hdr = populate_andr_img_hdr(dev_desc, &part_info);
982 	if (!hdr)
983 		return -EINVAL;
984 #ifdef DEBUG
985 	android_print_contents(hdr);
986 #endif
987 
988 	/*
989 	 * Google requires a/b system mandory from Android Header v3 for
990 	 * google authentication, that means there is not recovery.
991 	 *
992 	 * But for the products that don't care about google authentication,
993 	 * it's not mandory to use a/b system. So that we use the solution:
994 	 * boot.img(v3+) with recovery(v2).
995 	 *
996 	 * [recovery_dtbo fields]
997 	 *	recovery.img with boot_img_hdr_v1,2:  supported
998 	 *	recovery.img with boot_img_hdr_v0,3+: illegal
999 	 */
1000 	if ((hdr->header_version == 0) ||
1001 	    (hdr->header_version >= 3 && !strcmp(part_boot, PART_RECOVERY)))
1002 		goto out;
1003 
1004 	ret = android_get_dtbo(&fdt_dtbo, (void *)hdr, &index, part_dtbo);
1005 	if (!ret) {
1006 		phys_size_t fdt_size;
1007 
1008 		/* Must incease size before overlay */
1009 		fdt_size = fdt_totalsize((void *)fdt_addr) +
1010 				fdt_totalsize((void *)fdt_dtbo);
1011 		if (sysmem_free((phys_addr_t)fdt_addr))
1012 			goto out;
1013 
1014 		if (!sysmem_alloc_base(MEM_FDT_DTBO,
1015 				       (phys_addr_t)fdt_addr,
1016 					fdt_size + CONFIG_SYS_FDT_PAD))
1017 			goto out;
1018 		/*
1019 		 * Backup main fdt in case of being destroyed by
1020 		 * fdt_overlay_apply() when it overlys failed.
1021 		 */
1022 		totalsize = fdt_totalsize(fdt_addr);
1023 		fdt_backup = malloc(totalsize);
1024 		if (!fdt_backup)
1025 			goto out;
1026 
1027 		memcpy(fdt_backup, fdt_addr, totalsize);
1028 		fdt_increase_size(fdt_addr, fdt_totalsize((void *)fdt_dtbo));
1029 		ret = fdt_overlay_apply(fdt_addr, (void *)fdt_dtbo);
1030 		if (!ret) {
1031 			snprintf(buf, 32, "%s%d", "androidboot.dtbo_idx=", index);
1032 			env_update("bootargs", buf);
1033 			printf("ANDROID: fdt overlay OK\n");
1034 		} else {
1035 			memcpy(fdt_addr, fdt_backup, totalsize);
1036 			printf("ANDROID: fdt overlay failed, ret=%d\n", ret);
1037 		}
1038 
1039 		free(fdt_backup);
1040 	}
1041 
1042 out:
1043 	free(hdr);
1044 
1045 	return 0;
1046 }
1047 #endif
1048 
1049 int android_image_load_by_partname(struct blk_desc *dev_desc,
1050 				   const char *boot_partname,
1051 				   unsigned long *load_address)
1052 {
1053 	disk_partition_t boot_part;
1054 	int ret, part_num;
1055 
1056 	part_num = part_get_info_by_name(dev_desc, boot_partname, &boot_part);
1057 	if (part_num < 0) {
1058 		printf("%s: Can't find part: %s\n", __func__, boot_partname);
1059 		return -1;
1060 	}
1061 	debug("ANDROID: Loading kernel from \"%s\", partition %d.\n",
1062 	      boot_part.name, part_num);
1063 
1064 	ret = android_image_load(dev_desc, &boot_part, *load_address, -1UL);
1065 	if (ret < 0) {
1066 		debug("%s: %s part load fail, ret=%d\n",
1067 		      __func__, boot_part.name, ret);
1068 		return ret;
1069 	}
1070 	*load_address = ret;
1071 
1072 	return 0;
1073 }
1074 
1075 int android_bootloader_boot_flow(struct blk_desc *dev_desc,
1076 				 unsigned long load_address)
1077 {
1078 	enum android_boot_mode mode = ANDROID_BOOT_MODE_NORMAL;
1079 	disk_partition_t misc_part_info;
1080 	int part_num;
1081 	char *command_line;
1082 	char slot_suffix[3] = {0};
1083 	const char *mode_cmdline = NULL;
1084 	char *boot_partname = ANDROID_PARTITION_BOOT;
1085 
1086 	/*
1087 	 * 1. Load MISC partition and determine the boot mode
1088 	 *   clear its value for the next boot if needed.
1089 	 */
1090 	part_num = part_get_info_by_name(dev_desc, ANDROID_PARTITION_MISC,
1091 					 &misc_part_info);
1092 	if (part_num < 0) {
1093 		printf("Could not find misc partition\n");
1094 	} else {
1095 #ifdef CONFIG_ANDROID_KEYMASTER_CA
1096 		/* load attestation key from misc partition. */
1097 		load_attestation_key(dev_desc, &misc_part_info);
1098 #endif
1099 
1100 		mode = android_bootloader_load_and_clear_mode(dev_desc,
1101 							      &misc_part_info);
1102 #ifdef CONFIG_RKIMG_BOOTLOADER
1103 		if (mode == ANDROID_BOOT_MODE_NORMAL) {
1104 			if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY)
1105 				mode = ANDROID_BOOT_MODE_RECOVERY;
1106 		}
1107 #endif
1108 	}
1109 
1110 	printf("ANDROID: reboot reason: \"%s\"\n", android_boot_mode_str(mode));
1111 #ifdef CONFIG_ANDROID_AB
1112 	/* Get current slot_suffix */
1113 	if (ab_get_slot_suffix(slot_suffix))
1114 		return -1;
1115 #endif
1116 	switch (mode) {
1117 	case ANDROID_BOOT_MODE_NORMAL:
1118 		/* In normal mode, we load the kernel from "boot" but append
1119 		 * "skip_initramfs" to the cmdline to make it ignore the
1120 		 * recovery initramfs in the boot partition.
1121 		 */
1122 #ifdef CONFIG_ANDROID_AB
1123 		/*  In A/B, the recovery image is built as boot.img, containing the
1124 		* recovery's ramdisk. Previously, bootloader used the skip_initramfs
1125 		* kernel command line parameter to decide which mode to boot into.
1126 		* For Android >=10 and with dynamic partition support, the bootloader
1127 		* MUST NOT pass skip_initramfs to the kernel command-line.
1128 		* Instead, bootloader should pass androidboot.force_normal_boot=1
1129 		* and then Android's first-stage init in ramdisk
1130 		* will skip recovery and boot normal Android.
1131 		*/
1132 		if (ab_is_support_dynamic_partition(dev_desc)) {
1133 			mode_cmdline = "androidboot.force_normal_boot=1";
1134 		} else {
1135 			mode_cmdline = "skip_initramfs";
1136 		}
1137 #endif
1138 		break;
1139 	case ANDROID_BOOT_MODE_RECOVERY:
1140 		/*
1141 		 * In recovery mode, if have recovery partition, we still boot the
1142 		 * kernel from "recovery". If not, don't skip the initramfs so it
1143 		 * boots to recovery from image in partition "boot".
1144 		 */
1145 #ifdef CONFIG_ANDROID_AB
1146 		boot_partname = ab_can_find_recovery_part() ?
1147 			ANDROID_PARTITION_RECOVERY : ANDROID_PARTITION_BOOT;
1148 #else
1149 		boot_partname = ANDROID_PARTITION_RECOVERY;
1150 #endif
1151 		break;
1152 	case ANDROID_BOOT_MODE_BOOTLOADER:
1153 		/* Bootloader mode enters fastboot. If this operation fails we
1154 		 * simply return since we can't recover from this situation by
1155 		 * switching to another slot.
1156 		 */
1157 		return android_bootloader_boot_bootloader();
1158 	}
1159 
1160 #ifdef CONFIG_ANDROID_AVB
1161 	uint8_t vboot_flag = 0;
1162 	disk_partition_t vbmeta_part_info;
1163 
1164 #ifdef CONFIG_OPTEE_CLIENT
1165 	if (trusty_read_vbootkey_enable_flag(&vboot_flag)) {
1166 		printf("Can't read vboot flag\n");
1167 		return -1;
1168 	}
1169 #endif
1170 	if (vboot_flag) {
1171 		printf("Vboot=1, SecureBoot enabled, AVB verify\n");
1172 		if (android_slot_verify(boot_partname, &load_address,
1173 					slot_suffix)) {
1174 			printf("AVB verify failed\n");
1175 
1176 			return -1;
1177 		}
1178 	} else {
1179 		part_num = part_get_info_by_name(dev_desc,
1180 						 ANDROID_PARTITION_VBMETA,
1181 						 &vbmeta_part_info);
1182 		if (part_num < 0) {
1183 			printf("Not AVB images, AVB skip\n");
1184 			env_update("bootargs",
1185 				   "androidboot.verifiedbootstate=orange");
1186 			if (android_image_load_by_partname(dev_desc,
1187 							   boot_partname,
1188 							   &load_address)) {
1189 				printf("Android image load failed\n");
1190 				return -1;
1191 			}
1192 		} else {
1193 			printf("Vboot=0, AVB images, AVB verify\n");
1194 			if (android_slot_verify(boot_partname, &load_address,
1195 						slot_suffix)) {
1196 				printf("AVB verify failed\n");
1197 
1198 				return -1;
1199 			}
1200 		}
1201 	}
1202 #else
1203 	/*
1204 	 * 2. Load the boot/recovery from the desired "boot" partition.
1205 	 * Determine if this is an AOSP image.
1206 	 */
1207 	if (android_image_load_by_partname(dev_desc,
1208 					   boot_partname,
1209 					   &load_address)) {
1210 		printf("Android image load failed\n");
1211 		return -1;
1212 	}
1213 #endif
1214 
1215 	/* Set Android root variables. */
1216 	env_set_ulong("android_root_devnum", dev_desc->devnum);
1217 	env_set("android_slotsufix", slot_suffix);
1218 
1219 #ifdef CONFIG_FASTBOOT_OEM_UNLOCK
1220 	/* read oem unlock status and attach to bootargs */
1221 	uint8_t unlock = 0;
1222 	TEEC_Result result;
1223 	char oem_unlock[OEM_UNLOCK_ARG_SIZE] = {0};
1224 	result = trusty_read_oem_unlock(&unlock);
1225 	if (result) {
1226 		printf("read oem unlock status with error : 0x%x\n", result);
1227 	} else {
1228 		snprintf(oem_unlock, OEM_UNLOCK_ARG_SIZE, "androidboot.oem_unlocked=%d", unlock);
1229 		env_update("bootargs", oem_unlock);
1230 	}
1231 #endif
1232 
1233 	/* Assemble the command line */
1234 	command_line = android_assemble_cmdline(slot_suffix, mode_cmdline);
1235 	env_update("bootargs", command_line);
1236 
1237 	debug("ANDROID: bootargs: \"%s\"\n", command_line);
1238 
1239 #ifdef CONFIG_SUPPORT_OEM_DTB
1240 	if (android_bootloader_get_fdt(ANDROID_PARTITION_OEM,
1241 				       ANDROID_ARG_FDT_FILENAME)) {
1242 		printf("Can not get the fdt data from oem!\n");
1243 	}
1244 #endif
1245 #ifdef CONFIG_OPTEE_CLIENT
1246 	if (trusty_notify_optee_uboot_end())
1247 		printf("Close optee client failed!\n");
1248 #endif
1249 
1250 #ifdef CONFIG_AMP
1251 	return android_bootloader_boot_kernel(load_address);
1252 #else
1253 	android_bootloader_boot_kernel(load_address);
1254 
1255 	/* TODO: If the kernel doesn't boot mark the selected slot as bad. */
1256 	return -1;
1257 #endif
1258 }
1259 
1260 int android_avb_boot_flow(unsigned long kernel_address)
1261 {
1262 	struct blk_desc *dev_desc;
1263 	disk_partition_t boot_part_info;
1264 	int ret;
1265 
1266 	dev_desc = rockchip_get_bootdev();
1267 	if (!dev_desc) {
1268 		printf("%s: dev_desc is NULL!\n", __func__);
1269 		return -1;
1270 	}
1271 
1272 	/* Load the kernel from the desired "boot" partition. */
1273 	ret = part_get_info_by_name(dev_desc, ANDROID_PARTITION_BOOT,
1274 				    &boot_part_info);
1275 	if (ret < 0) {
1276 		printf("%s: failed to get boot part\n", __func__);
1277 		return ret;
1278 	}
1279 
1280 	ret = android_image_load(dev_desc, &boot_part_info,
1281 				 kernel_address, -1UL);
1282 	if (ret < 0) {
1283 		printf("Android avb boot failed, error %d.\n", ret);
1284 		return ret;
1285 	}
1286 
1287 	android_bootloader_boot_kernel(kernel_address);
1288 
1289 	/* TODO: If the kernel doesn't boot mark the selected slot as bad. */
1290 	return -1;
1291 }
1292 
1293 int android_boot_flow(unsigned long kernel_address)
1294 {
1295 	struct blk_desc *dev_desc;
1296 	disk_partition_t boot_part_info;
1297 	int ret;
1298 
1299 	dev_desc = rockchip_get_bootdev();
1300 	if (!dev_desc) {
1301 		printf("%s: dev_desc is NULL!\n", __func__);
1302 		return -1;
1303 	}
1304 	/* Load the kernel from the desired "boot" partition. */
1305 	ret = part_get_info_by_name(dev_desc, ANDROID_PARTITION_BOOT,
1306 				    &boot_part_info);
1307 	if (ret < 0) {
1308 		printf("%s: failed to get boot part\n", __func__);
1309 		return ret;
1310 	}
1311 
1312 	ret = android_image_load(dev_desc, &boot_part_info, kernel_address,
1313 				 -1UL);
1314 	if (ret < 0)
1315 		return ret;
1316 
1317 	android_bootloader_boot_kernel(kernel_address);
1318 
1319 	/* TODO: If the kernel doesn't boot mark the selected slot as bad. */
1320 	return -1;
1321 }
1322