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