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