blob: 774565fd1fea0abc8445834cc90647752062682b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05002/*
3 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05004 */
5
Simon Glass9fb625c2019-08-01 09:46:51 -06006#include <env.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05007#include <image.h>
Sam Protsenkoc3bfad82020-01-24 17:53:40 +02008#include <image-android-dt.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05009#include <android_image.h>
Ahmad Draidi86f46952014-10-23 20:50:07 +030010#include <malloc.h>
11#include <errno.h>
Eugeniu Rosca829ceb22019-04-08 17:35:27 +020012#include <asm/unaligned.h>
Sam Protsenkoc3bfad82020-01-24 17:53:40 +020013#include <mapmem.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <linux/libfdt.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050015
Maxime Ripard87f02d52015-04-24 12:53:12 +020016#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
17
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050018static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
19
Safae Ouajih57e405e2023-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 Korpershoek58fed992024-07-10 10:40:02 +020059__weak ulong get_avendor_bootimg_addr(void)
60{
61 return -1;
62}
63
Safae Ouajih11150272023-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 Stratiienkoda3447d2024-05-19 13:09:51 +000081 data->ramdisk_ptr = end;
Safae Ouajih11150272023-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 Ouajihb36b2272023-02-06 00:50:14 +0100101 data->kcmdline_extra = hdr->cmdline;
Safae Ouajih11150272023-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 Ouajih57e405e2023-02-06 00:50:18 +0100107 data->bootconfig_size = hdr->bootconfig_size;
Safae Ouajih11150272023-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 Ouajih57e405e2023-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 Ouajih11150272023-02-06 00:50:12 +0100129 data->vendor_boot_img_total_size = end - (ulong)hdr;
130}
131
Safae Ouajihf48efa02023-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 Ouajihe0581762023-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 Ouajihf48efa02023-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 Ouajih11150272023-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 Ouajihf48efa02023-02-06 00:50:07 +0100205 android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data);
Safae Ouajih11150272023-02-06 00:50:12 +0100206 }
Safae Ouajihf48efa02023-02-06 00:50:07 +0100207
208 return true;
209}
210
Safae Ouajih607b0752023-02-06 00:50:08 +0100211static ulong android_image_get_kernel_addr(struct andr_image_data *img_data)
Maxime Ripard87f02d52015-04-24 12:53:12 +0200212{
213 /*
214 * All the Android tools that generate a boot.img use this
215 * address as the default.
216 *
217 * Even though it doesn't really make a lot of sense, and it
218 * might be valid on some platforms, we treat that adress as
219 * the default value for this field, and try to execute the
220 * kernel in place in such a case.
221 *
222 * Otherwise, we will return the actual value set by the user.
223 */
Safae Ouajih607b0752023-02-06 00:50:08 +0100224 if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
225 return img_data->kernel_ptr;
Maxime Ripard87f02d52015-04-24 12:53:12 +0200226
Christian Gmeiner95712af2020-05-29 17:53:45 +0200227 /*
228 * abootimg creates images where all load addresses are 0
229 * and we need to fix them.
230 */
Safae Ouajih607b0752023-02-06 00:50:08 +0100231 if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0)
Christian Gmeiner95712af2020-05-29 17:53:45 +0200232 return env_get_ulong("kernel_addr_r", 16, 0);
233
Safae Ouajih607b0752023-02-06 00:50:08 +0100234 return img_data->kernel_addr;
Maxime Ripard87f02d52015-04-24 12:53:12 +0200235}
236
Ahmad Draidi86f46952014-10-23 20:50:07 +0300237/**
238 * android_image_get_kernel() - processes kernel part of Android boot images
Safae Ouajihe0581762023-02-06 00:50:11 +0100239 * @hdr: Pointer to boot image header, which is at the start
Ahmad Draidi86f46952014-10-23 20:50:07 +0300240 * of the image.
Safae Ouajihe0581762023-02-06 00:50:11 +0100241 * @vendor_boot_img: Pointer to vendor boot image header, which is at the
242 * start of the image.
Ahmad Draidi86f46952014-10-23 20:50:07 +0300243 * @verify: Checksum verification flag. Currently unimplemented.
244 * @os_data: Pointer to a ulong variable, will hold os data start
245 * address.
246 * @os_len: Pointer to a ulong variable, will hold os data length.
247 *
248 * This function returns the os image's start address and length. Also,
249 * it appends the kernel command line to the bootargs env variable.
250 *
251 * Return: Zero, os start address and length on success,
252 * otherwise on failure.
253 */
Safae Ouajih636da202023-02-06 00:50:17 +0100254int android_image_get_kernel(const void *hdr,
Safae Ouajihe0581762023-02-06 00:50:11 +0100255 const void *vendor_boot_img, int verify,
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500256 ulong *os_data, ulong *os_len)
257{
Safae Ouajih607b0752023-02-06 00:50:08 +0100258 struct andr_image_data img_data = {0};
259 u32 kernel_addr;
260 const struct legacy_img_hdr *ihdr;
261
Safae Ouajihe0581762023-02-06 00:50:11 +0100262 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100263 return -EINVAL;
264
265 kernel_addr = android_image_get_kernel_addr(&img_data);
266 ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr;
Maxime Ripard87f02d52015-04-24 12:53:12 +0200267
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500268 /*
269 * Not all Android tools use the id field for signing the image with
270 * sha1 (or anything) so we don't check it. It is not obvious that the
271 * string is null terminated so we take care of this.
272 */
Safae Ouajih607b0752023-02-06 00:50:08 +0100273 strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500274 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
275 if (strlen(andr_tmp_str))
276 printf("Android's image name: %s\n", andr_tmp_str);
277
278 printf("Kernel load addr 0x%08x size %u KiB\n",
Safae Ouajih607b0752023-02-06 00:50:08 +0100279 kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024));
Ahmad Draidi86f46952014-10-23 20:50:07 +0300280
281 int len = 0;
Safae Ouajih607b0752023-02-06 00:50:08 +0100282 if (*img_data.kcmdline) {
283 printf("Kernel command line: %s\n", img_data.kcmdline);
284 len += strlen(img_data.kcmdline);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500285 }
Ahmad Draidi86f46952014-10-23 20:50:07 +0300286
Safae Ouajihb36b2272023-02-06 00:50:14 +0100287 if (img_data.kcmdline_extra) {
288 printf("Kernel extra command line: %s\n", img_data.kcmdline_extra);
289 len += strlen(img_data.kcmdline_extra);
290 }
291
Simon Glass00caae62017-08-03 12:22:12 -0600292 char *bootargs = env_get("bootargs");
Ahmad Draidi86f46952014-10-23 20:50:07 +0300293 if (bootargs)
294 len += strlen(bootargs);
295
296 char *newbootargs = malloc(len + 2);
297 if (!newbootargs) {
298 puts("Error: malloc in android_image_get_kernel failed!\n");
299 return -ENOMEM;
300 }
301 *newbootargs = '\0';
302
303 if (bootargs) {
304 strcpy(newbootargs, bootargs);
305 strcat(newbootargs, " ");
306 }
Safae Ouajih607b0752023-02-06 00:50:08 +0100307
308 if (*img_data.kcmdline)
309 strcat(newbootargs, img_data.kcmdline);
Ahmad Draidi86f46952014-10-23 20:50:07 +0300310
Safae Ouajihb36b2272023-02-06 00:50:14 +0100311 if (img_data.kcmdline_extra) {
312 strcat(newbootargs, " ");
313 strcat(newbootargs, img_data.kcmdline_extra);
314 }
315
Simon Glass382bee52017-08-03 12:22:09 -0600316 env_set("bootargs", newbootargs);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500317
318 if (os_data) {
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300319 if (image_get_magic(ihdr) == IH_MAGIC) {
320 *os_data = image_get_data(ihdr);
321 } else {
Safae Ouajih607b0752023-02-06 00:50:08 +0100322 *os_data = img_data.kernel_ptr;
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300323 }
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500324 }
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300325 if (os_len) {
326 if (image_get_magic(ihdr) == IH_MAGIC)
327 *os_len = image_get_data_size(ihdr);
328 else
Safae Ouajih607b0752023-02-06 00:50:08 +0100329 *os_len = img_data.kernel_size;
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300330 }
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500331 return 0;
332}
333
Safae Ouajih11150272023-02-06 00:50:12 +0100334bool is_android_vendor_boot_image_header(const void *vendor_boot_img)
335{
336 return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE);
337}
338
Safae Ouajih636da202023-02-06 00:50:17 +0100339bool is_android_boot_image_header(const void *hdr)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500340{
Safae Ouajih734cb472023-02-06 00:50:05 +0100341 return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500342}
343
Safae Ouajihe0581762023-02-06 00:50:11 +0100344ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
345 const void *vendor_boot_img)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500346{
Safae Ouajih607b0752023-02-06 00:50:08 +0100347 struct andr_image_data img_data;
Sam Protsenkobb633632019-08-15 20:25:07 +0300348
Safae Ouajihe0581762023-02-06 00:50:11 +0100349 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100350 return -EINVAL;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500351
Safae Ouajih607b0752023-02-06 00:50:08 +0100352 if (img_data.header_version > 2)
353 return 0;
Sam Protsenkobb633632019-08-15 20:25:07 +0300354
Safae Ouajih607b0752023-02-06 00:50:08 +0100355 return img_data.boot_img_total_size;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500356}
357
Safae Ouajih636da202023-02-06 00:50:17 +0100358ulong android_image_get_kload(const void *hdr,
Safae Ouajihe0581762023-02-06 00:50:11 +0100359 const void *vendor_boot_img)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500360{
Safae Ouajih607b0752023-02-06 00:50:08 +0100361 struct andr_image_data img_data;
362
Safae Ouajihe0581762023-02-06 00:50:11 +0100363 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100364 return -EINVAL;
365
366 return android_image_get_kernel_addr(&img_data);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500367}
368
Safae Ouajih636da202023-02-06 00:50:17 +0100369ulong android_image_get_kcomp(const void *hdr,
Safae Ouajihe0581762023-02-06 00:50:11 +0100370 const void *vendor_boot_img)
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200371{
Safae Ouajihf48efa02023-02-06 00:50:07 +0100372 struct andr_image_data img_data;
373 const void *p;
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200374
Safae Ouajihe0581762023-02-06 00:50:11 +0100375 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajihf48efa02023-02-06 00:50:07 +0100376 return -EINVAL;
377
378 p = (const void *)img_data.kernel_ptr;
Simon Glassf3543e62022-09-06 20:26:52 -0600379 if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC)
380 return image_get_comp((struct legacy_img_hdr *)p);
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300381 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200382 return IH_COMP_LZ4;
383 else
Stephan Gerholdbc599042021-07-01 20:33:16 +0200384 return image_decomp_type(p, sizeof(u32));
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200385}
386
Safae Ouajihc79a2e62023-02-06 00:50:13 +0100387int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
388 ulong *rd_data, ulong *rd_len)
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500389{
Safae Ouajih607b0752023-02-06 00:50:08 +0100390 struct andr_image_data img_data = {0};
Safae Ouajihc79a2e62023-02-06 00:50:13 +0100391 ulong ramdisk_ptr;
Safae Ouajih607b0752023-02-06 00:50:08 +0100392
Safae Ouajihe0581762023-02-06 00:50:11 +0100393 if (!android_image_get_data(hdr, vendor_boot_img, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100394 return -EINVAL;
395
Michael Walle6509c3f2024-07-29 23:36:57 +0200396 if (!img_data.ramdisk_size)
397 return -ENOENT;
398
Safae Ouajihc79a2e62023-02-06 00:50:13 +0100399 if (img_data.header_version > 2) {
Roman Stratiienkoda3447d2024-05-19 13:09:51 +0000400 ramdisk_ptr = img_data.ramdisk_addr;
Safae Ouajihc79a2e62023-02-06 00:50:13 +0100401 memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr,
402 img_data.vendor_ramdisk_size);
Roman Stratiienkoda3447d2024-05-19 13:09:51 +0000403 ramdisk_ptr += img_data.vendor_ramdisk_size;
404 memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr,
Safae Ouajih57e405e2023-02-06 00:50:18 +0100405 img_data.boot_ramdisk_size);
Roman Stratiienkoda3447d2024-05-19 13:09:51 +0000406 ramdisk_ptr += img_data.boot_ramdisk_size;
Safae Ouajih57e405e2023-02-06 00:50:18 +0100407 if (img_data.bootconfig_size) {
408 memcpy((void *)
Roman Stratiienkoda3447d2024-05-19 13:09:51 +0000409 (ramdisk_ptr), (void *)img_data.bootconfig_addr,
Safae Ouajih57e405e2023-02-06 00:50:18 +0100410 img_data.bootconfig_size);
411 }
Safae Ouajihc79a2e62023-02-06 00:50:13 +0100412 }
Ahmad Draidi86f46952014-10-23 20:50:07 +0300413
Safae Ouajih607b0752023-02-06 00:50:08 +0100414 printf("RAM disk load addr 0x%08lx size %u KiB\n",
Roman Stratiienkoda3447d2024-05-19 13:09:51 +0000415 img_data.ramdisk_addr, DIV_ROUND_UP(img_data.ramdisk_size, 1024));
Ahmad Draidi86f46952014-10-23 20:50:07 +0300416
Roman Stratiienkoda3447d2024-05-19 13:09:51 +0000417 *rd_data = img_data.ramdisk_addr;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500418
Safae Ouajih607b0752023-02-06 00:50:08 +0100419 *rd_len = img_data.ramdisk_size;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500420 return 0;
421}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200422
Safae Ouajih636da202023-02-06 00:50:17 +0100423int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len)
Bin Chen10481612018-01-27 16:59:08 +1100424{
Safae Ouajih607b0752023-02-06 00:50:08 +0100425 struct andr_image_data img_data;
426
Safae Ouajihe0581762023-02-06 00:50:11 +0100427 if (!android_image_get_data(hdr, NULL, &img_data))
Safae Ouajih607b0752023-02-06 00:50:08 +0100428 return -EINVAL;
429
Safae Ouajih636da202023-02-06 00:50:17 +0100430 if (img_data.header_version > 2) {
431 printf("Second stage bootloader is only supported for boot image version <= 2\n");
432 return -EOPNOTSUPP;
433 }
434
Safae Ouajih607b0752023-02-06 00:50:08 +0100435 if (!img_data.second_size) {
Bin Chen10481612018-01-27 16:59:08 +1100436 *second_data = *second_len = 0;
437 return -1;
438 }
439
Safae Ouajih607b0752023-02-06 00:50:08 +0100440 *second_data = img_data.second_ptr;
Bin Chen10481612018-01-27 16:59:08 +1100441
442 printf("second address is 0x%lx\n",*second_data);
443
Safae Ouajih607b0752023-02-06 00:50:08 +0100444 *second_len = img_data.second_size;
Bin Chen10481612018-01-27 16:59:08 +1100445 return 0;
446}
447
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200448/**
Sam Protsenko7f253152020-01-24 17:53:41 +0200449 * android_image_get_dtbo() - Get address and size of recovery DTBO image.
450 * @hdr_addr: Boot image header address
451 * @addr: If not NULL, will contain address of recovery DTBO image
452 * @size: If not NULL, will contain size of recovery DTBO image
453 *
454 * Get the address and size of DTBO image in "Recovery DTBO" area of Android
455 * Boot Image in RAM. The format of this image is Android DTBO (see
456 * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once
457 * the address is obtained from this function, one can use 'adtimg' U-Boot
458 * command or android_dt_*() functions to extract desired DTBO blob.
459 *
460 * This DTBO (included in boot image) is only needed for non-A/B devices, and it
461 * only can be found in recovery image. On A/B devices we can always rely on
462 * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in
463 * AOSP documentation for details.
464 *
465 * Return: true on success or false on error.
466 */
467bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size)
468{
Safae Ouajihd71a7322023-02-06 00:50:03 +0100469 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko7f253152020-01-24 17:53:41 +0200470 ulong dtbo_img_addr;
471 bool ret = true;
472
473 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih734cb472023-02-06 00:50:05 +0100474 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko7f253152020-01-24 17:53:41 +0200475 printf("Error: Boot Image header is incorrect\n");
476 ret = false;
477 goto exit;
478 }
479
Safae Ouajih447240e2023-02-06 00:50:10 +0100480 if (hdr->header_version != 1 && hdr->header_version != 2) {
481 printf("Error: header version must be >= 1 and <= 2 to get dtbo\n");
Sam Protsenko7f253152020-01-24 17:53:41 +0200482 ret = false;
483 goto exit;
484 }
485
486 if (hdr->recovery_dtbo_size == 0) {
487 printf("Error: recovery_dtbo_size is 0\n");
488 ret = false;
489 goto exit;
490 }
491
492 /* Calculate the address of DTB area in boot image */
493 dtbo_img_addr = hdr_addr;
494 dtbo_img_addr += hdr->page_size;
495 dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
496 dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
497 dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size);
498
499 if (addr)
500 *addr = dtbo_img_addr;
501 if (size)
502 *size = hdr->recovery_dtbo_size;
503
504exit:
505 unmap_sysmem(hdr);
506 return ret;
507}
508
509/**
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200510 * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image.
511 * @hdr_addr: Boot image header address
Safae Ouajihe0581762023-02-06 00:50:11 +0100512 * @vhdr_addr: Vendor Boot image header address
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200513 * @addr: Will contain the address of DTB area in boot image
514 *
515 * Return: true on success or false on fail.
516 */
Safae Ouajihe0581762023-02-06 00:50:11 +0100517static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr)
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200518{
Safae Ouajihd71a7322023-02-06 00:50:03 +0100519 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajih2d0da192023-02-06 00:50:15 +0100520 const struct andr_vnd_boot_img_hdr *v_hdr;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200521 ulong dtb_img_addr;
522 bool ret = true;
523
524 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajih734cb472023-02-06 00:50:05 +0100525 if (!is_android_boot_image_header(hdr)) {
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200526 printf("Error: Boot Image header is incorrect\n");
527 ret = false;
528 goto exit;
529 }
530
531 if (hdr->header_version < 2) {
532 printf("Error: header_version must be >= 2 to get dtb\n");
533 ret = false;
534 goto exit;
535 }
536
Safae Ouajih2d0da192023-02-06 00:50:15 +0100537 if (hdr->header_version == 2) {
538 if (!hdr->dtb_size) {
539 printf("Error: dtb_size is 0\n");
540 ret = false;
541 goto exit;
542 }
543 /* Calculate the address of DTB area in boot image */
544 dtb_img_addr = hdr_addr;
545 dtb_img_addr += hdr->page_size;
546 dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size);
547 dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size);
548 dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size);
549 dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
550
551 *addr = dtb_img_addr;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200552 }
553
Safae Ouajih2d0da192023-02-06 00:50:15 +0100554 if (hdr->header_version > 2) {
555 v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr));
556 if (!v_hdr->dtb_size) {
557 printf("Error: dtb_size is 0\n");
558 ret = false;
559 unmap_sysmem(v_hdr);
560 goto exit;
561 }
562 /* Calculate the address of DTB area in boot image */
563 dtb_img_addr = vhdr_addr;
564 dtb_img_addr += v_hdr->page_size;
565 if (v_hdr->vendor_ramdisk_size)
566 dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size);
567 *addr = dtb_img_addr;
568 unmap_sysmem(v_hdr);
569 goto exit;
570 }
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200571exit:
572 unmap_sysmem(hdr);
573 return ret;
574}
575
576/**
577 * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
578 * @hdr_addr: Boot image header address
Safae Ouajihe0581762023-02-06 00:50:11 +0100579 * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200580 * @index: Index of desired DTB in DTB area (starting from 0)
581 * @addr: If not NULL, will contain address to specified DTB
582 * @size: If not NULL, will contain size of specified DTB
583 *
584 * Get the address and size of DTB blob by its index in DTB area of Android
585 * Boot Image in RAM.
586 *
587 * Return: true on success or false on error.
588 */
Safae Ouajihe0581762023-02-06 00:50:11 +0100589bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
590 u32 index, ulong *addr, u32 *size)
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200591{
Safae Ouajih607b0752023-02-06 00:50:08 +0100592 struct andr_image_data img_data;
Safae Ouajihd71a7322023-02-06 00:50:03 +0100593 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajihe0581762023-02-06 00:50:11 +0100594 const struct andr_vnd_boot_img_hdr *vhdr;
Safae Ouajih607b0752023-02-06 00:50:08 +0100595
596 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
Safae Ouajihe0581762023-02-06 00:50:11 +0100597 if (vendor_boot_img != -1)
598 vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr));
599 if (!android_image_get_data(hdr, vhdr, &img_data)) {
600 if (vendor_boot_img != -1)
601 unmap_sysmem(vhdr);
Safae Ouajih607b0752023-02-06 00:50:08 +0100602 unmap_sysmem(hdr);
603 return false;
604 }
Safae Ouajihe0581762023-02-06 00:50:11 +0100605 if (vendor_boot_img != -1)
606 unmap_sysmem(vhdr);
Safae Ouajih607b0752023-02-06 00:50:08 +0100607 unmap_sysmem(hdr);
608
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200609 ulong dtb_img_addr; /* address of DTB part in boot image */
610 u32 dtb_img_size; /* size of DTB payload in boot image */
611 ulong dtb_addr; /* address of DTB blob with specified index */
612 u32 i; /* index iterator */
613
Safae Ouajihe0581762023-02-06 00:50:11 +0100614 android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr);
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200615 /* Check if DTB area of boot image is in DTBO format */
616 if (android_dt_check_header(dtb_img_addr)) {
617 return android_dt_get_fdt_by_index(dtb_img_addr, index, addr,
618 size);
619 }
620
621 /* Find out the address of DTB with specified index in concat blobs */
Safae Ouajih607b0752023-02-06 00:50:08 +0100622 dtb_img_size = img_data.dtb_size;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200623 i = 0;
624 dtb_addr = dtb_img_addr;
625 while (dtb_addr < dtb_img_addr + dtb_img_size) {
626 const struct fdt_header *fdt;
627 u32 dtb_size;
628
629 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
630 if (fdt_check_header(fdt) != 0) {
631 unmap_sysmem(fdt);
632 printf("Error: Invalid FDT header for index %u\n", i);
633 return false;
634 }
635
636 dtb_size = fdt_totalsize(fdt);
637 unmap_sysmem(fdt);
638
639 if (i == index) {
640 if (size)
641 *size = dtb_size;
642 if (addr)
643 *addr = dtb_addr;
644 return true;
645 }
646
647 dtb_addr += dtb_size;
648 ++i;
649 }
650
651 printf("Error: Index is out of bounds (%u/%u)\n", index, i);
652 return false;
653}
654
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200655#if !defined(CONFIG_SPL_BUILD)
656/**
657 * android_print_contents - prints out the contents of the Android format image
658 * @hdr: pointer to the Android format image header
659 *
660 * android_print_contents() formats a multi line Android image contents
661 * description.
662 * The routine prints out Android image properties
663 *
664 * returns:
665 * no returned results
666 */
Safae Ouajihd71a7322023-02-06 00:50:03 +0100667void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr)
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200668{
Safae Ouajihbb5d6922023-02-06 00:50:09 +0100669 if (hdr->header_version >= 3) {
670 printf("Content print is not supported for boot image header version > 2");
671 return;
672 }
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200673 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo210a7172017-04-02 01:49:47 -0700674 /* os_version = ver << 11 | lvl */
675 u32 os_ver = hdr->os_version >> 11;
676 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200677
Sam Protsenkobb633632019-08-15 20:25:07 +0300678 printf("%skernel size: %x\n", p, hdr->kernel_size);
679 printf("%skernel address: %x\n", p, hdr->kernel_addr);
680 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
681 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
682 printf("%ssecond size: %x\n", p, hdr->second_size);
683 printf("%ssecond address: %x\n", p, hdr->second_addr);
684 printf("%stags address: %x\n", p, hdr->tags_addr);
685 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo210a7172017-04-02 01:49:47 -0700686 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
687 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenkobb633632019-08-15 20:25:07 +0300688 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo210a7172017-04-02 01:49:47 -0700689 p, hdr->os_version,
690 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
691 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenkobb633632019-08-15 20:25:07 +0300692 printf("%sname: %s\n", p, hdr->name);
693 printf("%scmdline: %s\n", p, hdr->cmdline);
694 printf("%sheader_version: %d\n", p, hdr->header_version);
695
696 if (hdr->header_version >= 1) {
697 printf("%srecovery dtbo size: %x\n", p,
698 hdr->recovery_dtbo_size);
699 printf("%srecovery dtbo offset: %llx\n", p,
700 hdr->recovery_dtbo_offset);
701 printf("%sheader size: %x\n", p,
702 hdr->header_size);
703 }
704
Safae Ouajihbb5d6922023-02-06 00:50:09 +0100705 if (hdr->header_version == 2) {
Sam Protsenkobb633632019-08-15 20:25:07 +0300706 printf("%sdtb size: %x\n", p, hdr->dtb_size);
707 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
708 }
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200709}
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200710
711/**
712 * android_image_print_dtb_info - Print info for one DTB blob in DTB area.
713 * @fdt: DTB header
714 * @index: Number of DTB blob in DTB area.
715 *
716 * Return: true on success or false on error.
717 */
718static bool android_image_print_dtb_info(const struct fdt_header *fdt,
719 u32 index)
720{
721 int root_node_off;
722 u32 fdt_size;
723 const char *model;
724 const char *compatible;
725
726 root_node_off = fdt_path_offset(fdt, "/");
727 if (root_node_off < 0) {
728 printf("Error: Root node not found\n");
729 return false;
730 }
731
732 fdt_size = fdt_totalsize(fdt);
733 compatible = fdt_getprop(fdt, root_node_off, "compatible",
734 NULL);
735 model = fdt_getprop(fdt, root_node_off, "model", NULL);
736
737 printf(" - DTB #%u:\n", index);
738 printf(" (DTB)size = %d\n", fdt_size);
739 printf(" (DTB)model = %s\n", model ? model : "(unknown)");
740 printf(" (DTB)compatible = %s\n",
741 compatible ? compatible : "(unknown)");
742
743 return true;
744}
745
746/**
747 * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area.
748 * @hdr_addr: Boot image header address
749 *
750 * DTB payload in Android Boot Image v2+ can be in one of following formats:
751 * 1. Concatenated DTB blobs
752 * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details)
753 *
754 * This function does next:
755 * 1. Prints out the format used in DTB area
756 * 2. Iterates over all DTB blobs in DTB area and prints out the info for
757 * each blob.
758 *
759 * Return: true on success or false on error.
760 */
761bool android_image_print_dtb_contents(ulong hdr_addr)
762{
Safae Ouajihd71a7322023-02-06 00:50:03 +0100763 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200764 bool res;
765 ulong dtb_img_addr; /* address of DTB part in boot image */
766 u32 dtb_img_size; /* size of DTB payload in boot image */
767 ulong dtb_addr; /* address of DTB blob with specified index */
768 u32 i; /* index iterator */
769
Safae Ouajihe0581762023-02-06 00:50:11 +0100770 res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr);
Sam Protsenkoc3bfad82020-01-24 17:53:40 +0200771 if (!res)
772 return false;
773
774 /* Check if DTB area of boot image is in DTBO format */
775 if (android_dt_check_header(dtb_img_addr)) {
776 printf("## DTB area contents (DTBO format):\n");
777 android_dt_print_contents(dtb_img_addr);
778 return true;
779 }
780
781 printf("## DTB area contents (concat format):\n");
782
783 /* Iterate over concatenated DTB blobs */
784 hdr = map_sysmem(hdr_addr, sizeof(*hdr));
785 dtb_img_size = hdr->dtb_size;
786 unmap_sysmem(hdr);
787 i = 0;
788 dtb_addr = dtb_img_addr;
789 while (dtb_addr < dtb_img_addr + dtb_img_size) {
790 const struct fdt_header *fdt;
791 u32 dtb_size;
792
793 fdt = map_sysmem(dtb_addr, sizeof(*fdt));
794 if (fdt_check_header(fdt) != 0) {
795 unmap_sysmem(fdt);
796 printf("Error: Invalid FDT header for index %u\n", i);
797 return false;
798 }
799
800 res = android_image_print_dtb_info(fdt, i);
801 if (!res) {
802 unmap_sysmem(fdt);
803 return false;
804 }
805
806 dtb_size = fdt_totalsize(fdt);
807 unmap_sysmem(fdt);
808 dtb_addr += dtb_size;
809 ++i;
810 }
811
812 return true;
813}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200814#endif