blob: f16eebff49ed1ac24cfe75dab5ab7c83332214a5 [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
Safae Ouajihf48efa02023-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
Safae Ouajihe0581762023-02-06 00:50:11 +010069bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
70 struct andr_image_data *data)
Safae Ouajihf48efa02023-02-06 00:50:07 +010071{
72 if (!boot_hdr || !data) {
73 printf("boot_hdr or data params can't be NULL\n");
74 return false;
75 }
76
77 if (!is_android_boot_image_header(boot_hdr)) {
78 printf("Incorrect boot image header\n");
79 return false;
80 }
81
82 if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2)
83 printf("Only boot image header version 2 and below are supported\n");
84 else
85 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
86
87 return true;
88}
89
Safae Ouajih607b0752023-02-06 00:50:08 +010090static ulong android_image_get_kernel_addr(struct andr_image_data *img_data)
Maxime Ripard87f02d52015-04-24 12:53:12 +020091{
92 /*
93 * All the Android tools that generate a boot.img use this
94 * address as the default.
95 *
96 * Even though it doesn't really make a lot of sense, and it
97 * might be valid on some platforms, we treat that adress as
98 * the default value for this field, and try to execute the
99 * kernel in place in such a case.
100 *
101 * Otherwise, we will return the actual value set by the user.
102 */
Safae Ouajih607b0752023-02-06 00:50:08 +0100103 if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
104 return img_data->kernel_ptr;
Maxime Ripard87f02d52015-04-24 12:53:12 +0200105
Christian Gmeiner95712af2020-05-29 17:53:45 +0200106 /*
107 * abootimg creates images where all load addresses are 0
108 * and we need to fix them.
109 */
Safae Ouajih607b0752023-02-06 00:50:08 +0100110 if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
Christian Gmeiner95712af2020-05-29 17:53:45 +0200111 return env_get_ulong("kernel_addr_r", 16, 0);
112
Safae Ouajih607b0752023-02-06 00:50:08 +0100113 return img_data->kernel_addr;
Maxime Ripard87f02d52015-04-24 12:53:12 +0200114}
115
Ahmad Draidi86f46952014-10-23 20:50:07 +0300116/**
117 * android_image_get_kernel() - processes kernel part of Android boot images
Safae Ouajihe0581762023-02-06 00:50:11 +0100118 * @hdr: Pointer to boot image header, which is at the start
Ahmad Draidi86f46952014-10-23 20:50:07 +0300119 * of the image.
Safae Ouajihe0581762023-02-06 00:50:11 +0100120 * @vendor_boot_img: Pointer to vendor boot image header, which is at the
121 * start of the image.
Ahmad Draidi86f46952014-10-23 20:50:07 +0300122 * @verify: Checksum verification flag. Currently unimplemented.
123 * @os_data: Pointer to a ulong variable, will hold os data start
124 * address.
125 * @os_len: Pointer to a ulong variable, will hold os data length.
126 *
127 * This function returns the os image's start address and length. Also,
128 * it appends the kernel command line to the bootargs env variable.
129 *
130 * Return: Zero, os start address and length on success,
131 * otherwise on failure.
132 */
Safae Ouajihe0581762023-02-06 00:50:11 +0100133int android_image_get_kernel(const struct andr_boot_img_hdr_v0 *hdr,
134 const void *vendor_boot_img, int verify,
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500135 ulong *os_data, ulong *os_len)
136{
Safae Ouajih607b0752023-02-06 00:50:08 +0100137 struct andr_image_data img_data = {0};
138 u32 kernel_addr;
139 const struct legacy_img_hdr *ihdr;
140
Safae Ouajihe0581762023-02-06 00:50:11 +0100141 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100142 return -EINVAL;
143
144 kernel_addr = android_image_get_kernel_addr(&img_data);
145 ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
Maxime Ripard87f02d52015-04-24 12:53:12 +0200146
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500147 /*
148 * Not all Android tools use the id field for signing the image with
149 * sha1 (or anything) so we don't check it. It is not obvious that the
150 * string is null terminated so we take care of this.
151 */
Safae Ouajih607b0752023-02-06 00:50:08 +0100152 strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500153 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
154 if (strlen(andr_tmp_str))
155 printf("Android's image name: %s\n", andr_tmp_str);
156
157 printf("Kernel load addr 0x%08x size %u KiB\n",
Safae Ouajih607b0752023-02-06 00:50:08 +0100158 kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
Ahmad Draidi86f46952014-10-23 20:50:07 +0300159
160 int len = 0;
Safae Ouajih607b0752023-02-06 00:50:08 +0100161 if (*img_data.kcmdline) {
162 printf("Kernel command line: %s\n", img_data.kcmdline);
163 len += strlen(img_data.kcmdline);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500164 }
Ahmad Draidi86f46952014-10-23 20:50:07 +0300165
Simon Glass00caae62017-08-03 12:22:12 -0600166 char *bootargs = env_get("bootargs");
Ahmad Draidi86f46952014-10-23 20:50:07 +0300167 if (bootargs)
168 len += strlen(bootargs);
169
170 char *newbootargs = malloc(len + 2);
171 if (!newbootargs) {
172 puts("Error: malloc in android_image_get_kernel failed!\n");
173 return -ENOMEM;
174 }
175 *newbootargs = '\0';
176
177 if (bootargs) {
178 strcpy(newbootargs, bootargs);
179 strcat(newbootargs, " ");
180 }
Safae Ouajih607b0752023-02-06 00:50:08 +0100181
182 if (*img_data.kcmdline)
183 strcat(newbootargs, img_data.kcmdline);
Ahmad Draidi86f46952014-10-23 20:50:07 +0300184
Simon Glass382bee52017-08-03 12:22:09 -0600185 env_set("bootargs", newbootargs);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500186
187 if (os_data) {
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300188 if (image_get_magic(ihdr) == IH_MAGIC) {
189 *os_data = image_get_data(ihdr);
190 } else {
Safae Ouajih607b0752023-02-06 00:50:08 +0100191 *os_data = img_data.kernel_ptr;
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300192 }
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500193 }
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300194 if (os_len) {
195 if (image_get_magic(ihdr) == IH_MAGIC)
196 *os_len = image_get_data_size(ihdr);
197 else
Safae Ouajih607b0752023-02-06 00:50:08 +0100198 *os_len = img_data.kernel_size;
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300199 }
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500200 return 0;
201}
202
Safae Ouajih734cb472023-02-06 00:50:05 +0100203bool is_android_boot_image_header(const struct andr_boot_img_hdr_v0 *hdr)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500204{
Safae Ouajih734cb472023-02-06 00:50:05 +0100205 return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500206}
207
Safae Ouajihe0581762023-02-06 00:50:11 +0100208ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
209 const void *vendor_boot_img)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500210{
Safae Ouajih607b0752023-02-06 00:50:08 +0100211 struct andr_image_data img_data;
Sam Protsenkobb633632019-08-15 20:25:07 +0300212
Safae Ouajihe0581762023-02-06 00:50:11 +0100213 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100214 return -EINVAL;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500215
Safae Ouajih607b0752023-02-06 00:50:08 +0100216 if (img_data.header_version > 2)
217 return 0;
Sam Protsenkobb633632019-08-15 20:25:07 +0300218
Safae Ouajih607b0752023-02-06 00:50:08 +0100219 return img_data.boot_img_total_size;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500220}
221
Safae Ouajihe0581762023-02-06 00:50:11 +0100222ulong android_image_get_kload(const struct andr_boot_img_hdr_v0 *hdr,
223 const void *vendor_boot_img)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500224{
Safae Ouajih607b0752023-02-06 00:50:08 +0100225 struct andr_image_data img_data;
226
Safae Ouajihe0581762023-02-06 00:50:11 +0100227 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100228 return -EINVAL;
229
230 return android_image_get_kernel_addr(&img_data);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500231}
232
Safae Ouajihe0581762023-02-06 00:50:11 +0100233ulong android_image_get_kcomp(const struct andr_boot_img_hdr_v0 *hdr,
234 const void *vendor_boot_img)
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200235{
Safae Ouajihf48efa02023-02-06 00:50:07 +0100236 struct andr_image_data img_data;
237 const void *p;
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200238
Safae Ouajihe0581762023-02-06 00:50:11 +0100239 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihf48efa02023-02-06 00:50:07 +0100240 return -EINVAL;
241
242 p = (const void *)img_data.kernel_ptr;
Simon Glassf3543e62022-09-06 20:26:52 -0600243 if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
244 return image_get_comp((struct legacy_img_hdr *)p);
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300245 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200246 return IH_COMP_LZ4;
247 else
Stephan Gerholdbc599042021-07-01 20:33:16 +0200248 return image_decomp_type(p, sizeof(u32));
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200249}
250
Safae Ouajihd71a7322023-02-06 00:50:03 +0100251int android_image_get_ramdisk(const struct andr_boot_img_hdr_v0 *hdr,
Safae Ouajihe0581762023-02-06 00:50:11 +0100252 const void *vendor_boot_img, ulong *rd_data, ulong *rd_len)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500253{
Safae Ouajih607b0752023-02-06 00:50:08 +0100254 struct andr_image_data img_data = {0};
255
Safae Ouajihe0581762023-02-06 00:50:11 +0100256 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100257 return -EINVAL;
258
259 if (!img_data.ramdisk_size) {
Rob Herring99500982015-10-05 14:37:07 -0500260 *rd_data = *rd_len = 0;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500261 return -1;
Rob Herring99500982015-10-05 14:37:07 -0500262 }
Ahmad Draidi86f46952014-10-23 20:50:07 +0300263
Safae Ouajih607b0752023-02-06 00:50:08 +0100264 printf("RAM disk load addr 0x%08lx size %u KiB\n",
265 img_data.ramdisk_ptr, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
Ahmad Draidi86f46952014-10-23 20:50:07 +0300266
Safae Ouajih607b0752023-02-06 00:50:08 +0100267 *rd_data = img_data.ramdisk_ptr;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500268
Safae Ouajih607b0752023-02-06 00:50:08 +0100269 *rd_len = img_data.ramdisk_size;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500270 return 0;
271}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200272
Safae Ouajihd71a7322023-02-06 00:50:03 +0100273int android_image_get_second(const struct andr_boot_img_hdr_v0 *hdr,
274 ulong *second_data, ulong *second_len)
Bin Chen10481612018-01-27 16:59:08 +1100275{
Safae Ouajih607b0752023-02-06 00:50:08 +0100276 struct andr_image_data img_data;
277
Safae Ouajihe0581762023-02-06 00:50:11 +0100278 if (!android_image_get_data(hdr, NULL, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100279 return -EINVAL;
280
281 if (!img_data.second_size) {
Bin Chen10481612018-01-27 16:59:08 +1100282 *second_data = *second_len = 0;
283 return -1;
284 }
285
Safae Ouajih607b0752023-02-06 00:50:08 +0100286 *second_data = img_data.second_ptr;
Bin Chen10481612018-01-27 16:59:08 +1100287
288 printf("second address is 0x%lx\n",*second_data);
289
Safae Ouajih607b0752023-02-06 00:50:08 +0100290 *second_len = img_data.second_size;
Bin Chen10481612018-01-27 16:59:08 +1100291 return 0;
292}
293
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200294/**
Sam Protsenko7f253152020-01-24 17:53:41 +0200295 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
296 * @hdr_addr: Boot image header address
297 * @addr: If not NULL, will contain address of recovery DTBO image
298 * @size: If not NULL, will contain size of recovery DTBO image
299 *
300 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
301 * Boot Image in RAM. The format of this image is Android DTBO (see
302 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
303 * the address is obtained from this function, one can use 'adtimg' U-Boot
304 * command or android_dt_*() functions to extract desired DTBO blob.
305 *
306 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
307 * only can be found in recovery image. On A/B devices we can always rely on
308 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
309 * AOSP documentation for details.
310 *
311 * Return: true on success or false on error.
312 */
313bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
314{
Safae Ouajihd71a7322023-02-06 00:50:03 +0100315 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko7f253152020-01-24 17:53:41 +0200316 ulong dtbo_img_addr;
317 bool ret = true;
318
319 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih734cb472023-02-06 00:50:05 +0100320 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko7f253152020-01-24 17:53:41 +0200321 printf("Error: Boot Image header is incorrect\n");
322 ret = false;
323 goto exit;
324 }
325
Safae Ouajih447240e2023-02-06 00:50:10 +0100326 if (hdr->header_version != 1 && hdr->header_version != 2) {
327 printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
Sam Protsenko7f253152020-01-24 17:53:41 +0200328 ret = false;
329 goto exit;
330 }
331
332 if (hdr->recovery_dtbo_size == 0) {
333 printf("Error: recovery_dtbo_size is 0\n");
334 ret = false;
335 goto exit;
336 }
337
338 /* Calculate the address of DTB area in boot image */
339 dtbo_img_addr = hdr_addr;
340 dtbo_img_addr += hdr->page_size;
341 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
342 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
343 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
344
345 if (addr)
346 *addr = dtbo_img_addr;
347 if (size)
348 *size = hdr->recovery_dtbo_size;
349
350exit:
351 unmap_sysmem(hdr);
352 return ret;
353}
354
355/**
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200356 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
357 * @hdr_addr: Boot image header address
Safae Ouajihe0581762023-02-06 00:50:11 +0100358 * @vhdr_addr: Vendor Boot image header address
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200359 * @addr: Will contain the address of DTB area in boot image
360 *
361 * Return: true on success or false on fail.
362 */
Safae Ouajihe0581762023-02-06 00:50:11 +0100363static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200364{
Safae Ouajihd71a7322023-02-06 00:50:03 +0100365 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200366 ulong dtb_img_addr;
367 bool ret = true;
368
369 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih734cb472023-02-06 00:50:05 +0100370 if (!is_android_boot_image_header(hdr)) {
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200371 printf("Error: Boot Image header is incorrect\n");
372 ret = false;
373 goto exit;
374 }
375
376 if (hdr->header_version < 2) {
377 printf("Error: header_version must be >= 2 to get dtb\n");
378 ret = false;
379 goto exit;
380 }
381
382 if (hdr->dtb_size == 0) {
383 printf("Error: dtb_size is 0\n");
384 ret = false;
385 goto exit;
386 }
387
388 /* Calculate the address of DTB area in boot image */
389 dtb_img_addr = hdr_addr;
390 dtb_img_addr += hdr->page_size;
391 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
392 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
393 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
394 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
395
396 *addr = dtb_img_addr;
397
398exit:
399 unmap_sysmem(hdr);
400 return ret;
401}
402
403/**
404 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
405 * @hdr_addr: Boot image header address
Safae Ouajihe0581762023-02-06 00:50:11 +0100406 * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200407 * @index: Index of desired DTB in DTB area (starting from 0)
408 * @addr: If not NULL, will contain address to specified DTB
409 * @size: If not NULL, will contain size of specified DTB
410 *
411 * Get the address and size of DTB blob by its index in DTB area of Android
412 * Boot Image in RAM.
413 *
414 * Return: true on success or false on error.
415 */
Safae Ouajihe0581762023-02-06 00:50:11 +0100416bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
417 u32 index, ulong *addr, u32 *size)
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200418{
Safae Ouajih607b0752023-02-06 00:50:08 +0100419 struct andr_image_data img_data;
Safae Ouajihd71a7322023-02-06 00:50:03 +0100420 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajihe0581762023-02-06 00:50:11 +0100421 const struct andr_vnd_boot_img_hdr *vhdr;
Safae Ouajih607b0752023-02-06 00:50:08 +0100422
423 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajihe0581762023-02-06 00:50:11 +0100424 if (vendor_boot_img != -1)
425 vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
426 if (!android_image_get_data(hdr, vhdr, &img_data)) {
427 if (vendor_boot_img != -1)
428 unmap_sysmem(vhdr);
Safae Ouajih607b0752023-02-06 00:50:08 +0100429 unmap_sysmem(hdr);
430 return false;
431 }
Safae Ouajihe0581762023-02-06 00:50:11 +0100432 if (vendor_boot_img != -1)
433 unmap_sysmem(vhdr);
Safae Ouajih607b0752023-02-06 00:50:08 +0100434 unmap_sysmem(hdr);
435
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200436 ulong dtb_img_addr; /* address of DTB part in boot image */
437 u32 dtb_img_size; /* size of DTB payload in boot image */
438 ulong dtb_addr; /* address of DTB blob with specified index */
439 u32 i; /* index iterator */
440
Safae Ouajihe0581762023-02-06 00:50:11 +0100441 android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr);
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200442 /* Check if DTB area of boot image is in DTBO format */
443 if (android_dt_check_header(dtb_img_addr)) {
444 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
445 size);
446 }
447
448 /* Find out the address of DTB with specified index in concat blobs */
Safae Ouajih607b0752023-02-06 00:50:08 +0100449 dtb_img_size = img_data.dtb_size;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200450 i = 0;
451 dtb_addr = dtb_img_addr;
452 while (dtb_addr < dtb_img_addr + dtb_img_size) {
453 const struct fdt_header *fdt;
454 u32 dtb_size;
455
456 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
457 if (fdt_check_header(fdt) != 0) {
458 unmap_sysmem(fdt);
459 printf("Error: Invalid FDT header for index %u\n", i);
460 return false;
461 }
462
463 dtb_size = fdt_totalsize(fdt);
464 unmap_sysmem(fdt);
465
466 if (i == index) {
467 if (size)
468 *size = dtb_size;
469 if (addr)
470 *addr = dtb_addr;
471 return true;
472 }
473
474 dtb_addr += dtb_size;
475 ++i;
476 }
477
478 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
479 return false;
480}
481
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200482#if !defined(CONFIG_SPL_BUILD)
483/**
484 * android_print_contents - prints out the contents of the Android format image
485 * @hdr: pointer to the Android format image header
486 *
487 * android_print_contents() formats a multi line Android image contents
488 * description.
489 * The routine prints out Android image properties
490 *
491 * returns:
492 * no returned results
493 */
Safae Ouajihd71a7322023-02-06 00:50:03 +0100494void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200495{
Safae Ouajihbb5d6922023-02-06 00:50:09 +0100496 if (hdr->header_version >= 3) {
497 printf("Content print is not supported for boot image header version > 2");
498 return;
499 }
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200500 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo210a7172017-04-02 01:49:47 -0700501 /* os_version = ver << 11 | lvl */
502 u32 os_ver = hdr->os_version >> 11;
503 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200504
Sam Protsenkobb633632019-08-15 20:25:07 +0300505 printf("%skernel size: %x\n", p, hdr->kernel_size);
506 printf("%skernel address: %x\n", p, hdr->kernel_addr);
507 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
508 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
509 printf("%ssecond size: %x\n", p, hdr->second_size);
510 printf("%ssecond address: %x\n", p, hdr->second_addr);
511 printf("%stags address: %x\n", p, hdr->tags_addr);
512 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo210a7172017-04-02 01:49:47 -0700513 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
514 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenkobb633632019-08-15 20:25:07 +0300515 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo210a7172017-04-02 01:49:47 -0700516 p, hdr->os_version,
517 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
518 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenkobb633632019-08-15 20:25:07 +0300519 printf("%sname: %s\n", p, hdr->name);
520 printf("%scmdline: %s\n", p, hdr->cmdline);
521 printf("%sheader_version: %d\n", p, hdr->header_version);
522
523 if (hdr->header_version >= 1) {
524 printf("%srecovery dtbo size: %x\n", p,
525 hdr->recovery_dtbo_size);
526 printf("%srecovery dtbo offset: %llx\n", p,
527 hdr->recovery_dtbo_offset);
528 printf("%sheader size: %x\n", p,
529 hdr->header_size);
530 }
531
Safae Ouajihbb5d6922023-02-06 00:50:09 +0100532 if (hdr->header_version == 2) {
Sam Protsenkobb633632019-08-15 20:25:07 +0300533 printf("%sdtb size: %x\n", p, hdr->dtb_size);
534 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
535 }
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200536}
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200537
538/**
539 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
540 * @fdt: DTB header
541 * @index: Number of DTB blob in DTB area.
542 *
543 * Return: true on success or false on error.
544 */
545static bool android_image_print_dtb_info(const struct fdt_header *fdt,
546 u32 index)
547{
548 int root_node_off;
549 u32 fdt_size;
550 const char *model;
551 const char *compatible;
552
553 root_node_off = fdt_path_offset(fdt, "/");
554 if (root_node_off < 0) {
555 printf("Error: Root node not found\n");
556 return false;
557 }
558
559 fdt_size = fdt_totalsize(fdt);
560 compatible = fdt_getprop(fdt, root_node_off, "compatible",
561 NULL);
562 model = fdt_getprop(fdt, root_node_off, "model", NULL);
563
564 printf(" - DTB #%u:\n", index);
565 printf(" (DTB)size = %d\n", fdt_size);
566 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
567 printf(" (DTB)compatible = %s\n",
568 compatible ? compatible : "(unknown)");
569
570 return true;
571}
572
573/**
574 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
575 * @hdr_addr: Boot image header address
576 *
577 * DTB payload in Android Boot Image v2+ can be in one of following formats:
578 * 1. Concatenated DTB blobs
579 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
580 *
581 * This function does next:
582 * 1. Prints out the format used in DTB area
583 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
584 * each blob.
585 *
586 * Return: true on success or false on error.
587 */
588bool android_image_print_dtb_contents(ulong hdr_addr)
589{
Safae Ouajihd71a7322023-02-06 00:50:03 +0100590 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200591 bool res;
592 ulong dtb_img_addr; /* address of DTB part in boot image */
593 u32 dtb_img_size; /* size of DTB payload in boot image */
594 ulong dtb_addr; /* address of DTB blob with specified index */
595 u32 i; /* index iterator */
596
Safae Ouajihe0581762023-02-06 00:50:11 +0100597 res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200598 if (!res)
599 return false;
600
601 /* Check if DTB area of boot image is in DTBO format */
602 if (android_dt_check_header(dtb_img_addr)) {
603 printf("## DTB area contents (DTBO format):\n");
604 android_dt_print_contents(dtb_img_addr);
605 return true;
606 }
607
608 printf("## DTB area contents (concat format):\n");
609
610 /* Iterate over concatenated DTB blobs */
611 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
612 dtb_img_size = hdr->dtb_size;
613 unmap_sysmem(hdr);
614 i = 0;
615 dtb_addr = dtb_img_addr;
616 while (dtb_addr < dtb_img_addr + dtb_img_size) {
617 const struct fdt_header *fdt;
618 u32 dtb_size;
619
620 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
621 if (fdt_check_header(fdt) != 0) {
622 unmap_sysmem(fdt);
623 printf("Error: Invalid FDT header for index %u\n", i);
624 return false;
625 }
626
627 res = android_image_print_dtb_info(fdt, i);
628 if (!res) {
629 unmap_sysmem(fdt);
630 return false;
631 }
632
633 dtb_size = fdt_totalsize(fdt);
634 unmap_sysmem(fdt);
635 dtb_addr += dtb_size;
636 ++i;
637 }
638
639 return true;
640}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200641#endif