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 /* 481 * @ram_base: !NULL means require memcpy for an exist full android image. 482 */ 483 static int android_image_separate(struct andr_img_hdr *hdr, 484 const disk_partition_t *part, 485 void *load_address, 486 void *ram_base) 487 { 488 ulong bstart; 489 490 if (android_image_check_header(hdr)) { 491 printf("Bad android image header\n"); 492 return -EINVAL; 493 } 494 495 /* set for image_load(IMG_KERNEL, ...) */ 496 env_set_hex("android_addr_r", (ulong)load_address); 497 bstart = part ? part->start : 0; 498 499 /* 500 * 1. Load images to their individual target ram position 501 * in order to disable fdt/ramdisk relocation. 502 */ 503 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) 504 struct udevice *dev; 505 sha_context ctx; 506 uchar hash[20]; 507 508 ctx.length = 0; 509 ctx.algo = CRYPTO_SHA1; 510 dev = crypto_get_device(ctx.algo); 511 if (!dev) { 512 printf("Can't find crypto device for SHA1 capability\n"); 513 return -ENODEV; 514 } 515 516 /* v1 & v2: requires total length before sha init */ 517 ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) + 518 hdr->ramdisk_size + sizeof(hdr->ramdisk_size) + 519 hdr->second_size + sizeof(hdr->second_size); 520 if (hdr->header_version > 0) 521 ctx.length += hdr->recovery_dtbo_size + 522 sizeof(hdr->recovery_dtbo_size); 523 if (hdr->header_version > 1) 524 ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size); 525 526 /* load, never change order ! */ 527 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 528 return -1; 529 530 crypto_sha_init(dev, &ctx); 531 if (image_load(IMG_KERNEL, hdr, bstart, ram_base, dev)) 532 return -1; 533 if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, dev)) 534 return -1; 535 if (image_load(IMG_SECOND, hdr, bstart, ram_base, dev)) 536 return -1; 537 if (hdr->header_version > 0) { 538 if (image_load(IMG_RECOVERY_DTBO, hdr, bstart, ram_base, dev)) 539 return -1; 540 } 541 if (hdr->header_version > 1) { 542 if (image_load(IMG_DTB, hdr, bstart, ram_base, dev)) 543 return -1; 544 } 545 546 crypto_sha_final(dev, &ctx, hash); 547 if (memcmp(hash, hdr->id, 20)) { 548 print_hash("Hash from header", (u8 *)hdr->id, 20); 549 print_hash("Hash real", (u8 *)hash, 20); 550 return -EBADFD; 551 } else { 552 printf("Image hash OK\n"); 553 } 554 555 #else /* !(CONFIG_DM_CRYPTO && CONFIG_ANDROID_BOOT_IMAGE_HASH) */ 556 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 557 return -1; 558 if (image_load(IMG_KERNEL, hdr, bstart, ram_base, NULL)) 559 return -1; 560 if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL)) 561 return -1; 562 if (image_load(IMG_SECOND, hdr, bstart, ram_base, NULL)) 563 return -1; 564 if (hdr->header_version > 0) { 565 if (image_load(IMG_RECOVERY_DTBO, hdr, bstart, ram_base, NULL)) 566 return -1; 567 } 568 if (hdr->header_version > 1) { 569 if (image_load(IMG_DTB, hdr, bstart, ram_base, NULL)) 570 return -1; 571 } 572 #endif 573 574 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 575 env_set("bootm-no-reloc", "y"); 576 577 return 0; 578 } 579 580 static int android_image_separate_v3(struct andr_img_hdr *hdr, 581 const disk_partition_t *part, 582 void *load_address, void *ram_base) 583 { 584 ulong bstart; 585 586 if (android_image_check_header(hdr)) { 587 printf("Bad android image header\n"); 588 return -EINVAL; 589 } 590 591 /* set for image_load(IMG_KERNEL, ...) */ 592 env_set_hex("android_addr_r", (ulong)load_address); 593 bstart = part ? part->start : 0; 594 595 /* 596 * 1. Load images to their individual target ram position 597 * in order to disable fdt/ramdisk relocation. 598 */ 599 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 600 return -1; 601 if (image_load(IMG_KERNEL, hdr, bstart, ram_base, NULL)) 602 return -1; 603 if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base, NULL)) 604 return -1; 605 if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL)) 606 return -1; 607 608 /* 609 * Copy the populated hdr to load address after image_load(IMG_KERNEL) 610 * 611 * The image_load(IMG_KERNEL) only reads boot_img_hdr_v3 while 612 * vendor_boot_img_hdr_v3 is not included, so fix it here. 613 */ 614 memcpy((char *)load_address, hdr, hdr->page_size); 615 616 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 617 env_set("bootm-no-reloc", "y"); 618 619 return 0; 620 } 621 622 static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp) 623 { 624 ulong kernel_addr_c; 625 ulong load_addr = 0; 626 627 kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0); 628 629 #ifdef CONFIG_ARM64 630 /* 631 * On 64-bit kernel, assuming use IMAGE by default. 632 * 633 * kernel_addr_c is for LZ4-IMAGE but maybe not defined. 634 * kernel_addr_r is for IMAGE. 635 */ 636 if (comp != IH_COMP_NONE) { 637 ulong comp_addr; 638 639 if (kernel_addr_c) { 640 comp_addr = kernel_addr_c; 641 } else { 642 printf("Warn: No \"kernel_addr_c\"\n"); 643 comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */ 644 env_set_hex("kernel_addr_c", comp_addr); 645 } 646 647 load_addr = comp_addr - hdr->page_size; 648 } 649 #else 650 /* 651 * On 32-bit kernel: 652 * 653 * The input load_addr is from env value: "kernel_addr_r", it has 654 * different role depends on whether kernel_addr_c is defined: 655 * 656 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined. 657 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined. 658 */ 659 if (comp == IH_COMP_NONE) { 660 if (kernel_addr_c) { 661 /* input load_addr is for Image, nothing to do */ 662 } else { 663 /* input load_addr is for lz4/zImage, set default addr for Image */ 664 load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000; 665 env_set_hex("kernel_addr_r", load_addr); 666 667 load_addr -= hdr->page_size; 668 } 669 } else { 670 if (kernel_addr_c) { 671 /* input load_addr is for Image, so use another for lz4/zImage */ 672 load_addr = kernel_addr_c - hdr->page_size; 673 } else { 674 /* input load_addr is for lz4/zImage, nothing to do */ 675 } 676 } 677 #endif 678 679 return load_addr; 680 } 681 682 /* 683 * 'boot_android' cmd use "kernel_addr_r" as default load address ! 684 * We update it according to compress type and "kernel_addr_c/r". 685 */ 686 int android_image_parse_comp(struct andr_img_hdr *hdr, ulong *load_addr) 687 { 688 ulong new_load_addr; 689 int comp; 690 691 comp = android_image_parse_kernel_comp(hdr); 692 env_set_ulong("os_comp", comp); 693 694 new_load_addr = android_image_get_comp_addr(hdr, comp); 695 if (new_load_addr != 0) 696 *load_addr = new_load_addr; 697 698 return comp; 699 } 700 701 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp) 702 { 703 ulong kernel_addr_r; 704 705 /* zImage handles decompress itself */ 706 if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) { 707 kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000); 708 android_image_set_kload(hdr, kernel_addr_r); 709 android_image_set_comp(hdr, comp); 710 } else { 711 android_image_set_comp(hdr, IH_COMP_NONE); 712 } 713 } 714 715 static int android_image_load_separate(struct andr_img_hdr *hdr, 716 const disk_partition_t *part, 717 void *load_addr) 718 { 719 if (hdr->header_version < 3) 720 return android_image_separate(hdr, part, load_addr, NULL); 721 else 722 return android_image_separate_v3(hdr, part, load_addr, NULL); 723 } 724 725 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr) 726 { 727 ulong comp_addr = *load_addr; 728 int comp; 729 730 comp = android_image_parse_comp(hdr, &comp_addr); 731 if (comp_addr == (ulong)hdr) 732 return 0; 733 734 if (hdr->header_version < 3) { 735 if (android_image_separate(hdr, NULL, (void *)comp_addr, hdr)) 736 return -1; 737 } else { 738 if (android_image_separate_v3(hdr, NULL, (void *)comp_addr, hdr)) 739 return -1; 740 } 741 742 *load_addr = comp_addr; 743 android_image_set_decomp((void *)comp_addr, comp); 744 745 return 0; 746 } 747 748 long android_image_load(struct blk_desc *dev_desc, 749 const disk_partition_t *part_info, 750 unsigned long load_address, 751 unsigned long max_size) { 752 struct andr_img_hdr *hdr; 753 int comp, ret; 754 int blk_off; 755 756 if (max_size < part_info->blksz) 757 return -1; 758 759 hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info); 760 if (!hdr) { 761 printf("No valid android hdr\n"); 762 return -1; 763 } 764 765 /* 766 * create the layout: 767 * 768 * |<- page_size ->|1-blk | 769 * |-----|---------|------|-----| 770 * | hdr | ... | kernel | 771 * |-----|----- ---|------------| 772 * 773 * Alloc page_size and 1 more blk for reading kernel image to 774 * get it's compression type, then fill the android hdr what 775 * we have populated before. 776 * 777 * Why? see: android_image_get_kernel_addr(). 778 */ 779 blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz); 780 hdr = (struct andr_img_hdr *) 781 realloc(hdr, (blk_off + 1) * dev_desc->blksz); 782 if (!hdr) 783 return -1; 784 785 if (blk_dread(dev_desc, part_info->start + blk_off, 1, 786 (char *)hdr + hdr->page_size) != 1) { 787 free(hdr); 788 return -1; 789 } 790 791 /* Make kernel start address at load_address */ 792 load_address -= hdr->page_size; 793 794 /* Let's load kernel now ! */ 795 comp = android_image_parse_comp(hdr, &load_address); 796 ret = android_image_load_separate(hdr, part_info, (void *)load_address); 797 if (ret) { 798 printf("Failed to load android image\n"); 799 goto fail; 800 } 801 android_image_set_decomp((void *)load_address, comp); 802 803 debug("Loading Android Image to 0x%08lx\n", load_address); 804 805 free(hdr); 806 return load_address; 807 808 fail: 809 free(hdr); 810 return -1; 811 } 812 813 static struct andr_img_hdr * 814 extract_boot_image_v012_header(struct blk_desc *dev_desc, 815 const disk_partition_t *boot_img) 816 { 817 struct andr_img_hdr *hdr; 818 long blk_cnt, blks_read; 819 820 blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz); 821 hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz); 822 823 if (!blk_cnt || !hdr) 824 return NULL; 825 826 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr); 827 if (blks_read != blk_cnt) { 828 debug("boot img header blk cnt is %ld and blks read is %ld\n", 829 blk_cnt, blks_read); 830 return NULL; 831 } 832 833 if (android_image_check_header((void *)hdr)) { 834 printf("boot header magic is invalid.\n"); 835 return NULL; 836 } 837 838 if (hdr->page_size < sizeof(*hdr)) { 839 printf("android hdr is over size\n"); 840 return NULL; 841 } 842 843 return hdr; 844 } 845 846 static struct boot_img_hdr_v3 * 847 extract_boot_image_v3_header(struct blk_desc *dev_desc, 848 const disk_partition_t *boot_img) 849 { 850 struct boot_img_hdr_v3 *boot_hdr; 851 long blk_cnt, blks_read; 852 853 blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v3), dev_desc->blksz); 854 boot_hdr = (struct boot_img_hdr_v3 *)malloc(blk_cnt * dev_desc->blksz); 855 856 if (!blk_cnt || !boot_hdr) 857 return NULL; 858 859 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_hdr); 860 if (blks_read != blk_cnt) { 861 debug("boot img header blk cnt is %ld and blks read is %ld\n", 862 blk_cnt, blks_read); 863 return NULL; 864 } 865 866 if (android_image_check_header((void *)boot_hdr)) { 867 printf("boot header magic is invalid.\n"); 868 return NULL; 869 } 870 871 if (boot_hdr->header_version != 3) { 872 printf("boot header is not v3.\n"); 873 return NULL; 874 } 875 876 return boot_hdr; 877 } 878 879 static struct vendor_boot_img_hdr_v3 * 880 extract_vendor_boot_image_v3_header(struct blk_desc *dev_desc, 881 const disk_partition_t *part_vendor_boot) 882 { 883 struct vendor_boot_img_hdr_v3 *vboot_hdr; 884 long blk_cnt, blks_read; 885 886 blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v3), 887 part_vendor_boot->blksz); 888 vboot_hdr = (struct vendor_boot_img_hdr_v3 *) 889 malloc(blk_cnt * part_vendor_boot->blksz); 890 891 if (!blk_cnt || !vboot_hdr) 892 return NULL; 893 894 blks_read = blk_dread(dev_desc, part_vendor_boot->start, 895 blk_cnt, vboot_hdr); 896 if (blks_read != blk_cnt) { 897 debug("vboot img header blk cnt is %ld and blks read is %ld\n", 898 blk_cnt, blks_read); 899 return NULL; 900 } 901 902 if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic, 903 VENDOR_BOOT_MAGIC_SIZE)) { 904 printf("vendor boot header is invalid.\n"); 905 return NULL; 906 } 907 908 if (vboot_hdr->header_version != 3) { 909 printf("vendor boot header is not v3.\n"); 910 return NULL; 911 } 912 913 return vboot_hdr; 914 } 915 916 static int populate_boot_info(const struct boot_img_hdr_v3 *boot_hdr, 917 const struct vendor_boot_img_hdr_v3 *vendor_hdr, 918 struct andr_img_hdr *hdr) 919 { 920 memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE); 921 memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE); 922 923 hdr->kernel_size = boot_hdr->kernel_size; 924 /* don't use vendor_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */ 925 hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR; 926 /* generic ramdisk: immediately following the vendor ramdisk */ 927 hdr->boot_ramdisk_size = boot_hdr->ramdisk_size; 928 hdr->ramdisk_size = boot_hdr->ramdisk_size + 929 vendor_hdr->vendor_ramdisk_size; 930 /* actually, useless */ 931 hdr->ramdisk_addr = vendor_hdr->ramdisk_addr + 932 vendor_hdr->vendor_ramdisk_size; 933 /* removed in v3 */ 934 hdr->second_size = 0; 935 hdr->second_addr = 0; 936 937 hdr->tags_addr = vendor_hdr->tags_addr; 938 939 /* fixed in v3 */ 940 hdr->page_size = 4096; 941 hdr->header_version = boot_hdr->header_version; 942 hdr->os_version = boot_hdr->os_version; 943 944 memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE); 945 strncpy(hdr->name, (const char *)vendor_hdr->name, ANDR_BOOT_NAME_SIZE); 946 947 /* removed in v3 */ 948 memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE); 949 memset(hdr->id, 0, 32); 950 memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE); 951 hdr->recovery_dtbo_size = 0; 952 hdr->recovery_dtbo_offset = 0; 953 954 hdr->header_size = boot_hdr->header_size; 955 hdr->dtb_size = vendor_hdr->dtb_size; 956 hdr->dtb_addr = vendor_hdr->dtb_addr; 957 958 /* boot_img_hdr_v3 fields */ 959 hdr->vendor_ramdisk_size = vendor_hdr->vendor_ramdisk_size; 960 hdr->vendor_page_size = vendor_hdr->page_size; 961 hdr->vendor_header_version = vendor_hdr->header_version; 962 hdr->vendor_header_size = vendor_hdr->header_size; 963 964 hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE); 965 if (!hdr->total_cmdline) 966 return -ENOMEM; 967 strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline, 968 sizeof(boot_hdr->cmdline)); 969 strncat(hdr->total_cmdline, " ", 1); 970 strncat(hdr->total_cmdline, (const char *)vendor_hdr->cmdline, 971 sizeof(vendor_hdr->cmdline)); 972 973 if (hdr->page_size < sizeof(*hdr)) { 974 printf("android hdr is over size\n"); 975 return -EINVAL; 976 } 977 978 return 0; 979 } 980 981 /* 982 * The possible cases of boot.img + recovery.img: 983 * 984 * [N]: 0, 1, 2 985 * [M]: 0, 1, 2, 3 986 * 987 * |--------------------|---------------------| 988 * | boot.img | recovery.img | 989 * |--------------------|---------------------| 990 * | boot_img_hdr_v[N] | boot_img_hdr_v[N] | <= if A/B is not required 991 * |--------------------|---------------------| 992 * | boot_img_hdr_v3 | boot_img_hdr_v2 | <= if A/B is not required 993 * |------------------------------------------| 994 * | boot_img_hdr_v[M], no recovery.img | <= if A/B is required 995 * |------------------------------------------| 996 */ 997 struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc, 998 disk_partition_t *part_boot) 999 { 1000 disk_partition_t part_vendor_boot; 1001 struct vendor_boot_img_hdr_v3 *vboot_hdr; 1002 struct boot_img_hdr_v3 *boot_hdr; 1003 struct andr_img_hdr *andr_hdr; 1004 int header_version; 1005 1006 if (!dev_desc || !part_boot) 1007 return NULL; 1008 1009 andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz); 1010 if (!andr_hdr) 1011 return NULL; 1012 1013 if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) { 1014 free(andr_hdr); 1015 return NULL; 1016 } 1017 1018 if (android_image_check_header(andr_hdr)) { 1019 free(andr_hdr); 1020 return NULL; 1021 } 1022 1023 header_version = andr_hdr->header_version; 1024 free(andr_hdr); 1025 1026 if (header_version < 3) { 1027 return extract_boot_image_v012_header(dev_desc, part_boot); 1028 } else { 1029 if (part_get_info_by_name(dev_desc, 1030 ANDROID_PARTITION_VENDOR_BOOT, 1031 &part_vendor_boot) < 0) { 1032 printf("No vendor boot partition\n"); 1033 return NULL; 1034 } 1035 boot_hdr = extract_boot_image_v3_header(dev_desc, part_boot); 1036 vboot_hdr = extract_vendor_boot_image_v3_header(dev_desc, 1037 &part_vendor_boot); 1038 if (!boot_hdr || !vboot_hdr) 1039 goto image_load_exit; 1040 1041 andr_hdr = (struct andr_img_hdr *) 1042 malloc(sizeof(struct andr_img_hdr)); 1043 if (!andr_hdr) { 1044 printf("No memory for andr hdr\n"); 1045 goto image_load_exit; 1046 } 1047 1048 if (populate_boot_info(boot_hdr, vboot_hdr, andr_hdr)) { 1049 printf("populate boot info failed\n"); 1050 goto image_load_exit; 1051 } 1052 1053 free(boot_hdr); 1054 free(vboot_hdr); 1055 1056 return andr_hdr; 1057 1058 image_load_exit: 1059 free(boot_hdr); 1060 free(vboot_hdr); 1061 1062 return NULL; 1063 } 1064 1065 return NULL; 1066 } 1067 1068 #if !defined(CONFIG_SPL_BUILD) 1069 /** 1070 * android_print_contents - prints out the contents of the Android format image 1071 * @hdr: pointer to the Android format image header 1072 * 1073 * android_print_contents() formats a multi line Android image contents 1074 * description. 1075 * The routine prints out Android image properties 1076 * 1077 * returns: 1078 * no returned results 1079 */ 1080 void android_print_contents(const struct andr_img_hdr *hdr) 1081 { 1082 const char * const p = IMAGE_INDENT_STRING; 1083 /* os_version = ver << 11 | lvl */ 1084 u32 os_ver = hdr->os_version >> 11; 1085 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 1086 u32 header_version = hdr->header_version; 1087 1088 printf("%skernel size: %x\n", p, hdr->kernel_size); 1089 printf("%skernel address: %x\n", p, hdr->kernel_addr); 1090 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); 1091 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr); 1092 printf("%ssecond size: %x\n", p, hdr->second_size); 1093 printf("%ssecond address: %x\n", p, hdr->second_addr); 1094 printf("%stags address: %x\n", p, hdr->tags_addr); 1095 printf("%spage size: %x\n", p, hdr->page_size); 1096 printf("%sheader_version: %x\n", p, header_version); 1097 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) 1098 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ 1099 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", 1100 p, hdr->os_version, 1101 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, 1102 (os_lvl >> 4) + 2000, os_lvl & 0x0F); 1103 printf("%sname: %s\n", p, hdr->name); 1104 printf("%scmdline: %s\n", p, hdr->cmdline); 1105 1106 if (header_version == 1 || header_version == 2) { 1107 printf("%srecovery dtbo size: %x\n", p, hdr->recovery_dtbo_size); 1108 printf("%srecovery dtbo offset: %llx\n", p, hdr->recovery_dtbo_offset); 1109 printf("%sheader size: %x\n", p, hdr->header_size); 1110 } 1111 1112 if (header_version == 2 || header_version == 3) { 1113 printf("%sdtb size: %x\n", p, hdr->dtb_size); 1114 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr); 1115 } 1116 1117 if (header_version == 3) { 1118 printf("%scmdline: %s\n", p, hdr->total_cmdline); 1119 printf("%svendor ramdisk size: %x\n", p, hdr->vendor_ramdisk_size); 1120 printf("%svendor page size: %x\n", p, hdr->vendor_page_size); 1121 printf("%svendor header version: %d\n", p, hdr->vendor_header_version); 1122 printf("%svendor header size: %x\n", p, hdr->vendor_header_size); 1123 } 1124 } 1125 #endif 1126