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