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