blob: 88e40bc7ec6c8a69c0b10885ea0bc5ebd31cf9a0 [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
6#include <common.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06007#include <env.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -05008#include <image.h>
Sam Protsenko4fabf402020-01-24 17:53:40 +02009#include <image-android-dt.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -050010#include <android_image.h>
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030011#include <malloc.h>
12#include <errno.h>
Eugeniu Rosca1403f392019-04-08 17:35:27 +020013#include <asm/unaligned.h>
Sam Protsenko4fabf402020-01-24 17:53:40 +020014#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <linux/libfdt.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -050016
Maxime Ripard17ef1f52015-04-24 12:53:12 +020017#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
18
Sebastian Siewior686d6672014-05-05 15:08:09 -050019static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
20
Safae Ouajih313cc842023-02-06 00:50:18 +010021static ulong checksum(const unsigned char *buffer, ulong size)
22{
23 ulong sum = 0;
24
25 for (ulong i = 0; i < size; i++)
26 sum += buffer[i];
27 return sum;
28}
29
30static bool is_trailer_present(ulong bootconfig_end_addr)
31{
32 return !strncmp((char *)(bootconfig_end_addr - BOOTCONFIG_MAGIC_SIZE),
33 BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
34}
35
36static ulong add_trailer(ulong bootconfig_start_addr, ulong bootconfig_size)
37{
38 ulong end;
39 ulong sum;
40
41 if (!bootconfig_start_addr)
42 return -1;
43 if (!bootconfig_size)
44 return 0;
45
46 end = bootconfig_start_addr + bootconfig_size;
47 if (is_trailer_present(end))
48 return 0;
49
50 memcpy((void *)(end), &bootconfig_size, BOOTCONFIG_SIZE_SIZE);
51 sum = checksum((unsigned char *)bootconfig_start_addr, bootconfig_size);
52 memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE), &sum,
53 BOOTCONFIG_CHECKSUM_SIZE);
54 memcpy((void *)(end + BOOTCONFIG_SIZE_SIZE + BOOTCONFIG_CHECKSUM_SIZE),
55 BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_SIZE);
56
57 return BOOTCONFIG_TRAILER_SIZE;
58}
59
Safae Ouajih889005f2023-02-06 00:50:12 +010060static void android_boot_image_v3_v4_parse_hdr(const struct andr_boot_img_hdr_v3 *hdr,
61 struct andr_image_data *data)
62{
63 ulong end;
64
65 data->kcmdline = hdr->cmdline;
66 data->header_version = hdr->header_version;
Safae Ouajih5b01a882023-02-06 00:50:13 +010067 data->ramdisk_ptr = env_get_ulong("ramdisk_addr_r", 16, 0);
Safae Ouajih889005f2023-02-06 00:50:12 +010068
69 /*
70 * The header takes a full page, the remaining components are aligned
71 * on page boundary.
72 */
73 end = (ulong)hdr;
74 end += ANDR_GKI_PAGE_SIZE;
75 data->kernel_ptr = end;
76 data->kernel_size = hdr->kernel_size;
77 end += ALIGN(hdr->kernel_size, ANDR_GKI_PAGE_SIZE);
78 data->ramdisk_size = hdr->ramdisk_size;
79 data->boot_ramdisk_size = hdr->ramdisk_size;
80 end += ALIGN(hdr->ramdisk_size, ANDR_GKI_PAGE_SIZE);
81
82 if (hdr->header_version > 3)
83 end += ALIGN(hdr->signature_size, ANDR_GKI_PAGE_SIZE);
84
85 data->boot_img_total_size = end - (ulong)hdr;
86}
87
88static void android_vendor_boot_image_v3_v4_parse_hdr(const struct andr_vnd_boot_img_hdr
89 *hdr, struct andr_image_data *data)
90{
91 ulong end;
92
93 /*
94 * The header takes a full page, the remaining components are aligned
95 * on page boundary.
96 */
Safae Ouajih6665296e2023-02-06 00:50:14 +010097 data->kcmdline_extra = hdr->cmdline;
Safae Ouajih889005f2023-02-06 00:50:12 +010098 data->tags_addr = hdr->tags_addr;
99 data->image_name = hdr->name;
100 data->kernel_addr = hdr->kernel_addr;
101 data->ramdisk_addr = hdr->ramdisk_addr;
102 data->dtb_load_addr = hdr->dtb_addr;
Safae Ouajih313cc842023-02-06 00:50:18 +0100103 data->bootconfig_size = hdr->bootconfig_size;
Safae Ouajih889005f2023-02-06 00:50:12 +0100104 end = (ulong)hdr;
105 end += hdr->page_size;
106 if (hdr->vendor_ramdisk_size) {
107 data->vendor_ramdisk_ptr = end;
108 data->vendor_ramdisk_size = hdr->vendor_ramdisk_size;
109 data->ramdisk_size += hdr->vendor_ramdisk_size;
110 end += ALIGN(hdr->vendor_ramdisk_size, hdr->page_size);
111 }
112
113 data->dtb_ptr = end;
114 data->dtb_size = hdr->dtb_size;
115
116 end += ALIGN(hdr->dtb_size, hdr->page_size);
117 end += ALIGN(hdr->vendor_ramdisk_table_size, hdr->page_size);
Safae Ouajih313cc842023-02-06 00:50:18 +0100118 data->bootconfig_addr = end;
119 if (hdr->bootconfig_size) {
120 data->bootconfig_size += add_trailer(data->bootconfig_addr,
121 data->bootconfig_size);
122 data->ramdisk_size += data->bootconfig_size;
123 }
124 end += ALIGN(data->bootconfig_size, hdr->page_size);
Safae Ouajih889005f2023-02-06 00:50:12 +0100125 data->vendor_boot_img_total_size = end - (ulong)hdr;
126}
127
Safae Ouajih027191d2023-02-06 00:50:07 +0100128static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr,
129 struct andr_image_data *data)
130{
131 ulong end;
132
133 data->image_name = hdr->name;
134 data->kcmdline = hdr->cmdline;
135 data->kernel_addr = hdr->kernel_addr;
136 data->ramdisk_addr = hdr->ramdisk_addr;
137 data->header_version = hdr->header_version;
138 data->dtb_load_addr = hdr->dtb_addr;
139
140 end = (ulong)hdr;
141
142 /*
143 * The header takes a full page, the remaining components are aligned
144 * on page boundary
145 */
146
147 end += hdr->page_size;
148
149 data->kernel_ptr = end;
150 data->kernel_size = hdr->kernel_size;
151 end += ALIGN(hdr->kernel_size, hdr->page_size);
152
153 data->ramdisk_ptr = end;
154 data->ramdisk_size = hdr->ramdisk_size;
155 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
156
157 data->second_ptr = end;
158 data->second_size = hdr->second_size;
159 end += ALIGN(hdr->second_size, hdr->page_size);
160
161 if (hdr->header_version >= 1) {
162 data->recovery_dtbo_ptr = end;
163 data->recovery_dtbo_size = hdr->recovery_dtbo_size;
164 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
165 }
166
167 if (hdr->header_version >= 2) {
168 data->dtb_ptr = end;
169 data->dtb_size = hdr->dtb_size;
170 end += ALIGN(hdr->dtb_size, hdr->page_size);
171 }
172
173 data->boot_img_total_size = end - (ulong)hdr;
174}
175
Safae Ouajihc60ae102023-02-06 00:50:11 +0100176bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
177 struct andr_image_data *data)
Safae Ouajih027191d2023-02-06 00:50:07 +0100178{
179 if (!boot_hdr || !data) {
180 printf("boot_hdr or data params can't be NULL\n");
181 return false;
182 }
183
184 if (!is_android_boot_image_header(boot_hdr)) {
185 printf("Incorrect boot image header\n");
186 return false;
187 }
188
Safae Ouajih889005f2023-02-06 00:50:12 +0100189 if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2) {
190 if (!vendor_boot_hdr) {
191 printf("For boot header v3+ vendor boot image has to be provided\n");
192 return false;
193 }
194 if (!is_android_vendor_boot_image_header(vendor_boot_hdr)) {
195 printf("Incorrect vendor boot image header\n");
196 return false;
197 }
198 android_boot_image_v3_v4_parse_hdr(boot_hdr, data);
199 android_vendor_boot_image_v3_v4_parse_hdr(vendor_boot_hdr, data);
200 } else {
Safae Ouajih027191d2023-02-06 00:50:07 +0100201 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
Safae Ouajih889005f2023-02-06 00:50:12 +0100202 }
Safae Ouajih027191d2023-02-06 00:50:07 +0100203
204 return true;
205}
206
Safae Ouajihbced1042023-02-06 00:50:08 +0100207static ulong android_image_get_kernel_addr(struct andr_image_data *img_data)
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200208{
209 /*
210 * All the Android tools that generate a boot.img use this
211 * address as the default.
212 *
213 * Even though it doesn't really make a lot of sense, and it
214 * might be valid on some platforms, we treat that adress as
215 * the default value for this field, and try to execute the
216 * kernel in place in such a case.
217 *
218 * Otherwise, we will return the actual value set by the user.
219 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100220 if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
221 return img_data->kernel_ptr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200222
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200223 /*
224 * abootimg creates images where all load addresses are 0
225 * and we need to fix them.
226 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100227 if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
Christian Gmeiner3fabf862020-05-29 17:53:45 +0200228 return env_get_ulong("kernel_addr_r", 16, 0);
229
Safae Ouajihbced1042023-02-06 00:50:08 +0100230 return img_data->kernel_addr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200231}
232
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300233/**
234 * android_image_get_kernel() - processes kernel part of Android boot images
Safae Ouajihc60ae102023-02-06 00:50:11 +0100235 * @hdr: Pointer to boot image header, which is at the start
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300236 * of the image.
Safae Ouajihc60ae102023-02-06 00:50:11 +0100237 * @vendor_boot_img: Pointer to vendor boot image header, which is at the
238 * start of the image.
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300239 * @verify: Checksum verification flag. Currently unimplemented.
240 * @os_data: Pointer to a ulong variable, will hold os data start
241 * address.
242 * @os_len: Pointer to a ulong variable, will hold os data length.
243 *
244 * This function returns the os image's start address and length. Also,
245 * it appends the kernel command line to the bootargs env variable.
246 *
247 * Return: Zero, os start address and length on success,
248 * otherwise on failure.
249 */
Safae Ouajih51c981b2023-02-06 00:50:17 +0100250int android_image_get_kernel(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100251 const void *vendor_boot_img, int verify,
Sebastian Siewior686d6672014-05-05 15:08:09 -0500252 ulong *os_data, ulong *os_len)
253{
Safae Ouajihbced1042023-02-06 00:50:08 +0100254 struct andr_image_data img_data = {0};
255 u32 kernel_addr;
256 const struct legacy_img_hdr *ihdr;
257
Safae Ouajihc60ae102023-02-06 00:50:11 +0100258 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100259 return -EINVAL;
260
261 kernel_addr = android_image_get_kernel_addr(&img_data);
262 ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200263
Sebastian Siewior686d6672014-05-05 15:08:09 -0500264 /*
265 * Not all Android tools use the id field for signing the image with
266 * sha1 (or anything) so we don't check it. It is not obvious that the
267 * string is null terminated so we take care of this.
268 */
Safae Ouajihbced1042023-02-06 00:50:08 +0100269 strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500270 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
271 if (strlen(andr_tmp_str))
272 printf("Android's image name: %s\n", andr_tmp_str);
273
274 printf("Kernel load addr 0x%08x size %u KiB\n",
Safae Ouajihbced1042023-02-06 00:50:08 +0100275 kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300276
277 int len = 0;
Safae Ouajihbced1042023-02-06 00:50:08 +0100278 if (*img_data.kcmdline) {
279 printf("Kernel command line: %s\n", img_data.kcmdline);
280 len += strlen(img_data.kcmdline);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500281 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300282
Safae Ouajih6665296e2023-02-06 00:50:14 +0100283 if (img_data.kcmdline_extra) {
284 printf("Kernel extra command line: %s\n", img_data.kcmdline_extra);
285 len += strlen(img_data.kcmdline_extra);
286 }
287
Simon Glass64b723f2017-08-03 12:22:12 -0600288 char *bootargs = env_get("bootargs");
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300289 if (bootargs)
290 len += strlen(bootargs);
291
292 char *newbootargs = malloc(len + 2);
293 if (!newbootargs) {
294 puts("Error: malloc in android_image_get_kernel failed!\n");
295 return -ENOMEM;
296 }
297 *newbootargs = '\0';
298
299 if (bootargs) {
300 strcpy(newbootargs, bootargs);
301 strcat(newbootargs, " ");
302 }
Safae Ouajihbced1042023-02-06 00:50:08 +0100303
304 if (*img_data.kcmdline)
305 strcat(newbootargs, img_data.kcmdline);
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300306
Safae Ouajih6665296e2023-02-06 00:50:14 +0100307 if (img_data.kcmdline_extra) {
308 strcat(newbootargs, " ");
309 strcat(newbootargs, img_data.kcmdline_extra);
310 }
311
Simon Glass6a38e412017-08-03 12:22:09 -0600312 env_set("bootargs", newbootargs);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500313
314 if (os_data) {
Roman Stratiienko73268582019-06-03 15:38:13 +0300315 if (image_get_magic(ihdr) == IH_MAGIC) {
316 *os_data = image_get_data(ihdr);
317 } else {
Safae Ouajihbced1042023-02-06 00:50:08 +0100318 *os_data = img_data.kernel_ptr;
Roman Stratiienko73268582019-06-03 15:38:13 +0300319 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500320 }
Roman Stratiienko73268582019-06-03 15:38:13 +0300321 if (os_len) {
322 if (image_get_magic(ihdr) == IH_MAGIC)
323 *os_len = image_get_data_size(ihdr);
324 else
Safae Ouajihbced1042023-02-06 00:50:08 +0100325 *os_len = img_data.kernel_size;
Roman Stratiienko73268582019-06-03 15:38:13 +0300326 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500327 return 0;
328}
329
Safae Ouajih889005f2023-02-06 00:50:12 +0100330bool is_android_vendor_boot_image_header(const void *vendor_boot_img)
331{
332 return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE);
333}
334
Safae Ouajih51c981b2023-02-06 00:50:17 +0100335bool is_android_boot_image_header(const void *hdr)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500336{
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100337 return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500338}
339
Safae Ouajihc60ae102023-02-06 00:50:11 +0100340ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
341 const void *vendor_boot_img)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500342{
Safae Ouajihbced1042023-02-06 00:50:08 +0100343 struct andr_image_data img_data;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500344
Safae Ouajihc60ae102023-02-06 00:50:11 +0100345 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100346 return -EINVAL;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300347
Safae Ouajihbced1042023-02-06 00:50:08 +0100348 if (img_data.header_version > 2)
349 return 0;
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300350
Safae Ouajihbced1042023-02-06 00:50:08 +0100351 return img_data.boot_img_total_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500352}
353
Safae Ouajih51c981b2023-02-06 00:50:17 +0100354ulong android_image_get_kload(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100355 const void *vendor_boot_img)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500356{
Safae Ouajihbced1042023-02-06 00:50:08 +0100357 struct andr_image_data img_data;
358
Safae Ouajihc60ae102023-02-06 00:50:11 +0100359 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100360 return -EINVAL;
361
362 return android_image_get_kernel_addr(&img_data);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500363}
364
Safae Ouajih51c981b2023-02-06 00:50:17 +0100365ulong android_image_get_kcomp(const void *hdr,
Safae Ouajihc60ae102023-02-06 00:50:11 +0100366 const void *vendor_boot_img)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200367{
Safae Ouajih027191d2023-02-06 00:50:07 +0100368 struct andr_image_data img_data;
369 const void *p;
370
Safae Ouajihc60ae102023-02-06 00:50:11 +0100371 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih027191d2023-02-06 00:50:07 +0100372 return -EINVAL;
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200373
Safae Ouajih027191d2023-02-06 00:50:07 +0100374 p = (const void *)img_data.kernel_ptr;
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600375 if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
376 return image_get_comp((struct legacy_img_hdr *)p);
Roman Stratiienko73268582019-06-03 15:38:13 +0300377 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200378 return IH_COMP_LZ4;
379 else
Stephan Gerhold9e5111d2021-07-01 20:33:16 +0200380 return image_decomp_type(p, sizeof(u32));
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200381}
382
Safae Ouajih5b01a882023-02-06 00:50:13 +0100383int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
384 ulong *rd_data, ulong *rd_len)
Sebastian Siewior686d6672014-05-05 15:08:09 -0500385{
Safae Ouajihbced1042023-02-06 00:50:08 +0100386 struct andr_image_data img_data = {0};
Safae Ouajih5b01a882023-02-06 00:50:13 +0100387 ulong ramdisk_ptr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100388
Safae Ouajihc60ae102023-02-06 00:50:11 +0100389 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100390 return -EINVAL;
391
392 if (!img_data.ramdisk_size) {
Rob Herringd9798782015-10-05 14:37:07 -0500393 *rd_data = *rd_len = 0;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500394 return -1;
Rob Herringd9798782015-10-05 14:37:07 -0500395 }
Safae Ouajih5b01a882023-02-06 00:50:13 +0100396 if (img_data.header_version > 2) {
397 ramdisk_ptr = img_data.ramdisk_ptr;
398 memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
399 img_data.vendor_ramdisk_size);
400 memcpy((void *)(ramdisk_ptr + img_data.vendor_ramdisk_size),
Safae Ouajih313cc842023-02-06 00:50:18 +0100401 (void *)img_data.ramdisk_ptr,
402 img_data.boot_ramdisk_size);
403 if (img_data.bootconfig_size) {
404 memcpy((void *)
405 (ramdisk_ptr + img_data.vendor_ramdisk_size +
406 img_data.boot_ramdisk_size),
407 (void *)img_data.bootconfig_addr,
408 img_data.bootconfig_size);
409 }
Safae Ouajih5b01a882023-02-06 00:50:13 +0100410 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300411
Safae Ouajihbced1042023-02-06 00:50:08 +0100412 printf("RAM disk load addr 0x%08lx size %u KiB\n",
413 img_data.ramdisk_ptr, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300414
Safae Ouajihbced1042023-02-06 00:50:08 +0100415 *rd_data = img_data.ramdisk_ptr;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500416
Safae Ouajihbced1042023-02-06 00:50:08 +0100417 *rd_len = img_data.ramdisk_size;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500418 return 0;
419}
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200420
Safae Ouajih51c981b2023-02-06 00:50:17 +0100421int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len)
Bin Chen909f1402018-01-27 16:59:08 +1100422{
Safae Ouajihbced1042023-02-06 00:50:08 +0100423 struct andr_image_data img_data;
424
Safae Ouajihc60ae102023-02-06 00:50:11 +0100425 if (!android_image_get_data(hdr, NULL, &img_data))
Safae Ouajihbced1042023-02-06 00:50:08 +0100426 return -EINVAL;
427
Safae Ouajih51c981b2023-02-06 00:50:17 +0100428 if (img_data.header_version > 2) {
429 printf("Second stage bootloader is only supported for boot image version <= 2\n");
430 return -EOPNOTSUPP;
431 }
432
Safae Ouajihbced1042023-02-06 00:50:08 +0100433 if (!img_data.second_size) {
Bin Chen909f1402018-01-27 16:59:08 +1100434 *second_data = *second_len = 0;
435 return -1;
436 }
437
Safae Ouajihbced1042023-02-06 00:50:08 +0100438 *second_data = img_data.second_ptr;
Bin Chen909f1402018-01-27 16:59:08 +1100439
440 printf("second address is 0x%lx\n",*second_data);
441
Safae Ouajihbced1042023-02-06 00:50:08 +0100442 *second_len = img_data.second_size;
Bin Chen909f1402018-01-27 16:59:08 +1100443 return 0;
444}
445
Sam Protsenko4fabf402020-01-24 17:53:40 +0200446/**
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200447 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
448 * @hdr_addr: Boot image header address
449 * @addr: If not NULL, will contain address of recovery DTBO image
450 * @size: If not NULL, will contain size of recovery DTBO image
451 *
452 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
453 * Boot Image in RAM. The format of this image is Android DTBO (see
454 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
455 * the address is obtained from this function, one can use 'adtimg' U-Boot
456 * command or android_dt_*() functions to extract desired DTBO blob.
457 *
458 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
459 * only can be found in recovery image. On A/B devices we can always rely on
460 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
461 * AOSP documentation for details.
462 *
463 * Return: true on success or false on error.
464 */
465bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
466{
Safae Ouajih8656e382023-02-06 00:50:03 +0100467 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200468 ulong dtbo_img_addr;
469 bool ret = true;
470
471 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100472 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200473 printf("Error: Boot Image header is incorrect\n");
474 ret = false;
475 goto exit;
476 }
477
Safae Ouajih8d351582023-02-06 00:50:10 +0100478 if (hdr->header_version != 1 && hdr->header_version != 2) {
479 printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
Sam Protsenko2666a1a2020-01-24 17:53:41 +0200480 ret = false;
481 goto exit;
482 }
483
484 if (hdr->recovery_dtbo_size == 0) {
485 printf("Error: recovery_dtbo_size is 0\n");
486 ret = false;
487 goto exit;
488 }
489
490 /* Calculate the address of DTB area in boot image */
491 dtbo_img_addr = hdr_addr;
492 dtbo_img_addr += hdr->page_size;
493 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
494 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
495 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
496
497 if (addr)
498 *addr = dtbo_img_addr;
499 if (size)
500 *size = hdr->recovery_dtbo_size;
501
502exit:
503 unmap_sysmem(hdr);
504 return ret;
505}
506
507/**
Sam Protsenko4fabf402020-01-24 17:53:40 +0200508 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
509 * @hdr_addr: Boot image header address
Safae Ouajihc60ae102023-02-06 00:50:11 +0100510 * @vhdr_addr: Vendor Boot image header address
Sam Protsenko4fabf402020-01-24 17:53:40 +0200511 * @addr: Will contain the address of DTB area in boot image
512 *
513 * Return: true on success or false on fail.
514 */
Safae Ouajihc60ae102023-02-06 00:50:11 +0100515static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
Sam Protsenko4fabf402020-01-24 17:53:40 +0200516{
Safae Ouajih8656e382023-02-06 00:50:03 +0100517 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100518 const struct andr_vnd_boot_img_hdr *v_hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200519 ulong dtb_img_addr;
520 bool ret = true;
521
522 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100523 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko4fabf402020-01-24 17:53:40 +0200524 printf("Error: Boot Image header is incorrect\n");
525 ret = false;
526 goto exit;
527 }
528
529 if (hdr->header_version < 2) {
530 printf("Error: header_version must be >= 2 to get dtb\n");
531 ret = false;
532 goto exit;
533 }
534
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100535 if (hdr->header_version == 2) {
536 if (!hdr->dtb_size) {
537 printf("Error: dtb_size is 0\n");
538 ret = false;
539 goto exit;
540 }
541 /* Calculate the address of DTB area in boot image */
542 dtb_img_addr = hdr_addr;
543 dtb_img_addr += hdr->page_size;
544 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
545 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
546 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
547 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200548
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100549 *addr = dtb_img_addr;
550 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200551
Safae Ouajih9a5cc7f2023-02-06 00:50:15 +0100552 if (hdr->header_version > 2) {
553 v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr));
554 if (!v_hdr->dtb_size) {
555 printf("Error: dtb_size is 0\n");
556 ret = false;
557 unmap_sysmem(v_hdr);
558 goto exit;
559 }
560 /* Calculate the address of DTB area in boot image */
561 dtb_img_addr = vhdr_addr;
562 dtb_img_addr += v_hdr->page_size;
563 if (v_hdr->vendor_ramdisk_size)
564 dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size);
565 *addr = dtb_img_addr;
566 unmap_sysmem(v_hdr);
567 goto exit;
568 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200569exit:
570 unmap_sysmem(hdr);
571 return ret;
572}
573
574/**
575 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
576 * @hdr_addr: Boot image header address
Safae Ouajihc60ae102023-02-06 00:50:11 +0100577 * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
Sam Protsenko4fabf402020-01-24 17:53:40 +0200578 * @index: Index of desired DTB in DTB area (starting from 0)
579 * @addr: If not NULL, will contain address to specified DTB
580 * @size: If not NULL, will contain size of specified DTB
581 *
582 * Get the address and size of DTB blob by its index in DTB area of Android
583 * Boot Image in RAM.
584 *
585 * Return: true on success or false on error.
586 */
Safae Ouajihc60ae102023-02-06 00:50:11 +0100587bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
588 u32 index, ulong *addr, u32 *size)
Sam Protsenko4fabf402020-01-24 17:53:40 +0200589{
Safae Ouajihbced1042023-02-06 00:50:08 +0100590 struct andr_image_data img_data;
Safae Ouajih8656e382023-02-06 00:50:03 +0100591 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajihc60ae102023-02-06 00:50:11 +0100592 const struct andr_vnd_boot_img_hdr *vhdr;
Safae Ouajihbced1042023-02-06 00:50:08 +0100593
594 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajihc60ae102023-02-06 00:50:11 +0100595 if (vendor_boot_img != -1)
596 vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
597 if (!android_image_get_data(hdr, vhdr, &img_data)) {
598 if (vendor_boot_img != -1)
599 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +0100600 unmap_sysmem(hdr);
601 return false;
602 }
Safae Ouajihc60ae102023-02-06 00:50:11 +0100603 if (vendor_boot_img != -1)
604 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +0100605 unmap_sysmem(hdr);
606
Sam Protsenko4fabf402020-01-24 17:53:40 +0200607 ulong dtb_img_addr; /* address of DTB part in boot image */
608 u32 dtb_img_size; /* size of DTB payload in boot image */
609 ulong dtb_addr; /* address of DTB blob with specified index */
610 u32 i; /* index iterator */
611
Safae Ouajihc60ae102023-02-06 00:50:11 +0100612 android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200613 /* Check if DTB area of boot image is in DTBO format */
614 if (android_dt_check_header(dtb_img_addr)) {
615 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
616 size);
617 }
618
619 /* Find out the address of DTB with specified index in concat blobs */
Safae Ouajihbced1042023-02-06 00:50:08 +0100620 dtb_img_size = img_data.dtb_size;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200621 i = 0;
622 dtb_addr = dtb_img_addr;
623 while (dtb_addr < dtb_img_addr + dtb_img_size) {
624 const struct fdt_header *fdt;
625 u32 dtb_size;
626
627 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
628 if (fdt_check_header(fdt) != 0) {
629 unmap_sysmem(fdt);
630 printf("Error: Invalid FDT header for index %u\n", i);
631 return false;
632 }
633
634 dtb_size = fdt_totalsize(fdt);
635 unmap_sysmem(fdt);
636
637 if (i == index) {
638 if (size)
639 *size = dtb_size;
640 if (addr)
641 *addr = dtb_addr;
642 return true;
643 }
644
645 dtb_addr += dtb_size;
646 ++i;
647 }
648
649 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
650 return false;
651}
652
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200653#if !defined(CONFIG_SPL_BUILD)
654/**
655 * android_print_contents - prints out the contents of the Android format image
656 * @hdr: pointer to the Android format image header
657 *
658 * android_print_contents() formats a multi line Android image contents
659 * description.
660 * The routine prints out Android image properties
661 *
662 * returns:
663 * no returned results
664 */
Safae Ouajih8656e382023-02-06 00:50:03 +0100665void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200666{
Safae Ouajiha148a822023-02-06 00:50:09 +0100667 if (hdr->header_version >= 3) {
668 printf("Content print is not supported for boot image header version > 2");
669 return;
670 }
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200671 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo41f513a2017-04-02 01:49:47 -0700672 /* os_version = ver << 11 | lvl */
673 u32 os_ver = hdr->os_version >> 11;
674 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200675
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300676 printf("%skernel size: %x\n", p, hdr->kernel_size);
677 printf("%skernel address: %x\n", p, hdr->kernel_addr);
678 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
679 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
680 printf("%ssecond size: %x\n", p, hdr->second_size);
681 printf("%ssecond address: %x\n", p, hdr->second_addr);
682 printf("%stags address: %x\n", p, hdr->tags_addr);
683 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo41f513a2017-04-02 01:49:47 -0700684 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
685 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300686 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo41f513a2017-04-02 01:49:47 -0700687 p, hdr->os_version,
688 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
689 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300690 printf("%sname: %s\n", p, hdr->name);
691 printf("%scmdline: %s\n", p, hdr->cmdline);
692 printf("%sheader_version: %d\n", p, hdr->header_version);
693
694 if (hdr->header_version >= 1) {
695 printf("%srecovery dtbo size: %x\n", p,
696 hdr->recovery_dtbo_size);
697 printf("%srecovery dtbo offset: %llx\n", p,
698 hdr->recovery_dtbo_offset);
699 printf("%sheader size: %x\n", p,
700 hdr->header_size);
701 }
702
Safae Ouajiha148a822023-02-06 00:50:09 +0100703 if (hdr->header_version == 2) {
Sam Protsenko9e4982c2019-08-15 20:25:07 +0300704 printf("%sdtb size: %x\n", p, hdr->dtb_size);
705 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
706 }
Sam Protsenko4fabf402020-01-24 17:53:40 +0200707}
708
709/**
710 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
711 * @fdt: DTB header
712 * @index: Number of DTB blob in DTB area.
713 *
714 * Return: true on success or false on error.
715 */
716static bool android_image_print_dtb_info(const struct fdt_header *fdt,
717 u32 index)
718{
719 int root_node_off;
720 u32 fdt_size;
721 const char *model;
722 const char *compatible;
723
724 root_node_off = fdt_path_offset(fdt, "/");
725 if (root_node_off < 0) {
726 printf("Error: Root node not found\n");
727 return false;
728 }
729
730 fdt_size = fdt_totalsize(fdt);
731 compatible = fdt_getprop(fdt, root_node_off, "compatible",
732 NULL);
733 model = fdt_getprop(fdt, root_node_off, "model", NULL);
734
735 printf(" - DTB #%u:\n", index);
736 printf(" (DTB)size = %d\n", fdt_size);
737 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
738 printf(" (DTB)compatible = %s\n",
739 compatible ? compatible : "(unknown)");
740
741 return true;
742}
743
744/**
745 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
746 * @hdr_addr: Boot image header address
747 *
748 * DTB payload in Android Boot Image v2+ can be in one of following formats:
749 * 1. Concatenated DTB blobs
750 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
751 *
752 * This function does next:
753 * 1. Prints out the format used in DTB area
754 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
755 * each blob.
756 *
757 * Return: true on success or false on error.
758 */
759bool android_image_print_dtb_contents(ulong hdr_addr)
760{
Safae Ouajih8656e382023-02-06 00:50:03 +0100761 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko4fabf402020-01-24 17:53:40 +0200762 bool res;
763 ulong dtb_img_addr; /* address of DTB part in boot image */
764 u32 dtb_img_size; /* size of DTB payload in boot image */
765 ulong dtb_addr; /* address of DTB blob with specified index */
766 u32 i; /* index iterator */
767
Safae Ouajihc60ae102023-02-06 00:50:11 +0100768 res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
Sam Protsenko4fabf402020-01-24 17:53:40 +0200769 if (!res)
770 return false;
771
772 /* Check if DTB area of boot image is in DTBO format */
773 if (android_dt_check_header(dtb_img_addr)) {
774 printf("## DTB area contents (DTBO format):\n");
775 android_dt_print_contents(dtb_img_addr);
776 return true;
777 }
778
779 printf("## DTB area contents (concat format):\n");
780
781 /* Iterate over concatenated DTB blobs */
782 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
783 dtb_img_size = hdr->dtb_size;
784 unmap_sysmem(hdr);
785 i = 0;
786 dtb_addr = dtb_img_addr;
787 while (dtb_addr < dtb_img_addr + dtb_img_size) {
788 const struct fdt_header *fdt;
789 u32 dtb_size;
790
791 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
792 if (fdt_check_header(fdt) != 0) {
793 unmap_sysmem(fdt);
794 printf("Error: Invalid FDT header for index %u\n", i);
795 return false;
796 }
797
798 res = android_image_print_dtb_info(fdt, i);
799 if (!res) {
800 unmap_sysmem(fdt);
801 return false;
802 }
803
804 dtb_size = fdt_totalsize(fdt);
805 unmap_sysmem(fdt);
806 dtb_addr += dtb_size;
807 ++i;
808 }
809
810 return true;
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200811}
812#endif