1 /* 2 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de> 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <image.h> 9 #include <android_image.h> 10 #include <android_bootloader.h> 11 #include <malloc.h> 12 #include <mapmem.h> 13 #include <errno.h> 14 #include <boot_rkimg.h> 15 #include <crypto.h> 16 #include <sysmem.h> 17 #include <u-boot/sha1.h> 18 #ifdef CONFIG_RKIMG_BOOTLOADER 19 #include <asm/arch/resource_img.h> 20 #endif 21 #ifdef CONFIG_RK_AVB_LIBAVB_USER 22 #include <android_avb/avb_slot_verify.h> 23 #include <android_avb/avb_ops_user.h> 24 #include <android_avb/rk_avb_ops_user.h> 25 #endif 26 #include <optee_include/OpteeClientInterface.h> 27 28 DECLARE_GLOBAL_DATA_PTR; 29 30 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000 31 #define ANDROID_ARG_FDT_FILENAME "rk-kernel.dtb" 32 #define ANDROID_Q_VER 10 33 #define ANDROID_PARTITION_VENDOR_BOOT "vendor_boot" 34 35 #define BLK_CNT(_num_bytes, _block_size) \ 36 ((_num_bytes + _block_size - 1) / _block_size) 37 38 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; 39 static u32 android_kernel_comp_type = IH_COMP_NONE; 40 41 u32 android_image_major_version(void) 42 { 43 /* MSB 7-bits */ 44 return gd->bd->bi_andr_version >> 25; 45 } 46 47 u32 android_bcb_msg_sector_offset(void) 48 { 49 /* 50 * Rockchip platforms defines BCB message at the 16KB offset of 51 * misc partition while the Google defines it at 0x00 offset. 52 * 53 * From Android-Q, the 0x00 offset is mandary on Google VTS, so that 54 * this is a compatibility according to android image 'os_version'. 55 */ 56 #ifdef CONFIG_RKIMG_BOOTLOADER 57 return (android_image_major_version() >= ANDROID_Q_VER) ? 0x00 : 0x20; 58 #else 59 return 0x00; 60 #endif 61 } 62 63 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr) 64 { 65 /* 66 * All the Android tools that generate a boot.img use this 67 * address as the default. 68 * 69 * Even though it doesn't really make a lot of sense, and it 70 * might be valid on some platforms, we treat that address as 71 * the default value for this field, and try to execute the 72 * kernel in place in such a case. 73 * 74 * Otherwise, we will return the actual value set by the user. 75 */ 76 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) 77 return (ulong)hdr + hdr->page_size; 78 79 #ifdef CONFIG_ARCH_ROCKCHIP 80 /* 81 * If kernel is compressed, kernel_addr is set as decompressed address 82 * after compressed being loaded to ram, so let's use it. 83 */ 84 if (android_kernel_comp_type != IH_COMP_NONE && 85 android_kernel_comp_type != IH_COMP_ZIMAGE) 86 return hdr->kernel_addr; 87 88 /* 89 * Compatble with rockchip legacy packing with kernel/ramdisk/second 90 * address base from 0x60000000(SDK versiont < 8.1), these are invalid 91 * address, so we calc it by real size. 92 */ 93 return (ulong)hdr + hdr->page_size; 94 #else 95 return hdr->kernel_addr; 96 #endif 97 98 } 99 100 void android_image_set_comp(struct andr_img_hdr *hdr, u32 comp) 101 { 102 android_kernel_comp_type = comp; 103 } 104 105 u32 android_image_get_comp(const struct andr_img_hdr *hdr) 106 { 107 return android_kernel_comp_type; 108 } 109 110 int android_image_parse_kernel_comp(const struct andr_img_hdr *hdr) 111 { 112 ulong kaddr = android_image_get_kernel_addr(hdr); 113 return bootm_parse_comp((const unsigned char *)kaddr); 114 } 115 116 /** 117 * android_image_get_kernel() - processes kernel part of Android boot images 118 * @hdr: Pointer to image header, which is at the start 119 * of the image. 120 * @verify: Checksum verification flag. Currently unimplemented. 121 * @os_data: Pointer to a ulong variable, will hold os data start 122 * address. 123 * @os_len: Pointer to a ulong variable, will hold os data length. 124 * 125 * This function returns the os image's start address and length. Also, 126 * it appends the kernel command line to the bootargs env variable. 127 * 128 * Return: Zero, os start address and length on success, 129 * otherwise on failure. 130 */ 131 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify, 132 ulong *os_data, ulong *os_len) 133 { 134 u32 kernel_addr = android_image_get_kernel_addr(hdr); 135 const char *cmdline = hdr->header_version < 3 ? 136 hdr->cmdline : hdr->total_cmdline; 137 /* 138 * Not all Android tools use the id field for signing the image with 139 * sha1 (or anything) so we don't check it. It is not obvious that the 140 * string is null terminated so we take care of this. 141 */ 142 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE); 143 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0'; 144 if (strlen(andr_tmp_str)) 145 printf("Android's image name: %s\n", andr_tmp_str); 146 147 printf("Kernel: 0x%08x - 0x%08x (%u KiB)\n", 148 kernel_addr, kernel_addr + hdr->kernel_size, 149 DIV_ROUND_UP(hdr->kernel_size, 1024)); 150 151 int len = 0; 152 if (cmdline) { 153 debug("Kernel command line: %s\n", cmdline); 154 len += strlen(cmdline); 155 } 156 157 char *bootargs = env_get("bootargs"); 158 if (bootargs) 159 len += strlen(bootargs); 160 161 char *newbootargs = malloc(len + 2); 162 if (!newbootargs) { 163 puts("Error: malloc in android_image_get_kernel failed!\n"); 164 return -ENOMEM; 165 } 166 *newbootargs = '\0'; 167 168 if (bootargs) { 169 strcpy(newbootargs, bootargs); 170 strcat(newbootargs, " "); 171 } 172 if (cmdline) 173 strcat(newbootargs, cmdline); 174 175 env_set("bootargs", newbootargs); 176 177 if (os_data) { 178 *os_data = (ulong)hdr; 179 *os_data += hdr->page_size; 180 } 181 if (os_len) 182 *os_len = hdr->kernel_size; 183 return 0; 184 } 185 186 int android_image_check_header(const struct andr_img_hdr *hdr) 187 { 188 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE); 189 } 190 191 ulong android_image_get_end(const struct andr_img_hdr *hdr) 192 { 193 ulong end; 194 /* 195 * The header takes a full page, the remaining components are aligned 196 * on page boundary 197 */ 198 end = (ulong)hdr; 199 if (hdr->header_version < 3) { 200 end += hdr->page_size; 201 end += ALIGN(hdr->kernel_size, hdr->page_size); 202 end += ALIGN(hdr->ramdisk_size, hdr->page_size); 203 end += ALIGN(hdr->second_size, hdr->page_size); 204 if (hdr->header_version == 1) { 205 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); 206 } else if (hdr->header_version == 2) { 207 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); 208 end += ALIGN(hdr->dtb_size, hdr->page_size); 209 } 210 } else { 211 /* boot_img_hdr_v34 */ 212 end += hdr->page_size; 213 end += ALIGN(hdr->kernel_size, hdr->page_size); 214 end += ALIGN(hdr->ramdisk_size, hdr->page_size); 215 } 216 217 return end; 218 } 219 220 u32 android_image_get_ksize(const struct andr_img_hdr *hdr) 221 { 222 return hdr->kernel_size; 223 } 224 225 void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address) 226 { 227 hdr->kernel_addr = load_address; 228 } 229 230 ulong android_image_get_kload(const struct andr_img_hdr *hdr) 231 { 232 return android_image_get_kernel_addr(hdr); 233 } 234 235 int android_image_get_ramdisk(const struct andr_img_hdr *hdr, 236 ulong *rd_data, ulong *rd_len) 237 { 238 ulong ramdisk_addr_r; 239 ulong start, end; 240 241 if (!hdr->ramdisk_size) { 242 *rd_data = *rd_len = 0; 243 return -1; 244 } 245 246 /* Have been loaded by android_image_load_separate() on ramdisk_addr_r */ 247 ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0); 248 if (!ramdisk_addr_r) { 249 printf("No Found Ramdisk Load Address.\n"); 250 return -1; 251 } 252 253 *rd_data = ramdisk_addr_r; 254 *rd_len = hdr->ramdisk_size; 255 if (hdr->header_version >= 3) 256 *rd_len += hdr->vendor_ramdisk_size; 257 if (hdr->header_version >= 4) { 258 *rd_len += hdr->vendor_bootconfig_size + 259 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 260 } 261 262 /* just for print msg */ 263 start = ramdisk_addr_r; 264 if (hdr->header_version >= 3) { 265 end = start + (ulong)hdr->vendor_ramdisk_size; 266 printf("v-ramdisk: 0x%08lx - 0x%08lx (%u KiB)\n", 267 start, end, DIV_ROUND_UP(hdr->vendor_ramdisk_size, 1024)); 268 start = end; 269 } 270 { 271 end = start + (ulong)hdr->ramdisk_size; 272 printf("ramdisk: 0x%08lx - 0x%08lx (%u KiB)\n", 273 start, end, DIV_ROUND_UP(hdr->ramdisk_size, 1024)); 274 start = end; 275 } 276 if (hdr->header_version >= 4) { 277 end = start + (ulong)hdr->vendor_bootconfig_size; 278 printf("bootconfig: 0x%08lx - 0x%08lx (%u KiB)\n", 279 start, end, DIV_ROUND_UP(hdr->vendor_bootconfig_size, 1024)); 280 start = end; 281 end = start + ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 282 printf("bootparams: 0x%08lx - 0x%08lx\n", start, end); 283 } 284 285 return 0; 286 } 287 288 int android_image_get_fdt(const struct andr_img_hdr *hdr, 289 ulong *rd_data) 290 { 291 ulong fdt_addr_r; 292 293 if (!hdr->second_size) { 294 *rd_data = 0; 295 return -1; 296 } 297 298 /* Have been loaded by android_image_load_separate() on fdt_addr_r */ 299 fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0); 300 if (!fdt_addr_r) { 301 printf("No Found FDT Load Address.\n"); 302 return -1; 303 } 304 305 *rd_data = fdt_addr_r; 306 307 debug("FDT load addr 0x%08x size %u KiB\n", 308 hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024)); 309 310 return 0; 311 } 312 313 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) 314 static void print_hash(const char *label, u8 *hash, int len) 315 { 316 int i; 317 318 printf("%s:\n 0x", label ? : "Hash"); 319 for (i = 0; i < len; i++) 320 printf("%02x", hash[i]); 321 printf("\n"); 322 } 323 #endif 324 325 typedef enum { 326 IMG_KERNEL, 327 IMG_RAMDISK, 328 IMG_SECOND, 329 IMG_RECOVERY_DTBO, 330 IMG_RK_DTB, /* within resource.img in second position */ 331 IMG_DTB, 332 IMG_VENDOR_RAMDISK, 333 IMG_BOOTCONFIG, 334 IMG_MAX, 335 } img_t; 336 337 static int image_load(img_t img, struct andr_img_hdr *hdr, 338 ulong blkstart, void *ram_base, 339 struct udevice *crypto) 340 { 341 struct blk_desc *desc = rockchip_get_bootdev(); 342 disk_partition_t part_vendor_boot; 343 __maybe_unused u32 typesz; 344 ulong pgsz = hdr->page_size; 345 ulong blksz = desc->blksz; 346 ulong blkcnt, blkoff; 347 ulong memmove_dst = 0; 348 ulong bsoffs = 0; 349 ulong extra = 0; 350 ulong length; 351 void *buffer; 352 int ret = 0; 353 354 switch (img) { 355 case IMG_KERNEL: 356 bsoffs = 0; /* include a page_size(image header) */ 357 length = hdr->kernel_size + pgsz; 358 buffer = (void *)env_get_ulong("android_addr_r", 16, 0); 359 blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz); 360 typesz = sizeof(hdr->kernel_size); 361 if (!sysmem_alloc_base(MEM_KERNEL, 362 (phys_addr_t)buffer, blkcnt * blksz)) 363 return -ENOMEM; 364 break; 365 case IMG_VENDOR_RAMDISK: 366 if (part_get_info_by_name(desc, 367 ANDROID_PARTITION_VENDOR_BOOT, 368 &part_vendor_boot) < 0) { 369 printf("No vendor boot partition\n"); 370 return -ENOENT; 371 } 372 /* Always load vendor boot from storage: avb full load boot/recovery */ 373 blkstart = part_vendor_boot.start; 374 pgsz = hdr->vendor_page_size; 375 ram_base = 0; 376 377 bsoffs = ALIGN(VENDOR_BOOT_HDRv3_SIZE, pgsz); 378 length = hdr->vendor_ramdisk_size; 379 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 380 blkcnt = DIV_ROUND_UP(hdr->vendor_ramdisk_size, blksz); 381 typesz = sizeof(hdr->vendor_ramdisk_size); 382 /* 383 * Add extra memory for generic ramdisk space. 384 * 385 * In case of unaligned vendor ramdisk size, reserve 386 * 1 more blksz. 387 * 388 * Reserve 8KB for bootloader cmdline. 389 */ 390 if (hdr->header_version >= 3) 391 extra += ALIGN(hdr->ramdisk_size, blksz) + blksz; 392 if (hdr->header_version >= 4) 393 extra += ALIGN(hdr->vendor_bootconfig_size, blksz) + 394 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 395 if (length && !sysmem_alloc_base(MEM_RAMDISK, 396 (phys_addr_t)buffer, blkcnt * blksz + extra)) 397 return -ENOMEM; 398 break; 399 case IMG_RAMDISK: 400 bsoffs = pgsz + ALIGN(hdr->kernel_size, pgsz); 401 length = hdr->ramdisk_size; 402 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 403 blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz); 404 typesz = sizeof(hdr->ramdisk_size); 405 406 /* 407 * ramdisk_addr_r v012: 408 * |----------------| 409 * | ramdisk | 410 * |----------------| 411 * 412 * ramdisk_addr_r v3: 413 * |----------------|---------| 414 * | vendor-ramdisk | ramdisk | 415 * |----------------|---------| 416 * 417 * ramdisk_addr_r v4: 418 * |----------------|---------|------------|------------| 419 * | vendor-ramdisk | ramdisk | bootconfig | bootparams | 420 * |----------------|---------|------------|------------| 421 */ 422 if (hdr->header_version >= 3) { 423 buffer += hdr->vendor_ramdisk_size; 424 if (!IS_ALIGNED((ulong)buffer, blksz)) { 425 memmove_dst = (ulong)buffer; 426 buffer = (void *)ALIGN(memmove_dst, blksz); 427 } 428 } 429 /* sysmem has been alloced by vendor ramdisk */ 430 if (hdr->header_version < 3) { 431 if (length && !sysmem_alloc_base(MEM_RAMDISK, 432 (phys_addr_t)buffer, blkcnt * blksz)) 433 return -ENOMEM; 434 } 435 break; 436 case IMG_BOOTCONFIG: 437 if (hdr->header_version < 4) 438 return 0; 439 if (part_get_info_by_name(desc, 440 ANDROID_PARTITION_VENDOR_BOOT, 441 &part_vendor_boot) < 0) { 442 printf("No vendor boot partition\n"); 443 return -ENOENT; 444 } 445 446 blkstart = part_vendor_boot.start; 447 pgsz = hdr->vendor_page_size; 448 ram_base = 0; 449 450 bsoffs = ALIGN(VENDOR_BOOT_HDRv4_SIZE, pgsz) + 451 ALIGN(hdr->vendor_ramdisk_size, pgsz) + 452 ALIGN(hdr->dtb_size, pgsz) + 453 ALIGN(hdr->vendor_ramdisk_table_size, pgsz); 454 length = hdr->vendor_bootconfig_size; 455 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 456 blkcnt = DIV_ROUND_UP(hdr->vendor_bootconfig_size, blksz); 457 typesz = sizeof(hdr->vendor_bootconfig_size); 458 459 buffer += hdr->vendor_ramdisk_size + hdr->ramdisk_size; 460 if (!IS_ALIGNED((ulong)buffer, blksz)) { 461 memmove_dst = (ulong)buffer; 462 buffer = (void *)ALIGN(memmove_dst, blksz); 463 } 464 break; 465 case IMG_SECOND: 466 bsoffs = pgsz + 467 ALIGN(hdr->kernel_size, pgsz) + 468 ALIGN(hdr->ramdisk_size, pgsz); 469 length = hdr->second_size; 470 blkcnt = DIV_ROUND_UP(hdr->second_size, blksz); 471 buffer = malloc(blkcnt * blksz); 472 typesz = sizeof(hdr->second_size); 473 break; 474 case IMG_RECOVERY_DTBO: 475 bsoffs = pgsz + 476 ALIGN(hdr->kernel_size, pgsz) + 477 ALIGN(hdr->ramdisk_size, pgsz) + 478 ALIGN(hdr->second_size, pgsz); 479 length = hdr->recovery_dtbo_size; 480 blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz); 481 buffer = malloc(blkcnt * blksz); 482 typesz = sizeof(hdr->recovery_dtbo_size); 483 break; 484 case IMG_DTB: 485 bsoffs = pgsz + 486 ALIGN(hdr->kernel_size, pgsz) + 487 ALIGN(hdr->ramdisk_size, pgsz) + 488 ALIGN(hdr->second_size, pgsz) + 489 ALIGN(hdr->recovery_dtbo_size, pgsz); 490 length = hdr->dtb_size; 491 blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz); 492 buffer = malloc(blkcnt * blksz); 493 typesz = sizeof(hdr->dtb_size); 494 break; 495 case IMG_RK_DTB: 496 #ifdef CONFIG_RKIMG_BOOTLOADER 497 /* No going further, it handles DTBO, HW-ID, etc */ 498 buffer = (void *)env_get_ulong("fdt_addr_r", 16, 0); 499 if (gd->fdt_blob != (void *)buffer) 500 ret = rockchip_read_dtb_file(buffer); 501 #endif 502 return ret < 0 ? ret : 0; 503 default: 504 return -EINVAL; 505 } 506 507 if (!buffer) { 508 printf("No memory for image(%d)\n", img); 509 return -ENOMEM; 510 } 511 512 if (!blksz || !length) 513 goto crypto_calc; 514 515 /* load */ 516 if (ram_base) { 517 memcpy(buffer, (char *)((ulong)ram_base + bsoffs), length); 518 } else { 519 blkoff = DIV_ROUND_UP(bsoffs, blksz); 520 ret = blk_dread(desc, blkstart + blkoff, blkcnt, buffer); 521 if (ret != blkcnt) { 522 printf("Failed to read img(%d), ret=%d\n", img, ret); 523 return -EIO; 524 } 525 } 526 527 if (memmove_dst) 528 memmove((char *)memmove_dst, buffer, length); 529 530 crypto_calc: 531 /* sha1 */ 532 #ifdef CONFIG_DM_CRYPTO 533 if (crypto) { 534 if (img == IMG_KERNEL) { 535 buffer += pgsz; 536 length -= pgsz; 537 } 538 539 crypto_sha_update(crypto, (u32 *)buffer, length); 540 crypto_sha_update(crypto, (u32 *)&length, typesz); 541 } 542 #endif 543 544 return 0; 545 } 546 547 static int images_load_verify(struct andr_img_hdr *hdr, ulong part_start, 548 void *ram_base, struct udevice *crypto) 549 { 550 /* load, never change order ! */ 551 if (image_load(IMG_KERNEL, hdr, part_start, ram_base, crypto)) 552 return -1; 553 if (image_load(IMG_RAMDISK, hdr, part_start, ram_base, crypto)) 554 return -1; 555 if (image_load(IMG_SECOND, hdr, part_start, ram_base, crypto)) 556 return -1; 557 if (hdr->header_version > 0) { 558 if (image_load(IMG_RECOVERY_DTBO, hdr, part_start, 559 ram_base, crypto)) 560 return -1; 561 } 562 if (hdr->header_version > 1) { 563 if (image_load(IMG_DTB, hdr, part_start, ram_base, crypto)) 564 return -1; 565 } 566 567 return 0; 568 } 569 570 /* 571 * @ram_base: !NULL means require memcpy for an exist full android image. 572 */ 573 static int android_image_separate(struct andr_img_hdr *hdr, 574 const disk_partition_t *part, 575 void *load_address, 576 void *ram_base) 577 { 578 ulong bstart; 579 int ret; 580 581 if (android_image_check_header(hdr)) { 582 printf("Bad android image header\n"); 583 return -EINVAL; 584 } 585 586 /* set for image_load(IMG_KERNEL, ...) */ 587 env_set_hex("android_addr_r", (ulong)load_address); 588 bstart = part ? part->start : 0; 589 590 /* 591 * 1. Load images to their individual target ram position 592 * in order to disable fdt/ramdisk relocation. 593 */ 594 595 /* load rk-kernel.dtb alone */ 596 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 597 return -1; 598 599 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) 600 if (hdr->header_version < 3) { 601 struct udevice *dev; 602 sha_context ctx; 603 uchar hash[20]; 604 605 ctx.length = 0; 606 ctx.algo = CRYPTO_SHA1; 607 dev = crypto_get_device(ctx.algo); 608 if (!dev) { 609 printf("Can't find crypto device for SHA1\n"); 610 return -ENODEV; 611 } 612 613 /* v1 & v2: requires total length before sha init */ 614 ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) + 615 hdr->ramdisk_size + sizeof(hdr->ramdisk_size) + 616 hdr->second_size + sizeof(hdr->second_size); 617 if (hdr->header_version > 0) 618 ctx.length += hdr->recovery_dtbo_size + 619 sizeof(hdr->recovery_dtbo_size); 620 if (hdr->header_version > 1) 621 ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size); 622 623 crypto_sha_init(dev, &ctx); 624 ret = images_load_verify(hdr, bstart, ram_base, dev); 625 if (ret) 626 return ret; 627 crypto_sha_final(dev, &ctx, hash); 628 629 if (memcmp(hash, hdr->id, 20)) { 630 print_hash("Hash from header", (u8 *)hdr->id, 20); 631 print_hash("Hash real", (u8 *)hash, 20); 632 return -EBADFD; 633 } else { 634 printf("ANDROID: Hash OK\n"); 635 } 636 } else 637 #endif 638 { 639 ret = images_load_verify(hdr, bstart, ram_base, NULL); 640 if (ret) 641 return ret; 642 } 643 644 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 645 env_set("bootm-no-reloc", "y"); 646 647 return 0; 648 } 649 650 static int android_image_separate_v34(struct andr_img_hdr *hdr, 651 const disk_partition_t *part, 652 void *load_address, void *ram_base) 653 { 654 ulong bstart; 655 656 if (android_image_check_header(hdr)) { 657 printf("Bad android image header\n"); 658 return -EINVAL; 659 } 660 661 /* set for image_load(IMG_KERNEL, ...) */ 662 env_set_hex("android_addr_r", (ulong)load_address); 663 bstart = part ? part->start : 0; 664 665 /* 666 * 1. Load images to their individual target ram position 667 * in order to disable fdt/ramdisk relocation. 668 */ 669 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 670 return -1; 671 if (image_load(IMG_KERNEL, hdr, bstart, ram_base, NULL)) 672 return -1; 673 if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base, NULL)) 674 return -1; 675 if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL)) 676 return -1; 677 if (image_load(IMG_BOOTCONFIG, hdr, bstart, ram_base, NULL)) 678 return -1; 679 /* 680 * Copy the populated hdr to load address after image_load(IMG_KERNEL) 681 * 682 * The image_load(IMG_KERNEL) only reads boot_img_hdr_v34 while 683 * vendor_boot_img_hdr_v34 is not included, so fix it here. 684 */ 685 memcpy((char *)load_address, hdr, hdr->page_size); 686 687 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 688 env_set("bootm-no-reloc", "y"); 689 690 return 0; 691 } 692 693 static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp) 694 { 695 ulong kernel_addr_c; 696 ulong load_addr = 0; 697 698 kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0); 699 700 #ifdef CONFIG_ARM64 701 /* 702 * On 64-bit kernel, assuming use IMAGE by default. 703 * 704 * kernel_addr_c is for LZ4-IMAGE but maybe not defined. 705 * kernel_addr_r is for IMAGE. 706 */ 707 if (comp != IH_COMP_NONE) { 708 ulong comp_addr; 709 710 if (kernel_addr_c) { 711 comp_addr = kernel_addr_c; 712 } else { 713 printf("Warn: No \"kernel_addr_c\"\n"); 714 comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */ 715 env_set_hex("kernel_addr_c", comp_addr); 716 } 717 718 load_addr = comp_addr - hdr->page_size; 719 } 720 #else 721 /* 722 * On 32-bit kernel: 723 * 724 * The input load_addr is from env value: "kernel_addr_r", it has 725 * different role depends on whether kernel_addr_c is defined: 726 * 727 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined. 728 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined. 729 */ 730 if (comp == IH_COMP_NONE) { 731 if (kernel_addr_c) { 732 /* input load_addr is for Image, nothing to do */ 733 } else { 734 /* input load_addr is for lz4/zImage, set default addr for Image */ 735 load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000; 736 env_set_hex("kernel_addr_r", load_addr); 737 738 load_addr -= hdr->page_size; 739 } 740 } else { 741 if (kernel_addr_c) { 742 /* input load_addr is for Image, so use another for lz4/zImage */ 743 load_addr = kernel_addr_c - hdr->page_size; 744 } else { 745 /* input load_addr is for lz4/zImage, nothing to do */ 746 } 747 } 748 #endif 749 750 return load_addr; 751 } 752 753 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp) 754 { 755 ulong kernel_addr_r; 756 757 env_set_ulong("os_comp", comp); 758 759 /* zImage handles decompress itself */ 760 if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) { 761 kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000); 762 android_image_set_kload(hdr, kernel_addr_r); 763 android_image_set_comp(hdr, comp); 764 } else { 765 android_image_set_comp(hdr, IH_COMP_NONE); 766 } 767 } 768 769 static int android_image_load_separate(struct andr_img_hdr *hdr, 770 const disk_partition_t *part, 771 void *load_addr) 772 { 773 if (hdr->header_version < 3) 774 return android_image_separate(hdr, part, load_addr, NULL); 775 else 776 return android_image_separate_v34(hdr, part, load_addr, NULL); 777 } 778 779 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr) 780 { 781 ulong comp_addr; 782 int comp; 783 784 comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size); 785 comp_addr = android_image_get_comp_addr(hdr, comp); 786 787 /* non-compressed image: already in-place */ 788 if ((ulong)hdr == *load_addr) 789 return 0; 790 791 /* compressed image */ 792 if (comp_addr) { 793 *load_addr = comp_addr; 794 if ((ulong)hdr == comp_addr) /* already in-place */ 795 return 0; 796 } 797 798 /* 799 * The most possible reason to arrive here is: 800 * 801 * VBoot=1 and AVB load full partition to a temp memory buffer, now we 802 * separate(memcpy) subimages from boot.img to where they should be. 803 */ 804 if (hdr->header_version < 3) { 805 if (android_image_separate(hdr, NULL, (void *)(*load_addr), hdr)) 806 return -1; 807 } else { 808 if (android_image_separate_v34(hdr, NULL, (void *)(*load_addr), hdr)) 809 return -1; 810 } 811 812 android_image_set_decomp((void *)(*load_addr), comp); 813 814 return 0; 815 } 816 817 long android_image_load(struct blk_desc *dev_desc, 818 const disk_partition_t *part_info, 819 unsigned long load_address, 820 unsigned long max_size) { 821 struct andr_img_hdr *hdr; 822 ulong comp_addr; 823 int comp, ret; 824 int blk_off; 825 826 if (max_size < part_info->blksz) 827 return -1; 828 829 hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info); 830 if (!hdr) { 831 printf("No valid android hdr\n"); 832 return -1; 833 } 834 835 /* 836 * create the layout: 837 * 838 * |<- page_size ->|1-blk | 839 * |-----|---------|------|-----| 840 * | hdr | ... | kernel | 841 * |-----|----- ---|------------| 842 * 843 * Alloc page_size and 1 more blk for reading kernel image to 844 * get it's compression type, then fill the android hdr what 845 * we have populated before. 846 * 847 * Why? see: android_image_get_kernel_addr(). 848 */ 849 blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz); 850 hdr = (struct andr_img_hdr *) 851 realloc(hdr, (blk_off + 1) * dev_desc->blksz); 852 if (!hdr) 853 return -1; 854 855 if (blk_dread(dev_desc, part_info->start + blk_off, 1, 856 (char *)hdr + hdr->page_size) != 1) { 857 free(hdr); 858 return -1; 859 } 860 861 /* Changed to compressed address ? */ 862 comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size); 863 comp_addr = android_image_get_comp_addr(hdr, comp); 864 if (comp_addr) 865 load_address = comp_addr; 866 else 867 load_address -= hdr->page_size; 868 869 ret = android_image_load_separate(hdr, part_info, (void *)load_address); 870 if (ret) { 871 printf("Failed to load android image\n"); 872 goto fail; 873 } 874 android_image_set_decomp((void *)load_address, comp); 875 876 debug("Loading Android Image to 0x%08lx\n", load_address); 877 878 free(hdr); 879 return load_address; 880 881 fail: 882 free(hdr); 883 return -1; 884 } 885 886 static struct andr_img_hdr * 887 extract_boot_image_v012_header(struct blk_desc *dev_desc, 888 const disk_partition_t *boot_img) 889 { 890 struct andr_img_hdr *hdr; 891 long blk_cnt, blks_read; 892 893 blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz); 894 hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz); 895 896 if (!blk_cnt || !hdr) 897 return NULL; 898 899 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr); 900 if (blks_read != blk_cnt) { 901 debug("boot img header blk cnt is %ld and blks read is %ld\n", 902 blk_cnt, blks_read); 903 return NULL; 904 } 905 906 if (android_image_check_header((void *)hdr)) { 907 printf("boot header magic is invalid.\n"); 908 return NULL; 909 } 910 911 if (hdr->page_size < sizeof(*hdr)) { 912 printf("android hdr is over size\n"); 913 return NULL; 914 } 915 916 return hdr; 917 } 918 919 static struct boot_img_hdr_v34 * 920 extract_boot_image_v34_header(struct blk_desc *dev_desc, 921 const disk_partition_t *boot_img) 922 { 923 struct boot_img_hdr_v34 *boot_hdr; 924 long blk_cnt, blks_read; 925 926 blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v34), dev_desc->blksz); 927 boot_hdr = (struct boot_img_hdr_v34 *)malloc(blk_cnt * dev_desc->blksz); 928 929 if (!blk_cnt || !boot_hdr) 930 return NULL; 931 932 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_hdr); 933 if (blks_read != blk_cnt) { 934 debug("boot img header blk cnt is %ld and blks read is %ld\n", 935 blk_cnt, blks_read); 936 return NULL; 937 } 938 939 if (android_image_check_header((void *)boot_hdr)) { 940 printf("boot header magic is invalid.\n"); 941 return NULL; 942 } 943 944 if (boot_hdr->header_version < 3) { 945 printf("boot header %d, is not >= v3.\n", 946 boot_hdr->header_version); 947 return NULL; 948 } 949 950 return boot_hdr; 951 } 952 953 static struct vendor_boot_img_hdr_v34 * 954 extract_vendor_boot_image_v34_header(struct blk_desc *dev_desc, 955 const disk_partition_t *part_vendor_boot) 956 { 957 struct vendor_boot_img_hdr_v34 *vboot_hdr; 958 long blk_cnt, blks_read; 959 960 blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v34), 961 part_vendor_boot->blksz); 962 vboot_hdr = (struct vendor_boot_img_hdr_v34 *) 963 malloc(blk_cnt * part_vendor_boot->blksz); 964 965 if (!blk_cnt || !vboot_hdr) 966 return NULL; 967 968 blks_read = blk_dread(dev_desc, part_vendor_boot->start, 969 blk_cnt, vboot_hdr); 970 if (blks_read != blk_cnt) { 971 debug("vboot img header blk cnt is %ld and blks read is %ld\n", 972 blk_cnt, blks_read); 973 return NULL; 974 } 975 976 if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic, 977 VENDOR_BOOT_MAGIC_SIZE)) { 978 printf("vendor boot header is invalid.\n"); 979 return NULL; 980 } 981 982 if (vboot_hdr->header_version < 3) { 983 printf("vendor boot header %d, is not >= v3.\n", 984 vboot_hdr->header_version); 985 return NULL; 986 } 987 988 return vboot_hdr; 989 } 990 991 static int populate_boot_info(const struct boot_img_hdr_v34 *boot_hdr, 992 const struct vendor_boot_img_hdr_v34 *vendor_hdr, 993 struct andr_img_hdr *hdr) 994 { 995 memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE); 996 memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE); 997 998 hdr->kernel_size = boot_hdr->kernel_size; 999 /* don't use vendor_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */ 1000 hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR; 1001 /* generic ramdisk: immediately following the vendor ramdisk */ 1002 hdr->boot_ramdisk_size = boot_hdr->ramdisk_size; 1003 hdr->ramdisk_size = boot_hdr->ramdisk_size; 1004 1005 /* actually, useless */ 1006 hdr->ramdisk_addr = env_get_ulong("ramdisk_addr_r", 16, 0); 1007 1008 /* removed in v3 */ 1009 hdr->second_size = 0; 1010 hdr->second_addr = 0; 1011 1012 hdr->tags_addr = vendor_hdr->tags_addr; 1013 1014 /* fixed in v3 */ 1015 hdr->page_size = 4096; 1016 hdr->header_version = boot_hdr->header_version; 1017 hdr->os_version = boot_hdr->os_version; 1018 1019 memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE); 1020 strncpy(hdr->name, (const char *)vendor_hdr->name, ANDR_BOOT_NAME_SIZE); 1021 1022 /* removed in v3 */ 1023 memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE); 1024 memset(hdr->id, 0, 32); 1025 memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE); 1026 hdr->recovery_dtbo_size = 0; 1027 hdr->recovery_dtbo_offset = 0; 1028 1029 hdr->header_size = boot_hdr->header_size; 1030 hdr->dtb_size = vendor_hdr->dtb_size; 1031 hdr->dtb_addr = vendor_hdr->dtb_addr; 1032 1033 /* boot_img_hdr_v34 fields */ 1034 hdr->vendor_ramdisk_size = vendor_hdr->vendor_ramdisk_size; 1035 hdr->vendor_page_size = vendor_hdr->page_size; 1036 hdr->vendor_header_version = vendor_hdr->header_version; 1037 hdr->vendor_header_size = vendor_hdr->header_size; 1038 1039 hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE); 1040 if (!hdr->total_cmdline) 1041 return -ENOMEM; 1042 strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline, 1043 sizeof(boot_hdr->cmdline)); 1044 strncat(hdr->total_cmdline, " ", 1); 1045 strncat(hdr->total_cmdline, (const char *)vendor_hdr->cmdline, 1046 sizeof(vendor_hdr->cmdline)); 1047 1048 /* new for header v4 */ 1049 if (vendor_hdr->header_version > 3) { 1050 hdr->vendor_ramdisk_table_size = 1051 vendor_hdr->vendor_ramdisk_table_size; 1052 hdr->vendor_ramdisk_table_entry_num = 1053 vendor_hdr->vendor_ramdisk_table_entry_num; 1054 hdr->vendor_ramdisk_table_entry_size = 1055 vendor_hdr->vendor_ramdisk_table_entry_size; 1056 /* 1057 * If we place additional "androidboot.xxx" parameters after 1058 * bootconfig, this field value should be increased, 1059 * but not over than ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE. 1060 */ 1061 hdr->vendor_bootconfig_size = 1062 vendor_hdr->vendor_bootconfig_size; 1063 } else { 1064 hdr->vendor_ramdisk_table_size = 0; 1065 hdr->vendor_ramdisk_table_entry_num = 0; 1066 hdr->vendor_ramdisk_table_entry_size = 0; 1067 hdr->vendor_bootconfig_size = 0; 1068 } 1069 1070 if (hdr->page_size < sizeof(*hdr)) { 1071 printf("android hdr is over size\n"); 1072 return -EINVAL; 1073 } 1074 1075 return 0; 1076 } 1077 1078 /* 1079 * The possible cases of boot.img + recovery.img: 1080 * 1081 * [N]: 0, 1, 2 1082 * [M]: 0, 1, 2, 3, 4 1083 * 1084 * |--------------------|---------------------| 1085 * | boot.img | recovery.img | 1086 * |--------------------|---------------------| 1087 * | boot_img_hdr_v[N] | boot_img_hdr_v[N] | <= if A/B is not required 1088 * |--------------------|---------------------| 1089 * | boot_img_hdr_v34 | boot_img_hdr_v2 | <= if A/B is not required 1090 * |------------------------------------------| 1091 * | boot_img_hdr_v[M], no recovery.img | <= if A/B is required 1092 * |------------------------------------------| 1093 */ 1094 struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc, 1095 disk_partition_t *part_boot) 1096 { 1097 disk_partition_t part_vendor_boot; 1098 struct vendor_boot_img_hdr_v34 *vboot_hdr; 1099 struct boot_img_hdr_v34 *boot_hdr; 1100 struct andr_img_hdr *andr_hdr; 1101 int header_version; 1102 1103 if (!dev_desc || !part_boot) 1104 return NULL; 1105 1106 andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz); 1107 if (!andr_hdr) 1108 return NULL; 1109 1110 if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) { 1111 free(andr_hdr); 1112 return NULL; 1113 } 1114 1115 if (android_image_check_header(andr_hdr)) { 1116 free(andr_hdr); 1117 return NULL; 1118 } 1119 1120 header_version = andr_hdr->header_version; 1121 free(andr_hdr); 1122 1123 if (header_version < 3) { 1124 return extract_boot_image_v012_header(dev_desc, part_boot); 1125 } else { 1126 if (part_get_info_by_name(dev_desc, 1127 ANDROID_PARTITION_VENDOR_BOOT, 1128 &part_vendor_boot) < 0) { 1129 printf("No vendor boot partition\n"); 1130 return NULL; 1131 } 1132 boot_hdr = extract_boot_image_v34_header(dev_desc, part_boot); 1133 vboot_hdr = extract_vendor_boot_image_v34_header(dev_desc, 1134 &part_vendor_boot); 1135 if (!boot_hdr || !vboot_hdr) 1136 goto image_load_exit; 1137 1138 andr_hdr = (struct andr_img_hdr *) 1139 malloc(sizeof(struct andr_img_hdr)); 1140 if (!andr_hdr) { 1141 printf("No memory for andr hdr\n"); 1142 goto image_load_exit; 1143 } 1144 1145 if (populate_boot_info(boot_hdr, vboot_hdr, andr_hdr)) { 1146 printf("populate boot info failed\n"); 1147 goto image_load_exit; 1148 } 1149 1150 free(boot_hdr); 1151 free(vboot_hdr); 1152 1153 return andr_hdr; 1154 1155 image_load_exit: 1156 free(boot_hdr); 1157 free(vboot_hdr); 1158 1159 return NULL; 1160 } 1161 1162 return NULL; 1163 } 1164 1165 #if !defined(CONFIG_SPL_BUILD) 1166 /** 1167 * android_print_contents - prints out the contents of the Android format image 1168 * @hdr: pointer to the Android format image header 1169 * 1170 * android_print_contents() formats a multi line Android image contents 1171 * description. 1172 * The routine prints out Android image properties 1173 * 1174 * returns: 1175 * no returned results 1176 */ 1177 void android_print_contents(const struct andr_img_hdr *hdr) 1178 { 1179 const char * const p = IMAGE_INDENT_STRING; 1180 /* os_version = ver << 11 | lvl */ 1181 u32 os_ver = hdr->os_version >> 11; 1182 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 1183 u32 header_version = hdr->header_version; 1184 1185 printf("%skernel size: %x\n", p, hdr->kernel_size); 1186 printf("%skernel address: %x\n", p, hdr->kernel_addr); 1187 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); 1188 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr); 1189 printf("%ssecond size: %x\n", p, hdr->second_size); 1190 printf("%ssecond address: %x\n", p, hdr->second_addr); 1191 printf("%stags address: %x\n", p, hdr->tags_addr); 1192 printf("%spage size: %x\n", p, hdr->page_size); 1193 printf("%sheader_version: %x\n", p, header_version); 1194 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) 1195 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ 1196 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", 1197 p, hdr->os_version, 1198 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, 1199 (os_lvl >> 4) + 2000, os_lvl & 0x0F); 1200 printf("%sname: %s\n", p, hdr->name); 1201 printf("%scmdline: %s\n", p, hdr->cmdline); 1202 1203 if (header_version == 1 || header_version == 2) { 1204 printf("%srecovery dtbo size: %x\n", p, hdr->recovery_dtbo_size); 1205 printf("%srecovery dtbo offset: %llx\n", p, hdr->recovery_dtbo_offset); 1206 printf("%sheader size: %x\n", p, hdr->header_size); 1207 } 1208 1209 if (header_version == 2 || header_version == 3) { 1210 printf("%sdtb size: %x\n", p, hdr->dtb_size); 1211 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr); 1212 } 1213 1214 if (header_version >= 3) { 1215 printf("%scmdline: %s\n", p, hdr->total_cmdline); 1216 printf("%svendor ramdisk size: %x\n", p, hdr->vendor_ramdisk_size); 1217 printf("%svendor page size: %x\n", p, hdr->vendor_page_size); 1218 printf("%svendor header version: %d\n", p, hdr->vendor_header_version); 1219 printf("%svendor header size: %x\n", p, hdr->vendor_header_size); 1220 } 1221 1222 if (header_version >= 4) { 1223 printf("%svendor ramdisk table size: %x\n", 1224 p, hdr->vendor_ramdisk_table_size); 1225 printf("%svendor ramdisk table entry num: %x\n", 1226 p, hdr->vendor_ramdisk_table_entry_num); 1227 printf("%svendor ramdisk table entry size: %x\n", 1228 p, hdr->vendor_ramdisk_table_entry_size); 1229 printf("%svendor bootconfig size: %d\n", 1230 p, hdr->vendor_bootconfig_size); 1231 } 1232 } 1233 #endif 1234