1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 #include <image-android-dt.h>
10 #include <android_image.h>
13 #include <asm/unaligned.h>
16 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
18 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
20 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
23 * All the Android tools that generate a boot.img use this
24 * address as the default.
26 * Even though it doesn't really make a lot of sense, and it
27 * might be valid on some platforms, we treat that adress as
28 * the default value for this field, and try to execute the
29 * kernel in place in such a case.
31 * Otherwise, we will return the actual value set by the user.
33 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
34 return (ulong)hdr + hdr->page_size;
37 * abootimg creates images where all load addresses are 0
38 * and we need to fix them.
40 if (hdr->kernel_addr == 0 && hdr->ramdisk_addr == 0)
41 return env_get_ulong("kernel_addr_r", 16, 0);
43 return hdr->kernel_addr;
47 * android_image_get_kernel() - processes kernel part of Android boot images
48 * @hdr: Pointer to image header, which is at the start
50 * @verify: Checksum verification flag. Currently unimplemented.
51 * @os_data: Pointer to a ulong variable, will hold os data start
53 * @os_len: Pointer to a ulong variable, will hold os data length.
55 * This function returns the os image's start address and length. Also,
56 * it appends the kernel command line to the bootargs env variable.
58 * Return: Zero, os start address and length on success,
59 * otherwise on failure.
61 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
62 ulong *os_data, ulong *os_len)
64 u32 kernel_addr = android_image_get_kernel_addr(hdr);
65 const struct image_header *ihdr = (const struct image_header *)
66 ((uintptr_t)hdr + hdr->page_size);
69 * Not all Android tools use the id field for signing the image with
70 * sha1 (or anything) so we don't check it. It is not obvious that the
71 * string is null terminated so we take care of this.
73 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
74 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
75 if (strlen(andr_tmp_str))
76 printf("Android's image name: %s\n", andr_tmp_str);
78 printf("Kernel load addr 0x%08x size %u KiB\n",
79 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
83 printf("Kernel command line: %s\n", hdr->cmdline);
84 len += strlen(hdr->cmdline);
87 char *bootargs = env_get("bootargs");
89 len += strlen(bootargs);
91 char *newbootargs = malloc(len + 2);
93 puts("Error: malloc in android_image_get_kernel failed!\n");
99 strcpy(newbootargs, bootargs);
100 strcat(newbootargs, " ");
103 strcat(newbootargs, hdr->cmdline);
105 env_set("bootargs", newbootargs);
108 if (image_get_magic(ihdr) == IH_MAGIC) {
109 *os_data = image_get_data(ihdr);
111 *os_data = (ulong)hdr;
112 *os_data += hdr->page_size;
116 if (image_get_magic(ihdr) == IH_MAGIC)
117 *os_len = image_get_data_size(ihdr);
119 *os_len = hdr->kernel_size;
124 int android_image_check_header(const struct andr_img_hdr *hdr)
126 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
129 ulong android_image_get_end(const struct andr_img_hdr *hdr)
134 * The header takes a full page, the remaining components are aligned
138 end += hdr->page_size;
139 end += ALIGN(hdr->kernel_size, hdr->page_size);
140 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
141 end += ALIGN(hdr->second_size, hdr->page_size);
143 if (hdr->header_version >= 1)
144 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
146 if (hdr->header_version >= 2)
147 end += ALIGN(hdr->dtb_size, hdr->page_size);
152 ulong android_image_get_kload(const struct andr_img_hdr *hdr)
154 return android_image_get_kernel_addr(hdr);
157 ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
159 const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
161 if (image_get_magic((image_header_t *)p) == IH_MAGIC)
162 return image_get_comp((image_header_t *)p);
163 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
169 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
170 ulong *rd_data, ulong *rd_len)
172 if (!hdr->ramdisk_size) {
173 *rd_data = *rd_len = 0;
177 printf("RAM disk load addr 0x%08x size %u KiB\n",
178 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
180 *rd_data = (unsigned long)hdr;
181 *rd_data += hdr->page_size;
182 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
184 *rd_len = hdr->ramdisk_size;
188 int android_image_get_second(const struct andr_img_hdr *hdr,
189 ulong *second_data, ulong *second_len)
191 if (!hdr->second_size) {
192 *second_data = *second_len = 0;
196 *second_data = (unsigned long)hdr;
197 *second_data += hdr->page_size;
198 *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
199 *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
201 printf("second address is 0x%lx\n",*second_data);
203 *second_len = hdr->second_size;
208 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
209 * @hdr_addr: Boot image header address
210 * @addr: If not NULL, will contain address of recovery DTBO image
211 * @size: If not NULL, will contain size of recovery DTBO image
213 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
214 * Boot Image in RAM. The format of this image is Android DTBO (see
215 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
216 * the address is obtained from this function, one can use 'adtimg' U-Boot
217 * command or android_dt_*() functions to extract desired DTBO blob.
219 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
220 * only can be found in recovery image. On A/B devices we can always rely on
221 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
222 * AOSP documentation for details.
224 * Return: true on success or false on error.
226 bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
228 const struct andr_img_hdr *hdr;
232 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
233 if (android_image_check_header(hdr)) {
234 printf("Error: Boot Image header is incorrect\n");
239 if (hdr->header_version < 1) {
240 printf("Error: header_version must be >= 1 to get dtbo\n");
245 if (hdr->recovery_dtbo_size == 0) {
246 printf("Error: recovery_dtbo_size is 0\n");
251 /* Calculate the address of DTB area in boot image */
252 dtbo_img_addr = hdr_addr;
253 dtbo_img_addr += hdr->page_size;
254 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
255 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
256 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
259 *addr = dtbo_img_addr;
261 *size = hdr->recovery_dtbo_size;
269 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
270 * @hdr_addr: Boot image header address
271 * @addr: Will contain the address of DTB area in boot image
273 * Return: true on success or false on fail.
275 static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
277 const struct andr_img_hdr *hdr;
281 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
282 if (android_image_check_header(hdr)) {
283 printf("Error: Boot Image header is incorrect\n");
288 if (hdr->header_version < 2) {
289 printf("Error: header_version must be >= 2 to get dtb\n");
294 if (hdr->dtb_size == 0) {
295 printf("Error: dtb_size is 0\n");
300 /* Calculate the address of DTB area in boot image */
301 dtb_img_addr = hdr_addr;
302 dtb_img_addr += hdr->page_size;
303 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
304 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
305 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
306 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
308 *addr = dtb_img_addr;
316 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
317 * @hdr_addr: Boot image header address
318 * @index: Index of desired DTB in DTB area (starting from 0)
319 * @addr: If not NULL, will contain address to specified DTB
320 * @size: If not NULL, will contain size of specified DTB
322 * Get the address and size of DTB blob by its index in DTB area of Android
325 * Return: true on success or false on error.
327 bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
330 const struct andr_img_hdr *hdr;
332 ulong dtb_img_addr; /* address of DTB part in boot image */
333 u32 dtb_img_size; /* size of DTB payload in boot image */
334 ulong dtb_addr; /* address of DTB blob with specified index */
335 u32 i; /* index iterator */
337 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
341 /* Check if DTB area of boot image is in DTBO format */
342 if (android_dt_check_header(dtb_img_addr)) {
343 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
347 /* Find out the address of DTB with specified index in concat blobs */
348 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
349 dtb_img_size = hdr->dtb_size;
352 dtb_addr = dtb_img_addr;
353 while (dtb_addr < dtb_img_addr + dtb_img_size) {
354 const struct fdt_header *fdt;
357 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
358 if (fdt_check_header(fdt) != 0) {
360 printf("Error: Invalid FDT header for index %u\n", i);
364 dtb_size = fdt_totalsize(fdt);
375 dtb_addr += dtb_size;
379 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
383 #if !defined(CONFIG_SPL_BUILD)
385 * android_print_contents - prints out the contents of the Android format image
386 * @hdr: pointer to the Android format image header
388 * android_print_contents() formats a multi line Android image contents
390 * The routine prints out Android image properties
393 * no returned results
395 void android_print_contents(const struct andr_img_hdr *hdr)
397 const char * const p = IMAGE_INDENT_STRING;
398 /* os_version = ver << 11 | lvl */
399 u32 os_ver = hdr->os_version >> 11;
400 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
402 printf("%skernel size: %x\n", p, hdr->kernel_size);
403 printf("%skernel address: %x\n", p, hdr->kernel_addr);
404 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
405 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
406 printf("%ssecond size: %x\n", p, hdr->second_size);
407 printf("%ssecond address: %x\n", p, hdr->second_addr);
408 printf("%stags address: %x\n", p, hdr->tags_addr);
409 printf("%spage size: %x\n", p, hdr->page_size);
410 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
411 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
412 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
414 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
415 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
416 printf("%sname: %s\n", p, hdr->name);
417 printf("%scmdline: %s\n", p, hdr->cmdline);
418 printf("%sheader_version: %d\n", p, hdr->header_version);
420 if (hdr->header_version >= 1) {
421 printf("%srecovery dtbo size: %x\n", p,
422 hdr->recovery_dtbo_size);
423 printf("%srecovery dtbo offset: %llx\n", p,
424 hdr->recovery_dtbo_offset);
425 printf("%sheader size: %x\n", p,
429 if (hdr->header_version >= 2) {
430 printf("%sdtb size: %x\n", p, hdr->dtb_size);
431 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
436 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
438 * @index: Number of DTB blob in DTB area.
440 * Return: true on success or false on error.
442 static bool android_image_print_dtb_info(const struct fdt_header *fdt,
448 const char *compatible;
450 root_node_off = fdt_path_offset(fdt, "/");
451 if (root_node_off < 0) {
452 printf("Error: Root node not found\n");
456 fdt_size = fdt_totalsize(fdt);
457 compatible = fdt_getprop(fdt, root_node_off, "compatible",
459 model = fdt_getprop(fdt, root_node_off, "model", NULL);
461 printf(" - DTB #%u:\n", index);
462 printf(" (DTB)size = %d\n", fdt_size);
463 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
464 printf(" (DTB)compatible = %s\n",
465 compatible ? compatible : "(unknown)");
471 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
472 * @hdr_addr: Boot image header address
474 * DTB payload in Android Boot Image v2+ can be in one of following formats:
475 * 1. Concatenated DTB blobs
476 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
478 * This function does next:
479 * 1. Prints out the format used in DTB area
480 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
483 * Return: true on success or false on error.
485 bool android_image_print_dtb_contents(ulong hdr_addr)
487 const struct andr_img_hdr *hdr;
489 ulong dtb_img_addr; /* address of DTB part in boot image */
490 u32 dtb_img_size; /* size of DTB payload in boot image */
491 ulong dtb_addr; /* address of DTB blob with specified index */
492 u32 i; /* index iterator */
494 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
498 /* Check if DTB area of boot image is in DTBO format */
499 if (android_dt_check_header(dtb_img_addr)) {
500 printf("## DTB area contents (DTBO format):\n");
501 android_dt_print_contents(dtb_img_addr);
505 printf("## DTB area contents (concat format):\n");
507 /* Iterate over concatenated DTB blobs */
508 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
509 dtb_img_size = hdr->dtb_size;
512 dtb_addr = dtb_img_addr;
513 while (dtb_addr < dtb_img_addr + dtb_img_size) {
514 const struct fdt_header *fdt;
517 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
518 if (fdt_check_header(fdt) != 0) {
520 printf("Error: Invalid FDT header for index %u\n", i);
524 res = android_image_print_dtb_info(fdt, i);
530 dtb_size = fdt_totalsize(fdt);
532 dtb_addr += dtb_size;