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