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