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