blob: d07b0e0f09b4087d56c4b7d8185b1730e5e9af6e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05002/*
3 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05004 */
5
6#include <common.h>
Simon Glass9fb625c2019-08-01 09:46:51 -06007#include <env.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05008#include <image.h>
Sam Protsenkoc3bfad82020-01-24 17:53:40 +02009#include <image-android-dt.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050010#include <android_image.h>
Ahmad Draidi86f46952014-10-23 20:50:07 +030011#include <malloc.h>
12#include <errno.h>
Eugeniu Rosca829ceb22019-04-08 17:35:27 +020013#include <asm/unaligned.h>
Sam Protsenkoc3bfad82020-01-24 17:53:40 +020014#include <mapmem.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <linux/libfdt.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050016
Maxime Ripard87f02d52015-04-24 12:53:12 +020017#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
18
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050019static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
20
Maxime Ripard87f02d52015-04-24 12:53:12 +020021static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
22{
23 /*
24 * All the Android tools that generate a boot.img use this
25 * address as the default.
26 *
27 * Even though it doesn't really make a lot of sense, and it
28 * might be valid on some platforms, we treat that adress as
29 * the default value for this field, and try to execute the
30 * kernel in place in such a case.
31 *
32 * Otherwise, we will return the actual value set by the user.
33 */
34 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
35 return (ulong)hdr + hdr->page_size;
36
Christian Gmeiner95712af2020-05-29 17:53:45 +020037 /*
38 * abootimg creates images where all load addresses are 0
39 * and we need to fix them.
40 */
41 if (hdr->kernel_addr == 0 && hdr->ramdisk_addr == 0)
42 return env_get_ulong("kernel_addr_r", 16, 0);
43
Maxime Ripard87f02d52015-04-24 12:53:12 +020044 return hdr->kernel_addr;
45}
46
Ahmad Draidi86f46952014-10-23 20:50:07 +030047/**
48 * android_image_get_kernel() - processes kernel part of Android boot images
49 * @hdr: Pointer to image header, which is at the start
50 * of the image.
51 * @verify: Checksum verification flag. Currently unimplemented.
52 * @os_data: Pointer to a ulong variable, will hold os data start
53 * address.
54 * @os_len: Pointer to a ulong variable, will hold os data length.
55 *
56 * This function returns the os image's start address and length. Also,
57 * it appends the kernel command line to the bootargs env variable.
58 *
59 * Return: Zero, os start address and length on success,
60 * otherwise on failure.
61 */
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050062int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
63 ulong *os_data, ulong *os_len)
64{
Maxime Ripard87f02d52015-04-24 12:53:12 +020065 u32 kernel_addr = android_image_get_kernel_addr(hdr);
Roman Stratiienko39f790b2019-06-03 15:38:13 +030066 const struct image_header *ihdr = (const struct image_header *)
67 ((uintptr_t)hdr + hdr->page_size);
Maxime Ripard87f02d52015-04-24 12:53:12 +020068
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050069 /*
70 * Not all Android tools use the id field for signing the image with
71 * sha1 (or anything) so we don't check it. It is not obvious that the
72 * string is null terminated so we take care of this.
73 */
74 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
75 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
76 if (strlen(andr_tmp_str))
77 printf("Android's image name: %s\n", andr_tmp_str);
78
79 printf("Kernel load addr 0x%08x size %u KiB\n",
Maxime Ripard87f02d52015-04-24 12:53:12 +020080 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
Ahmad Draidi86f46952014-10-23 20:50:07 +030081
82 int len = 0;
83 if (*hdr->cmdline) {
84 printf("Kernel command line: %s\n", hdr->cmdline);
85 len += strlen(hdr->cmdline);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050086 }
Ahmad Draidi86f46952014-10-23 20:50:07 +030087
Simon Glass00caae62017-08-03 12:22:12 -060088 char *bootargs = env_get("bootargs");
Ahmad Draidi86f46952014-10-23 20:50:07 +030089 if (bootargs)
90 len += strlen(bootargs);
91
92 char *newbootargs = malloc(len + 2);
93 if (!newbootargs) {
94 puts("Error: malloc in android_image_get_kernel failed!\n");
95 return -ENOMEM;
96 }
97 *newbootargs = '\0';
98
99 if (bootargs) {
100 strcpy(newbootargs, bootargs);
101 strcat(newbootargs, " ");
102 }
103 if (*hdr->cmdline)
104 strcat(newbootargs, hdr->cmdline);
105
Simon Glass382bee52017-08-03 12:22:09 -0600106 env_set("bootargs", newbootargs);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500107
108 if (os_data) {
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300109 if (image_get_magic(ihdr) == IH_MAGIC) {
110 *os_data = image_get_data(ihdr);
111 } else {
112 *os_data = (ulong)hdr;
113 *os_data += hdr->page_size;
114 }
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500115 }
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300116 if (os_len) {
117 if (image_get_magic(ihdr) == IH_MAGIC)
118 *os_len = image_get_data_size(ihdr);
119 else
120 *os_len = hdr->kernel_size;
121 }
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500122 return 0;
123}
124
125int android_image_check_header(const struct andr_img_hdr *hdr)
126{
127 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
128}
129
130ulong android_image_get_end(const struct andr_img_hdr *hdr)
131{
Ahmad Draidi86f46952014-10-23 20:50:07 +0300132 ulong end;
Sam Protsenkobb633632019-08-15 20:25:07 +0300133
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500134 /*
135 * The header takes a full page, the remaining components are aligned
136 * on page boundary
137 */
Ahmad Draidi86f46952014-10-23 20:50:07 +0300138 end = (ulong)hdr;
139 end += hdr->page_size;
140 end += ALIGN(hdr->kernel_size, hdr->page_size);
141 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
142 end += ALIGN(hdr->second_size, hdr->page_size);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500143
Sam Protsenkobb633632019-08-15 20:25:07 +0300144 if (hdr->header_version >= 1)
145 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
146
147 if (hdr->header_version >= 2)
148 end += ALIGN(hdr->dtb_size, hdr->page_size);
149
Ahmad Draidi86f46952014-10-23 20:50:07 +0300150 return end;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500151}
152
153ulong android_image_get_kload(const struct andr_img_hdr *hdr)
154{
Maxime Ripard87f02d52015-04-24 12:53:12 +0200155 return android_image_get_kernel_addr(hdr);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500156}
157
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200158ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
159{
160 const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
161
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300162 if (image_get_magic((image_header_t *)p) == IH_MAGIC)
163 return image_get_comp((image_header_t *)p);
164 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200165 return IH_COMP_LZ4;
166 else
167 return IH_COMP_NONE;
168}
169
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500170int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
171 ulong *rd_data, ulong *rd_len)
172{
Rob Herring99500982015-10-05 14:37:07 -0500173 if (!hdr->ramdisk_size) {
174 *rd_data = *rd_len = 0;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500175 return -1;
Rob Herring99500982015-10-05 14:37:07 -0500176 }
Ahmad Draidi86f46952014-10-23 20:50:07 +0300177
178 printf("RAM disk load addr 0x%08x size %u KiB\n",
179 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
180
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500181 *rd_data = (unsigned long)hdr;
182 *rd_data += hdr->page_size;
183 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
184
185 *rd_len = hdr->ramdisk_size;
186 return 0;
187}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200188
Bin Chen10481612018-01-27 16:59:08 +1100189int android_image_get_second(const struct andr_img_hdr *hdr,
190 ulong *second_data, ulong *second_len)
191{
192 if (!hdr->second_size) {
193 *second_data = *second_len = 0;
194 return -1;
195 }
196
197 *second_data = (unsigned long)hdr;
198 *second_data += hdr->page_size;
199 *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
200 *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
201
202 printf("second address is 0x%lx\n",*second_data);
203
204 *second_len = hdr->second_size;
205 return 0;
206}
207
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200208/**
Sam Protsenko7f253152020-01-24 17:53:41 +0200209 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
210 * @hdr_addr: Boot image header address
211 * @addr: If not NULL, will contain address of recovery DTBO image
212 * @size: If not NULL, will contain size of recovery DTBO image
213 *
214 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
215 * Boot Image in RAM. The format of this image is Android DTBO (see
216 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
217 * the address is obtained from this function, one can use 'adtimg' U-Boot
218 * command or android_dt_*() functions to extract desired DTBO blob.
219 *
220 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
221 * only can be found in recovery image. On A/B devices we can always rely on
222 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
223 * AOSP documentation for details.
224 *
225 * Return: true on success or false on error.
226 */
227bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
228{
229 const struct andr_img_hdr *hdr;
230 ulong dtbo_img_addr;
231 bool ret = true;
232
233 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
234 if (android_image_check_header(hdr)) {
235 printf("Error: Boot Image header is incorrect\n");
236 ret = false;
237 goto exit;
238 }
239
240 if (hdr->header_version < 1) {
241 printf("Error: header_version must be >= 1 to get dtbo\n");
242 ret = false;
243 goto exit;
244 }
245
246 if (hdr->recovery_dtbo_size == 0) {
247 printf("Error: recovery_dtbo_size is 0\n");
248 ret = false;
249 goto exit;
250 }
251
252 /* Calculate the address of DTB area in boot image */
253 dtbo_img_addr = hdr_addr;
254 dtbo_img_addr += hdr->page_size;
255 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
256 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
257 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
258
259 if (addr)
260 *addr = dtbo_img_addr;
261 if (size)
262 *size = hdr->recovery_dtbo_size;
263
264exit:
265 unmap_sysmem(hdr);
266 return ret;
267}
268
269/**
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200270 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
271 * @hdr_addr: Boot image header address
272 * @addr: Will contain the address of DTB area in boot image
273 *
274 * Return: true on success or false on fail.
275 */
276static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
277{
278 const struct andr_img_hdr *hdr;
279 ulong dtb_img_addr;
280 bool ret = true;
281
282 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
283 if (android_image_check_header(hdr)) {
284 printf("Error: Boot Image header is incorrect\n");
285 ret = false;
286 goto exit;
287 }
288
289 if (hdr->header_version < 2) {
290 printf("Error: header_version must be >= 2 to get dtb\n");
291 ret = false;
292 goto exit;
293 }
294
295 if (hdr->dtb_size == 0) {
296 printf("Error: dtb_size is 0\n");
297 ret = false;
298 goto exit;
299 }
300
301 /* Calculate the address of DTB area in boot image */
302 dtb_img_addr = hdr_addr;
303 dtb_img_addr += hdr->page_size;
304 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
305 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
306 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
307 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
308
309 *addr = dtb_img_addr;
310
311exit:
312 unmap_sysmem(hdr);
313 return ret;
314}
315
316/**
317 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
318 * @hdr_addr: Boot image header address
319 * @index: Index of desired DTB in DTB area (starting from 0)
320 * @addr: If not NULL, will contain address to specified DTB
321 * @size: If not NULL, will contain size of specified DTB
322 *
323 * Get the address and size of DTB blob by its index in DTB area of Android
324 * Boot Image in RAM.
325 *
326 * Return: true on success or false on error.
327 */
328bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
329 u32 *size)
330{
331 const struct andr_img_hdr *hdr;
332 bool res;
333 ulong dtb_img_addr; /* address of DTB part in boot image */
334 u32 dtb_img_size; /* size of DTB payload in boot image */
335 ulong dtb_addr; /* address of DTB blob with specified index */
336 u32 i; /* index iterator */
337
338 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
339 if (!res)
340 return false;
341
342 /* Check if DTB area of boot image is in DTBO format */
343 if (android_dt_check_header(dtb_img_addr)) {
344 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
345 size);
346 }
347
348 /* Find out the address of DTB with specified index in concat blobs */
349 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
350 dtb_img_size = hdr->dtb_size;
351 unmap_sysmem(hdr);
352 i = 0;
353 dtb_addr = dtb_img_addr;
354 while (dtb_addr < dtb_img_addr + dtb_img_size) {
355 const struct fdt_header *fdt;
356 u32 dtb_size;
357
358 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
359 if (fdt_check_header(fdt) != 0) {
360 unmap_sysmem(fdt);
361 printf("Error: Invalid FDT header for index %u\n", i);
362 return false;
363 }
364
365 dtb_size = fdt_totalsize(fdt);
366 unmap_sysmem(fdt);
367
368 if (i == index) {
369 if (size)
370 *size = dtb_size;
371 if (addr)
372 *addr = dtb_addr;
373 return true;
374 }
375
376 dtb_addr += dtb_size;
377 ++i;
378 }
379
380 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
381 return false;
382}
383
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200384#if !defined(CONFIG_SPL_BUILD)
385/**
386 * android_print_contents - prints out the contents of the Android format image
387 * @hdr: pointer to the Android format image header
388 *
389 * android_print_contents() formats a multi line Android image contents
390 * description.
391 * The routine prints out Android image properties
392 *
393 * returns:
394 * no returned results
395 */
396void android_print_contents(const struct andr_img_hdr *hdr)
397{
398 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo210a7172017-04-02 01:49:47 -0700399 /* os_version = ver << 11 | lvl */
400 u32 os_ver = hdr->os_version >> 11;
401 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200402
Sam Protsenkobb633632019-08-15 20:25:07 +0300403 printf("%skernel size: %x\n", p, hdr->kernel_size);
404 printf("%skernel address: %x\n", p, hdr->kernel_addr);
405 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
406 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
407 printf("%ssecond size: %x\n", p, hdr->second_size);
408 printf("%ssecond address: %x\n", p, hdr->second_addr);
409 printf("%stags address: %x\n", p, hdr->tags_addr);
410 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo210a7172017-04-02 01:49:47 -0700411 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
412 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenkobb633632019-08-15 20:25:07 +0300413 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo210a7172017-04-02 01:49:47 -0700414 p, hdr->os_version,
415 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
416 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenkobb633632019-08-15 20:25:07 +0300417 printf("%sname: %s\n", p, hdr->name);
418 printf("%scmdline: %s\n", p, hdr->cmdline);
419 printf("%sheader_version: %d\n", p, hdr->header_version);
420
421 if (hdr->header_version >= 1) {
422 printf("%srecovery dtbo size: %x\n", p,
423 hdr->recovery_dtbo_size);
424 printf("%srecovery dtbo offset: %llx\n", p,
425 hdr->recovery_dtbo_offset);
426 printf("%sheader size: %x\n", p,
427 hdr->header_size);
428 }
429
430 if (hdr->header_version >= 2) {
431 printf("%sdtb size: %x\n", p, hdr->dtb_size);
432 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
433 }
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200434}
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200435
436/**
437 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
438 * @fdt: DTB header
439 * @index: Number of DTB blob in DTB area.
440 *
441 * Return: true on success or false on error.
442 */
443static bool android_image_print_dtb_info(const struct fdt_header *fdt,
444 u32 index)
445{
446 int root_node_off;
447 u32 fdt_size;
448 const char *model;
449 const char *compatible;
450
451 root_node_off = fdt_path_offset(fdt, "/");
452 if (root_node_off < 0) {
453 printf("Error: Root node not found\n");
454 return false;
455 }
456
457 fdt_size = fdt_totalsize(fdt);
458 compatible = fdt_getprop(fdt, root_node_off, "compatible",
459 NULL);
460 model = fdt_getprop(fdt, root_node_off, "model", NULL);
461
462 printf(" - DTB #%u:\n", index);
463 printf(" (DTB)size = %d\n", fdt_size);
464 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
465 printf(" (DTB)compatible = %s\n",
466 compatible ? compatible : "(unknown)");
467
468 return true;
469}
470
471/**
472 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
473 * @hdr_addr: Boot image header address
474 *
475 * DTB payload in Android Boot Image v2+ can be in one of following formats:
476 * 1. Concatenated DTB blobs
477 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
478 *
479 * This function does next:
480 * 1. Prints out the format used in DTB area
481 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
482 * each blob.
483 *
484 * Return: true on success or false on error.
485 */
486bool android_image_print_dtb_contents(ulong hdr_addr)
487{
488 const struct andr_img_hdr *hdr;
489 bool res;
490 ulong dtb_img_addr; /* address of DTB part in boot image */
491 u32 dtb_img_size; /* size of DTB payload in boot image */
492 ulong dtb_addr; /* address of DTB blob with specified index */
493 u32 i; /* index iterator */
494
495 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
496 if (!res)
497 return false;
498
499 /* Check if DTB area of boot image is in DTBO format */
500 if (android_dt_check_header(dtb_img_addr)) {
501 printf("## DTB area contents (DTBO format):\n");
502 android_dt_print_contents(dtb_img_addr);
503 return true;
504 }
505
506 printf("## DTB area contents (concat format):\n");
507
508 /* Iterate over concatenated DTB blobs */
509 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
510 dtb_img_size = hdr->dtb_size;
511 unmap_sysmem(hdr);
512 i = 0;
513 dtb_addr = dtb_img_addr;
514 while (dtb_addr < dtb_img_addr + dtb_img_size) {
515 const struct fdt_header *fdt;
516 u32 dtb_size;
517
518 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
519 if (fdt_check_header(fdt) != 0) {
520 unmap_sysmem(fdt);
521 printf("Error: Invalid FDT header for index %u\n", i);
522 return false;
523 }
524
525 res = android_image_print_dtb_info(fdt, i);
526 if (!res) {
527 unmap_sysmem(fdt);
528 return false;
529 }
530
531 dtb_size = fdt_totalsize(fdt);
532 unmap_sysmem(fdt);
533 dtb_addr += dtb_size;
534 ++i;
535 }
536
537 return true;
538}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200539#endif