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