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