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