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