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 *android_load_address = load_address; 782 783 #ifdef CONFIG_ANDROID_BOOT_IMAGE_SEPARATE 784 android_image_memcpy_separate(hdr, (void *)load_address); 785 #else 786 memcpy((uint8_t *)load_address, 787 slot_data[0]->loaded_partitions->data, 788 slot_data[0]->loaded_partitions->data_size); 789 #endif 790 } else { 791 slot_set_unbootable(&ab_data.slots[slot_index_to_boot]); 792 } 793 794 out: 795 #if defined(CONFIG_ANDROID_AB) && !defined(CONFIG_ANDROID_AVB) 796 /* 797 * In ab & avb process, the tries_remaining minus one in function 798 * android_slot_verify, shield this function here. 799 */ 800 /* ... and decrement tries remaining, if applicable. */ 801 if (!ab_data.slots[slot_index_to_boot].successful_boot && 802 ab_data.slots[slot_index_to_boot].tries_remaining > 0) { 803 ab_data.slots[slot_index_to_boot].tries_remaining -= 1; 804 } 805 #endif 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, int boot_mode) 850 { 851 struct dt_table_header *dt_hdr = NULL; 852 struct blk_desc *dev_desc; 853 const char *part_name; 854 disk_partition_t part_info; 855 u32 blk_offset, blk_cnt; 856 void *buf; 857 ulong e_addr; 858 u32 e_size; 859 int e_idx; 860 int ret; 861 862 /* Get partition according to boot mode */ 863 if (boot_mode == BOOT_MODE_RECOVERY) 864 part_name = PART_RECOVERY; 865 else 866 part_name = PART_DTBO; 867 868 /* Get partition info */ 869 dev_desc = rockchip_get_bootdev(); 870 if (!dev_desc) { 871 printf("%s: dev_desc is NULL!\n", __func__); 872 return -ENODEV; 873 } 874 875 ret = part_get_info_by_name(dev_desc, part_name, &part_info); 876 if (ret < 0) { 877 printf("%s: failed to get %s part info, ret=%d\n", 878 __func__, part_name, ret); 879 return ret; 880 } 881 882 /* Check dt table header */ 883 if (!strcmp(part_name, PART_RECOVERY)) 884 blk_offset = part_info.start + 885 (hdr->recovery_dtbo_offset / part_info.blksz); 886 else 887 blk_offset = part_info.start; 888 889 dt_hdr = memalign(ARCH_DMA_MINALIGN, part_info.blksz); 890 if (!dt_hdr) { 891 printf("%s: out of memory for dt header!\n", __func__); 892 return -ENOMEM; 893 } 894 895 ret = blk_dread(dev_desc, blk_offset, 1, dt_hdr); 896 if (ret != 1) { 897 printf("%s: failed to read dt table header\n", 898 __func__); 899 goto out1; 900 } 901 902 if (!android_dt_check_header((ulong)dt_hdr)) { 903 printf("%s: Error: invalid dt table header: 0x%x\n", 904 __func__, dt_hdr->magic); 905 ret = -EINVAL; 906 goto out1; 907 } 908 909 #ifdef DEBUG 910 android_dt_print_contents((ulong)dt_hdr); 911 #endif 912 913 blk_cnt = DIV_ROUND_UP(fdt32_to_cpu(dt_hdr->total_size), 914 part_info.blksz); 915 /* Read all DT Image */ 916 buf = memalign(ARCH_DMA_MINALIGN, part_info.blksz * blk_cnt); 917 if (!buf) { 918 printf("%s: out of memory for %s part!\n", __func__, part_name); 919 ret = -ENOMEM; 920 goto out1; 921 } 922 923 ret = blk_dread(dev_desc, blk_offset, blk_cnt, buf); 924 if (ret != blk_cnt) { 925 printf("%s: failed to read dtbo, blk_cnt=%d, ret=%d\n", 926 __func__, blk_cnt, ret); 927 goto out2; 928 } 929 930 e_idx = board_select_fdt_index((ulong)buf); 931 if (e_idx < 0) { 932 printf("%s: failed to select board fdt index\n", __func__); 933 ret = -EINVAL; 934 goto out2; 935 } 936 937 ret = android_dt_get_fdt_by_index((ulong)buf, e_idx, &e_addr, &e_size); 938 if (!ret) { 939 printf("%s: failed to get fdt, index=%d\n", __func__, e_idx); 940 ret = -EINVAL; 941 goto out2; 942 } 943 944 if (fdt_dtbo) 945 *fdt_dtbo = e_addr; 946 if (index) 947 *index = e_idx; 948 949 free(dt_hdr); 950 debug("ANDROID: Loading dt entry to 0x%lx size 0x%x idx %d from \"%s\" part\n", 951 e_addr, e_size, e_idx, part_name); 952 953 return 0; 954 955 out2: 956 free(buf); 957 out1: 958 free(dt_hdr); 959 960 return ret; 961 } 962 963 int android_fdt_overlay_apply(void *fdt_addr) 964 { 965 struct andr_img_hdr *hdr; 966 struct blk_desc *dev_desc; 967 const char *part_name; 968 disk_partition_t part_info; 969 char buf[32] = {0}; 970 u32 blk_cnt; 971 ulong fdt_dtbo = -1; 972 int boot_mode; 973 int index = -1; 974 int ret; 975 976 boot_mode = rockchip_get_boot_mode(); 977 #ifdef CONFIG_ANDROID_AB 978 if (boot_mode == BOOT_MODE_RECOVERY) 979 boot_mode = BOOT_MODE_NORMAL; 980 #endif 981 if (boot_mode == BOOT_MODE_RECOVERY) 982 part_name = PART_RECOVERY; 983 else 984 part_name = PART_BOOT; 985 986 /* Get partition info */ 987 dev_desc = rockchip_get_bootdev(); 988 if (!dev_desc) { 989 printf("%s: dev_desc is NULL!\n", __func__); 990 return -ENODEV; 991 } 992 993 ret = part_get_info_by_name(dev_desc, part_name, &part_info); 994 if (ret < 0) { 995 printf("%s: failed to get %s part info, ret=%d\n", 996 __func__, part_name, ret); 997 return ret; 998 } 999 1000 blk_cnt = DIV_ROUND_UP(sizeof(*hdr), part_info.blksz); 1001 hdr = memalign(ARCH_DMA_MINALIGN, part_info.blksz * blk_cnt); 1002 if (!hdr) { 1003 printf("%s: out of memory!\n", __func__); 1004 return -ENOMEM; 1005 } 1006 1007 ret = blk_dread(dev_desc, part_info.start, blk_cnt, hdr); 1008 if (ret != blk_cnt) { 1009 printf("%s: failed to read %s hdr!\n", __func__, part_name); 1010 goto out; 1011 } 1012 1013 #ifdef DEBUG 1014 android_print_contents(hdr); 1015 #endif 1016 1017 if (android_image_check_header(hdr)) 1018 return -EINVAL; 1019 1020 /* Check header version */ 1021 if (!hdr->header_version) { 1022 printf("Android header version 0\n"); 1023 ret = -EINVAL; 1024 goto out; 1025 } 1026 1027 ret = android_get_dtbo(&fdt_dtbo, (void *)hdr, &index, boot_mode); 1028 if (!ret) { 1029 phys_size_t fdt_size; 1030 /* Must incease size before overlay */ 1031 fdt_size = fdt_totalsize((void *)fdt_addr) + 1032 fdt_totalsize((void *)fdt_dtbo); 1033 if (sysmem_free((phys_addr_t)fdt_addr)) 1034 goto out; 1035 1036 if (!sysmem_alloc_base(MEMBLK_ID_FDT_DTBO, 1037 (phys_addr_t)fdt_addr, 1038 fdt_size + CONFIG_SYS_FDT_PAD)) 1039 goto out; 1040 fdt_increase_size(fdt_addr, fdt_totalsize((void *)fdt_dtbo)); 1041 ret = fdt_overlay_apply(fdt_addr, (void *)fdt_dtbo); 1042 if (!ret) { 1043 snprintf(buf, 32, "%s%d", "androidboot.dtbo_idx=", index); 1044 env_update("bootargs", buf); 1045 printf("ANDROID: fdt overlay OK\n"); 1046 } else { 1047 printf("ANDROID: fdt overlay failed, ret=%d\n", ret); 1048 } 1049 } 1050 1051 out: 1052 free(hdr); 1053 1054 return 0; 1055 } 1056 #endif 1057 1058 static int load_android_image(struct blk_desc *dev_desc, 1059 char *boot_partname, 1060 unsigned long *load_address) 1061 { 1062 disk_partition_t boot_part; 1063 int ret, part_num; 1064 1065 part_num = part_get_info_by_name(dev_desc, boot_partname, &boot_part); 1066 if (part_num < 0) { 1067 printf("%s: Can't find part: %s\n", __func__, boot_partname); 1068 return -1; 1069 } 1070 debug("ANDROID: Loading kernel from \"%s\", partition %d.\n", 1071 boot_part.name, part_num); 1072 1073 ret = android_image_load(dev_desc, &boot_part, *load_address, -1UL); 1074 if (ret < 0) { 1075 debug("%s: %s part load fail, ret=%d\n", 1076 __func__, boot_part.name, ret); 1077 return ret; 1078 } 1079 *load_address = ret; 1080 1081 return 0; 1082 } 1083 1084 int android_bootloader_boot_flow(struct blk_desc *dev_desc, 1085 unsigned long load_address) 1086 { 1087 enum android_boot_mode mode = ANDROID_BOOT_MODE_NORMAL; 1088 disk_partition_t misc_part_info; 1089 int part_num; 1090 int ret; 1091 char *command_line; 1092 char slot_suffix[3] = {0}; 1093 const char *mode_cmdline = NULL; 1094 char *boot_partname = ANDROID_PARTITION_BOOT; 1095 ulong fdt_addr; 1096 1097 /* 1098 * 1. Load MISC partition and determine the boot mode 1099 * clear its value for the next boot if needed. 1100 */ 1101 part_num = part_get_info_by_name(dev_desc, ANDROID_PARTITION_MISC, 1102 &misc_part_info); 1103 if (part_num < 0) { 1104 printf("Could not find misc partition\n"); 1105 } else { 1106 #ifdef CONFIG_ANDROID_KEYMASTER_CA 1107 /* load attestation key from misc partition. */ 1108 load_attestation_key(dev_desc, &misc_part_info); 1109 #endif 1110 1111 mode = android_bootloader_load_and_clear_mode(dev_desc, 1112 &misc_part_info); 1113 #ifdef CONFIG_RKIMG_BOOTLOADER 1114 if (mode == ANDROID_BOOT_MODE_NORMAL) { 1115 if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) 1116 mode = ANDROID_BOOT_MODE_RECOVERY; 1117 } 1118 #endif 1119 } 1120 1121 printf("ANDROID: reboot reason: \"%s\"\n", android_boot_mode_str(mode)); 1122 1123 /* Get current slot_suffix */ 1124 if (decrease_tries_if_android_ab(slot_suffix)) 1125 return -1; 1126 1127 switch (mode) { 1128 case ANDROID_BOOT_MODE_NORMAL: 1129 /* In normal mode, we load the kernel from "boot" but append 1130 * "skip_initramfs" to the cmdline to make it ignore the 1131 * recovery initramfs in the boot partition. 1132 */ 1133 #ifdef CONFIG_ANDROID_AB 1134 /* In A/B, the recovery image is built as boot.img, containing the 1135 * recovery's ramdisk. Previously, bootloader used the skip_initramfs 1136 * kernel command line parameter to decide which mode to boot into. 1137 * For Android >=10 and with dynamic partition support, the bootloader 1138 * MUST NOT pass skip_initramfs to the kernel command-line. 1139 * Instead, bootloader should pass androidboot.force_normal_boot=1 1140 * and then Android's first-stage init in ramdisk 1141 * will skip recovery and boot normal Android. 1142 */ 1143 if (is_support_dynamic_partition(dev_desc)) { 1144 mode_cmdline = "androidboot.force_normal_boot=1"; 1145 } else { 1146 mode_cmdline = "skip_initramfs"; 1147 } 1148 #endif 1149 break; 1150 case ANDROID_BOOT_MODE_RECOVERY: 1151 /* In recovery mode we still boot the kernel from "boot" but 1152 * don't skip the initramfs so it boots to recovery. 1153 */ 1154 #ifndef CONFIG_ANDROID_AB 1155 boot_partname = ANDROID_PARTITION_RECOVERY; 1156 #endif 1157 break; 1158 case ANDROID_BOOT_MODE_BOOTLOADER: 1159 /* Bootloader mode enters fastboot. If this operation fails we 1160 * simply return since we can't recover from this situation by 1161 * switching to another slot. 1162 */ 1163 return android_bootloader_boot_bootloader(); 1164 } 1165 1166 #ifdef CONFIG_ANDROID_AVB 1167 uint8_t vboot_flag = 0; 1168 disk_partition_t vbmeta_part_info; 1169 1170 if (trusty_read_vbootkey_enable_flag(&vboot_flag)) { 1171 printf("Can't read vboot flag\n"); 1172 return -1; 1173 } 1174 1175 if (vboot_flag) { 1176 printf("Vboot=1, SecureBoot enabled, AVB verify\n"); 1177 if (android_slot_verify(boot_partname, &load_address, 1178 slot_suffix)) { 1179 printf("AVB verify failed\n"); 1180 reset_cpu_if_android_ab(); 1181 1182 return -1; 1183 } 1184 } else { 1185 part_num = part_get_info_by_name(dev_desc, 1186 ANDROID_PARTITION_VBMETA, 1187 &vbmeta_part_info); 1188 if (part_num < 0) { 1189 printf("Not AVB images, AVB skip\n"); 1190 env_update("bootargs", 1191 "androidboot.verifiedbootstate=orange"); 1192 if (load_android_image(dev_desc, boot_partname, 1193 &load_address)) { 1194 printf("Android image load failed\n"); 1195 return -1; 1196 } 1197 } else { 1198 printf("Vboot=0, AVB images, AVB verify\n"); 1199 if (android_slot_verify(boot_partname, &load_address, 1200 slot_suffix)) { 1201 printf("AVB verify failed\n"); 1202 reset_cpu_if_android_ab(); 1203 1204 return -1; 1205 } 1206 } 1207 } 1208 #else 1209 /* 1210 * 2. Load the boot/recovery from the desired "boot" partition. 1211 * Determine if this is an AOSP image. 1212 */ 1213 if (load_android_image(dev_desc, boot_partname, &load_address)) { 1214 printf("Android image load failed\n"); 1215 return -1; 1216 } 1217 #endif 1218 update_root_uuid_if_android_ab(); 1219 1220 /* Set Android root variables. */ 1221 env_set_ulong("android_root_devnum", dev_desc->devnum); 1222 env_set("android_slotsufix", slot_suffix); 1223 1224 #ifdef CONFIG_FASTBOOT_OEM_UNLOCK 1225 /* read oem unlock status and attach to bootargs */ 1226 uint8_t unlock = 0; 1227 TEEC_Result result; 1228 char oem_unlock[OEM_UNLOCK_ARG_SIZE] = {0}; 1229 result = trusty_read_oem_unlock(&unlock); 1230 if (result) { 1231 printf("read oem unlock status with error : 0x%x\n", result); 1232 } else { 1233 snprintf(oem_unlock, OEM_UNLOCK_ARG_SIZE, "androidboot.oem_unlocked=%d", unlock); 1234 env_update("bootargs", oem_unlock); 1235 } 1236 #endif 1237 1238 /* Assemble the command line */ 1239 command_line = android_assemble_cmdline(slot_suffix, mode_cmdline); 1240 env_update("bootargs", command_line); 1241 1242 debug("ANDROID: bootargs: \"%s\"\n", command_line); 1243 1244 #ifdef CONFIG_SUPPORT_OEM_DTB 1245 if (android_bootloader_get_fdt(ANDROID_PARTITION_OEM, 1246 ANDROID_ARG_FDT_FILENAME)) { 1247 printf("Can not get the fdt data from oem!\n"); 1248 } 1249 #else 1250 ret = android_image_get_fdt((void *)load_address, &fdt_addr); 1251 if (!ret) 1252 env_set_hex("fdt_addr", fdt_addr); 1253 #endif 1254 #ifdef CONFIG_OPTEE_CLIENT 1255 if (trusty_notify_optee_uboot_end()) 1256 printf("Close optee client failed!\n"); 1257 #endif 1258 android_bootloader_boot_kernel(load_address); 1259 1260 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 1261 return -1; 1262 } 1263 1264 int android_avb_boot_flow(unsigned long kernel_address) 1265 { 1266 struct blk_desc *dev_desc; 1267 disk_partition_t boot_part_info; 1268 int ret; 1269 1270 dev_desc = rockchip_get_bootdev(); 1271 if (!dev_desc) { 1272 printf("%s: dev_desc is NULL!\n", __func__); 1273 return -1; 1274 } 1275 1276 /* Load the kernel from the desired "boot" partition. */ 1277 ret = part_get_info_by_name(dev_desc, ANDROID_PARTITION_BOOT, 1278 &boot_part_info); 1279 if (ret < 0) { 1280 printf("%s: failed to get boot part\n", __func__); 1281 return ret; 1282 } 1283 1284 ret = android_image_load(dev_desc, &boot_part_info, 1285 kernel_address, -1UL); 1286 if (ret < 0) { 1287 printf("Android avb boot failed, error %d.\n", ret); 1288 return ret; 1289 } 1290 1291 android_bootloader_boot_kernel(kernel_address); 1292 1293 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 1294 return -1; 1295 } 1296 1297 int android_boot_flow(unsigned long kernel_address) 1298 { 1299 struct blk_desc *dev_desc; 1300 disk_partition_t boot_part_info; 1301 int ret; 1302 1303 dev_desc = rockchip_get_bootdev(); 1304 if (!dev_desc) { 1305 printf("%s: dev_desc is NULL!\n", __func__); 1306 return -1; 1307 } 1308 /* Load the kernel from the desired "boot" partition. */ 1309 ret = part_get_info_by_name(dev_desc, ANDROID_PARTITION_BOOT, 1310 &boot_part_info); 1311 if (ret < 0) { 1312 printf("%s: failed to get boot part\n", __func__); 1313 return ret; 1314 } 1315 1316 ret = android_image_load(dev_desc, &boot_part_info, kernel_address, 1317 -1UL); 1318 if (ret < 0) 1319 return ret; 1320 1321 android_bootloader_boot_kernel(kernel_address); 1322 1323 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 1324 return -1; 1325 } 1326