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