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