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_PARTITION_VENDOR_BOOT "vendor_boot" 32 #define ANDROID_PARTITION_INIT_BOOT "init_boot" 33 34 #define BLK_CNT(_num_bytes, _block_size) \ 35 ((_num_bytes + _block_size - 1) / _block_size) 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 & 0x7f; 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 0x00 offset. 51 * 52 * From Android-Q, the 0x00 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() >= 10) ? 0x00 : 0x20; 57 #else 58 return 0x00; 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 const char *cmdline = hdr->header_version < 3 ? 135 hdr->cmdline : hdr->total_cmdline; 136 /* 137 * Not all Android tools use the id field for signing the image with 138 * sha1 (or anything) so we don't check it. It is not obvious that the 139 * string is null terminated so we take care of this. 140 */ 141 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE); 142 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0'; 143 if (strlen(andr_tmp_str)) 144 printf("Android's image name: %s\n", andr_tmp_str); 145 146 printf("Kernel: 0x%08x - 0x%08x (%u KiB)\n", 147 kernel_addr, kernel_addr + hdr->kernel_size, 148 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_v34 */ 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 ulong start, end; 239 240 if (!hdr->ramdisk_size) { 241 *rd_data = *rd_len = 0; 242 return -1; 243 } 244 245 /* Have been loaded by android_image_load_separate() on ramdisk_addr_r */ 246 ramdisk_addr_r = env_get_ulong("ramdisk_addr_r", 16, 0); 247 if (!ramdisk_addr_r) { 248 printf("No Found Ramdisk Load Address.\n"); 249 return -1; 250 } 251 252 *rd_data = ramdisk_addr_r; 253 *rd_len = hdr->ramdisk_size; 254 if (hdr->header_version >= 3) 255 *rd_len += hdr->vendor_ramdisk_size; 256 if (hdr->header_version >= 4) { 257 *rd_len += hdr->vendor_bootconfig_size + 258 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 259 } 260 261 /* just for print msg */ 262 start = ramdisk_addr_r; 263 if (hdr->header_version >= 3) { 264 end = start + (ulong)hdr->vendor_ramdisk_size; 265 printf("v-ramdisk: 0x%08lx - 0x%08lx (%u KiB)\n", 266 start, end, DIV_ROUND_UP(hdr->vendor_ramdisk_size, 1024)); 267 start = end; 268 } 269 { 270 end = start + (ulong)hdr->ramdisk_size; 271 printf("ramdisk: 0x%08lx - 0x%08lx (%u KiB)\n", 272 start, end, DIV_ROUND_UP(hdr->ramdisk_size, 1024)); 273 start = end; 274 } 275 if (hdr->header_version >= 4) { 276 end = start + (ulong)hdr->vendor_bootconfig_size; 277 printf("bootconfig: 0x%08lx - 0x%08lx (%u KiB)\n", 278 start, end, DIV_ROUND_UP(hdr->vendor_bootconfig_size, 1024)); 279 start = end; 280 end = start + ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 281 printf("bootparams: 0x%08lx - 0x%08lx\n", start, end); 282 } 283 284 return 0; 285 } 286 287 int android_image_get_fdt(const struct andr_img_hdr *hdr, 288 ulong *rd_data) 289 { 290 ulong fdt_addr_r; 291 292 if (!hdr->second_size) { 293 *rd_data = 0; 294 return -1; 295 } 296 297 /* Have been loaded by android_image_load_separate() on fdt_addr_r */ 298 fdt_addr_r = env_get_ulong("fdt_addr_r", 16, 0); 299 if (!fdt_addr_r) { 300 printf("No Found FDT Load Address.\n"); 301 return -1; 302 } 303 304 *rd_data = fdt_addr_r; 305 306 debug("FDT load addr 0x%08x size %u KiB\n", 307 hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024)); 308 309 return 0; 310 } 311 312 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH 313 static void print_hash(const char *label, u8 *hash, int len) 314 { 315 int i; 316 317 printf("%s:\n 0x", label ? : "Hash"); 318 for (i = 0; i < len; i++) 319 printf("%02x", hash[i]); 320 printf("\n"); 321 } 322 #endif 323 324 typedef enum { 325 IMG_KERNEL, 326 IMG_RAMDISK, /* within boot.img or init_boot.img(Android-13 or later) */ 327 IMG_SECOND, 328 IMG_RECOVERY_DTBO, 329 IMG_RK_DTB, /* within resource.img in second position */ 330 IMG_DTB, 331 IMG_VENDOR_RAMDISK, 332 IMG_BOOTCONFIG, 333 IMG_MAX, 334 } img_t; 335 336 #if defined(CONFIG_ANDROID_BOOT_IMAGE_HASH) && !defined(CONFIG_DM_CRYPTO) 337 static sha1_context sha1_ctx; 338 #endif 339 340 static int image_load(img_t img, struct andr_img_hdr *hdr, 341 ulong blkstart, void *ram_base, 342 struct udevice *crypto) 343 { 344 struct blk_desc *desc = rockchip_get_bootdev(); 345 disk_partition_t part_vendor_boot; 346 disk_partition_t part_init_boot; 347 __maybe_unused u32 typesz; 348 u32 andr_version = (hdr->os_version >> 25) & 0x7f; 349 ulong pgsz = hdr->page_size; 350 ulong blksz = desc->blksz; 351 ulong blkcnt, blkoff; 352 ulong memmove_dst = 0; 353 ulong bsoffs = 0; 354 ulong extra = 0; 355 ulong length; 356 void *buffer; 357 void *tmp = NULL; 358 int ret = 0; 359 360 switch (img) { 361 case IMG_KERNEL: 362 bsoffs = 0; /* include a page_size(image header) */ 363 length = hdr->kernel_size + pgsz; 364 buffer = (void *)env_get_ulong("android_addr_r", 16, 0); 365 blkcnt = DIV_ROUND_UP(hdr->kernel_size + pgsz, blksz); 366 typesz = sizeof(hdr->kernel_size); 367 if (!sysmem_alloc_base(MEM_KERNEL, 368 (phys_addr_t)buffer, blkcnt * blksz)) 369 return -ENOMEM; 370 break; 371 case IMG_VENDOR_RAMDISK: 372 if (hdr->vendor_boot_buf) { 373 ram_base = hdr->vendor_boot_buf; 374 } else { 375 if (part_get_info_by_name(desc, 376 ANDROID_PARTITION_VENDOR_BOOT, 377 &part_vendor_boot) < 0) { 378 printf("No vendor boot partition\n"); 379 return -ENOENT; 380 } 381 ram_base = 0; 382 } 383 384 blkstart = part_vendor_boot.start; 385 pgsz = hdr->vendor_page_size; 386 bsoffs = ALIGN(VENDOR_BOOT_HDRv3_SIZE, pgsz); 387 length = hdr->vendor_ramdisk_size; 388 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 389 blkcnt = DIV_ROUND_UP(hdr->vendor_ramdisk_size, blksz); 390 typesz = sizeof(hdr->vendor_ramdisk_size); 391 /* 392 * Add extra memory for generic ramdisk space. 393 * 394 * In case of unaligned vendor ramdisk size, reserve 395 * 1 more blksz. 396 * 397 * Reserve 8KB for bootloader cmdline. 398 */ 399 if (hdr->header_version >= 3) 400 extra += ALIGN(hdr->ramdisk_size, blksz) + blksz; 401 if (hdr->header_version >= 4) 402 extra += ALIGN(hdr->vendor_bootconfig_size, blksz) + 403 ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE; 404 if (length && !sysmem_alloc_base(MEM_RAMDISK, 405 (phys_addr_t)buffer, blkcnt * blksz + extra)) 406 return -ENOMEM; 407 break; 408 case IMG_RAMDISK: 409 /* get ramdisk from init_boot.img ? */ 410 if (hdr->header_version >= 4 && andr_version >= 13) { 411 if (hdr->init_boot_buf) { 412 ram_base = hdr->init_boot_buf; 413 } else { 414 if (part_get_info_by_name(desc, 415 ANDROID_PARTITION_INIT_BOOT, &part_init_boot) < 0) { 416 printf("No init boot partition\n"); 417 return -ENOENT; 418 } 419 blkstart = part_init_boot.start; 420 ram_base = 0; 421 } 422 bsoffs = pgsz; 423 } else { 424 /* get ramdisk from generic boot.img */ 425 bsoffs = pgsz + ALIGN(hdr->kernel_size, pgsz); 426 } 427 428 length = hdr->ramdisk_size; 429 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 430 blkcnt = DIV_ROUND_UP(hdr->ramdisk_size, blksz); 431 typesz = sizeof(hdr->ramdisk_size); 432 433 /* 434 * ramdisk_addr_r v012: 435 * |----------------| 436 * | ramdisk | 437 * |----------------| 438 * 439 * ramdisk_addr_r v3 (Android-11 and later): 440 * |----------------|---------| 441 * | vendor-ramdisk | ramdisk | 442 * |----------------|---------| 443 * 444 * ramdisk_addr_r v4 (Android-12 and later): 445 * |----------------|---------|------------|------------| 446 * | vendor-ramdisk | ramdisk | bootconfig | bootparams | 447 * |----------------|---------|------------|------------| 448 * 449 * ramdisk_addr_r v4 + init_boot(Android-13 and later): 450 * |----------------|----------------|------------|------------| 451 * | vendor-ramdisk | (init_)ramdisk | bootconfig | bootparams | 452 * |----------------|----------------|------------|------------| 453 */ 454 if (hdr->header_version >= 3) { 455 buffer += hdr->vendor_ramdisk_size; 456 if (!IS_ALIGNED((ulong)buffer, blksz)) { 457 memmove_dst = (ulong)buffer; 458 buffer = (void *)ALIGN(memmove_dst, blksz); 459 } 460 } 461 /* sysmem has been alloced by vendor ramdisk */ 462 if (hdr->header_version < 3) { 463 if (length && !sysmem_alloc_base(MEM_RAMDISK, 464 (phys_addr_t)buffer, blkcnt * blksz)) 465 return -ENOMEM; 466 } 467 break; 468 case IMG_BOOTCONFIG: 469 if (hdr->header_version < 4) 470 return 0; 471 472 if (hdr->vendor_boot_buf) { 473 ram_base = hdr->vendor_boot_buf; 474 } else { 475 if (part_get_info_by_name(desc, 476 ANDROID_PARTITION_VENDOR_BOOT, 477 &part_vendor_boot) < 0) { 478 printf("No vendor boot partition\n"); 479 return -ENOENT; 480 } 481 ram_base = 0; 482 } 483 blkstart = part_vendor_boot.start; 484 pgsz = hdr->vendor_page_size; 485 bsoffs = ALIGN(VENDOR_BOOT_HDRv4_SIZE, pgsz) + 486 ALIGN(hdr->vendor_ramdisk_size, pgsz) + 487 ALIGN(hdr->dtb_size, pgsz) + 488 ALIGN(hdr->vendor_ramdisk_table_size, pgsz); 489 length = hdr->vendor_bootconfig_size; 490 buffer = (void *)env_get_ulong("ramdisk_addr_r", 16, 0); 491 blkcnt = DIV_ROUND_UP(hdr->vendor_bootconfig_size, blksz); 492 typesz = sizeof(hdr->vendor_bootconfig_size); 493 494 buffer += hdr->vendor_ramdisk_size + hdr->ramdisk_size; 495 if (!IS_ALIGNED((ulong)buffer, blksz)) { 496 memmove_dst = (ulong)buffer; 497 buffer = (void *)ALIGN(memmove_dst, blksz); 498 } 499 break; 500 case IMG_SECOND: 501 bsoffs = pgsz + 502 ALIGN(hdr->kernel_size, pgsz) + 503 ALIGN(hdr->ramdisk_size, pgsz); 504 length = hdr->second_size; 505 blkcnt = DIV_ROUND_UP(hdr->second_size, blksz); 506 buffer = tmp = malloc(blkcnt * blksz); 507 typesz = sizeof(hdr->second_size); 508 break; 509 case IMG_RECOVERY_DTBO: 510 bsoffs = pgsz + 511 ALIGN(hdr->kernel_size, pgsz) + 512 ALIGN(hdr->ramdisk_size, pgsz) + 513 ALIGN(hdr->second_size, pgsz); 514 length = hdr->recovery_dtbo_size; 515 blkcnt = DIV_ROUND_UP(hdr->recovery_dtbo_size, blksz); 516 buffer = tmp = malloc(blkcnt * blksz); 517 typesz = sizeof(hdr->recovery_dtbo_size); 518 break; 519 case IMG_DTB: 520 bsoffs = pgsz + 521 ALIGN(hdr->kernel_size, pgsz) + 522 ALIGN(hdr->ramdisk_size, pgsz) + 523 ALIGN(hdr->second_size, pgsz) + 524 ALIGN(hdr->recovery_dtbo_size, pgsz); 525 length = hdr->dtb_size; 526 blkcnt = DIV_ROUND_UP(hdr->dtb_size, blksz); 527 buffer = tmp = malloc(blkcnt * blksz); 528 typesz = sizeof(hdr->dtb_size); 529 break; 530 case IMG_RK_DTB: 531 #ifdef CONFIG_RKIMG_BOOTLOADER 532 /* No going further, it handles DTBO, HW-ID, etc */ 533 buffer = (void *)env_get_ulong("fdt_addr_r", 16, 0); 534 if (gd->fdt_blob != (void *)buffer) 535 ret = rockchip_read_dtb_file(buffer); 536 #endif 537 return ret < 0 ? ret : 0; 538 default: 539 return -EINVAL; 540 } 541 542 if (!buffer) { 543 printf("No memory for image(%d)\n", img); 544 return -ENOMEM; 545 } 546 547 if (!blksz || !length) 548 goto crypto_calc; 549 550 /* load */ 551 if (ram_base) { 552 memcpy(buffer, (char *)((ulong)ram_base + bsoffs), length); 553 } else { 554 blkoff = DIV_ROUND_UP(bsoffs, blksz); 555 ret = blk_dread(desc, blkstart + blkoff, blkcnt, buffer); 556 if (ret != blkcnt) { 557 printf("Failed to read img(%d), ret=%d\n", img, ret); 558 return -EIO; 559 } 560 } 561 562 if (memmove_dst) 563 memmove((char *)memmove_dst, buffer, length); 564 565 crypto_calc: 566 if (img == IMG_KERNEL) { 567 buffer += pgsz; 568 length -= pgsz; 569 } 570 571 /* sha1 */ 572 if (hdr->header_version < 3) { 573 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH 574 #ifdef CONFIG_DM_CRYPTO 575 crypto_sha_update(crypto, (u32 *)buffer, length); 576 crypto_sha_update(crypto, (u32 *)&length, typesz); 577 #else 578 sha1_update(&sha1_ctx, (void *)buffer, length); 579 sha1_update(&sha1_ctx, (void *)&length, typesz); 580 #endif 581 #endif 582 } 583 584 if (tmp) 585 free(tmp); 586 587 return 0; 588 } 589 590 static int images_load_verify(struct andr_img_hdr *hdr, ulong part_start, 591 void *ram_base, struct udevice *crypto) 592 { 593 /* load, never change order ! */ 594 if (image_load(IMG_KERNEL, hdr, part_start, ram_base, crypto)) 595 return -1; 596 if (image_load(IMG_RAMDISK, hdr, part_start, ram_base, crypto)) 597 return -1; 598 if (image_load(IMG_SECOND, hdr, part_start, ram_base, crypto)) 599 return -1; 600 if (hdr->header_version > 0) { 601 if (image_load(IMG_RECOVERY_DTBO, hdr, part_start, 602 ram_base, crypto)) 603 return -1; 604 } 605 if (hdr->header_version > 1) { 606 if (image_load(IMG_DTB, hdr, part_start, ram_base, crypto)) 607 return -1; 608 } 609 610 return 0; 611 } 612 613 /* 614 * @ram_base: !NULL means require memcpy for an exist full android image. 615 */ 616 static int android_image_separate(struct andr_img_hdr *hdr, 617 const disk_partition_t *part, 618 void *load_address, 619 void *ram_base) 620 { 621 ulong bstart; 622 int ret; 623 624 if (android_image_check_header(hdr)) { 625 printf("Bad android image header\n"); 626 return -EINVAL; 627 } 628 629 /* set for image_load(IMG_KERNEL, ...) */ 630 env_set_hex("android_addr_r", (ulong)load_address); 631 bstart = part ? part->start : 0; 632 633 /* 634 * 1. Load images to their individual target ram position 635 * in order to disable fdt/ramdisk relocation. 636 */ 637 638 /* load rk-kernel.dtb alone */ 639 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 640 return -1; 641 642 #ifdef CONFIG_ANDROID_BOOT_IMAGE_HASH 643 if (hdr->header_version < 3) { 644 struct udevice *dev = NULL; 645 uchar hash[20]; 646 #ifdef CONFIG_DM_CRYPTO 647 sha_context ctx; 648 649 ctx.length = 0; 650 ctx.algo = CRYPTO_SHA1; 651 dev = crypto_get_device(ctx.algo); 652 if (!dev) { 653 printf("Can't find crypto device for SHA1\n"); 654 return -ENODEV; 655 } 656 657 /* v1 & v2: requires total length before sha init */ 658 ctx.length += hdr->kernel_size + sizeof(hdr->kernel_size) + 659 hdr->ramdisk_size + sizeof(hdr->ramdisk_size) + 660 hdr->second_size + sizeof(hdr->second_size); 661 if (hdr->header_version > 0) 662 ctx.length += hdr->recovery_dtbo_size + 663 sizeof(hdr->recovery_dtbo_size); 664 if (hdr->header_version > 1) 665 ctx.length += hdr->dtb_size + sizeof(hdr->dtb_size); 666 crypto_sha_init(dev, &ctx); 667 #else 668 sha1_starts(&sha1_ctx); 669 #endif 670 ret = images_load_verify(hdr, bstart, ram_base, dev); 671 if (ret) 672 return ret; 673 674 #ifdef CONFIG_DM_CRYPTO 675 crypto_sha_final(dev, &ctx, hash); 676 #else 677 sha1_finish(&sha1_ctx, hash); 678 #endif 679 if (memcmp(hash, hdr->id, 20)) { 680 print_hash("Hash from header", (u8 *)hdr->id, 20); 681 print_hash("Hash real", (u8 *)hash, 20); 682 return -EBADFD; 683 } else { 684 printf("ANDROID: Hash OK\n"); 685 } 686 } else 687 #endif 688 { 689 ret = images_load_verify(hdr, bstart, ram_base, NULL); 690 if (ret) 691 return ret; 692 } 693 694 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 695 env_set("bootm-no-reloc", "y"); 696 697 return 0; 698 } 699 700 static int android_image_separate_v34(struct andr_img_hdr *hdr, 701 const disk_partition_t *part, 702 void *load_address, void *ram_base) 703 { 704 ulong bstart; 705 706 if (android_image_check_header(hdr)) { 707 printf("Bad android image header\n"); 708 return -EINVAL; 709 } 710 711 /* set for image_load(IMG_KERNEL, ...) */ 712 env_set_hex("android_addr_r", (ulong)load_address); 713 bstart = part ? part->start : 0; 714 715 /* 716 * 1. Load images to their individual target ram position 717 * in order to disable fdt/ramdisk relocation. 718 */ 719 if (image_load(IMG_RK_DTB, hdr, bstart, ram_base, NULL)) 720 return -1; 721 if (image_load(IMG_KERNEL, hdr, bstart, ram_base, NULL)) 722 return -1; 723 if (image_load(IMG_VENDOR_RAMDISK, hdr, bstart, ram_base, NULL)) 724 return -1; 725 if (image_load(IMG_RAMDISK, hdr, bstart, ram_base, NULL)) 726 return -1; 727 if (image_load(IMG_BOOTCONFIG, hdr, bstart, ram_base, NULL)) 728 return -1; 729 /* 730 * Copy the populated hdr to load address after image_load(IMG_KERNEL) 731 * 732 * The image_load(IMG_KERNEL) only reads boot_img_hdr_v34 while 733 * vendor_boot_img_hdr_v34 is not included, so fix it here. 734 */ 735 memcpy((char *)load_address, hdr, hdr->page_size); 736 737 /* 2. Disable fdt/ramdisk relocation, it saves boot time */ 738 env_set("bootm-no-reloc", "y"); 739 740 return 0; 741 } 742 743 static ulong android_image_get_comp_addr(struct andr_img_hdr *hdr, int comp) 744 { 745 ulong kernel_addr_c; 746 ulong load_addr = 0; 747 748 kernel_addr_c = env_get_ulong("kernel_addr_c", 16, 0); 749 750 #ifdef CONFIG_ARM64 751 /* 752 * On 64-bit kernel, assuming use IMAGE by default. 753 * 754 * kernel_addr_c is for LZ4-IMAGE but maybe not defined. 755 * kernel_addr_r is for IMAGE. 756 */ 757 if (comp != IH_COMP_NONE) { 758 ulong comp_addr; 759 760 if (kernel_addr_c) { 761 comp_addr = kernel_addr_c; 762 } else { 763 printf("Warn: No \"kernel_addr_c\"\n"); 764 comp_addr = CONFIG_SYS_SDRAM_BASE + 0x2000000;/* 32M */ 765 env_set_hex("kernel_addr_c", comp_addr); 766 } 767 768 load_addr = comp_addr - hdr->page_size; 769 } 770 #else 771 /* 772 * On 32-bit kernel: 773 * 774 * The input load_addr is from env value: "kernel_addr_r", it has 775 * different role depends on whether kernel_addr_c is defined: 776 * 777 * - kernel_addr_r is for lz4/zImage if kernel_addr_c if [not] defined. 778 * - kernel_addr_r is for IMAGE if kernel_addr_c is defined. 779 */ 780 if (comp == IH_COMP_NONE) { 781 if (kernel_addr_c) { 782 /* input load_addr is for Image, nothing to do */ 783 } else { 784 /* input load_addr is for lz4/zImage, set default addr for Image */ 785 load_addr = CONFIG_SYS_SDRAM_BASE + 0x8000; 786 env_set_hex("kernel_addr_r", load_addr); 787 788 load_addr -= hdr->page_size; 789 } 790 } else { 791 if (kernel_addr_c) { 792 /* input load_addr is for Image, so use another for lz4/zImage */ 793 load_addr = kernel_addr_c - hdr->page_size; 794 } else { 795 /* input load_addr is for lz4/zImage, nothing to do */ 796 } 797 } 798 #endif 799 800 return load_addr; 801 } 802 803 void android_image_set_decomp(struct andr_img_hdr *hdr, int comp) 804 { 805 ulong kernel_addr_r; 806 807 env_set_ulong("os_comp", comp); 808 809 /* zImage handles decompress itself */ 810 if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) { 811 kernel_addr_r = env_get_ulong("kernel_addr_r", 16, 0x02080000); 812 android_image_set_kload(hdr, kernel_addr_r); 813 android_image_set_comp(hdr, comp); 814 } else { 815 android_image_set_comp(hdr, IH_COMP_NONE); 816 } 817 } 818 819 static int android_image_load_separate(struct andr_img_hdr *hdr, 820 const disk_partition_t *part, 821 void *load_addr) 822 { 823 if (hdr->header_version < 3) 824 return android_image_separate(hdr, part, load_addr, NULL); 825 else 826 return android_image_separate_v34(hdr, part, load_addr, NULL); 827 } 828 829 int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_addr) 830 { 831 ulong comp_addr; 832 int comp; 833 834 comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size); 835 comp_addr = android_image_get_comp_addr(hdr, comp); 836 837 /* non-compressed image: already in-place */ 838 if ((ulong)hdr == *load_addr) 839 return 0; 840 841 /* compressed image */ 842 if (comp_addr) { 843 *load_addr = comp_addr; 844 if ((ulong)hdr == comp_addr) /* already in-place */ 845 return 0; 846 } 847 848 /* 849 * The most possible reason to arrive here is: 850 * 851 * VBoot=1 and AVB load full partition to a temp memory buffer, now we 852 * separate(memcpy) subimages from boot.img to where they should be. 853 */ 854 if (hdr->header_version < 3) { 855 if (android_image_separate(hdr, NULL, (void *)(*load_addr), hdr)) 856 return -1; 857 } else { 858 if (android_image_separate_v34(hdr, NULL, (void *)(*load_addr), hdr)) 859 return -1; 860 } 861 862 android_image_set_decomp((void *)(*load_addr), comp); 863 864 return 0; 865 } 866 867 long android_image_load(struct blk_desc *dev_desc, 868 const disk_partition_t *part_info, 869 unsigned long load_address, 870 unsigned long max_size) { 871 struct andr_img_hdr *hdr; 872 ulong comp_addr; 873 int comp, ret; 874 int blk_off; 875 876 if (max_size < part_info->blksz) 877 return -1; 878 879 hdr = populate_andr_img_hdr(dev_desc, (disk_partition_t *)part_info); 880 if (!hdr) { 881 printf("No valid android hdr\n"); 882 return -1; 883 } 884 885 /* 886 * create the layout: 887 * 888 * |<- page_size ->|1-blk | 889 * |-----|---------|------|-----| 890 * | hdr | ... | kernel | 891 * |-----|----- ---|------------| 892 * 893 * Alloc page_size and 1 more blk for reading kernel image to 894 * get it's compression type, then fill the android hdr what 895 * we have populated before. 896 * 897 * Why? see: android_image_get_kernel_addr(). 898 */ 899 blk_off = BLK_CNT(hdr->page_size, dev_desc->blksz); 900 hdr = (struct andr_img_hdr *) 901 realloc(hdr, (blk_off + 1) * dev_desc->blksz); 902 if (!hdr) 903 return -1; 904 905 if (blk_dread(dev_desc, part_info->start + blk_off, 1, 906 (char *)hdr + hdr->page_size) != 1) { 907 free(hdr); 908 return -1; 909 } 910 911 /* Changed to compressed address ? */ 912 comp = bootm_parse_comp((void *)(ulong)hdr + hdr->page_size); 913 comp_addr = android_image_get_comp_addr(hdr, comp); 914 if (comp_addr) 915 load_address = comp_addr; 916 else 917 load_address -= hdr->page_size; 918 919 ret = android_image_load_separate(hdr, part_info, (void *)load_address); 920 if (ret) { 921 printf("Failed to load android image\n"); 922 goto fail; 923 } 924 android_image_set_decomp((void *)load_address, comp); 925 926 debug("Loading Android Image to 0x%08lx\n", load_address); 927 928 free(hdr); 929 return load_address; 930 931 fail: 932 free(hdr); 933 return -1; 934 } 935 936 static struct andr_img_hdr * 937 extract_boot_image_v012_header(struct blk_desc *dev_desc, 938 const disk_partition_t *boot_img) 939 { 940 struct andr_img_hdr *hdr; 941 long blk_cnt, blks_read; 942 943 blk_cnt = BLK_CNT(sizeof(struct andr_img_hdr), dev_desc->blksz); 944 hdr = (struct andr_img_hdr *)malloc(blk_cnt * dev_desc->blksz); 945 946 if (!blk_cnt || !hdr) 947 return NULL; 948 949 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, hdr); 950 if (blks_read != blk_cnt) { 951 debug("boot img header blk cnt is %ld and blks read is %ld\n", 952 blk_cnt, blks_read); 953 return NULL; 954 } 955 956 if (android_image_check_header((void *)hdr)) { 957 printf("boot header magic is invalid.\n"); 958 return NULL; 959 } 960 961 if (hdr->page_size < sizeof(*hdr)) { 962 printf("android hdr is over size\n"); 963 return NULL; 964 } 965 966 return hdr; 967 } 968 969 static struct boot_img_hdr_v34 * 970 extract_boot_image_v34_header(struct blk_desc *dev_desc, 971 const disk_partition_t *boot_img) 972 { 973 struct boot_img_hdr_v34 *boot_hdr; 974 disk_partition_t part; 975 long blk_cnt, blks_read; 976 977 blk_cnt = BLK_CNT(sizeof(struct boot_img_hdr_v34), dev_desc->blksz); 978 boot_hdr = (struct boot_img_hdr_v34 *)malloc(blk_cnt * dev_desc->blksz); 979 980 if (!blk_cnt || !boot_hdr) 981 return NULL; 982 983 blks_read = blk_dread(dev_desc, boot_img->start, blk_cnt, boot_hdr); 984 if (blks_read != blk_cnt) { 985 debug("boot img header blk cnt is %ld and blks read is %ld\n", 986 blk_cnt, blks_read); 987 return NULL; 988 } 989 990 if (android_image_check_header((void *)boot_hdr)) { 991 printf("boot header magic is invalid.\n"); 992 return NULL; 993 } 994 995 if (boot_hdr->header_version < 3) { 996 printf("boot header %d, is not >= v3.\n", 997 boot_hdr->header_version); 998 return NULL; 999 } 1000 1001 /* Start from android-13 GKI, it doesn't assign 'os_version' */ 1002 if (boot_hdr->header_version >= 4 && boot_hdr->os_version == 0) { 1003 if (part_get_info_by_name(dev_desc, 1004 ANDROID_PARTITION_INIT_BOOT, &part) > 0) 1005 boot_hdr->os_version = 13 << 25; 1006 } 1007 1008 return boot_hdr; 1009 } 1010 1011 static struct vendor_boot_img_hdr_v34 * 1012 extract_vendor_boot_image_v34_header(struct blk_desc *dev_desc, 1013 const disk_partition_t *part_vendor_boot) 1014 { 1015 struct vendor_boot_img_hdr_v34 *vboot_hdr; 1016 long blk_cnt, blks_read; 1017 1018 blk_cnt = BLK_CNT(sizeof(struct vendor_boot_img_hdr_v34), 1019 part_vendor_boot->blksz); 1020 vboot_hdr = (struct vendor_boot_img_hdr_v34 *) 1021 malloc(blk_cnt * part_vendor_boot->blksz); 1022 1023 if (!blk_cnt || !vboot_hdr) 1024 return NULL; 1025 1026 blks_read = blk_dread(dev_desc, part_vendor_boot->start, 1027 blk_cnt, vboot_hdr); 1028 if (blks_read != blk_cnt) { 1029 debug("vboot img header blk cnt is %ld and blks read is %ld\n", 1030 blk_cnt, blks_read); 1031 return NULL; 1032 } 1033 1034 if (strncmp(VENDOR_BOOT_MAGIC, (void *)vboot_hdr->magic, 1035 VENDOR_BOOT_MAGIC_SIZE)) { 1036 printf("vendor boot header is invalid.\n"); 1037 return NULL; 1038 } 1039 1040 if (vboot_hdr->header_version < 3) { 1041 printf("vendor boot header %d, is not >= v3.\n", 1042 vboot_hdr->header_version); 1043 return NULL; 1044 } 1045 1046 return vboot_hdr; 1047 } 1048 1049 int populate_boot_info(const struct boot_img_hdr_v34 *boot_hdr, 1050 const struct vendor_boot_img_hdr_v34 *vendor_boot_hdr, 1051 const struct boot_img_hdr_v34 *init_boot_hdr, 1052 struct andr_img_hdr *hdr, bool save_hdr) 1053 { 1054 memset(hdr->magic, 0, ANDR_BOOT_MAGIC_SIZE); 1055 memcpy(hdr->magic, boot_hdr->magic, ANDR_BOOT_MAGIC_SIZE); 1056 1057 hdr->kernel_size = boot_hdr->kernel_size; 1058 /* don't use vendor_boot_hdr->kernel_addr, we prefer "hdr + hdr->page_size" */ 1059 hdr->kernel_addr = ANDROID_IMAGE_DEFAULT_KERNEL_ADDR; 1060 1061 /* 1062 * generic ramdisk: immediately following the vendor ramdisk. 1063 * It can be from init_boot.img or boot.img. 1064 */ 1065 if (init_boot_hdr) 1066 hdr->ramdisk_size = init_boot_hdr->ramdisk_size; 1067 else 1068 hdr->ramdisk_size = boot_hdr->ramdisk_size; 1069 1070 /* actually, useless */ 1071 hdr->ramdisk_addr = env_get_ulong("ramdisk_addr_r", 16, 0); 1072 1073 /* removed in v3 */ 1074 hdr->second_size = 0; 1075 hdr->second_addr = 0; 1076 1077 hdr->tags_addr = vendor_boot_hdr->tags_addr; 1078 1079 /* fixed in v3 */ 1080 hdr->page_size = 4096; 1081 hdr->header_version = boot_hdr->header_version; 1082 hdr->os_version = boot_hdr->os_version; 1083 1084 memset(hdr->name, 0, ANDR_BOOT_NAME_SIZE); 1085 strncpy(hdr->name, (const char *)vendor_boot_hdr->name, ANDR_BOOT_NAME_SIZE); 1086 1087 /* removed in v3 */ 1088 memset(hdr->cmdline, 0, ANDR_BOOT_ARGS_SIZE); 1089 memset(hdr->id, 0, 32); 1090 memset(hdr->extra_cmdline, 0, ANDR_BOOT_EXTRA_ARGS_SIZE); 1091 hdr->recovery_dtbo_size = 0; 1092 hdr->recovery_dtbo_offset = 0; 1093 1094 hdr->header_size = boot_hdr->header_size; 1095 hdr->dtb_size = vendor_boot_hdr->dtb_size; 1096 hdr->dtb_addr = vendor_boot_hdr->dtb_addr; 1097 1098 /* boot_img_hdr_v34 fields */ 1099 hdr->vendor_ramdisk_size = vendor_boot_hdr->vendor_ramdisk_size; 1100 hdr->vendor_page_size = vendor_boot_hdr->page_size; 1101 hdr->vendor_header_version = vendor_boot_hdr->header_version; 1102 hdr->vendor_header_size = vendor_boot_hdr->header_size; 1103 1104 hdr->total_cmdline = calloc(1, TOTAL_BOOT_ARGS_SIZE); 1105 if (!hdr->total_cmdline) 1106 return -ENOMEM; 1107 strncpy(hdr->total_cmdline, (const char *)boot_hdr->cmdline, 1108 sizeof(boot_hdr->cmdline)); 1109 strncat(hdr->total_cmdline, " ", 1); 1110 strncat(hdr->total_cmdline, (const char *)vendor_boot_hdr->cmdline, 1111 sizeof(vendor_boot_hdr->cmdline)); 1112 1113 /* new for header v4 */ 1114 if (vendor_boot_hdr->header_version >= 4) { 1115 hdr->vendor_ramdisk_table_size = 1116 vendor_boot_hdr->vendor_ramdisk_table_size; 1117 hdr->vendor_ramdisk_table_entry_num = 1118 vendor_boot_hdr->vendor_ramdisk_table_entry_num; 1119 hdr->vendor_ramdisk_table_entry_size = 1120 vendor_boot_hdr->vendor_ramdisk_table_entry_size; 1121 /* 1122 * If we place additional "androidboot.xxx" parameters after 1123 * bootconfig, this field value should be increased, 1124 * but not over than ANDROID_ADDITION_BOOTCONFIG_PARAMS_MAX_SIZE. 1125 */ 1126 hdr->vendor_bootconfig_size = 1127 vendor_boot_hdr->vendor_bootconfig_size; 1128 } else { 1129 hdr->vendor_ramdisk_table_size = 0; 1130 hdr->vendor_ramdisk_table_entry_num = 0; 1131 hdr->vendor_ramdisk_table_entry_size = 0; 1132 hdr->vendor_bootconfig_size = 0; 1133 } 1134 1135 hdr->init_boot_buf = save_hdr ? (void *)init_boot_hdr : 0; 1136 hdr->vendor_boot_buf = save_hdr ? (void *)vendor_boot_hdr : 0; 1137 1138 if (hdr->page_size < sizeof(*hdr)) { 1139 printf("android hdr is over size\n"); 1140 return -EINVAL; 1141 } 1142 1143 return 0; 1144 } 1145 1146 /* 1147 * The possible cases of boot.img + recovery.img: 1148 * 1149 * [N]: 0, 1, 2 1150 * [M]: 0, 1, 2, 3, 4 1151 * 1152 * |--------------------|---------------------| 1153 * | boot.img | recovery.img | 1154 * |--------------------|---------------------| 1155 * | boot_img_hdr_v[N] | boot_img_hdr_v[N] | <= if A/B is not required 1156 * |--------------------|---------------------| 1157 * | boot_img_hdr_v34 | boot_img_hdr_v2 | <= if A/B is not required 1158 * |------------------------------------------| 1159 * | boot_img_hdr_v[M], no recovery.img | <= if A/B is required 1160 * |------------------------------------------| 1161 */ 1162 struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc, 1163 disk_partition_t *part_boot) 1164 { 1165 disk_partition_t part_vendor_boot; 1166 disk_partition_t part_init_boot; 1167 struct vendor_boot_img_hdr_v34 *vboot_hdr = NULL; 1168 struct boot_img_hdr_v34 *iboot_hdr = NULL; 1169 struct boot_img_hdr_v34 *boot_hdr = NULL; 1170 struct andr_img_hdr *andr_hdr = NULL; 1171 int header_version; 1172 int andr_version; 1173 1174 if (!dev_desc || !part_boot) 1175 return NULL; 1176 1177 andr_hdr = (struct andr_img_hdr *)malloc(1 * dev_desc->blksz); 1178 if (!andr_hdr) 1179 return NULL; 1180 1181 if (blk_dread(dev_desc, part_boot->start, 1, andr_hdr) != 1) { 1182 free(andr_hdr); 1183 return NULL; 1184 } 1185 1186 if (android_image_check_header(andr_hdr)) { 1187 free(andr_hdr); 1188 return NULL; 1189 } 1190 1191 header_version = andr_hdr->header_version; 1192 free(andr_hdr); 1193 andr_hdr = NULL; 1194 1195 if (header_version < 3) { 1196 return extract_boot_image_v012_header(dev_desc, part_boot); 1197 } else { 1198 if (part_get_info_by_name(dev_desc, 1199 ANDROID_PARTITION_VENDOR_BOOT, 1200 &part_vendor_boot) < 0) { 1201 printf("No vendor boot partition\n"); 1202 return NULL; 1203 } 1204 boot_hdr = extract_boot_image_v34_header(dev_desc, part_boot); 1205 vboot_hdr = extract_vendor_boot_image_v34_header(dev_desc, 1206 &part_vendor_boot); 1207 if (!boot_hdr || !vboot_hdr) 1208 goto image_load_exit; 1209 1210 andr_version = (boot_hdr->os_version >> 25) & 0x7f; 1211 if (header_version >= 4 && andr_version >= 13) { 1212 if (part_get_info_by_name(dev_desc, 1213 ANDROID_PARTITION_INIT_BOOT, 1214 &part_init_boot) < 0) { 1215 printf("No init boot partition\n"); 1216 return NULL; 1217 } 1218 iboot_hdr = extract_boot_image_v34_header(dev_desc, &part_init_boot); 1219 if (!iboot_hdr) 1220 goto image_load_exit; 1221 if (!iboot_hdr->ramdisk_size) { 1222 printf("No ramdisk in init boot partition\n"); 1223 goto image_load_exit; 1224 } 1225 } 1226 1227 andr_hdr = (struct andr_img_hdr *) 1228 malloc(sizeof(struct andr_img_hdr)); 1229 if (!andr_hdr) { 1230 printf("No memory for andr hdr\n"); 1231 goto image_load_exit; 1232 } 1233 1234 if (populate_boot_info(boot_hdr, vboot_hdr, 1235 iboot_hdr, andr_hdr, false)) { 1236 printf("populate boot info failed\n"); 1237 goto image_load_exit; 1238 } 1239 1240 image_load_exit: 1241 if (boot_hdr) 1242 free(boot_hdr); 1243 if (iboot_hdr) 1244 free(iboot_hdr); 1245 if (vboot_hdr) 1246 free(vboot_hdr); 1247 1248 return andr_hdr; 1249 } 1250 1251 return NULL; 1252 } 1253 1254 #if !defined(CONFIG_SPL_BUILD) 1255 /** 1256 * android_print_contents - prints out the contents of the Android format image 1257 * @hdr: pointer to the Android format image header 1258 * 1259 * android_print_contents() formats a multi line Android image contents 1260 * description. 1261 * The routine prints out Android image properties 1262 * 1263 * returns: 1264 * no returned results 1265 */ 1266 void android_print_contents(const struct andr_img_hdr *hdr) 1267 { 1268 const char * const p = IMAGE_INDENT_STRING; 1269 /* os_version = ver << 11 | lvl */ 1270 u32 os_ver = hdr->os_version >> 11; 1271 u32 os_lvl = hdr->os_version & ((1U << 11) - 1); 1272 u32 header_version = hdr->header_version; 1273 1274 printf("%skernel size: %x\n", p, hdr->kernel_size); 1275 printf("%skernel address: %x\n", p, hdr->kernel_addr); 1276 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); 1277 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr); 1278 printf("%ssecond size: %x\n", p, hdr->second_size); 1279 printf("%ssecond address: %x\n", p, hdr->second_addr); 1280 printf("%stags address: %x\n", p, hdr->tags_addr); 1281 printf("%spage size: %x\n", p, hdr->page_size); 1282 printf("%sheader_version: %x\n", p, header_version); 1283 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) 1284 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ 1285 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", 1286 p, hdr->os_version, 1287 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, 1288 (os_lvl >> 4) + 2000, os_lvl & 0x0F); 1289 printf("%sname: %s\n", p, hdr->name); 1290 printf("%scmdline: %s\n", p, hdr->cmdline); 1291 1292 if (header_version == 1 || header_version == 2) { 1293 printf("%srecovery dtbo size: %x\n", p, hdr->recovery_dtbo_size); 1294 printf("%srecovery dtbo offset: %llx\n", p, hdr->recovery_dtbo_offset); 1295 printf("%sheader size: %x\n", p, hdr->header_size); 1296 } 1297 1298 if (header_version == 2 || header_version == 3) { 1299 printf("%sdtb size: %x\n", p, hdr->dtb_size); 1300 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr); 1301 } 1302 1303 if (header_version >= 3) { 1304 printf("%scmdline: %s\n", p, hdr->total_cmdline); 1305 printf("%svendor ramdisk size: %x\n", p, hdr->vendor_ramdisk_size); 1306 printf("%svendor page size: %x\n", p, hdr->vendor_page_size); 1307 printf("%svendor header version: %d\n", p, hdr->vendor_header_version); 1308 printf("%svendor header size: %x\n", p, hdr->vendor_header_size); 1309 } 1310 1311 if (header_version >= 4) { 1312 printf("%svendor ramdisk table size: %x\n", 1313 p, hdr->vendor_ramdisk_table_size); 1314 printf("%svendor ramdisk table entry num: %x\n", 1315 p, hdr->vendor_ramdisk_table_entry_num); 1316 printf("%svendor ramdisk table entry size: %x\n", 1317 p, hdr->vendor_ramdisk_table_entry_size); 1318 printf("%svendor bootconfig size: %d\n", 1319 p, hdr->vendor_bootconfig_size); 1320 } 1321 } 1322 #endif 1323