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