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 34 /* Defined by rockchip legacy mkboot tool(SDK version < 8.1) */ 35 #define ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE 0x4000 36 37 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; 38 static u32 android_kernel_comp_type = IH_COMP_NONE; 39 40 u32 android_image_major_version(void) 41 { 42 /* MSB 7-bits */ 43 return gd->bd->bi_andr_version >> 25; 44 } 45 46 u32 android_bcb_msg_sector_offset(void) 47 { 48 /* 49 * Rockchip platforms defines BCB message at the 16KB offset of 50 * misc partition while the Google defines it at 0x0 offset. 51 * 52 * From Android-Q, the 0x0 offset is mandary on Google VTS, so that 53 * this is a compatibility according to android image 'os_version'. 54 */ 55 #ifdef CONFIG_RKIMG_BOOTLOADER 56 return (android_image_major_version() >= ANDROID_Q_VER) ? 0x0 : 0x20; 57 #else 58 return 0x0; 59 #endif 60 } 61 62 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr) 63 { 64 /* 65 * All the Android tools that generate a boot.img use this 66 * address as the default. 67 * 68 * Even though it doesn't really make a lot of sense, and it 69 * might be valid on some platforms, we treat that address as 70 * the default value for this field, and try to execute the 71 * kernel in place in such a case. 72 * 73 * Otherwise, we will return the actual value set by the user. 74 */ 75 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) 76 return (ulong)hdr + hdr->page_size; 77 78 #ifdef CONFIG_ARCH_ROCKCHIP 79 /* 80 * If kernel is compressed, kernel_addr is set as decompressed address 81 * after compressed being loaded to ram, so let's use it. 82 */ 83 if (android_kernel_comp_type != IH_COMP_NONE && 84 android_kernel_comp_type != IH_COMP_ZIMAGE) 85 return hdr->kernel_addr; 86 87 /* 88 * Compatble with rockchip legacy packing with kernel/ramdisk/second 89 * address base from 0x60000000(SDK versiont < 8.1), these are invalid 90 * address, so we calc it by real size. 91 */ 92 return (ulong)hdr + hdr->page_size; 93 #else 94 return hdr->kernel_addr; 95 #endif 96 97 } 98 99 void android_image_set_comp(struct andr_img_hdr *hdr, u32 comp) 100 { 101 android_kernel_comp_type = comp; 102 } 103 104 u32 android_image_get_comp(const struct andr_img_hdr *hdr) 105 { 106 return android_kernel_comp_type; 107 } 108 109 int android_image_parse_kernel_comp(const struct andr_img_hdr *hdr) 110 { 111 ulong kaddr = android_image_get_kernel_addr(hdr); 112 return bootm_parse_comp((const unsigned char *)kaddr); 113 } 114 115 /** 116 * android_image_get_kernel() - processes kernel part of Android boot images 117 * @hdr: Pointer to image header, which is at the start 118 * of the image. 119 * @verify: Checksum verification flag. Currently unimplemented. 120 * @os_data: Pointer to a ulong variable, will hold os data start 121 * address. 122 * @os_len: Pointer to a ulong variable, will hold os data length. 123 * 124 * This function returns the os image's start address and length. Also, 125 * it appends the kernel command line to the bootargs env variable. 126 * 127 * Return: Zero, os start address and length on success, 128 * otherwise on failure. 129 */ 130 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify, 131 ulong *os_data, ulong *os_len) 132 { 133 u32 kernel_addr = android_image_get_kernel_addr(hdr); 134 135 /* 136 * Not all Android tools use the id field for signing the image with 137 * sha1 (or anything) so we don't check it. It is not obvious that the 138 * string is null terminated so we take care of this. 139 */ 140 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE); 141 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0'; 142 if (strlen(andr_tmp_str)) 143 printf("Android's image name: %s\n", andr_tmp_str); 144 145 printf("Kernel load addr 0x%08x size %u KiB\n", 146 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024)); 147 148 int len = 0; 149 if (*hdr->cmdline) { 150 debug("Kernel command line: %s\n", hdr->cmdline); 151 len += strlen(hdr->cmdline); 152 } 153 154 char *bootargs = env_get("bootargs"); 155 if (bootargs) 156 len += strlen(bootargs); 157 158 char *newbootargs = malloc(len + 2); 159 if (!newbootargs) { 160 puts("Error: malloc in android_image_get_kernel failed!\n"); 161 return -ENOMEM; 162 } 163 *newbootargs = '\0'; 164 165 if (bootargs) { 166 strcpy(newbootargs, bootargs); 167 strcat(newbootargs, " "); 168 } 169 if (*hdr->cmdline) 170 strcat(newbootargs, hdr->cmdline); 171 172 env_set("bootargs", newbootargs); 173 174 if (os_data) { 175 *os_data = (ulong)hdr; 176 *os_data += hdr->page_size; 177 } 178 if (os_len) 179 *os_len = hdr->kernel_size; 180 return 0; 181 } 182 183 int android_image_check_header(const struct andr_img_hdr *hdr) 184 { 185 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE); 186 } 187 188 ulong android_image_get_end(const struct andr_img_hdr *hdr) 189 { 190 ulong end; 191 /* 192 * The header takes a full page, the remaining components are aligned 193 * on page boundary 194 */ 195 end = (ulong)hdr; 196 end += hdr->page_size; 197 end += ALIGN(hdr->kernel_size, hdr->page_size); 198 end += ALIGN(hdr->ramdisk_size, hdr->page_size); 199 end += ALIGN(hdr->second_size, hdr->page_size); 200 201 if (hdr->header_version >= 2) { 202 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); 203 end += ALIGN(hdr->dtb_size, hdr->page_size); 204 } else if (hdr->header_version >= 1) { 205 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); 206 } 207 208 return end; 209 } 210 211 u32 android_image_get_ksize(const struct andr_img_hdr *hdr) 212 { 213 return hdr->kernel_size; 214 } 215 216 void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address) 217 { 218 hdr->kernel_addr = load_address; 219 } 220 221 ulong android_image_get_kload(const struct andr_img_hdr *hdr) 222 { 223 return android_image_get_kernel_addr(hdr); 224 } 225 226 int android_image_get_ramdisk(const struct andr_img_hdr *hdr, 227 ulong *rd_data, ulong *rd_len) 228 { 229 ulong ramdisk_addr_r; 230 231 if (!hdr->ramdisk_size) { 232 *rd_data = *rd_len = 0; 233 return -1; 234 } 235 236 /* Have been loaded by android_image_load_separate() on ramdisk_addr_r */ 237 ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0); 238 if (!ramdisk_addr_r) { 239 printf("No Found Ramdisk Load Address.\n"); 240 return -1; 241 } 242 243 *rd_data = ramdisk_addr_r; 244 *rd_len = hdr->ramdisk_size; 245 246 printf("RAM disk load addr 0x%08lx size %u KiB\n", 247 *rd_data, DIV_ROUND_UP(hdr->ramdisk_size, 1024)); 248 249 return 0; 250 } 251 252 int android_image_get_fdt(const struct andr_img_hdr *hdr, 253 ulong *rd_data) 254 { 255 ulong fdt_addr_r; 256 257 if (!hdr->second_size) { 258 *rd_data = 0; 259 return -1; 260 } 261 262 /* Have been loaded by android_image_load_separate() on fdt_addr_r */ 263 fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0); 264 if (!fdt_addr_r) { 265 printf("No Found FDT Load Address.\n"); 266 return -1; 267 } 268 269 *rd_data = fdt_addr_r; 270 271 debug("FDT load addr 0x%08x size %u KiB\n", 272 hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024)); 273 274 return 0; 275 } 276 277 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) 278 static void print_hash(const char *label, u8 *hash, int len) 279 { 280 int i; 281 282 printf("%s:\n 0x", label ? : "Hash"); 283 for (i = 0; i < len; i++) 284 printf("%02x", hash[i]); 285 printf("\n"); 286 } 287 #endif 288 289 typedef enum { 290 IMG_KERNEL, 291 IMG_RAMDISK, 292 IMG_SECOND, 293 IMG_RECOVERY_DTBO, 294 IMG_RK_DTB, /* within resource.img in second position */ 295 IMG_DTB, 296 IMG_MAX, 297 } img_t; 298 299 static int image_read(img_t img, struct andr_img_hdr *hdr, 300 ulong blkstart, void *ram_base, 301 struct udevice *crypto) 302 { 303 struct blk_desc *desc = rockchip_get_bootdev(); 304 __maybe_unused u32 sizesz; 305 ulong pgsz = hdr->page_size; 306 ulong blksz = desc->blksz; 307 ulong blkcnt, blkoff; 308 ulong offset = 0; 309 ulong datasz; 310 void *ramdst; 311 int ret = 0; 312 313 switch (img) { 314 case IMG_KERNEL: 315 offset = 0; /* include a page_size(image header) */ 316 blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz); 317 ramdst = (void *)env_get_ulong("android_addr_r", 16, 0); 318 datasz = hdr->kernel_size + pgsz; 319 sizesz = sizeof(hdr->kernel_size); 320 if (!sysmem_alloc_base(MEM_KERNEL, 321 (phys_addr_t)ramdst, blkcnt * blksz)) 322 return -ENOMEM; 323 break; 324 case IMG_RAMDISK: 325 offset = pgsz + ALIGN(hdr->kernel_size, pgsz); 326 blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz); 327 ramdst = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 328 datasz = hdr->ramdisk_size; 329 sizesz = sizeof(hdr->ramdisk_size); 330 if (datasz && !sysmem_alloc_base(MEM_RAMDISK, 331 (phys_addr_t)ramdst, blkcnt * blksz)) 332 return -ENOMEM; 333 break; 334 case IMG_SECOND: 335 offset = pgsz + 336 ALIGN(hdr->kernel_size, pgsz) + 337 ALIGN(hdr->ramdisk_size, pgsz); 338 blkcnt = DIV_ROUND_UP(hdr->second_size, blksz); 339 datasz = hdr->second_size; 340 sizesz = sizeof(hdr->second_size); 341 ramdst = malloc(blkcnt * blksz); 342 break; 343 case IMG_RECOVERY_DTBO: 344 offset = pgsz + 345 ALIGN(hdr->kernel_size, pgsz) + 346 ALIGN(hdr->ramdisk_size, pgsz) + 347 ALIGN(hdr->second_size, pgsz); 348 blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz); 349 datasz = hdr->recovery_dtbo_size; 350 sizesz = sizeof(hdr->recovery_dtbo_size); 351 ramdst = malloc(blkcnt * blksz); 352 break; 353 case IMG_DTB: 354 offset = pgsz + 355 ALIGN(hdr->kernel_size, pgsz) + 356 ALIGN(hdr->ramdisk_size, pgsz) + 357 ALIGN(hdr->second_size, pgsz) + 358 ALIGN(hdr->recovery_dtbo_size, pgsz); 359 blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz); 360 datasz = hdr->dtb_size; 361 sizesz = sizeof(hdr->dtb_size); 362 ramdst = malloc(blkcnt * blksz); 363 break; 364 case IMG_RK_DTB: 365 #ifdef CONFIG_RKIMG_BOOTLOADER 366 /* No going further, it handles DTBO, HW-ID, etc */ 367 ramdst = (void *)env_get_ulong("fdt_addr_r", 16, 0); 368 if (gd->fdt_blob != (void *)ramdst) 369 ret = rockchip_read_dtb_file(ramdst); 370 #endif 371 return ret < 0 ? ret : 0; 372 default: 373 return -EINVAL; 374 } 375 376 if (!ramdst) { 377 printf("No memory for image(%d)\n", img); 378 return -ENOMEM; 379 } 380 381 if (!blksz || !datasz) 382 goto crypto_calc; 383 384 /* load */ 385 if (ram_base) { 386 memcpy(ramdst, (char *)((ulong)ram_base + offset), datasz); 387 } else { 388 blkoff = DIV_ROUND_UP(offset, blksz); 389 ret = blk_dread(desc, blkstart + blkoff, blkcnt, ramdst); 390 if (ret != blkcnt) { 391 printf("Failed to read img(%d), ret=%d\n", img, ret); 392 return -EIO; 393 } 394 } 395 396 crypto_calc: 397 /* sha1 */ 398 #ifdef CONFIG_DM_CRYPTO 399 if (crypto) { 400 if (img == IMG_KERNEL) { 401 ramdst += pgsz; 402 datasz -= pgsz; 403 } 404 405 crypto_sha_update(crypto, (u32 *)ramdst, datasz); 406 crypto_sha_update(crypto, (u32 *)&datasz, sizesz); 407 } 408 #endif 409 410 return 0; 411 } 412 413 static int android_image_separate(struct andr_img_hdr *hdr, 414 const disk_partition_t *part, 415 void *load_address, 416 void *ram_base) 417 { 418 char *initrd_high; 419 char *fdt_high; 420 ulong bstart; 421 422 if (android_image_check_header(hdr)) { 423 printf("Bad android image header\n"); 424 return -EINVAL; 425 } 426 427 /* set for image_read(IMG_KERNEL, ...) */ 428 env_set_hex("android_addr_r", (ulong)load_address); 429 bstart = part ? part->start : 0; 430 431 /* 432 * 1. Load images to their individual target ram position 433 * in order to disable fdt/ramdisk relocation. 434 */ 435 #if defined(CONFIG_DM_CRYPTO) && defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) 436 struct udevice *dev; 437 sha_context ctx; 438 uchar hash[20]; 439 440 ctx.length = 0; 441 ctx.algo = CRYPTO_SHA1; 442 dev = crypto_get_device(ctx.algo); 443 if (!dev) { 444 printf("Can't find crypto device for SHA1 capability\n"); 445 return -ENODEV; 446 } 447 448 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 449 /* v1: requires total length before sha init */ 450 ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) + 451 hdr->ramdisk_size + sizeof(hdr->ramdisk_size) + 452 hdr->second_size + sizeof(hdr->second_size); 453 if (hdr->header_version > 0) 454 ctx.length += hdr->recovery_dtbo_size + 455 sizeof(hdr->recovery_dtbo_size); 456 if (hdr->header_version > 1) 457 ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size); 458 #endif 459 crypto_sha_init(dev, &ctx); 460 461 /* load, never change order ! */ 462 if (image_read(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 463 return -1; 464 if (image_read(IMG_KERNEL, hdr, bstart, ram_base, dev)) 465 return -1; 466 if (image_read(IMG_RAMDISK, hdr, bstart, ram_base, dev)) 467 return -1; 468 if (image_read(IMG_SECOND, hdr, bstart, ram_base, dev)) 469 return -1; 470 if (hdr->header_version > 0) { 471 if (image_read(IMG_RECOVERY_DTBO, hdr, bstart, ram_base, dev)) 472 return -1; 473 } 474 if (hdr->header_version > 1) { 475 if (image_read(IMG_DTB, hdr, bstart, ram_base, dev)) 476 return -1; 477 } 478 479 crypto_sha_final(dev, &ctx, hash); 480 if (memcmp(hash, hdr->id, 20)) { 481 print_hash("Hash from header", (u8 *)hdr->id, 20); 482 print_hash("Hash real", (u8 *)hash, 20); 483 return -EBADFD; 484 } else { 485 printf("Image hash OK\n"); 486 } 487 488 #else /* !(CONFIG_DM_CRYPTO && CONFIG_ANDROID_BOOT_IMAGE_HASH) */ 489 if (image_read(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 490 return -1; 491 if (image_read(IMG_KERNEL, hdr, bstart, ram_base, NULL)) 492 return -1; 493 if (image_read(IMG_RAMDISK, hdr, bstart, ram_base, NULL)) 494 return -1; 495 if (image_read(IMG_SECOND, hdr, bstart, ram_base, NULL)) 496 return -1; 497 if (hdr->header_version > 0) { 498 if (image_read(IMG_RECOVERY_DTBO, hdr, bstart, ram_base, NULL)) 499 return -1; 500 } 501 if (hdr->header_version > 1) { 502 if (image_read(IMG_DTB, hdr, bstart, ram_base, NULL)) 503 return -1; 504 } 505 #endif 506 507 /* 508 * 2. Disable fdt/ramdisk relocation, it saves boot time. 509 */ 510 initrd_high = env_get("initrd_high"); 511 fdt_high = env_get("fdt_high"); 512 513 if (!fdt_high) { 514 env_set_hex("fdt_high", -1UL); 515 printf("Fdt "); 516 } 517 if (!initrd_high) { 518 env_set_hex("initrd_high", -1UL); 519 printf("Ramdisk "); 520 } 521 if (!fdt_high || !initrd_high) 522 printf("skip relocation\n"); 523 524 return 0; 525 } 526 527 /* 528 * 'boot_android' cmd use "kernel_addr_r" as default load address ! 529 * We update it according to compress type and "kernel_addr_c/r". 530 */ 531 int android_image_parse_comp(struct andr_img_hdr *hdr, ulong *load_addr) 532 { 533 ulong kernel_addr_c; 534 int comp; 535 536 kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0); 537 comp = android_image_parse_kernel_comp(hdr); 538 539 #ifdef CONFIG_ARM64 540 /* 541 * On 64-bit kernel, assuming use IMAGE by default. 542 * 543 * kernel_addr_c is for LZ4-IMAGE but maybe not defined. 544 * kernel_addr_r is for IMAGE. 545 */ 546 if (comp != IH_COMP_NONE) { 547 ulong comp_addr; 548 549 if (kernel_addr_c) { 550 comp_addr = kernel_addr_c; 551 } else { 552 printf("Warn: No \"kernel_addr_c\"\n"); 553 comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */ 554 env_set_ulong("kernel_addr_c", comp_addr); 555 } 556 557 *load_addr = comp_addr - hdr->page_size; 558 } 559 #else 560 /* 561 * On 32-bit kernel, assuming use zImage by default. 562 * 563 * kernel_addr_c is for LZ4/zImage but maybe not defined. 564 * kernel_addr_r is for zImage when kernel_addr_c is not defined. 565 * kernel_addr_r is for IMAGE when kernel_addr_c is defined. 566 */ 567 if (comp == IH_COMP_NONE) { 568 if (kernel_addr_c) 569 *load_addr = env_get_ulong("kernel_addr_r", 16, 0); 570 else 571 *load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000; 572 } else { 573 if (kernel_addr_c) 574 *load_addr = kernel_addr_c - hdr->page_size; 575 } 576 #endif 577 578 env_set_ulong("os_comp", comp); 579 return comp; 580 } 581 582 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp) 583 { 584 ulong kernel_addr_r; 585 586 /* zImage handles decompress itself */ 587 if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) { 588 kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000); 589 android_image_set_kload(hdr, kernel_addr_r); 590 android_image_set_comp(hdr, comp); 591 } else { 592 android_image_set_comp(hdr, IH_COMP_NONE); 593 } 594 } 595 596 static int android_image_load_separate(struct andr_img_hdr *hdr, 597 const disk_partition_t *part, 598 void *load_addr) 599 { 600 return android_image_separate(hdr, part, load_addr, NULL); 601 } 602 603 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr) 604 { 605 ulong comp_addr = *load_addr; 606 int comp; 607 608 comp = android_image_parse_comp(hdr, &comp_addr); 609 if (comp_addr == (ulong)hdr) 610 return 0; 611 612 if (android_image_separate(hdr, NULL, (void *)comp_addr, hdr)) 613 return -1; 614 615 *load_addr = comp_addr; 616 android_image_set_decomp((void *)comp_addr, comp); 617 618 return 0; 619 } 620 621 long android_image_load(struct blk_desc *dev_desc, 622 const disk_partition_t *part_info, 623 unsigned long load_address, 624 unsigned long max_size) { 625 struct andr_img_hdr *hdr; 626 u32 blksz = dev_desc->blksz; 627 u32 pszcnt, hdrcnt, kercnt; 628 int comp, ret; 629 630 if (max_size < part_info->blksz) 631 return -1; 632 633 /* 634 * read Android image header and leave enough space for page_size align 635 * and kernel image header(1 block maybe enough). 636 * 637 * ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE is defined by rockchip legacy 638 * mkboot tool(SDK version < 8.1) and larger than Google defined. 639 * 640 * To compatible this, we malloc enough buffer but only read android 641 * header and kernel image(1 block) from storage(ignore page size). 642 */ 643 kercnt = 1; 644 hdrcnt = DIV_ROUND_UP(sizeof(*hdr), blksz); 645 pszcnt = DIV_ROUND_UP(ANDROID_ROCKCHIP_LEGACY_PAGE_SIZE, blksz); 646 647 hdr = memalign(ARCH_DMA_MINALIGN, (hdrcnt + pszcnt + kercnt) * blksz); 648 if (!hdr) { 649 printf("No memory\n"); 650 return -1; 651 } 652 653 if (blk_dread(dev_desc, part_info->start, hdrcnt, hdr) != hdrcnt) { 654 printf("Failed to read image header\n"); 655 goto fail; 656 } 657 658 if (android_image_check_header(hdr) != 0) { 659 printf("** Invalid Android Image header **\n"); 660 goto fail; 661 } 662 663 /* 664 * Update and skip pszcnt(hdr is included) according to hdr->page_size, 665 * reading kernel image for compress validation. 666 */ 667 pszcnt = DIV_ROUND_UP(hdr->page_size, blksz); 668 if (blk_dread(dev_desc, part_info->start + pszcnt, kercnt, 669 (void *)((ulong)hdr + hdr->page_size)) != kercnt) { 670 printf("Failed to read kernel header\n"); 671 goto fail; 672 } 673 674 load_address -= hdr->page_size; 675 676 /* Let's load kernel now ! */ 677 comp = android_image_parse_comp(hdr, &load_address); 678 ret = android_image_load_separate(hdr, part_info, (void *)load_address); 679 if (ret) { 680 printf("Failed to load android image\n"); 681 goto fail; 682 } 683 android_image_set_decomp((void *)load_address, comp); 684 685 debug("Loading Android Image to 0x%08lx\n", load_address); 686 687 free(hdr); 688 return load_address; 689 690 fail: 691 free(hdr); 692 return -1; 693 } 694 695 #if !defined(CONFIG_SPL_BUILD) 696 /** 697 * android_print_contents - prints out the contents of the Android format image 698 * @hdr: pointer to the Android format image header 699 * 700 * android_print_contents() formats a multi line Android image contents 701 * description. 702 * The routine prints out Android image properties 703 * 704 * returns: 705 * no returned results 706 */ 707 void android_print_contents(const struct andr_img_hdr *hdr) 708 { 709 const char * const p = IMAGE_INDENT_STRING; 710 /* os_version = ver << 11 | lvl */ 711 u32 os_ver = hdr->os_version >> 11; 712 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 713 u32 header_version = hdr->header_version; 714 715 printf("%skernel size: %x\n", p, hdr->kernel_size); 716 printf("%skernel address: %x\n", p, hdr->kernel_addr); 717 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); 718 printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr); 719 printf("%ssecond size: %x\n", p, hdr->second_size); 720 printf("%ssecond address: %x\n", p, hdr->second_addr); 721 printf("%stags address: %x\n", p, hdr->tags_addr); 722 printf("%spage size: %x\n", p, hdr->page_size); 723 printf("%sheader_version: %x\n", p, header_version); 724 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) 725 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ 726 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", 727 p, hdr->os_version, 728 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, 729 (os_lvl >> 4) + 2000, os_lvl & 0x0F); 730 printf("%sname: %s\n", p, hdr->name); 731 printf("%scmdline: %s\n", p, hdr->cmdline); 732 733 if (header_version >= 1) { 734 printf("%srecovery dtbo size: %x\n", p, hdr->recovery_dtbo_size); 735 printf("%srecovery dtbo offset: %llx\n", p, hdr->recovery_dtbo_offset); 736 printf("%sheader size: %x\n", p, hdr->header_size); 737 } 738 739 if (header_version >= 2) { 740 printf("%sdtb size: %x\n", p, hdr->dtb_size); 741 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr); 742 } 743 } 744 #endif 745