]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
9ace3fc8 SS |
2 | /* |
3 | * Copyright (c) 2011 Sebastian Andrzej Siewior <[email protected]> | |
9ace3fc8 SS |
4 | */ |
5 | ||
6 | #include <common.h> | |
9fb625ce | 7 | #include <env.h> |
9ace3fc8 | 8 | #include <image.h> |
c3bfad82 | 9 | #include <image-android-dt.h> |
9ace3fc8 | 10 | #include <android_image.h> |
86f4695b AD |
11 | #include <malloc.h> |
12 | #include <errno.h> | |
829ceb28 | 13 | #include <asm/unaligned.h> |
c3bfad82 | 14 | #include <mapmem.h> |
401d1c4f | 15 | #include <linux/libfdt.h> |
9ace3fc8 | 16 | |
87f02d5a MR |
17 | #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000 |
18 | ||
9ace3fc8 SS |
19 | static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; |
20 | ||
f48efa0e SO |
21 | static 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 | ||
69 | bool 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 | ||
607b0755 | 89 | static ulong android_image_get_kernel_addr(struct andr_image_data *img_data) |
87f02d5a MR |
90 | { |
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 | */ | |
607b0755 SO |
102 | if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) |
103 | return img_data->kernel_ptr; | |
87f02d5a | 104 | |
95712afc CG |
105 | /* |
106 | * abootimg creates images where all load addresses are 0 | |
107 | * and we need to fix them. | |
108 | */ | |
607b0755 | 109 | if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0) |
95712afc CG |
110 | return env_get_ulong("kernel_addr_r", 16, 0); |
111 | ||
607b0755 | 112 | return img_data->kernel_addr; |
87f02d5a MR |
113 | } |
114 | ||
86f4695b AD |
115 | /** |
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 | */ | |
d71a732a | 130 | int android_image_get_kernel(const struct andr_boot_img_hdr_v0 *hdr, int verify, |
9ace3fc8 SS |
131 | ulong *os_data, ulong *os_len) |
132 | { | |
607b0755 SO |
133 | 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; | |
87f02d5a | 142 | |
9ace3fc8 SS |
143 | /* |
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 | */ | |
607b0755 | 148 | strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE); |
9ace3fc8 SS |
149 | 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", | |
607b0755 | 154 | kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024)); |
86f4695b AD |
155 | |
156 | int len = 0; | |
607b0755 SO |
157 | if (*img_data.kcmdline) { |
158 | printf("Kernel command line: %s\n", img_data.kcmdline); | |
159 | len += strlen(img_data.kcmdline); | |
86f4695b AD |
160 | } |
161 | ||
00caae6d | 162 | char *bootargs = env_get("bootargs"); |
86f4695b AD |
163 | 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, " "); | |
9ace3fc8 | 176 | } |
607b0755 SO |
177 | |
178 | if (*img_data.kcmdline) | |
179 | strcat(newbootargs, img_data.kcmdline); | |
86f4695b | 180 | |
382bee57 | 181 | env_set("bootargs", newbootargs); |
9ace3fc8 SS |
182 | |
183 | if (os_data) { | |
39f790b0 RS |
184 | if (image_get_magic(ihdr) == IH_MAGIC) { |
185 | *os_data = image_get_data(ihdr); | |
186 | } else { | |
607b0755 | 187 | *os_data = img_data.kernel_ptr; |
39f790b0 RS |
188 | } |
189 | } | |
190 | if (os_len) { | |
191 | if (image_get_magic(ihdr) == IH_MAGIC) | |
192 | *os_len = image_get_data_size(ihdr); | |
193 | else | |
607b0755 | 194 | *os_len = img_data.kernel_size; |
9ace3fc8 | 195 | } |
9ace3fc8 SS |
196 | return 0; |
197 | } | |
198 | ||
734cb47d | 199 | bool is_android_boot_image_header(const struct andr_boot_img_hdr_v0 *hdr) |
9ace3fc8 | 200 | { |
734cb47d | 201 | return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE); |
9ace3fc8 SS |
202 | } |
203 | ||
d71a732a | 204 | ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr) |
9ace3fc8 | 205 | { |
607b0755 | 206 | struct andr_image_data img_data; |
9ace3fc8 | 207 | |
607b0755 SO |
208 | if (!android_image_get_data(hdr, &img_data)) |
209 | return -EINVAL; | |
bb63363f | 210 | |
607b0755 SO |
211 | if (img_data.header_version > 2) |
212 | return 0; | |
bb63363f | 213 | |
607b0755 | 214 | return img_data.boot_img_total_size; |
9ace3fc8 SS |
215 | } |
216 | ||
d71a732a | 217 | ulong android_image_get_kload(const struct andr_boot_img_hdr_v0 *hdr) |
9ace3fc8 | 218 | { |
607b0755 SO |
219 | 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); | |
9ace3fc8 SS |
225 | } |
226 | ||
d71a732a | 227 | ulong android_image_get_kcomp(const struct andr_boot_img_hdr_v0 *hdr) |
829ceb28 | 228 | { |
f48efa0e SO |
229 | struct andr_image_data img_data; |
230 | const void *p; | |
231 | ||
232 | if (!android_image_get_data(hdr, &img_data)) | |
233 | return -EINVAL; | |
829ceb28 | 234 | |
f48efa0e | 235 | p = (const void *)img_data.kernel_ptr; |
f3543e69 SG |
236 | if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC) |
237 | return image_get_comp((struct legacy_img_hdr *)p); | |
39f790b0 | 238 | else if (get_unaligned_le32(p) == LZ4F_MAGIC) |
829ceb28 ER |
239 | return IH_COMP_LZ4; |
240 | else | |
bc599042 | 241 | return image_decomp_type(p, sizeof(u32)); |
829ceb28 ER |
242 | } |
243 | ||
d71a732a | 244 | int android_image_get_ramdisk(const struct andr_boot_img_hdr_v0 *hdr, |
9ace3fc8 SS |
245 | ulong *rd_data, ulong *rd_len) |
246 | { | |
607b0755 SO |
247 | 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) { | |
9950098e | 253 | *rd_data = *rd_len = 0; |
9ace3fc8 | 254 | return -1; |
9950098e | 255 | } |
86f4695b | 256 | |
607b0755 SO |
257 | printf("RAM disk load addr 0x%08lx size %u KiB\n", |
258 | img_data.ramdisk_ptr, DIV_ROUND_UP(img_data.ramdisk_size, 1024)); | |
86f4695b | 259 | |
607b0755 | 260 | *rd_data = img_data.ramdisk_ptr; |
9ace3fc8 | 261 | |
607b0755 | 262 | *rd_len = img_data.ramdisk_size; |
9ace3fc8 SS |
263 | return 0; |
264 | } | |
4f1318b2 | 265 | |
d71a732a SO |
266 | int android_image_get_second(const struct andr_boot_img_hdr_v0 *hdr, |
267 | ulong *second_data, ulong *second_len) | |
10481614 | 268 | { |
607b0755 SO |
269 | 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) { | |
10481614 BC |
275 | *second_data = *second_len = 0; |
276 | return -1; | |
277 | } | |
278 | ||
607b0755 | 279 | *second_data = img_data.second_ptr; |
10481614 BC |
280 | |
281 | printf("second address is 0x%lx\n",*second_data); | |
282 | ||
607b0755 | 283 | *second_len = img_data.second_size; |
10481614 BC |
284 | return 0; |
285 | } | |
286 | ||
7f253150 SP |
287 | /** |
288 | * 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 | */ | |
306 | bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size) | |
307 | { | |
d71a732a | 308 | const struct andr_boot_img_hdr_v0 *hdr; |
7f253150 SP |
309 | ulong dtbo_img_addr; |
310 | bool ret = true; | |
311 | ||
312 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
734cb47d | 313 | if (!is_android_boot_image_header(hdr)) { |
7f253150 SP |
314 | printf("Error: Boot Image header is incorrect\n"); |
315 | ret = false; | |
316 | goto exit; | |
317 | } | |
318 | ||
447240e2 SO |
319 | if (hdr->header_version != 1 && hdr->header_version != 2) { |
320 | printf("Error: header version must be >= 1 and <= 2 to get dtbo\n"); | |
7f253150 SP |
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 | ||
343 | exit: | |
344 | unmap_sysmem(hdr); | |
345 | return ret; | |
346 | } | |
347 | ||
c3bfad82 SP |
348 | /** |
349 | * 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 | */ | |
355 | static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong *addr) | |
356 | { | |
d71a732a | 357 | const struct andr_boot_img_hdr_v0 *hdr; |
c3bfad82 SP |
358 | ulong dtb_img_addr; |
359 | bool ret = true; | |
360 | ||
361 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
734cb47d | 362 | if (!is_android_boot_image_header(hdr)) { |
c3bfad82 SP |
363 | 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 | ||
390 | exit: | |
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 | */ | |
407 | bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr, | |
408 | u32 *size) | |
409 | { | |
607b0755 | 410 | struct andr_image_data img_data; |
d71a732a | 411 | const struct andr_boot_img_hdr_v0 *hdr; |
607b0755 SO |
412 | |
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 | ||
c3bfad82 SP |
420 | 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 | ||
607b0755 | 425 | android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr); |
c3bfad82 SP |
426 | /* 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 */ | |
607b0755 | 433 | dtb_img_size = img_data.dtb_size; |
c3bfad82 SP |
434 | 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 | ||
4f1318b2 MT |
466 | #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 | */ | |
d71a732a | 478 | void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr) |
4f1318b2 | 479 | { |
bb5d6927 SO |
480 | if (hdr->header_version >= 3) { |
481 | printf("Content print is not supported for boot image header version > 2"); | |
482 | return; | |
483 | } | |
4f1318b2 | 484 | const char * const p = IMAGE_INDENT_STRING; |
210a7176 AD |
485 | /* os_version = ver << 11 | lvl */ |
486 | u32 os_ver = hdr->os_version >> 11; | |
487 | u32 os_lvl = hdr->os_version & ((1U << 11) - 1); | |
4f1318b2 | 488 | |
bb63363f SP |
489 | printf("%skernel size: %x\n", p, hdr->kernel_size); |
490 | printf("%skernel address: %x\n", p, hdr->kernel_addr); | |
491 | printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); | |
492 | printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr); | |
493 | printf("%ssecond size: %x\n", p, hdr->second_size); | |
494 | printf("%ssecond address: %x\n", p, hdr->second_addr); | |
495 | printf("%stags address: %x\n", p, hdr->tags_addr); | |
496 | printf("%spage size: %x\n", p, hdr->page_size); | |
210a7176 AD |
497 | /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) |
498 | * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ | |
bb63363f | 499 | printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", |
210a7176 AD |
500 | p, hdr->os_version, |
501 | (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, | |
502 | (os_lvl >> 4) + 2000, os_lvl & 0x0F); | |
bb63363f SP |
503 | printf("%sname: %s\n", p, hdr->name); |
504 | printf("%scmdline: %s\n", p, hdr->cmdline); | |
505 | printf("%sheader_version: %d\n", p, hdr->header_version); | |
506 | ||
507 | if (hdr->header_version >= 1) { | |
508 | printf("%srecovery dtbo size: %x\n", p, | |
509 | hdr->recovery_dtbo_size); | |
510 | printf("%srecovery dtbo offset: %llx\n", p, | |
511 | hdr->recovery_dtbo_offset); | |
512 | printf("%sheader size: %x\n", p, | |
513 | hdr->header_size); | |
514 | } | |
515 | ||
bb5d6927 | 516 | if (hdr->header_version == 2) { |
bb63363f SP |
517 | printf("%sdtb size: %x\n", p, hdr->dtb_size); |
518 | printf("%sdtb addr: %llx\n", p, hdr->dtb_addr); | |
519 | } | |
4f1318b2 | 520 | } |
c3bfad82 SP |
521 | |
522 | /** | |
523 | * android_image_print_dtb_info - Print info for one DTB blob in DTB area. | |
524 | * @fdt: DTB header | |
525 | * @index: Number of DTB blob in DTB area. | |
526 | * | |
527 | * Return: true on success or false on error. | |
528 | */ | |
529 | static bool android_image_print_dtb_info(const struct fdt_header *fdt, | |
530 | u32 index) | |
531 | { | |
532 | int root_node_off; | |
533 | u32 fdt_size; | |
534 | const char *model; | |
535 | const char *compatible; | |
536 | ||
537 | root_node_off = fdt_path_offset(fdt, "/"); | |
538 | if (root_node_off < 0) { | |
539 | printf("Error: Root node not found\n"); | |
540 | return false; | |
541 | } | |
542 | ||
543 | fdt_size = fdt_totalsize(fdt); | |
544 | compatible = fdt_getprop(fdt, root_node_off, "compatible", | |
545 | NULL); | |
546 | model = fdt_getprop(fdt, root_node_off, "model", NULL); | |
547 | ||
548 | printf(" - DTB #%u:\n", index); | |
549 | printf(" (DTB)size = %d\n", fdt_size); | |
550 | printf(" (DTB)model = %s\n", model ? model : "(unknown)"); | |
551 | printf(" (DTB)compatible = %s\n", | |
552 | compatible ? compatible : "(unknown)"); | |
553 | ||
554 | return true; | |
555 | } | |
556 | ||
557 | /** | |
558 | * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area. | |
559 | * @hdr_addr: Boot image header address | |
560 | * | |
561 | * DTB payload in Android Boot Image v2+ can be in one of following formats: | |
562 | * 1. Concatenated DTB blobs | |
563 | * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details) | |
564 | * | |
565 | * This function does next: | |
566 | * 1. Prints out the format used in DTB area | |
567 | * 2. Iterates over all DTB blobs in DTB area and prints out the info for | |
568 | * each blob. | |
569 | * | |
570 | * Return: true on success or false on error. | |
571 | */ | |
572 | bool android_image_print_dtb_contents(ulong hdr_addr) | |
573 | { | |
d71a732a | 574 | const struct andr_boot_img_hdr_v0 *hdr; |
c3bfad82 SP |
575 | bool res; |
576 | ulong dtb_img_addr; /* address of DTB part in boot image */ | |
577 | u32 dtb_img_size; /* size of DTB payload in boot image */ | |
578 | ulong dtb_addr; /* address of DTB blob with specified index */ | |
579 | u32 i; /* index iterator */ | |
580 | ||
581 | res = android_image_get_dtb_img_addr(hdr_addr, &dtb_img_addr); | |
582 | if (!res) | |
583 | return false; | |
584 | ||
585 | /* Check if DTB area of boot image is in DTBO format */ | |
586 | if (android_dt_check_header(dtb_img_addr)) { | |
587 | printf("## DTB area contents (DTBO format):\n"); | |
588 | android_dt_print_contents(dtb_img_addr); | |
589 | return true; | |
590 | } | |
591 | ||
592 | printf("## DTB area contents (concat format):\n"); | |
593 | ||
594 | /* Iterate over concatenated DTB blobs */ | |
595 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
596 | dtb_img_size = hdr->dtb_size; | |
597 | unmap_sysmem(hdr); | |
598 | i = 0; | |
599 | dtb_addr = dtb_img_addr; | |
600 | while (dtb_addr < dtb_img_addr + dtb_img_size) { | |
601 | const struct fdt_header *fdt; | |
602 | u32 dtb_size; | |
603 | ||
604 | fdt = map_sysmem(dtb_addr, sizeof(*fdt)); | |
605 | if (fdt_check_header(fdt) != 0) { | |
606 | unmap_sysmem(fdt); | |
607 | printf("Error: Invalid FDT header for index %u\n", i); | |
608 | return false; | |
609 | } | |
610 | ||
611 | res = android_image_print_dtb_info(fdt, i); | |
612 | if (!res) { | |
613 | unmap_sysmem(fdt); | |
614 | return false; | |
615 | } | |
616 | ||
617 | dtb_size = fdt_totalsize(fdt); | |
618 | unmap_sysmem(fdt); | |
619 | dtb_addr += dtb_size; | |
620 | ++i; | |
621 | } | |
622 | ||
623 | return true; | |
624 | } | |
4f1318b2 | 625 | #endif |