1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
4 */
5
6 #include <common.h>
7 #include <image.h>
8 #include <android_image.h>
9 #include <malloc.h>
10 #include <mapmem.h>
11 #include <errno.h>
12
13 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
14
15 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
16
android_image_get_kernel_addr(const struct andr_img_hdr * hdr)17 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
18 {
19 /*
20 * All the Android tools that generate a boot.img use this
21 * address as the default.
22 *
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.
27 *
28 * Otherwise, we will return the actual value set by the user.
29 */
30 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
31 return (ulong)hdr + hdr->page_size;
32
33 return hdr->kernel_addr;
34 }
35
36 /**
37 * android_image_get_kernel() - processes kernel part of Android boot images
38 * @hdr: Pointer to image header, which is at the start
39 * of the image.
40 * @verify: Checksum verification flag. Currently unimplemented.
41 * @os_data: Pointer to a ulong variable, will hold os data start
42 * address.
43 * @os_len: Pointer to a ulong variable, will hold os data length.
44 *
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.
47 *
48 * Return: Zero, os start address and length on success,
49 * otherwise on failure.
50 */
android_image_get_kernel(const struct andr_img_hdr * hdr,int verify,ulong * os_data,ulong * os_len)51 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
52 ulong *os_data, ulong *os_len)
53 {
54 u32 kernel_addr = android_image_get_kernel_addr(hdr);
55
56 /*
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.
60 */
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);
65
66 printf("Kernel load addr 0x%08x size %u KiB\n",
67 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
68
69 int len = 0;
70 if (*hdr->cmdline) {
71 printf("Kernel command line: %s\n", hdr->cmdline);
72 len += strlen(hdr->cmdline);
73 }
74
75 char *bootargs = env_get("bootargs");
76 if (bootargs)
77 len += strlen(bootargs);
78
79 char *newbootargs = malloc(len + 2);
80 if (!newbootargs) {
81 puts("Error: malloc in android_image_get_kernel failed!\n");
82 return -ENOMEM;
83 }
84 *newbootargs = '\0';
85
86 if (bootargs) {
87 strcpy(newbootargs, bootargs);
88 strcat(newbootargs, " ");
89 }
90 if (*hdr->cmdline)
91 strcat(newbootargs, hdr->cmdline);
92
93 env_set("bootargs", newbootargs);
94
95 if (os_data) {
96 *os_data = (ulong)hdr;
97 *os_data += hdr->page_size;
98 }
99 if (os_len)
100 *os_len = hdr->kernel_size;
101 return 0;
102 }
103
android_image_check_header(const struct andr_img_hdr * hdr)104 int android_image_check_header(const struct andr_img_hdr *hdr)
105 {
106 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
107 }
108
android_image_get_end(const struct andr_img_hdr * hdr)109 ulong android_image_get_end(const struct andr_img_hdr *hdr)
110 {
111 ulong end;
112 /*
113 * The header takes a full page, the remaining components are aligned
114 * on page boundary
115 */
116 end = (ulong)hdr;
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);
121
122 return end;
123 }
124
android_image_get_kload(const struct andr_img_hdr * hdr)125 ulong android_image_get_kload(const struct andr_img_hdr *hdr)
126 {
127 return android_image_get_kernel_addr(hdr);
128 }
129
android_image_get_ramdisk(const struct andr_img_hdr * hdr,ulong * rd_data,ulong * rd_len)130 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
131 ulong *rd_data, ulong *rd_len)
132 {
133 if (!hdr->ramdisk_size) {
134 *rd_data = *rd_len = 0;
135 return -1;
136 }
137
138 printf("RAM disk load addr 0x%08x size %u KiB\n",
139 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
140
141 *rd_data = (unsigned long)hdr;
142 *rd_data += hdr->page_size;
143 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
144
145 *rd_len = hdr->ramdisk_size;
146 return 0;
147 }
148
android_image_load(struct blk_desc * dev_desc,const disk_partition_t * part_info,unsigned long load_address,unsigned long max_size)149 long android_image_load(struct blk_desc *dev_desc,
150 const disk_partition_t *part_info,
151 unsigned long load_address,
152 unsigned long max_size) {
153 void *buf;
154 long blk_cnt, blk_read = 0;
155
156 if (max_size < part_info->blksz)
157 return -1;
158
159 /* We don't know the size of the Android image before reading the header
160 * so we don't limit the size of the mapped memory.
161 */
162 buf = map_sysmem(load_address, 0 /* size */);
163
164 /* Read the Android header first and then read the rest. */
165 if (blk_dread(dev_desc, part_info->start, 1, buf) != 1)
166 blk_read = -1;
167
168 if (!blk_read && android_image_check_header(buf) != 0) {
169 printf("** Invalid Android Image header **\n");
170 blk_read = -1;
171 }
172 if (!blk_read) {
173 blk_cnt = (android_image_get_end(buf) - (ulong)buf +
174 part_info->blksz - 1) / part_info->blksz;
175 if (blk_cnt * part_info->blksz > max_size) {
176 debug("Android Image too big (%lu bytes, max %lu)\n",
177 android_image_get_end(buf) - (ulong)buf,
178 max_size);
179 blk_read = -1;
180 } else {
181 debug("Loading Android Image (%lu blocks) to 0x%lx... ",
182 blk_cnt, load_address);
183 blk_read = blk_dread(dev_desc, part_info->start,
184 blk_cnt, buf);
185 }
186 }
187
188 unmap_sysmem(buf);
189 if (blk_read < 0)
190 return blk_read;
191
192 debug("%lu blocks read: %s\n",
193 blk_read, (blk_read == blk_cnt) ? "OK" : "ERROR");
194 if (blk_read != blk_cnt)
195 return -1;
196 return blk_read;
197 }
198
android_image_get_second(const struct andr_img_hdr * hdr,ulong * second_data,ulong * second_len)199 int android_image_get_second(const struct andr_img_hdr *hdr,
200 ulong *second_data, ulong *second_len)
201 {
202 if (!hdr->second_size) {
203 *second_data = *second_len = 0;
204 return -1;
205 }
206
207 *second_data = (unsigned long)hdr;
208 *second_data += hdr->page_size;
209 *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
210 *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
211
212 printf("second address is 0x%lx\n",*second_data);
213
214 *second_len = hdr->second_size;
215 return 0;
216 }
217
218 #if !defined(CONFIG_SPL_BUILD)
219 /**
220 * android_print_contents - prints out the contents of the Android format image
221 * @hdr: pointer to the Android format image header
222 *
223 * android_print_contents() formats a multi line Android image contents
224 * description.
225 * The routine prints out Android image properties
226 *
227 * returns:
228 * no returned results
229 */
android_print_contents(const struct andr_img_hdr * hdr)230 void android_print_contents(const struct andr_img_hdr *hdr)
231 {
232 const char * const p = IMAGE_INDENT_STRING;
233 /* os_version = ver << 11 | lvl */
234 u32 os_ver = hdr->os_version >> 11;
235 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
236
237 printf("%skernel size: %x\n", p, hdr->kernel_size);
238 printf("%skernel address: %x\n", p, hdr->kernel_addr);
239 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
240 printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr);
241 printf("%ssecond size: %x\n", p, hdr->second_size);
242 printf("%ssecond address: %x\n", p, hdr->second_addr);
243 printf("%stags address: %x\n", p, hdr->tags_addr);
244 printf("%spage size: %x\n", p, hdr->page_size);
245 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
246 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
247 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
248 p, hdr->os_version,
249 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
250 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
251 printf("%sname: %s\n", p, hdr->name);
252 printf("%scmdline: %s\n", p, hdr->cmdline);
253 }
254 #endif
255