blob: 712d437766b35e2e5a4134a8b93f12a61a1896c6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05002/*
3 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05004 */
5
6#include <common.h>
Simon Glass9fb625c2019-08-01 09:46:51 -06007#include <env.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05008#include <image.h>
Sam Protsenkoc3bfad82020-01-24 17:53:40 +02009#include <image-android-dt.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050010#include <android_image.h>
Ahmad Draidi86f46952014-10-23 20:50:07 +030011#include <malloc.h>
12#include <errno.h>
Eugeniu Rosca829ceb22019-04-08 17:35:27 +020013#include <asm/unaligned.h>
Sam Protsenkoc3bfad82020-01-24 17:53:40 +020014#include <mapmem.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <linux/libfdt.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050016
Maxime Ripard87f02d52015-04-24 12:53:12 +020017#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
18
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050019static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
20
Safae Ouajih11150272023-02-06 00:50:12 +010021static void android_boot_image_v3_v4_parse_hdr(const struct andr_boot_img_hdr_v3 *hdr,
22 struct andr_image_data *data)
23{
24 ulong end;
25
26 data->kcmdline = hdr->cmdline;
27 data->header_version = hdr->header_version;
28
29 /*
30 * The header takes a full page, the remaining components are aligned
31 * on page boundary.
32 */
33 end = (ulong)hdr;
34 end += ANDR_GKI_PAGE_SIZE;
35 data->kernel_ptr = end;
36 data->kernel_size = hdr->kernel_size;
37 end += ALIGN(hdr->kernel_size, ANDR_GKI_PAGE_SIZE);
38 data->ramdisk_size = hdr->ramdisk_size;
39 data->boot_ramdisk_size = hdr->ramdisk_size;
40 end += ALIGN(hdr->ramdisk_size, ANDR_GKI_PAGE_SIZE);
41
42 if (hdr->header_version > 3)
43 end += ALIGN(hdr->signature_size, ANDR_GKI_PAGE_SIZE);
44
45 data->boot_img_total_size = end - (ulong)hdr;
46}
47
48static void android_vendor_boot_image_v3_v4_parse_hdr(const struct andr_vnd_boot_img_hdr
49 *hdr, struct andr_image_data *data)
50{
51 ulong end;
52
53 /*
54 * The header takes a full page, the remaining components are aligned
55 * on page boundary.
56 */
57 data->tags_addr = hdr->tags_addr;
58 data->image_name = hdr->name;
59 data->kernel_addr = hdr->kernel_addr;
60 data->ramdisk_addr = hdr->ramdisk_addr;
61 data->dtb_load_addr = hdr->dtb_addr;
62 end = (ulong)hdr;
63 end += hdr->page_size;
64 if (hdr->vendor_ramdisk_size) {
65 data->vendor_ramdisk_ptr = end;
66 data->vendor_ramdisk_size = hdr->vendor_ramdisk_size;
67 data->ramdisk_size += hdr->vendor_ramdisk_size;
68 end += ALIGN(hdr->vendor_ramdisk_size, hdr->page_size);
69 }
70
71 data->dtb_ptr = end;
72 data->dtb_size = hdr->dtb_size;
73
74 end += ALIGN(hdr->dtb_size, hdr->page_size);
75 end += ALIGN(hdr->vendor_ramdisk_table_size, hdr->page_size);
76 end += ALIGN(hdr->bootconfig_size, hdr->page_size);
77 data->vendor_boot_img_total_size = end - (ulong)hdr;
78}
79
Safae Ouajihf48efa02023-02-06 00:50:07 +010080static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
81 struct andr_image_data *data)
82{
83 ulong end;
84
85 data->image_name = hdr->name;
86 data->kcmdline = hdr->cmdline;
87 data->kernel_addr = hdr->kernel_addr;
88 data->ramdisk_addr = hdr->ramdisk_addr;
89 data->header_version = hdr->header_version;
90 data->dtb_load_addr = hdr->dtb_addr;
91
92 end = (ulong)hdr;
93
94 /*
95 * The header takes a full page, the remaining components are aligned
96 * on page boundary
97 */
98
99 end += hdr->page_size;
100
101 data->kernel_ptr = end;
102 data->kernel_size = hdr->kernel_size;
103 end += ALIGN(hdr->kernel_size, hdr->page_size);
104
105 data->ramdisk_ptr = end;
106 data->ramdisk_size = hdr->ramdisk_size;
107 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
108
109 data->second_ptr = end;
110 data->second_size = hdr->second_size;
111 end += ALIGN(hdr->second_size, hdr->page_size);
112
113 if (hdr->header_version >= 1) {
114 data->recovery_dtbo_ptr = end;
115 data->recovery_dtbo_size = hdr->recovery_dtbo_size;
116 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
117 }
118
119 if (hdr->header_version >= 2) {
120 data->dtb_ptr = end;
121 data->dtb_size = hdr->dtb_size;
122 end += ALIGN(hdr->dtb_size, hdr->page_size);
123 }
124
125 data->boot_img_total_size = end - (ulong)hdr;
126}
127
Safae Ouajihe0581762023-02-06 00:50:11 +0100128bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
129 struct andr_image_data *data)
Safae Ouajihf48efa02023-02-06 00:50:07 +0100130{
131 if (!boot_hdr || !data) {
132 printf("boot_hdr or data params can't be NULL\n");
133 return false;
134 }
135
136 if (!is_android_boot_image_header(boot_hdr)) {
137 printf("Incorrect boot image header\n");
138 return false;
139 }
140
Safae Ouajih11150272023-02-06 00:50:12 +0100141 if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2) {
142 if (!vendor_boot_hdr) {
143 printf("For boot header v3+ vendor boot image has to be provided\n");
144 return false;
145 }
146 if (!is_android_vendor_boot_image_header(vendor_boot_hdr)) {
147 printf("Incorrect vendor boot image header\n");
148 return false;
149 }
150 android_boot_image_v3_v4_parse_hdr(boot_hdr, data);
151 android_vendor_boot_image_v3_v4_parse_hdr(vendor_boot_hdr, data);
152 } else {
Safae Ouajihf48efa02023-02-06 00:50:07 +0100153 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
Safae Ouajih11150272023-02-06 00:50:12 +0100154 }
Safae Ouajihf48efa02023-02-06 00:50:07 +0100155
156 return true;
157}
158
Safae Ouajih607b0752023-02-06 00:50:08 +0100159static ulong android_image_get_kernel_addr(struct andr_image_data *img_data)
Maxime Ripard87f02d52015-04-24 12:53:12 +0200160{
161 /*
162 * All the Android tools that generate a boot.img use this
163 * address as the default.
164 *
165 * Even though it doesn't really make a lot of sense, and it
166 * might be valid on some platforms, we treat that adress as
167 * the default value for this field, and try to execute the
168 * kernel in place in such a case.
169 *
170 * Otherwise, we will return the actual value set by the user.
171 */
Safae Ouajih607b0752023-02-06 00:50:08 +0100172 if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
173 return img_data->kernel_ptr;
Maxime Ripard87f02d52015-04-24 12:53:12 +0200174
Christian Gmeiner95712af2020-05-29 17:53:45 +0200175 /*
176 * abootimg creates images where all load addresses are 0
177 * and we need to fix them.
178 */
Safae Ouajih607b0752023-02-06 00:50:08 +0100179 if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
Christian Gmeiner95712af2020-05-29 17:53:45 +0200180 return env_get_ulong("kernel_addr_r", 16, 0);
181
Safae Ouajih607b0752023-02-06 00:50:08 +0100182 return img_data->kernel_addr;
Maxime Ripard87f02d52015-04-24 12:53:12 +0200183}
184
Ahmad Draidi86f46952014-10-23 20:50:07 +0300185/**
186 * android_image_get_kernel() - processes kernel part of Android boot images
Safae Ouajihe0581762023-02-06 00:50:11 +0100187 * @hdr: Pointer to boot image header, which is at the start
Ahmad Draidi86f46952014-10-23 20:50:07 +0300188 * of the image.
Safae Ouajihe0581762023-02-06 00:50:11 +0100189 * @vendor_boot_img: Pointer to vendor boot image header, which is at the
190 * start of the image.
Ahmad Draidi86f46952014-10-23 20:50:07 +0300191 * @verify: Checksum verification flag. Currently unimplemented.
192 * @os_data: Pointer to a ulong variable, will hold os data start
193 * address.
194 * @os_len: Pointer to a ulong variable, will hold os data length.
195 *
196 * This function returns the os image's start address and length. Also,
197 * it appends the kernel command line to the bootargs env variable.
198 *
199 * Return: Zero, os start address and length on success,
200 * otherwise on failure.
201 */
Safae Ouajihe0581762023-02-06 00:50:11 +0100202int android_image_get_kernel(const struct andr_boot_img_hdr_v0 *hdr,
203 const void *vendor_boot_img, int verify,
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500204 ulong *os_data, ulong *os_len)
205{
Safae Ouajih607b0752023-02-06 00:50:08 +0100206 struct andr_image_data img_data = {0};
207 u32 kernel_addr;
208 const struct legacy_img_hdr *ihdr;
209
Safae Ouajihe0581762023-02-06 00:50:11 +0100210 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100211 return -EINVAL;
212
213 kernel_addr = android_image_get_kernel_addr(&img_data);
214 ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
Maxime Ripard87f02d52015-04-24 12:53:12 +0200215
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500216 /*
217 * Not all Android tools use the id field for signing the image with
218 * sha1 (or anything) so we don't check it. It is not obvious that the
219 * string is null terminated so we take care of this.
220 */
Safae Ouajih607b0752023-02-06 00:50:08 +0100221 strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500222 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
223 if (strlen(andr_tmp_str))
224 printf("Android's image name: %s\n", andr_tmp_str);
225
226 printf("Kernel load addr 0x%08x size %u KiB\n",
Safae Ouajih607b0752023-02-06 00:50:08 +0100227 kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
Ahmad Draidi86f46952014-10-23 20:50:07 +0300228
229 int len = 0;
Safae Ouajih607b0752023-02-06 00:50:08 +0100230 if (*img_data.kcmdline) {
231 printf("Kernel command line: %s\n", img_data.kcmdline);
232 len += strlen(img_data.kcmdline);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500233 }
Ahmad Draidi86f46952014-10-23 20:50:07 +0300234
Simon Glass00caae62017-08-03 12:22:12 -0600235 char *bootargs = env_get("bootargs");
Ahmad Draidi86f46952014-10-23 20:50:07 +0300236 if (bootargs)
237 len += strlen(bootargs);
238
239 char *newbootargs = malloc(len + 2);
240 if (!newbootargs) {
241 puts("Error: malloc in android_image_get_kernel failed!\n");
242 return -ENOMEM;
243 }
244 *newbootargs = '\0';
245
246 if (bootargs) {
247 strcpy(newbootargs, bootargs);
248 strcat(newbootargs, " ");
249 }
Safae Ouajih607b0752023-02-06 00:50:08 +0100250
251 if (*img_data.kcmdline)
252 strcat(newbootargs, img_data.kcmdline);
Ahmad Draidi86f46952014-10-23 20:50:07 +0300253
Simon Glass382bee52017-08-03 12:22:09 -0600254 env_set("bootargs", newbootargs);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500255
256 if (os_data) {
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300257 if (image_get_magic(ihdr) == IH_MAGIC) {
258 *os_data = image_get_data(ihdr);
259 } else {
Safae Ouajih607b0752023-02-06 00:50:08 +0100260 *os_data = img_data.kernel_ptr;
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300261 }
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500262 }
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300263 if (os_len) {
264 if (image_get_magic(ihdr) == IH_MAGIC)
265 *os_len = image_get_data_size(ihdr);
266 else
Safae Ouajih607b0752023-02-06 00:50:08 +0100267 *os_len = img_data.kernel_size;
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300268 }
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500269 return 0;
270}
271
Safae Ouajih11150272023-02-06 00:50:12 +0100272bool is_android_vendor_boot_image_header(const void *vendor_boot_img)
273{
274 return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE);
275}
276
Safae Ouajih734cb472023-02-06 00:50:05 +0100277bool is_android_boot_image_header(const struct andr_boot_img_hdr_v0 *hdr)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500278{
Safae Ouajih734cb472023-02-06 00:50:05 +0100279 return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500280}
281
Safae Ouajihe0581762023-02-06 00:50:11 +0100282ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
283 const void *vendor_boot_img)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500284{
Safae Ouajih607b0752023-02-06 00:50:08 +0100285 struct andr_image_data img_data;
Sam Protsenkobb633632019-08-15 20:25:07 +0300286
Safae Ouajihe0581762023-02-06 00:50:11 +0100287 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100288 return -EINVAL;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500289
Safae Ouajih607b0752023-02-06 00:50:08 +0100290 if (img_data.header_version > 2)
291 return 0;
Sam Protsenkobb633632019-08-15 20:25:07 +0300292
Safae Ouajih607b0752023-02-06 00:50:08 +0100293 return img_data.boot_img_total_size;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500294}
295
Safae Ouajihe0581762023-02-06 00:50:11 +0100296ulong android_image_get_kload(const struct andr_boot_img_hdr_v0 *hdr,
297 const void *vendor_boot_img)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500298{
Safae Ouajih607b0752023-02-06 00:50:08 +0100299 struct andr_image_data img_data;
300
Safae Ouajihe0581762023-02-06 00:50:11 +0100301 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100302 return -EINVAL;
303
304 return android_image_get_kernel_addr(&img_data);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500305}
306
Safae Ouajihe0581762023-02-06 00:50:11 +0100307ulong android_image_get_kcomp(const struct andr_boot_img_hdr_v0 *hdr,
308 const void *vendor_boot_img)
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200309{
Safae Ouajihf48efa02023-02-06 00:50:07 +0100310 struct andr_image_data img_data;
311 const void *p;
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200312
Safae Ouajihe0581762023-02-06 00:50:11 +0100313 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihf48efa02023-02-06 00:50:07 +0100314 return -EINVAL;
315
316 p = (const void *)img_data.kernel_ptr;
Simon Glassf3543e62022-09-06 20:26:52 -0600317 if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
318 return image_get_comp((struct legacy_img_hdr *)p);
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300319 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200320 return IH_COMP_LZ4;
321 else
Stephan Gerholdbc599042021-07-01 20:33:16 +0200322 return image_decomp_type(p, sizeof(u32));
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200323}
324
Safae Ouajihd71a7322023-02-06 00:50:03 +0100325int android_image_get_ramdisk(const struct andr_boot_img_hdr_v0 *hdr,
Safae Ouajihe0581762023-02-06 00:50:11 +0100326 const void *vendor_boot_img, ulong *rd_data, ulong *rd_len)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500327{
Safae Ouajih607b0752023-02-06 00:50:08 +0100328 struct andr_image_data img_data = {0};
329
Safae Ouajihe0581762023-02-06 00:50:11 +0100330 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100331 return -EINVAL;
332
333 if (!img_data.ramdisk_size) {
Rob Herring99500982015-10-05 14:37:07 -0500334 *rd_data = *rd_len = 0;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500335 return -1;
Rob Herring99500982015-10-05 14:37:07 -0500336 }
Ahmad Draidi86f46952014-10-23 20:50:07 +0300337
Safae Ouajih607b0752023-02-06 00:50:08 +0100338 printf("RAM disk load addr 0x%08lx size %u KiB\n",
339 img_data.ramdisk_ptr, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
Ahmad Draidi86f46952014-10-23 20:50:07 +0300340
Safae Ouajih607b0752023-02-06 00:50:08 +0100341 *rd_data = img_data.ramdisk_ptr;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500342
Safae Ouajih607b0752023-02-06 00:50:08 +0100343 *rd_len = img_data.ramdisk_size;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500344 return 0;
345}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200346
Safae Ouajihd71a7322023-02-06 00:50:03 +0100347int android_image_get_second(const struct andr_boot_img_hdr_v0 *hdr,
348 ulong *second_data, ulong *second_len)
Bin Chen10481612018-01-27 16:59:08 +1100349{
Safae Ouajih607b0752023-02-06 00:50:08 +0100350 struct andr_image_data img_data;
351
Safae Ouajihe0581762023-02-06 00:50:11 +0100352 if (!android_image_get_data(hdr, NULL, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100353 return -EINVAL;
354
355 if (!img_data.second_size) {
Bin Chen10481612018-01-27 16:59:08 +1100356 *second_data = *second_len = 0;
357 return -1;
358 }
359
Safae Ouajih607b0752023-02-06 00:50:08 +0100360 *second_data = img_data.second_ptr;
Bin Chen10481612018-01-27 16:59:08 +1100361
362 printf("second address is 0x%lx\n",*second_data);
363
Safae Ouajih607b0752023-02-06 00:50:08 +0100364 *second_len = img_data.second_size;
Bin Chen10481612018-01-27 16:59:08 +1100365 return 0;
366}
367
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200368/**
Sam Protsenko7f253152020-01-24 17:53:41 +0200369 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
370 * @hdr_addr: Boot image header address
371 * @addr: If not NULL, will contain address of recovery DTBO image
372 * @size: If not NULL, will contain size of recovery DTBO image
373 *
374 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
375 * Boot Image in RAM. The format of this image is Android DTBO (see
376 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
377 * the address is obtained from this function, one can use 'adtimg' U-Boot
378 * command or android_dt_*() functions to extract desired DTBO blob.
379 *
380 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
381 * only can be found in recovery image. On A/B devices we can always rely on
382 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
383 * AOSP documentation for details.
384 *
385 * Return: true on success or false on error.
386 */
387bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
388{
Safae Ouajihd71a7322023-02-06 00:50:03 +0100389 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko7f253152020-01-24 17:53:41 +0200390 ulong dtbo_img_addr;
391 bool ret = true;
392
393 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih734cb472023-02-06 00:50:05 +0100394 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko7f253152020-01-24 17:53:41 +0200395 printf("Error: Boot Image header is incorrect\n");
396 ret = false;
397 goto exit;
398 }
399
Safae Ouajih447240e2023-02-06 00:50:10 +0100400 if (hdr->header_version != 1 && hdr->header_version != 2) {
401 printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
Sam Protsenko7f253152020-01-24 17:53:41 +0200402 ret = false;
403 goto exit;
404 }
405
406 if (hdr->recovery_dtbo_size == 0) {
407 printf("Error: recovery_dtbo_size is 0\n");
408 ret = false;
409 goto exit;
410 }
411
412 /* Calculate the address of DTB area in boot image */
413 dtbo_img_addr = hdr_addr;
414 dtbo_img_addr += hdr->page_size;
415 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
416 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
417 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
418
419 if (addr)
420 *addr = dtbo_img_addr;
421 if (size)
422 *size = hdr->recovery_dtbo_size;
423
424exit:
425 unmap_sysmem(hdr);
426 return ret;
427}
428
429/**
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200430 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
431 * @hdr_addr: Boot image header address
Safae Ouajihe0581762023-02-06 00:50:11 +0100432 * @vhdr_addr: Vendor Boot image header address
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200433 * @addr: Will contain the address of DTB area in boot image
434 *
435 * Return: true on success or false on fail.
436 */
Safae Ouajihe0581762023-02-06 00:50:11 +0100437static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200438{
Safae Ouajihd71a7322023-02-06 00:50:03 +0100439 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200440 ulong dtb_img_addr;
441 bool ret = true;
442
443 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih734cb472023-02-06 00:50:05 +0100444 if (!is_android_boot_image_header(hdr)) {
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200445 printf("Error: Boot Image header is incorrect\n");
446 ret = false;
447 goto exit;
448 }
449
450 if (hdr->header_version < 2) {
451 printf("Error: header_version must be >= 2 to get dtb\n");
452 ret = false;
453 goto exit;
454 }
455
456 if (hdr->dtb_size == 0) {
457 printf("Error: dtb_size is 0\n");
458 ret = false;
459 goto exit;
460 }
461
462 /* Calculate the address of DTB area in boot image */
463 dtb_img_addr = hdr_addr;
464 dtb_img_addr += hdr->page_size;
465 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
466 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
467 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
468 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
469
470 *addr = dtb_img_addr;
471
472exit:
473 unmap_sysmem(hdr);
474 return ret;
475}
476
477/**
478 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
479 * @hdr_addr: Boot image header address
Safae Ouajihe0581762023-02-06 00:50:11 +0100480 * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200481 * @index: Index of desired DTB in DTB area (starting from 0)
482 * @addr: If not NULL, will contain address to specified DTB
483 * @size: If not NULL, will contain size of specified DTB
484 *
485 * Get the address and size of DTB blob by its index in DTB area of Android
486 * Boot Image in RAM.
487 *
488 * Return: true on success or false on error.
489 */
Safae Ouajihe0581762023-02-06 00:50:11 +0100490bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
491 u32 index, ulong *addr, u32 *size)
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200492{
Safae Ouajih607b0752023-02-06 00:50:08 +0100493 struct andr_image_data img_data;
Safae Ouajihd71a7322023-02-06 00:50:03 +0100494 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajihe0581762023-02-06 00:50:11 +0100495 const struct andr_vnd_boot_img_hdr *vhdr;
Safae Ouajih607b0752023-02-06 00:50:08 +0100496
497 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajihe0581762023-02-06 00:50:11 +0100498 if (vendor_boot_img != -1)
499 vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
500 if (!android_image_get_data(hdr, vhdr, &img_data)) {
501 if (vendor_boot_img != -1)
502 unmap_sysmem(vhdr);
Safae Ouajih607b0752023-02-06 00:50:08 +0100503 unmap_sysmem(hdr);
504 return false;
505 }
Safae Ouajihe0581762023-02-06 00:50:11 +0100506 if (vendor_boot_img != -1)
507 unmap_sysmem(vhdr);
Safae Ouajih607b0752023-02-06 00:50:08 +0100508 unmap_sysmem(hdr);
509
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200510 ulong dtb_img_addr; /* address of DTB part in boot image */
511 u32 dtb_img_size; /* size of DTB payload in boot image */
512 ulong dtb_addr; /* address of DTB blob with specified index */
513 u32 i; /* index iterator */
514
Safae Ouajihe0581762023-02-06 00:50:11 +0100515 android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr);
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200516 /* Check if DTB area of boot image is in DTBO format */
517 if (android_dt_check_header(dtb_img_addr)) {
518 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
519 size);
520 }
521
522 /* Find out the address of DTB with specified index in concat blobs */
Safae Ouajih607b0752023-02-06 00:50:08 +0100523 dtb_img_size = img_data.dtb_size;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200524 i = 0;
525 dtb_addr = dtb_img_addr;
526 while (dtb_addr < dtb_img_addr + dtb_img_size) {
527 const struct fdt_header *fdt;
528 u32 dtb_size;
529
530 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
531 if (fdt_check_header(fdt) != 0) {
532 unmap_sysmem(fdt);
533 printf("Error: Invalid FDT header for index %u\n", i);
534 return false;
535 }
536
537 dtb_size = fdt_totalsize(fdt);
538 unmap_sysmem(fdt);
539
540 if (i == index) {
541 if (size)
542 *size = dtb_size;
543 if (addr)
544 *addr = dtb_addr;
545 return true;
546 }
547
548 dtb_addr += dtb_size;
549 ++i;
550 }
551
552 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
553 return false;
554}
555
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200556#if !defined(CONFIG_SPL_BUILD)
557/**
558 * android_print_contents - prints out the contents of the Android format image
559 * @hdr: pointer to the Android format image header
560 *
561 * android_print_contents() formats a multi line Android image contents
562 * description.
563 * The routine prints out Android image properties
564 *
565 * returns:
566 * no returned results
567 */
Safae Ouajihd71a7322023-02-06 00:50:03 +0100568void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200569{
Safae Ouajihbb5d6922023-02-06 00:50:09 +0100570 if (hdr->header_version >= 3) {
571 printf("Content print is not supported for boot image header version > 2");
572 return;
573 }
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200574 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo210a7172017-04-02 01:49:47 -0700575 /* os_version = ver << 11 | lvl */
576 u32 os_ver = hdr->os_version >> 11;
577 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200578
Sam Protsenkobb633632019-08-15 20:25:07 +0300579 printf("%skernel size: %x\n", p, hdr->kernel_size);
580 printf("%skernel address: %x\n", p, hdr->kernel_addr);
581 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
582 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
583 printf("%ssecond size: %x\n", p, hdr->second_size);
584 printf("%ssecond address: %x\n", p, hdr->second_addr);
585 printf("%stags address: %x\n", p, hdr->tags_addr);
586 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo210a7172017-04-02 01:49:47 -0700587 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
588 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenkobb633632019-08-15 20:25:07 +0300589 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo210a7172017-04-02 01:49:47 -0700590 p, hdr->os_version,
591 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
592 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenkobb633632019-08-15 20:25:07 +0300593 printf("%sname: %s\n", p, hdr->name);
594 printf("%scmdline: %s\n", p, hdr->cmdline);
595 printf("%sheader_version: %d\n", p, hdr->header_version);
596
597 if (hdr->header_version >= 1) {
598 printf("%srecovery dtbo size: %x\n", p,
599 hdr->recovery_dtbo_size);
600 printf("%srecovery dtbo offset: %llx\n", p,
601 hdr->recovery_dtbo_offset);
602 printf("%sheader size: %x\n", p,
603 hdr->header_size);
604 }
605
Safae Ouajihbb5d6922023-02-06 00:50:09 +0100606 if (hdr->header_version == 2) {
Sam Protsenkobb633632019-08-15 20:25:07 +0300607 printf("%sdtb size: %x\n", p, hdr->dtb_size);
608 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
609 }
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200610}
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200611
612/**
613 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
614 * @fdt: DTB header
615 * @index: Number of DTB blob in DTB area.
616 *
617 * Return: true on success or false on error.
618 */
619static bool android_image_print_dtb_info(const struct fdt_header *fdt,
620 u32 index)
621{
622 int root_node_off;
623 u32 fdt_size;
624 const char *model;
625 const char *compatible;
626
627 root_node_off = fdt_path_offset(fdt, "/");
628 if (root_node_off < 0) {
629 printf("Error: Root node not found\n");
630 return false;
631 }
632
633 fdt_size = fdt_totalsize(fdt);
634 compatible = fdt_getprop(fdt, root_node_off, "compatible",
635 NULL);
636 model = fdt_getprop(fdt, root_node_off, "model", NULL);
637
638 printf(" - DTB #%u:\n", index);
639 printf(" (DTB)size = %d\n", fdt_size);
640 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
641 printf(" (DTB)compatible = %s\n",
642 compatible ? compatible : "(unknown)");
643
644 return true;
645}
646
647/**
648 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
649 * @hdr_addr: Boot image header address
650 *
651 * DTB payload in Android Boot Image v2+ can be in one of following formats:
652 * 1. Concatenated DTB blobs
653 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
654 *
655 * This function does next:
656 * 1. Prints out the format used in DTB area
657 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
658 * each blob.
659 *
660 * Return: true on success or false on error.
661 */
662bool android_image_print_dtb_contents(ulong hdr_addr)
663{
Safae Ouajihd71a7322023-02-06 00:50:03 +0100664 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200665 bool res;
666 ulong dtb_img_addr; /* address of DTB part in boot image */
667 u32 dtb_img_size; /* size of DTB payload in boot image */
668 ulong dtb_addr; /* address of DTB blob with specified index */
669 u32 i; /* index iterator */
670
Safae Ouajihe0581762023-02-06 00:50:11 +0100671 res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200672 if (!res)
673 return false;
674
675 /* Check if DTB area of boot image is in DTBO format */
676 if (android_dt_check_header(dtb_img_addr)) {
677 printf("## DTB area contents (DTBO format):\n");
678 android_dt_print_contents(dtb_img_addr);
679 return true;
680 }
681
682 printf("## DTB area contents (concat format):\n");
683
684 /* Iterate over concatenated DTB blobs */
685 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
686 dtb_img_size = hdr->dtb_size;
687 unmap_sysmem(hdr);
688 i = 0;
689 dtb_addr = dtb_img_addr;
690 while (dtb_addr < dtb_img_addr + dtb_img_size) {
691 const struct fdt_header *fdt;
692 u32 dtb_size;
693
694 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
695 if (fdt_check_header(fdt) != 0) {
696 unmap_sysmem(fdt);
697 printf("Error: Invalid FDT header for index %u\n", i);
698 return false;
699 }
700
701 res = android_image_print_dtb_info(fdt, i);
702 if (!res) {
703 unmap_sysmem(fdt);
704 return false;
705 }
706
707 dtb_size = fdt_totalsize(fdt);
708 unmap_sysmem(fdt);
709 dtb_addr += dtb_size;
710 ++i;
711 }
712
713 return true;
714}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200715#endif