blob: 3adcc69a392f74ae64f3fbcf1b85204f60ac9aff [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
17
Sebastian Siewior686d6672014-05-05 15:08:09 -050018static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
19
Safae Ouajih313cc842023-02-06 00:50:18 +010020static ulong checksum(const unsigned char *buffer, ulong size)
21{
22 ulong sum = 0;
23
24 for (ulong i = 0; i < size; i++)
25 sum += buffer[i];
26 return sum;
27}
28
29static bool is_trailer_present(ulong bootconfig_end_addr)
30{
31 return !strncmp((char *)(bootconfig_end_addr - BOOTCONFIG_MAGIC_SIZE),
32 BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
33}
34
35static ulong add_trailer(ulong bootconfig_start_addr, ulong bootconfig_size)
36{
37 ulong end;
38 ulong sum;
39
40 if (!bootconfig_start_addr)
41 return -1;
42 if (!bootconfig_size)
43 return 0;
44
45 end = bootconfig_start_addr + bootconfig_size;
46 if (is_trailer_present(end))
47 return 0;
48
49 memcpy((void *)(end), &bootconfig_size, BOOTCONFIG_SIZE_SIZE);
50 sum = checksum((unsigned char *)bootconfig_start_addr, bootconfig_size);
51 memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE), &sum,
52 BOOTCONFIG_CHECKSUM_SIZE);
53 memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE + BOOTCONFIG_CHECKSUM_SIZE),
54 BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
55
56 return BOOTCONFIG_TRAILER_SIZE;
57}
58
Mattijs Korpershoek2b5c70a2024-07-10 10:40:02 +020059__weak ulong get_avendor_bootimg_addr(void)
60{
61 return -1;
62}
63
Safae Ouajih889005f2023-02-06 00:50:12 +010064static void android_boot_image_v3_v4_parse_hdr(const struct andr_boot_img_hdr_v3 *hdr,
65 struct andr_image_data *data)
66{
67 ulong end;
68
69 data->kcmdline = hdr->cmdline;
70 data->header_version = hdr->header_version;
71
72 /*
73 * The header takes a full page, the remaining components are aligned
74 * on page boundary.
75 */
76 end = (ulong)hdr;
77 end += ANDR_GKI_PAGE_SIZE;
78 data->kernel_ptr = end;
79 data->kernel_size = hdr->kernel_size;
80 end += ALIGN(hdr->kernel_size, ANDR_GKI_PAGE_SIZE);
Roman Stratiienko227b4fa2024-05-19 13:09:51 +000081 data->ramdisk_ptr = end;
Safae Ouajih889005f2023-02-06 00:50:12 +010082 data->ramdisk_size = hdr->ramdisk_size;
83 data->boot_ramdisk_size = hdr->ramdisk_size;
84 end += ALIGN(hdr->ramdisk_size, ANDR_GKI_PAGE_SIZE);
85
86 if (hdr->header_version > 3)
87 end += ALIGN(hdr->signature_size, ANDR_GKI_PAGE_SIZE);
88
89 data->boot_img_total_size = end - (ulong)hdr;
90}
91
92static void android_vendor_boot_image_v3_v4_parse_hdr(const struct andr_vnd_boot_img_hdr
93 *hdr, struct andr_image_data *data)
94{
95 ulong end;
96
97 /*
98 * The header takes a full page, the remaining components are aligned
99 * on page boundary.
100 */
Safae Ouajih6665296e2023-02-06 00:50:14 +0100101 data->kcmdline_extra = hdr->cmdline;
Safae Ouajih889005f2023-02-06 00:50:12 +0100102 data->tags_addr = hdr->tags_addr;
103 data->image_name = hdr->name;
104 data->kernel_addr = hdr->kernel_addr;
105 data->ramdisk_addr = hdr->ramdisk_addr;
106 data->dtb_load_addr = hdr->dtb_addr;
Safae Ouajih313cc842023-02-06 00:50:18 +0100107 data->bootconfig_size = hdr->bootconfig_size;
Safae Ouajih889005f2023-02-06 00:50:12 +0100108 end = (ulong)hdr;
109 end += hdr->page_size;
110 if (hdr->vendor_ramdisk_size) {
111 data->vendor_ramdisk_ptr = end;
112 data->vendor_ramdisk_size = hdr->vendor_ramdisk_size;
113 data->ramdisk_size += hdr->vendor_ramdisk_size;
114 end += ALIGN(hdr->vendor_ramdisk_size, hdr->page_size);
115 }
116
117 data->dtb_ptr = end;
118 data->dtb_size = hdr->dtb_size;
119
120 end += ALIGN(hdr->dtb_size, hdr->page_size);
121 end += ALIGN(hdr->vendor_ramdisk_table_size, hdr->page_size);
Safae Ouajih313cc842023-02-06 00:50:18 +0100122 data->bootconfig_addr = end;
123 if (hdr->bootconfig_size) {
124 data->bootconfig_size += add_trailer(data->bootconfig_addr,
125 data->bootconfig_size);
126 data->ramdisk_size += data->bootconfig_size;
127 }
128 end += ALIGN(data->bootconfig_size, hdr->page_size);
Safae Ouajih889005f2023-02-06 00:50:12 +0100129 data->vendor_boot_img_total_size = end - (ulong)hdr;
130}
131
Safae Ouajih027191d2023-02-06 00:50:07 +0100132static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
133 struct andr_image_data *data)
134{
135 ulong end;
136
137 data->image_name = hdr->name;
138 data->kcmdline = hdr->cmdline;
139 data->kernel_addr = hdr->kernel_addr;
140 data->ramdisk_addr = hdr->ramdisk_addr;
141 data->header_version = hdr->header_version;
142 data->dtb_load_addr = hdr->dtb_addr;
143
144 end = (ulong)hdr;
145
146 /*
147 * The header takes a full page, the remaining components are aligned
148 * on page boundary
149 */
150
151 end += hdr->page_size;
152
153 data->kernel_ptr = end;
154 data->kernel_size = hdr->kernel_size;
155 end += ALIGN(hdr->kernel_size, hdr->page_size);
156
157 data->ramdisk_ptr = end;
158 data->ramdisk_size = hdr->ramdisk_size;
159 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
160
161 data->second_ptr = end;
162 data->second_size = hdr->second_size;
163 end += ALIGN(hdr->second_size, hdr->page_size);
164
165 if (hdr->header_version >= 1) {
166 data->recovery_dtbo_ptr = end;
167 data->recovery_dtbo_size = hdr->recovery_dtbo_size;
168 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
169 }
170
171 if (hdr->header_version >= 2) {
172 data->dtb_ptr = end;
173 data->dtb_size = hdr->dtb_size;
174 end += ALIGN(hdr->dtb_size, hdr->page_size);
175 }
176
177 data->boot_img_total_size = end - (ulong)hdr;
178}
179
Safae Ouajihc60ae102023-02-06 00:50:11 +0100180bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
181 struct andr_image_data *data)
Safae Ouajih027191d2023-02-06 00:50:07 +0100182{
183 if (!boot_hdr || !data) {
184 printf("boot_hdr or data params can't be NULL\n");
185 return false;
186 }
187
188 if (!is_android_boot_image_header(boot_hdr)) {
189 printf("Incorrect boot image header\n");
190 return false;
191 }
192
Safae Ouajih889005f2023-02-06 00:50:12 +0100193 if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2) {
194 if (!vendor_boot_hdr) {
195 printf("For boot header v3+ vendor boot image has to be provided\n");
196 return false;
197 }
198 if (!is_android_vendor_boot_image_header(vendor_boot_hdr)) {
199 printf("Incorrect vendor boot image header\n");
200 return false;
201 }
202 android_boot_image_v3_v4_parse_hdr(boot_hdr, data);
203 android_vendor_boot_image_v3_v4_parse_hdr(vendor_boot_hdr, data);
204 } else {
Safae Ouajih027191d2023-02-06 00:50:07 +0100205 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
Safae Ouajih889005f2023-02-06 00:50:12 +0100206 }
Safae Ouajih027191d2023-02-06 00:50:07 +0100207
208 return true;
209}
210
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200211static ulong android_image_get_kernel_addr(struct andr_image_data *img_data,
212 ulong comp)
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200213{
214 /*
215 * All the Android tools that generate a boot.img use this
216 * address as the default.
217 *
218 * Even though it doesn't really make a lot of sense, and it
219 * might be valid on some platforms, we treat that adress as
220 * the default value for this field, and try to execute the
221 * kernel in place in such a case.
222 *
223 * Otherwise, we will return the actual value set by the user.
224 */
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200225 if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) {
226 if (comp == IH_COMP_NONE)
227 return img_data->kernel_ptr;
228 return env_get_ulong("kernel_addr_r", 16, 0);
229 }
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200230
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200231 /*
232 * abootimg creates images where all load addresses are 0
233 * and we need to fix them.
234 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100235 if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200236 return env_get_ulong("kernel_addr_r", 16, 0);
237
Safae Ouajihbced1042023-02-06 00:50:08 +0100238 return img_data->kernel_addr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200239}
240
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300241/**
242 * android_image_get_kernel() - processes kernel part of Android boot images
Safae Ouajihc60ae102023-02-06 00:50:11 +0100243 * @hdr: Pointer to boot image header, which is at the start
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300244 * of the image.
Safae Ouajihc60ae102023-02-06 00:50:11 +0100245 * @vendor_boot_img: Pointer to vendor boot image header, which is at the
246 * start of the image.
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300247 * @verify: Checksum verification flag. Currently unimplemented.
248 * @os_data: Pointer to a ulong variable, will hold os data start
249 * address.
250 * @os_len: Pointer to a ulong variable, will hold os data length.
251 *
252 * This function returns the os image's start address and length. Also,
253 * it appends the kernel command line to the bootargs env variable.
254 *
255 * Return: Zero, os start address and length on success,
256 * otherwise on failure.
257 */
Safae Ouajih51c981b2023-02-06 00:50:17 +0100258int android_image_get_kernel(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100259 const void *vendor_boot_img, int verify,
Sebastian Siewior686d6672014-05-05 15:08:09 -0500260 ulong *os_data, ulong *os_len)
261{
Safae Ouajihbced1042023-02-06 00:50:08 +0100262 struct andr_image_data img_data = {0};
Neil Armstrongfd0318b2024-10-17 16:44:42 +0200263 ulong kernel_addr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100264 const struct legacy_img_hdr *ihdr;
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200265 ulong comp;
Safae Ouajihbced1042023-02-06 00:50:08 +0100266
Safae Ouajihc60ae102023-02-06 00:50:11 +0100267 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100268 return -EINVAL;
269
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200270 comp = android_image_get_kcomp(hdr, vendor_boot_img);
271
272 kernel_addr = android_image_get_kernel_addr(&img_data, comp);
Safae Ouajihbced1042023-02-06 00:50:08 +0100273 ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200274
Sebastian Siewior686d6672014-05-05 15:08:09 -0500275 /*
276 * Not all Android tools use the id field for signing the image with
277 * sha1 (or anything) so we don't check it. It is not obvious that the
278 * string is null terminated so we take care of this.
279 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100280 strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500281 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
282 if (strlen(andr_tmp_str))
283 printf("Android's image name: %s\n", andr_tmp_str);
284
Neil Armstrongfd0318b2024-10-17 16:44:42 +0200285 printf("Kernel load addr 0x%08lx size %u KiB\n",
Safae Ouajihbced1042023-02-06 00:50:08 +0100286 kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300287
288 int len = 0;
Safae Ouajihbced1042023-02-06 00:50:08 +0100289 if (*img_data.kcmdline) {
290 printf("Kernel command line: %s\n", img_data.kcmdline);
291 len += strlen(img_data.kcmdline);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500292 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300293
Safae Ouajih6665296e2023-02-06 00:50:14 +0100294 if (img_data.kcmdline_extra) {
295 printf("Kernel extra command line: %s\n", img_data.kcmdline_extra);
296 len += strlen(img_data.kcmdline_extra);
297 }
298
Simon Glass64b723f2017-08-03 12:22:12 -0600299 char *bootargs = env_get("bootargs");
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300300 if (bootargs)
301 len += strlen(bootargs);
302
303 char *newbootargs = malloc(len + 2);
304 if (!newbootargs) {
305 puts("Error: malloc in android_image_get_kernel failed!\n");
306 return -ENOMEM;
307 }
308 *newbootargs = '\0';
309
310 if (bootargs) {
311 strcpy(newbootargs, bootargs);
312 strcat(newbootargs, " ");
313 }
Safae Ouajihbced1042023-02-06 00:50:08 +0100314
315 if (*img_data.kcmdline)
316 strcat(newbootargs, img_data.kcmdline);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300317
Safae Ouajih6665296e2023-02-06 00:50:14 +0100318 if (img_data.kcmdline_extra) {
319 strcat(newbootargs, " ");
320 strcat(newbootargs, img_data.kcmdline_extra);
321 }
322
Simon Glass6a38e412017-08-03 12:22:09 -0600323 env_set("bootargs", newbootargs);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500324
325 if (os_data) {
Roman Stratiienko73268582019-06-03 15:38:13 +0300326 if (image_get_magic(ihdr) == IH_MAGIC) {
327 *os_data = image_get_data(ihdr);
328 } else {
Safae Ouajihbced1042023-02-06 00:50:08 +0100329 *os_data = img_data.kernel_ptr;
Roman Stratiienko73268582019-06-03 15:38:13 +0300330 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500331 }
Roman Stratiienko73268582019-06-03 15:38:13 +0300332 if (os_len) {
333 if (image_get_magic(ihdr) == IH_MAGIC)
334 *os_len = image_get_data_size(ihdr);
335 else
Safae Ouajihbced1042023-02-06 00:50:08 +0100336 *os_len = img_data.kernel_size;
Roman Stratiienko73268582019-06-03 15:38:13 +0300337 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500338 return 0;
339}
340
Safae Ouajih889005f2023-02-06 00:50:12 +0100341bool is_android_vendor_boot_image_header(const void *vendor_boot_img)
342{
343 return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE);
344}
345
Safae Ouajih51c981b2023-02-06 00:50:17 +0100346bool is_android_boot_image_header(const void *hdr)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500347{
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100348 return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500349}
350
Safae Ouajihc60ae102023-02-06 00:50:11 +0100351ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
352 const void *vendor_boot_img)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500353{
Safae Ouajihbced1042023-02-06 00:50:08 +0100354 struct andr_image_data img_data;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500355
Safae Ouajihc60ae102023-02-06 00:50:11 +0100356 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100357 return -EINVAL;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300358
Safae Ouajihbced1042023-02-06 00:50:08 +0100359 if (img_data.header_version > 2)
360 return 0;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300361
Safae Ouajihbced1042023-02-06 00:50:08 +0100362 return img_data.boot_img_total_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500363}
364
Safae Ouajih51c981b2023-02-06 00:50:17 +0100365ulong android_image_get_kload(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100366 const void *vendor_boot_img)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500367{
Safae Ouajihbced1042023-02-06 00:50:08 +0100368 struct andr_image_data img_data;
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200369 ulong comp;
Safae Ouajihbced1042023-02-06 00:50:08 +0100370
Safae Ouajihc60ae102023-02-06 00:50:11 +0100371 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100372 return -EINVAL;
373
Neil Armstrong7cb867d2024-10-17 16:44:43 +0200374 comp = android_image_get_kcomp(hdr, vendor_boot_img);
375
376 return android_image_get_kernel_addr(&img_data, comp);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500377}
378
Safae Ouajih51c981b2023-02-06 00:50:17 +0100379ulong android_image_get_kcomp(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100380 const void *vendor_boot_img)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200381{
Safae Ouajih027191d2023-02-06 00:50:07 +0100382 struct andr_image_data img_data;
383 const void *p;
384
Safae Ouajihc60ae102023-02-06 00:50:11 +0100385 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih027191d2023-02-06 00:50:07 +0100386 return -EINVAL;
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200387
Safae Ouajih027191d2023-02-06 00:50:07 +0100388 p = (const void *)img_data.kernel_ptr;
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600389 if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
390 return image_get_comp((struct legacy_img_hdr *)p);
Roman Stratiienko73268582019-06-03 15:38:13 +0300391 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200392 return IH_COMP_LZ4;
393 else
Stephan Gerhold9e5111d2021-07-01 20:33:16 +0200394 return image_decomp_type(p, sizeof(u32));
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200395}
396
Safae Ouajih5b01a882023-02-06 00:50:13 +0100397int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
398 ulong *rd_data, ulong *rd_len)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500399{
Safae Ouajihbced1042023-02-06 00:50:08 +0100400 struct andr_image_data img_data = {0};
Safae Ouajih5b01a882023-02-06 00:50:13 +0100401 ulong ramdisk_ptr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100402
Safae Ouajihc60ae102023-02-06 00:50:11 +0100403 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100404 return -EINVAL;
405
Michael Walle955415b2024-07-29 23:36:57 +0200406 if (!img_data.ramdisk_size)
407 return -ENOENT;
408
Safae Ouajih5b01a882023-02-06 00:50:13 +0100409 if (img_data.header_version > 2) {
Roman Stratiienko227b4fa2024-05-19 13:09:51 +0000410 ramdisk_ptr = img_data.ramdisk_addr;
Safae Ouajih5b01a882023-02-06 00:50:13 +0100411 memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
412 img_data.vendor_ramdisk_size);
Roman Stratiienko227b4fa2024-05-19 13:09:51 +0000413 ramdisk_ptr += img_data.vendor_ramdisk_size;
414 memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr,
Safae Ouajih313cc842023-02-06 00:50:18 +0100415 img_data.boot_ramdisk_size);
Roman Stratiienko227b4fa2024-05-19 13:09:51 +0000416 ramdisk_ptr += img_data.boot_ramdisk_size;
Safae Ouajih313cc842023-02-06 00:50:18 +0100417 if (img_data.bootconfig_size) {
418 memcpy((void *)
Roman Stratiienko227b4fa2024-05-19 13:09:51 +0000419 (ramdisk_ptr), (void *)img_data.bootconfig_addr,
Safae Ouajih313cc842023-02-06 00:50:18 +0100420 img_data.bootconfig_size);
421 }
Mattijs Korpershoek91359972024-10-03 14:42:39 +0200422 } else {
423 ramdisk_ptr = img_data.ramdisk_addr;
424 memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr,
425 img_data.ramdisk_size);
Safae Ouajih5b01a882023-02-06 00:50:13 +0100426 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300427
Safae Ouajihbced1042023-02-06 00:50:08 +0100428 printf("RAM disk load addr 0x%08lx size %u KiB\n",
Roman Stratiienko227b4fa2024-05-19 13:09:51 +0000429 img_data.ramdisk_addr, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300430
Roman Stratiienko227b4fa2024-05-19 13:09:51 +0000431 *rd_data = img_data.ramdisk_addr;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500432
Safae Ouajihbced1042023-02-06 00:50:08 +0100433 *rd_len = img_data.ramdisk_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500434 return 0;
435}
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200436
Safae Ouajih51c981b2023-02-06 00:50:17 +0100437int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len)
Bin Chen909f1402018-01-27 16:59:08 +1100438{
Safae Ouajihbced1042023-02-06 00:50:08 +0100439 struct andr_image_data img_data;
440
Safae Ouajihc60ae102023-02-06 00:50:11 +0100441 if (!android_image_get_data(hdr, NULL, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100442 return -EINVAL;
443
Safae Ouajih51c981b2023-02-06 00:50:17 +0100444 if (img_data.header_version > 2) {
445 printf("Second stage bootloader is only supported for boot image version <= 2\n");
446 return -EOPNOTSUPP;
447 }
448
Safae Ouajihbced1042023-02-06 00:50:08 +0100449 if (!img_data.second_size) {
Bin Chen909f1402018-01-27 16:59:08 +1100450 *second_data = *second_len = 0;
451 return -1;
452 }
453
Safae Ouajihbced1042023-02-06 00:50:08 +0100454 *second_data = img_data.second_ptr;
Bin Chen909f1402018-01-27 16:59:08 +1100455
456 printf("second address is 0x%lx\n",*second_data);
457
Safae Ouajihbced1042023-02-06 00:50:08 +0100458 *second_len = img_data.second_size;
Bin Chen909f1402018-01-27 16:59:08 +1100459 return 0;
460}
461
Sam Protsenko4fabf402020-01-24 17:53:40 +0200462/**
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200463 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
464 * @hdr_addr: Boot image header address
465 * @addr: If not NULL, will contain address of recovery DTBO image
466 * @size: If not NULL, will contain size of recovery DTBO image
467 *
468 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
469 * Boot Image in RAM. The format of this image is Android DTBO (see
470 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
471 * the address is obtained from this function, one can use 'adtimg' U-Boot
472 * command or android_dt_*() functions to extract desired DTBO blob.
473 *
474 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
475 * only can be found in recovery image. On A/B devices we can always rely on
476 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
477 * AOSP documentation for details.
478 *
479 * Return: true on success or false on error.
480 */
481bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
482{
Safae Ouajih8656e382023-02-06 00:50:03 +0100483 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200484 ulong dtbo_img_addr;
485 bool ret = true;
486
487 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100488 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200489 printf("Error: Boot Image header is incorrect\n");
490 ret = false;
491 goto exit;
492 }
493
Safae Ouajih8d351582023-02-06 00:50:10 +0100494 if (hdr->header_version != 1 && hdr->header_version != 2) {
495 printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200496 ret = false;
497 goto exit;
498 }
499
500 if (hdr->recovery_dtbo_size == 0) {
501 printf("Error: recovery_dtbo_size is 0\n");
502 ret = false;
503 goto exit;
504 }
505
506 /* Calculate the address of DTB area in boot image */
507 dtbo_img_addr = hdr_addr;
508 dtbo_img_addr += hdr->page_size;
509 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
510 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
511 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
512
513 if (addr)
514 *addr = dtbo_img_addr;
515 if (size)
516 *size = hdr->recovery_dtbo_size;
517
518exit:
519 unmap_sysmem(hdr);
520 return ret;
521}
522
523/**
Sam Protsenko4fabf402020-01-24 17:53:40 +0200524 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
525 * @hdr_addr: Boot image header address
Safae Ouajihc60ae102023-02-06 00:50:11 +0100526 * @vhdr_addr: Vendor Boot image header address
Sam Protsenko4fabf402020-01-24 17:53:40 +0200527 * @addr: Will contain the address of DTB area in boot image
528 *
529 * Return: true on success or false on fail.
530 */
Safae Ouajihc60ae102023-02-06 00:50:11 +0100531static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
Sam Protsenko4fabf402020-01-24 17:53:40 +0200532{
Safae Ouajih8656e382023-02-06 00:50:03 +0100533 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100534 const struct andr_vnd_boot_img_hdr *v_hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200535 ulong dtb_img_addr;
536 bool ret = true;
537
538 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100539 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko4fabf402020-01-24 17:53:40 +0200540 printf("Error: Boot Image header is incorrect\n");
541 ret = false;
542 goto exit;
543 }
544
545 if (hdr->header_version < 2) {
546 printf("Error: header_version must be >= 2 to get dtb\n");
547 ret = false;
548 goto exit;
549 }
550
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100551 if (hdr->header_version == 2) {
552 if (!hdr->dtb_size) {
553 printf("Error: dtb_size is 0\n");
554 ret = false;
555 goto exit;
556 }
557 /* Calculate the address of DTB area in boot image */
558 dtb_img_addr = hdr_addr;
559 dtb_img_addr += hdr->page_size;
560 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
561 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
562 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
563 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200564
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100565 *addr = dtb_img_addr;
566 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200567
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100568 if (hdr->header_version > 2) {
569 v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr));
570 if (!v_hdr->dtb_size) {
571 printf("Error: dtb_size is 0\n");
572 ret = false;
573 unmap_sysmem(v_hdr);
574 goto exit;
575 }
576 /* Calculate the address of DTB area in boot image */
577 dtb_img_addr = vhdr_addr;
578 dtb_img_addr += v_hdr->page_size;
579 if (v_hdr->vendor_ramdisk_size)
580 dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size);
581 *addr = dtb_img_addr;
582 unmap_sysmem(v_hdr);
583 goto exit;
584 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200585exit:
586 unmap_sysmem(hdr);
587 return ret;
588}
589
590/**
591 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
592 * @hdr_addr: Boot image header address
Safae Ouajihc60ae102023-02-06 00:50:11 +0100593 * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
Sam Protsenko4fabf402020-01-24 17:53:40 +0200594 * @index: Index of desired DTB in DTB area (starting from 0)
595 * @addr: If not NULL, will contain address to specified DTB
596 * @size: If not NULL, will contain size of specified DTB
597 *
598 * Get the address and size of DTB blob by its index in DTB area of Android
599 * Boot Image in RAM.
600 *
601 * Return: true on success or false on error.
602 */
Safae Ouajihc60ae102023-02-06 00:50:11 +0100603bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
604 u32 index, ulong *addr, u32 *size)
Sam Protsenko4fabf402020-01-24 17:53:40 +0200605{
Safae Ouajihbced1042023-02-06 00:50:08 +0100606 struct andr_image_data img_data;
Safae Ouajih8656e382023-02-06 00:50:03 +0100607 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajihc60ae102023-02-06 00:50:11 +0100608 const struct andr_vnd_boot_img_hdr *vhdr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100609
610 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajihc60ae102023-02-06 00:50:11 +0100611 if (vendor_boot_img != -1)
612 vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
613 if (!android_image_get_data(hdr, vhdr, &img_data)) {
614 if (vendor_boot_img != -1)
615 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +0100616 unmap_sysmem(hdr);
617 return false;
618 }
Safae Ouajihc60ae102023-02-06 00:50:11 +0100619 if (vendor_boot_img != -1)
620 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +0100621 unmap_sysmem(hdr);
622
Sam Protsenko4fabf402020-01-24 17:53:40 +0200623 ulong dtb_img_addr; /* address of DTB part in boot image */
624 u32 dtb_img_size; /* size of DTB payload in boot image */
625 ulong dtb_addr; /* address of DTB blob with specified index */
626 u32 i; /* index iterator */
627
Safae Ouajihc60ae102023-02-06 00:50:11 +0100628 android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200629 /* Check if DTB area of boot image is in DTBO format */
630 if (android_dt_check_header(dtb_img_addr)) {
631 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
632 size);
633 }
634
635 /* Find out the address of DTB with specified index in concat blobs */
Safae Ouajihbced1042023-02-06 00:50:08 +0100636 dtb_img_size = img_data.dtb_size;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200637 i = 0;
638 dtb_addr = dtb_img_addr;
639 while (dtb_addr < dtb_img_addr + dtb_img_size) {
640 const struct fdt_header *fdt;
641 u32 dtb_size;
642
643 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
644 if (fdt_check_header(fdt) != 0) {
645 unmap_sysmem(fdt);
646 printf("Error: Invalid FDT header for index %u\n", i);
647 return false;
648 }
649
650 dtb_size = fdt_totalsize(fdt);
651 unmap_sysmem(fdt);
652
653 if (i == index) {
654 if (size)
655 *size = dtb_size;
656 if (addr)
657 *addr = dtb_addr;
658 return true;
659 }
660
661 dtb_addr += dtb_size;
662 ++i;
663 }
664
665 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
666 return false;
667}
668
Simon Glass0e84d962024-09-29 19:49:50 -0600669#if !defined(CONFIG_XPL_BUILD)
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200670/**
671 * android_print_contents - prints out the contents of the Android format image
672 * @hdr: pointer to the Android format image header
673 *
674 * android_print_contents() formats a multi line Android image contents
675 * description.
676 * The routine prints out Android image properties
677 *
678 * returns:
679 * no returned results
680 */
Safae Ouajih8656e382023-02-06 00:50:03 +0100681void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200682{
Safae Ouajiha148a822023-02-06 00:50:09 +0100683 if (hdr->header_version >= 3) {
684 printf("Content print is not supported for boot image header version > 2");
685 return;
686 }
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200687 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo41f513a2017-04-02 01:49:47 -0700688 /* os_version = ver << 11 | lvl */
689 u32 os_ver = hdr->os_version >> 11;
690 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200691
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300692 printf("%skernel size: %x\n", p, hdr->kernel_size);
693 printf("%skernel address: %x\n", p, hdr->kernel_addr);
694 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
695 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
696 printf("%ssecond size: %x\n", p, hdr->second_size);
697 printf("%ssecond address: %x\n", p, hdr->second_addr);
698 printf("%stags address: %x\n", p, hdr->tags_addr);
699 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo41f513a2017-04-02 01:49:47 -0700700 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
701 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300702 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo41f513a2017-04-02 01:49:47 -0700703 p, hdr->os_version,
704 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
705 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300706 printf("%sname: %s\n", p, hdr->name);
707 printf("%scmdline: %s\n", p, hdr->cmdline);
708 printf("%sheader_version: %d\n", p, hdr->header_version);
709
710 if (hdr->header_version >= 1) {
711 printf("%srecovery dtbo size: %x\n", p,
712 hdr->recovery_dtbo_size);
713 printf("%srecovery dtbo offset: %llx\n", p,
714 hdr->recovery_dtbo_offset);
715 printf("%sheader size: %x\n", p,
716 hdr->header_size);
717 }
718
Safae Ouajiha148a822023-02-06 00:50:09 +0100719 if (hdr->header_version == 2) {
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300720 printf("%sdtb size: %x\n", p, hdr->dtb_size);
721 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
722 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200723}
724
725/**
726 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
727 * @fdt: DTB header
728 * @index: Number of DTB blob in DTB area.
729 *
730 * Return: true on success or false on error.
731 */
732static bool android_image_print_dtb_info(const struct fdt_header *fdt,
733 u32 index)
734{
735 int root_node_off;
736 u32 fdt_size;
737 const char *model;
738 const char *compatible;
739
740 root_node_off = fdt_path_offset(fdt, "/");
741 if (root_node_off < 0) {
742 printf("Error: Root node not found\n");
743 return false;
744 }
745
746 fdt_size = fdt_totalsize(fdt);
747 compatible = fdt_getprop(fdt, root_node_off, "compatible",
748 NULL);
749 model = fdt_getprop(fdt, root_node_off, "model", NULL);
750
751 printf(" - DTB #%u:\n", index);
752 printf(" (DTB)size = %d\n", fdt_size);
753 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
754 printf(" (DTB)compatible = %s\n",
755 compatible ? compatible : "(unknown)");
756
757 return true;
758}
759
760/**
761 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
762 * @hdr_addr: Boot image header address
763 *
764 * DTB payload in Android Boot Image v2+ can be in one of following formats:
765 * 1. Concatenated DTB blobs
766 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
767 *
768 * This function does next:
769 * 1. Prints out the format used in DTB area
770 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
771 * each blob.
772 *
773 * Return: true on success or false on error.
774 */
775bool android_image_print_dtb_contents(ulong hdr_addr)
776{
Safae Ouajih8656e382023-02-06 00:50:03 +0100777 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200778 bool res;
779 ulong dtb_img_addr; /* address of DTB part in boot image */
780 u32 dtb_img_size; /* size of DTB payload in boot image */
781 ulong dtb_addr; /* address of DTB blob with specified index */
782 u32 i; /* index iterator */
783
Safae Ouajihc60ae102023-02-06 00:50:11 +0100784 res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200785 if (!res)
786 return false;
787
788 /* Check if DTB area of boot image is in DTBO format */
789 if (android_dt_check_header(dtb_img_addr)) {
790 printf("## DTB area contents (DTBO format):\n");
791 android_dt_print_contents(dtb_img_addr);
792 return true;
793 }
794
795 printf("## DTB area contents (concat format):\n");
796
797 /* Iterate over concatenated DTB blobs */
798 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
799 dtb_img_size = hdr->dtb_size;
800 unmap_sysmem(hdr);
801 i = 0;
802 dtb_addr = dtb_img_addr;
803 while (dtb_addr < dtb_img_addr + dtb_img_size) {
804 const struct fdt_header *fdt;
805 u32 dtb_size;
806
807 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
808 if (fdt_check_header(fdt) != 0) {
809 unmap_sysmem(fdt);
810 printf("Error: Invalid FDT header for index %u\n", i);
811 return false;
812 }
813
814 res = android_image_print_dtb_info(fdt, i);
815 if (!res) {
816 unmap_sysmem(fdt);
817 return false;
818 }
819
820 dtb_size = fdt_totalsize(fdt);
821 unmap_sysmem(fdt);
822 dtb_addr += dtb_size;
823 ++i;
824 }
825
826 return true;
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200827}
828#endif