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 hdr->vendor_ramdisk_size + 256 hdr->vendor_bootconfig_size + 257 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 258 259 start = ramdisk_addr_r; 260 if (hdr->header_version >= 3) { 261 end = start + (ulong)hdr->vendor_ramdisk_size; 262 printf("v-ramdisk: 0x%08lx - 0x%08lx (%u KiB)\n", 263 start, end, DIV_ROUND_UP(hdr->vendor_ramdisk_size, 1024)); 264 start = end; 265 } 266 { 267 end = start + (ulong)hdr->ramdisk_size; 268 printf("ramdisk: 0x%08lx - 0x%08lx (%u KiB)\n", 269 start, end, DIV_ROUND_UP(hdr->ramdisk_size, 1024)); 270 start = end; 271 } 272 if (hdr->header_version >= 4) { 273 end = start + (ulong)hdr->vendor_bootconfig_size; 274 printf("bootconfig: 0x%08lx - 0x%08lx (%u KiB)\n", 275 start, end, DIV_ROUND_UP(hdr->vendor_bootconfig_size, 1024)); 276 start = end; 277 end = start + ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 278 printf("bootparams: 0x%08lx - 0x%08lx\n", start, end); 279 } 280 281 return 0; 282 } 283 284 int android_image_get_fdt(const struct andr_img_hdr *hdr, 285 ulong *rd_data) 286 { 287 ulong fdt_addr_r; 288 289 if (!hdr->second_size) { 290 *rd_data = 0; 291 return -1; 292 } 293 294 /* Have been loaded by android_image_load_separate() on fdt_addr_r */ 295 fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0); 296 if (!fdt_addr_r) { 297 printf("No Found FDT Load Address.\n"); 298 return -1; 299 } 300 301 *rd_data = fdt_addr_r; 302 303 debug("FDT load addr 0x%08x size %u KiB\n", 304 hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024)); 305 306 return 0; 307 } 308 309 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) 310 static void print_hash(const char *label, u8 *hash, int len) 311 { 312 int i; 313 314 printf("%s:\n 0x", label ? : "Hash"); 315 for (i = 0; i < len; i++) 316 printf("%02x", hash[i]); 317 printf("\n"); 318 } 319 #endif 320 321 typedef enum { 322 IMG_KERNEL, 323 IMG_RAMDISK, 324 IMG_SECOND, 325 IMG_RECOVERY_DTBO, 326 IMG_RK_DTB, /* within resource.img in second position */ 327 IMG_DTB, 328 IMG_VENDOR_RAMDISK, 329 IMG_BOOTCONFIG, 330 IMG_MAX, 331 } img_t; 332 333 static int image_load(img_t img, struct andr_img_hdr *hdr, 334 ulong blkstart, void *ram_base, 335 struct udevice *crypto) 336 { 337 struct blk_desc *desc = rockchip_get_bootdev(); 338 disk_partition_t part_vendor_boot; 339 __maybe_unused u32 typesz; 340 ulong pgsz = hdr->page_size; 341 ulong blksz = desc->blksz; 342 ulong blkcnt, blkoff; 343 ulong memmove_dst = 0; 344 ulong bsoffs = 0; 345 ulong extra = 0; 346 ulong length; 347 void *buffer; 348 int ret = 0; 349 350 switch (img) { 351 case IMG_KERNEL: 352 bsoffs = 0; /* include a page_size(image header) */ 353 length = hdr->kernel_size + pgsz; 354 buffer = (void *)env_get_ulong("android_addr_r", 16, 0); 355 blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz); 356 typesz = sizeof(hdr->kernel_size); 357 if (!sysmem_alloc_base(MEM_KERNEL, 358 (phys_addr_t)buffer, blkcnt * blksz)) 359 return -ENOMEM; 360 break; 361 case IMG_VENDOR_RAMDISK: 362 if (part_get_info_by_name(desc, 363 ANDROID_PARTITION_VENDOR_BOOT, 364 &part_vendor_boot) < 0) { 365 printf("No vendor boot partition\n"); 366 return -ENOENT; 367 } 368 /* Always load vendor boot from storage: avb full load boot/recovery */ 369 blkstart = part_vendor_boot.start; 370 pgsz = hdr->vendor_page_size; 371 ram_base = 0; 372 373 bsoffs = ALIGN(VENDOR_BOOT_HDRv3_SIZE, pgsz); 374 length = hdr->vendor_ramdisk_size; 375 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 376 blkcnt = DIV_ROUND_UP(hdr->vendor_ramdisk_size, blksz); 377 typesz = sizeof(hdr->vendor_ramdisk_size); 378 /* 379 * Add extra memory for generic ramdisk space. 380 * 381 * In case of unaligned vendor ramdisk size, reserve 382 * 1 more blksz. 383 * 384 * Reserve 8KB for bootloader cmdline. 385 */ 386 if (hdr->header_version >= 3) 387 extra += ALIGN(hdr->ramdisk_size, blksz) + blksz; 388 if (hdr->header_version >= 4) 389 extra += ALIGN(hdr->vendor_bootconfig_size, blksz) + 390 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 391 if (length && !sysmem_alloc_base(MEM_RAMDISK, 392 (phys_addr_t)buffer, blkcnt * blksz + extra)) 393 return -ENOMEM; 394 break; 395 case IMG_RAMDISK: 396 bsoffs = pgsz + ALIGN(hdr->kernel_size, pgsz); 397 length = hdr->ramdisk_size; 398 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 399 blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz); 400 typesz = sizeof(hdr->ramdisk_size); 401 402 /* 403 * ramdisk_addr_r v012: 404 * |----------------| 405 * | ramdisk | 406 * |----------------| 407 * 408 * ramdisk_addr_r v3: 409 * |----------------|---------| 410 * | vendor-ramdisk | ramdisk | 411 * |----------------|---------| 412 * 413 * ramdisk_addr_r v4: 414 * |----------------|---------|------------|------------| 415 * | vendor-ramdisk | ramdisk | bootconfig | bootparams | 416 * |----------------|---------|------------|------------| 417 */ 418 if (hdr->header_version >= 3) { 419 buffer += hdr->vendor_ramdisk_size; 420 if (!IS_ALIGNED((ulong)buffer, blksz)) { 421 memmove_dst = (ulong)buffer; 422 buffer = (void *)ALIGN(memmove_dst, blksz); 423 } 424 } 425 /* sysmem has been alloced by vendor ramdisk */ 426 if (hdr->header_version < 3) { 427 if (length && !sysmem_alloc_base(MEM_RAMDISK, 428 (phys_addr_t)buffer, blkcnt * blksz)) 429 return -ENOMEM; 430 } 431 break; 432 case IMG_BOOTCONFIG: 433 if (hdr->header_version < 4) 434 return 0; 435 if (part_get_info_by_name(desc, 436 ANDROID_PARTITION_VENDOR_BOOT, 437 &part_vendor_boot) < 0) { 438 printf("No vendor boot partition\n"); 439 return -ENOENT; 440 } 441 442 blkstart = part_vendor_boot.start; 443 pgsz = hdr->vendor_page_size; 444 ram_base = 0; 445 446 bsoffs = ALIGN(VENDOR_BOOT_HDRv4_SIZE, pgsz) + 447 ALIGN(hdr->vendor_ramdisk_size, pgsz) + 448 ALIGN(hdr->dtb_size, pgsz) + 449 ALIGN(hdr->vendor_ramdisk_table_size, pgsz); 450 length = hdr->vendor_bootconfig_size; 451 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 452 blkcnt = DIV_ROUND_UP(hdr->vendor_bootconfig_size, blksz); 453 typesz = sizeof(hdr->vendor_bootconfig_size); 454 455 buffer += hdr->vendor_ramdisk_size + hdr->ramdisk_size; 456 if (!IS_ALIGNED((ulong)buffer, blksz)) { 457 memmove_dst = (ulong)buffer; 458 buffer = (void *)ALIGN(memmove_dst, blksz); 459 } 460 break; 461 case IMG_SECOND: 462 bsoffs = pgsz + 463 ALIGN(hdr->kernel_size, pgsz) + 464 ALIGN(hdr->ramdisk_size, pgsz); 465 length = hdr->second_size; 466 blkcnt = DIV_ROUND_UP(hdr->second_size, blksz); 467 buffer = malloc(blkcnt * blksz); 468 typesz = sizeof(hdr->second_size); 469 break; 470 case IMG_RECOVERY_DTBO: 471 bsoffs = pgsz + 472 ALIGN(hdr->kernel_size, pgsz) + 473 ALIGN(hdr->ramdisk_size, pgsz) + 474 ALIGN(hdr->second_size, pgsz); 475 length = hdr->recovery_dtbo_size; 476 blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz); 477 buffer = malloc(blkcnt * blksz); 478 typesz = sizeof(hdr->recovery_dtbo_size); 479 break; 480 case IMG_DTB: 481 bsoffs = pgsz + 482 ALIGN(hdr->kernel_size, pgsz) + 483 ALIGN(hdr->ramdisk_size, pgsz) + 484 ALIGN(hdr->second_size, pgsz) + 485 ALIGN(hdr->recovery_dtbo_size, pgsz); 486 length = hdr->dtb_size; 487 blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz); 488 buffer = malloc(blkcnt * blksz); 489 typesz = sizeof(hdr->dtb_size); 490 break; 491 case IMG_RK_DTB: 492 #ifdef CONFIG_RKIMG_BOOTLOADER 493 /* No going further, it handles DTBO, HW-ID, etc */ 494 buffer = (void *)env_get_ulong("fdt_addr_r", 16, 0); 495 if (gd->fdt_blob != (void *)buffer) 496 ret = rockchip_read_dtb_file(buffer); 497 #endif 498 return ret < 0 ? ret : 0; 499 default: 500 return -EINVAL; 501 } 502 503 if (!buffer) { 504 printf("No memory for image(%d)\n", img); 505 return -ENOMEM; 506 } 507 508 if (!blksz || !length) 509 goto crypto_calc; 510 511 /* load */ 512 if (ram_base) { 513 memcpy(buffer, (char *)((ulong)ram_base + bsoffs), length); 514 } else { 515 blkoff = DIV_ROUND_UP(bsoffs, blksz); 516 ret = blk_dread(desc, blkstart + blkoff, blkcnt, buffer); 517 if (ret != blkcnt) { 518 printf("Failed to read img(%d), ret=%d\n", img, ret); 519 return -EIO; 520 } 521 } 522 523 if (memmove_dst) 524 memmove((char *)memmove_dst, buffer, length); 525 526 crypto_calc: 527 /* sha1 */ 528 #ifdef CONFIG_DM_CRYPTO 529 if (crypto) { 530 if (img == IMG_KERNEL) { 531 buffer += pgsz; 532 length -= pgsz; 533 } 534 535 crypto_sha_update(crypto, (u32 *)buffer, length); 536 crypto_sha_update(crypto, (u32 *)&length, typesz); 537 } 538 #endif 539 540 return 0; 541 } 542 543 static int images_load_verify(struct andr_img_hdr *hdr, ulong part_start, 544 void *ram_base, struct udevice *crypto) 545 { 546 /* load, never change order ! */ 547 if (image_load(IMG_KERNEL, hdr, part_start, ram_base, crypto)) 548 return -1; 549 if (image_load(IMG_RAMDISK, hdr, part_start, ram_base, crypto)) 550 return -1; 551 if (image_load(IMG_SECOND, hdr, part_start, ram_base, crypto)) 552 return -1; 553 if (hdr->header_version > 0) { 554 if (image_load(IMG_RECOVERY_DTBO, hdr, part_start, 555 ram_base, crypto)) 556 return -1; 557 } 558 if (hdr->header_version > 1) { 559 if (image_load(IMG_DTB, hdr, part_start, ram_base, crypto)) 560 return -1; 561 } 562 563 return 0; 564 } 565 566 /* 567 * @ram_base: !NULL means require memcpy for an exist full android image. 568 */ 569 static int android_image_separate(struct andr_img_hdr *hdr, 570 const disk_partition_t *part, 571 void *load_address, 572 void *ram_base) 573 { 574 ulong bstart; 575 int ret; 576 577 if (android_image_check_header(hdr)) { 578 printf("Bad android image header\n"); 579 return -EINVAL; 580 } 581 582 /* set for image_load(IMG_KERNEL, ...) */ 583 env_set_hex("android_addr_r", (ulong)load_address); 584 bstart = part ? part->start : 0; 585 586 /* 587 * 1. Load images to their individual target ram position 588 * in order to disable fdt/ramdisk relocation. 589 */ 590 591 /* load rk-kernel.dtb alone */ 592 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 593 return -1; 594 595 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) 596 if (hdr->header_version < 3) { 597 struct udevice *dev; 598 sha_context ctx; 599 uchar hash[20]; 600 601 ctx.length = 0; 602 ctx.algo = CRYPTO_SHA1; 603 dev = crypto_get_device(ctx.algo); 604 if (!dev) { 605 printf("Can't find crypto device for SHA1\n"); 606 return -ENODEV; 607 } 608 609 /* v1 & v2: requires total length before sha init */ 610 ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) + 611 hdr->ramdisk_size + sizeof(hdr->ramdisk_size) + 612 hdr->second_size + sizeof(hdr->second_size); 613 if (hdr->header_version > 0) 614 ctx.length += hdr->recovery_dtbo_size + 615 sizeof(hdr->recovery_dtbo_size); 616 if (hdr->header_version > 1) 617 ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size); 618 619 crypto_sha_init(dev, &ctx); 620 ret = images_load_verify(hdr, bstart, ram_base, dev); 621 if (ret) 622 return ret; 623 crypto_sha_final(dev, &ctx, hash); 624 625 if (memcmp(hash, hdr->id, 20)) { 626 print_hash("Hash from header", (u8 *)hdr->id, 20); 627 print_hash("Hash real", (u8 *)hash, 20); 628 return -EBADFD; 629 } else { 630 printf("ANDROID: Hash OK\n"); 631 } 632 } else 633 #endif 634 { 635 ret = images_load_verify(hdr, bstart, ram_base, NULL); 636 if (ret) 637 return ret; 638 } 639 640 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 641 env_set("bootm-no-reloc", "y"); 642 643 return 0; 644 } 645 646 static int android_image_separate_v34(struct andr_img_hdr *hdr, 647 const disk_partition_t *part, 648 void *load_address, void *ram_base) 649 { 650 ulong bstart; 651 652 if (android_image_check_header(hdr)) { 653 printf("Bad android image header\n"); 654 return -EINVAL; 655 } 656 657 /* set for image_load(IMG_KERNEL, ...) */ 658 env_set_hex("android_addr_r", (ulong)load_address); 659 bstart = part ? part->start : 0; 660 661 /* 662 * 1. Load images to their individual target ram position 663 * in order to disable fdt/ramdisk relocation. 664 */ 665 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 666 return -1; 667 if (image_load(IMG_KERNEL, hdr, bstart, ram_base, NULL)) 668 return -1; 669 if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base, NULL)) 670 return -1; 671 if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL)) 672 return -1; 673 if (image_load(IMG_BOOTCONFIG, hdr, bstart, ram_base, NULL)) 674 return -1; 675 /* 676 * Copy the populated hdr to load address after image_load(IMG_KERNEL) 677 * 678 * The image_load(IMG_KERNEL) only reads boot_img_hdr_v34 while 679 * vendor_boot_img_hdr_v34 is not included, so fix it here. 680 */ 681 memcpy((char *)load_address, hdr, hdr->page_size); 682 683 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 684 env_set("bootm-no-reloc", "y"); 685 686 return 0; 687 } 688 689 static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp) 690 { 691 ulong kernel_addr_c; 692 ulong load_addr = 0; 693 694 kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0); 695 696 #ifdef CONFIG_ARM64 697 /* 698 * On 64-bit kernel, assuming use IMAGE by default. 699 * 700 * kernel_addr_c is for LZ4-IMAGE but maybe not defined. 701 * kernel_addr_r is for IMAGE. 702 */ 703 if (comp != IH_COMP_NONE) { 704 ulong comp_addr; 705 706 if (kernel_addr_c) { 707 comp_addr = kernel_addr_c; 708 } else { 709 printf("Warn: No \"kernel_addr_c\"\n"); 710 comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */ 711 env_set_hex("kernel_addr_c", comp_addr); 712 } 713 714 load_addr = comp_addr - hdr->page_size; 715 } 716 #else 717 /* 718 * On 32-bit kernel: 719 * 720 * The input load_addr is from env value: "kernel_addr_r", it has 721 * different role depends on whether kernel_addr_c is defined: 722 * 723 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined. 724 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined. 725 */ 726 if (comp == IH_COMP_NONE) { 727 if (kernel_addr_c) { 728 /* input load_addr is for Image, nothing to do */ 729 } else { 730 /* input load_addr is for lz4/zImage, set default addr for Image */ 731 load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000; 732 env_set_hex("kernel_addr_r", load_addr); 733 734 load_addr -= hdr->page_size; 735 } 736 } else { 737 if (kernel_addr_c) { 738 /* input load_addr is for Image, so use another for lz4/zImage */ 739 load_addr = kernel_addr_c - hdr->page_size; 740 } else { 741 /* input load_addr is for lz4/zImage, nothing to do */ 742 } 743 } 744 #endif 745 746 return load_addr; 747 } 748 749 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp) 750 { 751 ulong kernel_addr_r; 752 753 env_set_ulong("os_comp", comp); 754 755 /* zImage handles decompress itself */ 756 if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) { 757 kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000); 758 android_image_set_kload(hdr, kernel_addr_r); 759 android_image_set_comp(hdr, comp); 760 } else { 761 android_image_set_comp(hdr, IH_COMP_NONE); 762 } 763 } 764 765 static int android_image_load_separate(struct andr_img_hdr *hdr, 766 const disk_partition_t *part, 767 void *load_addr) 768 { 769 if (hdr->header_version < 3) 770 return android_image_separate(hdr, part, load_addr, NULL); 771 else 772 return android_image_separate_v34(hdr, part, load_addr, NULL); 773 } 774 775 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr) 776 { 777 ulong comp_addr; 778 int comp; 779 780 comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size); 781 comp_addr = android_image_get_comp_addr(hdr, comp); 782 783 /* non-compressed image: already in-place */ 784 if ((ulong)hdr == *load_addr) 785 return 0; 786 787 /* compressed image */ 788 if (comp_addr) { 789 *load_addr = comp_addr; 790 if ((ulong)hdr == comp_addr) /* already in-place */ 791 return 0; 792 } 793 794 /* 795 * The most possible reason to arrive here is: 796 * 797 * VBoot=1 and AVB load full partition to a temp memory buffer, now we 798 * separate(memcpy) subimages from boot.img to where they should be. 799 */ 800 if (hdr->header_version < 3) { 801 if (android_image_separate(hdr, NULL, (void *)(*load_addr), hdr)) 802 return -1; 803 } else { 804 if (android_image_separate_v34(hdr, NULL, (void *)(*load_addr), hdr)) 805 return -1; 806 } 807 808 android_image_set_decomp((void *)(*load_addr), comp); 809 810 return 0; 811 } 812 813 long android_image_load(struct blk_desc *dev_desc, 814 const disk_partition_t *part_info, 815 unsigned long load_address, 816 unsigned long max_size) { 817 struct andr_img_hdr *hdr; 818 ulong comp_addr; 819 int comp, ret; 820 int blk_off; 821 822 if (max_size < part_info->blksz) 823 return -1; 824 825 hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info); 826 if (!hdr) { 827 printf("No valid android hdr\n"); 828 return -1; 829 } 830 831 /* 832 * create the layout: 833 * 834 * |<- page_size ->|1-blk | 835 * |-----|---------|------|-----| 836 * | hdr | ... | kernel | 837 * |-----|----- ---|------------| 838 * 839 * Alloc page_size and 1 more blk for reading kernel image to 840 * get it's compression type, then fill the android hdr what 841 * we have populated before. 842 * 843 * Why? see: android_image_get_kernel_addr(). 844 */ 845 blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz); 846 hdr = (struct andr_img_hdr *) 847 realloc(hdr, (blk_off + 1) * dev_desc->blksz); 848 if (!hdr) 849 return -1; 850 851 if (blk_dread(dev_desc, part_info->start + blk_off, 1, 852 (char *)hdr + hdr->page_size) != 1) { 853 free(hdr); 854 return -1; 855 } 856 857 /* Changed to compressed address ? */ 858 comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size); 859 comp_addr = android_image_get_comp_addr(hdr, comp); 860 if (comp_addr) 861 load_address = comp_addr; 862 else 863 load_address -= hdr->page_size; 864 865 ret = android_image_load_separate(hdr, part_info, (void *)load_address); 866 if (ret) { 867 printf("Failed to load android image\n"); 868 goto fail; 869 } 870 android_image_set_decomp((void *)load_address, comp); 871 872 debug("Loading Android Image to 0x%08lx\n", load_address); 873 874 free(hdr); 875 return load_address; 876 877 fail: 878 free(hdr); 879 return -1; 880 } 881 882 static struct andr_img_hdr * 883 extract_boot_image_v012_header(struct blk_desc *dev_desc, 884 const disk_partition_t *boot_img) 885 { 886 struct andr_img_hdr *hdr; 887 long blk_cnt, blks_read; 888 889 blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz); 890 hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz); 891 892 if (!blk_cnt || !hdr) 893 return NULL; 894 895 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr); 896 if (blks_read != blk_cnt) { 897 debug("boot img header blk cnt is %ld and blks read is %ld\n", 898 blk_cnt, blks_read); 899 return NULL; 900 } 901 902 if (android_image_check_header((void *)hdr)) { 903 printf("boot header magic is invalid.\n"); 904 return NULL; 905 } 906 907 if (hdr->page_size < sizeof(*hdr)) { 908 printf("android hdr is over size\n"); 909 return NULL; 910 } 911 912 return hdr; 913 } 914 915 static struct boot_img_hdr_v34 * 916 extract_boot_image_v34_header(struct blk_desc *dev_desc, 917 const disk_partition_t *boot_img) 918 { 919 struct boot_img_hdr_v34 *boot_hdr; 920 long blk_cnt, blks_read; 921 922 blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v34), dev_desc->blksz); 923 boot_hdr = (struct boot_img_hdr_v34 *)malloc(blk_cnt * dev_desc->blksz); 924 925 if (!blk_cnt || !boot_hdr) 926 return NULL; 927 928 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_hdr); 929 if (blks_read != blk_cnt) { 930 debug("boot img header blk cnt is %ld and blks read is %ld\n", 931 blk_cnt, blks_read); 932 return NULL; 933 } 934 935 if (android_image_check_header((void *)boot_hdr)) { 936 printf("boot header magic is invalid.\n"); 937 return NULL; 938 } 939 940 if (boot_hdr->header_version < 3) { 941 printf("boot header %d, is not >= v3.\n", 942 boot_hdr->header_version); 943 return NULL; 944 } 945 946 return boot_hdr; 947 } 948 949 static struct vendor_boot_img_hdr_v34 * 950 extract_vendor_boot_image_v34_header(struct blk_desc *dev_desc, 951 const disk_partition_t *part_vendor_boot) 952 { 953 struct vendor_boot_img_hdr_v34 *vboot_hdr; 954 long blk_cnt, blks_read; 955 956 blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v34), 957 part_vendor_boot->blksz); 958 vboot_hdr = (struct vendor_boot_img_hdr_v34 *) 959 malloc(blk_cnt * part_vendor_boot->blksz); 960 961 if (!blk_cnt || !vboot_hdr) 962 return NULL; 963 964 blks_read = blk_dread(dev_desc, part_vendor_boot->start, 965 blk_cnt, vboot_hdr); 966 if (blks_read != blk_cnt) { 967 debug("vboot img header blk cnt is %ld and blks read is %ld\n", 968 blk_cnt, blks_read); 969 return NULL; 970 } 971 972 if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic, 973 VENDOR_BOOT_MAGIC_SIZE)) { 974 printf("vendor boot header is invalid.\n"); 975 return NULL; 976 } 977 978 if (vboot_hdr->header_version < 3) { 979 printf("vendor boot header %d, is not >= v3.\n", 980 vboot_hdr->header_version); 981 return NULL; 982 } 983 984 return vboot_hdr; 985 } 986 987 static int populate_boot_info(const struct boot_img_hdr_v34 *boot_hdr, 988 const struct vendor_boot_img_hdr_v34 *vendor_hdr, 989 struct andr_img_hdr *hdr) 990 { 991 memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE); 992 memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE); 993 994 hdr->kernel_size = boot_hdr->kernel_size; 995 /* don't use vendor_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */ 996 hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR; 997 /* generic ramdisk: immediately following the vendor ramdisk */ 998 hdr->boot_ramdisk_size = boot_hdr->ramdisk_size; 999 hdr->ramdisk_size = boot_hdr->ramdisk_size; 1000 1001 /* actually, useless */ 1002 hdr->ramdisk_addr = env_get_ulong("ramdisk_addr_r", 16, 0); 1003 1004 /* removed in v3 */ 1005 hdr->second_size = 0; 1006 hdr->second_addr = 0; 1007 1008 hdr->tags_addr = vendor_hdr->tags_addr; 1009 1010 /* fixed in v3 */ 1011 hdr->page_size = 4096; 1012 hdr->header_version = boot_hdr->header_version; 1013 hdr->os_version = boot_hdr->os_version; 1014 1015 memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE); 1016 strncpy(hdr->name, (const char *)vendor_hdr->name, ANDR_BOOT_NAME_SIZE); 1017 1018 /* removed in v3 */ 1019 memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE); 1020 memset(hdr->id, 0, 32); 1021 memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE); 1022 hdr->recovery_dtbo_size = 0; 1023 hdr->recovery_dtbo_offset = 0; 1024 1025 hdr->header_size = boot_hdr->header_size; 1026 hdr->dtb_size = vendor_hdr->dtb_size; 1027 hdr->dtb_addr = vendor_hdr->dtb_addr; 1028 1029 /* boot_img_hdr_v34 fields */ 1030 hdr->vendor_ramdisk_size = vendor_hdr->vendor_ramdisk_size; 1031 hdr->vendor_page_size = vendor_hdr->page_size; 1032 hdr->vendor_header_version = vendor_hdr->header_version; 1033 hdr->vendor_header_size = vendor_hdr->header_size; 1034 1035 hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE); 1036 if (!hdr->total_cmdline) 1037 return -ENOMEM; 1038 strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline, 1039 sizeof(boot_hdr->cmdline)); 1040 strncat(hdr->total_cmdline, " ", 1); 1041 strncat(hdr->total_cmdline, (const char *)vendor_hdr->cmdline, 1042 sizeof(vendor_hdr->cmdline)); 1043 1044 /* new for header v4 */ 1045 if (vendor_hdr->header_version > 3) { 1046 hdr->vendor_ramdisk_table_size = 1047 vendor_hdr->vendor_ramdisk_table_size; 1048 hdr->vendor_ramdisk_table_entry_num = 1049 vendor_hdr->vendor_ramdisk_table_entry_num; 1050 hdr->vendor_ramdisk_table_entry_size = 1051 vendor_hdr->vendor_ramdisk_table_entry_size; 1052 /* 1053 * If we place additional "androidboot.xxx" parameters after 1054 * bootconfig, this field value should be increased, 1055 * but not over than ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE. 1056 */ 1057 hdr->vendor_bootconfig_size = 1058 vendor_hdr->vendor_bootconfig_size; 1059 } else { 1060 hdr->vendor_ramdisk_table_size = 0; 1061 hdr->vendor_ramdisk_table_entry_num = 0; 1062 hdr->vendor_ramdisk_table_entry_size = 0; 1063 hdr->vendor_bootconfig_size = 0; 1064 } 1065 1066 if (hdr->page_size < sizeof(*hdr)) { 1067 printf("android hdr is over size\n"); 1068 return -EINVAL; 1069 } 1070 1071 return 0; 1072 } 1073 1074 /* 1075 * The possible cases of boot.img + recovery.img: 1076 * 1077 * [N]: 0, 1, 2 1078 * [M]: 0, 1, 2, 3, 4 1079 * 1080 * |--------------------|---------------------| 1081 * | boot.img | recovery.img | 1082 * |--------------------|---------------------| 1083 * | boot_img_hdr_v[N] | boot_img_hdr_v[N] | <= if A/B is not required 1084 * |--------------------|---------------------| 1085 * | boot_img_hdr_v34 | boot_img_hdr_v2 | <= if A/B is not required 1086 * |------------------------------------------| 1087 * | boot_img_hdr_v[M], no recovery.img | <= if A/B is required 1088 * |------------------------------------------| 1089 */ 1090 struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc, 1091 disk_partition_t *part_boot) 1092 { 1093 disk_partition_t part_vendor_boot; 1094 struct vendor_boot_img_hdr_v34 *vboot_hdr; 1095 struct boot_img_hdr_v34 *boot_hdr; 1096 struct andr_img_hdr *andr_hdr; 1097 int header_version; 1098 1099 if (!dev_desc || !part_boot) 1100 return NULL; 1101 1102 andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz); 1103 if (!andr_hdr) 1104 return NULL; 1105 1106 if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) { 1107 free(andr_hdr); 1108 return NULL; 1109 } 1110 1111 if (android_image_check_header(andr_hdr)) { 1112 free(andr_hdr); 1113 return NULL; 1114 } 1115 1116 header_version = andr_hdr->header_version; 1117 free(andr_hdr); 1118 1119 if (header_version < 3) { 1120 return extract_boot_image_v012_header(dev_desc, part_boot); 1121 } else { 1122 if (part_get_info_by_name(dev_desc, 1123 ANDROID_PARTITION_VENDOR_BOOT, 1124 &part_vendor_boot) < 0) { 1125 printf("No vendor boot partition\n"); 1126 return NULL; 1127 } 1128 boot_hdr = extract_boot_image_v34_header(dev_desc, part_boot); 1129 vboot_hdr = extract_vendor_boot_image_v34_header(dev_desc, 1130 &part_vendor_boot); 1131 if (!boot_hdr || !vboot_hdr) 1132 goto image_load_exit; 1133 1134 andr_hdr = (struct andr_img_hdr *) 1135 malloc(sizeof(struct andr_img_hdr)); 1136 if (!andr_hdr) { 1137 printf("No memory for andr hdr\n"); 1138 goto image_load_exit; 1139 } 1140 1141 if (populate_boot_info(boot_hdr, vboot_hdr, andr_hdr)) { 1142 printf("populate boot info failed\n"); 1143 goto image_load_exit; 1144 } 1145 1146 free(boot_hdr); 1147 free(vboot_hdr); 1148 1149 return andr_hdr; 1150 1151 image_load_exit: 1152 free(boot_hdr); 1153 free(vboot_hdr); 1154 1155 return NULL; 1156 } 1157 1158 return NULL; 1159 } 1160 1161 #if !defined(CONFIG_SPL_BUILD) 1162 /** 1163 * android_print_contents - prints out the contents of the Android format image 1164 * @hdr: pointer to the Android format image header 1165 * 1166 * android_print_contents() formats a multi line Android image contents 1167 * description. 1168 * The routine prints out Android image properties 1169 * 1170 * returns: 1171 * no returned results 1172 */ 1173 void android_print_contents(const struct andr_img_hdr *hdr) 1174 { 1175 const char * const p = IMAGE_INDENT_STRING; 1176 /* os_version = ver << 11 | lvl */ 1177 u32 os_ver = hdr->os_version >> 11; 1178 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 1179 u32 header_version = hdr->header_version; 1180 1181 printf("%skernel size: %x\n", p, hdr->kernel_size); 1182 printf("%skernel address: %x\n", p, hdr->kernel_addr); 1183 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); 1184 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr); 1185 printf("%ssecond size: %x\n", p, hdr->second_size); 1186 printf("%ssecond address: %x\n", p, hdr->second_addr); 1187 printf("%stags address: %x\n", p, hdr->tags_addr); 1188 printf("%spage size: %x\n", p, hdr->page_size); 1189 printf("%sheader_version: %x\n", p, header_version); 1190 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) 1191 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ 1192 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", 1193 p, hdr->os_version, 1194 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, 1195 (os_lvl >> 4) + 2000, os_lvl & 0x0F); 1196 printf("%sname: %s\n", p, hdr->name); 1197 printf("%scmdline: %s\n", p, hdr->cmdline); 1198 1199 if (header_version == 1 || header_version == 2) { 1200 printf("%srecovery dtbo size: %x\n", p, hdr->recovery_dtbo_size); 1201 printf("%srecovery dtbo offset: %llx\n", p, hdr->recovery_dtbo_offset); 1202 printf("%sheader size: %x\n", p, hdr->header_size); 1203 } 1204 1205 if (header_version == 2 || header_version == 3) { 1206 printf("%sdtb size: %x\n", p, hdr->dtb_size); 1207 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr); 1208 } 1209 1210 if (header_version >= 3) { 1211 printf("%scmdline: %s\n", p, hdr->total_cmdline); 1212 printf("%svendor ramdisk size: %x\n", p, hdr->vendor_ramdisk_size); 1213 printf("%svendor page size: %x\n", p, hdr->vendor_page_size); 1214 printf("%svendor header version: %d\n", p, hdr->vendor_header_version); 1215 printf("%svendor header size: %x\n", p, hdr->vendor_header_size); 1216 } 1217 1218 if (header_version >= 4) { 1219 printf("%svendor ramdisk table size: %x\n", 1220 p, hdr->vendor_ramdisk_table_size); 1221 printf("%svendor ramdisk table entry num: %x\n", 1222 p, hdr->vendor_ramdisk_table_entry_num); 1223 printf("%svendor ramdisk table entry size: %x\n", 1224 p, hdr->vendor_ramdisk_table_entry_size); 1225 printf("%svendor bootconfig size: %d\n", 1226 p, hdr->vendor_bootconfig_size); 1227 } 1228 } 1229 #endif 1230