]>
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 | 16 | #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000 |
21e7fa0e | 17 | #define ANDROID_IMAGE_DEFAULT_RAMDISK_ADDR 0x11000000 |
87f02d5a | 18 | |
9ace3fc8 SS |
19 | static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; |
20 | ||
57e405e1 SO |
21 | static 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 | ||
30 | static 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 | ||
36 | static 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 | ||
58fed99f MK |
60 | __weak ulong get_avendor_bootimg_addr(void) |
61 | { | |
62 | return -1; | |
63 | } | |
64 | ||
1115027d SO |
65 | static void android_boot_image_v3_v4_parse_hdr(const struct andr_boot_img_hdr_v3 *hdr, |
66 | struct andr_image_data *data) | |
67 | { | |
68 | ulong end; | |
69 | ||
70 | data->kcmdline = hdr->cmdline; | |
71 | data->header_version = hdr->header_version; | |
72 | ||
73 | /* | |
74 | * The header takes a full page, the remaining components are aligned | |
75 | * on page boundary. | |
76 | */ | |
77 | end = (ulong)hdr; | |
78 | end += ANDR_GKI_PAGE_SIZE; | |
79 | data->kernel_ptr = end; | |
80 | data->kernel_size = hdr->kernel_size; | |
81 | end += ALIGN(hdr->kernel_size, ANDR_GKI_PAGE_SIZE); | |
da3447d0 | 82 | data->ramdisk_ptr = end; |
1115027d SO |
83 | data->ramdisk_size = hdr->ramdisk_size; |
84 | data->boot_ramdisk_size = hdr->ramdisk_size; | |
85 | end += ALIGN(hdr->ramdisk_size, ANDR_GKI_PAGE_SIZE); | |
86 | ||
87 | if (hdr->header_version > 3) | |
88 | end += ALIGN(hdr->signature_size, ANDR_GKI_PAGE_SIZE); | |
89 | ||
90 | data->boot_img_total_size = end - (ulong)hdr; | |
91 | } | |
92 | ||
93 | static void android_vendor_boot_image_v3_v4_parse_hdr(const struct andr_vnd_boot_img_hdr | |
94 | *hdr, struct andr_image_data *data) | |
95 | { | |
96 | ulong end; | |
97 | ||
98 | /* | |
99 | * The header takes a full page, the remaining components are aligned | |
100 | * on page boundary. | |
101 | */ | |
b36b227b | 102 | data->kcmdline_extra = hdr->cmdline; |
1115027d SO |
103 | data->tags_addr = hdr->tags_addr; |
104 | data->image_name = hdr->name; | |
105 | data->kernel_addr = hdr->kernel_addr; | |
106 | data->ramdisk_addr = hdr->ramdisk_addr; | |
107 | data->dtb_load_addr = hdr->dtb_addr; | |
57e405e1 | 108 | data->bootconfig_size = hdr->bootconfig_size; |
1115027d SO |
109 | end = (ulong)hdr; |
110 | end += hdr->page_size; | |
111 | if (hdr->vendor_ramdisk_size) { | |
112 | data->vendor_ramdisk_ptr = end; | |
113 | data->vendor_ramdisk_size = hdr->vendor_ramdisk_size; | |
114 | data->ramdisk_size += hdr->vendor_ramdisk_size; | |
115 | end += ALIGN(hdr->vendor_ramdisk_size, hdr->page_size); | |
116 | } | |
117 | ||
118 | data->dtb_ptr = end; | |
119 | data->dtb_size = hdr->dtb_size; | |
120 | ||
121 | end += ALIGN(hdr->dtb_size, hdr->page_size); | |
122 | end += ALIGN(hdr->vendor_ramdisk_table_size, hdr->page_size); | |
57e405e1 SO |
123 | data->bootconfig_addr = end; |
124 | if (hdr->bootconfig_size) { | |
125 | data->bootconfig_size += add_trailer(data->bootconfig_addr, | |
126 | data->bootconfig_size); | |
127 | data->ramdisk_size += data->bootconfig_size; | |
128 | } | |
129 | end += ALIGN(data->bootconfig_size, hdr->page_size); | |
1115027d SO |
130 | data->vendor_boot_img_total_size = end - (ulong)hdr; |
131 | } | |
132 | ||
f48efa0e SO |
133 | static void android_boot_image_v0_v1_v2_parse_hdr(const struct andr_boot_img_hdr_v0 *hdr, |
134 | struct andr_image_data *data) | |
135 | { | |
136 | ulong end; | |
137 | ||
138 | data->image_name = hdr->name; | |
139 | data->kcmdline = hdr->cmdline; | |
140 | data->kernel_addr = hdr->kernel_addr; | |
141 | data->ramdisk_addr = hdr->ramdisk_addr; | |
142 | data->header_version = hdr->header_version; | |
143 | data->dtb_load_addr = hdr->dtb_addr; | |
144 | ||
145 | end = (ulong)hdr; | |
146 | ||
147 | /* | |
148 | * The header takes a full page, the remaining components are aligned | |
149 | * on page boundary | |
150 | */ | |
151 | ||
152 | end += hdr->page_size; | |
153 | ||
154 | data->kernel_ptr = end; | |
155 | data->kernel_size = hdr->kernel_size; | |
156 | end += ALIGN(hdr->kernel_size, hdr->page_size); | |
157 | ||
158 | data->ramdisk_ptr = end; | |
159 | data->ramdisk_size = hdr->ramdisk_size; | |
160 | end += ALIGN(hdr->ramdisk_size, hdr->page_size); | |
161 | ||
162 | data->second_ptr = end; | |
163 | data->second_size = hdr->second_size; | |
164 | end += ALIGN(hdr->second_size, hdr->page_size); | |
165 | ||
166 | if (hdr->header_version >= 1) { | |
167 | data->recovery_dtbo_ptr = end; | |
168 | data->recovery_dtbo_size = hdr->recovery_dtbo_size; | |
169 | end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); | |
170 | } | |
171 | ||
172 | if (hdr->header_version >= 2) { | |
173 | data->dtb_ptr = end; | |
174 | data->dtb_size = hdr->dtb_size; | |
175 | end += ALIGN(hdr->dtb_size, hdr->page_size); | |
176 | } | |
177 | ||
178 | data->boot_img_total_size = end - (ulong)hdr; | |
179 | } | |
180 | ||
e058176b SO |
181 | bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr, |
182 | struct andr_image_data *data) | |
f48efa0e SO |
183 | { |
184 | if (!boot_hdr || !data) { | |
185 | printf("boot_hdr or data params can't be NULL\n"); | |
186 | return false; | |
187 | } | |
188 | ||
189 | if (!is_android_boot_image_header(boot_hdr)) { | |
190 | printf("Incorrect boot image header\n"); | |
191 | return false; | |
192 | } | |
193 | ||
1115027d SO |
194 | if (((struct andr_boot_img_hdr_v0 *)boot_hdr)->header_version > 2) { |
195 | if (!vendor_boot_hdr) { | |
196 | printf("For boot header v3+ vendor boot image has to be provided\n"); | |
197 | return false; | |
198 | } | |
199 | if (!is_android_vendor_boot_image_header(vendor_boot_hdr)) { | |
200 | printf("Incorrect vendor boot image header\n"); | |
201 | return false; | |
202 | } | |
203 | android_boot_image_v3_v4_parse_hdr(boot_hdr, data); | |
204 | android_vendor_boot_image_v3_v4_parse_hdr(vendor_boot_hdr, data); | |
205 | } else { | |
f48efa0e | 206 | android_boot_image_v0_v1_v2_parse_hdr(boot_hdr, data); |
1115027d | 207 | } |
f48efa0e SO |
208 | |
209 | return true; | |
210 | } | |
211 | ||
d5a85e8e NA |
212 | static ulong android_image_get_kernel_addr(struct andr_image_data *img_data, |
213 | ulong comp) | |
87f02d5a MR |
214 | { |
215 | /* | |
216 | * All the Android tools that generate a boot.img use this | |
217 | * address as the default. | |
218 | * | |
219 | * Even though it doesn't really make a lot of sense, and it | |
220 | * might be valid on some platforms, we treat that adress as | |
221 | * the default value for this field, and try to execute the | |
222 | * kernel in place in such a case. | |
223 | * | |
224 | * Otherwise, we will return the actual value set by the user. | |
225 | */ | |
d5a85e8e NA |
226 | if (img_data->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) { |
227 | if (comp == IH_COMP_NONE) | |
228 | return img_data->kernel_ptr; | |
229 | return env_get_ulong("kernel_addr_r", 16, 0); | |
230 | } | |
87f02d5a | 231 | |
95712afc CG |
232 | /* |
233 | * abootimg creates images where all load addresses are 0 | |
234 | * and we need to fix them. | |
235 | */ | |
607b0755 | 236 | if (img_data->kernel_addr == 0 && img_data->ramdisk_addr == 0) |
95712afc CG |
237 | return env_get_ulong("kernel_addr_r", 16, 0); |
238 | ||
607b0755 | 239 | return img_data->kernel_addr; |
87f02d5a MR |
240 | } |
241 | ||
86f4695b AD |
242 | /** |
243 | * android_image_get_kernel() - processes kernel part of Android boot images | |
e058176b | 244 | * @hdr: Pointer to boot image header, which is at the start |
86f4695b | 245 | * of the image. |
e058176b SO |
246 | * @vendor_boot_img: Pointer to vendor boot image header, which is at the |
247 | * start of the image. | |
86f4695b AD |
248 | * @verify: Checksum verification flag. Currently unimplemented. |
249 | * @os_data: Pointer to a ulong variable, will hold os data start | |
250 | * address. | |
251 | * @os_len: Pointer to a ulong variable, will hold os data length. | |
252 | * | |
253 | * This function returns the os image's start address and length. Also, | |
254 | * it appends the kernel command line to the bootargs env variable. | |
255 | * | |
256 | * Return: Zero, os start address and length on success, | |
257 | * otherwise on failure. | |
258 | */ | |
636da203 | 259 | int android_image_get_kernel(const void *hdr, |
e058176b | 260 | const void *vendor_boot_img, int verify, |
9ace3fc8 SS |
261 | ulong *os_data, ulong *os_len) |
262 | { | |
607b0755 | 263 | struct andr_image_data img_data = {0}; |
8f8e646d | 264 | ulong kernel_addr; |
607b0755 | 265 | const struct legacy_img_hdr *ihdr; |
d5a85e8e | 266 | ulong comp; |
607b0755 | 267 | |
e058176b | 268 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
607b0755 SO |
269 | return -EINVAL; |
270 | ||
d5a85e8e NA |
271 | comp = android_image_get_kcomp(hdr, vendor_boot_img); |
272 | ||
273 | kernel_addr = android_image_get_kernel_addr(&img_data, comp); | |
607b0755 | 274 | ihdr = (const struct legacy_img_hdr *)img_data.kernel_ptr; |
87f02d5a | 275 | |
9ace3fc8 SS |
276 | /* |
277 | * Not all Android tools use the id field for signing the image with | |
278 | * sha1 (or anything) so we don't check it. It is not obvious that the | |
279 | * string is null terminated so we take care of this. | |
280 | */ | |
607b0755 | 281 | strlcpy(andr_tmp_str, img_data.image_name, ANDR_BOOT_NAME_SIZE); |
9ace3fc8 SS |
282 | andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0'; |
283 | if (strlen(andr_tmp_str)) | |
284 | printf("Android's image name: %s\n", andr_tmp_str); | |
285 | ||
8f8e646d | 286 | printf("Kernel load addr 0x%08lx size %u KiB\n", |
607b0755 | 287 | kernel_addr, DIV_ROUND_UP(img_data.kernel_size, 1024)); |
86f4695b AD |
288 | |
289 | int len = 0; | |
9e5fad0f NB |
290 | char *bootargs = env_get("bootargs"); |
291 | ||
292 | if (bootargs) | |
293 | len += strlen(bootargs); | |
294 | ||
607b0755 SO |
295 | if (*img_data.kcmdline) { |
296 | printf("Kernel command line: %s\n", img_data.kcmdline); | |
9e5fad0f | 297 | len += strlen(img_data.kcmdline) + (len ? 1 : 0); /* +1 for extra space */ |
86f4695b AD |
298 | } |
299 | ||
53a0ddb6 | 300 | if (*img_data.kcmdline_extra) { |
b36b227b | 301 | printf("Kernel extra command line: %s\n", img_data.kcmdline_extra); |
9e5fad0f | 302 | len += strlen(img_data.kcmdline_extra) + (len ? 1 : 0); /* +1 for extra space */ |
b36b227b SO |
303 | } |
304 | ||
9e5fad0f | 305 | char *newbootargs = malloc(len + 1); /* +1 for the '\0' */ |
86f4695b AD |
306 | if (!newbootargs) { |
307 | puts("Error: malloc in android_image_get_kernel failed!\n"); | |
308 | return -ENOMEM; | |
309 | } | |
9e5fad0f | 310 | *newbootargs = '\0'; /* set to Null in case no components below are present */ |
86f4695b | 311 | |
9e5fad0f | 312 | if (bootargs) |
86f4695b | 313 | strcpy(newbootargs, bootargs); |
607b0755 | 314 | |
9e5fad0f NB |
315 | if (*img_data.kcmdline) { |
316 | if (*newbootargs) /* If there is something in newbootargs, a space is needed */ | |
317 | strcat(newbootargs, " "); | |
607b0755 | 318 | strcat(newbootargs, img_data.kcmdline); |
9e5fad0f | 319 | } |
86f4695b | 320 | |
53a0ddb6 | 321 | if (*img_data.kcmdline_extra) { |
9e5fad0f NB |
322 | if (*newbootargs) /* If there is something in newbootargs, a space is needed */ |
323 | strcat(newbootargs, " "); | |
b36b227b SO |
324 | strcat(newbootargs, img_data.kcmdline_extra); |
325 | } | |
326 | ||
382bee57 | 327 | env_set("bootargs", newbootargs); |
fd8b44a8 | 328 | free(newbootargs); |
9ace3fc8 SS |
329 | |
330 | if (os_data) { | |
39f790b0 RS |
331 | if (image_get_magic(ihdr) == IH_MAGIC) { |
332 | *os_data = image_get_data(ihdr); | |
333 | } else { | |
607b0755 | 334 | *os_data = img_data.kernel_ptr; |
39f790b0 RS |
335 | } |
336 | } | |
337 | if (os_len) { | |
338 | if (image_get_magic(ihdr) == IH_MAGIC) | |
339 | *os_len = image_get_data_size(ihdr); | |
340 | else | |
607b0755 | 341 | *os_len = img_data.kernel_size; |
9ace3fc8 | 342 | } |
9ace3fc8 SS |
343 | return 0; |
344 | } | |
345 | ||
1115027d SO |
346 | bool is_android_vendor_boot_image_header(const void *vendor_boot_img) |
347 | { | |
348 | return !memcmp(VENDOR_BOOT_MAGIC, vendor_boot_img, ANDR_VENDOR_BOOT_MAGIC_SIZE); | |
349 | } | |
350 | ||
636da203 | 351 | bool is_android_boot_image_header(const void *hdr) |
9ace3fc8 | 352 | { |
734cb47d | 353 | return !memcmp(ANDR_BOOT_MAGIC, hdr, ANDR_BOOT_MAGIC_SIZE); |
9ace3fc8 SS |
354 | } |
355 | ||
e058176b SO |
356 | ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr, |
357 | const void *vendor_boot_img) | |
9ace3fc8 | 358 | { |
607b0755 | 359 | struct andr_image_data img_data; |
9ace3fc8 | 360 | |
e058176b | 361 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
607b0755 | 362 | return -EINVAL; |
bb63363f | 363 | |
607b0755 SO |
364 | if (img_data.header_version > 2) |
365 | return 0; | |
bb63363f | 366 | |
607b0755 | 367 | return img_data.boot_img_total_size; |
9ace3fc8 SS |
368 | } |
369 | ||
636da203 | 370 | ulong android_image_get_kload(const void *hdr, |
e058176b | 371 | const void *vendor_boot_img) |
9ace3fc8 | 372 | { |
607b0755 | 373 | struct andr_image_data img_data; |
d5a85e8e | 374 | ulong comp; |
607b0755 | 375 | |
e058176b | 376 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
607b0755 SO |
377 | return -EINVAL; |
378 | ||
d5a85e8e NA |
379 | comp = android_image_get_kcomp(hdr, vendor_boot_img); |
380 | ||
381 | return android_image_get_kernel_addr(&img_data, comp); | |
9ace3fc8 SS |
382 | } |
383 | ||
636da203 | 384 | ulong android_image_get_kcomp(const void *hdr, |
e058176b | 385 | const void *vendor_boot_img) |
829ceb28 | 386 | { |
f48efa0e SO |
387 | struct andr_image_data img_data; |
388 | const void *p; | |
389 | ||
e058176b | 390 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
f48efa0e | 391 | return -EINVAL; |
829ceb28 | 392 | |
f48efa0e | 393 | p = (const void *)img_data.kernel_ptr; |
f3543e69 SG |
394 | if (image_get_magic((struct legacy_img_hdr *)p) == IH_MAGIC) |
395 | return image_get_comp((struct legacy_img_hdr *)p); | |
39f790b0 | 396 | else if (get_unaligned_le32(p) == LZ4F_MAGIC) |
829ceb28 ER |
397 | return IH_COMP_LZ4; |
398 | else | |
bc599042 | 399 | return image_decomp_type(p, sizeof(u32)); |
829ceb28 ER |
400 | } |
401 | ||
c79a2e68 SO |
402 | int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img, |
403 | ulong *rd_data, ulong *rd_len) | |
9ace3fc8 | 404 | { |
607b0755 | 405 | struct andr_image_data img_data = {0}; |
c79a2e68 | 406 | ulong ramdisk_ptr; |
607b0755 | 407 | |
e058176b | 408 | if (!android_image_get_data(hdr, vendor_boot_img, &img_data)) |
607b0755 SO |
409 | return -EINVAL; |
410 | ||
6509c3fe MW |
411 | if (!img_data.ramdisk_size) |
412 | return -ENOENT; | |
21e7fa0e NA |
413 | /* |
414 | * Android tools can generate a boot.img with default load address | |
415 | * or 0, even though it doesn't really make a lot of sense, and it | |
416 | * might be valid on some platforms, we treat that address as | |
417 | * the default value for this field, and try to pass ramdisk | |
418 | * in place if possible. | |
419 | */ | |
c79a2e68 | 420 | if (img_data.header_version > 2) { |
21e7fa0e NA |
421 | /* Ramdisk can't be used in-place, copy it to ramdisk_addr_r */ |
422 | if (img_data.ramdisk_addr == ANDROID_IMAGE_DEFAULT_RAMDISK_ADDR) { | |
423 | ramdisk_ptr = env_get_ulong("ramdisk_addr_r", 16, 0); | |
424 | if (!ramdisk_ptr) { | |
425 | printf("Invalid ramdisk_addr_r to copy ramdisk into\n"); | |
426 | return -EINVAL; | |
427 | } | |
428 | } else { | |
429 | ramdisk_ptr = img_data.ramdisk_addr; | |
430 | } | |
431 | *rd_data = ramdisk_ptr; | |
c79a2e68 SO |
432 | memcpy((void *)(ramdisk_ptr), (void *)img_data.vendor_ramdisk_ptr, |
433 | img_data.vendor_ramdisk_size); | |
da3447d0 RS |
434 | ramdisk_ptr += img_data.vendor_ramdisk_size; |
435 | memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr, | |
57e405e1 | 436 | img_data.boot_ramdisk_size); |
da3447d0 | 437 | ramdisk_ptr += img_data.boot_ramdisk_size; |
57e405e1 SO |
438 | if (img_data.bootconfig_size) { |
439 | memcpy((void *) | |
da3447d0 | 440 | (ramdisk_ptr), (void *)img_data.bootconfig_addr, |
57e405e1 SO |
441 | img_data.bootconfig_size); |
442 | } | |
1b1ffda4 | 443 | } else { |
21e7fa0e NA |
444 | /* Ramdisk can be used in-place, use current ptr */ |
445 | if (img_data.ramdisk_addr == 0 || | |
446 | img_data.ramdisk_addr == ANDROID_IMAGE_DEFAULT_RAMDISK_ADDR) { | |
447 | *rd_data = img_data.ramdisk_ptr; | |
448 | } else { | |
449 | ramdisk_ptr = img_data.ramdisk_addr; | |
450 | *rd_data = ramdisk_ptr; | |
451 | memcpy((void *)(ramdisk_ptr), (void *)img_data.ramdisk_ptr, | |
452 | img_data.ramdisk_size); | |
453 | } | |
c79a2e68 | 454 | } |
86f4695b | 455 | |
607b0755 | 456 | printf("RAM disk load addr 0x%08lx size %u KiB\n", |
21e7fa0e | 457 | *rd_data, DIV_ROUND_UP(img_data.ramdisk_size, 1024)); |
9ace3fc8 | 458 | |
607b0755 | 459 | *rd_len = img_data.ramdisk_size; |
9ace3fc8 SS |
460 | return 0; |
461 | } | |
4f1318b2 | 462 | |
636da203 | 463 | int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len) |
10481614 | 464 | { |
607b0755 SO |
465 | struct andr_image_data img_data; |
466 | ||
e058176b | 467 | if (!android_image_get_data(hdr, NULL, &img_data)) |
607b0755 SO |
468 | return -EINVAL; |
469 | ||
636da203 SO |
470 | if (img_data.header_version > 2) { |
471 | printf("Second stage bootloader is only supported for boot image version <= 2\n"); | |
472 | return -EOPNOTSUPP; | |
473 | } | |
474 | ||
607b0755 | 475 | if (!img_data.second_size) { |
10481614 BC |
476 | *second_data = *second_len = 0; |
477 | return -1; | |
478 | } | |
479 | ||
607b0755 | 480 | *second_data = img_data.second_ptr; |
10481614 BC |
481 | |
482 | printf("second address is 0x%lx\n",*second_data); | |
483 | ||
607b0755 | 484 | *second_len = img_data.second_size; |
10481614 BC |
485 | return 0; |
486 | } | |
487 | ||
7f253150 SP |
488 | /** |
489 | * android_image_get_dtbo() - Get address and size of recovery DTBO image. | |
490 | * @hdr_addr: Boot image header address | |
491 | * @addr: If not NULL, will contain address of recovery DTBO image | |
492 | * @size: If not NULL, will contain size of recovery DTBO image | |
493 | * | |
494 | * Get the address and size of DTBO image in "Recovery DTBO" area of Android | |
495 | * Boot Image in RAM. The format of this image is Android DTBO (see | |
496 | * corresponding "DTB/DTBO Partitions" AOSP documentation for details). Once | |
497 | * the address is obtained from this function, one can use 'adtimg' U-Boot | |
498 | * command or android_dt_*() functions to extract desired DTBO blob. | |
499 | * | |
500 | * This DTBO (included in boot image) is only needed for non-A/B devices, and it | |
501 | * only can be found in recovery image. On A/B devices we can always rely on | |
502 | * "dtbo" partition. See "Including DTBO in Recovery for Non-A/B Devices" in | |
503 | * AOSP documentation for details. | |
504 | * | |
505 | * Return: true on success or false on error. | |
506 | */ | |
507 | bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size) | |
508 | { | |
d71a732a | 509 | const struct andr_boot_img_hdr_v0 *hdr; |
7f253150 SP |
510 | ulong dtbo_img_addr; |
511 | bool ret = true; | |
512 | ||
513 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
734cb47d | 514 | if (!is_android_boot_image_header(hdr)) { |
7f253150 SP |
515 | printf("Error: Boot Image header is incorrect\n"); |
516 | ret = false; | |
517 | goto exit; | |
518 | } | |
519 | ||
447240e2 SO |
520 | if (hdr->header_version != 1 && hdr->header_version != 2) { |
521 | printf("Error: header version must be >= 1 and <= 2 to get dtbo\n"); | |
7f253150 SP |
522 | ret = false; |
523 | goto exit; | |
524 | } | |
525 | ||
526 | if (hdr->recovery_dtbo_size == 0) { | |
527 | printf("Error: recovery_dtbo_size is 0\n"); | |
528 | ret = false; | |
529 | goto exit; | |
530 | } | |
531 | ||
532 | /* Calculate the address of DTB area in boot image */ | |
533 | dtbo_img_addr = hdr_addr; | |
534 | dtbo_img_addr += hdr->page_size; | |
535 | dtbo_img_addr += ALIGN(hdr->kernel_size, hdr->page_size); | |
536 | dtbo_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size); | |
537 | dtbo_img_addr += ALIGN(hdr->second_size, hdr->page_size); | |
538 | ||
539 | if (addr) | |
540 | *addr = dtbo_img_addr; | |
541 | if (size) | |
542 | *size = hdr->recovery_dtbo_size; | |
543 | ||
544 | exit: | |
545 | unmap_sysmem(hdr); | |
546 | return ret; | |
547 | } | |
548 | ||
c3bfad82 SP |
549 | /** |
550 | * android_image_get_dtb_img_addr() - Get the address of DTB area in boot image. | |
551 | * @hdr_addr: Boot image header address | |
e058176b | 552 | * @vhdr_addr: Vendor Boot image header address |
c3bfad82 SP |
553 | * @addr: Will contain the address of DTB area in boot image |
554 | * | |
555 | * Return: true on success or false on fail. | |
556 | */ | |
e058176b | 557 | static bool android_image_get_dtb_img_addr(ulong hdr_addr, ulong vhdr_addr, ulong *addr) |
c3bfad82 | 558 | { |
d71a732a | 559 | const struct andr_boot_img_hdr_v0 *hdr; |
2d0da197 | 560 | const struct andr_vnd_boot_img_hdr *v_hdr; |
c3bfad82 SP |
561 | ulong dtb_img_addr; |
562 | bool ret = true; | |
563 | ||
564 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
734cb47d | 565 | if (!is_android_boot_image_header(hdr)) { |
c3bfad82 SP |
566 | printf("Error: Boot Image header is incorrect\n"); |
567 | ret = false; | |
568 | goto exit; | |
569 | } | |
570 | ||
571 | if (hdr->header_version < 2) { | |
572 | printf("Error: header_version must be >= 2 to get dtb\n"); | |
573 | ret = false; | |
574 | goto exit; | |
575 | } | |
576 | ||
2d0da197 SO |
577 | if (hdr->header_version == 2) { |
578 | if (!hdr->dtb_size) { | |
579 | printf("Error: dtb_size is 0\n"); | |
580 | ret = false; | |
581 | goto exit; | |
582 | } | |
583 | /* Calculate the address of DTB area in boot image */ | |
584 | dtb_img_addr = hdr_addr; | |
585 | dtb_img_addr += hdr->page_size; | |
586 | dtb_img_addr += ALIGN(hdr->kernel_size, hdr->page_size); | |
587 | dtb_img_addr += ALIGN(hdr->ramdisk_size, hdr->page_size); | |
588 | dtb_img_addr += ALIGN(hdr->second_size, hdr->page_size); | |
589 | dtb_img_addr += ALIGN(hdr->recovery_dtbo_size, hdr->page_size); | |
590 | ||
591 | *addr = dtb_img_addr; | |
c3bfad82 SP |
592 | } |
593 | ||
2d0da197 SO |
594 | if (hdr->header_version > 2) { |
595 | v_hdr = map_sysmem(vhdr_addr, sizeof(*v_hdr)); | |
596 | if (!v_hdr->dtb_size) { | |
597 | printf("Error: dtb_size is 0\n"); | |
598 | ret = false; | |
599 | unmap_sysmem(v_hdr); | |
600 | goto exit; | |
601 | } | |
602 | /* Calculate the address of DTB area in boot image */ | |
603 | dtb_img_addr = vhdr_addr; | |
604 | dtb_img_addr += v_hdr->page_size; | |
605 | if (v_hdr->vendor_ramdisk_size) | |
606 | dtb_img_addr += ALIGN(v_hdr->vendor_ramdisk_size, v_hdr->page_size); | |
607 | *addr = dtb_img_addr; | |
608 | unmap_sysmem(v_hdr); | |
609 | goto exit; | |
610 | } | |
c3bfad82 SP |
611 | exit: |
612 | unmap_sysmem(hdr); | |
613 | return ret; | |
614 | } | |
615 | ||
616 | /** | |
617 | * android_image_get_dtb_by_index() - Get address and size of blob in DTB area. | |
618 | * @hdr_addr: Boot image header address | |
e058176b | 619 | * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image. |
c3bfad82 SP |
620 | * @index: Index of desired DTB in DTB area (starting from 0) |
621 | * @addr: If not NULL, will contain address to specified DTB | |
622 | * @size: If not NULL, will contain size of specified DTB | |
623 | * | |
624 | * Get the address and size of DTB blob by its index in DTB area of Android | |
625 | * Boot Image in RAM. | |
626 | * | |
627 | * Return: true on success or false on error. | |
628 | */ | |
e058176b SO |
629 | bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img, |
630 | u32 index, ulong *addr, u32 *size) | |
c3bfad82 | 631 | { |
607b0755 | 632 | struct andr_image_data img_data; |
d71a732a | 633 | const struct andr_boot_img_hdr_v0 *hdr; |
e058176b | 634 | const struct andr_vnd_boot_img_hdr *vhdr; |
607b0755 SO |
635 | |
636 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
e058176b SO |
637 | if (vendor_boot_img != -1) |
638 | vhdr = map_sysmem(vendor_boot_img, sizeof(*vhdr)); | |
639 | if (!android_image_get_data(hdr, vhdr, &img_data)) { | |
640 | if (vendor_boot_img != -1) | |
641 | unmap_sysmem(vhdr); | |
607b0755 SO |
642 | unmap_sysmem(hdr); |
643 | return false; | |
644 | } | |
e058176b SO |
645 | if (vendor_boot_img != -1) |
646 | unmap_sysmem(vhdr); | |
607b0755 SO |
647 | unmap_sysmem(hdr); |
648 | ||
c3bfad82 SP |
649 | ulong dtb_img_addr; /* address of DTB part in boot image */ |
650 | u32 dtb_img_size; /* size of DTB payload in boot image */ | |
651 | ulong dtb_addr; /* address of DTB blob with specified index */ | |
652 | u32 i; /* index iterator */ | |
653 | ||
e058176b | 654 | android_image_get_dtb_img_addr(hdr_addr, vendor_boot_img, &dtb_img_addr); |
c3bfad82 SP |
655 | /* Check if DTB area of boot image is in DTBO format */ |
656 | if (android_dt_check_header(dtb_img_addr)) { | |
657 | return android_dt_get_fdt_by_index(dtb_img_addr, index, addr, | |
658 | size); | |
659 | } | |
660 | ||
661 | /* Find out the address of DTB with specified index in concat blobs */ | |
607b0755 | 662 | dtb_img_size = img_data.dtb_size; |
c3bfad82 SP |
663 | i = 0; |
664 | dtb_addr = dtb_img_addr; | |
665 | while (dtb_addr < dtb_img_addr + dtb_img_size) { | |
666 | const struct fdt_header *fdt; | |
667 | u32 dtb_size; | |
668 | ||
669 | fdt = map_sysmem(dtb_addr, sizeof(*fdt)); | |
670 | if (fdt_check_header(fdt) != 0) { | |
671 | unmap_sysmem(fdt); | |
672 | printf("Error: Invalid FDT header for index %u\n", i); | |
673 | return false; | |
674 | } | |
675 | ||
676 | dtb_size = fdt_totalsize(fdt); | |
677 | unmap_sysmem(fdt); | |
678 | ||
679 | if (i == index) { | |
680 | if (size) | |
681 | *size = dtb_size; | |
682 | if (addr) | |
683 | *addr = dtb_addr; | |
684 | return true; | |
685 | } | |
686 | ||
687 | dtb_addr += dtb_size; | |
688 | ++i; | |
689 | } | |
690 | ||
691 | printf("Error: Index is out of bounds (%u/%u)\n", index, i); | |
692 | return false; | |
693 | } | |
694 | ||
1d6132e2 | 695 | #if !defined(CONFIG_XPL_BUILD) |
4f1318b2 MT |
696 | /** |
697 | * android_print_contents - prints out the contents of the Android format image | |
698 | * @hdr: pointer to the Android format image header | |
699 | * | |
700 | * android_print_contents() formats a multi line Android image contents | |
701 | * description. | |
702 | * The routine prints out Android image properties | |
703 | * | |
704 | * returns: | |
705 | * no returned results | |
706 | */ | |
d71a732a | 707 | void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr) |
4f1318b2 | 708 | { |
bb5d6927 SO |
709 | if (hdr->header_version >= 3) { |
710 | printf("Content print is not supported for boot image header version > 2"); | |
711 | return; | |
712 | } | |
4f1318b2 | 713 | const char * const p = IMAGE_INDENT_STRING; |
210a7176 AD |
714 | /* os_version = ver << 11 | lvl */ |
715 | u32 os_ver = hdr->os_version >> 11; | |
716 | u32 os_lvl = hdr->os_version & ((1U << 11) - 1); | |
4f1318b2 | 717 | |
bb63363f SP |
718 | printf("%skernel size: %x\n", p, hdr->kernel_size); |
719 | printf("%skernel address: %x\n", p, hdr->kernel_addr); | |
720 | printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); | |
721 | printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr); | |
722 | printf("%ssecond size: %x\n", p, hdr->second_size); | |
723 | printf("%ssecond address: %x\n", p, hdr->second_addr); | |
724 | printf("%stags address: %x\n", p, hdr->tags_addr); | |
725 | printf("%spage size: %x\n", p, hdr->page_size); | |
210a7176 AD |
726 | /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) |
727 | * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ | |
bb63363f | 728 | printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", |
210a7176 AD |
729 | p, hdr->os_version, |
730 | (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, | |
731 | (os_lvl >> 4) + 2000, os_lvl & 0x0F); | |
bb63363f SP |
732 | printf("%sname: %s\n", p, hdr->name); |
733 | printf("%scmdline: %s\n", p, hdr->cmdline); | |
734 | printf("%sheader_version: %d\n", p, hdr->header_version); | |
735 | ||
736 | if (hdr->header_version >= 1) { | |
737 | printf("%srecovery dtbo size: %x\n", p, | |
738 | hdr->recovery_dtbo_size); | |
739 | printf("%srecovery dtbo offset: %llx\n", p, | |
740 | hdr->recovery_dtbo_offset); | |
741 | printf("%sheader size: %x\n", p, | |
742 | hdr->header_size); | |
743 | } | |
744 | ||
bb5d6927 | 745 | if (hdr->header_version == 2) { |
bb63363f SP |
746 | printf("%sdtb size: %x\n", p, hdr->dtb_size); |
747 | printf("%sdtb addr: %llx\n", p, hdr->dtb_addr); | |
748 | } | |
4f1318b2 | 749 | } |
c3bfad82 SP |
750 | |
751 | /** | |
752 | * android_image_print_dtb_info - Print info for one DTB blob in DTB area. | |
753 | * @fdt: DTB header | |
754 | * @index: Number of DTB blob in DTB area. | |
755 | * | |
756 | * Return: true on success or false on error. | |
757 | */ | |
758 | static bool android_image_print_dtb_info(const struct fdt_header *fdt, | |
759 | u32 index) | |
760 | { | |
761 | int root_node_off; | |
762 | u32 fdt_size; | |
763 | const char *model; | |
764 | const char *compatible; | |
765 | ||
766 | root_node_off = fdt_path_offset(fdt, "/"); | |
767 | if (root_node_off < 0) { | |
768 | printf("Error: Root node not found\n"); | |
769 | return false; | |
770 | } | |
771 | ||
772 | fdt_size = fdt_totalsize(fdt); | |
773 | compatible = fdt_getprop(fdt, root_node_off, "compatible", | |
774 | NULL); | |
775 | model = fdt_getprop(fdt, root_node_off, "model", NULL); | |
776 | ||
777 | printf(" - DTB #%u:\n", index); | |
778 | printf(" (DTB)size = %d\n", fdt_size); | |
779 | printf(" (DTB)model = %s\n", model ? model : "(unknown)"); | |
780 | printf(" (DTB)compatible = %s\n", | |
781 | compatible ? compatible : "(unknown)"); | |
782 | ||
783 | return true; | |
784 | } | |
785 | ||
786 | /** | |
787 | * android_image_print_dtb_contents() - Print info for DTB blobs in DTB area. | |
788 | * @hdr_addr: Boot image header address | |
789 | * | |
790 | * DTB payload in Android Boot Image v2+ can be in one of following formats: | |
791 | * 1. Concatenated DTB blobs | |
792 | * 2. Android DTBO format (see CONFIG_CMD_ADTIMG for details) | |
793 | * | |
794 | * This function does next: | |
795 | * 1. Prints out the format used in DTB area | |
796 | * 2. Iterates over all DTB blobs in DTB area and prints out the info for | |
797 | * each blob. | |
798 | * | |
799 | * Return: true on success or false on error. | |
800 | */ | |
801 | bool android_image_print_dtb_contents(ulong hdr_addr) | |
802 | { | |
d71a732a | 803 | const struct andr_boot_img_hdr_v0 *hdr; |
c3bfad82 SP |
804 | bool res; |
805 | ulong dtb_img_addr; /* address of DTB part in boot image */ | |
806 | u32 dtb_img_size; /* size of DTB payload in boot image */ | |
807 | ulong dtb_addr; /* address of DTB blob with specified index */ | |
808 | u32 i; /* index iterator */ | |
809 | ||
e058176b | 810 | res = android_image_get_dtb_img_addr(hdr_addr, 0, &dtb_img_addr); |
c3bfad82 SP |
811 | if (!res) |
812 | return false; | |
813 | ||
814 | /* Check if DTB area of boot image is in DTBO format */ | |
815 | if (android_dt_check_header(dtb_img_addr)) { | |
816 | printf("## DTB area contents (DTBO format):\n"); | |
817 | android_dt_print_contents(dtb_img_addr); | |
818 | return true; | |
819 | } | |
820 | ||
821 | printf("## DTB area contents (concat format):\n"); | |
822 | ||
823 | /* Iterate over concatenated DTB blobs */ | |
824 | hdr = map_sysmem(hdr_addr, sizeof(*hdr)); | |
825 | dtb_img_size = hdr->dtb_size; | |
826 | unmap_sysmem(hdr); | |
827 | i = 0; | |
828 | dtb_addr = dtb_img_addr; | |
829 | while (dtb_addr < dtb_img_addr + dtb_img_size) { | |
830 | const struct fdt_header *fdt; | |
831 | u32 dtb_size; | |
832 | ||
833 | fdt = map_sysmem(dtb_addr, sizeof(*fdt)); | |
834 | if (fdt_check_header(fdt) != 0) { | |
835 | unmap_sysmem(fdt); | |
836 | printf("Error: Invalid FDT header for index %u\n", i); | |
837 | return false; | |
838 | } | |
839 | ||
840 | res = android_image_print_dtb_info(fdt, i); | |
841 | if (!res) { | |
842 | unmap_sysmem(fdt); | |
843 | return false; | |
844 | } | |
845 | ||
846 | dtb_size = fdt_totalsize(fdt); | |
847 | unmap_sysmem(fdt); | |
848 | dtb_addr += dtb_size; | |
849 | ++i; | |
850 | } | |
851 | ||
852 | return true; | |
853 | } | |
4f1318b2 | 854 | #endif |