blob: 18f7c8db03ba7284d78d1c1723fd97988093ee0e [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
Christian Gmeiner3fabf862020-05-29 17:53:45 +020036 /*
37 * abootimg creates images where all load addresses are 0
38 * and we need to fix them.
39 */
40 if (hdr->kernel_addr == 0 && hdr->ramdisk_addr == 0)
41 return env_get_ulong("kernel_addr_r", 16, 0);
42
Maxime Ripard17ef1f52015-04-24 12:53:12 +020043 return hdr->kernel_addr;
44}
45
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030046/**
47 * android_image_get_kernel() - processes kernel part of Android boot images
48 * @hdr: Pointer to image header, which is at the start
49 * of the image.
50 * @verify: Checksum verification flag. Currently unimplemented.
51 * @os_data: Pointer to a ulong variable, will hold os data start
52 * address.
53 * @os_len: Pointer to a ulong variable, will hold os data length.
54 *
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.
57 *
58 * Return: Zero, os start address and length on success,
59 * otherwise on failure.
60 */
Sebastian Siewior686d6672014-05-05 15:08:09 -050061int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
62 ulong *os_data, ulong *os_len)
63{
Maxime Ripard17ef1f52015-04-24 12:53:12 +020064 u32 kernel_addr = android_image_get_kernel_addr(hdr);
Roman Stratiienko73268582019-06-03 15:38:13 +030065 const struct image_header *ihdr = (const struct image_header *)
66 ((uintptr_t)hdr + hdr->page_size);
Maxime Ripard17ef1f52015-04-24 12:53:12 +020067
Sebastian Siewior686d6672014-05-05 15:08:09 -050068 /*
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.
72 */
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);
77
78 printf("Kernel load addr 0x%08x size %u KiB\n",
Maxime Ripard17ef1f52015-04-24 12:53:12 +020079 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030080
81 int len = 0;
82 if (*hdr->cmdline) {
83 printf("Kernel command line: %s\n", hdr->cmdline);
84 len += strlen(hdr->cmdline);
Sebastian Siewior686d6672014-05-05 15:08:09 -050085 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030086
Simon Glass64b723f2017-08-03 12:22:12 -060087 char *bootargs = env_get("bootargs");
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030088 if (bootargs)
89 len += strlen(bootargs);
90
91 char *newbootargs = malloc(len + 2);
92 if (!newbootargs) {
93 puts("Error: malloc in android_image_get_kernel failed!\n");
94 return -ENOMEM;
95 }
96 *newbootargs = '\0';
97
98 if (bootargs) {
99 strcpy(newbootargs, bootargs);
100 strcat(newbootargs, " ");
101 }
102 if (*hdr->cmdline)
103 strcat(newbootargs, hdr->cmdline);
104
Simon Glass6a38e412017-08-03 12:22:09 -0600105 env_set("bootargs", newbootargs);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500106
107 if (os_data) {
Roman Stratiienko73268582019-06-03 15:38:13 +0300108 if (image_get_magic(ihdr) == IH_MAGIC) {
109 *os_data = image_get_data(ihdr);
110 } else {
111 *os_data = (ulong)hdr;
112 *os_data += hdr->page_size;
113 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500114 }
Roman Stratiienko73268582019-06-03 15:38:13 +0300115 if (os_len) {
116 if (image_get_magic(ihdr) == IH_MAGIC)
117 *os_len = image_get_data_size(ihdr);
118 else
119 *os_len = hdr->kernel_size;
120 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500121 return 0;
122}
123
124int android_image_check_header(const struct andr_img_hdr *hdr)
125{
126 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
127}
128
129ulong android_image_get_end(const struct andr_img_hdr *hdr)
130{
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300131 ulong end;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300132
Sebastian Siewior686d6672014-05-05 15:08:09 -0500133 /*
134 * The header takes a full page, the remaining components are aligned
135 * on page boundary
136 */
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300137 end = (ulong)hdr;
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);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500142
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300143 if (hdr->header_version >= 1)
144 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
145
146 if (hdr->header_version >= 2)
147 end += ALIGN(hdr->dtb_size, hdr->page_size);
148
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300149 return end;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500150}
151
152ulong android_image_get_kload(const struct andr_img_hdr *hdr)
153{
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200154 return android_image_get_kernel_addr(hdr);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500155}
156
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200157ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
158{
159 const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
160
Roman Stratiienko73268582019-06-03 15:38:13 +0300161 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)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200164 return IH_COMP_LZ4;
165 else
166 return IH_COMP_NONE;
167}
168
Sebastian Siewior686d6672014-05-05 15:08:09 -0500169int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
170 ulong *rd_data, ulong *rd_len)
171{
Rob Herringd9798782015-10-05 14:37:07 -0500172 if (!hdr->ramdisk_size) {
173 *rd_data = *rd_len = 0;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500174 return -1;
Rob Herringd9798782015-10-05 14:37:07 -0500175 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300176
177 printf("RAM disk load addr 0x%08x size %u KiB\n",
178 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
179
Sebastian Siewior686d6672014-05-05 15:08:09 -0500180 *rd_data = (unsigned long)hdr;
181 *rd_data += hdr->page_size;
182 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
183
184 *rd_len = hdr->ramdisk_size;
185 return 0;
186}
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200187
Bin Chen909f1402018-01-27 16:59:08 +1100188int android_image_get_second(const struct andr_img_hdr *hdr,
189 ulong *second_data, ulong *second_len)
190{
191 if (!hdr->second_size) {
192 *second_data = *second_len = 0;
193 return -1;
194 }
195
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);
200
201 printf("second address is 0x%lx\n",*second_data);
202
203 *second_len = hdr->second_size;
204 return 0;
205}
206
Sam Protsenko4fabf402020-01-24 17:53:40 +0200207/**
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200208 * 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
212 *
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.
218 *
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.
223 *
224 * Return: true on success or false on error.
225 */
226bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
227{
228 const struct andr_img_hdr *hdr;
229 ulong dtbo_img_addr;
230 bool ret = true;
231
232 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
233 if (android_image_check_header(hdr)) {
234 printf("Error: Boot Image header is incorrect\n");
235 ret = false;
236 goto exit;
237 }
238
239 if (hdr->header_version < 1) {
240 printf("Error: header_version must be >= 1 to get dtbo\n");
241 ret = false;
242 goto exit;
243 }
244
245 if (hdr->recovery_dtbo_size == 0) {
246 printf("Error: recovery_dtbo_size is 0\n");
247 ret = false;
248 goto exit;
249 }
250
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);
257
258 if (addr)
259 *addr = dtbo_img_addr;
260 if (size)
261 *size = hdr->recovery_dtbo_size;
262
263exit:
264 unmap_sysmem(hdr);
265 return ret;
266}
267
268/**
Sam Protsenko4fabf402020-01-24 17:53:40 +0200269 * 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
272 *
273 * Return: true on success or false on fail.
274 */
275static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
276{
277 const struct andr_img_hdr *hdr;
278 ulong dtb_img_addr;
279 bool ret = true;
280
281 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
282 if (android_image_check_header(hdr)) {
283 printf("Error: Boot Image header is incorrect\n");
284 ret = false;
285 goto exit;
286 }
287
288 if (hdr->header_version < 2) {
289 printf("Error: header_version must be >= 2 to get dtb\n");
290 ret = false;
291 goto exit;
292 }
293
294 if (hdr->dtb_size == 0) {
295 printf("Error: dtb_size is 0\n");
296 ret = false;
297 goto exit;
298 }
299
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);
307
308 *addr = dtb_img_addr;
309
310exit:
311 unmap_sysmem(hdr);
312 return ret;
313}
314
315/**
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
321 *
322 * Get the address and size of DTB blob by its index in DTB area of Android
323 * Boot Image in RAM.
324 *
325 * Return: true on success or false on error.
326 */
327bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
328 u32 *size)
329{
330 const struct andr_img_hdr *hdr;
331 bool res;
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 */
336
337 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
338 if (!res)
339 return false;
340
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,
344 size);
345 }
346
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;
350 unmap_sysmem(hdr);
351 i = 0;
352 dtb_addr = dtb_img_addr;
353 while (dtb_addr < dtb_img_addr + dtb_img_size) {
354 const struct fdt_header *fdt;
355 u32 dtb_size;
356
357 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
358 if (fdt_check_header(fdt) != 0) {
359 unmap_sysmem(fdt);
360 printf("Error: Invalid FDT header for index %u\n", i);
361 return false;
362 }
363
364 dtb_size = fdt_totalsize(fdt);
365 unmap_sysmem(fdt);
366
367 if (i == index) {
368 if (size)
369 *size = dtb_size;
370 if (addr)
371 *addr = dtb_addr;
372 return true;
373 }
374
375 dtb_addr += dtb_size;
376 ++i;
377 }
378
379 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
380 return false;
381}
382
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200383#if !defined(CONFIG_SPL_BUILD)
384/**
385 * android_print_contents - prints out the contents of the Android format image
386 * @hdr: pointer to the Android format image header
387 *
388 * android_print_contents() formats a multi line Android image contents
389 * description.
390 * The routine prints out Android image properties
391 *
392 * returns:
393 * no returned results
394 */
395void android_print_contents(const struct andr_img_hdr *hdr)
396{
397 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo41f513a2017-04-02 01:49:47 -0700398 /* os_version = ver << 11 | lvl */
399 u32 os_ver = hdr->os_version >> 11;
400 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200401
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300402 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);
Alex Deymo41f513a2017-04-02 01:49:47 -0700410 /* 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) */
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300412 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo41f513a2017-04-02 01:49:47 -0700413 p, hdr->os_version,
414 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
415 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300416 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);
419
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,
426 hdr->header_size);
427 }
428
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);
432 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200433}
434
435/**
436 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
437 * @fdt: DTB header
438 * @index: Number of DTB blob in DTB area.
439 *
440 * Return: true on success or false on error.
441 */
442static bool android_image_print_dtb_info(const struct fdt_header *fdt,
443 u32 index)
444{
445 int root_node_off;
446 u32 fdt_size;
447 const char *model;
448 const char *compatible;
449
450 root_node_off = fdt_path_offset(fdt, "/");
451 if (root_node_off < 0) {
452 printf("Error: Root node not found\n");
453 return false;
454 }
455
456 fdt_size = fdt_totalsize(fdt);
457 compatible = fdt_getprop(fdt, root_node_off, "compatible",
458 NULL);
459 model = fdt_getprop(fdt, root_node_off, "model", NULL);
460
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)");
466
467 return true;
468}
469
470/**
471 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
472 * @hdr_addr: Boot image header address
473 *
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)
477 *
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
481 * each blob.
482 *
483 * Return: true on success or false on error.
484 */
485bool android_image_print_dtb_contents(ulong hdr_addr)
486{
487 const struct andr_img_hdr *hdr;
488 bool res;
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 */
493
494 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
495 if (!res)
496 return false;
497
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);
502 return true;
503 }
504
505 printf("## DTB area contents (concat format):\n");
506
507 /* Iterate over concatenated DTB blobs */
508 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
509 dtb_img_size = hdr->dtb_size;
510 unmap_sysmem(hdr);
511 i = 0;
512 dtb_addr = dtb_img_addr;
513 while (dtb_addr < dtb_img_addr + dtb_img_size) {
514 const struct fdt_header *fdt;
515 u32 dtb_size;
516
517 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
518 if (fdt_check_header(fdt) != 0) {
519 unmap_sysmem(fdt);
520 printf("Error: Invalid FDT header for index %u\n", i);
521 return false;
522 }
523
524 res = android_image_print_dtb_info(fdt, i);
525 if (!res) {
526 unmap_sysmem(fdt);
527 return false;
528 }
529
530 dtb_size = fdt_totalsize(fdt);
531 unmap_sysmem(fdt);
532 dtb_addr += dtb_size;
533 ++i;
534 }
535
536 return true;
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200537}
538#endif