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