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