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 489 return 0; 490 } 491 492 int android_bootloader_boot_kernel(unsigned long kernel_address) 493 { 494 char *kernel_addr_r = env_get("kernel_addr_r"); 495 char *kernel_addr_c = env_get("kernel_addr_c"); 496 char *fdt_addr = env_get("fdt_addr_r"); 497 char kernel_addr_str[12]; 498 char comp_str[32] = {0}; 499 ulong comp_type; 500 const char *comp_name[] = { 501 [IH_COMP_NONE] = "IMAGE", 502 [IH_COMP_GZIP] = "GZIP", 503 [IH_COMP_BZIP2] = "BZIP2", 504 [IH_COMP_LZMA] = "LZMA", 505 [IH_COMP_LZO] = "LZO", 506 [IH_COMP_LZ4] = "LZ4", 507 [IH_COMP_ZIMAGE]= "ZIMAGE", 508 }; 509 char *bootm_args[] = { 510 kernel_addr_str, kernel_addr_str, fdt_addr, NULL }; 511 512 comp_type = env_get_ulong("os_comp", 10, 0); 513 sprintf(kernel_addr_str, "0x%08lx", kernel_address); 514 515 if (comp_type != IH_COMP_NONE) { 516 if (comp_type == IH_COMP_ZIMAGE && 517 kernel_addr_r && !kernel_addr_c) { 518 kernel_addr_c = kernel_addr_r; 519 kernel_addr_r = __stringify(CONFIG_SYS_SDRAM_BASE); 520 } 521 snprintf(comp_str, 32, "%s%s%s", 522 "(Uncompress to ", kernel_addr_r, ")"); 523 } 524 525 printf("Booting %s kernel at %s%s with fdt at %s...\n\n\n", 526 comp_name[comp_type], 527 comp_type != IH_COMP_NONE ? kernel_addr_c : kernel_addr_r, 528 comp_str, fdt_addr); 529 530 hotkey_run(HK_SYSMEM); 531 532 /* 533 * Check whether there is enough space for uncompress kernel, 534 * Actually, here only gives a sysmem warning message when failed 535 * but never return -1. 536 */ 537 if (sysmem_alloc_uncomp_kernel(kernel_address, 538 simple_strtoul(kernel_addr_r, NULL, 16), 539 comp_type)) 540 return -1; 541 542 return do_bootm_states(NULL, 0, ARRAY_SIZE(bootm_args), bootm_args, 543 BOOTM_STATE_START | 544 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER | 545 BOOTM_STATE_LOADOS | 546 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH 547 BOOTM_STATE_RAMDISK | 548 #endif 549 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO | 550 BOOTM_STATE_OS_GO, &images, 1); 551 } 552 553 static char *strjoin(const char **chunks, char separator) 554 { 555 int len, joined_len = 0; 556 char *ret, *current; 557 const char **p; 558 559 for (p = chunks; *p; p++) 560 joined_len += strlen(*p) + 1; 561 562 if (!joined_len) { 563 ret = malloc(1); 564 if (ret) 565 ret[0] = '\0'; 566 return ret; 567 } 568 569 ret = malloc(joined_len); 570 current = ret; 571 if (!ret) 572 return ret; 573 574 for (p = chunks; *p; p++) { 575 len = strlen(*p); 576 memcpy(current, *p, len); 577 current += len; 578 *current = separator; 579 current++; 580 } 581 /* Replace the last separator by a \0. */ 582 current[-1] = '\0'; 583 return ret; 584 } 585 586 /** android_assemble_cmdline - Assemble the command line to pass to the kernel 587 * @return a newly allocated string 588 */ 589 char *android_assemble_cmdline(const char *slot_suffix, 590 const char *extra_args) 591 { 592 const char *cmdline_chunks[16]; 593 const char **current_chunk = cmdline_chunks; 594 char *env_cmdline, *cmdline, *rootdev_input, *serialno; 595 char *allocated_suffix = NULL; 596 char *allocated_serialno = NULL; 597 char *allocated_rootdev = NULL; 598 unsigned long rootdev_len; 599 600 env_cmdline = env_get("bootargs"); 601 if (env_cmdline) 602 *(current_chunk++) = env_cmdline; 603 604 /* The |slot_suffix| needs to be passed to the kernel to know what 605 * slot to boot from. 606 */ 607 if (slot_suffix) { 608 allocated_suffix = malloc(strlen(ANDROID_ARG_SLOT_SUFFIX) + 609 strlen(slot_suffix) + 1); 610 memset(allocated_suffix, 0, strlen(ANDROID_ARG_SLOT_SUFFIX) 611 + strlen(slot_suffix) + 1); 612 strcpy(allocated_suffix, ANDROID_ARG_SLOT_SUFFIX); 613 strcat(allocated_suffix, slot_suffix); 614 *(current_chunk++) = allocated_suffix; 615 } 616 617 serialno = env_get("serial#"); 618 if (serialno) { 619 allocated_serialno = malloc(strlen(ANDROID_ARG_SERIALNO) + 620 strlen(serialno) + 1); 621 memset(allocated_serialno, 0, strlen(ANDROID_ARG_SERIALNO) + 622 strlen(serialno) + 1); 623 strcpy(allocated_serialno, ANDROID_ARG_SERIALNO); 624 strcat(allocated_serialno, serialno); 625 *(current_chunk++) = allocated_serialno; 626 } 627 628 rootdev_input = env_get("android_rootdev"); 629 if (rootdev_input) { 630 rootdev_len = strlen(ANDROID_ARG_ROOT) + CONFIG_SYS_CBSIZE + 1; 631 allocated_rootdev = malloc(rootdev_len); 632 strcpy(allocated_rootdev, ANDROID_ARG_ROOT); 633 cli_simple_process_macros(rootdev_input, 634 allocated_rootdev + 635 strlen(ANDROID_ARG_ROOT)); 636 /* Make sure that the string is null-terminated since the 637 * previous could not copy to the end of the input string if it 638 * is too big. 639 */ 640 allocated_rootdev[rootdev_len - 1] = '\0'; 641 *(current_chunk++) = allocated_rootdev; 642 } 643 644 if (extra_args) 645 *(current_chunk++) = extra_args; 646 647 *(current_chunk++) = NULL; 648 cmdline = strjoin(cmdline_chunks, ' '); 649 free(allocated_suffix); 650 free(allocated_rootdev); 651 return cmdline; 652 } 653 654 #ifdef CONFIG_ANDROID_AVB 655 static void slot_set_unbootable(AvbABSlotData* slot) 656 { 657 slot->priority = 0; 658 slot->tries_remaining = 0; 659 slot->successful_boot = 0; 660 } 661 662 static AvbSlotVerifyResult android_slot_verify(char *boot_partname, 663 unsigned long *android_load_address, 664 char *slot_suffix) 665 { 666 const char *requested_partitions[1] = {NULL}; 667 uint8_t unlocked = true; 668 AvbOps *ops; 669 AvbSlotVerifyFlags flags; 670 AvbSlotVerifyData *slot_data[1] = {NULL}; 671 AvbSlotVerifyResult verify_result; 672 AvbABData ab_data, ab_data_orig; 673 size_t slot_index_to_boot = 0; 674 char verify_state[38] = {0}; 675 char can_boot = 1; 676 unsigned long load_address = *android_load_address; 677 struct andr_img_hdr *hdr; 678 679 requested_partitions[0] = boot_partname; 680 ops = avb_ops_user_new(); 681 if (ops == NULL) { 682 printf("avb_ops_user_new() failed!\n"); 683 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 684 } 685 686 if (ops->read_is_device_unlocked(ops, (bool *)&unlocked) != AVB_IO_RESULT_OK) 687 printf("Error determining whether device is unlocked.\n"); 688 689 printf("read_is_device_unlocked() ops returned that device is %s\n", 690 (unlocked & LOCK_MASK)? "UNLOCKED" : "LOCKED"); 691 692 flags = AVB_SLOT_VERIFY_FLAGS_NONE; 693 if (unlocked & LOCK_MASK) 694 flags |= AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR; 695 696 if(load_metadata(ops->ab_ops, &ab_data, &ab_data_orig)) { 697 printf("Can not load metadata\n"); 698 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 699 } 700 701 if (!strncmp(slot_suffix, "_a", 2)) 702 slot_index_to_boot = 0; 703 else if (!strncmp(slot_suffix, "_b", 2)) 704 slot_index_to_boot = 1; 705 else 706 slot_index_to_boot = 0; 707 708 verify_result = 709 avb_slot_verify(ops, 710 requested_partitions, 711 slot_suffix, 712 flags, 713 AVB_HASHTREE_ERROR_MODE_RESTART, 714 &slot_data[0]); 715 716 strcat(verify_state, ANDROID_VERIFY_STATE); 717 switch (verify_result) { 718 case AVB_SLOT_VERIFY_RESULT_OK: 719 if (unlocked & LOCK_MASK) 720 strcat(verify_state, "orange"); 721 else 722 strcat(verify_state, "green"); 723 break; 724 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED: 725 if (unlocked & LOCK_MASK) 726 strcat(verify_state, "orange"); 727 else 728 strcat(verify_state, "yellow"); 729 break; 730 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM: 731 case AVB_SLOT_VERIFY_RESULT_ERROR_IO: 732 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA: 733 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION: 734 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION: 735 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX: 736 default: 737 if (unlocked & LOCK_MASK) 738 strcat(verify_state, "orange"); 739 else 740 strcat(verify_state, "red"); 741 break; 742 } 743 744 if (!slot_data[0]) { 745 can_boot = 0; 746 goto out; 747 } 748 749 if (verify_result == AVB_SLOT_VERIFY_RESULT_OK || 750 verify_result == AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED || 751 (unlocked & LOCK_MASK)) { 752 int len = 0; 753 char *bootargs, *newbootargs; 754 755 if (*slot_data[0]->cmdline) { 756 debug("Kernel command line: %s\n", slot_data[0]->cmdline); 757 len += strlen(slot_data[0]->cmdline); 758 } 759 760 bootargs = env_get("bootargs"); 761 if (bootargs) 762 len += strlen(bootargs); 763 764 newbootargs = malloc(len + 2); 765 766 if (!newbootargs) { 767 puts("Error: malloc in android_slot_verify failed!\n"); 768 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 769 } 770 *newbootargs = '\0'; 771 772 if (bootargs) { 773 strcpy(newbootargs, bootargs); 774 strcat(newbootargs, " "); 775 } 776 if (*slot_data[0]->cmdline) 777 strcat(newbootargs, slot_data[0]->cmdline); 778 env_set("bootargs", newbootargs); 779 780 /* Reserve page_size */ 781 hdr = (void *)slot_data[0]->loaded_partitions->data; 782 load_address -= hdr->page_size; 783 if (android_image_memcpy_separate(hdr, &load_address)) { 784 printf("Failed to separate copy android image\n"); 785 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 786 } 787 *android_load_address = load_address; 788 } else { 789 slot_set_unbootable(&ab_data.slots[slot_index_to_boot]); 790 } 791 792 out: 793 #if defined(CONFIG_ANDROID_AB) && !defined(CONFIG_ANDROID_AVB) 794 /* 795 * In ab & avb process, the tries_remaining minus one in function 796 * android_slot_verify, shield this function here. 797 */ 798 /* ... and decrement tries remaining, if applicable. */ 799 if (!ab_data.slots[slot_index_to_boot].successful_boot && 800 ab_data.slots[slot_index_to_boot].tries_remaining > 0) { 801 ab_data.slots[slot_index_to_boot].tries_remaining -= 1; 802 } 803 #endif 804 env_update("bootargs", verify_state); 805 if (save_metadata_if_changed(ops->ab_ops, &ab_data, &ab_data_orig)) { 806 printf("Can not save metadata\n"); 807 verify_result = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 808 } 809 810 if (slot_data[0] != NULL) 811 avb_slot_verify_data_free(slot_data[0]); 812 813 if ((unlocked & LOCK_MASK) && can_boot) 814 return 0; 815 else 816 return verify_result; 817 } 818 #endif 819 820 #if defined(CONFIG_CMD_DTIMG) && defined(CONFIG_OF_LIBFDT_OVERLAY) 821 822 /* 823 * Default return index 0. 824 */ 825 __weak int board_select_fdt_index(ulong dt_table_hdr) 826 { 827 /* 828 * User can use "dt_for_each_entry(entry, hdr, idx)" to iterate 829 * over all dt entry of DT image and pick up which they want. 830 * 831 * Example: 832 * struct dt_table_entry *entry; 833 * int index; 834 * 835 * dt_for_each_entry(entry, dt_table_hdr, index) { 836 * 837 * .... (use entry) 838 * } 839 * 840 * return index; 841 */ 842 return 0; 843 } 844 845 static int android_get_dtbo(ulong *fdt_dtbo, 846 const struct andr_img_hdr *hdr, 847 int *index, int boot_mode) 848 { 849 struct dt_table_header *dt_hdr = NULL; 850 struct blk_desc *dev_desc; 851 const char *part_name; 852 disk_partition_t part_info; 853 u32 blk_offset, blk_cnt; 854 void *buf; 855 ulong e_addr; 856 u32 e_size; 857 int e_idx; 858 int ret; 859 860 /* Get partition according to boot mode */ 861 if (boot_mode == BOOT_MODE_RECOVERY) 862 part_name = PART_RECOVERY; 863 else 864 part_name = PART_DTBO; 865 866 /* Get partition info */ 867 dev_desc = rockchip_get_bootdev(); 868 if (!dev_desc) { 869 printf("%s: dev_desc is NULL!\n", __func__); 870 return -ENODEV; 871 } 872 873 ret = part_get_info_by_name(dev_desc, part_name, &part_info); 874 if (ret < 0) { 875 printf("%s: failed to get %s part info, ret=%d\n", 876 __func__, part_name, ret); 877 return ret; 878 } 879 880 /* Check dt table header */ 881 if (!strcmp(part_name, PART_RECOVERY)) 882 blk_offset = part_info.start + 883 (hdr->recovery_dtbo_offset / part_info.blksz); 884 else 885 blk_offset = part_info.start; 886 887 dt_hdr = memalign(ARCH_DMA_MINALIGN, part_info.blksz); 888 if (!dt_hdr) { 889 printf("%s: out of memory for dt header!\n", __func__); 890 return -ENOMEM; 891 } 892 893 ret = blk_dread(dev_desc, blk_offset, 1, dt_hdr); 894 if (ret != 1) { 895 printf("%s: failed to read dt table header\n", 896 __func__); 897 goto out1; 898 } 899 900 if (!android_dt_check_header((ulong)dt_hdr)) { 901 printf("%s: Error: invalid dt table header: 0x%x\n", 902 __func__, dt_hdr->magic); 903 ret = -EINVAL; 904 goto out1; 905 } 906 907 #ifdef DEBUG 908 android_dt_print_contents((ulong)dt_hdr); 909 #endif 910 911 blk_cnt = DIV_ROUND_UP(fdt32_to_cpu(dt_hdr->total_size), 912 part_info.blksz); 913 /* Read all DT Image */ 914 buf = memalign(ARCH_DMA_MINALIGN, part_info.blksz * blk_cnt); 915 if (!buf) { 916 printf("%s: out of memory for %s part!\n", __func__, part_name); 917 ret = -ENOMEM; 918 goto out1; 919 } 920 921 ret = blk_dread(dev_desc, blk_offset, blk_cnt, buf); 922 if (ret != blk_cnt) { 923 printf("%s: failed to read dtbo, blk_cnt=%d, ret=%d\n", 924 __func__, blk_cnt, ret); 925 goto out2; 926 } 927 928 e_idx = board_select_fdt_index((ulong)buf); 929 if (e_idx < 0) { 930 printf("%s: failed to select board fdt index\n", __func__); 931 ret = -EINVAL; 932 goto out2; 933 } 934 935 ret = android_dt_get_fdt_by_index((ulong)buf, e_idx, &e_addr, &e_size); 936 if (!ret) { 937 printf("%s: failed to get fdt, index=%d\n", __func__, e_idx); 938 ret = -EINVAL; 939 goto out2; 940 } 941 942 if (fdt_dtbo) 943 *fdt_dtbo = e_addr; 944 if (index) 945 *index = e_idx; 946 947 free(dt_hdr); 948 debug("ANDROID: Loading dt entry to 0x%lx size 0x%x idx %d from \"%s\" part\n", 949 e_addr, e_size, e_idx, part_name); 950 951 return 0; 952 953 out2: 954 free(buf); 955 out1: 956 free(dt_hdr); 957 958 return ret; 959 } 960 961 int android_fdt_overlay_apply(void *fdt_addr) 962 { 963 struct andr_img_hdr *hdr; 964 struct blk_desc *dev_desc; 965 const char *part_name; 966 disk_partition_t part_info; 967 char buf[32] = {0}; 968 u32 blk_cnt; 969 ulong fdt_dtbo = -1; 970 int boot_mode; 971 int index = -1; 972 int ret; 973 974 boot_mode = rockchip_get_boot_mode(); 975 #ifdef CONFIG_ANDROID_AB 976 if (boot_mode == BOOT_MODE_RECOVERY) 977 boot_mode = BOOT_MODE_NORMAL; 978 #endif 979 if (boot_mode == BOOT_MODE_RECOVERY) 980 part_name = PART_RECOVERY; 981 else 982 part_name = PART_BOOT; 983 984 /* Get partition info */ 985 dev_desc = rockchip_get_bootdev(); 986 if (!dev_desc) { 987 printf("%s: dev_desc is NULL!\n", __func__); 988 return -ENODEV; 989 } 990 991 ret = part_get_info_by_name(dev_desc, part_name, &part_info); 992 if (ret < 0) { 993 printf("%s: failed to get %s part info, ret=%d\n", 994 __func__, part_name, ret); 995 return ret; 996 } 997 998 blk_cnt = DIV_ROUND_UP(sizeof(*hdr), part_info.blksz); 999 hdr = memalign(ARCH_DMA_MINALIGN, part_info.blksz * blk_cnt); 1000 if (!hdr) { 1001 printf("%s: out of memory!\n", __func__); 1002 return -ENOMEM; 1003 } 1004 1005 ret = blk_dread(dev_desc, part_info.start, blk_cnt, hdr); 1006 if (ret != blk_cnt) { 1007 printf("%s: failed to read %s hdr!\n", __func__, part_name); 1008 goto out; 1009 } 1010 1011 #ifdef DEBUG 1012 android_print_contents(hdr); 1013 #endif 1014 1015 if (android_image_check_header(hdr)) 1016 return -EINVAL; 1017 1018 /* Check header version */ 1019 if (!hdr->header_version) { 1020 printf("Android header version 0\n"); 1021 ret = -EINVAL; 1022 goto out; 1023 } 1024 1025 ret = android_get_dtbo(&fdt_dtbo, (void *)hdr, &index, boot_mode); 1026 if (!ret) { 1027 phys_size_t fdt_size; 1028 /* Must incease size before overlay */ 1029 fdt_size = fdt_totalsize((void *)fdt_addr) + 1030 fdt_totalsize((void *)fdt_dtbo); 1031 if (sysmem_free((phys_addr_t)fdt_addr)) 1032 goto out; 1033 1034 if (!sysmem_alloc_base(MEM_FDT_DTBO, 1035 (phys_addr_t)fdt_addr, 1036 fdt_size + CONFIG_SYS_FDT_PAD)) 1037 goto out; 1038 fdt_increase_size(fdt_addr, fdt_totalsize((void *)fdt_dtbo)); 1039 ret = fdt_overlay_apply(fdt_addr, (void *)fdt_dtbo); 1040 if (!ret) { 1041 snprintf(buf, 32, "%s%d", "androidboot.dtbo_idx=", index); 1042 env_update("bootargs", buf); 1043 printf("ANDROID: fdt overlay OK\n"); 1044 } else { 1045 printf("ANDROID: fdt overlay failed, ret=%d\n", ret); 1046 } 1047 } 1048 1049 out: 1050 free(hdr); 1051 1052 return 0; 1053 } 1054 #endif 1055 1056 int android_image_load_by_partname(struct blk_desc *dev_desc, 1057 const char *boot_partname, 1058 unsigned long *load_address) 1059 { 1060 disk_partition_t boot_part; 1061 int ret, part_num; 1062 1063 part_num = part_get_info_by_name(dev_desc, boot_partname, &boot_part); 1064 if (part_num < 0) { 1065 printf("%s: Can't find part: %s\n", __func__, boot_partname); 1066 return -1; 1067 } 1068 debug("ANDROID: Loading kernel from \"%s\", partition %d.\n", 1069 boot_part.name, part_num); 1070 1071 ret = android_image_load(dev_desc, &boot_part, *load_address, -1UL); 1072 if (ret < 0) { 1073 debug("%s: %s part load fail, ret=%d\n", 1074 __func__, boot_part.name, ret); 1075 return ret; 1076 } 1077 *load_address = ret; 1078 1079 return 0; 1080 } 1081 1082 int android_bootloader_boot_flow(struct blk_desc *dev_desc, 1083 unsigned long load_address) 1084 { 1085 enum android_boot_mode mode = ANDROID_BOOT_MODE_NORMAL; 1086 disk_partition_t misc_part_info; 1087 int part_num; 1088 char *command_line; 1089 char slot_suffix[3] = {0}; 1090 const char *mode_cmdline = NULL; 1091 char *boot_partname = ANDROID_PARTITION_BOOT; 1092 1093 /* 1094 * 1. Load MISC partition and determine the boot mode 1095 * clear its value for the next boot if needed. 1096 */ 1097 part_num = part_get_info_by_name(dev_desc, ANDROID_PARTITION_MISC, 1098 &misc_part_info); 1099 if (part_num < 0) { 1100 printf("Could not find misc partition\n"); 1101 } else { 1102 #ifdef CONFIG_ANDROID_KEYMASTER_CA 1103 /* load attestation key from misc partition. */ 1104 load_attestation_key(dev_desc, &misc_part_info); 1105 #endif 1106 1107 mode = android_bootloader_load_and_clear_mode(dev_desc, 1108 &misc_part_info); 1109 #ifdef CONFIG_RKIMG_BOOTLOADER 1110 if (mode == ANDROID_BOOT_MODE_NORMAL) { 1111 if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) 1112 mode = ANDROID_BOOT_MODE_RECOVERY; 1113 } 1114 #endif 1115 } 1116 1117 printf("ANDROID: reboot reason: \"%s\"\n", android_boot_mode_str(mode)); 1118 1119 /* Get current slot_suffix */ 1120 if (decrease_tries_if_android_ab(slot_suffix)) 1121 return -1; 1122 1123 switch (mode) { 1124 case ANDROID_BOOT_MODE_NORMAL: 1125 /* In normal mode, we load the kernel from "boot" but append 1126 * "skip_initramfs" to the cmdline to make it ignore the 1127 * recovery initramfs in the boot partition. 1128 */ 1129 #ifdef CONFIG_ANDROID_AB 1130 /* In A/B, the recovery image is built as boot.img, containing the 1131 * recovery's ramdisk. Previously, bootloader used the skip_initramfs 1132 * kernel command line parameter to decide which mode to boot into. 1133 * For Android >=10 and with dynamic partition support, the bootloader 1134 * MUST NOT pass skip_initramfs to the kernel command-line. 1135 * Instead, bootloader should pass androidboot.force_normal_boot=1 1136 * and then Android's first-stage init in ramdisk 1137 * will skip recovery and boot normal Android. 1138 */ 1139 if (is_support_dynamic_partition(dev_desc)) { 1140 mode_cmdline = "androidboot.force_normal_boot=1"; 1141 } else { 1142 mode_cmdline = "skip_initramfs"; 1143 } 1144 #endif 1145 break; 1146 case ANDROID_BOOT_MODE_RECOVERY: 1147 /* In recovery mode we still boot the kernel from "boot" but 1148 * don't skip the initramfs so it boots to recovery. 1149 */ 1150 #ifndef CONFIG_ANDROID_AB 1151 boot_partname = ANDROID_PARTITION_RECOVERY; 1152 #endif 1153 break; 1154 case ANDROID_BOOT_MODE_BOOTLOADER: 1155 /* Bootloader mode enters fastboot. If this operation fails we 1156 * simply return since we can't recover from this situation by 1157 * switching to another slot. 1158 */ 1159 return android_bootloader_boot_bootloader(); 1160 } 1161 1162 #ifdef CONFIG_ANDROID_AVB 1163 uint8_t vboot_flag = 0; 1164 disk_partition_t vbmeta_part_info; 1165 1166 if (trusty_read_vbootkey_enable_flag(&vboot_flag)) { 1167 printf("Can't read vboot flag\n"); 1168 return -1; 1169 } 1170 1171 if (vboot_flag) { 1172 printf("Vboot=1, SecureBoot enabled, AVB verify\n"); 1173 if (android_slot_verify(boot_partname, &load_address, 1174 slot_suffix)) { 1175 printf("AVB verify failed\n"); 1176 reset_cpu_if_android_ab(); 1177 1178 return -1; 1179 } 1180 } else { 1181 part_num = part_get_info_by_name(dev_desc, 1182 ANDROID_PARTITION_VBMETA, 1183 &vbmeta_part_info); 1184 if (part_num < 0) { 1185 printf("Not AVB images, AVB skip\n"); 1186 env_update("bootargs", 1187 "androidboot.verifiedbootstate=orange"); 1188 if (android_image_load_by_partname(dev_desc, 1189 boot_partname, 1190 &load_address)) { 1191 printf("Android image load failed\n"); 1192 return -1; 1193 } 1194 } else { 1195 printf("Vboot=0, AVB images, AVB verify\n"); 1196 if (android_slot_verify(boot_partname, &load_address, 1197 slot_suffix)) { 1198 printf("AVB verify failed\n"); 1199 reset_cpu_if_android_ab(); 1200 1201 return -1; 1202 } 1203 } 1204 } 1205 #else 1206 /* 1207 * 2. Load the boot/recovery from the desired "boot" partition. 1208 * Determine if this is an AOSP image. 1209 */ 1210 if (android_image_load_by_partname(dev_desc, 1211 boot_partname, 1212 &load_address)) { 1213 printf("Android image load failed\n"); 1214 return -1; 1215 } 1216 #endif 1217 update_root_uuid_if_android_ab(); 1218 1219 /* Set Android root variables. */ 1220 env_set_ulong("android_root_devnum", dev_desc->devnum); 1221 env_set("android_slotsufix", slot_suffix); 1222 1223 #ifdef CONFIG_FASTBOOT_OEM_UNLOCK 1224 /* read oem unlock status and attach to bootargs */ 1225 uint8_t unlock = 0; 1226 TEEC_Result result; 1227 char oem_unlock[OEM_UNLOCK_ARG_SIZE] = {0}; 1228 result = trusty_read_oem_unlock(&unlock); 1229 if (result) { 1230 printf("read oem unlock status with error : 0x%x\n", result); 1231 } else { 1232 snprintf(oem_unlock, OEM_UNLOCK_ARG_SIZE, "androidboot.oem_unlocked=%d", unlock); 1233 env_update("bootargs", oem_unlock); 1234 } 1235 #endif 1236 1237 /* Assemble the command line */ 1238 command_line = android_assemble_cmdline(slot_suffix, mode_cmdline); 1239 env_update("bootargs", command_line); 1240 1241 debug("ANDROID: bootargs: \"%s\"\n", command_line); 1242 1243 #ifdef CONFIG_SUPPORT_OEM_DTB 1244 if (android_bootloader_get_fdt(ANDROID_PARTITION_OEM, 1245 ANDROID_ARG_FDT_FILENAME)) { 1246 printf("Can not get the fdt data from oem!\n"); 1247 } 1248 #endif 1249 #ifdef CONFIG_OPTEE_CLIENT 1250 if (trusty_notify_optee_uboot_end()) 1251 printf("Close optee client failed!\n"); 1252 #endif 1253 android_bootloader_boot_kernel(load_address); 1254 1255 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 1256 return -1; 1257 } 1258 1259 int android_avb_boot_flow(unsigned long kernel_address) 1260 { 1261 struct blk_desc *dev_desc; 1262 disk_partition_t boot_part_info; 1263 int ret; 1264 1265 dev_desc = rockchip_get_bootdev(); 1266 if (!dev_desc) { 1267 printf("%s: dev_desc is NULL!\n", __func__); 1268 return -1; 1269 } 1270 1271 /* Load the kernel from the desired "boot" partition. */ 1272 ret = part_get_info_by_name(dev_desc, ANDROID_PARTITION_BOOT, 1273 &boot_part_info); 1274 if (ret < 0) { 1275 printf("%s: failed to get boot part\n", __func__); 1276 return ret; 1277 } 1278 1279 ret = android_image_load(dev_desc, &boot_part_info, 1280 kernel_address, -1UL); 1281 if (ret < 0) { 1282 printf("Android avb boot failed, error %d.\n", ret); 1283 return ret; 1284 } 1285 1286 android_bootloader_boot_kernel(kernel_address); 1287 1288 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 1289 return -1; 1290 } 1291 1292 int android_boot_flow(unsigned long kernel_address) 1293 { 1294 struct blk_desc *dev_desc; 1295 disk_partition_t boot_part_info; 1296 int ret; 1297 1298 dev_desc = rockchip_get_bootdev(); 1299 if (!dev_desc) { 1300 printf("%s: dev_desc is NULL!\n", __func__); 1301 return -1; 1302 } 1303 /* Load the kernel from the desired "boot" partition. */ 1304 ret = part_get_info_by_name(dev_desc, ANDROID_PARTITION_BOOT, 1305 &boot_part_info); 1306 if (ret < 0) { 1307 printf("%s: failed to get boot part\n", __func__); 1308 return ret; 1309 } 1310 1311 ret = android_image_load(dev_desc, &boot_part_info, kernel_address, 1312 -1UL); 1313 if (ret < 0) 1314 return ret; 1315 1316 android_bootloader_boot_kernel(kernel_address); 1317 1318 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 1319 return -1; 1320 } 1321