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