blob: 15a735e23030f91e7acd99b87c080f821de42f8f [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <linux/libfdt.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -050016
Maxime Ripard17ef1f52015-04-24 12:53:12 +020017#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
18
Sebastian Siewior686d6672014-05-05 15:08:09 -050019static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
20
Safae Ouajih027191d2023-02-06 00:50:07 +010021static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
22 struct andr_image_data *data)
23{
24 ulong end;
25
26 data->image_name = hdr->name;
27 data->kcmdline = hdr->cmdline;
28 data->kernel_addr = hdr->kernel_addr;
29 data->ramdisk_addr = hdr->ramdisk_addr;
30 data->header_version = hdr->header_version;
31 data->dtb_load_addr = hdr->dtb_addr;
32
33 end = (ulong)hdr;
34
35 /*
36 * The header takes a full page, the remaining components are aligned
37 * on page boundary
38 */
39
40 end += hdr->page_size;
41
42 data->kernel_ptr = end;
43 data->kernel_size = hdr->kernel_size;
44 end += ALIGN(hdr->kernel_size, hdr->page_size);
45
46 data->ramdisk_ptr = end;
47 data->ramdisk_size = hdr->ramdisk_size;
48 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
49
50 data->second_ptr = end;
51 data->second_size = hdr->second_size;
52 end += ALIGN(hdr->second_size, hdr->page_size);
53
54 if (hdr->header_version >= 1) {
55 data->recovery_dtbo_ptr = end;
56 data->recovery_dtbo_size = hdr->recovery_dtbo_size;
57 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
58 }
59
60 if (hdr->header_version >= 2) {
61 data->dtb_ptr = end;
62 data->dtb_size = hdr->dtb_size;
63 end += ALIGN(hdr->dtb_size, hdr->page_size);
64 }
65
66 data->boot_img_total_size = end - (ulong)hdr;
67}
68
69bool android_image_get_data(const void *boot_hdr, struct andr_image_data *data)
70{
71 if (!boot_hdr || !data) {
72 printf("boot_hdr or data params can't be NULL\n");
73 return false;
74 }
75
76 if (!is_android_boot_image_header(boot_hdr)) {
77 printf("Incorrect boot image header\n");
78 return false;
79 }
80
81 if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2)
82 printf("Only boot image header version 2 and below are supported\n");
83 else
84 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
85
86 return true;
87}
88
Safae Ouajihbced1042023-02-06 00:50:08 +010089static ulong android_image_get_kernel_addr(struct andr_image_data *img_data)
Maxime Ripard17ef1f52015-04-24 12:53:12 +020090{
91 /*
92 * All the Android tools that generate a boot.img use this
93 * address as the default.
94 *
95 * Even though it doesn't really make a lot of sense, and it
96 * might be valid on some platforms, we treat that adress as
97 * the default value for this field, and try to execute the
98 * kernel in place in such a case.
99 *
100 * Otherwise, we will return the actual value set by the user.
101 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100102 if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
103 return img_data->kernel_ptr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200104
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200105 /*
106 * abootimg creates images where all load addresses are 0
107 * and we need to fix them.
108 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100109 if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200110 return env_get_ulong("kernel_addr_r", 16, 0);
111
Safae Ouajihbced1042023-02-06 00:50:08 +0100112 return img_data->kernel_addr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200113}
114
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300115/**
116 * android_image_get_kernel() - processes kernel part of Android boot images
117 * @hdr: Pointer to image header, which is at the start
118 * of the image.
119 * @verify: Checksum verification flag. Currently unimplemented.
120 * @os_data: Pointer to a ulong variable, will hold os data start
121 * address.
122 * @os_len: Pointer to a ulong variable, will hold os data length.
123 *
124 * This function returns the os image's start address and length. Also,
125 * it appends the kernel command line to the bootargs env variable.
126 *
127 * Return: Zero, os start address and length on success,
128 * otherwise on failure.
129 */
Safae Ouajih8656e382023-02-06 00:50:03 +0100130int android_image_get_kernel(const struct andr_boot_img_hdr_v0 *hdr, int verify,
Sebastian Siewior686d6672014-05-05 15:08:09 -0500131 ulong *os_data, ulong *os_len)
132{
Safae Ouajihbced1042023-02-06 00:50:08 +0100133 struct andr_image_data img_data = {0};
134 u32 kernel_addr;
135 const struct legacy_img_hdr *ihdr;
136
137 if (!android_image_get_data(hdr, &img_data))
138 return -EINVAL;
139
140 kernel_addr = android_image_get_kernel_addr(&img_data);
141 ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200142
Sebastian Siewior686d6672014-05-05 15:08:09 -0500143 /*
144 * Not all Android tools use the id field for signing the image with
145 * sha1 (or anything) so we don't check it. It is not obvious that the
146 * string is null terminated so we take care of this.
147 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100148 strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500149 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
150 if (strlen(andr_tmp_str))
151 printf("Android's image name: %s\n", andr_tmp_str);
152
153 printf("Kernel load addr 0x%08x size %u KiB\n",
Safae Ouajihbced1042023-02-06 00:50:08 +0100154 kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300155
156 int len = 0;
Safae Ouajihbced1042023-02-06 00:50:08 +0100157 if (*img_data.kcmdline) {
158 printf("Kernel command line: %s\n", img_data.kcmdline);
159 len += strlen(img_data.kcmdline);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500160 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300161
Simon Glass64b723f2017-08-03 12:22:12 -0600162 char *bootargs = env_get("bootargs");
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300163 if (bootargs)
164 len += strlen(bootargs);
165
166 char *newbootargs = malloc(len + 2);
167 if (!newbootargs) {
168 puts("Error: malloc in android_image_get_kernel failed!\n");
169 return -ENOMEM;
170 }
171 *newbootargs = '\0';
172
173 if (bootargs) {
174 strcpy(newbootargs, bootargs);
175 strcat(newbootargs, " ");
176 }
Safae Ouajihbced1042023-02-06 00:50:08 +0100177
178 if (*img_data.kcmdline)
179 strcat(newbootargs, img_data.kcmdline);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300180
Simon Glass6a38e412017-08-03 12:22:09 -0600181 env_set("bootargs", newbootargs);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500182
183 if (os_data) {
Roman Stratiienko73268582019-06-03 15:38:13 +0300184 if (image_get_magic(ihdr) == IH_MAGIC) {
185 *os_data = image_get_data(ihdr);
186 } else {
Safae Ouajihbced1042023-02-06 00:50:08 +0100187 *os_data = img_data.kernel_ptr;
Roman Stratiienko73268582019-06-03 15:38:13 +0300188 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500189 }
Roman Stratiienko73268582019-06-03 15:38:13 +0300190 if (os_len) {
191 if (image_get_magic(ihdr) == IH_MAGIC)
192 *os_len = image_get_data_size(ihdr);
193 else
Safae Ouajihbced1042023-02-06 00:50:08 +0100194 *os_len = img_data.kernel_size;
Roman Stratiienko73268582019-06-03 15:38:13 +0300195 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500196 return 0;
197}
198
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100199bool is_android_boot_image_header(const struct andr_boot_img_hdr_v0 *hdr)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500200{
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100201 return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500202}
203
Safae Ouajih8656e382023-02-06 00:50:03 +0100204ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500205{
Safae Ouajihbced1042023-02-06 00:50:08 +0100206 struct andr_image_data img_data;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500207
Safae Ouajihbced1042023-02-06 00:50:08 +0100208 if (!android_image_get_data(hdr, &img_data))
209 return -EINVAL;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300210
Safae Ouajihbced1042023-02-06 00:50:08 +0100211 if (img_data.header_version > 2)
212 return 0;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300213
Safae Ouajihbced1042023-02-06 00:50:08 +0100214 return img_data.boot_img_total_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500215}
216
Safae Ouajih8656e382023-02-06 00:50:03 +0100217ulong android_image_get_kload(const struct andr_boot_img_hdr_v0 *hdr)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500218{
Safae Ouajihbced1042023-02-06 00:50:08 +0100219 struct andr_image_data img_data;
220
221 if (!android_image_get_data(hdr, &img_data))
222 return -EINVAL;
223
224 return android_image_get_kernel_addr(&img_data);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500225}
226
Safae Ouajih8656e382023-02-06 00:50:03 +0100227ulong android_image_get_kcomp(const struct andr_boot_img_hdr_v0 *hdr)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200228{
Safae Ouajih027191d2023-02-06 00:50:07 +0100229 struct andr_image_data img_data;
230 const void *p;
231
232 if (!android_image_get_data(hdr, &img_data))
233 return -EINVAL;
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200234
Safae Ouajih027191d2023-02-06 00:50:07 +0100235 p = (const void *)img_data.kernel_ptr;
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600236 if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
237 return image_get_comp((struct legacy_img_hdr *)p);
Roman Stratiienko73268582019-06-03 15:38:13 +0300238 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200239 return IH_COMP_LZ4;
240 else
Stephan Gerhold9e5111d2021-07-01 20:33:16 +0200241 return image_decomp_type(p, sizeof(u32));
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200242}
243
Safae Ouajih8656e382023-02-06 00:50:03 +0100244int android_image_get_ramdisk(const struct andr_boot_img_hdr_v0 *hdr,
Sebastian Siewior686d6672014-05-05 15:08:09 -0500245 ulong *rd_data, ulong *rd_len)
246{
Safae Ouajihbced1042023-02-06 00:50:08 +0100247 struct andr_image_data img_data = {0};
248
249 if (!android_image_get_data(hdr, &img_data))
250 return -EINVAL;
251
252 if (!img_data.ramdisk_size) {
Rob Herringd9798782015-10-05 14:37:07 -0500253 *rd_data = *rd_len = 0;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500254 return -1;
Rob Herringd9798782015-10-05 14:37:07 -0500255 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300256
Safae Ouajihbced1042023-02-06 00:50:08 +0100257 printf("RAM disk load addr 0x%08lx size %u KiB\n",
258 img_data.ramdisk_ptr, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300259
Safae Ouajihbced1042023-02-06 00:50:08 +0100260 *rd_data = img_data.ramdisk_ptr;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500261
Safae Ouajihbced1042023-02-06 00:50:08 +0100262 *rd_len = img_data.ramdisk_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500263 return 0;
264}
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200265
Safae Ouajih8656e382023-02-06 00:50:03 +0100266int android_image_get_second(const struct andr_boot_img_hdr_v0 *hdr,
267 ulong *second_data, ulong *second_len)
Bin Chen909f1402018-01-27 16:59:08 +1100268{
Safae Ouajihbced1042023-02-06 00:50:08 +0100269 struct andr_image_data img_data;
270
271 if (!android_image_get_data(hdr, &img_data))
272 return -EINVAL;
273
274 if (!img_data.second_size) {
Bin Chen909f1402018-01-27 16:59:08 +1100275 *second_data = *second_len = 0;
276 return -1;
277 }
278
Safae Ouajihbced1042023-02-06 00:50:08 +0100279 *second_data = img_data.second_ptr;
Bin Chen909f1402018-01-27 16:59:08 +1100280
281 printf("second address is 0x%lx\n",*second_data);
282
Safae Ouajihbced1042023-02-06 00:50:08 +0100283 *second_len = img_data.second_size;
Bin Chen909f1402018-01-27 16:59:08 +1100284 return 0;
285}
286
Sam Protsenko4fabf402020-01-24 17:53:40 +0200287/**
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200288 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
289 * @hdr_addr: Boot image header address
290 * @addr: If not NULL, will contain address of recovery DTBO image
291 * @size: If not NULL, will contain size of recovery DTBO image
292 *
293 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
294 * Boot Image in RAM. The format of this image is Android DTBO (see
295 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
296 * the address is obtained from this function, one can use 'adtimg' U-Boot
297 * command or android_dt_*() functions to extract desired DTBO blob.
298 *
299 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
300 * only can be found in recovery image. On A/B devices we can always rely on
301 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
302 * AOSP documentation for details.
303 *
304 * Return: true on success or false on error.
305 */
306bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
307{
Safae Ouajih8656e382023-02-06 00:50:03 +0100308 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200309 ulong dtbo_img_addr;
310 bool ret = true;
311
312 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100313 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200314 printf("Error: Boot Image header is incorrect\n");
315 ret = false;
316 goto exit;
317 }
318
319 if (hdr->header_version < 1) {
320 printf("Error: header_version must be >= 1 to get dtbo\n");
321 ret = false;
322 goto exit;
323 }
324
325 if (hdr->recovery_dtbo_size == 0) {
326 printf("Error: recovery_dtbo_size is 0\n");
327 ret = false;
328 goto exit;
329 }
330
331 /* Calculate the address of DTB area in boot image */
332 dtbo_img_addr = hdr_addr;
333 dtbo_img_addr += hdr->page_size;
334 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
335 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
336 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
337
338 if (addr)
339 *addr = dtbo_img_addr;
340 if (size)
341 *size = hdr->recovery_dtbo_size;
342
343exit:
344 unmap_sysmem(hdr);
345 return ret;
346}
347
348/**
Sam Protsenko4fabf402020-01-24 17:53:40 +0200349 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
350 * @hdr_addr: Boot image header address
351 * @addr: Will contain the address of DTB area in boot image
352 *
353 * Return: true on success or false on fail.
354 */
355static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr)
356{
Safae Ouajih8656e382023-02-06 00:50:03 +0100357 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200358 ulong dtb_img_addr;
359 bool ret = true;
360
361 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100362 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko4fabf402020-01-24 17:53:40 +0200363 printf("Error: Boot Image header is incorrect\n");
364 ret = false;
365 goto exit;
366 }
367
368 if (hdr->header_version < 2) {
369 printf("Error: header_version must be >= 2 to get dtb\n");
370 ret = false;
371 goto exit;
372 }
373
374 if (hdr->dtb_size == 0) {
375 printf("Error: dtb_size is 0\n");
376 ret = false;
377 goto exit;
378 }
379
380 /* Calculate the address of DTB area in boot image */
381 dtb_img_addr = hdr_addr;
382 dtb_img_addr += hdr->page_size;
383 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
384 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
385 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
386 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
387
388 *addr = dtb_img_addr;
389
390exit:
391 unmap_sysmem(hdr);
392 return ret;
393}
394
395/**
396 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
397 * @hdr_addr: Boot image header address
398 * @index: Index of desired DTB in DTB area (starting from 0)
399 * @addr: If not NULL, will contain address to specified DTB
400 * @size: If not NULL, will contain size of specified DTB
401 *
402 * Get the address and size of DTB blob by its index in DTB area of Android
403 * Boot Image in RAM.
404 *
405 * Return: true on success or false on error.
406 */
407bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
408 u32 *size)
409{
Safae Ouajihbced1042023-02-06 00:50:08 +0100410 struct andr_image_data img_data;
Safae Ouajih8656e382023-02-06 00:50:03 +0100411 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100412
413 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
414 if (!android_image_get_data(hdr, &img_data)) {
415 unmap_sysmem(hdr);
416 return false;
417 }
418 unmap_sysmem(hdr);
419
Sam Protsenko4fabf402020-01-24 17:53:40 +0200420 ulong dtb_img_addr; /* address of DTB part in boot image */
421 u32 dtb_img_size; /* size of DTB payload in boot image */
422 ulong dtb_addr; /* address of DTB blob with specified index */
423 u32 i; /* index iterator */
424
Safae Ouajihbced1042023-02-06 00:50:08 +0100425 android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200426 /* Check if DTB area of boot image is in DTBO format */
427 if (android_dt_check_header(dtb_img_addr)) {
428 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
429 size);
430 }
431
432 /* Find out the address of DTB with specified index in concat blobs */
Safae Ouajihbced1042023-02-06 00:50:08 +0100433 dtb_img_size = img_data.dtb_size;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200434 i = 0;
435 dtb_addr = dtb_img_addr;
436 while (dtb_addr < dtb_img_addr + dtb_img_size) {
437 const struct fdt_header *fdt;
438 u32 dtb_size;
439
440 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
441 if (fdt_check_header(fdt) != 0) {
442 unmap_sysmem(fdt);
443 printf("Error: Invalid FDT header for index %u\n", i);
444 return false;
445 }
446
447 dtb_size = fdt_totalsize(fdt);
448 unmap_sysmem(fdt);
449
450 if (i == index) {
451 if (size)
452 *size = dtb_size;
453 if (addr)
454 *addr = dtb_addr;
455 return true;
456 }
457
458 dtb_addr += dtb_size;
459 ++i;
460 }
461
462 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
463 return false;
464}
465
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200466#if !defined(CONFIG_SPL_BUILD)
467/**
468 * android_print_contents - prints out the contents of the Android format image
469 * @hdr: pointer to the Android format image header
470 *
471 * android_print_contents() formats a multi line Android image contents
472 * description.
473 * The routine prints out Android image properties
474 *
475 * returns:
476 * no returned results
477 */
Safae Ouajih8656e382023-02-06 00:50:03 +0100478void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200479{
480 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo41f513a2017-04-02 01:49:47 -0700481 /* os_version = ver << 11 | lvl */
482 u32 os_ver = hdr->os_version >> 11;
483 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200484
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300485 printf("%skernel size: %x\n", p, hdr->kernel_size);
486 printf("%skernel address: %x\n", p, hdr->kernel_addr);
487 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
488 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
489 printf("%ssecond size: %x\n", p, hdr->second_size);
490 printf("%ssecond address: %x\n", p, hdr->second_addr);
491 printf("%stags address: %x\n", p, hdr->tags_addr);
492 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo41f513a2017-04-02 01:49:47 -0700493 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
494 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300495 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo41f513a2017-04-02 01:49:47 -0700496 p, hdr->os_version,
497 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
498 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300499 printf("%sname: %s\n", p, hdr->name);
500 printf("%scmdline: %s\n", p, hdr->cmdline);
501 printf("%sheader_version: %d\n", p, hdr->header_version);
502
503 if (hdr->header_version >= 1) {
504 printf("%srecovery dtbo size: %x\n", p,
505 hdr->recovery_dtbo_size);
506 printf("%srecovery dtbo offset: %llx\n", p,
507 hdr->recovery_dtbo_offset);
508 printf("%sheader size: %x\n", p,
509 hdr->header_size);
510 }
511
512 if (hdr->header_version >= 2) {
513 printf("%sdtb size: %x\n", p, hdr->dtb_size);
514 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
515 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200516}
517
518/**
519 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
520 * @fdt: DTB header
521 * @index: Number of DTB blob in DTB area.
522 *
523 * Return: true on success or false on error.
524 */
525static bool android_image_print_dtb_info(const struct fdt_header *fdt,
526 u32 index)
527{
528 int root_node_off;
529 u32 fdt_size;
530 const char *model;
531 const char *compatible;
532
533 root_node_off = fdt_path_offset(fdt, "/");
534 if (root_node_off < 0) {
535 printf("Error: Root node not found\n");
536 return false;
537 }
538
539 fdt_size = fdt_totalsize(fdt);
540 compatible = fdt_getprop(fdt, root_node_off, "compatible",
541 NULL);
542 model = fdt_getprop(fdt, root_node_off, "model", NULL);
543
544 printf(" - DTB #%u:\n", index);
545 printf(" (DTB)size = %d\n", fdt_size);
546 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
547 printf(" (DTB)compatible = %s\n",
548 compatible ? compatible : "(unknown)");
549
550 return true;
551}
552
553/**
554 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
555 * @hdr_addr: Boot image header address
556 *
557 * DTB payload in Android Boot Image v2+ can be in one of following formats:
558 * 1. Concatenated DTB blobs
559 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
560 *
561 * This function does next:
562 * 1. Prints out the format used in DTB area
563 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
564 * each blob.
565 *
566 * Return: true on success or false on error.
567 */
568bool android_image_print_dtb_contents(ulong hdr_addr)
569{
Safae Ouajih8656e382023-02-06 00:50:03 +0100570 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200571 bool res;
572 ulong dtb_img_addr; /* address of DTB part in boot image */
573 u32 dtb_img_size; /* size of DTB payload in boot image */
574 ulong dtb_addr; /* address of DTB blob with specified index */
575 u32 i; /* index iterator */
576
577 res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr);
578 if (!res)
579 return false;
580
581 /* Check if DTB area of boot image is in DTBO format */
582 if (android_dt_check_header(dtb_img_addr)) {
583 printf("## DTB area contents (DTBO format):\n");
584 android_dt_print_contents(dtb_img_addr);
585 return true;
586 }
587
588 printf("## DTB area contents (concat format):\n");
589
590 /* Iterate over concatenated DTB blobs */
591 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
592 dtb_img_size = hdr->dtb_size;
593 unmap_sysmem(hdr);
594 i = 0;
595 dtb_addr = dtb_img_addr;
596 while (dtb_addr < dtb_img_addr + dtb_img_size) {
597 const struct fdt_header *fdt;
598 u32 dtb_size;
599
600 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
601 if (fdt_check_header(fdt) != 0) {
602 unmap_sysmem(fdt);
603 printf("Error: Invalid FDT header for index %u\n", i);
604 return false;
605 }
606
607 res = android_image_print_dtb_info(fdt, i);
608 if (!res) {
609 unmap_sysmem(fdt);
610 return false;
611 }
612
613 dtb_size = fdt_totalsize(fdt);
614 unmap_sysmem(fdt);
615 dtb_addr += dtb_size;
616 ++i;
617 }
618
619 return true;
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200620}
621#endif