]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
9ace3fc8 SS |
2 | /* |
3 | * Copyright (c) 2011 Sebastian Andrzej Siewior <[email protected]> | |
9ace3fc8 SS |
4 | */ |
5 | ||
9fb625ce | 6 | #include <env.h> |
9ace3fc8 | 7 | #include <image.h> |
c3bfad82 | 8 | #include <image-android-dt.h> |
9ace3fc8 | 9 | #include <android_image.h> |
86f4695b AD |
10 | #include <malloc.h> |
11 | #include <errno.h> | |
829ceb28 | 12 | #include <asm/unaligned.h> |
c3bfad82 | 13 | #include <mapmem.h> |
401d1c4f | 14 | #include <linux/libfdt.h> |
9ace3fc8 | 15 | |
87f02d5a MR |
16 | #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000 |
17 | ||
9ace3fc8 SS |
18 | static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; |
19 | ||
57e405e1 SO |
20 | static 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 | ||
29 | static 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 | ||
35 | static 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 | ||
58fed99f MK |
59 | __weak ulong get_avendor_bootimg_addr(void) |
60 | { | |
61 | return -1; | |
62 | } | |
63 | ||
1115027d SO |
64 | static 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); | |
da3447d0 | 81 | data->ramdisk_ptr = end; |
1115027d SO |
82 | 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 | ||
92 | static 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 | */ | |
b36b227b | 101 | data->kcmdline_extra = hdr->cmdline; |
1115027d SO |
102 | 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; | |
57e405e1 | 107 | data->bootconfig_size = hdr->bootconfig_size; |
1115027d SO |
108 | 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); | |
57e405e1 SO |
122 | 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); | |
1115027d SO |
129 | data->vendor_boot_img_total_size = end - (ulong)hdr; |
130 | } | |
131 | ||
f48efa0e SO |
132 | static 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 | ||
e058176b SO |
180 | bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr, |
181 | struct andr_image_data *data) | |
f48efa0e SO |
182 | { |
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 | ||
1115027d SO |
193 | 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 { | |
f48efa0e | 205 | android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data); |
1115027d | 206 | } |
f48efa0e SO |
207 | |
208 | return true; | |
209 | } | |
210 | ||
607b0755 | 211 | static ulong android_image_get_kernel_addr(struct andr_image_data *img_data) |
87f02d5a MR |
212 | { |
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 | */ | |
607b0755 SO |
224 | if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) |
225 | return img_data->kernel_ptr; | |
87f02d5a | 226 | |
95712afc CG |
227 | /* |
228 | * abootimg creates images where all load addresses are 0 | |
229 | * and we need to fix them. | |
230 | */ | |
607b0755 | 231 | if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0) |
95712afc CG |
232 | return env_get_ulong("kernel_addr_r", 16, 0); |
233 | ||
607b0755 | 234 | return img_data->kernel_addr; |
87f02d5a MR |
235 | } |
236 | ||
86f4695b AD |
237 | /** |
238 | * android_image_get_kernel() - processes kernel part of Android boot images | |
e058176b | 239 | * @hdr: Pointer to boot image header, which is at the start |
86f4695b | 240 | * of the image. |
e058176b SO |
241 | * @vendor_boot_img: Pointer to vendor boot image header, which is at the |
242 | * start of the image. | |
86f4695b AD |
243 | * @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 | */ | |
636da203 | 254 | int android_image_get_kernel(const void *hdr, |
e058176b | 255 | const void *vendor_boot_img, int verify, |
9ace3fc8 SS |
256 | ulong *os_data, ulong *os_len) |
257 | { | |
607b0755 SO |
258 | struct andr_image_data img_data = {0}; |
259 | u32 kernel_addr; | |
260 | const struct legacy_img_hdr *ihdr; | |
261 | ||
e058176b | 262 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
607b0755 SO |
263 | return -EINVAL; |
264 | ||
265 | kernel_addr = android_image_get_kernel_addr(&img_data); | |
266 | ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr; | |
87f02d5a | 267 | |
9ace3fc8 SS |
268 | /* |
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 | */ | |
607b0755 | 273 | strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE); |
9ace3fc8 SS |
274 | 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", | |
607b0755 | 279 | kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024)); |
86f4695b AD |
280 | |
281 | int len = 0; | |
607b0755 SO |
282 | if (*img_data.kcmdline) { |
283 | printf("Kernel command line: %s\n", img_data.kcmdline); | |
284 | len += strlen(img_data.kcmdline); | |
86f4695b AD |
285 | } |
286 | ||
b36b227b SO |
287 | 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 | ||
00caae6d | 292 | char *bootargs = env_get("bootargs"); |
86f4695b AD |
293 | 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, " "); | |
9ace3fc8 | 306 | } |
607b0755 SO |
307 | |
308 | if (*img_data.kcmdline) | |
309 | strcat(newbootargs, img_data.kcmdline); | |
86f4695b | 310 | |
b36b227b SO |
311 | if (img_data.kcmdline_extra) { |
312 | strcat(newbootargs, " "); | |
313 | strcat(newbootargs, img_data.kcmdline_extra); | |
314 | } | |
315 | ||
382bee57 | 316 | env_set("bootargs", newbootargs); |
9ace3fc8 SS |
317 | |
318 | if (os_data) { | |
39f790b0 RS |
319 | if (image_get_magic(ihdr) == IH_MAGIC) { |
320 | *os_data = image_get_data(ihdr); | |
321 | } else { | |
607b0755 | 322 | *os_data = img_data.kernel_ptr; |
39f790b0 RS |
323 | } |
324 | } | |
325 | if (os_len) { | |
326 | if (image_get_magic(ihdr) == IH_MAGIC) | |
327 | *os_len = image_get_data_size(ihdr); | |
328 | else | |
607b0755 | 329 | *os_len = img_data.kernel_size; |
9ace3fc8 | 330 | } |
9ace3fc8 SS |
331 | return 0; |
332 | } | |
333 | ||
1115027d SO |
334 | bool 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 | ||
636da203 | 339 | bool is_android_boot_image_header(const void *hdr) |
9ace3fc8 | 340 | { |
734cb47d | 341 | return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE); |
9ace3fc8 SS |
342 | } |
343 | ||
e058176b SO |
344 | ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr, |
345 | const void *vendor_boot_img) | |
9ace3fc8 | 346 | { |
607b0755 | 347 | struct andr_image_data img_data; |
9ace3fc8 | 348 | |
e058176b | 349 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
607b0755 | 350 | return -EINVAL; |
bb63363f | 351 | |
607b0755 SO |
352 | if (img_data.header_version > 2) |
353 | return 0; | |
bb63363f | 354 | |
607b0755 | 355 | return img_data.boot_img_total_size; |
9ace3fc8 SS |
356 | } |
357 | ||
636da203 | 358 | ulong android_image_get_kload(const void *hdr, |
e058176b | 359 | const void *vendor_boot_img) |
9ace3fc8 | 360 | { |
607b0755 SO |
361 | struct andr_image_data img_data; |
362 | ||
e058176b | 363 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
607b0755 SO |
364 | return -EINVAL; |
365 | ||
366 | return android_image_get_kernel_addr(&img_data); | |
9ace3fc8 SS |
367 | } |
368 | ||
636da203 | 369 | ulong android_image_get_kcomp(const void *hdr, |
e058176b | 370 | const void *vendor_boot_img) |
829ceb28 | 371 | { |
f48efa0e SO |
372 | struct andr_image_data img_data; |
373 | const void *p; | |
374 | ||
e058176b | 375 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
f48efa0e | 376 | return -EINVAL; |
829ceb28 | 377 | |
f48efa0e | 378 | p = (const void *)img_data.kernel_ptr; |
f3543e69 SG |
379 | if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC) |
380 | return image_get_comp((struct legacy_img_hdr *)p); | |
39f790b0 | 381 | else if (get_unaligned_le32(p) == LZ4F_MAGIC) |
829ceb28 ER |
382 | return IH_COMP_LZ4; |
383 | else | |
bc599042 | 384 | return image_decomp_type(p, sizeof(u32)); |
829ceb28 ER |
385 | } |
386 | ||
c79a2e68 SO |
387 | int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img, |
388 | ulong *rd_data, ulong *rd_len) | |
9ace3fc8 | 389 | { |
607b0755 | 390 | struct andr_image_data img_data = {0}; |
c79a2e68 | 391 | ulong ramdisk_ptr; |
607b0755 | 392 | |
e058176b | 393 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
607b0755 SO |
394 | return -EINVAL; |
395 | ||
396 | if (!img_data.ramdisk_size) { | |
9950098e | 397 | *rd_data = *rd_len = 0; |
9ace3fc8 | 398 | return -1; |
9950098e | 399 | } |
c79a2e68 | 400 | if (img_data.header_version > 2) { |
da3447d0 | 401 | ramdisk_ptr = img_data.ramdisk_addr; |
c79a2e68 SO |
402 | memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr, |
403 | img_data.vendor_ramdisk_size); | |
da3447d0 RS |
404 | ramdisk_ptr += img_data.vendor_ramdisk_size; |
405 | memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr, | |
57e405e1 | 406 | img_data.boot_ramdisk_size); |
da3447d0 | 407 | ramdisk_ptr += img_data.boot_ramdisk_size; |
57e405e1 SO |
408 | if (img_data.bootconfig_size) { |
409 | memcpy((void *) | |
da3447d0 | 410 | (ramdisk_ptr), (void *)img_data.bootconfig_addr, |
57e405e1 SO |
411 | img_data.bootconfig_size); |
412 | } | |
c79a2e68 | 413 | } |
86f4695b | 414 | |
607b0755 | 415 | printf("RAM disk load addr 0x%08lx size %u KiB\n", |
da3447d0 | 416 | img_data.ramdisk_addr, DIV_ROUND_UP(img_data.ramdisk_size, 1024)); |
86f4695b | 417 | |
da3447d0 | 418 | *rd_data = img_data.ramdisk_addr; |
9ace3fc8 | 419 | |
607b0755 | 420 | *rd_len = img_data.ramdisk_size; |
9ace3fc8 SS |
421 | return 0; |
422 | } | |
4f1318b2 | 423 | |
636da203 | 424 | int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len) |
10481614 | 425 | { |
607b0755 SO |
426 | struct andr_image_data img_data; |
427 | ||
e058176b | 428 | if (!android_image_get_data(hdr, NULL, &img_data)) |
607b0755 SO |
429 | return -EINVAL; |
430 | ||
636da203 SO |
431 | if (img_data.header_version > 2) { |
432 | printf("Second stage bootloader is only supported for boot image version <= 2\n"); | |
433 | return -EOPNOTSUPP; | |
434 | } | |
435 | ||
607b0755 | 436 | if (!img_data.second_size) { |
10481614 BC |
437 | *second_data = *second_len = 0; |
438 | return -1; | |
439 | } | |
440 | ||
607b0755 | 441 | *second_data = img_data.second_ptr; |
10481614 BC |
442 | |
443 | printf("second address is 0x%lx\n",*second_data); | |
444 | ||
607b0755 | 445 | *second_len = img_data.second_size; |
10481614 BC |
446 | return 0; |
447 | } | |
448 | ||
7f253150 SP |
449 | /** |
450 | * android_image_get_dtbo() - Get address and size of recovery DTBO image. | |
451 | * @hdr_addr: Boot image header address | |
452 | * @addr: If not NULL, will contain address of recovery DTBO image | |
453 | * @size: If not NULL, will contain size of recovery DTBO image | |
454 | * | |
455 | * Get the address and size of DTBO image in "Recovery DTBO" area of Android | |
456 | * Boot Image in RAM. The format of this image is Android DTBO (see | |
457 | * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once | |
458 | * the address is obtained from this function, one can use 'adtimg' U-Boot | |
459 | * command or android_dt_*() functions to extract desired DTBO blob. | |
460 | * | |
461 | * This DTBO (included in boot image) is only needed for non-A/B devices, and it | |
462 | * only can be found in recovery image. On A/B devices we can always rely on | |
463 | * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in | |
464 | * AOSP documentation for details. | |
465 | * | |
466 | * Return: true on success or false on error. | |
467 | */ | |
468 | bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size) | |
469 | { | |
d71a732a | 470 | const struct andr_boot_img_hdr_v0 *hdr; |
7f253150 SP |
471 | ulong dtbo_img_addr; |
472 | bool ret = true; | |
473 | ||
474 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
734cb47d | 475 | if (!is_android_boot_image_header(hdr)) { |
7f253150 SP |
476 | printf("Error: Boot Image header is incorrect\n"); |
477 | ret = false; | |
478 | goto exit; | |
479 | } | |
480 | ||
447240e2 SO |
481 | if (hdr->header_version != 1 && hdr->header_version != 2) { |
482 | printf("Error: header version must be >= 1 and <= 2 to get dtbo\n"); | |
7f253150 SP |
483 | ret = false; |
484 | goto exit; | |
485 | } | |
486 | ||
487 | if (hdr->recovery_dtbo_size == 0) { | |
488 | printf("Error: recovery_dtbo_size is 0\n"); | |
489 | ret = false; | |
490 | goto exit; | |
491 | } | |
492 | ||
493 | /* Calculate the address of DTB area in boot image */ | |
494 | dtbo_img_addr = hdr_addr; | |
495 | dtbo_img_addr += hdr->page_size; | |
496 | dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size); | |
497 | dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size); | |
498 | dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size); | |
499 | ||
500 | if (addr) | |
501 | *addr = dtbo_img_addr; | |
502 | if (size) | |
503 | *size = hdr->recovery_dtbo_size; | |
504 | ||
505 | exit: | |
506 | unmap_sysmem(hdr); | |
507 | return ret; | |
508 | } | |
509 | ||
c3bfad82 SP |
510 | /** |
511 | * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image. | |
512 | * @hdr_addr: Boot image header address | |
e058176b | 513 | * @vhdr_addr: Vendor Boot image header address |
c3bfad82 SP |
514 | * @addr: Will contain the address of DTB area in boot image |
515 | * | |
516 | * Return: true on success or false on fail. | |
517 | */ | |
e058176b | 518 | static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr) |
c3bfad82 | 519 | { |
d71a732a | 520 | const struct andr_boot_img_hdr_v0 *hdr; |
2d0da197 | 521 | const struct andr_vnd_boot_img_hdr *v_hdr; |
c3bfad82 SP |
522 | ulong dtb_img_addr; |
523 | bool ret = true; | |
524 | ||
525 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
734cb47d | 526 | if (!is_android_boot_image_header(hdr)) { |
c3bfad82 SP |
527 | printf("Error: Boot Image header is incorrect\n"); |
528 | ret = false; | |
529 | goto exit; | |
530 | } | |
531 | ||
532 | if (hdr->header_version < 2) { | |
533 | printf("Error: header_version must be >= 2 to get dtb\n"); | |
534 | ret = false; | |
535 | goto exit; | |
536 | } | |
537 | ||
2d0da197 SO |
538 | if (hdr->header_version == 2) { |
539 | if (!hdr->dtb_size) { | |
540 | printf("Error: dtb_size is 0\n"); | |
541 | ret = false; | |
542 | goto exit; | |
543 | } | |
544 | /* Calculate the address of DTB area in boot image */ | |
545 | dtb_img_addr = hdr_addr; | |
546 | dtb_img_addr += hdr->page_size; | |
547 | dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size); | |
548 | dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size); | |
549 | dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size); | |
550 | dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); | |
551 | ||
552 | *addr = dtb_img_addr; | |
c3bfad82 SP |
553 | } |
554 | ||
2d0da197 SO |
555 | if (hdr->header_version > 2) { |
556 | v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr)); | |
557 | if (!v_hdr->dtb_size) { | |
558 | printf("Error: dtb_size is 0\n"); | |
559 | ret = false; | |
560 | unmap_sysmem(v_hdr); | |
561 | goto exit; | |
562 | } | |
563 | /* Calculate the address of DTB area in boot image */ | |
564 | dtb_img_addr = vhdr_addr; | |
565 | dtb_img_addr += v_hdr->page_size; | |
566 | if (v_hdr->vendor_ramdisk_size) | |
567 | dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size); | |
568 | *addr = dtb_img_addr; | |
569 | unmap_sysmem(v_hdr); | |
570 | goto exit; | |
571 | } | |
c3bfad82 SP |
572 | exit: |
573 | unmap_sysmem(hdr); | |
574 | return ret; | |
575 | } | |
576 | ||
577 | /** | |
578 | * android_image_get_dtb_by_index() - Get address and size of blob in DTB area. | |
579 | * @hdr_addr: Boot image header address | |
e058176b | 580 | * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image. |
c3bfad82 SP |
581 | * @index: Index of desired DTB in DTB area (starting from 0) |
582 | * @addr: If not NULL, will contain address to specified DTB | |
583 | * @size: If not NULL, will contain size of specified DTB | |
584 | * | |
585 | * Get the address and size of DTB blob by its index in DTB area of Android | |
586 | * Boot Image in RAM. | |
587 | * | |
588 | * Return: true on success or false on error. | |
589 | */ | |
e058176b SO |
590 | bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img, |
591 | u32 index, ulong *addr, u32 *size) | |
c3bfad82 | 592 | { |
607b0755 | 593 | struct andr_image_data img_data; |
d71a732a | 594 | const struct andr_boot_img_hdr_v0 *hdr; |
e058176b | 595 | const struct andr_vnd_boot_img_hdr *vhdr; |
607b0755 SO |
596 | |
597 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
e058176b SO |
598 | if (vendor_boot_img != -1) |
599 | vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr)); | |
600 | if (!android_image_get_data(hdr, vhdr, &img_data)) { | |
601 | if (vendor_boot_img != -1) | |
602 | unmap_sysmem(vhdr); | |
607b0755 SO |
603 | unmap_sysmem(hdr); |
604 | return false; | |
605 | } | |
e058176b SO |
606 | if (vendor_boot_img != -1) |
607 | unmap_sysmem(vhdr); | |
607b0755 SO |
608 | unmap_sysmem(hdr); |
609 | ||
c3bfad82 SP |
610 | ulong dtb_img_addr; /* address of DTB part in boot image */ |
611 | u32 dtb_img_size; /* size of DTB payload in boot image */ | |
612 | ulong dtb_addr; /* address of DTB blob with specified index */ | |
613 | u32 i; /* index iterator */ | |
614 | ||
e058176b | 615 | android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr); |
c3bfad82 SP |
616 | /* Check if DTB area of boot image is in DTBO format */ |
617 | if (android_dt_check_header(dtb_img_addr)) { | |
618 | return android_dt_get_fdt_by_index(dtb_img_addr, index, addr, | |
619 | size); | |
620 | } | |
621 | ||
622 | /* Find out the address of DTB with specified index in concat blobs */ | |
607b0755 | 623 | dtb_img_size = img_data.dtb_size; |
c3bfad82 SP |
624 | i = 0; |
625 | dtb_addr = dtb_img_addr; | |
626 | while (dtb_addr < dtb_img_addr + dtb_img_size) { | |
627 | const struct fdt_header *fdt; | |
628 | u32 dtb_size; | |
629 | ||
630 | fdt = map_sysmem(dtb_addr, sizeof(*fdt)); | |
631 | if (fdt_check_header(fdt) != 0) { | |
632 | unmap_sysmem(fdt); | |
633 | printf("Error: Invalid FDT header for index %u\n", i); | |
634 | return false; | |
635 | } | |
636 | ||
637 | dtb_size = fdt_totalsize(fdt); | |
638 | unmap_sysmem(fdt); | |
639 | ||
640 | if (i == index) { | |
641 | if (size) | |
642 | *size = dtb_size; | |
643 | if (addr) | |
644 | *addr = dtb_addr; | |
645 | return true; | |
646 | } | |
647 | ||
648 | dtb_addr += dtb_size; | |
649 | ++i; | |
650 | } | |
651 | ||
652 | printf("Error: Index is out of bounds (%u/%u)\n", index, i); | |
653 | return false; | |
654 | } | |
655 | ||
4f1318b2 MT |
656 | #if !defined(CONFIG_SPL_BUILD) |
657 | /** | |
658 | * android_print_contents - prints out the contents of the Android format image | |
659 | * @hdr: pointer to the Android format image header | |
660 | * | |
661 | * android_print_contents() formats a multi line Android image contents | |
662 | * description. | |
663 | * The routine prints out Android image properties | |
664 | * | |
665 | * returns: | |
666 | * no returned results | |
667 | */ | |
d71a732a | 668 | void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr) |
4f1318b2 | 669 | { |
bb5d6927 SO |
670 | if (hdr->header_version >= 3) { |
671 | printf("Content print is not supported for boot image header version > 2"); | |
672 | return; | |
673 | } | |
4f1318b2 | 674 | const char * const p = IMAGE_INDENT_STRING; |
210a7176 AD |
675 | /* os_version = ver << 11 | lvl */ |
676 | u32 os_ver = hdr->os_version >> 11; | |
677 | u32 os_lvl = hdr->os_version & ((1U << 11) - 1); | |
4f1318b2 | 678 | |
bb63363f SP |
679 | printf("%skernel size: %x\n", p, hdr->kernel_size); |
680 | printf("%skernel address: %x\n", p, hdr->kernel_addr); | |
681 | printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); | |
682 | printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr); | |
683 | printf("%ssecond size: %x\n", p, hdr->second_size); | |
684 | printf("%ssecond address: %x\n", p, hdr->second_addr); | |
685 | printf("%stags address: %x\n", p, hdr->tags_addr); | |
686 | printf("%spage size: %x\n", p, hdr->page_size); | |
210a7176 AD |
687 | /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) |
688 | * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ | |
bb63363f | 689 | printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", |
210a7176 AD |
690 | p, hdr->os_version, |
691 | (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, | |
692 | (os_lvl >> 4) + 2000, os_lvl & 0x0F); | |
bb63363f SP |
693 | printf("%sname: %s\n", p, hdr->name); |
694 | printf("%scmdline: %s\n", p, hdr->cmdline); | |
695 | printf("%sheader_version: %d\n", p, hdr->header_version); | |
696 | ||
697 | if (hdr->header_version >= 1) { | |
698 | printf("%srecovery dtbo size: %x\n", p, | |
699 | hdr->recovery_dtbo_size); | |
700 | printf("%srecovery dtbo offset: %llx\n", p, | |
701 | hdr->recovery_dtbo_offset); | |
702 | printf("%sheader size: %x\n", p, | |
703 | hdr->header_size); | |
704 | } | |
705 | ||
bb5d6927 | 706 | if (hdr->header_version == 2) { |
bb63363f SP |
707 | printf("%sdtb size: %x\n", p, hdr->dtb_size); |
708 | printf("%sdtb addr: %llx\n", p, hdr->dtb_addr); | |
709 | } | |
4f1318b2 | 710 | } |
c3bfad82 SP |
711 | |
712 | /** | |
713 | * android_image_print_dtb_info - Print info for one DTB blob in DTB area. | |
714 | * @fdt: DTB header | |
715 | * @index: Number of DTB blob in DTB area. | |
716 | * | |
717 | * Return: true on success or false on error. | |
718 | */ | |
719 | static bool android_image_print_dtb_info(const struct fdt_header *fdt, | |
720 | u32 index) | |
721 | { | |
722 | int root_node_off; | |
723 | u32 fdt_size; | |
724 | const char *model; | |
725 | const char *compatible; | |
726 | ||
727 | root_node_off = fdt_path_offset(fdt, "/"); | |
728 | if (root_node_off < 0) { | |
729 | printf("Error: Root node not found\n"); | |
730 | return false; | |
731 | } | |
732 | ||
733 | fdt_size = fdt_totalsize(fdt); | |
734 | compatible = fdt_getprop(fdt, root_node_off, "compatible", | |
735 | NULL); | |
736 | model = fdt_getprop(fdt, root_node_off, "model", NULL); | |
737 | ||
738 | printf(" - DTB #%u:\n", index); | |
739 | printf(" (DTB)size = %d\n", fdt_size); | |
740 | printf(" (DTB)model = %s\n", model ? model : "(unknown)"); | |
741 | printf(" (DTB)compatible = %s\n", | |
742 | compatible ? compatible : "(unknown)"); | |
743 | ||
744 | return true; | |
745 | } | |
746 | ||
747 | /** | |
748 | * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area. | |
749 | * @hdr_addr: Boot image header address | |
750 | * | |
751 | * DTB payload in Android Boot Image v2+ can be in one of following formats: | |
752 | * 1. Concatenated DTB blobs | |
753 | * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details) | |
754 | * | |
755 | * This function does next: | |
756 | * 1. Prints out the format used in DTB area | |
757 | * 2. Iterates over all DTB blobs in DTB area and prints out the info for | |
758 | * each blob. | |
759 | * | |
760 | * Return: true on success or false on error. | |
761 | */ | |
762 | bool android_image_print_dtb_contents(ulong hdr_addr) | |
763 | { | |
d71a732a | 764 | const struct andr_boot_img_hdr_v0 *hdr; |
c3bfad82 SP |
765 | bool res; |
766 | ulong dtb_img_addr; /* address of DTB part in boot image */ | |
767 | u32 dtb_img_size; /* size of DTB payload in boot image */ | |
768 | ulong dtb_addr; /* address of DTB blob with specified index */ | |
769 | u32 i; /* index iterator */ | |
770 | ||
e058176b | 771 | res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr); |
c3bfad82 SP |
772 | if (!res) |
773 | return false; | |
774 | ||
775 | /* Check if DTB area of boot image is in DTBO format */ | |
776 | if (android_dt_check_header(dtb_img_addr)) { | |
777 | printf("## DTB area contents (DTBO format):\n"); | |
778 | android_dt_print_contents(dtb_img_addr); | |
779 | return true; | |
780 | } | |
781 | ||
782 | printf("## DTB area contents (concat format):\n"); | |
783 | ||
784 | /* Iterate over concatenated DTB blobs */ | |
785 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
786 | dtb_img_size = hdr->dtb_size; | |
787 | unmap_sysmem(hdr); | |
788 | i = 0; | |
789 | dtb_addr = dtb_img_addr; | |
790 | while (dtb_addr < dtb_img_addr + dtb_img_size) { | |
791 | const struct fdt_header *fdt; | |
792 | u32 dtb_size; | |
793 | ||
794 | fdt = map_sysmem(dtb_addr, sizeof(*fdt)); | |
795 | if (fdt_check_header(fdt) != 0) { | |
796 | unmap_sysmem(fdt); | |
797 | printf("Error: Invalid FDT header for index %u\n", i); | |
798 | return false; | |
799 | } | |
800 | ||
801 | res = android_image_print_dtb_info(fdt, i); | |
802 | if (!res) { | |
803 | unmap_sysmem(fdt); | |
804 | return false; | |
805 | } | |
806 | ||
807 | dtb_size = fdt_totalsize(fdt); | |
808 | unmap_sysmem(fdt); | |
809 | dtb_addr += dtb_size; | |
810 | ++i; | |
811 | } | |
812 | ||
813 | return true; | |
814 | } | |
4f1318b2 | 815 | #endif |