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