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