2 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <android_image.h>
13 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
15 static char andr_tmp_str
[ANDR_BOOT_ARGS_SIZE
+ 1];
17 static ulong
android_image_get_kernel_addr(const struct andr_img_hdr
*hdr
)
20 * All the Android tools that generate a boot.img use this
21 * address as the default.
23 * Even though it doesn't really make a lot of sense, and it
24 * might be valid on some platforms, we treat that adress as
25 * the default value for this field, and try to execute the
26 * kernel in place in such a case.
28 * Otherwise, we will return the actual value set by the user.
30 if (hdr
->kernel_addr
== ANDROID_IMAGE_DEFAULT_KERNEL_ADDR
)
31 return (ulong
)hdr
+ hdr
->page_size
;
33 return hdr
->kernel_addr
;
37 * android_image_get_kernel() - processes kernel part of Android boot images
38 * @hdr: Pointer to image header, which is at the start
40 * @verify: Checksum verification flag. Currently unimplemented.
41 * @os_data: Pointer to a ulong variable, will hold os data start
43 * @os_len: Pointer to a ulong variable, will hold os data length.
45 * This function returns the os image's start address and length. Also,
46 * it appends the kernel command line to the bootargs env variable.
48 * Return: Zero, os start address and length on success,
49 * otherwise on failure.
51 int android_image_get_kernel(const struct andr_img_hdr
*hdr
, int verify
,
52 ulong
*os_data
, ulong
*os_len
)
54 u32 kernel_addr
= android_image_get_kernel_addr(hdr
);
57 * Not all Android tools use the id field for signing the image with
58 * sha1 (or anything) so we don't check it. It is not obvious that the
59 * string is null terminated so we take care of this.
61 strncpy(andr_tmp_str
, hdr
->name
, ANDR_BOOT_NAME_SIZE
);
62 andr_tmp_str
[ANDR_BOOT_NAME_SIZE
] = '\0';
63 if (strlen(andr_tmp_str
))
64 printf("Android's image name: %s\n", andr_tmp_str
);
66 printf("Kernel load addr 0x%08x size %u KiB\n",
67 kernel_addr
, DIV_ROUND_UP(hdr
->kernel_size
, 1024));
71 printf("Kernel command line: %s\n", hdr
->cmdline
);
72 len
+= strlen(hdr
->cmdline
);
75 char *bootargs
= getenv("bootargs");
77 len
+= strlen(bootargs
);
79 char *newbootargs
= malloc(len
+ 2);
81 puts("Error: malloc in android_image_get_kernel failed!\n");
87 strcpy(newbootargs
, bootargs
);
88 strcat(newbootargs
, " ");
91 strcat(newbootargs
, hdr
->cmdline
);
93 setenv("bootargs", newbootargs
);
96 *os_data
= (ulong
)hdr
;
97 *os_data
+= hdr
->page_size
;
100 *os_len
= hdr
->kernel_size
;
104 int android_image_check_header(const struct andr_img_hdr
*hdr
)
106 return memcmp(ANDR_BOOT_MAGIC
, hdr
->magic
, ANDR_BOOT_MAGIC_SIZE
);
109 ulong
android_image_get_end(const struct andr_img_hdr
*hdr
)
113 * The header takes a full page, the remaining components are aligned
117 end
+= hdr
->page_size
;
118 end
+= ALIGN(hdr
->kernel_size
, hdr
->page_size
);
119 end
+= ALIGN(hdr
->ramdisk_size
, hdr
->page_size
);
120 end
+= ALIGN(hdr
->second_size
, hdr
->page_size
);
125 ulong
android_image_get_kload(const struct andr_img_hdr
*hdr
)
127 return android_image_get_kernel_addr(hdr
);
130 int android_image_get_ramdisk(const struct andr_img_hdr
*hdr
,
131 ulong
*rd_data
, ulong
*rd_len
)
133 if (!hdr
->ramdisk_size
) {
134 *rd_data
= *rd_len
= 0;
138 printf("RAM disk load addr 0x%08x size %u KiB\n",
139 hdr
->ramdisk_addr
, DIV_ROUND_UP(hdr
->ramdisk_size
, 1024));
141 *rd_data
= (unsigned long)hdr
;
142 *rd_data
+= hdr
->page_size
;
143 *rd_data
+= ALIGN(hdr
->kernel_size
, hdr
->page_size
);
145 *rd_len
= hdr
->ramdisk_size
;
149 #if !defined(CONFIG_SPL_BUILD)
151 * android_print_contents - prints out the contents of the Android format image
152 * @hdr: pointer to the Android format image header
154 * android_print_contents() formats a multi line Android image contents
156 * The routine prints out Android image properties
159 * no returned results
161 void android_print_contents(const struct andr_img_hdr
*hdr
)
163 const char * const p
= IMAGE_INDENT_STRING
;
164 /* os_version = ver << 11 | lvl */
165 u32 os_ver
= hdr
->os_version
>> 11;
166 u32 os_lvl
= hdr
->os_version
& ((1U << 11) - 1);
168 printf("%skernel size: %x\n", p
, hdr
->kernel_size
);
169 printf("%skernel address: %x\n", p
, hdr
->kernel_addr
);
170 printf("%sramdisk size: %x\n", p
, hdr
->ramdisk_size
);
171 printf("%sramdisk addrress: %x\n", p
, hdr
->ramdisk_addr
);
172 printf("%ssecond size: %x\n", p
, hdr
->second_size
);
173 printf("%ssecond address: %x\n", p
, hdr
->second_addr
);
174 printf("%stags address: %x\n", p
, hdr
->tags_addr
);
175 printf("%spage size: %x\n", p
, hdr
->page_size
);
176 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
177 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
178 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
180 (os_ver
>> 7) & 0x7F, (os_ver
>> 14) & 0x7F, os_ver
& 0x7F,
181 (os_lvl
>> 4) + 2000, os_lvl
& 0x0F);
182 printf("%sname: %s\n", p
, hdr
->name
);
183 printf("%scmdline: %s\n", p
, hdr
->cmdline
);