doc: fix mistyped "env flags" command
[pandora-u-boot.git] / boot / image-android.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
4  */
5
6 #include <common.h>
7 #include <env.h>
8 #include <image.h>
9 #include <image-android-dt.h>
10 #include <android_image.h>
11 #include <malloc.h>
12 #include <errno.h>
13 #include <asm/unaligned.h>
14 #include <mapmem.h>
15 #include <linux/libfdt.h>
16
17 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR       0x10008000
18
19 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
20
21 static 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
30 static 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
36 static 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
60 static void android_boot_image_v3_v4_parse_hdr(const struct andr_boot_img_hdr_v3 *hdr,
61                                                struct andr_image_data *data)
62 {
63         ulong end;
64
65         data->kcmdline = hdr->cmdline;
66         data->header_version = hdr->header_version;
67         data->ramdisk_ptr = env_get_ulong("ramdisk_addr_r", 16, 0);
68
69         /*
70          * The header takes a full page, the remaining components are aligned
71          * on page boundary.
72          */
73         end = (ulong)hdr;
74         end += ANDR_GKI_PAGE_SIZE;
75         data->kernel_ptr = end;
76         data->kernel_size = hdr->kernel_size;
77         end += ALIGN(hdr->kernel_size, ANDR_GKI_PAGE_SIZE);
78         data->ramdisk_size = hdr->ramdisk_size;
79         data->boot_ramdisk_size = hdr->ramdisk_size;
80         end += ALIGN(hdr->ramdisk_size, ANDR_GKI_PAGE_SIZE);
81
82         if (hdr->header_version > 3)
83                 end += ALIGN(hdr->signature_size, ANDR_GKI_PAGE_SIZE);
84
85         data->boot_img_total_size = end - (ulong)hdr;
86 }
87
88 static void android_vendor_boot_image_v3_v4_parse_hdr(const struct andr_vnd_boot_img_hdr
89                                                       *hdr, struct andr_image_data *data)
90 {
91         ulong end;
92
93         /*
94          * The header takes a full page, the remaining components are aligned
95          * on page boundary.
96          */
97         data->kcmdline_extra = hdr->cmdline;
98         data->tags_addr = hdr->tags_addr;
99         data->image_name = hdr->name;
100         data->kernel_addr = hdr->kernel_addr;
101         data->ramdisk_addr = hdr->ramdisk_addr;
102         data->dtb_load_addr = hdr->dtb_addr;
103         data->bootconfig_size = hdr->bootconfig_size;
104         end = (ulong)hdr;
105         end += hdr->page_size;
106         if (hdr->vendor_ramdisk_size) {
107                 data->vendor_ramdisk_ptr = end;
108                 data->vendor_ramdisk_size = hdr->vendor_ramdisk_size;
109                 data->ramdisk_size += hdr->vendor_ramdisk_size;
110                 end += ALIGN(hdr->vendor_ramdisk_size, hdr->page_size);
111         }
112
113         data->dtb_ptr = end;
114         data->dtb_size = hdr->dtb_size;
115
116         end += ALIGN(hdr->dtb_size, hdr->page_size);
117         end += ALIGN(hdr->vendor_ramdisk_table_size, hdr->page_size);
118         data->bootconfig_addr = end;
119         if (hdr->bootconfig_size) {
120                 data->bootconfig_size += add_trailer(data->bootconfig_addr,
121                                                      data->bootconfig_size);
122                 data->ramdisk_size += data->bootconfig_size;
123         }
124         end += ALIGN(data->bootconfig_size, hdr->page_size);
125         data->vendor_boot_img_total_size = end - (ulong)hdr;
126 }
127
128 static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
129                                                   struct andr_image_data *data)
130 {
131         ulong end;
132
133         data->image_name = hdr->name;
134         data->kcmdline = hdr->cmdline;
135         data->kernel_addr = hdr->kernel_addr;
136         data->ramdisk_addr = hdr->ramdisk_addr;
137         data->header_version = hdr->header_version;
138         data->dtb_load_addr = hdr->dtb_addr;
139
140         end = (ulong)hdr;
141
142         /*
143          * The header takes a full page, the remaining components are aligned
144          * on page boundary
145          */
146
147         end += hdr->page_size;
148
149         data->kernel_ptr = end;
150         data->kernel_size = hdr->kernel_size;
151         end += ALIGN(hdr->kernel_size, hdr->page_size);
152
153         data->ramdisk_ptr = end;
154         data->ramdisk_size = hdr->ramdisk_size;
155         end += ALIGN(hdr->ramdisk_size, hdr->page_size);
156
157         data->second_ptr = end;
158         data->second_size = hdr->second_size;
159         end += ALIGN(hdr->second_size, hdr->page_size);
160
161         if (hdr->header_version >= 1) {
162                 data->recovery_dtbo_ptr = end;
163                 data->recovery_dtbo_size = hdr->recovery_dtbo_size;
164                 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
165         }
166
167         if (hdr->header_version >= 2) {
168                 data->dtb_ptr = end;
169                 data->dtb_size = hdr->dtb_size;
170                 end += ALIGN(hdr->dtb_size, hdr->page_size);
171         }
172
173         data->boot_img_total_size = end - (ulong)hdr;
174 }
175
176 bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
177                             struct andr_image_data *data)
178 {
179         if (!boot_hdr || !data) {
180                 printf("boot_hdr or data params can't be NULL\n");
181                 return false;
182         }
183
184         if (!is_android_boot_image_header(boot_hdr)) {
185                 printf("Incorrect boot image header\n");
186                 return false;
187         }
188
189         if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2) {
190                 if (!vendor_boot_hdr) {
191                         printf("For boot header v3+ vendor boot image has to be provided\n");
192                         return false;
193                 }
194                 if (!is_android_vendor_boot_image_header(vendor_boot_hdr)) {
195                         printf("Incorrect vendor boot image header\n");
196                         return false;
197                 }
198                 android_boot_image_v3_v4_parse_hdr(boot_hdr, data);
199                 android_vendor_boot_image_v3_v4_parse_hdr(vendor_boot_hdr, data);
200         } else {
201                 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
202         }
203
204         return true;
205 }
206
207 static ulong android_image_get_kernel_addr(struct andr_image_data *img_data)
208 {
209         /*
210          * All the Android tools that generate a boot.img use this
211          * address as the default.
212          *
213          * Even though it doesn't really make a lot of sense, and it
214          * might be valid on some platforms, we treat that adress as
215          * the default value for this field, and try to execute the
216          * kernel in place in such a case.
217          *
218          * Otherwise, we will return the actual value set by the user.
219          */
220         if (img_data->kernel_addr  == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
221                 return img_data->kernel_ptr;
222
223         /*
224          * abootimg creates images where all load addresses are 0
225          * and we need to fix them.
226          */
227         if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
228                 return env_get_ulong("kernel_addr_r", 16, 0);
229
230         return img_data->kernel_addr;
231 }
232
233 /**
234  * android_image_get_kernel() - processes kernel part of Android boot images
235  * @hdr:        Pointer to boot image header, which is at the start
236  *                      of the image.
237  * @vendor_boot_img:    Pointer to vendor boot image header, which is at the
238  *                              start of the image.
239  * @verify:     Checksum verification flag. Currently unimplemented.
240  * @os_data:    Pointer to a ulong variable, will hold os data start
241  *                      address.
242  * @os_len:     Pointer to a ulong variable, will hold os data length.
243  *
244  * This function returns the os image's start address and length. Also,
245  * it appends the kernel command line to the bootargs env variable.
246  *
247  * Return: Zero, os start address and length on success,
248  *              otherwise on failure.
249  */
250 int android_image_get_kernel(const void *hdr,
251                              const void *vendor_boot_img, int verify,
252                              ulong *os_data, ulong *os_len)
253 {
254         struct andr_image_data img_data = {0};
255         u32 kernel_addr;
256         const struct legacy_img_hdr *ihdr;
257
258         if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
259                 return -EINVAL;
260
261         kernel_addr = android_image_get_kernel_addr(&img_data);
262         ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
263
264         /*
265          * Not all Android tools use the id field for signing the image with
266          * sha1 (or anything) so we don't check it. It is not obvious that the
267          * string is null terminated so we take care of this.
268          */
269         strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
270         andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
271         if (strlen(andr_tmp_str))
272                 printf("Android's image name: %s\n", andr_tmp_str);
273
274         printf("Kernel load addr 0x%08x size %u KiB\n",
275                kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
276
277         int len = 0;
278         if (*img_data.kcmdline) {
279                 printf("Kernel command line: %s\n", img_data.kcmdline);
280                 len += strlen(img_data.kcmdline);
281         }
282
283         if (img_data.kcmdline_extra) {
284                 printf("Kernel extra command line: %s\n", img_data.kcmdline_extra);
285                 len += strlen(img_data.kcmdline_extra);
286         }
287
288         char *bootargs = env_get("bootargs");
289         if (bootargs)
290                 len += strlen(bootargs);
291
292         char *newbootargs = malloc(len + 2);
293         if (!newbootargs) {
294                 puts("Error: malloc in android_image_get_kernel failed!\n");
295                 return -ENOMEM;
296         }
297         *newbootargs = '\0';
298
299         if (bootargs) {
300                 strcpy(newbootargs, bootargs);
301                 strcat(newbootargs, " ");
302         }
303
304         if (*img_data.kcmdline)
305                 strcat(newbootargs, img_data.kcmdline);
306
307         if (img_data.kcmdline_extra) {
308                 strcat(newbootargs, " ");
309                 strcat(newbootargs, img_data.kcmdline_extra);
310         }
311
312         env_set("bootargs", newbootargs);
313
314         if (os_data) {
315                 if (image_get_magic(ihdr) == IH_MAGIC) {
316                         *os_data = image_get_data(ihdr);
317                 } else {
318                         *os_data = img_data.kernel_ptr;
319                 }
320         }
321         if (os_len) {
322                 if (image_get_magic(ihdr) == IH_MAGIC)
323                         *os_len = image_get_data_size(ihdr);
324                 else
325                         *os_len = img_data.kernel_size;
326         }
327         return 0;
328 }
329
330 bool is_android_vendor_boot_image_header(const void *vendor_boot_img)
331 {
332         return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE);
333 }
334
335 bool is_android_boot_image_header(const void *hdr)
336 {
337         return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
338 }
339
340 ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
341                             const void *vendor_boot_img)
342 {
343         struct andr_image_data img_data;
344
345         if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
346                 return -EINVAL;
347
348         if (img_data.header_version > 2)
349                 return 0;
350
351         return img_data.boot_img_total_size;
352 }
353
354 ulong android_image_get_kload(const void *hdr,
355                               const void *vendor_boot_img)
356 {
357         struct andr_image_data img_data;
358
359         if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
360                 return -EINVAL;
361
362         return android_image_get_kernel_addr(&img_data);
363 }
364
365 ulong android_image_get_kcomp(const void *hdr,
366                               const void *vendor_boot_img)
367 {
368         struct andr_image_data img_data;
369         const void *p;
370
371         if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
372                 return -EINVAL;
373
374         p = (const void *)img_data.kernel_ptr;
375         if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
376                 return image_get_comp((struct legacy_img_hdr *)p);
377         else if (get_unaligned_le32(p) == LZ4F_MAGIC)
378                 return IH_COMP_LZ4;
379         else
380                 return image_decomp_type(p, sizeof(u32));
381 }
382
383 int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
384                               ulong *rd_data, ulong *rd_len)
385 {
386         struct andr_image_data img_data = {0};
387         ulong ramdisk_ptr;
388
389         if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
390                 return -EINVAL;
391
392         if (!img_data.ramdisk_size) {
393                 *rd_data = *rd_len = 0;
394                 return -1;
395         }
396         if (img_data.header_version > 2) {
397                 ramdisk_ptr = img_data.ramdisk_ptr;
398                 memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
399                        img_data.vendor_ramdisk_size);
400                 memcpy((void *)(ramdisk_ptr + img_data.vendor_ramdisk_size),
401                        (void *)img_data.ramdisk_ptr,
402                        img_data.boot_ramdisk_size);
403                 if (img_data.bootconfig_size) {
404                         memcpy((void *)
405                                (ramdisk_ptr + img_data.vendor_ramdisk_size +
406                                img_data.boot_ramdisk_size),
407                                (void *)img_data.bootconfig_addr,
408                                img_data.bootconfig_size);
409                 }
410         }
411
412         printf("RAM disk load addr 0x%08lx size %u KiB\n",
413                img_data.ramdisk_ptr, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
414
415         *rd_data = img_data.ramdisk_ptr;
416
417         *rd_len = img_data.ramdisk_size;
418         return 0;
419 }
420
421 int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len)
422 {
423         struct andr_image_data img_data;
424
425         if (!android_image_get_data(hdr, NULL, &img_data))
426                 return -EINVAL;
427
428         if (img_data.header_version > 2) {
429                 printf("Second stage bootloader is only supported for boot image version <= 2\n");
430                 return -EOPNOTSUPP;
431         }
432
433         if (!img_data.second_size) {
434                 *second_data = *second_len = 0;
435                 return -1;
436         }
437
438         *second_data = img_data.second_ptr;
439
440         printf("second address is 0x%lx\n",*second_data);
441
442         *second_len = img_data.second_size;
443         return 0;
444 }
445
446 /**
447  * android_image_get_dtbo() - Get address and size of recovery DTBO image.
448  * @hdr_addr: Boot image header address
449  * @addr: If not NULL, will contain address of recovery DTBO image
450  * @size: If not NULL, will contain size of recovery DTBO image
451  *
452  * Get the address and size of DTBO image in "Recovery DTBO" area of Android
453  * Boot Image in RAM. The format of this image is Android DTBO (see
454  * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
455  * the address is obtained from this function, one can use 'adtimg' U-Boot
456  * command or android_dt_*() functions to extract desired DTBO blob.
457  *
458  * This DTBO (included in boot image) is only needed for non-A/B devices, and it
459  * only can be found in recovery image. On A/B devices we can always rely on
460  * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
461  * AOSP documentation for details.
462  *
463  * Return: true on success or false on error.
464  */
465 bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
466 {
467         const struct andr_boot_img_hdr_v0 *hdr;
468         ulong dtbo_img_addr;
469         bool ret = true;
470
471         hdr = map_sysmem(hdr_addr, sizeof(*hdr));
472         if (!is_android_boot_image_header(hdr)) {
473                 printf("Error: Boot Image header is incorrect\n");
474                 ret = false;
475                 goto exit;
476         }
477
478         if (hdr->header_version != 1 && hdr->header_version != 2) {
479                 printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
480                 ret = false;
481                 goto exit;
482         }
483
484         if (hdr->recovery_dtbo_size == 0) {
485                 printf("Error: recovery_dtbo_size is 0\n");
486                 ret = false;
487                 goto exit;
488         }
489
490         /* Calculate the address of DTB area in boot image */
491         dtbo_img_addr = hdr_addr;
492         dtbo_img_addr += hdr->page_size;
493         dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
494         dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
495         dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
496
497         if (addr)
498                 *addr = dtbo_img_addr;
499         if (size)
500                 *size = hdr->recovery_dtbo_size;
501
502 exit:
503         unmap_sysmem(hdr);
504         return ret;
505 }
506
507 /**
508  * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
509  * @hdr_addr: Boot image header address
510  * @vhdr_addr: Vendor Boot image header address
511  * @addr: Will contain the address of DTB area in boot image
512  *
513  * Return: true on success or false on fail.
514  */
515 static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
516 {
517         const struct andr_boot_img_hdr_v0 *hdr;
518         const struct andr_vnd_boot_img_hdr *v_hdr;
519         ulong dtb_img_addr;
520         bool ret = true;
521
522         hdr = map_sysmem(hdr_addr, sizeof(*hdr));
523         if (!is_android_boot_image_header(hdr)) {
524                 printf("Error: Boot Image header is incorrect\n");
525                 ret = false;
526                 goto exit;
527         }
528
529         if (hdr->header_version < 2) {
530                 printf("Error: header_version must be >= 2 to get dtb\n");
531                 ret = false;
532                 goto exit;
533         }
534
535         if (hdr->header_version == 2) {
536                 if (!hdr->dtb_size) {
537                         printf("Error: dtb_size is 0\n");
538                         ret = false;
539                         goto exit;
540                 }
541                 /* Calculate the address of DTB area in boot image */
542                 dtb_img_addr = hdr_addr;
543                 dtb_img_addr += hdr->page_size;
544                 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
545                 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
546                 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
547                 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
548
549                 *addr = dtb_img_addr;
550         }
551
552         if (hdr->header_version > 2) {
553                 v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr));
554                 if (!v_hdr->dtb_size) {
555                         printf("Error: dtb_size is 0\n");
556                         ret = false;
557                         unmap_sysmem(v_hdr);
558                         goto exit;
559                 }
560                 /* Calculate the address of DTB area in boot image */
561                 dtb_img_addr = vhdr_addr;
562                 dtb_img_addr += v_hdr->page_size;
563                 if (v_hdr->vendor_ramdisk_size)
564                         dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size);
565                 *addr = dtb_img_addr;
566                 unmap_sysmem(v_hdr);
567                 goto exit;
568         }
569 exit:
570         unmap_sysmem(hdr);
571         return ret;
572 }
573
574 /**
575  * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
576  * @hdr_addr: Boot image header address
577  * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
578  * @index: Index of desired DTB in DTB area (starting from 0)
579  * @addr: If not NULL, will contain address to specified DTB
580  * @size: If not NULL, will contain size of specified DTB
581  *
582  * Get the address and size of DTB blob by its index in DTB area of Android
583  * Boot Image in RAM.
584  *
585  * Return: true on success or false on error.
586  */
587 bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
588                                     u32 index, ulong *addr, u32 *size)
589 {
590         struct andr_image_data img_data;
591         const struct andr_boot_img_hdr_v0 *hdr;
592         const struct andr_vnd_boot_img_hdr *vhdr;
593
594         hdr = map_sysmem(hdr_addr, sizeof(*hdr));
595         if (vendor_boot_img != -1)
596                 vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
597         if (!android_image_get_data(hdr, vhdr, &img_data)) {
598                 if (vendor_boot_img != -1)
599                         unmap_sysmem(vhdr);
600                 unmap_sysmem(hdr);
601                 return false;
602         }
603         if (vendor_boot_img != -1)
604                 unmap_sysmem(vhdr);
605         unmap_sysmem(hdr);
606
607         ulong dtb_img_addr;     /* address of DTB part in boot image */
608         u32 dtb_img_size;       /* size of DTB payload in boot image */
609         ulong dtb_addr;         /* address of DTB blob with specified index  */
610         u32 i;                  /* index iterator */
611
612         android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr);
613         /* Check if DTB area of boot image is in DTBO format */
614         if (android_dt_check_header(dtb_img_addr)) {
615                 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
616                                                    size);
617         }
618
619         /* Find out the address of DTB with specified index in concat blobs */
620         dtb_img_size = img_data.dtb_size;
621         i = 0;
622         dtb_addr = dtb_img_addr;
623         while (dtb_addr < dtb_img_addr + dtb_img_size) {
624                 const struct fdt_header *fdt;
625                 u32 dtb_size;
626
627                 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
628                 if (fdt_check_header(fdt) != 0) {
629                         unmap_sysmem(fdt);
630                         printf("Error: Invalid FDT header for index %u\n", i);
631                         return false;
632                 }
633
634                 dtb_size = fdt_totalsize(fdt);
635                 unmap_sysmem(fdt);
636
637                 if (i == index) {
638                         if (size)
639                                 *size = dtb_size;
640                         if (addr)
641                                 *addr = dtb_addr;
642                         return true;
643                 }
644
645                 dtb_addr += dtb_size;
646                 ++i;
647         }
648
649         printf("Error: Index is out of bounds (%u/%u)\n", index, i);
650         return false;
651 }
652
653 #if !defined(CONFIG_SPL_BUILD)
654 /**
655  * android_print_contents - prints out the contents of the Android format image
656  * @hdr: pointer to the Android format image header
657  *
658  * android_print_contents() formats a multi line Android image contents
659  * description.
660  * The routine prints out Android image properties
661  *
662  * returns:
663  *     no returned results
664  */
665 void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
666 {
667         if (hdr->header_version >= 3) {
668                 printf("Content print is not supported for boot image header version > 2");
669                 return;
670         }
671         const char * const p = IMAGE_INDENT_STRING;
672         /* os_version = ver << 11 | lvl */
673         u32 os_ver = hdr->os_version >> 11;
674         u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
675
676         printf("%skernel size:          %x\n", p, hdr->kernel_size);
677         printf("%skernel address:       %x\n", p, hdr->kernel_addr);
678         printf("%sramdisk size:         %x\n", p, hdr->ramdisk_size);
679         printf("%sramdisk address:      %x\n", p, hdr->ramdisk_addr);
680         printf("%ssecond size:          %x\n", p, hdr->second_size);
681         printf("%ssecond address:       %x\n", p, hdr->second_addr);
682         printf("%stags address:         %x\n", p, hdr->tags_addr);
683         printf("%spage size:            %x\n", p, hdr->page_size);
684         /* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
685          * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
686         printf("%sos_version:           %x (ver: %u.%u.%u, level: %u.%u)\n",
687                p, hdr->os_version,
688                (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
689                (os_lvl >> 4) + 2000, os_lvl & 0x0F);
690         printf("%sname:                 %s\n", p, hdr->name);
691         printf("%scmdline:              %s\n", p, hdr->cmdline);
692         printf("%sheader_version:       %d\n", p, hdr->header_version);
693
694         if (hdr->header_version >= 1) {
695                 printf("%srecovery dtbo size:   %x\n", p,
696                        hdr->recovery_dtbo_size);
697                 printf("%srecovery dtbo offset: %llx\n", p,
698                        hdr->recovery_dtbo_offset);
699                 printf("%sheader size:          %x\n", p,
700                        hdr->header_size);
701         }
702
703         if (hdr->header_version == 2) {
704                 printf("%sdtb size:             %x\n", p, hdr->dtb_size);
705                 printf("%sdtb addr:             %llx\n", p, hdr->dtb_addr);
706         }
707 }
708
709 /**
710  * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
711  * @fdt: DTB header
712  * @index: Number of DTB blob in DTB area.
713  *
714  * Return: true on success or false on error.
715  */
716 static bool android_image_print_dtb_info(const struct fdt_header *fdt,
717                                          u32 index)
718 {
719         int root_node_off;
720         u32 fdt_size;
721         const char *model;
722         const char *compatible;
723
724         root_node_off = fdt_path_offset(fdt, "/");
725         if (root_node_off < 0) {
726                 printf("Error: Root node not found\n");
727                 return false;
728         }
729
730         fdt_size = fdt_totalsize(fdt);
731         compatible = fdt_getprop(fdt, root_node_off, "compatible",
732                                  NULL);
733         model = fdt_getprop(fdt, root_node_off, "model", NULL);
734
735         printf(" - DTB #%u:\n", index);
736         printf("           (DTB)size = %d\n", fdt_size);
737         printf("          (DTB)model = %s\n", model ? model : "(unknown)");
738         printf("     (DTB)compatible = %s\n",
739                compatible ? compatible : "(unknown)");
740
741         return true;
742 }
743
744 /**
745  * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
746  * @hdr_addr: Boot image header address
747  *
748  * DTB payload in Android Boot Image v2+ can be in one of following formats:
749  *   1. Concatenated DTB blobs
750  *   2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
751  *
752  * This function does next:
753  *   1. Prints out the format used in DTB area
754  *   2. Iterates over all DTB blobs in DTB area and prints out the info for
755  *      each blob.
756  *
757  * Return: true on success or false on error.
758  */
759 bool android_image_print_dtb_contents(ulong hdr_addr)
760 {
761         const struct andr_boot_img_hdr_v0 *hdr;
762         bool res;
763         ulong dtb_img_addr;     /* address of DTB part in boot image */
764         u32 dtb_img_size;       /* size of DTB payload in boot image */
765         ulong dtb_addr;         /* address of DTB blob with specified index  */
766         u32 i;                  /* index iterator */
767
768         res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
769         if (!res)
770                 return false;
771
772         /* Check if DTB area of boot image is in DTBO format */
773         if (android_dt_check_header(dtb_img_addr)) {
774                 printf("## DTB area contents (DTBO format):\n");
775                 android_dt_print_contents(dtb_img_addr);
776                 return true;
777         }
778
779         printf("## DTB area contents (concat format):\n");
780
781         /* Iterate over concatenated DTB blobs */
782         hdr = map_sysmem(hdr_addr, sizeof(*hdr));
783         dtb_img_size = hdr->dtb_size;
784         unmap_sysmem(hdr);
785         i = 0;
786         dtb_addr = dtb_img_addr;
787         while (dtb_addr < dtb_img_addr + dtb_img_size) {
788                 const struct fdt_header *fdt;
789                 u32 dtb_size;
790
791                 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
792                 if (fdt_check_header(fdt) != 0) {
793                         unmap_sysmem(fdt);
794                         printf("Error: Invalid FDT header for index %u\n", i);
795                         return false;
796                 }
797
798                 res = android_image_print_dtb_info(fdt, i);
799                 if (!res) {
800                         unmap_sysmem(fdt);
801                         return false;
802                 }
803
804                 dtb_size = fdt_totalsize(fdt);
805                 unmap_sysmem(fdt);
806                 dtb_addr += dtb_size;
807                 ++i;
808         }
809
810         return true;
811 }
812 #endif