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