blob: 6af9baa121dbab45a827454b41f02bff895e657f [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/**
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200201 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
202 * @hdr_addr: Boot image header address
203 * @addr: If not NULL, will contain address of recovery DTBO image
204 * @size: If not NULL, will contain size of recovery DTBO image
205 *
206 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
207 * Boot Image in RAM. The format of this image is Android DTBO (see
208 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
209 * the address is obtained from this function, one can use 'adtimg' U-Boot
210 * command or android_dt_*() functions to extract desired DTBO blob.
211 *
212 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
213 * only can be found in recovery image. On A/B devices we can always rely on
214 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
215 * AOSP documentation for details.
216 *
217 * Return: true on success or false on error.
218 */
219bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
220{
221 const struct andr_img_hdr *hdr;
222 ulong dtbo_img_addr;
223 bool ret = true;
224
225 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
226 if (android_image_check_header(hdr)) {
227 printf("Error: Boot Image header is incorrect\n");
228 ret = false;
229 goto exit;
230 }
231
232 if (hdr->header_version < 1) {
233 printf("Error: header_version must be >= 1 to get dtbo\n");
234 ret = false;
235 goto exit;
236 }
237
238 if (hdr->recovery_dtbo_size == 0) {
239 printf("Error: recovery_dtbo_size is 0\n");
240 ret = false;
241 goto exit;
242 }
243
244 /* Calculate the address of DTB area in boot image */
245 dtbo_img_addr = hdr_addr;
246 dtbo_img_addr += hdr->page_size;
247 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
248 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
249 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
250
251 if (addr)
252 *addr = dtbo_img_addr;
253 if (size)
254 *size = hdr->recovery_dtbo_size;
255
256exit:
257 unmap_sysmem(hdr);
258 return ret;
259}
260
261/**
Sam Protsenko4fabf402020-01-24 17:53:40 +0200262 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
263 * @hdr_addr: Boot image header address
264 * @addr: Will contain the address of DTB area in boot image
265 *
266 * Return: true on success or false on fail.
267 */
268static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
269{
270 const struct andr_img_hdr *hdr;
271 ulong dtb_img_addr;
272 bool ret = true;
273
274 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
275 if (android_image_check_header(hdr)) {
276 printf("Error: Boot Image header is incorrect\n");
277 ret = false;
278 goto exit;
279 }
280
281 if (hdr->header_version < 2) {
282 printf("Error: header_version must be >= 2 to get dtb\n");
283 ret = false;
284 goto exit;
285 }
286
287 if (hdr->dtb_size == 0) {
288 printf("Error: dtb_size is 0\n");
289 ret = false;
290 goto exit;
291 }
292
293 /* Calculate the address of DTB area in boot image */
294 dtb_img_addr = hdr_addr;
295 dtb_img_addr += hdr->page_size;
296 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
297 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
298 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
299 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
300
301 *addr = dtb_img_addr;
302
303exit:
304 unmap_sysmem(hdr);
305 return ret;
306}
307
308/**
309 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
310 * @hdr_addr: Boot image header address
311 * @index: Index of desired DTB in DTB area (starting from 0)
312 * @addr: If not NULL, will contain address to specified DTB
313 * @size: If not NULL, will contain size of specified DTB
314 *
315 * Get the address and size of DTB blob by its index in DTB area of Android
316 * Boot Image in RAM.
317 *
318 * Return: true on success or false on error.
319 */
320bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
321 u32 *size)
322{
323 const struct andr_img_hdr *hdr;
324 bool res;
325 ulong dtb_img_addr; /* address of DTB part in boot image */
326 u32 dtb_img_size; /* size of DTB payload in boot image */
327 ulong dtb_addr; /* address of DTB blob with specified index */
328 u32 i; /* index iterator */
329
330 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
331 if (!res)
332 return false;
333
334 /* Check if DTB area of boot image is in DTBO format */
335 if (android_dt_check_header(dtb_img_addr)) {
336 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
337 size);
338 }
339
340 /* Find out the address of DTB with specified index in concat blobs */
341 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
342 dtb_img_size = hdr->dtb_size;
343 unmap_sysmem(hdr);
344 i = 0;
345 dtb_addr = dtb_img_addr;
346 while (dtb_addr < dtb_img_addr + dtb_img_size) {
347 const struct fdt_header *fdt;
348 u32 dtb_size;
349
350 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
351 if (fdt_check_header(fdt) != 0) {
352 unmap_sysmem(fdt);
353 printf("Error: Invalid FDT header for index %u\n", i);
354 return false;
355 }
356
357 dtb_size = fdt_totalsize(fdt);
358 unmap_sysmem(fdt);
359
360 if (i == index) {
361 if (size)
362 *size = dtb_size;
363 if (addr)
364 *addr = dtb_addr;
365 return true;
366 }
367
368 dtb_addr += dtb_size;
369 ++i;
370 }
371
372 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
373 return false;
374}
375
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200376#if !defined(CONFIG_SPL_BUILD)
377/**
378 * android_print_contents - prints out the contents of the Android format image
379 * @hdr: pointer to the Android format image header
380 *
381 * android_print_contents() formats a multi line Android image contents
382 * description.
383 * The routine prints out Android image properties
384 *
385 * returns:
386 * no returned results
387 */
388void android_print_contents(const struct andr_img_hdr *hdr)
389{
390 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo41f513a2017-04-02 01:49:47 -0700391 /* os_version = ver << 11 | lvl */
392 u32 os_ver = hdr->os_version >> 11;
393 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200394
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300395 printf("%skernel size: %x\n", p, hdr->kernel_size);
396 printf("%skernel address: %x\n", p, hdr->kernel_addr);
397 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
398 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
399 printf("%ssecond size: %x\n", p, hdr->second_size);
400 printf("%ssecond address: %x\n", p, hdr->second_addr);
401 printf("%stags address: %x\n", p, hdr->tags_addr);
402 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo41f513a2017-04-02 01:49:47 -0700403 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
404 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300405 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo41f513a2017-04-02 01:49:47 -0700406 p, hdr->os_version,
407 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
408 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300409 printf("%sname: %s\n", p, hdr->name);
410 printf("%scmdline: %s\n", p, hdr->cmdline);
411 printf("%sheader_version: %d\n", p, hdr->header_version);
412
413 if (hdr->header_version >= 1) {
414 printf("%srecovery dtbo size: %x\n", p,
415 hdr->recovery_dtbo_size);
416 printf("%srecovery dtbo offset: %llx\n", p,
417 hdr->recovery_dtbo_offset);
418 printf("%sheader size: %x\n", p,
419 hdr->header_size);
420 }
421
422 if (hdr->header_version >= 2) {
423 printf("%sdtb size: %x\n", p, hdr->dtb_size);
424 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
425 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200426}
427
428/**
429 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
430 * @fdt: DTB header
431 * @index: Number of DTB blob in DTB area.
432 *
433 * Return: true on success or false on error.
434 */
435static bool android_image_print_dtb_info(const struct fdt_header *fdt,
436 u32 index)
437{
438 int root_node_off;
439 u32 fdt_size;
440 const char *model;
441 const char *compatible;
442
443 root_node_off = fdt_path_offset(fdt, "/");
444 if (root_node_off < 0) {
445 printf("Error: Root node not found\n");
446 return false;
447 }
448
449 fdt_size = fdt_totalsize(fdt);
450 compatible = fdt_getprop(fdt, root_node_off, "compatible",
451 NULL);
452 model = fdt_getprop(fdt, root_node_off, "model", NULL);
453
454 printf(" - DTB #%u:\n", index);
455 printf(" (DTB)size = %d\n", fdt_size);
456 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
457 printf(" (DTB)compatible = %s\n",
458 compatible ? compatible : "(unknown)");
459
460 return true;
461}
462
463/**
464 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
465 * @hdr_addr: Boot image header address
466 *
467 * DTB payload in Android Boot Image v2+ can be in one of following formats:
468 * 1. Concatenated DTB blobs
469 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
470 *
471 * This function does next:
472 * 1. Prints out the format used in DTB area
473 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
474 * each blob.
475 *
476 * Return: true on success or false on error.
477 */
478bool android_image_print_dtb_contents(ulong hdr_addr)
479{
480 const struct andr_img_hdr *hdr;
481 bool res;
482 ulong dtb_img_addr; /* address of DTB part in boot image */
483 u32 dtb_img_size; /* size of DTB payload in boot image */
484 ulong dtb_addr; /* address of DTB blob with specified index */
485 u32 i; /* index iterator */
486
487 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
488 if (!res)
489 return false;
490
491 /* Check if DTB area of boot image is in DTBO format */
492 if (android_dt_check_header(dtb_img_addr)) {
493 printf("## DTB area contents (DTBO format):\n");
494 android_dt_print_contents(dtb_img_addr);
495 return true;
496 }
497
498 printf("## DTB area contents (concat format):\n");
499
500 /* Iterate over concatenated DTB blobs */
501 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
502 dtb_img_size = hdr->dtb_size;
503 unmap_sysmem(hdr);
504 i = 0;
505 dtb_addr = dtb_img_addr;
506 while (dtb_addr < dtb_img_addr + dtb_img_size) {
507 const struct fdt_header *fdt;
508 u32 dtb_size;
509
510 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
511 if (fdt_check_header(fdt) != 0) {
512 unmap_sysmem(fdt);
513 printf("Error: Invalid FDT header for index %u\n", i);
514 return false;
515 }
516
517 res = android_image_print_dtb_info(fdt, i);
518 if (!res) {
519 unmap_sysmem(fdt);
520 return false;
521 }
522
523 dtb_size = fdt_totalsize(fdt);
524 unmap_sysmem(fdt);
525 dtb_addr += dtb_size;
526 ++i;
527 }
528
529 return true;
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200530}
531#endif