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