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