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