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