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