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