blob: 220983655ad46c1e467ccb98b5d494ac81e0ae98 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior686d6672014-05-05 15:08:09 -05002/*
3 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Sebastian Siewior686d6672014-05-05 15:08:09 -05004 */
5
6#include <common.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06007#include <env.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -05008#include <image.h>
Sam Protsenko4fabf402020-01-24 17:53:40 +02009#include <image-android-dt.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -050010#include <android_image.h>
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030011#include <malloc.h>
12#include <errno.h>
Eugeniu Rosca1403f392019-04-08 17:35:27 +020013#include <asm/unaligned.h>
Sam Protsenko4fabf402020-01-24 17:53:40 +020014#include <mapmem.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -050015
Maxime Ripard17ef1f52015-04-24 12:53:12 +020016#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
17
Sebastian Siewior686d6672014-05-05 15:08:09 -050018static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
19
Maxime Ripard17ef1f52015-04-24 12:53:12 +020020static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
21{
22 /*
23 * All the Android tools that generate a boot.img use this
24 * address as the default.
25 *
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.
30 *
31 * Otherwise, we will return the actual value set by the user.
32 */
33 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
34 return (ulong)hdr + hdr->page_size;
35
36 return hdr->kernel_addr;
37}
38
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030039/**
40 * android_image_get_kernel() - processes kernel part of Android boot images
41 * @hdr: Pointer to image header, which is at the start
42 * of the image.
43 * @verify: Checksum verification flag. Currently unimplemented.
44 * @os_data: Pointer to a ulong variable, will hold os data start
45 * address.
46 * @os_len: Pointer to a ulong variable, will hold os data length.
47 *
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.
50 *
51 * Return: Zero, os start address and length on success,
52 * otherwise on failure.
53 */
Sebastian Siewior686d6672014-05-05 15:08:09 -050054int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
55 ulong *os_data, ulong *os_len)
56{
Maxime Ripard17ef1f52015-04-24 12:53:12 +020057 u32 kernel_addr = android_image_get_kernel_addr(hdr);
Roman Stratiienko73268582019-06-03 15:38:13 +030058 const struct image_header *ihdr = (const struct image_header *)
59 ((uintptr_t)hdr + hdr->page_size);
Maxime Ripard17ef1f52015-04-24 12:53:12 +020060
Sebastian Siewior686d6672014-05-05 15:08:09 -050061 /*
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.
65 */
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);
70
71 printf("Kernel load addr 0x%08x size %u KiB\n",
Maxime Ripard17ef1f52015-04-24 12:53:12 +020072 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030073
74 int len = 0;
75 if (*hdr->cmdline) {
76 printf("Kernel command line: %s\n", hdr->cmdline);
77 len += strlen(hdr->cmdline);
Sebastian Siewior686d6672014-05-05 15:08:09 -050078 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030079
Simon Glass64b723f2017-08-03 12:22:12 -060080 char *bootargs = env_get("bootargs");
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030081 if (bootargs)
82 len += strlen(bootargs);
83
84 char *newbootargs = malloc(len + 2);
85 if (!newbootargs) {
86 puts("Error: malloc in android_image_get_kernel failed!\n");
87 return -ENOMEM;
88 }
89 *newbootargs = '\0';
90
91 if (bootargs) {
92 strcpy(newbootargs, bootargs);
93 strcat(newbootargs, " ");
94 }
95 if (*hdr->cmdline)
96 strcat(newbootargs, hdr->cmdline);
97
Simon Glass6a38e412017-08-03 12:22:09 -060098 env_set("bootargs", newbootargs);
Sebastian Siewior686d6672014-05-05 15:08:09 -050099
100 if (os_data) {
Roman Stratiienko73268582019-06-03 15:38:13 +0300101 if (image_get_magic(ihdr) == IH_MAGIC) {
102 *os_data = image_get_data(ihdr);
103 } else {
104 *os_data = (ulong)hdr;
105 *os_data += hdr->page_size;
106 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500107 }
Roman Stratiienko73268582019-06-03 15:38:13 +0300108 if (os_len) {
109 if (image_get_magic(ihdr) == IH_MAGIC)
110 *os_len = image_get_data_size(ihdr);
111 else
112 *os_len = hdr->kernel_size;
113 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500114 return 0;
115}
116
117int android_image_check_header(const struct andr_img_hdr *hdr)
118{
119 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
120}
121
122ulong android_image_get_end(const struct andr_img_hdr *hdr)
123{
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300124 ulong end;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300125
Sebastian Siewior686d6672014-05-05 15:08:09 -0500126 /*
127 * The header takes a full page, the remaining components are aligned
128 * on page boundary
129 */
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300130 end = (ulong)hdr;
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);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500135
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300136 if (hdr->header_version >= 1)
137 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
138
139 if (hdr->header_version >= 2)
140 end += ALIGN(hdr->dtb_size, hdr->page_size);
141
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300142 return end;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500143}
144
145ulong android_image_get_kload(const struct andr_img_hdr *hdr)
146{
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200147 return android_image_get_kernel_addr(hdr);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500148}
149
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200150ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
151{
152 const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
153
Roman Stratiienko73268582019-06-03 15:38:13 +0300154 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)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200157 return IH_COMP_LZ4;
158 else
159 return IH_COMP_NONE;
160}
161
Sebastian Siewior686d6672014-05-05 15:08:09 -0500162int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
163 ulong *rd_data, ulong *rd_len)
164{
Rob Herringd9798782015-10-05 14:37:07 -0500165 if (!hdr->ramdisk_size) {
166 *rd_data = *rd_len = 0;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500167 return -1;
Rob Herringd9798782015-10-05 14:37:07 -0500168 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300169
170 printf("RAM disk load addr 0x%08x size %u KiB\n",
171 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
172
Sebastian Siewior686d6672014-05-05 15:08:09 -0500173 *rd_data = (unsigned long)hdr;
174 *rd_data += hdr->page_size;
175 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
176
177 *rd_len = hdr->ramdisk_size;
178 return 0;
179}
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200180
Bin Chen909f1402018-01-27 16:59:08 +1100181int android_image_get_second(const struct andr_img_hdr *hdr,
182 ulong *second_data, ulong *second_len)
183{
184 if (!hdr->second_size) {
185 *second_data = *second_len = 0;
186 return -1;
187 }
188
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);
193
194 printf("second address is 0x%lx\n",*second_data);
195
196 *second_len = hdr->second_size;
197 return 0;
198}
199
Sam Protsenko4fabf402020-01-24 17:53:40 +0200200/**
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
204 *
205 * Return: true on success or false on fail.
206 */
207static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
208{
209 const struct andr_img_hdr *hdr;
210 ulong dtb_img_addr;
211 bool ret = true;
212
213 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
214 if (android_image_check_header(hdr)) {
215 printf("Error: Boot Image header is incorrect\n");
216 ret = false;
217 goto exit;
218 }
219
220 if (hdr->header_version < 2) {
221 printf("Error: header_version must be >= 2 to get dtb\n");
222 ret = false;
223 goto exit;
224 }
225
226 if (hdr->dtb_size == 0) {
227 printf("Error: dtb_size is 0\n");
228 ret = false;
229 goto exit;
230 }
231
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);
239
240 *addr = dtb_img_addr;
241
242exit:
243 unmap_sysmem(hdr);
244 return ret;
245}
246
247/**
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
253 *
254 * Get the address and size of DTB blob by its index in DTB area of Android
255 * Boot Image in RAM.
256 *
257 * Return: true on success or false on error.
258 */
259bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
260 u32 *size)
261{
262 const struct andr_img_hdr *hdr;
263 bool res;
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 */
268
269 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
270 if (!res)
271 return false;
272
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,
276 size);
277 }
278
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;
282 unmap_sysmem(hdr);
283 i = 0;
284 dtb_addr = dtb_img_addr;
285 while (dtb_addr < dtb_img_addr + dtb_img_size) {
286 const struct fdt_header *fdt;
287 u32 dtb_size;
288
289 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
290 if (fdt_check_header(fdt) != 0) {
291 unmap_sysmem(fdt);
292 printf("Error: Invalid FDT header for index %u\n", i);
293 return false;
294 }
295
296 dtb_size = fdt_totalsize(fdt);
297 unmap_sysmem(fdt);
298
299 if (i == index) {
300 if (size)
301 *size = dtb_size;
302 if (addr)
303 *addr = dtb_addr;
304 return true;
305 }
306
307 dtb_addr += dtb_size;
308 ++i;
309 }
310
311 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
312 return false;
313}
314
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200315#if !defined(CONFIG_SPL_BUILD)
316/**
317 * android_print_contents - prints out the contents of the Android format image
318 * @hdr: pointer to the Android format image header
319 *
320 * android_print_contents() formats a multi line Android image contents
321 * description.
322 * The routine prints out Android image properties
323 *
324 * returns:
325 * no returned results
326 */
327void android_print_contents(const struct andr_img_hdr *hdr)
328{
329 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo41f513a2017-04-02 01:49:47 -0700330 /* os_version = ver << 11 | lvl */
331 u32 os_ver = hdr->os_version >> 11;
332 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200333
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300334 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);
Alex Deymo41f513a2017-04-02 01:49:47 -0700342 /* 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) */
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300344 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo41f513a2017-04-02 01:49:47 -0700345 p, hdr->os_version,
346 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
347 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300348 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);
351
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,
358 hdr->header_size);
359 }
360
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);
364 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200365}
366
367/**
368 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
369 * @fdt: DTB header
370 * @index: Number of DTB blob in DTB area.
371 *
372 * Return: true on success or false on error.
373 */
374static bool android_image_print_dtb_info(const struct fdt_header *fdt,
375 u32 index)
376{
377 int root_node_off;
378 u32 fdt_size;
379 const char *model;
380 const char *compatible;
381
382 root_node_off = fdt_path_offset(fdt, "/");
383 if (root_node_off < 0) {
384 printf("Error: Root node not found\n");
385 return false;
386 }
387
388 fdt_size = fdt_totalsize(fdt);
389 compatible = fdt_getprop(fdt, root_node_off, "compatible",
390 NULL);
391 model = fdt_getprop(fdt, root_node_off, "model", NULL);
392
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)");
398
399 return true;
400}
401
402/**
403 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
404 * @hdr_addr: Boot image header address
405 *
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)
409 *
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
413 * each blob.
414 *
415 * Return: true on success or false on error.
416 */
417bool android_image_print_dtb_contents(ulong hdr_addr)
418{
419 const struct andr_img_hdr *hdr;
420 bool res;
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 */
425
426 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
427 if (!res)
428 return false;
429
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);
434 return true;
435 }
436
437 printf("## DTB area contents (concat format):\n");
438
439 /* Iterate over concatenated DTB blobs */
440 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
441 dtb_img_size = hdr->dtb_size;
442 unmap_sysmem(hdr);
443 i = 0;
444 dtb_addr = dtb_img_addr;
445 while (dtb_addr < dtb_img_addr + dtb_img_size) {
446 const struct fdt_header *fdt;
447 u32 dtb_size;
448
449 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
450 if (fdt_check_header(fdt) != 0) {
451 unmap_sysmem(fdt);
452 printf("Error: Invalid FDT header for index %u\n", i);
453 return false;
454 }
455
456 res = android_image_print_dtb_info(fdt, i);
457 if (!res) {
458 unmap_sysmem(fdt);
459 return false;
460 }
461
462 dtb_size = fdt_totalsize(fdt);
463 unmap_sysmem(fdt);
464 dtb_addr += dtb_size;
465 ++i;
466 }
467
468 return true;
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200469}
470#endif