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