blob: 1cd2060bb3fdc85ff5f1dd81111ddc7663bf3b57 [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
Simon Glass5e6201b2019-08-01 09:46:51 -06006#include <env.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -05007#include <image.h>
Sam Protsenko4fabf402020-01-24 17:53:40 +02008#include <image-android-dt.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -05009#include <android_image.h>
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030010#include <malloc.h>
11#include <errno.h>
Eugeniu Rosca1403f392019-04-08 17:35:27 +020012#include <asm/unaligned.h>
Sam Protsenko4fabf402020-01-24 17:53:40 +020013#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <linux/libfdt.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -050015
Maxime Ripard17ef1f52015-04-24 12:53:12 +020016#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
Neil Armstrong04ba5572024-10-17 16:44:44 +020017#define ANDROID_IMAGE_DEFAULT_RAMDISK_ADDR 0x11000000
Maxime Ripard17ef1f52015-04-24 12:53:12 +020018
Sebastian Siewior686d6672014-05-05 15:08:09 -050019static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
20
Safae Ouajih313cc842023-02-06 00:50:18 +010021static ulong checksum(const unsigned char *buffer, ulong size)
22{
23 ulong sum = 0;
24
25 for (ulong i = 0; i < size; i++)
26 sum += buffer[i];
27 return sum;
28}
29
30static bool is_trailer_present(ulong bootconfig_end_addr)
31{
32 return !strncmp((char *)(bootconfig_end_addr - BOOTCONFIG_MAGIC_SIZE),
33 BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
34}
35
36static ulong add_trailer(ulong bootconfig_start_addr, ulong bootconfig_size)
37{
38 ulong end;
39 ulong sum;
40
41 if (!bootconfig_start_addr)
42 return -1;
43 if (!bootconfig_size)
44 return 0;
45
46 end = bootconfig_start_addr + bootconfig_size;
47 if (is_trailer_present(end))
48 return 0;
49
50 memcpy((void *)(end), &bootconfig_size, BOOTCONFIG_SIZE_SIZE);
51 sum = checksum((unsigned char *)bootconfig_start_addr, bootconfig_size);
52 memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE), &sum,
53 BOOTCONFIG_CHECKSUM_SIZE);
54 memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE + BOOTCONFIG_CHECKSUM_SIZE),
55 BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
56
57 return BOOTCONFIG_TRAILER_SIZE;
58}
59
Mattijs Korpershoek2b5c70a2024-07-10 10:40:02 +020060__weak ulong get_avendor_bootimg_addr(void)
61{
62 return -1;
63}
64
Safae Ouajih889005f2023-02-06 00:50:12 +010065static void android_boot_image_v3_v4_parse_hdr(const struct andr_boot_img_hdr_v3 *hdr,
66 struct andr_image_data *data)
67{
68 ulong end;
69
70 data->kcmdline = hdr->cmdline;
71 data->header_version = hdr->header_version;
72
73 /*
74 * The header takes a full page, the remaining components are aligned
75 * on page boundary.
76 */
77 end = (ulong)hdr;
78 end += ANDR_GKI_PAGE_SIZE;
79 data->kernel_ptr = end;
80 data->kernel_size = hdr->kernel_size;
81 end += ALIGN(hdr->kernel_size, ANDR_GKI_PAGE_SIZE);
Roman Stratiienko227b4fa2024-05-19 13:09:51 +000082 data->ramdisk_ptr = end;
Safae Ouajih889005f2023-02-06 00:50:12 +010083 data->ramdisk_size = hdr->ramdisk_size;
84 data->boot_ramdisk_size = hdr->ramdisk_size;
85 end += ALIGN(hdr->ramdisk_size, ANDR_GKI_PAGE_SIZE);
86
87 if (hdr->header_version > 3)
88 end += ALIGN(hdr->signature_size, ANDR_GKI_PAGE_SIZE);
89
90 data->boot_img_total_size = end - (ulong)hdr;
91}
92
93static void android_vendor_boot_image_v3_v4_parse_hdr(const struct andr_vnd_boot_img_hdr
94 *hdr, struct andr_image_data *data)
95{
96 ulong end;
97
98 /*
99 * The header takes a full page, the remaining components are aligned
100 * on page boundary.
101 */
Safae Ouajih6665296e2023-02-06 00:50:14 +0100102 data->kcmdline_extra = hdr->cmdline;
Safae Ouajih889005f2023-02-06 00:50:12 +0100103 data->tags_addr = hdr->tags_addr;
104 data->image_name = hdr->name;
105 data->kernel_addr = hdr->kernel_addr;
106 data->ramdisk_addr = hdr->ramdisk_addr;
107 data->dtb_load_addr = hdr->dtb_addr;
Safae Ouajih313cc842023-02-06 00:50:18 +0100108 data->bootconfig_size = hdr->bootconfig_size;
Safae Ouajih889005f2023-02-06 00:50:12 +0100109 end = (ulong)hdr;
110 end += hdr->page_size;
111 if (hdr->vendor_ramdisk_size) {
112 data->vendor_ramdisk_ptr = end;
113 data->vendor_ramdisk_size = hdr->vendor_ramdisk_size;
114 data->ramdisk_size += hdr->vendor_ramdisk_size;
115 end += ALIGN(hdr->vendor_ramdisk_size, hdr->page_size);
116 }
117
118 data->dtb_ptr = end;
119 data->dtb_size = hdr->dtb_size;
120
121 end += ALIGN(hdr->dtb_size, hdr->page_size);
122 end += ALIGN(hdr->vendor_ramdisk_table_size, hdr->page_size);
Safae Ouajih313cc842023-02-06 00:50:18 +0100123 data->bootconfig_addr = end;
124 if (hdr->bootconfig_size) {
125 data->bootconfig_size += add_trailer(data->bootconfig_addr,
126 data->bootconfig_size);
127 data->ramdisk_size += data->bootconfig_size;
128 }
129 end += ALIGN(data->bootconfig_size, hdr->page_size);
Safae Ouajih889005f2023-02-06 00:50:12 +0100130 data->vendor_boot_img_total_size = end - (ulong)hdr;
131}
132
Safae Ouajih027191d2023-02-06 00:50:07 +0100133static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
134 struct andr_image_data *data)
135{
136 ulong end;
137
138 data->image_name = hdr->name;
139 data->kcmdline = hdr->cmdline;
140 data->kernel_addr = hdr->kernel_addr;
141 data->ramdisk_addr = hdr->ramdisk_addr;
142 data->header_version = hdr->header_version;
143 data->dtb_load_addr = hdr->dtb_addr;
144
145 end = (ulong)hdr;
146
147 /*
148 * The header takes a full page, the remaining components are aligned
149 * on page boundary
150 */
151
152 end += hdr->page_size;
153
154 data->kernel_ptr = end;
155 data->kernel_size = hdr->kernel_size;
156 end += ALIGN(hdr->kernel_size, hdr->page_size);
157
158 data->ramdisk_ptr = end;
159 data->ramdisk_size = hdr->ramdisk_size;
160 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
161
162 data->second_ptr = end;
163 data->second_size = hdr->second_size;
164 end += ALIGN(hdr->second_size, hdr->page_size);
165
166 if (hdr->header_version >= 1) {
167 data->recovery_dtbo_ptr = end;
168 data->recovery_dtbo_size = hdr->recovery_dtbo_size;
169 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
170 }
171
172 if (hdr->header_version >= 2) {
173 data->dtb_ptr = end;
174 data->dtb_size = hdr->dtb_size;
175 end += ALIGN(hdr->dtb_size, hdr->page_size);
176 }
177
178 data->boot_img_total_size = end - (ulong)hdr;
179}
180
Julien Massonb238dee2024-11-21 11:59:55 +0100181bool android_image_get_bootimg_size(const void *hdr, u32 *boot_img_size)
182{
183 struct andr_image_data data;
184
185 if (!hdr || !boot_img_size) {
186 printf("hdr or boot_img_size can't be NULL\n");
187 return false;
188 }
189
190 if (!is_android_boot_image_header(hdr)) {
191 printf("Incorrect boot image header\n");
192 return false;
193 }
194
195 if (((struct andr_boot_img_hdr_v0 *)hdr)->header_version <= 2)
196 android_boot_image_v0_v1_v2_parse_hdr(hdr, &data);
197 else
198 android_boot_image_v3_v4_parse_hdr(hdr, &data);
199
200 *boot_img_size = data.boot_img_total_size;
201
202 return true;
203}
204
205bool android_image_get_vendor_bootimg_size(const void *hdr, u32 *vendor_boot_img_size)
206{
207 struct andr_image_data data;
208
209 if (!hdr || !vendor_boot_img_size) {
210 printf("hdr or vendor_boot_img_size can't be NULL\n");
211 return false;
212 }
213
214 if (!is_android_vendor_boot_image_header(hdr)) {
215 printf("Incorrect vendor boot image header\n");
216 return false;
217 }
218
219 android_vendor_boot_image_v3_v4_parse_hdr(hdr, &data);
220
221 *vendor_boot_img_size = data.vendor_boot_img_total_size;
222
223 return true;
224}
225
Safae Ouajihc60ae102023-02-06 00:50:11 +0100226bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
227 struct andr_image_data *data)
Safae Ouajih027191d2023-02-06 00:50:07 +0100228{
229 if (!boot_hdr || !data) {
230 printf("boot_hdr or data params can't be NULL\n");
231 return false;
232 }
233
234 if (!is_android_boot_image_header(boot_hdr)) {
235 printf("Incorrect boot image header\n");
236 return false;
237 }
238
Safae Ouajih889005f2023-02-06 00:50:12 +0100239 if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2) {
240 if (!vendor_boot_hdr) {
241 printf("For boot header v3+ vendor boot image has to be provided\n");
242 return false;
243 }
244 if (!is_android_vendor_boot_image_header(vendor_boot_hdr)) {
245 printf("Incorrect vendor boot image header\n");
246 return false;
247 }
248 android_boot_image_v3_v4_parse_hdr(boot_hdr, data);
249 android_vendor_boot_image_v3_v4_parse_hdr(vendor_boot_hdr, data);
250 } else {
Safae Ouajih027191d2023-02-06 00:50:07 +0100251 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
Safae Ouajih889005f2023-02-06 00:50:12 +0100252 }
Safae Ouajih027191d2023-02-06 00:50:07 +0100253
254 return true;
255}
256
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200257static ulong android_image_get_kernel_addr(struct andr_image_data *img_data,
258 ulong comp)
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200259{
260 /*
261 * All the Android tools that generate a boot.img use this
262 * address as the default.
263 *
264 * Even though it doesn't really make a lot of sense, and it
265 * might be valid on some platforms, we treat that adress as
266 * the default value for this field, and try to execute the
267 * kernel in place in such a case.
268 *
269 * Otherwise, we will return the actual value set by the user.
270 */
George Chan8549bb32025-05-18 19:16:18 +0800271 if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR ||
272 IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE_IGNORE_BLOB_ADDR)) {
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200273 if (comp == IH_COMP_NONE)
274 return img_data->kernel_ptr;
275 return env_get_ulong("kernel_addr_r", 16, 0);
276 }
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200277
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200278 /*
279 * abootimg creates images where all load addresses are 0
280 * and we need to fix them.
281 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100282 if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200283 return env_get_ulong("kernel_addr_r", 16, 0);
284
Safae Ouajihbced1042023-02-06 00:50:08 +0100285 return img_data->kernel_addr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200286}
287
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300288/**
289 * android_image_get_kernel() - processes kernel part of Android boot images
Safae Ouajihc60ae102023-02-06 00:50:11 +0100290 * @hdr: Pointer to boot image header, which is at the start
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300291 * of the image.
Safae Ouajihc60ae102023-02-06 00:50:11 +0100292 * @vendor_boot_img: Pointer to vendor boot image header, which is at the
293 * start of the image.
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300294 * @verify: Checksum verification flag. Currently unimplemented.
295 * @os_data: Pointer to a ulong variable, will hold os data start
296 * address.
297 * @os_len: Pointer to a ulong variable, will hold os data length.
298 *
299 * This function returns the os image's start address and length. Also,
300 * it appends the kernel command line to the bootargs env variable.
301 *
302 * Return: Zero, os start address and length on success,
303 * otherwise on failure.
304 */
Safae Ouajih51c981b2023-02-06 00:50:17 +0100305int android_image_get_kernel(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100306 const void *vendor_boot_img, int verify,
Sebastian Siewior686d6672014-05-05 15:08:09 -0500307 ulong *os_data, ulong *os_len)
308{
Safae Ouajihbced1042023-02-06 00:50:08 +0100309 struct andr_image_data img_data = {0};
Neil Armstrongfd0318b2024-10-17 16:44:42 +0200310 ulong kernel_addr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100311 const struct legacy_img_hdr *ihdr;
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200312 ulong comp;
Safae Ouajihbced1042023-02-06 00:50:08 +0100313
Safae Ouajihc60ae102023-02-06 00:50:11 +0100314 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100315 return -EINVAL;
316
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200317 comp = android_image_get_kcomp(hdr, vendor_boot_img);
318
319 kernel_addr = android_image_get_kernel_addr(&img_data, comp);
Safae Ouajihbced1042023-02-06 00:50:08 +0100320 ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200321
Sebastian Siewior686d6672014-05-05 15:08:09 -0500322 /*
323 * Not all Android tools use the id field for signing the image with
324 * sha1 (or anything) so we don't check it. It is not obvious that the
325 * string is null terminated so we take care of this.
326 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100327 strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500328 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
329 if (strlen(andr_tmp_str))
330 printf("Android's image name: %s\n", andr_tmp_str);
331
Neil Armstrongfd0318b2024-10-17 16:44:42 +0200332 printf("Kernel load addr 0x%08lx size %u KiB\n",
Safae Ouajihbced1042023-02-06 00:50:08 +0100333 kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300334
335 int len = 0;
Nicolas Belin5799ab12024-12-17 14:29:10 +0100336 char *bootargs = env_get("bootargs");
337
338 if (bootargs)
339 len += strlen(bootargs);
340
Aaron Kling22354ba2025-01-13 10:11:45 +0100341 if (img_data.kcmdline && *img_data.kcmdline) {
Safae Ouajihbced1042023-02-06 00:50:08 +0100342 printf("Kernel command line: %s\n", img_data.kcmdline);
Nicolas Belin5799ab12024-12-17 14:29:10 +0100343 len += strlen(img_data.kcmdline) + (len ? 1 : 0); /* +1 for extra space */
Sebastian Siewior686d6672014-05-05 15:08:09 -0500344 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300345
Aaron Kling22354ba2025-01-13 10:11:45 +0100346 if (img_data.kcmdline_extra && *img_data.kcmdline_extra) {
Safae Ouajih6665296e2023-02-06 00:50:14 +0100347 printf("Kernel extra command line: %s\n", img_data.kcmdline_extra);
Nicolas Belin5799ab12024-12-17 14:29:10 +0100348 len += strlen(img_data.kcmdline_extra) + (len ? 1 : 0); /* +1 for extra space */
Safae Ouajih6665296e2023-02-06 00:50:14 +0100349 }
350
Nicolas Belin5799ab12024-12-17 14:29:10 +0100351 char *newbootargs = malloc(len + 1); /* +1 for the '\0' */
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300352 if (!newbootargs) {
353 puts("Error: malloc in android_image_get_kernel failed!\n");
354 return -ENOMEM;
355 }
Nicolas Belin5799ab12024-12-17 14:29:10 +0100356 *newbootargs = '\0'; /* set to Null in case no components below are present */
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300357
Nicolas Belin5799ab12024-12-17 14:29:10 +0100358 if (bootargs)
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300359 strcpy(newbootargs, bootargs);
Safae Ouajihbced1042023-02-06 00:50:08 +0100360
Aaron Kling22354ba2025-01-13 10:11:45 +0100361 if (img_data.kcmdline && *img_data.kcmdline) {
Nicolas Belin5799ab12024-12-17 14:29:10 +0100362 if (*newbootargs) /* If there is something in newbootargs, a space is needed */
363 strcat(newbootargs, " ");
Safae Ouajihbced1042023-02-06 00:50:08 +0100364 strcat(newbootargs, img_data.kcmdline);
Nicolas Belin5799ab12024-12-17 14:29:10 +0100365 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300366
Aaron Kling22354ba2025-01-13 10:11:45 +0100367 if (img_data.kcmdline_extra && *img_data.kcmdline_extra) {
Nicolas Belin5799ab12024-12-17 14:29:10 +0100368 if (*newbootargs) /* If there is something in newbootargs, a space is needed */
369 strcat(newbootargs, " ");
Safae Ouajih6665296e2023-02-06 00:50:14 +0100370 strcat(newbootargs, img_data.kcmdline_extra);
371 }
372
Simon Glass6a38e412017-08-03 12:22:09 -0600373 env_set("bootargs", newbootargs);
Nicolas Belin52d1c2e2024-12-17 14:29:09 +0100374 free(newbootargs);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500375
376 if (os_data) {
Roman Stratiienko73268582019-06-03 15:38:13 +0300377 if (image_get_magic(ihdr) == IH_MAGIC) {
378 *os_data = image_get_data(ihdr);
379 } else {
Safae Ouajihbced1042023-02-06 00:50:08 +0100380 *os_data = img_data.kernel_ptr;
Roman Stratiienko73268582019-06-03 15:38:13 +0300381 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500382 }
Roman Stratiienko73268582019-06-03 15:38:13 +0300383 if (os_len) {
384 if (image_get_magic(ihdr) == IH_MAGIC)
385 *os_len = image_get_data_size(ihdr);
386 else
Safae Ouajihbced1042023-02-06 00:50:08 +0100387 *os_len = img_data.kernel_size;
Roman Stratiienko73268582019-06-03 15:38:13 +0300388 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500389 return 0;
390}
391
Safae Ouajih889005f2023-02-06 00:50:12 +0100392bool is_android_vendor_boot_image_header(const void *vendor_boot_img)
393{
394 return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE);
395}
396
Safae Ouajih51c981b2023-02-06 00:50:17 +0100397bool is_android_boot_image_header(const void *hdr)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500398{
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100399 return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500400}
401
Safae Ouajihc60ae102023-02-06 00:50:11 +0100402ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
403 const void *vendor_boot_img)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500404{
Safae Ouajihbced1042023-02-06 00:50:08 +0100405 struct andr_image_data img_data;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500406
Safae Ouajihc60ae102023-02-06 00:50:11 +0100407 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100408 return -EINVAL;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300409
Safae Ouajihbced1042023-02-06 00:50:08 +0100410 if (img_data.header_version > 2)
411 return 0;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300412
Safae Ouajihbced1042023-02-06 00:50:08 +0100413 return img_data.boot_img_total_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500414}
415
Safae Ouajih51c981b2023-02-06 00:50:17 +0100416ulong android_image_get_kload(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100417 const void *vendor_boot_img)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500418{
Safae Ouajihbced1042023-02-06 00:50:08 +0100419 struct andr_image_data img_data;
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200420 ulong comp;
Safae Ouajihbced1042023-02-06 00:50:08 +0100421
Safae Ouajihc60ae102023-02-06 00:50:11 +0100422 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100423 return -EINVAL;
424
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200425 comp = android_image_get_kcomp(hdr, vendor_boot_img);
426
427 return android_image_get_kernel_addr(&img_data, comp);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500428}
429
Safae Ouajih51c981b2023-02-06 00:50:17 +0100430ulong android_image_get_kcomp(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100431 const void *vendor_boot_img)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200432{
Safae Ouajih027191d2023-02-06 00:50:07 +0100433 struct andr_image_data img_data;
434 const void *p;
435
Safae Ouajihc60ae102023-02-06 00:50:11 +0100436 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih027191d2023-02-06 00:50:07 +0100437 return -EINVAL;
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200438
Safae Ouajih027191d2023-02-06 00:50:07 +0100439 p = (const void *)img_data.kernel_ptr;
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600440 if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
441 return image_get_comp((struct legacy_img_hdr *)p);
Roman Stratiienko73268582019-06-03 15:38:13 +0300442 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200443 return IH_COMP_LZ4;
444 else
Stephan Gerhold9e5111d2021-07-01 20:33:16 +0200445 return image_decomp_type(p, sizeof(u32));
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200446}
447
Safae Ouajih5b01a882023-02-06 00:50:13 +0100448int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
449 ulong *rd_data, ulong *rd_len)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500450{
Safae Ouajihbced1042023-02-06 00:50:08 +0100451 struct andr_image_data img_data = {0};
Safae Ouajih5b01a882023-02-06 00:50:13 +0100452 ulong ramdisk_ptr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100453
Safae Ouajihc60ae102023-02-06 00:50:11 +0100454 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100455 return -EINVAL;
456
Michael Walle955415b2024-07-29 23:36:57 +0200457 if (!img_data.ramdisk_size)
458 return -ENOENT;
Neil Armstrong04ba5572024-10-17 16:44:44 +0200459 /*
460 * Android tools can generate a boot.img with default load address
461 * or 0, even though it doesn't really make a lot of sense, and it
462 * might be valid on some platforms, we treat that address as
463 * the default value for this field, and try to pass ramdisk
464 * in place if possible.
465 */
Safae Ouajih5b01a882023-02-06 00:50:13 +0100466 if (img_data.header_version > 2) {
Neil Armstrong04ba5572024-10-17 16:44:44 +0200467 /* Ramdisk can't be used in-place, copy it to ramdisk_addr_r */
George Chan8549bb32025-05-18 19:16:18 +0800468 if (img_data.ramdisk_addr == ANDROID_IMAGE_DEFAULT_RAMDISK_ADDR ||
469 IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE_IGNORE_BLOB_ADDR)) {
Neil Armstrong04ba5572024-10-17 16:44:44 +0200470 ramdisk_ptr = env_get_ulong("ramdisk_addr_r", 16, 0);
471 if (!ramdisk_ptr) {
472 printf("Invalid ramdisk_addr_r to copy ramdisk into\n");
473 return -EINVAL;
474 }
475 } else {
476 ramdisk_ptr = img_data.ramdisk_addr;
477 }
478 *rd_data = ramdisk_ptr;
Safae Ouajih5b01a882023-02-06 00:50:13 +0100479 memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
480 img_data.vendor_ramdisk_size);
Roman Stratiienko227b4fa2024-05-19 13:09:51 +0000481 ramdisk_ptr += img_data.vendor_ramdisk_size;
482 memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr,
Safae Ouajih313cc842023-02-06 00:50:18 +0100483 img_data.boot_ramdisk_size);
Roman Stratiienko227b4fa2024-05-19 13:09:51 +0000484 ramdisk_ptr += img_data.boot_ramdisk_size;
Safae Ouajih313cc842023-02-06 00:50:18 +0100485 if (img_data.bootconfig_size) {
486 memcpy((void *)
Roman Stratiienko227b4fa2024-05-19 13:09:51 +0000487 (ramdisk_ptr), (void *)img_data.bootconfig_addr,
Safae Ouajih313cc842023-02-06 00:50:18 +0100488 img_data.bootconfig_size);
489 }
Mattijs Korpershoek91359972024-10-03 14:42:39 +0200490 } else {
Neil Armstrong04ba5572024-10-17 16:44:44 +0200491 /* Ramdisk can be used in-place, use current ptr */
492 if (img_data.ramdisk_addr == 0 ||
Eddie Kovskyc4fdd982025-05-21 15:26:59 -0600493 img_data.ramdisk_addr == ANDROID_IMAGE_DEFAULT_RAMDISK_ADDR ||
George Chan8549bb32025-05-18 19:16:18 +0800494 img_data.ramdisk_addr == img_data.kernel_addr ||
495 IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE_IGNORE_BLOB_ADDR)) {
Neil Armstrong04ba5572024-10-17 16:44:44 +0200496 *rd_data = img_data.ramdisk_ptr;
497 } else {
498 ramdisk_ptr = img_data.ramdisk_addr;
499 *rd_data = ramdisk_ptr;
500 memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr,
501 img_data.ramdisk_size);
502 }
Safae Ouajih5b01a882023-02-06 00:50:13 +0100503 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300504
Safae Ouajihbced1042023-02-06 00:50:08 +0100505 printf("RAM disk load addr 0x%08lx size %u KiB\n",
Neil Armstrong04ba5572024-10-17 16:44:44 +0200506 *rd_data, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
Sebastian Siewior686d6672014-05-05 15:08:09 -0500507
Safae Ouajihbced1042023-02-06 00:50:08 +0100508 *rd_len = img_data.ramdisk_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500509 return 0;
510}
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200511
Safae Ouajih51c981b2023-02-06 00:50:17 +0100512int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len)
Bin Chen909f1402018-01-27 16:59:08 +1100513{
Safae Ouajihbced1042023-02-06 00:50:08 +0100514 struct andr_image_data img_data;
515
Safae Ouajihc60ae102023-02-06 00:50:11 +0100516 if (!android_image_get_data(hdr, NULL, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100517 return -EINVAL;
518
Safae Ouajih51c981b2023-02-06 00:50:17 +0100519 if (img_data.header_version > 2) {
520 printf("Second stage bootloader is only supported for boot image version <= 2\n");
521 return -EOPNOTSUPP;
522 }
523
Safae Ouajihbced1042023-02-06 00:50:08 +0100524 if (!img_data.second_size) {
Bin Chen909f1402018-01-27 16:59:08 +1100525 *second_data = *second_len = 0;
526 return -1;
527 }
528
Safae Ouajihbced1042023-02-06 00:50:08 +0100529 *second_data = img_data.second_ptr;
Bin Chen909f1402018-01-27 16:59:08 +1100530
531 printf("second address is 0x%lx\n",*second_data);
532
Safae Ouajihbced1042023-02-06 00:50:08 +0100533 *second_len = img_data.second_size;
Bin Chen909f1402018-01-27 16:59:08 +1100534 return 0;
535}
536
Sam Protsenko4fabf402020-01-24 17:53:40 +0200537/**
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200538 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
539 * @hdr_addr: Boot image header address
540 * @addr: If not NULL, will contain address of recovery DTBO image
541 * @size: If not NULL, will contain size of recovery DTBO image
542 *
543 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
544 * Boot Image in RAM. The format of this image is Android DTBO (see
545 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
546 * the address is obtained from this function, one can use 'adtimg' U-Boot
547 * command or android_dt_*() functions to extract desired DTBO blob.
548 *
549 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
550 * only can be found in recovery image. On A/B devices we can always rely on
551 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
552 * AOSP documentation for details.
553 *
554 * Return: true on success or false on error.
555 */
556bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
557{
Safae Ouajih8656e382023-02-06 00:50:03 +0100558 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200559 ulong dtbo_img_addr;
560 bool ret = true;
561
562 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100563 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200564 printf("Error: Boot Image header is incorrect\n");
565 ret = false;
566 goto exit;
567 }
568
Safae Ouajih8d351582023-02-06 00:50:10 +0100569 if (hdr->header_version != 1 && hdr->header_version != 2) {
570 printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200571 ret = false;
572 goto exit;
573 }
574
575 if (hdr->recovery_dtbo_size == 0) {
576 printf("Error: recovery_dtbo_size is 0\n");
577 ret = false;
578 goto exit;
579 }
580
581 /* Calculate the address of DTB area in boot image */
582 dtbo_img_addr = hdr_addr;
583 dtbo_img_addr += hdr->page_size;
584 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
585 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
586 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
587
588 if (addr)
589 *addr = dtbo_img_addr;
590 if (size)
591 *size = hdr->recovery_dtbo_size;
592
593exit:
594 unmap_sysmem(hdr);
595 return ret;
596}
597
598/**
Sam Protsenko4fabf402020-01-24 17:53:40 +0200599 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
600 * @hdr_addr: Boot image header address
Safae Ouajihc60ae102023-02-06 00:50:11 +0100601 * @vhdr_addr: Vendor Boot image header address
Sam Protsenko4fabf402020-01-24 17:53:40 +0200602 * @addr: Will contain the address of DTB area in boot image
603 *
604 * Return: true on success or false on fail.
605 */
Safae Ouajihc60ae102023-02-06 00:50:11 +0100606static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
Sam Protsenko4fabf402020-01-24 17:53:40 +0200607{
Safae Ouajih8656e382023-02-06 00:50:03 +0100608 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100609 const struct andr_vnd_boot_img_hdr *v_hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200610 ulong dtb_img_addr;
611 bool ret = true;
612
613 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100614 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko4fabf402020-01-24 17:53:40 +0200615 printf("Error: Boot Image header is incorrect\n");
616 ret = false;
617 goto exit;
618 }
619
620 if (hdr->header_version < 2) {
621 printf("Error: header_version must be >= 2 to get dtb\n");
622 ret = false;
623 goto exit;
624 }
625
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100626 if (hdr->header_version == 2) {
627 if (!hdr->dtb_size) {
628 printf("Error: dtb_size is 0\n");
629 ret = false;
630 goto exit;
631 }
632 /* Calculate the address of DTB area in boot image */
633 dtb_img_addr = hdr_addr;
634 dtb_img_addr += hdr->page_size;
635 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
636 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
637 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
638 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200639
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100640 *addr = dtb_img_addr;
641 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200642
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100643 if (hdr->header_version > 2) {
644 v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr));
645 if (!v_hdr->dtb_size) {
646 printf("Error: dtb_size is 0\n");
647 ret = false;
648 unmap_sysmem(v_hdr);
649 goto exit;
650 }
651 /* Calculate the address of DTB area in boot image */
652 dtb_img_addr = vhdr_addr;
653 dtb_img_addr += v_hdr->page_size;
654 if (v_hdr->vendor_ramdisk_size)
655 dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size);
656 *addr = dtb_img_addr;
657 unmap_sysmem(v_hdr);
658 goto exit;
659 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200660exit:
661 unmap_sysmem(hdr);
662 return ret;
663}
664
665/**
666 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
667 * @hdr_addr: Boot image header address
Safae Ouajihc60ae102023-02-06 00:50:11 +0100668 * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
Sam Protsenko4fabf402020-01-24 17:53:40 +0200669 * @index: Index of desired DTB in DTB area (starting from 0)
670 * @addr: If not NULL, will contain address to specified DTB
671 * @size: If not NULL, will contain size of specified DTB
672 *
673 * Get the address and size of DTB blob by its index in DTB area of Android
674 * Boot Image in RAM.
675 *
676 * Return: true on success or false on error.
677 */
Safae Ouajihc60ae102023-02-06 00:50:11 +0100678bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
679 u32 index, ulong *addr, u32 *size)
Sam Protsenko4fabf402020-01-24 17:53:40 +0200680{
Safae Ouajihbced1042023-02-06 00:50:08 +0100681 struct andr_image_data img_data;
Safae Ouajih8656e382023-02-06 00:50:03 +0100682 const struct andr_boot_img_hdr_v0 *hdr;
Andrew Goodbody8df778b2025-06-26 17:38:55 +0100683 const struct andr_vnd_boot_img_hdr *vhdr = NULL;
Safae Ouajihbced1042023-02-06 00:50:08 +0100684
685 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajihc60ae102023-02-06 00:50:11 +0100686 if (vendor_boot_img != -1)
687 vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
688 if (!android_image_get_data(hdr, vhdr, &img_data)) {
689 if (vendor_boot_img != -1)
690 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +0100691 unmap_sysmem(hdr);
692 return false;
693 }
Safae Ouajihc60ae102023-02-06 00:50:11 +0100694 if (vendor_boot_img != -1)
695 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +0100696 unmap_sysmem(hdr);
697
Sam Protsenko4fabf402020-01-24 17:53:40 +0200698 ulong dtb_img_addr; /* address of DTB part in boot image */
699 u32 dtb_img_size; /* size of DTB payload in boot image */
700 ulong dtb_addr; /* address of DTB blob with specified index */
701 u32 i; /* index iterator */
702
Sam Dayda658ae2025-01-23 14:35:01 +0000703 if (!android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img,
704 &dtb_img_addr))
705 return false;
706
Sam Protsenko4fabf402020-01-24 17:53:40 +0200707 /* Check if DTB area of boot image is in DTBO format */
708 if (android_dt_check_header(dtb_img_addr)) {
709 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
710 size);
711 }
712
713 /* Find out the address of DTB with specified index in concat blobs */
Safae Ouajihbced1042023-02-06 00:50:08 +0100714 dtb_img_size = img_data.dtb_size;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200715 i = 0;
716 dtb_addr = dtb_img_addr;
717 while (dtb_addr < dtb_img_addr + dtb_img_size) {
718 const struct fdt_header *fdt;
719 u32 dtb_size;
720
721 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
722 if (fdt_check_header(fdt) != 0) {
723 unmap_sysmem(fdt);
724 printf("Error: Invalid FDT header for index %u\n", i);
725 return false;
726 }
727
728 dtb_size = fdt_totalsize(fdt);
729 unmap_sysmem(fdt);
730
731 if (i == index) {
732 if (size)
733 *size = dtb_size;
734 if (addr)
735 *addr = dtb_addr;
736 return true;
737 }
738
739 dtb_addr += dtb_size;
740 ++i;
741 }
742
743 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
744 return false;
745}
746
Simon Glass0e84d962024-09-29 19:49:50 -0600747#if !defined(CONFIG_XPL_BUILD)
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200748/**
749 * android_print_contents - prints out the contents of the Android format image
750 * @hdr: pointer to the Android format image header
751 *
752 * android_print_contents() formats a multi line Android image contents
753 * description.
754 * The routine prints out Android image properties
755 *
756 * returns:
757 * no returned results
758 */
Safae Ouajih8656e382023-02-06 00:50:03 +0100759void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200760{
Safae Ouajiha148a822023-02-06 00:50:09 +0100761 if (hdr->header_version >= 3) {
762 printf("Content print is not supported for boot image header version > 2");
763 return;
764 }
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200765 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo41f513a2017-04-02 01:49:47 -0700766 /* os_version = ver << 11 | lvl */
767 u32 os_ver = hdr->os_version >> 11;
768 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200769
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300770 printf("%skernel size: %x\n", p, hdr->kernel_size);
771 printf("%skernel address: %x\n", p, hdr->kernel_addr);
772 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
773 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
774 printf("%ssecond size: %x\n", p, hdr->second_size);
775 printf("%ssecond address: %x\n", p, hdr->second_addr);
776 printf("%stags address: %x\n", p, hdr->tags_addr);
777 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo41f513a2017-04-02 01:49:47 -0700778 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
779 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300780 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo41f513a2017-04-02 01:49:47 -0700781 p, hdr->os_version,
782 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
783 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300784 printf("%sname: %s\n", p, hdr->name);
785 printf("%scmdline: %s\n", p, hdr->cmdline);
786 printf("%sheader_version: %d\n", p, hdr->header_version);
787
788 if (hdr->header_version >= 1) {
789 printf("%srecovery dtbo size: %x\n", p,
790 hdr->recovery_dtbo_size);
791 printf("%srecovery dtbo offset: %llx\n", p,
792 hdr->recovery_dtbo_offset);
793 printf("%sheader size: %x\n", p,
794 hdr->header_size);
795 }
796
Safae Ouajiha148a822023-02-06 00:50:09 +0100797 if (hdr->header_version == 2) {
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300798 printf("%sdtb size: %x\n", p, hdr->dtb_size);
799 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
800 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200801}
802
803/**
804 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
805 * @fdt: DTB header
806 * @index: Number of DTB blob in DTB area.
807 *
808 * Return: true on success or false on error.
809 */
810static bool android_image_print_dtb_info(const struct fdt_header *fdt,
811 u32 index)
812{
813 int root_node_off;
814 u32 fdt_size;
815 const char *model;
816 const char *compatible;
817
818 root_node_off = fdt_path_offset(fdt, "/");
819 if (root_node_off < 0) {
820 printf("Error: Root node not found\n");
821 return false;
822 }
823
824 fdt_size = fdt_totalsize(fdt);
825 compatible = fdt_getprop(fdt, root_node_off, "compatible",
826 NULL);
827 model = fdt_getprop(fdt, root_node_off, "model", NULL);
828
829 printf(" - DTB #%u:\n", index);
830 printf(" (DTB)size = %d\n", fdt_size);
831 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
832 printf(" (DTB)compatible = %s\n",
833 compatible ? compatible : "(unknown)");
834
835 return true;
836}
837
838/**
839 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
840 * @hdr_addr: Boot image header address
841 *
842 * DTB payload in Android Boot Image v2+ can be in one of following formats:
843 * 1. Concatenated DTB blobs
844 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
845 *
846 * This function does next:
847 * 1. Prints out the format used in DTB area
848 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
849 * each blob.
850 *
851 * Return: true on success or false on error.
852 */
853bool android_image_print_dtb_contents(ulong hdr_addr)
854{
Safae Ouajih8656e382023-02-06 00:50:03 +0100855 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200856 bool res;
857 ulong dtb_img_addr; /* address of DTB part in boot image */
858 u32 dtb_img_size; /* size of DTB payload in boot image */
859 ulong dtb_addr; /* address of DTB blob with specified index */
860 u32 i; /* index iterator */
861
Safae Ouajihc60ae102023-02-06 00:50:11 +0100862 res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200863 if (!res)
864 return false;
865
866 /* Check if DTB area of boot image is in DTBO format */
867 if (android_dt_check_header(dtb_img_addr)) {
868 printf("## DTB area contents (DTBO format):\n");
869 android_dt_print_contents(dtb_img_addr);
870 return true;
871 }
872
873 printf("## DTB area contents (concat format):\n");
874
875 /* Iterate over concatenated DTB blobs */
876 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
877 dtb_img_size = hdr->dtb_size;
878 unmap_sysmem(hdr);
879 i = 0;
880 dtb_addr = dtb_img_addr;
881 while (dtb_addr < dtb_img_addr + dtb_img_size) {
882 const struct fdt_header *fdt;
883 u32 dtb_size;
884
885 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
886 if (fdt_check_header(fdt) != 0) {
887 unmap_sysmem(fdt);
888 printf("Error: Invalid FDT header for index %u\n", i);
889 return false;
890 }
891
892 res = android_image_print_dtb_info(fdt, i);
893 if (!res) {
894 unmap_sysmem(fdt);
895 return false;
896 }
897
898 dtb_size = fdt_totalsize(fdt);
899 unmap_sysmem(fdt);
900 dtb_addr += dtb_size;
901 ++i;
902 }
903
904 return true;
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200905}
906#endif