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;
36 return hdr->kernel_addr;
40 * android_image_get_kernel() - processes kernel part of Android boot images
41 * @hdr: Pointer to image header, which is at the start
43 * @verify: Checksum verification flag. Currently unimplemented.
44 * @os_data: Pointer to a ulong variable, will hold os data start
46 * @os_len: Pointer to a ulong variable, will hold os data length.
48 * This function returns the os image's start address and length. Also,
49 * it appends the kernel command line to the bootargs env variable.
51 * Return: Zero, os start address and length on success,
52 * otherwise on failure.
54 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
55 ulong *os_data, ulong *os_len)
57 u32 kernel_addr = android_image_get_kernel_addr(hdr);
58 const struct image_header *ihdr = (const struct image_header *)
59 ((uintptr_t)hdr + hdr->page_size);
62 * Not all Android tools use the id field for signing the image with
63 * sha1 (or anything) so we don't check it. It is not obvious that the
64 * string is null terminated so we take care of this.
66 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
67 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
68 if (strlen(andr_tmp_str))
69 printf("Android's image name: %s\n", andr_tmp_str);
71 printf("Kernel load addr 0x%08x size %u KiB\n",
72 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
76 printf("Kernel command line: %s\n", hdr->cmdline);
77 len += strlen(hdr->cmdline);
80 char *bootargs = env_get("bootargs");
82 len += strlen(bootargs);
84 char *newbootargs = malloc(len + 2);
86 puts("Error: malloc in android_image_get_kernel failed!\n");
92 strcpy(newbootargs, bootargs);
93 strcat(newbootargs, " ");
96 strcat(newbootargs, hdr->cmdline);
98 env_set("bootargs", newbootargs);
101 if (image_get_magic(ihdr) == IH_MAGIC) {
102 *os_data = image_get_data(ihdr);
104 *os_data = (ulong)hdr;
105 *os_data += hdr->page_size;
109 if (image_get_magic(ihdr) == IH_MAGIC)
110 *os_len = image_get_data_size(ihdr);
112 *os_len = hdr->kernel_size;
117 int android_image_check_header(const struct andr_img_hdr *hdr)
119 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
122 ulong android_image_get_end(const struct andr_img_hdr *hdr)
127 * The header takes a full page, the remaining components are aligned
131 end += hdr->page_size;
132 end += ALIGN(hdr->kernel_size, hdr->page_size);
133 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
134 end += ALIGN(hdr->second_size, hdr->page_size);
136 if (hdr->header_version >= 1)
137 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
139 if (hdr->header_version >= 2)
140 end += ALIGN(hdr->dtb_size, hdr->page_size);
145 ulong android_image_get_kload(const struct andr_img_hdr *hdr)
147 return android_image_get_kernel_addr(hdr);
150 ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
152 const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
154 if (image_get_magic((image_header_t *)p) == IH_MAGIC)
155 return image_get_comp((image_header_t *)p);
156 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
162 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
163 ulong *rd_data, ulong *rd_len)
165 if (!hdr->ramdisk_size) {
166 *rd_data = *rd_len = 0;
170 printf("RAM disk load addr 0x%08x size %u KiB\n",
171 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
173 *rd_data = (unsigned long)hdr;
174 *rd_data += hdr->page_size;
175 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
177 *rd_len = hdr->ramdisk_size;
181 int android_image_get_second(const struct andr_img_hdr *hdr,
182 ulong *second_data, ulong *second_len)
184 if (!hdr->second_size) {
185 *second_data = *second_len = 0;
189 *second_data = (unsigned long)hdr;
190 *second_data += hdr->page_size;
191 *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
192 *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
194 printf("second address is 0x%lx\n",*second_data);
196 *second_len = hdr->second_size;
201 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
202 * @hdr_addr: Boot image header address
203 * @addr: Will contain the address of DTB area in boot image
205 * Return: true on success or false on fail.
207 static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
209 const struct andr_img_hdr *hdr;
213 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
214 if (android_image_check_header(hdr)) {
215 printf("Error: Boot Image header is incorrect\n");
220 if (hdr->header_version < 2) {
221 printf("Error: header_version must be >= 2 to get dtb\n");
226 if (hdr->dtb_size == 0) {
227 printf("Error: dtb_size is 0\n");
232 /* Calculate the address of DTB area in boot image */
233 dtb_img_addr = hdr_addr;
234 dtb_img_addr += hdr->page_size;
235 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
236 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
237 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
238 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
240 *addr = dtb_img_addr;
248 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
249 * @hdr_addr: Boot image header address
250 * @index: Index of desired DTB in DTB area (starting from 0)
251 * @addr: If not NULL, will contain address to specified DTB
252 * @size: If not NULL, will contain size of specified DTB
254 * Get the address and size of DTB blob by its index in DTB area of Android
257 * Return: true on success or false on error.
259 bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
262 const struct andr_img_hdr *hdr;
264 ulong dtb_img_addr; /* address of DTB part in boot image */
265 u32 dtb_img_size; /* size of DTB payload in boot image */
266 ulong dtb_addr; /* address of DTB blob with specified index */
267 u32 i; /* index iterator */
269 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
273 /* Check if DTB area of boot image is in DTBO format */
274 if (android_dt_check_header(dtb_img_addr)) {
275 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
279 /* Find out the address of DTB with specified index in concat blobs */
280 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
281 dtb_img_size = hdr->dtb_size;
284 dtb_addr = dtb_img_addr;
285 while (dtb_addr < dtb_img_addr + dtb_img_size) {
286 const struct fdt_header *fdt;
289 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
290 if (fdt_check_header(fdt) != 0) {
292 printf("Error: Invalid FDT header for index %u\n", i);
296 dtb_size = fdt_totalsize(fdt);
307 dtb_addr += dtb_size;
311 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
315 #if !defined(CONFIG_SPL_BUILD)
317 * android_print_contents - prints out the contents of the Android format image
318 * @hdr: pointer to the Android format image header
320 * android_print_contents() formats a multi line Android image contents
322 * The routine prints out Android image properties
325 * no returned results
327 void android_print_contents(const struct andr_img_hdr *hdr)
329 const char * const p = IMAGE_INDENT_STRING;
330 /* os_version = ver << 11 | lvl */
331 u32 os_ver = hdr->os_version >> 11;
332 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
334 printf("%skernel size: %x\n", p, hdr->kernel_size);
335 printf("%skernel address: %x\n", p, hdr->kernel_addr);
336 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
337 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
338 printf("%ssecond size: %x\n", p, hdr->second_size);
339 printf("%ssecond address: %x\n", p, hdr->second_addr);
340 printf("%stags address: %x\n", p, hdr->tags_addr);
341 printf("%spage size: %x\n", p, hdr->page_size);
342 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
343 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
344 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
346 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
347 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
348 printf("%sname: %s\n", p, hdr->name);
349 printf("%scmdline: %s\n", p, hdr->cmdline);
350 printf("%sheader_version: %d\n", p, hdr->header_version);
352 if (hdr->header_version >= 1) {
353 printf("%srecovery dtbo size: %x\n", p,
354 hdr->recovery_dtbo_size);
355 printf("%srecovery dtbo offset: %llx\n", p,
356 hdr->recovery_dtbo_offset);
357 printf("%sheader size: %x\n", p,
361 if (hdr->header_version >= 2) {
362 printf("%sdtb size: %x\n", p, hdr->dtb_size);
363 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
368 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
370 * @index: Number of DTB blob in DTB area.
372 * Return: true on success or false on error.
374 static bool android_image_print_dtb_info(const struct fdt_header *fdt,
380 const char *compatible;
382 root_node_off = fdt_path_offset(fdt, "/");
383 if (root_node_off < 0) {
384 printf("Error: Root node not found\n");
388 fdt_size = fdt_totalsize(fdt);
389 compatible = fdt_getprop(fdt, root_node_off, "compatible",
391 model = fdt_getprop(fdt, root_node_off, "model", NULL);
393 printf(" - DTB #%u:\n", index);
394 printf(" (DTB)size = %d\n", fdt_size);
395 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
396 printf(" (DTB)compatible = %s\n",
397 compatible ? compatible : "(unknown)");
403 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
404 * @hdr_addr: Boot image header address
406 * DTB payload in Android Boot Image v2+ can be in one of following formats:
407 * 1. Concatenated DTB blobs
408 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
410 * This function does next:
411 * 1. Prints out the format used in DTB area
412 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
415 * Return: true on success or false on error.
417 bool android_image_print_dtb_contents(ulong hdr_addr)
419 const struct andr_img_hdr *hdr;
421 ulong dtb_img_addr; /* address of DTB part in boot image */
422 u32 dtb_img_size; /* size of DTB payload in boot image */
423 ulong dtb_addr; /* address of DTB blob with specified index */
424 u32 i; /* index iterator */
426 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
430 /* Check if DTB area of boot image is in DTBO format */
431 if (android_dt_check_header(dtb_img_addr)) {
432 printf("## DTB area contents (DTBO format):\n");
433 android_dt_print_contents(dtb_img_addr);
437 printf("## DTB area contents (concat format):\n");
439 /* Iterate over concatenated DTB blobs */
440 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
441 dtb_img_size = hdr->dtb_size;
444 dtb_addr = dtb_img_addr;
445 while (dtb_addr < dtb_img_addr + dtb_img_size) {
446 const struct fdt_header *fdt;
449 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
450 if (fdt_check_header(fdt) != 0) {
452 printf("Error: Invalid FDT header for index %u\n", i);
456 res = android_image_print_dtb_info(fdt, i);
462 dtb_size = fdt_totalsize(fdt);
464 dtb_addr += dtb_size;