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 char can_boot = 1; 440 441 requested_partitions[0] = boot_partname; 442 ops = avb_ops_user_new(); 443 if (ops == NULL) { 444 printf("avb_ops_user_new() failed!\n"); 445 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 446 } 447 448 if (ops->read_is_device_unlocked(ops, (bool *)&unlocked) != AVB_IO_RESULT_OK) 449 printf("Error determining whether device is unlocked.\n"); 450 451 printf("read_is_device_unlocked() ops returned that device is %s\n", 452 (unlocked & LOCK_MASK)? "UNLOCKED" : "LOCKED"); 453 454 flags = AVB_SLOT_VERIFY_FLAGS_NONE; 455 if (unlocked & LOCK_MASK) 456 flags |= AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR; 457 458 if(load_metadata(ops->ab_ops, &ab_data, &ab_data_orig)) { 459 printf("Can not load metadata\n"); 460 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 461 } 462 463 if (strncmp(slot_suffix, "_a", 2)) 464 slot_index_to_boot = 0; 465 else if(strncmp(slot_suffix, "_b", 2)) 466 slot_index_to_boot = 1; 467 else 468 slot_index_to_boot = 0; 469 470 verify_result = 471 avb_slot_verify(ops, 472 requested_partitions, 473 slot_suffix, 474 flags, 475 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, 476 &slot_data[0]); 477 478 strcat(verify_state, ANDROID_VERIFY_STATE); 479 switch (verify_result) { 480 case AVB_SLOT_VERIFY_RESULT_OK: 481 if (unlocked & LOCK_MASK) 482 strcat(verify_state, "orange"); 483 else 484 strcat(verify_state, "green"); 485 break; 486 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED: 487 if (unlocked & LOCK_MASK) 488 strcat(verify_state, "orange"); 489 else 490 strcat(verify_state, "yellow"); 491 break; 492 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM: 493 case AVB_SLOT_VERIFY_RESULT_ERROR_IO: 494 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA: 495 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION: 496 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION: 497 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX: 498 default: 499 if (unlocked & LOCK_MASK) 500 strcat(verify_state, "orange"); 501 else 502 strcat(verify_state, "red"); 503 break; 504 } 505 506 if (!slot_data[0]) { 507 can_boot = 0; 508 goto out; 509 } 510 511 if (verify_result == AVB_SLOT_VERIFY_RESULT_OK || 512 verify_result == AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED || 513 (unlocked & LOCK_MASK)) { 514 int len = 0; 515 char *bootargs, *newbootargs; 516 517 if (*slot_data[0]->cmdline) { 518 debug("Kernel command line: %s\n", slot_data[0]->cmdline); 519 len += strlen(slot_data[0]->cmdline); 520 } 521 522 bootargs = env_get("bootargs"); 523 if (bootargs) 524 len += strlen(bootargs); 525 526 newbootargs = malloc(len + 2); 527 528 if (!newbootargs) { 529 puts("Error: malloc in android_slot_verify failed!\n"); 530 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 531 } 532 *newbootargs = '\0'; 533 534 if (bootargs) { 535 strcpy(newbootargs, bootargs); 536 strcat(newbootargs, " "); 537 } 538 if (*slot_data[0]->cmdline) 539 strcat(newbootargs, slot_data[0]->cmdline); 540 env_set("bootargs", newbootargs); 541 542 memcpy((uint8_t *)load_address, 543 slot_data[0]->loaded_partitions->data, 544 slot_data[0]->loaded_partitions->data_size); 545 546 /* ... and decrement tries remaining, if applicable. */ 547 if (!ab_data.slots[slot_index_to_boot].successful_boot && 548 ab_data.slots[slot_index_to_boot].tries_remaining > 0) { 549 ab_data.slots[slot_index_to_boot].tries_remaining -= 1; 550 } 551 } else { 552 slot_set_unbootable(&ab_data.slots[slot_index_to_boot]); 553 } 554 555 out: 556 env_update("bootargs", verify_state); 557 if (save_metadata_if_changed(ops->ab_ops, &ab_data, &ab_data_orig)) { 558 printf("Can not save metadata\n"); 559 verify_result = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 560 } 561 562 if (slot_data[0] != NULL) 563 avb_slot_verify_data_free(slot_data[0]); 564 565 if ((unlocked & LOCK_MASK) && can_boot) 566 return 0; 567 else 568 return verify_result; 569 } 570 #endif 571 572 #if defined(CONFIG_CMD_DTIMG) && defined(CONFIG_OF_LIBFDT_OVERLAY) 573 574 /* 575 * Default return index 0. 576 */ 577 __weak int board_select_fdt_index(ulong dt_table_hdr) 578 { 579 /* 580 * User can use "dt_for_each_entry(entry, hdr, idx)" to iterate 581 * over all dt entry of DT image and pick up which they want. 582 * 583 * Example: 584 * struct dt_table_entry *entry; 585 * int index; 586 * 587 * dt_for_each_entry(entry, dt_table_hdr, index) { 588 * 589 * .... (use entry) 590 * } 591 * 592 * return index; 593 */ 594 return 0; 595 } 596 597 static int android_get_dtbo(ulong *fdt_dtbo, 598 const struct andr_img_hdr *hdr, 599 int *index) 600 { 601 struct dt_table_header *dt_hdr = NULL; 602 struct blk_desc *dev_desc; 603 const char *part_name; 604 disk_partition_t part_info; 605 u32 blk_offset, blk_cnt; 606 void *buf; 607 ulong e_addr; 608 u32 e_size; 609 int e_idx; 610 int ret; 611 612 /* Get partition according to boot mode */ 613 if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) 614 part_name = PART_RECOVERY; 615 else 616 part_name = PART_DTBO; 617 618 /* Get partition info */ 619 dev_desc = rockchip_get_bootdev(); 620 if (!dev_desc) { 621 printf("%s: dev_desc is NULL!\n", __func__); 622 return -ENODEV; 623 } 624 625 ret = part_get_info_by_name(dev_desc, part_name, &part_info); 626 if (ret < 0) { 627 printf("%s: failed to get %s part info, ret=%d\n", 628 __func__, part_name, ret); 629 return ret; 630 } 631 632 /* Check dt table header */ 633 if (!strcmp(part_name, PART_RECOVERY)) 634 blk_offset = part_info.start + 635 (hdr->recovery_dtbo_offset / part_info.blksz); 636 else 637 blk_offset = part_info.start; 638 639 dt_hdr = memalign(ARCH_DMA_MINALIGN, part_info.blksz); 640 if (!dt_hdr) { 641 printf("%s: out of memory for dt header!\n", __func__); 642 return -ENOMEM; 643 } 644 645 ret = blk_dread(dev_desc, blk_offset, 1, dt_hdr); 646 if (ret != 1) { 647 printf("%s: failed to read dt table header\n", 648 __func__); 649 goto out1; 650 } 651 652 if (!android_dt_check_header((ulong)dt_hdr)) { 653 printf("%s: Error: invalid dt table header: 0x%x\n", 654 __func__, dt_hdr->magic); 655 ret = -EINVAL; 656 goto out1; 657 } 658 659 #ifdef DEBUG 660 android_dt_print_contents((ulong)dt_hdr); 661 #endif 662 663 blk_cnt = DIV_ROUND_UP(fdt32_to_cpu(dt_hdr->total_size), 664 part_info.blksz); 665 /* Read all DT Image */ 666 buf = memalign(ARCH_DMA_MINALIGN, part_info.blksz * blk_cnt); 667 if (!buf) { 668 printf("%s: out of memory for %s part!\n", __func__, part_name); 669 ret = -ENOMEM; 670 goto out1; 671 } 672 673 ret = blk_dread(dev_desc, blk_offset, blk_cnt, buf); 674 if (ret != blk_cnt) { 675 printf("%s: failed to read dtbo, blk_cnt=%d, ret=%d\n", 676 __func__, blk_cnt, ret); 677 goto out2; 678 } 679 680 e_idx = board_select_fdt_index((ulong)buf); 681 if (e_idx < 0) { 682 printf("%s: failed to select board fdt index\n", __func__); 683 ret = -EINVAL; 684 goto out2; 685 } 686 687 ret = android_dt_get_fdt_by_index((ulong)buf, e_idx, &e_addr, &e_size); 688 if (!ret) { 689 printf("%s: failed to get fdt, index=%d\n", __func__, e_idx); 690 ret = -EINVAL; 691 goto out2; 692 } 693 694 if (fdt_dtbo) 695 *fdt_dtbo = e_addr; 696 if (index) 697 *index = e_idx; 698 699 free(dt_hdr); 700 debug("ANDROID: Loading dt entry to 0x%lx size 0x%x idx %d from \"%s\" part\n", 701 e_addr, e_size, e_idx, part_name); 702 703 return 0; 704 705 out2: 706 free(buf); 707 out1: 708 free(dt_hdr); 709 710 return ret; 711 } 712 713 int android_fdt_overlay_apply(void *fdt_addr) 714 { 715 struct andr_img_hdr *hdr; 716 struct blk_desc *dev_desc; 717 const char *part_name; 718 disk_partition_t part_info; 719 char buf[32] = {0}; 720 u32 blk_cnt; 721 ulong fdt_dtbo = -1; 722 int index = -1; 723 int ret; 724 725 /* Get partition according to boot mode */ 726 if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) 727 part_name = PART_RECOVERY; 728 else 729 part_name = PART_BOOT; 730 731 /* Get partition info */ 732 dev_desc = rockchip_get_bootdev(); 733 if (!dev_desc) { 734 printf("%s: dev_desc is NULL!\n", __func__); 735 return -ENODEV; 736 } 737 738 ret = part_get_info_by_name(dev_desc, part_name, &part_info); 739 if (ret < 0) { 740 printf("%s: failed to get %s part info, ret=%d\n", 741 __func__, part_name, ret); 742 return ret; 743 } 744 745 blk_cnt = DIV_ROUND_UP(sizeof(*hdr), part_info.blksz); 746 hdr = memalign(ARCH_DMA_MINALIGN, part_info.blksz * blk_cnt); 747 if (!hdr) { 748 printf("%s: out of memory!\n", __func__); 749 return -ENOMEM; 750 } 751 752 ret = blk_dread(dev_desc, part_info.start, blk_cnt, hdr); 753 if (ret != blk_cnt) { 754 printf("%s: failed to read %s hdr!\n", __func__, part_name); 755 goto out; 756 } 757 758 #ifdef DEBUG 759 android_print_contents(hdr); 760 #endif 761 762 if (android_image_check_header(hdr)) { 763 printf("%s: Invalid Android header %s\n", __func__, hdr->magic); 764 return -EINVAL; 765 } 766 767 /* Check header version */ 768 if (!hdr->header_version) { 769 printf("Android header version 0\n"); 770 ret = -EINVAL; 771 goto out; 772 } 773 774 ret = android_get_dtbo(&fdt_dtbo, (void *)hdr, &index); 775 if (!ret) { 776 /* Must incease size before overlay */ 777 fdt_increase_size(fdt_addr, fdt_totalsize((void *)fdt_dtbo)); 778 ret = fdt_overlay_apply(fdt_addr, (void *)fdt_dtbo); 779 if (!ret) { 780 snprintf(buf, 32, "%s%d", "androidboot.dtbo_idx=", index); 781 env_update("bootargs", buf); 782 printf("ANDROID: fdt overlay OK\n"); 783 } else { 784 printf("ANDROID: fdt overlay failed, ret=%d\n", ret); 785 } 786 } 787 788 out: 789 free(hdr); 790 791 return 0; 792 } 793 #endif 794 795 int android_bootloader_boot_flow(struct blk_desc *dev_desc, 796 unsigned long load_address) 797 { 798 enum android_boot_mode mode; 799 disk_partition_t misc_part_info; 800 int part_num; 801 int ret; 802 char *command_line; 803 char slot_suffix[3] = {0}; 804 const char *mode_cmdline = NULL; 805 char *boot_partname = ANDROID_PARTITION_BOOT; 806 ulong fdt_addr; 807 808 /* 809 * 1. Load MISC partition and determine the boot mode 810 * clear its value for the next boot if needed. 811 */ 812 part_num = part_get_info_by_name(dev_desc, ANDROID_PARTITION_MISC, 813 &misc_part_info); 814 if (part_num < 0) 815 printf("%s Could not find misc partition\n", __func__); 816 817 #ifdef CONFIG_OPTEE_CLIENT 818 /* load attestation key from misc partition. */ 819 load_attestation_key(dev_desc, &misc_part_info); 820 #endif 821 822 mode = android_bootloader_load_and_clear_mode(dev_desc, &misc_part_info); 823 #ifdef CONFIG_RKIMG_BOOTLOADER 824 if (mode == ANDROID_BOOT_MODE_NORMAL) { 825 if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) 826 mode = ANDROID_BOOT_MODE_RECOVERY; 827 } 828 #endif 829 printf("ANDROID: reboot reason: \"%s\"\n", android_boot_mode_str(mode)); 830 831 #ifdef CONFIG_ANDROID_AB 832 /*TODO: get from pre-loader or misc partition*/ 833 if (rk_avb_get_current_slot(slot_suffix)) 834 return -1; 835 836 if (slot_suffix[0] != '_') { 837 printf("There is no bootable slot!\n"); 838 return -1; 839 } 840 #endif 841 842 switch (mode) { 843 case ANDROID_BOOT_MODE_NORMAL: 844 /* In normal mode, we load the kernel from "boot" but append 845 * "skip_initramfs" to the cmdline to make it ignore the 846 * recovery initramfs in the boot partition. 847 */ 848 #if defined(CONFIG_ANDROID_AB) && !defined(CONFIG_ANDROID_AVB) 849 char root_partition[20] = {0}; 850 char guid_buf[UUID_SIZE] = {0}; 851 char root_partuuid[70] = "root=PARTUUID="; 852 853 strcat(root_partition, ANDROID_PARTITION_SYSTEM); 854 strcat(root_partition, slot_suffix); 855 get_partition_unique_uuid(root_partition, guid_buf, UUID_SIZE); 856 strcat(root_partuuid, guid_buf); 857 env_update("bootargs", root_partuuid); 858 #endif 859 860 #ifdef CONFIG_ANDROID_AB 861 mode_cmdline = "skip_initramfs"; 862 #endif 863 break; 864 case ANDROID_BOOT_MODE_RECOVERY: 865 /* In recovery mode we still boot the kernel from "boot" but 866 * don't skip the initramfs so it boots to recovery. 867 */ 868 #ifndef CONFIG_ANDROID_AB 869 boot_partname = ANDROID_PARTITION_RECOVERY; 870 #endif 871 break; 872 case ANDROID_BOOT_MODE_BOOTLOADER: 873 /* Bootloader mode enters fastboot. If this operation fails we 874 * simply return since we can't recover from this situation by 875 * switching to another slot. 876 */ 877 return android_bootloader_boot_bootloader(); 878 } 879 880 #ifdef CONFIG_ANDROID_AVB 881 if (android_slot_verify(boot_partname, load_address, slot_suffix)) 882 return -1; 883 #else 884 /* 885 * 2. Load the boot/recovery from the desired "boot" partition. 886 * Determine if this is an AOSP image. 887 */ 888 disk_partition_t boot_part_info; 889 part_num = 890 android_part_get_info_by_name_suffix(dev_desc, 891 boot_partname, 892 slot_suffix, &boot_part_info); 893 if (part_num < 0) { 894 printf("%s Could not found bootable partition %s\n", __func__, 895 boot_partname); 896 return -1; 897 } 898 debug("ANDROID: Loading kernel from \"%s\", partition %d.\n", 899 boot_part_info.name, part_num); 900 901 ret = android_image_load(dev_desc, &boot_part_info, load_address, 902 -1UL); 903 if (ret < 0) { 904 printf("%s %s part load fail\n", __func__, boot_part_info.name); 905 return ret; 906 } 907 load_address = ret; 908 #endif 909 910 /* Set Android root variables. */ 911 env_set_ulong("android_root_devnum", dev_desc->devnum); 912 env_set("android_slotsufix", slot_suffix); 913 914 #ifdef CONFIG_FASTBOOT_OEM_UNLOCK 915 /* read oem unlock status and attach to bootargs */ 916 uint8_t unlock = 0; 917 TEEC_Result result; 918 char oem_unlock[OEM_UNLOCK_ARG_SIZE] = {0}; 919 result = trusty_read_oem_unlock(&unlock); 920 if (result) { 921 printf("read oem unlock status with error : 0x%x\n", result); 922 } else { 923 snprintf(oem_unlock, OEM_UNLOCK_ARG_SIZE, "androidboot.oem_unlocked=%d", unlock); 924 env_update("bootargs", oem_unlock); 925 } 926 #endif 927 928 /* Assemble the command line */ 929 command_line = android_assemble_cmdline(slot_suffix, mode_cmdline); 930 env_update("bootargs", command_line); 931 932 debug("ANDROID: bootargs: \"%s\"\n", command_line); 933 934 #ifdef CONFIG_SUPPORT_OEM_DTB 935 if (android_bootloader_get_fdt(ANDROID_PARTITION_OEM, 936 ANDROID_ARG_FDT_FILENAME)) { 937 printf("Can not get the fdt data from oem!\n"); 938 } 939 #else 940 ret = android_image_get_fdt((void *)load_address, &fdt_addr); 941 if (!ret) 942 env_set_hex("fdt_addr", fdt_addr); 943 #endif 944 android_bootloader_boot_kernel(load_address); 945 946 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 947 return -1; 948 } 949 950 int android_avb_boot_flow(char *slot_suffix, unsigned long kernel_address) 951 { 952 struct blk_desc *dev_desc; 953 disk_partition_t boot_part_info; 954 int ret; 955 dev_desc = rockchip_get_bootdev(); 956 if (!dev_desc) { 957 printf("%s: dev_desc is NULL!\n", __func__); 958 return -1; 959 } 960 /* Load the kernel from the desired "boot" partition. */ 961 android_part_get_info_by_name_suffix(dev_desc, 962 ANDROID_PARTITION_BOOT, 963 slot_suffix, &boot_part_info); 964 ret = android_image_load(dev_desc, &boot_part_info, kernel_address, 965 -1UL); 966 if (ret < 0) 967 return ret; 968 android_bootloader_boot_kernel(kernel_address); 969 970 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 971 return -1; 972 } 973 974 int android_boot_flow(unsigned long kernel_address) 975 { 976 struct blk_desc *dev_desc; 977 disk_partition_t boot_part_info; 978 int ret; 979 dev_desc = rockchip_get_bootdev(); 980 if (!dev_desc) { 981 printf("%s: dev_desc is NULL!\n", __func__); 982 return -1; 983 } 984 /* Load the kernel from the desired "boot" partition. */ 985 part_get_info_by_name(dev_desc, ANDROID_PARTITION_BOOT, &boot_part_info); 986 ret = android_image_load(dev_desc, &boot_part_info, kernel_address, 987 -1UL); 988 if (ret < 0) 989 return ret; 990 android_bootloader_boot_kernel(kernel_address); 991 992 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 993 return -1; 994 } 995