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_ab.h> 10 #include <android_bootloader.h> 11 #include <android_image.h> 12 #include <malloc.h> 13 #include <mapmem.h> 14 #include <errno.h> 15 #include <boot_rkimg.h> 16 #include <sysmem.h> 17 #include <mp_boot.h> 18 #include <u-boot/sha1.h> 19 #ifdef CONFIG_RKIMG_BOOTLOADER 20 #include <asm/arch/resource_img.h> 21 #endif 22 #ifdef CONFIG_RK_AVB_LIBAVB_USER 23 #include <android_avb/avb_slot_verify.h> 24 #include <android_avb/avb_ops_user.h> 25 #include <android_avb/rk_avb_ops_user.h> 26 #endif 27 #include <optee_include/OpteeClientInterface.h> 28 29 DECLARE_GLOBAL_DATA_PTR; 30 31 #define BLK_CNT(_num_bytes, _block_size) \ 32 ((_num_bytes + _block_size - 1) / _block_size) 33 34 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; 35 static u32 android_kernel_comp_type = IH_COMP_NONE; 36 37 static int android_version_init(void) 38 { 39 struct andr_img_hdr *hdr = NULL; 40 struct blk_desc *desc; 41 const char *part_name = PART_BOOT; 42 disk_partition_t part; 43 int os_version; 44 45 desc = rockchip_get_bootdev(); 46 if (!desc) { 47 printf("No bootdev\n"); 48 return -1; 49 } 50 51 #ifdef CONFIG_ANDROID_AB 52 part_name = ab_can_find_recovery_part() ? PART_RECOVERY : PART_BOOT; 53 #endif 54 if (part_get_info_by_name(desc, part_name, &part) < 0) 55 return -1; 56 57 hdr = populate_andr_img_hdr(desc, &part); 58 if (!hdr) 59 return -1; 60 61 os_version = hdr->os_version; 62 if (os_version) 63 printf("Android %u.%u, Build %u.%u, v%d\n", 64 (os_version >> 25) & 0x7f, (os_version >> 18) & 0x7F, 65 ((os_version >> 4) & 0x7f) + 2000, os_version & 0x0F, 66 hdr->header_version); 67 free(hdr); 68 69 return (os_version >> 25) & 0x7f; 70 } 71 72 u32 android_bcb_msg_sector_offset(void) 73 { 74 static int android_version = -1; /* static */ 75 76 /* 77 * get android os version: 78 * 79 * There are two types of misc.img: 80 * Rockchip platforms defines BCB message at the 16KB offset of 81 * misc.img except for the Android version >= 10. Because Google 82 * defines it at 0x00 offset, and from Android-10 it becoms mandary 83 * on Google VTS. 84 * 85 * So we must get android 'os_version' to identify which type we 86 * are using, then we could able to use rockchip_get_boot_mode() 87 * which reads BCB from misc.img. 88 */ 89 #ifdef CONFIG_RKIMG_BOOTLOADER 90 if (android_version < 0) 91 android_version = android_version_init(); 92 93 return (android_version >= 10) ? 0x00 : 0x20; 94 #else 95 return 0x00; 96 #endif 97 } 98 99 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE 100 int android_image_init_resource(struct blk_desc *desc, 101 disk_partition_t *out_part, 102 ulong *out_blk_offset) 103 { 104 struct andr_img_hdr *hdr = NULL; 105 const char *part_name = ANDROID_PARTITION_BOOT; 106 disk_partition_t part; 107 ulong offset; 108 int ret = 0; 109 110 if (!desc) 111 return -ENODEV; 112 113 #ifndef CONFIG_ANDROID_AB 114 if (rockchip_get_boot_mode() == BOOT_MODE_RECOVERY) 115 part_name = ANDROID_PARTITION_RECOVERY; 116 #endif 117 if (part_get_info_by_name(desc, part_name, &part) < 0) 118 return -ENOENT; 119 120 hdr = populate_andr_img_hdr(desc, &part); 121 if (!hdr) 122 return -EINVAL; 123 124 if (hdr->header_version >= 2 && hdr->dtb_size) 125 env_update("bootargs", "androidboot.dtb_idx=0"); 126 127 if (hdr->header_version <= 2) { 128 offset = hdr->page_size + 129 ALIGN(hdr->kernel_size, hdr->page_size) + 130 ALIGN(hdr->ramdisk_size, hdr->page_size); 131 *out_part = part; 132 *out_blk_offset = DIV_ROUND_UP(offset, desc->blksz); 133 } else { 134 ret = -EINVAL; 135 } 136 free(hdr); 137 138 return ret; 139 } 140 #endif 141 142 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr) 143 { 144 /* 145 * All the Android tools that generate a boot.img use this 146 * address as the default. 147 * 148 * Even though it doesn't really make a lot of sense, and it 149 * might be valid on some platforms, we treat that address as 150 * the default value for this field, and try to execute the 151 * kernel in place in such a case. 152 * 153 * Otherwise, we will return the actual value set by the user. 154 */ 155 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) 156 return (ulong)hdr + hdr->page_size; 157 158 #ifdef CONFIG_ARCH_ROCKCHIP 159 /* 160 * If kernel is compressed, kernel_addr is set as decompressed address 161 * after compressed being loaded to ram, so let's use it. 162 */ 163 if (android_kernel_comp_type != IH_COMP_NONE && 164 android_kernel_comp_type != IH_COMP_ZIMAGE) 165 return hdr->kernel_addr; 166 167 /* 168 * Compatble with rockchip legacy packing with kernel/ramdisk/second 169 * address base from 0x60000000(SDK versiont < 8.1), these are invalid 170 * address, so we calc it by real size. 171 */ 172 return (ulong)hdr + hdr->page_size; 173 #else 174 return hdr->kernel_addr; 175 #endif 176 177 } 178 179 void android_image_set_comp(struct andr_img_hdr *hdr, u32 comp) 180 { 181 android_kernel_comp_type = comp; 182 } 183 184 u32 android_image_get_comp(const struct andr_img_hdr *hdr) 185 { 186 return android_kernel_comp_type; 187 } 188 189 int android_image_parse_kernel_comp(const struct andr_img_hdr *hdr) 190 { 191 ulong kaddr = android_image_get_kernel_addr(hdr); 192 return bootm_parse_comp((const unsigned char *)kaddr); 193 } 194 195 /** 196 * android_image_get_kernel() - processes kernel part of Android boot images 197 * @hdr: Pointer to image header, which is at the start 198 * of the image. 199 * @verify: Checksum verification flag. Currently unimplemented. 200 * @os_data: Pointer to a ulong variable, will hold os data start 201 * address. 202 * @os_len: Pointer to a ulong variable, will hold os data length. 203 * 204 * This function returns the os image's start address and length. Also, 205 * it appends the kernel command line to the bootargs env variable. 206 * 207 * Return: Zero, os start address and length on success, 208 * otherwise on failure. 209 */ 210 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify, 211 ulong *os_data, ulong *os_len) 212 { 213 u32 kernel_addr = android_image_get_kernel_addr(hdr); 214 const char *cmdline = hdr->header_version < 3 ? 215 hdr->cmdline : hdr->total_cmdline; 216 /* 217 * Not all Android tools use the id field for signing the image with 218 * sha1 (or anything) so we don't check it. It is not obvious that the 219 * string is null terminated so we take care of this. 220 */ 221 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE); 222 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0'; 223 if (strlen(andr_tmp_str)) 224 printf("Android's image name: %s\n", andr_tmp_str); 225 226 printf("Kernel: 0x%08x - 0x%08x (%u KiB)\n", 227 kernel_addr, kernel_addr + hdr->kernel_size, 228 DIV_ROUND_UP(hdr->kernel_size, 1024)); 229 230 int len = 0; 231 if (cmdline) { 232 debug("Kernel command line: %s\n", cmdline); 233 len += strlen(cmdline); 234 } 235 236 char *bootargs = env_get("bootargs"); 237 if (bootargs) 238 len += strlen(bootargs); 239 240 char *newbootargs = malloc(len + 2); 241 if (!newbootargs) { 242 puts("Error: malloc in android_image_get_kernel failed!\n"); 243 return -ENOMEM; 244 } 245 *newbootargs = '\0'; 246 247 if (bootargs) { 248 strcpy(newbootargs, bootargs); 249 strcat(newbootargs, " "); 250 } 251 if (cmdline) 252 strcat(newbootargs, cmdline); 253 254 env_set("bootargs", newbootargs); 255 256 if (os_data) { 257 *os_data = (ulong)hdr; 258 *os_data += hdr->page_size; 259 } 260 if (os_len) 261 *os_len = hdr->kernel_size; 262 return 0; 263 } 264 265 int android_image_check_header(const struct andr_img_hdr *hdr) 266 { 267 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE); 268 } 269 270 ulong android_image_get_end(const struct andr_img_hdr *hdr) 271 { 272 ulong end; 273 /* 274 * The header takes a full page, the remaining components are aligned 275 * on page boundary 276 */ 277 end = (ulong)hdr; 278 if (hdr->header_version < 3) { 279 end += hdr->page_size; 280 end += ALIGN(hdr->kernel_size, hdr->page_size); 281 end += ALIGN(hdr->ramdisk_size, hdr->page_size); 282 end += ALIGN(hdr->second_size, hdr->page_size); 283 if (hdr->header_version == 1) { 284 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); 285 } else if (hdr->header_version == 2) { 286 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); 287 end += ALIGN(hdr->dtb_size, hdr->page_size); 288 } 289 } else { 290 /* boot_img_hdr_v34 */ 291 end += hdr->page_size; 292 end += ALIGN(hdr->kernel_size, hdr->page_size); 293 end += ALIGN(hdr->ramdisk_size, hdr->page_size); 294 } 295 296 return end; 297 } 298 299 u32 android_image_get_ksize(const struct andr_img_hdr *hdr) 300 { 301 return hdr->kernel_size; 302 } 303 304 void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address) 305 { 306 hdr->kernel_addr = load_address; 307 } 308 309 ulong android_image_get_kload(const struct andr_img_hdr *hdr) 310 { 311 return android_image_get_kernel_addr(hdr); 312 } 313 314 int android_image_get_ramdisk(const struct andr_img_hdr *hdr, 315 ulong *rd_data, ulong *rd_len) 316 { 317 ulong ramdisk_addr_r; 318 ulong start, end; 319 320 if (!hdr->ramdisk_size) { 321 *rd_data = *rd_len = 0; 322 return -1; 323 } 324 325 /* Have been loaded by android_image_load_separate() on ramdisk_addr_r */ 326 ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0); 327 if (!ramdisk_addr_r) { 328 printf("No Found Ramdisk Load Address.\n"); 329 return -1; 330 } 331 332 *rd_data = ramdisk_addr_r; 333 *rd_len = hdr->ramdisk_size; 334 if (hdr->header_version >= 3) 335 *rd_len += hdr->vendor_ramdisk_size; 336 if (hdr->header_version >= 4) { 337 *rd_len += hdr->vendor_bootconfig_size + 338 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 339 } 340 341 /* just for print msg */ 342 start = ramdisk_addr_r; 343 if (hdr->header_version >= 3) { 344 end = start + (ulong)hdr->vendor_ramdisk_size; 345 printf("v-ramdisk: 0x%08lx - 0x%08lx (%u KiB)\n", 346 start, end, DIV_ROUND_UP(hdr->vendor_ramdisk_size, 1024)); 347 start = end; 348 } 349 { 350 end = start + (ulong)hdr->ramdisk_size; 351 printf("ramdisk: 0x%08lx - 0x%08lx (%u KiB)\n", 352 start, end, DIV_ROUND_UP(hdr->ramdisk_size, 1024)); 353 start = end; 354 } 355 if (hdr->header_version >= 4) { 356 end = start + (ulong)hdr->vendor_bootconfig_size; 357 printf("bootconfig: 0x%08lx - 0x%08lx (%u KiB)\n", 358 start, end, DIV_ROUND_UP(hdr->vendor_bootconfig_size, 1024)); 359 start = end; 360 end = start + ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 361 printf("bootparams: 0x%08lx - 0x%08lx\n", start, end); 362 } 363 364 return 0; 365 } 366 367 int android_image_get_fdt(const struct andr_img_hdr *hdr, 368 ulong *rd_data) 369 { 370 ulong fdt_addr_r; 371 372 if (!hdr->second_size) { 373 *rd_data = 0; 374 return -1; 375 } 376 377 /* Have been loaded by android_image_load_separate() on fdt_addr_r */ 378 fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0); 379 if (!fdt_addr_r) { 380 printf("No Found FDT Load Address.\n"); 381 return -1; 382 } 383 384 *rd_data = fdt_addr_r; 385 386 debug("FDT load addr 0x%08x size %u KiB\n", 387 hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024)); 388 389 return 0; 390 } 391 392 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH 393 static void print_hash(const char *label, u8 *hash, int len) 394 { 395 int i; 396 397 printf("%s:\n 0x", label ? : "Hash"); 398 for (i = 0; i < len; i++) 399 printf("%02x", hash[i]); 400 printf("\n"); 401 } 402 #endif 403 404 typedef enum { 405 IMG_KERNEL, 406 IMG_RAMDISK, /* within boot.img or init_boot.img(Android-13 or later) */ 407 IMG_SECOND, 408 IMG_RECOVERY_DTBO, 409 IMG_RK_DTB, /* within resource.img in second position */ 410 IMG_DTB, 411 IMG_VENDOR_RAMDISK, 412 IMG_BOOTCONFIG, 413 IMG_MAX, 414 } img_t; 415 416 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH 417 static sha1_context sha1_ctx; 418 #endif 419 420 static int image_load(img_t img, struct andr_img_hdr *hdr, 421 ulong blkstart, void *ram_base) 422 { 423 struct blk_desc *desc = rockchip_get_bootdev(); 424 disk_partition_t part_vendor_boot; 425 disk_partition_t part_init_boot; 426 __maybe_unused u32 typesz; 427 u32 andr_version = (hdr->os_version >> 25) & 0x7f; 428 ulong pgsz = hdr->page_size; 429 ulong blksz = desc->blksz; 430 ulong blkcnt, blkoff; 431 ulong memmove_dst = 0; 432 ulong bsoffs = 0; 433 ulong extra = 0; 434 ulong length; 435 void *buffer; 436 void *tmp = NULL; 437 int ret = 0; 438 439 switch (img) { 440 case IMG_KERNEL: 441 bsoffs = 0; /* include a page_size(image header) */ 442 length = hdr->kernel_size + pgsz; 443 buffer = (void *)env_get_ulong("android_addr_r", 16, 0); 444 blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz); 445 typesz = sizeof(hdr->kernel_size); 446 if (!sysmem_alloc_base(MEM_KERNEL, 447 (phys_addr_t)buffer, blkcnt * blksz)) 448 return -ENOMEM; 449 break; 450 case IMG_VENDOR_RAMDISK: 451 if (hdr->vendor_boot_buf) { 452 ram_base = hdr->vendor_boot_buf; 453 } else { 454 if (part_get_info_by_name(desc, 455 ANDROID_PARTITION_VENDOR_BOOT, 456 &part_vendor_boot) < 0) { 457 printf("No vendor boot partition\n"); 458 return -ENOENT; 459 } 460 ram_base = 0; 461 } 462 463 blkstart = part_vendor_boot.start; 464 pgsz = hdr->vendor_page_size; 465 bsoffs = ALIGN(VENDOR_BOOT_HDRv3_SIZE, pgsz); 466 length = hdr->vendor_ramdisk_size; 467 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 468 blkcnt = DIV_ROUND_UP(hdr->vendor_ramdisk_size, blksz); 469 typesz = sizeof(hdr->vendor_ramdisk_size); 470 /* 471 * Add extra memory for generic ramdisk space. 472 * 473 * In case of unaligned vendor ramdisk size, reserve 474 * 1 more blksz. 475 * 476 * Reserve 8KB for bootloader cmdline. 477 */ 478 if (hdr->header_version >= 3) 479 extra += ALIGN(hdr->ramdisk_size, blksz) + blksz; 480 if (hdr->header_version >= 4) 481 extra += ALIGN(hdr->vendor_bootconfig_size, blksz) + 482 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 483 if (length && !sysmem_alloc_base(MEM_RAMDISK, 484 (phys_addr_t)buffer, blkcnt * blksz + extra)) 485 return -ENOMEM; 486 break; 487 case IMG_RAMDISK: 488 /* get ramdisk from init_boot.img ? */ 489 if (hdr->header_version >= 4 && andr_version >= 13) { 490 if (hdr->init_boot_buf) { 491 ram_base = hdr->init_boot_buf; 492 } else { 493 if (part_get_info_by_name(desc, 494 ANDROID_PARTITION_INIT_BOOT, &part_init_boot) < 0) { 495 printf("No init boot partition\n"); 496 return -ENOENT; 497 } 498 blkstart = part_init_boot.start; 499 ram_base = 0; 500 } 501 bsoffs = pgsz; 502 } else { 503 /* get ramdisk from generic boot.img */ 504 bsoffs = pgsz + ALIGN(hdr->kernel_size, pgsz); 505 } 506 507 length = hdr->ramdisk_size; 508 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 509 blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz); 510 typesz = sizeof(hdr->ramdisk_size); 511 512 /* 513 * ramdisk_addr_r v012: 514 * |----------------| 515 * | ramdisk | 516 * |----------------| 517 * 518 * ramdisk_addr_r v3 (Android-11 and later): 519 * |----------------|---------| 520 * | vendor-ramdisk | ramdisk | 521 * |----------------|---------| 522 * 523 * ramdisk_addr_r v4 (Android-12 and later): 524 * |----------------|---------|------------|------------| 525 * | vendor-ramdisk | ramdisk | bootconfig | bootparams | 526 * |----------------|---------|------------|------------| 527 * 528 * ramdisk_addr_r v4 + init_boot(Android-13 and later): 529 * |----------------|----------------|------------|------------| 530 * | vendor-ramdisk | (init_)ramdisk | bootconfig | bootparams | 531 * |----------------|----------------|------------|------------| 532 */ 533 if (hdr->header_version >= 3) { 534 buffer += hdr->vendor_ramdisk_size; 535 if (!IS_ALIGNED((ulong)buffer, blksz)) { 536 memmove_dst = (ulong)buffer; 537 buffer = (void *)ALIGN(memmove_dst, blksz); 538 } 539 } 540 /* sysmem has been alloced by vendor ramdisk */ 541 if (hdr->header_version < 3) { 542 if (length && !sysmem_alloc_base(MEM_RAMDISK, 543 (phys_addr_t)buffer, blkcnt * blksz)) 544 return -ENOMEM; 545 } 546 break; 547 case IMG_BOOTCONFIG: 548 if (hdr->header_version < 4) 549 return 0; 550 551 if (hdr->vendor_boot_buf) { 552 ram_base = hdr->vendor_boot_buf; 553 } else { 554 if (part_get_info_by_name(desc, 555 ANDROID_PARTITION_VENDOR_BOOT, 556 &part_vendor_boot) < 0) { 557 printf("No vendor boot partition\n"); 558 return -ENOENT; 559 } 560 ram_base = 0; 561 } 562 blkstart = part_vendor_boot.start; 563 pgsz = hdr->vendor_page_size; 564 bsoffs = ALIGN(VENDOR_BOOT_HDRv4_SIZE, pgsz) + 565 ALIGN(hdr->vendor_ramdisk_size, pgsz) + 566 ALIGN(hdr->dtb_size, pgsz) + 567 ALIGN(hdr->vendor_ramdisk_table_size, pgsz); 568 length = hdr->vendor_bootconfig_size; 569 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 570 blkcnt = DIV_ROUND_UP(hdr->vendor_bootconfig_size, blksz); 571 typesz = sizeof(hdr->vendor_bootconfig_size); 572 573 buffer += hdr->vendor_ramdisk_size + hdr->ramdisk_size; 574 if (!IS_ALIGNED((ulong)buffer, blksz)) { 575 memmove_dst = (ulong)buffer; 576 buffer = (void *)ALIGN(memmove_dst, blksz); 577 } 578 break; 579 case IMG_SECOND: 580 bsoffs = pgsz + 581 ALIGN(hdr->kernel_size, pgsz) + 582 ALIGN(hdr->ramdisk_size, pgsz); 583 length = hdr->second_size; 584 blkcnt = DIV_ROUND_UP(hdr->second_size, blksz); 585 buffer = tmp = malloc(blkcnt * blksz); 586 typesz = sizeof(hdr->second_size); 587 break; 588 case IMG_RECOVERY_DTBO: 589 bsoffs = pgsz + 590 ALIGN(hdr->kernel_size, pgsz) + 591 ALIGN(hdr->ramdisk_size, pgsz) + 592 ALIGN(hdr->second_size, pgsz); 593 length = hdr->recovery_dtbo_size; 594 blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz); 595 buffer = tmp = malloc(blkcnt * blksz); 596 typesz = sizeof(hdr->recovery_dtbo_size); 597 break; 598 case IMG_DTB: 599 bsoffs = pgsz + 600 ALIGN(hdr->kernel_size, pgsz) + 601 ALIGN(hdr->ramdisk_size, pgsz) + 602 ALIGN(hdr->second_size, pgsz) + 603 ALIGN(hdr->recovery_dtbo_size, pgsz); 604 length = hdr->dtb_size; 605 blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz); 606 buffer = tmp = malloc(blkcnt * blksz); 607 typesz = sizeof(hdr->dtb_size); 608 break; 609 case IMG_RK_DTB: 610 #ifdef CONFIG_RKIMG_BOOTLOADER 611 /* No going further, it handles DTBO, HW-ID, etc */ 612 buffer = (void *)env_get_ulong("fdt_addr_r", 16, 0); 613 if (gd->fdt_blob != (void *)buffer) 614 ret = rockchip_read_dtb_file(buffer); 615 #endif 616 return ret < 0 ? ret : 0; 617 default: 618 return -EINVAL; 619 } 620 621 if (!buffer) { 622 printf("No memory for image(%d)\n", img); 623 return -ENOMEM; 624 } 625 626 if (!blksz || !length) 627 goto crypto_calc; 628 629 /* load */ 630 if (ram_base) { 631 memcpy(buffer, (char *)((ulong)ram_base + bsoffs), length); 632 } else { 633 blkoff = DIV_ROUND_UP(bsoffs, blksz); 634 ret = blk_dread(desc, blkstart + blkoff, blkcnt, buffer); 635 if (ret != blkcnt) { 636 printf("Failed to read img(%d), ret=%d\n", img, ret); 637 return -EIO; 638 } 639 } 640 641 if (memmove_dst) 642 memmove((char *)memmove_dst, buffer, length); 643 644 crypto_calc: 645 if (img == IMG_KERNEL) { 646 buffer += pgsz; 647 length -= pgsz; 648 } 649 650 /* sha1 */ 651 if (hdr->header_version < 3) { 652 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH 653 sha1_update(&sha1_ctx, (void *)buffer, length); 654 sha1_update(&sha1_ctx, (void *)&length, typesz); 655 #endif 656 } 657 658 if (tmp) 659 free(tmp); 660 661 return 0; 662 } 663 664 static int images_load_verify(struct andr_img_hdr *hdr, ulong part_start, void *ram_base) 665 { 666 /* load, never change order ! */ 667 if (image_load(IMG_KERNEL, hdr, part_start, ram_base)) 668 return -1; 669 if (image_load(IMG_RAMDISK, hdr, part_start, ram_base)) 670 return -1; 671 if (image_load(IMG_SECOND, hdr, part_start, ram_base)) 672 return -1; 673 if (hdr->header_version > 0) { 674 if (image_load(IMG_RECOVERY_DTBO, hdr, part_start, ram_base)) 675 return -1; 676 } 677 if (hdr->header_version > 1) { 678 if (image_load(IMG_DTB, hdr, part_start, ram_base)) 679 return -1; 680 } 681 682 return 0; 683 } 684 685 /* 686 * @ram_base: !NULL means require memcpy for an exist full android image. 687 */ 688 static int android_image_separate(struct andr_img_hdr *hdr, 689 const disk_partition_t *part, 690 void *load_address, 691 void *ram_base) 692 { 693 ulong bstart; 694 int ret; 695 696 if (android_image_check_header(hdr)) { 697 printf("Bad android image header\n"); 698 return -EINVAL; 699 } 700 701 /* set for image_load(IMG_KERNEL, ...) */ 702 env_set_hex("android_addr_r", (ulong)load_address); 703 bstart = part ? part->start : 0; 704 705 /* 706 * 1. Load images to their individual target ram position 707 * in order to disable fdt/ramdisk relocation. 708 */ 709 710 /* load rk-kernel.dtb alone */ 711 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base)) 712 return -1; 713 714 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH 715 int verify = 1; 716 717 #ifdef CONFIG_MP_BOOT 718 verify = mpb_post(3); 719 #endif 720 if (hdr->header_version < 3 && verify) { 721 uchar hash[20]; 722 723 /* rk crypto: requires total length before sha init */ 724 sha1_ctx.length = 0; 725 sha1_ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) + 726 hdr->ramdisk_size + sizeof(hdr->ramdisk_size) + 727 hdr->second_size + sizeof(hdr->second_size); 728 if (hdr->header_version > 0) 729 sha1_ctx.length += hdr->recovery_dtbo_size + 730 sizeof(hdr->recovery_dtbo_size); 731 if (hdr->header_version > 1) 732 sha1_ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size); 733 734 sha1_starts(&sha1_ctx); 735 ret = images_load_verify(hdr, bstart, ram_base); 736 if (ret) 737 return ret; 738 739 sha1_finish(&sha1_ctx, hash); 740 if (memcmp(hash, hdr->id, 20)) { 741 print_hash("Hash from header", (u8 *)hdr->id, 20); 742 print_hash("Hash real", (u8 *)hash, 20); 743 return -EBADFD; 744 } else { 745 int i; 746 747 printf("ANDROID: sha1("); 748 for (i = 0; i < 5; i++) 749 printf("%02x", hash[i]); 750 printf("...) + OK\n"); 751 } 752 } else 753 #endif 754 { 755 ret = images_load_verify(hdr, bstart, ram_base); 756 if (ret) 757 return ret; 758 } 759 760 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 761 env_set("bootm-no-reloc", "y"); 762 763 return 0; 764 } 765 766 static int android_image_separate_v34(struct andr_img_hdr *hdr, 767 const disk_partition_t *part, 768 void *load_address, void *ram_base) 769 { 770 ulong bstart; 771 772 if (android_image_check_header(hdr)) { 773 printf("Bad android image header\n"); 774 return -EINVAL; 775 } 776 777 /* set for image_load(IMG_KERNEL, ...) */ 778 env_set_hex("android_addr_r", (ulong)load_address); 779 bstart = part ? part->start : 0; 780 781 /* 782 * 1. Load images to their individual target ram position 783 * in order to disable fdt/ramdisk relocation. 784 */ 785 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base)) 786 return -1; 787 if (image_load(IMG_KERNEL, hdr, bstart, ram_base)) 788 return -1; 789 if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base)) 790 return -1; 791 if (image_load(IMG_RAMDISK, hdr, bstart, ram_base)) 792 return -1; 793 if (image_load(IMG_BOOTCONFIG, hdr, bstart, ram_base)) 794 return -1; 795 /* 796 * Copy the populated hdr to load address after image_load(IMG_KERNEL) 797 * 798 * The image_load(IMG_KERNEL) only reads boot_img_hdr_v34 while 799 * vendor_boot_img_hdr_v34 is not included, so fix it here. 800 */ 801 memcpy((char *)load_address, hdr, hdr->page_size); 802 803 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 804 env_set("bootm-no-reloc", "y"); 805 806 return 0; 807 } 808 809 static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp) 810 { 811 ulong kernel_addr_c; 812 ulong load_addr = 0; 813 814 kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0); 815 816 #ifdef CONFIG_ARM64 817 /* 818 * On 64-bit kernel, assuming use IMAGE by default. 819 * 820 * kernel_addr_c is for LZ4-IMAGE but maybe not defined. 821 * kernel_addr_r is for IMAGE. 822 */ 823 if (comp != IH_COMP_NONE) { 824 ulong comp_addr; 825 826 if (kernel_addr_c) { 827 comp_addr = kernel_addr_c; 828 } else { 829 printf("Warn: No \"kernel_addr_c\"\n"); 830 comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */ 831 env_set_hex("kernel_addr_c", comp_addr); 832 } 833 834 load_addr = comp_addr - hdr->page_size; 835 } 836 #else 837 /* 838 * On 32-bit kernel: 839 * 840 * The input load_addr is from env value: "kernel_addr_r", it has 841 * different role depends on whether kernel_addr_c is defined: 842 * 843 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined. 844 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined. 845 */ 846 if (comp == IH_COMP_NONE) { 847 if (kernel_addr_c) { 848 /* input load_addr is for Image, nothing to do */ 849 } else { 850 /* input load_addr is for lz4/zImage, set default addr for Image */ 851 load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000; 852 env_set_hex("kernel_addr_r", load_addr); 853 854 load_addr -= hdr->page_size; 855 } 856 } else { 857 if (kernel_addr_c) { 858 /* input load_addr is for Image, so use another for lz4/zImage */ 859 load_addr = kernel_addr_c - hdr->page_size; 860 } else { 861 /* input load_addr is for lz4/zImage, nothing to do */ 862 } 863 } 864 #endif 865 866 return load_addr; 867 } 868 869 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp) 870 { 871 ulong kernel_addr_r; 872 873 env_set_ulong("os_comp", comp); 874 875 /* zImage handles decompress itself */ 876 if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) { 877 kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000); 878 android_image_set_kload(hdr, kernel_addr_r); 879 android_image_set_comp(hdr, comp); 880 } else { 881 android_image_set_comp(hdr, IH_COMP_NONE); 882 } 883 } 884 885 static int android_image_load_separate(struct andr_img_hdr *hdr, 886 const disk_partition_t *part, 887 void *load_addr) 888 { 889 if (hdr->header_version < 3) 890 return android_image_separate(hdr, part, load_addr, NULL); 891 else 892 return android_image_separate_v34(hdr, part, load_addr, NULL); 893 } 894 895 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr) 896 { 897 ulong comp_addr; 898 int comp; 899 900 comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size); 901 comp_addr = android_image_get_comp_addr(hdr, comp); 902 903 /* non-compressed image: already in-place */ 904 if ((ulong)hdr == *load_addr) 905 return 0; 906 907 /* compressed image */ 908 if (comp_addr) { 909 *load_addr = comp_addr; 910 if ((ulong)hdr == comp_addr) /* already in-place */ 911 return 0; 912 } 913 914 /* 915 * The most possible reason to arrive here is: 916 * 917 * VBoot=1 and AVB load full partition to a temp memory buffer, now we 918 * separate(memcpy) subimages from boot.img to where they should be. 919 */ 920 if (hdr->header_version < 3) { 921 if (android_image_separate(hdr, NULL, (void *)(*load_addr), hdr)) 922 return -1; 923 } else { 924 if (android_image_separate_v34(hdr, NULL, (void *)(*load_addr), hdr)) 925 return -1; 926 } 927 928 android_image_set_decomp((void *)(*load_addr), comp); 929 930 return 0; 931 } 932 933 long android_image_load(struct blk_desc *dev_desc, 934 const disk_partition_t *part_info, 935 unsigned long load_address, 936 unsigned long max_size) { 937 struct andr_img_hdr *hdr; 938 ulong comp_addr; 939 int comp, ret; 940 int blk_off; 941 942 if (max_size < part_info->blksz) 943 return -1; 944 945 hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info); 946 if (!hdr) { 947 printf("No valid android hdr\n"); 948 return -1; 949 } 950 951 /* 952 * create the layout: 953 * 954 * |<- page_size ->|1-blk | 955 * |-----|---------|------|-----| 956 * | hdr | ... | kernel | 957 * |-----|----- ---|------------| 958 * 959 * Alloc page_size and 1 more blk for reading kernel image to 960 * get it's compression type, then fill the android hdr what 961 * we have populated before. 962 * 963 * Why? see: android_image_get_kernel_addr(). 964 */ 965 blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz); 966 hdr = (struct andr_img_hdr *) 967 realloc(hdr, (blk_off + 1) * dev_desc->blksz); 968 if (!hdr) 969 return -1; 970 971 if (blk_dread(dev_desc, part_info->start + blk_off, 1, 972 (char *)hdr + hdr->page_size) != 1) { 973 free(hdr); 974 return -1; 975 } 976 977 /* Changed to compressed address ? */ 978 comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size); 979 comp_addr = android_image_get_comp_addr(hdr, comp); 980 if (comp_addr) 981 load_address = comp_addr; 982 else 983 load_address -= hdr->page_size; 984 985 ret = android_image_load_separate(hdr, part_info, (void *)load_address); 986 if (ret) { 987 printf("Failed to load android image\n"); 988 goto fail; 989 } 990 android_image_set_decomp((void *)load_address, comp); 991 992 debug("Loading Android Image to 0x%08lx\n", load_address); 993 994 free(hdr); 995 return load_address; 996 997 fail: 998 free(hdr); 999 return -1; 1000 } 1001 1002 static struct andr_img_hdr * 1003 extract_boot_image_v012_header(struct blk_desc *dev_desc, 1004 const disk_partition_t *boot_img) 1005 { 1006 struct andr_img_hdr *hdr; 1007 long blk_cnt, blks_read; 1008 1009 blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz); 1010 hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz); 1011 1012 if (!blk_cnt || !hdr) 1013 return NULL; 1014 1015 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr); 1016 if (blks_read != blk_cnt) { 1017 debug("boot img header blk cnt is %ld and blks read is %ld\n", 1018 blk_cnt, blks_read); 1019 return NULL; 1020 } 1021 1022 if (android_image_check_header((void *)hdr)) { 1023 printf("boot header magic is invalid.\n"); 1024 return NULL; 1025 } 1026 1027 if (hdr->page_size < sizeof(*hdr)) { 1028 printf("android hdr is over size\n"); 1029 return NULL; 1030 } 1031 1032 return hdr; 1033 } 1034 1035 static struct boot_img_hdr_v34 * 1036 extract_boot_image_v34_header(struct blk_desc *dev_desc, 1037 const disk_partition_t *boot_img) 1038 { 1039 struct boot_img_hdr_v34 *boot_hdr; 1040 disk_partition_t part; 1041 long blk_cnt, blks_read; 1042 1043 blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v34), dev_desc->blksz); 1044 boot_hdr = (struct boot_img_hdr_v34 *)malloc(blk_cnt * dev_desc->blksz); 1045 1046 if (!blk_cnt || !boot_hdr) 1047 return NULL; 1048 1049 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_hdr); 1050 if (blks_read != blk_cnt) { 1051 debug("boot img header blk cnt is %ld and blks read is %ld\n", 1052 blk_cnt, blks_read); 1053 return NULL; 1054 } 1055 1056 if (android_image_check_header((void *)boot_hdr)) { 1057 printf("boot header magic is invalid.\n"); 1058 return NULL; 1059 } 1060 1061 if (boot_hdr->header_version < 3) { 1062 printf("boot header %d, is not >= v3.\n", 1063 boot_hdr->header_version); 1064 return NULL; 1065 } 1066 1067 /* 1068 * [Start from android-13 GKI, it doesn't assign 'os_version'] 1069 * 1070 * Android_12 or later are header_version >= 4. 1071 * Android_13(GKI) introduce a new partition named "init_boot" and 1072 * doesn't assign 'os_version' any more(ie. default 0). 1073 * 1074 * We only assign 'os_version' depend on whether there is 1075 * init_boot partition or not. 1076 */ 1077 if (boot_hdr->header_version >= 4 && boot_hdr->os_version == 0) { 1078 if (part_get_info_by_name(dev_desc, 1079 ANDROID_PARTITION_INIT_BOOT, &part) > 0) 1080 boot_hdr->os_version = 13 << 25; 1081 } 1082 1083 return boot_hdr; 1084 } 1085 1086 static struct vendor_boot_img_hdr_v34 * 1087 extract_vendor_boot_image_v34_header(struct blk_desc *dev_desc, 1088 const disk_partition_t *part_vendor_boot) 1089 { 1090 struct vendor_boot_img_hdr_v34 *vboot_hdr; 1091 long blk_cnt, blks_read; 1092 1093 blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v34), 1094 part_vendor_boot->blksz); 1095 vboot_hdr = (struct vendor_boot_img_hdr_v34 *) 1096 malloc(blk_cnt * part_vendor_boot->blksz); 1097 1098 if (!blk_cnt || !vboot_hdr) 1099 return NULL; 1100 1101 blks_read = blk_dread(dev_desc, part_vendor_boot->start, 1102 blk_cnt, vboot_hdr); 1103 if (blks_read != blk_cnt) { 1104 debug("vboot img header blk cnt is %ld and blks read is %ld\n", 1105 blk_cnt, blks_read); 1106 return NULL; 1107 } 1108 1109 if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic, 1110 VENDOR_BOOT_MAGIC_SIZE)) { 1111 printf("vendor boot header is invalid.\n"); 1112 return NULL; 1113 } 1114 1115 if (vboot_hdr->header_version < 3) { 1116 printf("vendor boot header %d, is not >= v3.\n", 1117 vboot_hdr->header_version); 1118 return NULL; 1119 } 1120 1121 return vboot_hdr; 1122 } 1123 1124 int populate_boot_info(const struct boot_img_hdr_v34 *boot_hdr, 1125 const struct vendor_boot_img_hdr_v34 *vendor_boot_hdr, 1126 const struct boot_img_hdr_v34 *init_boot_hdr, 1127 struct andr_img_hdr *hdr, bool save_hdr) 1128 { 1129 memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE); 1130 memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE); 1131 1132 hdr->kernel_size = boot_hdr->kernel_size; 1133 /* don't use vendor_boot_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */ 1134 hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR; 1135 1136 /* 1137 * generic ramdisk: immediately following the vendor ramdisk. 1138 * It can be from init_boot.img or boot.img. 1139 */ 1140 if (init_boot_hdr) 1141 hdr->ramdisk_size = init_boot_hdr->ramdisk_size; 1142 else 1143 hdr->ramdisk_size = boot_hdr->ramdisk_size; 1144 1145 /* actually, useless */ 1146 hdr->ramdisk_addr = env_get_ulong("ramdisk_addr_r", 16, 0); 1147 1148 /* removed in v3 */ 1149 hdr->second_size = 0; 1150 hdr->second_addr = 0; 1151 1152 hdr->tags_addr = vendor_boot_hdr->tags_addr; 1153 1154 /* fixed in v3 */ 1155 hdr->page_size = 4096; 1156 hdr->header_version = boot_hdr->header_version; 1157 /* 1158 * [Start from android-13 GKI, it doesn't assign 'os_version'] 1159 * 1160 * Android_12 or later are header_version >= 4. 1161 * Android_13(GKI) introduce a new partition named "init_boot" and 1162 * doesn't assign 'os_version' any more(ie. default 0). 1163 * 1164 * We only assign 'os_version' depend on whether there is 1165 * init_boot partition or not. 1166 */ 1167 if (boot_hdr->header_version >= 4 && boot_hdr->os_version == 0) { 1168 if (init_boot_hdr) 1169 hdr->os_version = 13 << 25; 1170 1171 if (!hdr->os_version) 1172 printf("WARN: it seems to be an invalid Android os_version: 0\n"); 1173 } else { 1174 hdr->os_version = boot_hdr->os_version; 1175 } 1176 1177 memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE); 1178 strncpy(hdr->name, (const char *)vendor_boot_hdr->name, ANDR_BOOT_NAME_SIZE); 1179 1180 /* removed in v3 */ 1181 memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE); 1182 memset(hdr->id, 0, 32); 1183 memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE); 1184 hdr->recovery_dtbo_size = 0; 1185 hdr->recovery_dtbo_offset = 0; 1186 1187 hdr->header_size = boot_hdr->header_size; 1188 hdr->dtb_size = vendor_boot_hdr->dtb_size; 1189 hdr->dtb_addr = vendor_boot_hdr->dtb_addr; 1190 1191 /* boot_img_hdr_v34 fields */ 1192 hdr->vendor_ramdisk_size = vendor_boot_hdr->vendor_ramdisk_size; 1193 hdr->vendor_page_size = vendor_boot_hdr->page_size; 1194 hdr->vendor_header_version = vendor_boot_hdr->header_version; 1195 hdr->vendor_header_size = vendor_boot_hdr->header_size; 1196 1197 hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE); 1198 if (!hdr->total_cmdline) 1199 return -ENOMEM; 1200 strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline, 1201 sizeof(boot_hdr->cmdline)); 1202 strncat(hdr->total_cmdline, " ", 1); 1203 strncat(hdr->total_cmdline, (const char *)vendor_boot_hdr->cmdline, 1204 sizeof(vendor_boot_hdr->cmdline)); 1205 1206 /* new for header v4 */ 1207 if (vendor_boot_hdr->header_version >= 4) { 1208 hdr->vendor_ramdisk_table_size = 1209 vendor_boot_hdr->vendor_ramdisk_table_size; 1210 hdr->vendor_ramdisk_table_entry_num = 1211 vendor_boot_hdr->vendor_ramdisk_table_entry_num; 1212 hdr->vendor_ramdisk_table_entry_size = 1213 vendor_boot_hdr->vendor_ramdisk_table_entry_size; 1214 /* 1215 * If we place additional "androidboot.xxx" parameters after 1216 * bootconfig, this field value should be increased, 1217 * but not over than ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE. 1218 */ 1219 hdr->vendor_bootconfig_size = 1220 vendor_boot_hdr->vendor_bootconfig_size; 1221 } else { 1222 hdr->vendor_ramdisk_table_size = 0; 1223 hdr->vendor_ramdisk_table_entry_num = 0; 1224 hdr->vendor_ramdisk_table_entry_size = 0; 1225 hdr->vendor_bootconfig_size = 0; 1226 } 1227 1228 hdr->init_boot_buf = save_hdr ? (void *)init_boot_hdr : 0; 1229 hdr->vendor_boot_buf = save_hdr ? (void *)vendor_boot_hdr : 0; 1230 1231 if (hdr->page_size < sizeof(*hdr)) { 1232 printf("android hdr is over size\n"); 1233 return -EINVAL; 1234 } 1235 1236 return 0; 1237 } 1238 1239 /* 1240 * The possible cases of boot.img + recovery.img: 1241 * 1242 * [N]: 0, 1, 2 1243 * [M]: 0, 1, 2, 3, 4 1244 * 1245 * |--------------------|---------------------| 1246 * | boot.img | recovery.img | 1247 * |--------------------|---------------------| 1248 * | boot_img_hdr_v[N] | boot_img_hdr_v[N] | <= if A/B is not required 1249 * |--------------------|---------------------| 1250 * | boot_img_hdr_v34 | boot_img_hdr_v2 | <= if A/B is not required 1251 * |------------------------------------------| 1252 * | boot_img_hdr_v[M], no recovery.img | <= if A/B is required 1253 * |------------------------------------------| 1254 */ 1255 struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc, 1256 disk_partition_t *part_boot) 1257 { 1258 disk_partition_t part_vendor_boot; 1259 disk_partition_t part_init_boot; 1260 struct vendor_boot_img_hdr_v34 *vboot_hdr = NULL; 1261 struct boot_img_hdr_v34 *iboot_hdr = NULL; 1262 struct boot_img_hdr_v34 *boot_hdr = NULL; 1263 struct andr_img_hdr *andr_hdr = NULL; 1264 int header_version; 1265 int andr_version; 1266 1267 if (!dev_desc || !part_boot) 1268 return NULL; 1269 1270 andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz); 1271 if (!andr_hdr) 1272 return NULL; 1273 1274 if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) { 1275 free(andr_hdr); 1276 return NULL; 1277 } 1278 1279 if (android_image_check_header(andr_hdr)) { 1280 free(andr_hdr); 1281 return NULL; 1282 } 1283 1284 header_version = andr_hdr->header_version; 1285 free(andr_hdr); 1286 andr_hdr = NULL; 1287 1288 if (header_version < 3) { 1289 return extract_boot_image_v012_header(dev_desc, part_boot); 1290 } else { 1291 if (part_get_info_by_name(dev_desc, 1292 ANDROID_PARTITION_VENDOR_BOOT, 1293 &part_vendor_boot) < 0) { 1294 printf("No vendor boot partition\n"); 1295 return NULL; 1296 } 1297 boot_hdr = extract_boot_image_v34_header(dev_desc, part_boot); 1298 vboot_hdr = extract_vendor_boot_image_v34_header(dev_desc, 1299 &part_vendor_boot); 1300 if (!boot_hdr || !vboot_hdr) 1301 goto image_load_exit; 1302 1303 andr_version = (boot_hdr->os_version >> 25) & 0x7f; 1304 if (header_version >= 4 && andr_version >= 13) { 1305 if (part_get_info_by_name(dev_desc, 1306 ANDROID_PARTITION_INIT_BOOT, 1307 &part_init_boot) < 0) { 1308 printf("No init boot partition\n"); 1309 return NULL; 1310 } 1311 iboot_hdr = extract_boot_image_v34_header(dev_desc, &part_init_boot); 1312 if (!iboot_hdr) 1313 goto image_load_exit; 1314 if (!iboot_hdr->ramdisk_size) { 1315 printf("No ramdisk in init boot partition\n"); 1316 goto image_load_exit; 1317 } 1318 } 1319 1320 andr_hdr = (struct andr_img_hdr *) 1321 malloc(sizeof(struct andr_img_hdr)); 1322 if (!andr_hdr) { 1323 printf("No memory for andr hdr\n"); 1324 goto image_load_exit; 1325 } 1326 1327 if (populate_boot_info(boot_hdr, vboot_hdr, 1328 iboot_hdr, andr_hdr, false)) { 1329 printf("populate boot info failed\n"); 1330 goto image_load_exit; 1331 } 1332 1333 image_load_exit: 1334 if (boot_hdr) 1335 free(boot_hdr); 1336 if (iboot_hdr) 1337 free(iboot_hdr); 1338 if (vboot_hdr) 1339 free(vboot_hdr); 1340 1341 return andr_hdr; 1342 } 1343 1344 return NULL; 1345 } 1346 1347 #if !defined(CONFIG_SPL_BUILD) 1348 /** 1349 * android_print_contents - prints out the contents of the Android format image 1350 * @hdr: pointer to the Android format image header 1351 * 1352 * android_print_contents() formats a multi line Android image contents 1353 * description. 1354 * The routine prints out Android image properties 1355 * 1356 * returns: 1357 * no returned results 1358 */ 1359 void android_print_contents(const struct andr_img_hdr *hdr) 1360 { 1361 const char * const p = IMAGE_INDENT_STRING; 1362 /* os_version = ver << 11 | lvl */ 1363 u32 os_ver = hdr->os_version >> 11; 1364 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 1365 u32 header_version = hdr->header_version; 1366 1367 printf("%skernel size: %x\n", p, hdr->kernel_size); 1368 printf("%skernel address: %x\n", p, hdr->kernel_addr); 1369 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); 1370 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr); 1371 printf("%ssecond size: %x\n", p, hdr->second_size); 1372 printf("%ssecond address: %x\n", p, hdr->second_addr); 1373 printf("%stags address: %x\n", p, hdr->tags_addr); 1374 printf("%spage size: %x\n", p, hdr->page_size); 1375 printf("%sheader_version: %x\n", p, header_version); 1376 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) 1377 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ 1378 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", 1379 p, hdr->os_version, 1380 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, 1381 (os_lvl >> 4) + 2000, os_lvl & 0x0F); 1382 printf("%sname: %s\n", p, hdr->name); 1383 printf("%scmdline: %s\n", p, hdr->cmdline); 1384 1385 if (header_version == 1 || header_version == 2) { 1386 printf("%srecovery dtbo size: %x\n", p, hdr->recovery_dtbo_size); 1387 printf("%srecovery dtbo offset: %llx\n", p, hdr->recovery_dtbo_offset); 1388 printf("%sheader size: %x\n", p, hdr->header_size); 1389 } 1390 1391 if (header_version == 2 || header_version == 3) { 1392 printf("%sdtb size: %x\n", p, hdr->dtb_size); 1393 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr); 1394 } 1395 1396 if (header_version >= 3) { 1397 printf("%scmdline: %s\n", p, hdr->total_cmdline); 1398 printf("%svendor ramdisk size: %x\n", p, hdr->vendor_ramdisk_size); 1399 printf("%svendor page size: %x\n", p, hdr->vendor_page_size); 1400 printf("%svendor header version: %d\n", p, hdr->vendor_header_version); 1401 printf("%svendor header size: %x\n", p, hdr->vendor_header_size); 1402 } 1403 1404 if (header_version >= 4) { 1405 printf("%svendor ramdisk table size: %x\n", 1406 p, hdr->vendor_ramdisk_table_size); 1407 printf("%svendor ramdisk table entry num: %x\n", 1408 p, hdr->vendor_ramdisk_table_entry_num); 1409 printf("%svendor ramdisk table entry size: %x\n", 1410 p, hdr->vendor_ramdisk_table_entry_size); 1411 printf("%svendor bootconfig size: %d\n", 1412 p, hdr->vendor_bootconfig_size); 1413 } 1414 } 1415 #endif 1416