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 13 #include <cli.h> 14 #include <common.h> 15 #include <malloc.h> 16 #include <fs.h> 17 #include <boot_rkimg.h> 18 #include <attestation_key.h> 19 #include <optee_include/OpteeClientInterface.h> 20 21 #define ANDROID_PARTITION_BOOT "boot" 22 #define ANDROID_PARTITION_MISC "misc" 23 #define ANDROID_PARTITION_OEM "oem" 24 #define ANDROID_PARTITION_RECOVERY "recovery" 25 #define ANDROID_PARTITION_SYSTEM "system" 26 27 #define ANDROID_ARG_SLOT_SUFFIX "androidboot.slot_suffix=" 28 #define ANDROID_ARG_ROOT "root=" 29 #define ANDROID_ARG_SERIALNO "androidboot.serialno=" 30 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE 31 #define ANDROID_ARG_FDT_FILENAME "rk-kernel.dtb" 32 #define BOOTLOADER_MESSAGE_OFFSET_IN_MISC (16 * 1024) 33 #define BOOTLOADER_MESSAGE_BLK_OFFSET (BOOTLOADER_MESSAGE_OFFSET_IN_MISC >> 9) 34 #else 35 #define ANDROID_ARG_FDT_FILENAME "kernel.dtb" 36 #endif 37 #define OEM_UNLOCK_ARG_SIZE 30 38 39 char *android_str_append(char *base_name, char *slot_suffix) 40 { 41 char *part_name; 42 size_t part_name_len; 43 44 part_name_len = strlen(base_name) + 1; 45 if (slot_suffix) 46 part_name_len += strlen(slot_suffix); 47 part_name = malloc(part_name_len); 48 if (!part_name) 49 return NULL; 50 strcpy(part_name, base_name); 51 if (slot_suffix && (slot_suffix[0] != '\0')) 52 strcat(part_name, slot_suffix); 53 54 return part_name; 55 } 56 57 int android_bootloader_message_load( 58 struct blk_desc *dev_desc, 59 const disk_partition_t *part_info, 60 struct android_bootloader_message *message) 61 { 62 ulong message_blocks = sizeof(struct android_bootloader_message) / 63 part_info->blksz; 64 if (message_blocks > part_info->size) { 65 printf("misc partition too small.\n"); 66 return -1; 67 } 68 69 #ifdef CONFIG_RKIMG_BOOTLOADER 70 if (blk_dread(dev_desc, part_info->start + BOOTLOADER_MESSAGE_BLK_OFFSET, 71 message_blocks, message) != 72 #else 73 if (blk_dread(dev_desc, part_info->start, message_blocks, message) != 74 #endif 75 message_blocks) { 76 printf("Could not read from misc partition\n"); 77 return -1; 78 } 79 debug("ANDROID: Loaded BCB, %lu blocks.\n", message_blocks); 80 return 0; 81 } 82 83 static int android_bootloader_message_write( 84 struct blk_desc *dev_desc, 85 const disk_partition_t *part_info, 86 struct android_bootloader_message *message) 87 { 88 ulong message_blocks = sizeof(struct android_bootloader_message) / 89 part_info->blksz; 90 if (message_blocks > part_info->size) { 91 printf("misc partition too small.\n"); 92 return -1; 93 } 94 95 if (blk_dwrite(dev_desc, part_info->start, message_blocks, message) != 96 message_blocks) { 97 printf("Could not write to misc partition\n"); 98 return -1; 99 } 100 debug("ANDROID: Wrote new BCB, %lu blocks.\n", message_blocks); 101 return 0; 102 } 103 104 static enum android_boot_mode android_bootloader_load_and_clear_mode( 105 struct blk_desc *dev_desc, 106 const disk_partition_t *misc_part_info) 107 { 108 struct android_bootloader_message bcb; 109 110 #ifdef CONFIG_FASTBOOT 111 char *bootloader_str; 112 113 /* Check for message from bootloader stored in RAM from a previous boot. 114 */ 115 bootloader_str = (char *)CONFIG_FASTBOOT_BUF_ADDR; 116 if (!strcmp("reboot-bootloader", bootloader_str)) { 117 bootloader_str[0] = '\0'; 118 return ANDROID_BOOT_MODE_BOOTLOADER; 119 } 120 #endif 121 122 /* Check and update the BCB message if needed. */ 123 if (android_bootloader_message_load(dev_desc, misc_part_info, &bcb) < 124 0) { 125 printf("WARNING: Unable to load the BCB.\n"); 126 return ANDROID_BOOT_MODE_NORMAL; 127 } 128 129 if (!strcmp("bootonce-bootloader", bcb.command)) { 130 /* Erase the message in the BCB since this value should be used 131 * only once. 132 */ 133 memset(bcb.command, 0, sizeof(bcb.command)); 134 android_bootloader_message_write(dev_desc, misc_part_info, 135 &bcb); 136 return ANDROID_BOOT_MODE_BOOTLOADER; 137 } 138 139 if (!strcmp("boot-recovery", bcb.command)) 140 return ANDROID_BOOT_MODE_RECOVERY; 141 142 return ANDROID_BOOT_MODE_NORMAL; 143 } 144 145 /** 146 * Return the reboot reason string for the passed boot mode. 147 * 148 * @param mode The Android Boot mode. 149 * @return a pointer to the reboot reason string for mode. 150 */ 151 static const char *android_boot_mode_str(enum android_boot_mode mode) 152 { 153 switch (mode) { 154 case ANDROID_BOOT_MODE_NORMAL: 155 return "(none)"; 156 case ANDROID_BOOT_MODE_RECOVERY: 157 return "recovery"; 158 case ANDROID_BOOT_MODE_BOOTLOADER: 159 return "bootloader"; 160 } 161 return NULL; 162 } 163 164 static int android_part_get_info_by_name_suffix(struct blk_desc *dev_desc, 165 const char *base_name, 166 const char *slot_suffix, 167 disk_partition_t *part_info) 168 { 169 char *part_name; 170 int part_num; 171 size_t part_name_len; 172 173 part_name_len = strlen(base_name) + 1; 174 if (slot_suffix) 175 part_name_len += strlen(slot_suffix); 176 part_name = malloc(part_name_len); 177 if (!part_name) 178 return -1; 179 strcpy(part_name, base_name); 180 if (slot_suffix && (slot_suffix[0] != '\0')) 181 strcat(part_name, slot_suffix); 182 183 part_num = part_get_info_by_name(dev_desc, part_name, part_info); 184 if (part_num < 0) { 185 debug("ANDROID: Could not find partition \"%s\"\n", part_name); 186 part_num = -1; 187 } 188 189 free(part_name); 190 return part_num; 191 } 192 193 static int android_bootloader_boot_bootloader(void) 194 { 195 const char *fastboot_cmd = env_get("fastbootcmd"); 196 197 if (fastboot_cmd == NULL) { 198 printf("fastboot_cmd is null, run default fastboot_cmd!\n"); 199 fastboot_cmd = "fastboot usb 0"; 200 } 201 202 return run_command(fastboot_cmd, CMD_FLAG_ENV); 203 } 204 205 #ifdef CONFIG_SUPPORT_OEM_DTB 206 static int android_bootloader_get_fdt(const char *part_name, 207 const char *load_file_name) 208 { 209 const char *dev_iface = "mmc"; 210 struct blk_desc *dev_desc; 211 disk_partition_t boot_part_info; 212 char *fdt_addr = NULL; 213 char slot_suffix[5] = {0}; 214 char dev_part[3] = {0}; 215 loff_t bytes = 0; 216 loff_t pos = 0; 217 loff_t len_read; 218 unsigned long addr = 0; 219 int part_num = -1; 220 int dev_num = 0; 221 int ret; 222 223 dev_desc = blk_get_dev(dev_iface, dev_num); 224 if (!dev_desc) { 225 printf("Could not find %s %d\n", dev_iface, dev_num); 226 return -1; 227 } 228 229 memset(&boot_part_info, 0, sizeof(boot_part_info)); 230 231 #ifdef CONFIG_RK_AVB_LIBAVB_USER 232 if (rk_avb_get_current_slot(slot_suffix)) { 233 printf("ANDROID: Get Current Slot error.\n"); 234 return -1; 235 } 236 237 part_num = android_part_get_info_by_name_suffix(dev_desc, 238 part_name, 239 slot_suffix, &boot_part_info); 240 #else 241 part_num = part_get_info_by_name(dev_desc, part_name, &boot_part_info); 242 if (part_num < 0) { 243 printf("ANDROID: Could not find partition \"%s\"\n", part_name); 244 return -1; 245 } 246 #endif 247 248 snprintf(dev_part, ARRAY_SIZE(dev_part), ":%x", part_num); 249 if (fs_set_blk_dev(dev_iface, dev_part, FS_TYPE_EXT)) 250 return -1; 251 252 fdt_addr = env_get("fdt_addr_r"); 253 if (!fdt_addr) { 254 printf("ANDROID: No Found FDT Load Address.\n"); 255 return -1; 256 } 257 addr = simple_strtoul(fdt_addr, NULL, 16); 258 259 ret = fs_read(load_file_name, addr, pos, bytes, &len_read); 260 if (ret < 0) 261 return -1; 262 263 return 0; 264 } 265 #endif 266 267 int android_bootloader_boot_kernel(unsigned long kernel_address) 268 { 269 char kernel_addr_str[12]; 270 char *fdt_addr = env_get("fdt_addr"); 271 char *bootm_args[] = { 272 "bootm", kernel_addr_str, kernel_addr_str, fdt_addr, NULL }; 273 274 sprintf(kernel_addr_str, "0x%lx", kernel_address); 275 276 printf("Booting kernel at %s with fdt at %s...\n\n\n", 277 kernel_addr_str, fdt_addr); 278 do_bootm(NULL, 0, 4, bootm_args); 279 280 return -1; 281 } 282 283 static char *strjoin(const char **chunks, char separator) 284 { 285 int len, joined_len = 0; 286 char *ret, *current; 287 const char **p; 288 289 for (p = chunks; *p; p++) 290 joined_len += strlen(*p) + 1; 291 292 if (!joined_len) { 293 ret = malloc(1); 294 if (ret) 295 ret[0] = '\0'; 296 return ret; 297 } 298 299 ret = malloc(joined_len); 300 current = ret; 301 if (!ret) 302 return ret; 303 304 for (p = chunks; *p; p++) { 305 len = strlen(*p); 306 memcpy(current, *p, len); 307 current += len; 308 *current = separator; 309 current++; 310 } 311 /* Replace the last separator by a \0. */ 312 current[-1] = '\0'; 313 return ret; 314 } 315 316 /** android_assemble_cmdline - Assemble the command line to pass to the kernel 317 * @return a newly allocated string 318 */ 319 char *android_assemble_cmdline(const char *slot_suffix, 320 const char *extra_args) 321 { 322 const char *cmdline_chunks[16]; 323 const char **current_chunk = cmdline_chunks; 324 char *env_cmdline, *cmdline, *rootdev_input, *serialno; 325 char *allocated_suffix = NULL; 326 char *allocated_serialno = NULL; 327 char *allocated_rootdev = NULL; 328 unsigned long rootdev_len; 329 330 env_cmdline = env_get("bootargs"); 331 if (env_cmdline) 332 *(current_chunk++) = env_cmdline; 333 334 /* The |slot_suffix| needs to be passed to the kernel to know what 335 * slot to boot from. 336 */ 337 if (slot_suffix) { 338 allocated_suffix = malloc(strlen(ANDROID_ARG_SLOT_SUFFIX) + 339 strlen(slot_suffix) + 1); 340 memset(allocated_suffix, 0, strlen(ANDROID_ARG_SLOT_SUFFIX) 341 + strlen(slot_suffix) + 1); 342 strcpy(allocated_suffix, ANDROID_ARG_SLOT_SUFFIX); 343 strcat(allocated_suffix, slot_suffix); 344 *(current_chunk++) = allocated_suffix; 345 } 346 347 serialno = env_get("serial#"); 348 if (serialno) { 349 allocated_serialno = malloc(strlen(ANDROID_ARG_SERIALNO) + 350 strlen(serialno) + 1); 351 memset(allocated_serialno, 0, strlen(ANDROID_ARG_SERIALNO) + 352 strlen(serialno) + 1); 353 strcpy(allocated_serialno, ANDROID_ARG_SERIALNO); 354 strcat(allocated_serialno, serialno); 355 *(current_chunk++) = allocated_serialno; 356 } 357 358 rootdev_input = env_get("android_rootdev"); 359 if (rootdev_input) { 360 rootdev_len = strlen(ANDROID_ARG_ROOT) + CONFIG_SYS_CBSIZE + 1; 361 allocated_rootdev = malloc(rootdev_len); 362 strcpy(allocated_rootdev, ANDROID_ARG_ROOT); 363 cli_simple_process_macros(rootdev_input, 364 allocated_rootdev + 365 strlen(ANDROID_ARG_ROOT)); 366 /* Make sure that the string is null-terminated since the 367 * previous could not copy to the end of the input string if it 368 * is too big. 369 */ 370 allocated_rootdev[rootdev_len - 1] = '\0'; 371 *(current_chunk++) = allocated_rootdev; 372 } 373 374 if (extra_args) 375 *(current_chunk++) = extra_args; 376 377 *(current_chunk++) = NULL; 378 cmdline = strjoin(cmdline_chunks, ' '); 379 free(allocated_suffix); 380 free(allocated_rootdev); 381 return cmdline; 382 } 383 384 #ifdef CONFIG_ANDROID_AVB 385 static void slot_set_unbootable(AvbABSlotData* slot) 386 { 387 slot->priority = 0; 388 slot->tries_remaining = 0; 389 slot->successful_boot = 0; 390 } 391 392 static AvbSlotVerifyResult android_slot_verify(char *boot_partname, 393 unsigned long load_address, 394 char *slot_suffix) 395 { 396 const char *requested_partitions[1] = {NULL}; 397 uint8_t unlocked = true; 398 AvbOps *ops; 399 AvbSlotVerifyFlags flags; 400 AvbSlotVerifyData *slot_data[1] = {NULL}; 401 AvbSlotVerifyResult verify_result; 402 AvbABData ab_data, ab_data_orig; 403 size_t slot_index_to_boot = 0; 404 405 requested_partitions[0] = boot_partname; 406 ops = avb_ops_user_new(); 407 if (ops == NULL) { 408 printf("avb_ops_user_new() failed!\n"); 409 return AVB_SLOT_VERIFY_RESULT_ERROR_OOM; 410 } 411 412 if (ops->read_is_device_unlocked(ops, (bool *)&unlocked) != AVB_IO_RESULT_OK) 413 printf("Error determining whether device is unlocked.\n"); 414 415 printf("read_is_device_unlocked() ops returned that device is %s\n", 416 (unlocked & LOCK_MASK)? "UNLOCKED" : "LOCKED"); 417 418 flags = AVB_SLOT_VERIFY_FLAGS_NONE; 419 if (unlocked & LOCK_MASK) 420 flags |= AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR; 421 422 if(load_metadata(ops->ab_ops, &ab_data, &ab_data_orig)) { 423 printf("Can not load metadata\n"); 424 return AVB_SLOT_VERIFY_RESULT_ERROR_IO; 425 } 426 427 if (strncmp(slot_suffix, "_a", 2)) 428 slot_index_to_boot = 0; 429 else if(strncmp(slot_suffix, "_b", 2)) 430 slot_index_to_boot = 1; 431 else 432 slot_index_to_boot = 0; 433 434 verify_result = 435 avb_slot_verify(ops, 436 requested_partitions, 437 slot_suffix, 438 flags, 439 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, 440 &slot_data[0]); 441 442 if (verify_result != AVB_SLOT_VERIFY_RESULT_OK && !(unlocked & LOCK_MASK)) { 443 slot_set_unbootable(&ab_data.slots[slot_index_to_boot]); 444 goto out; 445 } 446 447 memcpy((uint8_t*)load_address, 448 slot_data[0]->loaded_partitions->data, 449 slot_data[0]->loaded_partitions->data_size); 450 env_set("bootargs", slot_data[0]->cmdline); 451 452 /* ... and decrement tries remaining, if applicable. */ 453 if (!ab_data.slots[slot_index_to_boot].successful_boot && 454 ab_data.slots[slot_index_to_boot].tries_remaining > 0) { 455 ab_data.slots[slot_index_to_boot].tries_remaining -= 1; 456 } 457 out: 458 if (save_metadata_if_changed(ops->ab_ops, &ab_data, &ab_data_orig)) { 459 printf("Can not save metadata\n"); 460 verify_result = AVB_SLOT_VERIFY_RESULT_ERROR_IO; 461 } 462 463 if (slot_data[0] != NULL) 464 avb_slot_verify_data_free(slot_data[0]); 465 466 if (unlocked & LOCK_MASK) 467 return 0; 468 else 469 return verify_result; 470 } 471 #endif 472 473 int android_bootloader_boot_flow(struct blk_desc *dev_desc, 474 unsigned long load_address) 475 { 476 enum android_boot_mode mode; 477 disk_partition_t misc_part_info; 478 int part_num; 479 int ret; 480 char *command_line; 481 char slot_suffix[3] = {0}; 482 const char *mode_cmdline = NULL; 483 char *boot_partname = ANDROID_PARTITION_BOOT; 484 ulong fdt_addr; 485 486 /* 487 * 1. Load MISC partition and determine the boot mode 488 * clear its value for the next boot if needed. 489 */ 490 part_num = part_get_info_by_name(dev_desc, ANDROID_PARTITION_MISC, 491 &misc_part_info); 492 if (part_num < 0) 493 printf("%s Could not find misc partition\n", __func__); 494 495 #ifdef CONFIG_OPTEE_CLIENT 496 /* load attestation key from misc partition. */ 497 load_attestation_key(dev_desc, &misc_part_info); 498 #endif 499 500 mode = android_bootloader_load_and_clear_mode(dev_desc, &misc_part_info); 501 #ifdef CONFIG_RKIMG_BOOTLOADER 502 if (mode == ANDROID_BOOT_MODE_NORMAL) { 503 if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) 504 mode = ANDROID_BOOT_MODE_RECOVERY; 505 } 506 #endif 507 printf("ANDROID: reboot reason: \"%s\"\n", android_boot_mode_str(mode)); 508 509 switch (mode) { 510 case ANDROID_BOOT_MODE_NORMAL: 511 /* In normal mode, we load the kernel from "boot" but append 512 * "skip_initramfs" to the cmdline to make it ignore the 513 * recovery initramfs in the boot partition. 514 */ 515 #ifdef CONFIG_ANDROID_AB 516 mode_cmdline = "skip_initramfs"; 517 #endif 518 break; 519 case ANDROID_BOOT_MODE_RECOVERY: 520 /* In recovery mode we still boot the kernel from "boot" but 521 * don't skip the initramfs so it boots to recovery. 522 */ 523 #ifndef CONFIG_ANDROID_AB 524 boot_partname = ANDROID_PARTITION_RECOVERY; 525 #endif 526 break; 527 case ANDROID_BOOT_MODE_BOOTLOADER: 528 /* Bootloader mode enters fastboot. If this operation fails we 529 * simply return since we can't recover from this situation by 530 * switching to another slot. 531 */ 532 return android_bootloader_boot_bootloader(); 533 } 534 535 #ifdef CONFIG_ANDROID_AB 536 /*TODO: get from pre-loader or misc partition*/ 537 if (rk_avb_get_current_slot(slot_suffix)) 538 return -1; 539 540 if (slot_suffix[0] != '_') { 541 printf("There is no bootable slot!\n"); 542 return -1; 543 } 544 #endif 545 546 #ifdef CONFIG_ANDROID_AVB 547 if (android_slot_verify(boot_partname, load_address, slot_suffix)) 548 return -1; 549 #else 550 /* 551 * 2. Load the boot/recovery from the desired "boot" partition. 552 * Determine if this is an AOSP image. 553 */ 554 disk_partition_t boot_part_info; 555 part_num = 556 android_part_get_info_by_name_suffix(dev_desc, 557 boot_partname, 558 slot_suffix, &boot_part_info); 559 if (part_num < 0) { 560 printf("%s Could not found bootable partition %s\n", __func__, 561 boot_partname); 562 return -1; 563 } 564 debug("ANDROID: Loading kernel from \"%s\", partition %d.\n", 565 boot_part_info.name, part_num); 566 567 ret = android_image_load(dev_desc, &boot_part_info, load_address, 568 -1UL); 569 if (ret < 0) { 570 printf("%s %s part load fail\n", __func__, boot_part_info.name); 571 return ret; 572 } 573 #endif 574 575 /* Set Android root variables. */ 576 env_set_ulong("android_root_devnum", dev_desc->devnum); 577 env_set("android_slotsufix", slot_suffix); 578 579 #ifdef CONFIG_OPTEE_CLIENT 580 /* read oem unlock status and attach to bootargs */ 581 uint8_t unlock = 0; 582 TEEC_Result result; 583 char oem_unlock[OEM_UNLOCK_ARG_SIZE] = {0}; 584 result = trusty_read_oem_unlock(&unlock); 585 if (result) { 586 printf("read oem unlock status with error : 0x%x\n", result); 587 } else { 588 snprintf(oem_unlock, OEM_UNLOCK_ARG_SIZE, "androidboot.oem_unlocked=%d", unlock); 589 env_update("bootargs", oem_unlock); 590 } 591 #endif 592 593 /* Assemble the command line */ 594 command_line = android_assemble_cmdline(slot_suffix, mode_cmdline); 595 env_update("bootargs", command_line); 596 597 debug("ANDROID: bootargs: \"%s\"\n", command_line); 598 599 #ifdef CONFIG_SUPPORT_OEM_DTB 600 if (android_bootloader_get_fdt(ANDROID_PARTITION_OEM, 601 ANDROID_ARG_FDT_FILENAME)) { 602 printf("Can not get the fdt data from oem!\n"); 603 } 604 #else 605 ret = android_image_get_fdt((void *)load_address, &fdt_addr); 606 if (!ret) 607 env_set_hex("fdt_addr", fdt_addr); 608 #endif 609 android_bootloader_boot_kernel(load_address); 610 611 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 612 return -1; 613 } 614 615 int android_avb_boot_flow(char *slot_suffix, unsigned long kernel_address) 616 { 617 const char *dev_iface = "mmc"; 618 int dev_num = 0; 619 struct blk_desc *dev_desc; 620 disk_partition_t boot_part_info; 621 int ret; 622 dev_desc = blk_get_dev(dev_iface, dev_num); 623 if (!dev_desc) { 624 printf("Could not find %s %d\n", dev_iface, dev_num); 625 return -1; 626 } 627 /* Load the kernel from the desired "boot" partition. */ 628 android_part_get_info_by_name_suffix(dev_desc, 629 ANDROID_PARTITION_BOOT, 630 slot_suffix, &boot_part_info); 631 ret = android_image_load(dev_desc, &boot_part_info, kernel_address, 632 -1UL); 633 if (ret < 0) 634 return ret; 635 android_bootloader_boot_kernel(kernel_address); 636 637 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 638 return -1; 639 } 640 641 int android_boot_flow(unsigned long kernel_address) 642 { 643 const char *dev_iface = "mmc"; 644 int dev_num = 0; 645 struct blk_desc *dev_desc; 646 disk_partition_t boot_part_info; 647 int ret; 648 dev_desc = blk_get_dev(dev_iface, dev_num); 649 if (!dev_desc) { 650 printf("Could not find %s %d\n", dev_iface, dev_num); 651 return -1; 652 } 653 /* Load the kernel from the desired "boot" partition. */ 654 part_get_info_by_name(dev_desc, ANDROID_PARTITION_BOOT, &boot_part_info); 655 ret = android_image_load(dev_desc, &boot_part_info, kernel_address, 656 -1UL); 657 if (ret < 0) 658 return ret; 659 android_bootloader_boot_kernel(kernel_address); 660 661 /* TODO: If the kernel doesn't boot mark the selected slot as bad. */ 662 return -1; 663 } 664