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