blob: ddd8ffd5e5408a7a517918753656a887cc233dd6 [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
Safae Ouajih889005f2023-02-06 00:50:12 +010059static void android_boot_image_v3_v4_parse_hdr(const struct andr_boot_img_hdr_v3 *hdr,
60 struct andr_image_data *data)
61{
62 ulong end;
63
64 data->kcmdline = hdr->cmdline;
65 data->header_version = hdr->header_version;
Safae Ouajih5b01a882023-02-06 00:50:13 +010066 data->ramdisk_ptr = env_get_ulong("ramdisk_addr_r", 16, 0);
Safae Ouajih889005f2023-02-06 00:50:12 +010067
68 /*
69 * The header takes a full page, the remaining components are aligned
70 * on page boundary.
71 */
72 end = (ulong)hdr;
73 end += ANDR_GKI_PAGE_SIZE;
74 data->kernel_ptr = end;
75 data->kernel_size = hdr->kernel_size;
76 end += ALIGN(hdr->kernel_size, ANDR_GKI_PAGE_SIZE);
77 data->ramdisk_size = hdr->ramdisk_size;
78 data->boot_ramdisk_size = hdr->ramdisk_size;
79 end += ALIGN(hdr->ramdisk_size, ANDR_GKI_PAGE_SIZE);
80
81 if (hdr->header_version > 3)
82 end += ALIGN(hdr->signature_size, ANDR_GKI_PAGE_SIZE);
83
84 data->boot_img_total_size = end - (ulong)hdr;
85}
86
87static void android_vendor_boot_image_v3_v4_parse_hdr(const struct andr_vnd_boot_img_hdr
88 *hdr, struct andr_image_data *data)
89{
90 ulong end;
91
92 /*
93 * The header takes a full page, the remaining components are aligned
94 * on page boundary.
95 */
Safae Ouajih6665296e2023-02-06 00:50:14 +010096 data->kcmdline_extra = hdr->cmdline;
Safae Ouajih889005f2023-02-06 00:50:12 +010097 data->tags_addr = hdr->tags_addr;
98 data->image_name = hdr->name;
99 data->kernel_addr = hdr->kernel_addr;
100 data->ramdisk_addr = hdr->ramdisk_addr;
101 data->dtb_load_addr = hdr->dtb_addr;
Safae Ouajih313cc842023-02-06 00:50:18 +0100102 data->bootconfig_size = hdr->bootconfig_size;
Safae Ouajih889005f2023-02-06 00:50:12 +0100103 end = (ulong)hdr;
104 end += hdr->page_size;
105 if (hdr->vendor_ramdisk_size) {
106 data->vendor_ramdisk_ptr = end;
107 data->vendor_ramdisk_size = hdr->vendor_ramdisk_size;
108 data->ramdisk_size += hdr->vendor_ramdisk_size;
109 end += ALIGN(hdr->vendor_ramdisk_size, hdr->page_size);
110 }
111
112 data->dtb_ptr = end;
113 data->dtb_size = hdr->dtb_size;
114
115 end += ALIGN(hdr->dtb_size, hdr->page_size);
116 end += ALIGN(hdr->vendor_ramdisk_table_size, hdr->page_size);
Safae Ouajih313cc842023-02-06 00:50:18 +0100117 data->bootconfig_addr = end;
118 if (hdr->bootconfig_size) {
119 data->bootconfig_size += add_trailer(data->bootconfig_addr,
120 data->bootconfig_size);
121 data->ramdisk_size += data->bootconfig_size;
122 }
123 end += ALIGN(data->bootconfig_size, hdr->page_size);
Safae Ouajih889005f2023-02-06 00:50:12 +0100124 data->vendor_boot_img_total_size = end - (ulong)hdr;
125}
126
Safae Ouajih027191d2023-02-06 00:50:07 +0100127static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
128 struct andr_image_data *data)
129{
130 ulong end;
131
132 data->image_name = hdr->name;
133 data->kcmdline = hdr->cmdline;
134 data->kernel_addr = hdr->kernel_addr;
135 data->ramdisk_addr = hdr->ramdisk_addr;
136 data->header_version = hdr->header_version;
137 data->dtb_load_addr = hdr->dtb_addr;
138
139 end = (ulong)hdr;
140
141 /*
142 * The header takes a full page, the remaining components are aligned
143 * on page boundary
144 */
145
146 end += hdr->page_size;
147
148 data->kernel_ptr = end;
149 data->kernel_size = hdr->kernel_size;
150 end += ALIGN(hdr->kernel_size, hdr->page_size);
151
152 data->ramdisk_ptr = end;
153 data->ramdisk_size = hdr->ramdisk_size;
154 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
155
156 data->second_ptr = end;
157 data->second_size = hdr->second_size;
158 end += ALIGN(hdr->second_size, hdr->page_size);
159
160 if (hdr->header_version >= 1) {
161 data->recovery_dtbo_ptr = end;
162 data->recovery_dtbo_size = hdr->recovery_dtbo_size;
163 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
164 }
165
166 if (hdr->header_version >= 2) {
167 data->dtb_ptr = end;
168 data->dtb_size = hdr->dtb_size;
169 end += ALIGN(hdr->dtb_size, hdr->page_size);
170 }
171
172 data->boot_img_total_size = end - (ulong)hdr;
173}
174
Safae Ouajihc60ae102023-02-06 00:50:11 +0100175bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
176 struct andr_image_data *data)
Safae Ouajih027191d2023-02-06 00:50:07 +0100177{
178 if (!boot_hdr || !data) {
179 printf("boot_hdr or data params can't be NULL\n");
180 return false;
181 }
182
183 if (!is_android_boot_image_header(boot_hdr)) {
184 printf("Incorrect boot image header\n");
185 return false;
186 }
187
Safae Ouajih889005f2023-02-06 00:50:12 +0100188 if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2) {
189 if (!vendor_boot_hdr) {
190 printf("For boot header v3+ vendor boot image has to be provided\n");
191 return false;
192 }
193 if (!is_android_vendor_boot_image_header(vendor_boot_hdr)) {
194 printf("Incorrect vendor boot image header\n");
195 return false;
196 }
197 android_boot_image_v3_v4_parse_hdr(boot_hdr, data);
198 android_vendor_boot_image_v3_v4_parse_hdr(vendor_boot_hdr, data);
199 } else {
Safae Ouajih027191d2023-02-06 00:50:07 +0100200 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
Safae Ouajih889005f2023-02-06 00:50:12 +0100201 }
Safae Ouajih027191d2023-02-06 00:50:07 +0100202
203 return true;
204}
205
Safae Ouajihbced1042023-02-06 00:50:08 +0100206static ulong android_image_get_kernel_addr(struct andr_image_data *img_data)
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200207{
208 /*
209 * All the Android tools that generate a boot.img use this
210 * address as the default.
211 *
212 * Even though it doesn't really make a lot of sense, and it
213 * might be valid on some platforms, we treat that adress as
214 * the default value for this field, and try to execute the
215 * kernel in place in such a case.
216 *
217 * Otherwise, we will return the actual value set by the user.
218 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100219 if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
220 return img_data->kernel_ptr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200221
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200222 /*
223 * abootimg creates images where all load addresses are 0
224 * and we need to fix them.
225 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100226 if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200227 return env_get_ulong("kernel_addr_r", 16, 0);
228
Safae Ouajihbced1042023-02-06 00:50:08 +0100229 return img_data->kernel_addr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200230}
231
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300232/**
233 * android_image_get_kernel() - processes kernel part of Android boot images
Safae Ouajihc60ae102023-02-06 00:50:11 +0100234 * @hdr: Pointer to boot image header, which is at the start
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300235 * of the image.
Safae Ouajihc60ae102023-02-06 00:50:11 +0100236 * @vendor_boot_img: Pointer to vendor boot image header, which is at the
237 * start of the image.
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300238 * @verify: Checksum verification flag. Currently unimplemented.
239 * @os_data: Pointer to a ulong variable, will hold os data start
240 * address.
241 * @os_len: Pointer to a ulong variable, will hold os data length.
242 *
243 * This function returns the os image's start address and length. Also,
244 * it appends the kernel command line to the bootargs env variable.
245 *
246 * Return: Zero, os start address and length on success,
247 * otherwise on failure.
248 */
Safae Ouajih51c981b2023-02-06 00:50:17 +0100249int android_image_get_kernel(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100250 const void *vendor_boot_img, int verify,
Sebastian Siewior686d6672014-05-05 15:08:09 -0500251 ulong *os_data, ulong *os_len)
252{
Safae Ouajihbced1042023-02-06 00:50:08 +0100253 struct andr_image_data img_data = {0};
254 u32 kernel_addr;
255 const struct legacy_img_hdr *ihdr;
256
Safae Ouajihc60ae102023-02-06 00:50:11 +0100257 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100258 return -EINVAL;
259
260 kernel_addr = android_image_get_kernel_addr(&img_data);
261 ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200262
Sebastian Siewior686d6672014-05-05 15:08:09 -0500263 /*
264 * Not all Android tools use the id field for signing the image with
265 * sha1 (or anything) so we don't check it. It is not obvious that the
266 * string is null terminated so we take care of this.
267 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100268 strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500269 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
270 if (strlen(andr_tmp_str))
271 printf("Android's image name: %s\n", andr_tmp_str);
272
273 printf("Kernel load addr 0x%08x size %u KiB\n",
Safae Ouajihbced1042023-02-06 00:50:08 +0100274 kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300275
276 int len = 0;
Safae Ouajihbced1042023-02-06 00:50:08 +0100277 if (*img_data.kcmdline) {
278 printf("Kernel command line: %s\n", img_data.kcmdline);
279 len += strlen(img_data.kcmdline);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500280 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300281
Safae Ouajih6665296e2023-02-06 00:50:14 +0100282 if (img_data.kcmdline_extra) {
283 printf("Kernel extra command line: %s\n", img_data.kcmdline_extra);
284 len += strlen(img_data.kcmdline_extra);
285 }
286
Simon Glass64b723f2017-08-03 12:22:12 -0600287 char *bootargs = env_get("bootargs");
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300288 if (bootargs)
289 len += strlen(bootargs);
290
291 char *newbootargs = malloc(len + 2);
292 if (!newbootargs) {
293 puts("Error: malloc in android_image_get_kernel failed!\n");
294 return -ENOMEM;
295 }
296 *newbootargs = '\0';
297
298 if (bootargs) {
299 strcpy(newbootargs, bootargs);
300 strcat(newbootargs, " ");
301 }
Safae Ouajihbced1042023-02-06 00:50:08 +0100302
303 if (*img_data.kcmdline)
304 strcat(newbootargs, img_data.kcmdline);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300305
Safae Ouajih6665296e2023-02-06 00:50:14 +0100306 if (img_data.kcmdline_extra) {
307 strcat(newbootargs, " ");
308 strcat(newbootargs, img_data.kcmdline_extra);
309 }
310
Simon Glass6a38e412017-08-03 12:22:09 -0600311 env_set("bootargs", newbootargs);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500312
313 if (os_data) {
Roman Stratiienko73268582019-06-03 15:38:13 +0300314 if (image_get_magic(ihdr) == IH_MAGIC) {
315 *os_data = image_get_data(ihdr);
316 } else {
Safae Ouajihbced1042023-02-06 00:50:08 +0100317 *os_data = img_data.kernel_ptr;
Roman Stratiienko73268582019-06-03 15:38:13 +0300318 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500319 }
Roman Stratiienko73268582019-06-03 15:38:13 +0300320 if (os_len) {
321 if (image_get_magic(ihdr) == IH_MAGIC)
322 *os_len = image_get_data_size(ihdr);
323 else
Safae Ouajihbced1042023-02-06 00:50:08 +0100324 *os_len = img_data.kernel_size;
Roman Stratiienko73268582019-06-03 15:38:13 +0300325 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500326 return 0;
327}
328
Safae Ouajih889005f2023-02-06 00:50:12 +0100329bool is_android_vendor_boot_image_header(const void *vendor_boot_img)
330{
331 return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE);
332}
333
Safae Ouajih51c981b2023-02-06 00:50:17 +0100334bool is_android_boot_image_header(const void *hdr)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500335{
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100336 return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500337}
338
Safae Ouajihc60ae102023-02-06 00:50:11 +0100339ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
340 const void *vendor_boot_img)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500341{
Safae Ouajihbced1042023-02-06 00:50:08 +0100342 struct andr_image_data img_data;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500343
Safae Ouajihc60ae102023-02-06 00:50:11 +0100344 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100345 return -EINVAL;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300346
Safae Ouajihbced1042023-02-06 00:50:08 +0100347 if (img_data.header_version > 2)
348 return 0;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300349
Safae Ouajihbced1042023-02-06 00:50:08 +0100350 return img_data.boot_img_total_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500351}
352
Safae Ouajih51c981b2023-02-06 00:50:17 +0100353ulong android_image_get_kload(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100354 const void *vendor_boot_img)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500355{
Safae Ouajihbced1042023-02-06 00:50:08 +0100356 struct andr_image_data img_data;
357
Safae Ouajihc60ae102023-02-06 00:50:11 +0100358 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100359 return -EINVAL;
360
361 return android_image_get_kernel_addr(&img_data);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500362}
363
Safae Ouajih51c981b2023-02-06 00:50:17 +0100364ulong android_image_get_kcomp(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100365 const void *vendor_boot_img)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200366{
Safae Ouajih027191d2023-02-06 00:50:07 +0100367 struct andr_image_data img_data;
368 const void *p;
369
Safae Ouajihc60ae102023-02-06 00:50:11 +0100370 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih027191d2023-02-06 00:50:07 +0100371 return -EINVAL;
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200372
Safae Ouajih027191d2023-02-06 00:50:07 +0100373 p = (const void *)img_data.kernel_ptr;
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600374 if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
375 return image_get_comp((struct legacy_img_hdr *)p);
Roman Stratiienko73268582019-06-03 15:38:13 +0300376 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200377 return IH_COMP_LZ4;
378 else
Stephan Gerhold9e5111d2021-07-01 20:33:16 +0200379 return image_decomp_type(p, sizeof(u32));
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200380}
381
Safae Ouajih5b01a882023-02-06 00:50:13 +0100382int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
383 ulong *rd_data, ulong *rd_len)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500384{
Safae Ouajihbced1042023-02-06 00:50:08 +0100385 struct andr_image_data img_data = {0};
Safae Ouajih5b01a882023-02-06 00:50:13 +0100386 ulong ramdisk_ptr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100387
Safae Ouajihc60ae102023-02-06 00:50:11 +0100388 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100389 return -EINVAL;
390
391 if (!img_data.ramdisk_size) {
Rob Herringd9798782015-10-05 14:37:07 -0500392 *rd_data = *rd_len = 0;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500393 return -1;
Rob Herringd9798782015-10-05 14:37:07 -0500394 }
Safae Ouajih5b01a882023-02-06 00:50:13 +0100395 if (img_data.header_version > 2) {
396 ramdisk_ptr = img_data.ramdisk_ptr;
397 memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
398 img_data.vendor_ramdisk_size);
399 memcpy((void *)(ramdisk_ptr + img_data.vendor_ramdisk_size),
Safae Ouajih313cc842023-02-06 00:50:18 +0100400 (void *)img_data.ramdisk_ptr,
401 img_data.boot_ramdisk_size);
402 if (img_data.bootconfig_size) {
403 memcpy((void *)
404 (ramdisk_ptr + img_data.vendor_ramdisk_size +
405 img_data.boot_ramdisk_size),
406 (void *)img_data.bootconfig_addr,
407 img_data.bootconfig_size);
408 }
Safae Ouajih5b01a882023-02-06 00:50:13 +0100409 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300410
Safae Ouajihbced1042023-02-06 00:50:08 +0100411 printf("RAM disk load addr 0x%08lx size %u KiB\n",
412 img_data.ramdisk_ptr, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300413
Safae Ouajihbced1042023-02-06 00:50:08 +0100414 *rd_data = img_data.ramdisk_ptr;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500415
Safae Ouajihbced1042023-02-06 00:50:08 +0100416 *rd_len = img_data.ramdisk_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500417 return 0;
418}
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200419
Safae Ouajih51c981b2023-02-06 00:50:17 +0100420int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len)
Bin Chen909f1402018-01-27 16:59:08 +1100421{
Safae Ouajihbced1042023-02-06 00:50:08 +0100422 struct andr_image_data img_data;
423
Safae Ouajihc60ae102023-02-06 00:50:11 +0100424 if (!android_image_get_data(hdr, NULL, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100425 return -EINVAL;
426
Safae Ouajih51c981b2023-02-06 00:50:17 +0100427 if (img_data.header_version > 2) {
428 printf("Second stage bootloader is only supported for boot image version <= 2\n");
429 return -EOPNOTSUPP;
430 }
431
Safae Ouajihbced1042023-02-06 00:50:08 +0100432 if (!img_data.second_size) {
Bin Chen909f1402018-01-27 16:59:08 +1100433 *second_data = *second_len = 0;
434 return -1;
435 }
436
Safae Ouajihbced1042023-02-06 00:50:08 +0100437 *second_data = img_data.second_ptr;
Bin Chen909f1402018-01-27 16:59:08 +1100438
439 printf("second address is 0x%lx\n",*second_data);
440
Safae Ouajihbced1042023-02-06 00:50:08 +0100441 *second_len = img_data.second_size;
Bin Chen909f1402018-01-27 16:59:08 +1100442 return 0;
443}
444
Sam Protsenko4fabf402020-01-24 17:53:40 +0200445/**
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200446 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
447 * @hdr_addr: Boot image header address
448 * @addr: If not NULL, will contain address of recovery DTBO image
449 * @size: If not NULL, will contain size of recovery DTBO image
450 *
451 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
452 * Boot Image in RAM. The format of this image is Android DTBO (see
453 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
454 * the address is obtained from this function, one can use 'adtimg' U-Boot
455 * command or android_dt_*() functions to extract desired DTBO blob.
456 *
457 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
458 * only can be found in recovery image. On A/B devices we can always rely on
459 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
460 * AOSP documentation for details.
461 *
462 * Return: true on success or false on error.
463 */
464bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
465{
Safae Ouajih8656e382023-02-06 00:50:03 +0100466 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200467 ulong dtbo_img_addr;
468 bool ret = true;
469
470 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100471 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200472 printf("Error: Boot Image header is incorrect\n");
473 ret = false;
474 goto exit;
475 }
476
Safae Ouajih8d351582023-02-06 00:50:10 +0100477 if (hdr->header_version != 1 && hdr->header_version != 2) {
478 printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200479 ret = false;
480 goto exit;
481 }
482
483 if (hdr->recovery_dtbo_size == 0) {
484 printf("Error: recovery_dtbo_size is 0\n");
485 ret = false;
486 goto exit;
487 }
488
489 /* Calculate the address of DTB area in boot image */
490 dtbo_img_addr = hdr_addr;
491 dtbo_img_addr += hdr->page_size;
492 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
493 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
494 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
495
496 if (addr)
497 *addr = dtbo_img_addr;
498 if (size)
499 *size = hdr->recovery_dtbo_size;
500
501exit:
502 unmap_sysmem(hdr);
503 return ret;
504}
505
506/**
Sam Protsenko4fabf402020-01-24 17:53:40 +0200507 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
508 * @hdr_addr: Boot image header address
Safae Ouajihc60ae102023-02-06 00:50:11 +0100509 * @vhdr_addr: Vendor Boot image header address
Sam Protsenko4fabf402020-01-24 17:53:40 +0200510 * @addr: Will contain the address of DTB area in boot image
511 *
512 * Return: true on success or false on fail.
513 */
Safae Ouajihc60ae102023-02-06 00:50:11 +0100514static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
Sam Protsenko4fabf402020-01-24 17:53:40 +0200515{
Safae Ouajih8656e382023-02-06 00:50:03 +0100516 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100517 const struct andr_vnd_boot_img_hdr *v_hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200518 ulong dtb_img_addr;
519 bool ret = true;
520
521 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100522 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko4fabf402020-01-24 17:53:40 +0200523 printf("Error: Boot Image header is incorrect\n");
524 ret = false;
525 goto exit;
526 }
527
528 if (hdr->header_version < 2) {
529 printf("Error: header_version must be >= 2 to get dtb\n");
530 ret = false;
531 goto exit;
532 }
533
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100534 if (hdr->header_version == 2) {
535 if (!hdr->dtb_size) {
536 printf("Error: dtb_size is 0\n");
537 ret = false;
538 goto exit;
539 }
540 /* Calculate the address of DTB area in boot image */
541 dtb_img_addr = hdr_addr;
542 dtb_img_addr += hdr->page_size;
543 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
544 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
545 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
546 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200547
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100548 *addr = dtb_img_addr;
549 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200550
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100551 if (hdr->header_version > 2) {
552 v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr));
553 if (!v_hdr->dtb_size) {
554 printf("Error: dtb_size is 0\n");
555 ret = false;
556 unmap_sysmem(v_hdr);
557 goto exit;
558 }
559 /* Calculate the address of DTB area in boot image */
560 dtb_img_addr = vhdr_addr;
561 dtb_img_addr += v_hdr->page_size;
562 if (v_hdr->vendor_ramdisk_size)
563 dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size);
564 *addr = dtb_img_addr;
565 unmap_sysmem(v_hdr);
566 goto exit;
567 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200568exit:
569 unmap_sysmem(hdr);
570 return ret;
571}
572
573/**
574 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
575 * @hdr_addr: Boot image header address
Safae Ouajihc60ae102023-02-06 00:50:11 +0100576 * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
Sam Protsenko4fabf402020-01-24 17:53:40 +0200577 * @index: Index of desired DTB in DTB area (starting from 0)
578 * @addr: If not NULL, will contain address to specified DTB
579 * @size: If not NULL, will contain size of specified DTB
580 *
581 * Get the address and size of DTB blob by its index in DTB area of Android
582 * Boot Image in RAM.
583 *
584 * Return: true on success or false on error.
585 */
Safae Ouajihc60ae102023-02-06 00:50:11 +0100586bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
587 u32 index, ulong *addr, u32 *size)
Sam Protsenko4fabf402020-01-24 17:53:40 +0200588{
Safae Ouajihbced1042023-02-06 00:50:08 +0100589 struct andr_image_data img_data;
Safae Ouajih8656e382023-02-06 00:50:03 +0100590 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajihc60ae102023-02-06 00:50:11 +0100591 const struct andr_vnd_boot_img_hdr *vhdr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100592
593 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajihc60ae102023-02-06 00:50:11 +0100594 if (vendor_boot_img != -1)
595 vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
596 if (!android_image_get_data(hdr, vhdr, &img_data)) {
597 if (vendor_boot_img != -1)
598 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +0100599 unmap_sysmem(hdr);
600 return false;
601 }
Safae Ouajihc60ae102023-02-06 00:50:11 +0100602 if (vendor_boot_img != -1)
603 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +0100604 unmap_sysmem(hdr);
605
Sam Protsenko4fabf402020-01-24 17:53:40 +0200606 ulong dtb_img_addr; /* address of DTB part in boot image */
607 u32 dtb_img_size; /* size of DTB payload in boot image */
608 ulong dtb_addr; /* address of DTB blob with specified index */
609 u32 i; /* index iterator */
610
Safae Ouajihc60ae102023-02-06 00:50:11 +0100611 android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200612 /* Check if DTB area of boot image is in DTBO format */
613 if (android_dt_check_header(dtb_img_addr)) {
614 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
615 size);
616 }
617
618 /* Find out the address of DTB with specified index in concat blobs */
Safae Ouajihbced1042023-02-06 00:50:08 +0100619 dtb_img_size = img_data.dtb_size;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200620 i = 0;
621 dtb_addr = dtb_img_addr;
622 while (dtb_addr < dtb_img_addr + dtb_img_size) {
623 const struct fdt_header *fdt;
624 u32 dtb_size;
625
626 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
627 if (fdt_check_header(fdt) != 0) {
628 unmap_sysmem(fdt);
629 printf("Error: Invalid FDT header for index %u\n", i);
630 return false;
631 }
632
633 dtb_size = fdt_totalsize(fdt);
634 unmap_sysmem(fdt);
635
636 if (i == index) {
637 if (size)
638 *size = dtb_size;
639 if (addr)
640 *addr = dtb_addr;
641 return true;
642 }
643
644 dtb_addr += dtb_size;
645 ++i;
646 }
647
648 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
649 return false;
650}
651
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200652#if !defined(CONFIG_SPL_BUILD)
653/**
654 * android_print_contents - prints out the contents of the Android format image
655 * @hdr: pointer to the Android format image header
656 *
657 * android_print_contents() formats a multi line Android image contents
658 * description.
659 * The routine prints out Android image properties
660 *
661 * returns:
662 * no returned results
663 */
Safae Ouajih8656e382023-02-06 00:50:03 +0100664void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200665{
Safae Ouajiha148a822023-02-06 00:50:09 +0100666 if (hdr->header_version >= 3) {
667 printf("Content print is not supported for boot image header version > 2");
668 return;
669 }
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200670 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo41f513a2017-04-02 01:49:47 -0700671 /* os_version = ver << 11 | lvl */
672 u32 os_ver = hdr->os_version >> 11;
673 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200674
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300675 printf("%skernel size: %x\n", p, hdr->kernel_size);
676 printf("%skernel address: %x\n", p, hdr->kernel_addr);
677 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
678 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
679 printf("%ssecond size: %x\n", p, hdr->second_size);
680 printf("%ssecond address: %x\n", p, hdr->second_addr);
681 printf("%stags address: %x\n", p, hdr->tags_addr);
682 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo41f513a2017-04-02 01:49:47 -0700683 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
684 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300685 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo41f513a2017-04-02 01:49:47 -0700686 p, hdr->os_version,
687 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
688 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300689 printf("%sname: %s\n", p, hdr->name);
690 printf("%scmdline: %s\n", p, hdr->cmdline);
691 printf("%sheader_version: %d\n", p, hdr->header_version);
692
693 if (hdr->header_version >= 1) {
694 printf("%srecovery dtbo size: %x\n", p,
695 hdr->recovery_dtbo_size);
696 printf("%srecovery dtbo offset: %llx\n", p,
697 hdr->recovery_dtbo_offset);
698 printf("%sheader size: %x\n", p,
699 hdr->header_size);
700 }
701
Safae Ouajiha148a822023-02-06 00:50:09 +0100702 if (hdr->header_version == 2) {
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300703 printf("%sdtb size: %x\n", p, hdr->dtb_size);
704 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
705 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200706}
707
708/**
709 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
710 * @fdt: DTB header
711 * @index: Number of DTB blob in DTB area.
712 *
713 * Return: true on success or false on error.
714 */
715static bool android_image_print_dtb_info(const struct fdt_header *fdt,
716 u32 index)
717{
718 int root_node_off;
719 u32 fdt_size;
720 const char *model;
721 const char *compatible;
722
723 root_node_off = fdt_path_offset(fdt, "/");
724 if (root_node_off < 0) {
725 printf("Error: Root node not found\n");
726 return false;
727 }
728
729 fdt_size = fdt_totalsize(fdt);
730 compatible = fdt_getprop(fdt, root_node_off, "compatible",
731 NULL);
732 model = fdt_getprop(fdt, root_node_off, "model", NULL);
733
734 printf(" - DTB #%u:\n", index);
735 printf(" (DTB)size = %d\n", fdt_size);
736 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
737 printf(" (DTB)compatible = %s\n",
738 compatible ? compatible : "(unknown)");
739
740 return true;
741}
742
743/**
744 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
745 * @hdr_addr: Boot image header address
746 *
747 * DTB payload in Android Boot Image v2+ can be in one of following formats:
748 * 1. Concatenated DTB blobs
749 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
750 *
751 * This function does next:
752 * 1. Prints out the format used in DTB area
753 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
754 * each blob.
755 *
756 * Return: true on success or false on error.
757 */
758bool android_image_print_dtb_contents(ulong hdr_addr)
759{
Safae Ouajih8656e382023-02-06 00:50:03 +0100760 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200761 bool res;
762 ulong dtb_img_addr; /* address of DTB part in boot image */
763 u32 dtb_img_size; /* size of DTB payload in boot image */
764 ulong dtb_addr; /* address of DTB blob with specified index */
765 u32 i; /* index iterator */
766
Safae Ouajihc60ae102023-02-06 00:50:11 +0100767 res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200768 if (!res)
769 return false;
770
771 /* Check if DTB area of boot image is in DTBO format */
772 if (android_dt_check_header(dtb_img_addr)) {
773 printf("## DTB area contents (DTBO format):\n");
774 android_dt_print_contents(dtb_img_addr);
775 return true;
776 }
777
778 printf("## DTB area contents (concat format):\n");
779
780 /* Iterate over concatenated DTB blobs */
781 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
782 dtb_img_size = hdr->dtb_size;
783 unmap_sysmem(hdr);
784 i = 0;
785 dtb_addr = dtb_img_addr;
786 while (dtb_addr < dtb_img_addr + dtb_img_size) {
787 const struct fdt_header *fdt;
788 u32 dtb_size;
789
790 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
791 if (fdt_check_header(fdt) != 0) {
792 unmap_sysmem(fdt);
793 printf("Error: Invalid FDT header for index %u\n", i);
794 return false;
795 }
796
797 res = android_image_print_dtb_info(fdt, i);
798 if (!res) {
799 unmap_sysmem(fdt);
800 return false;
801 }
802
803 dtb_size = fdt_totalsize(fdt);
804 unmap_sysmem(fdt);
805 dtb_addr += dtb_size;
806 ++i;
807 }
808
809 return true;
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200810}
811#endif