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