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