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