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> |
| 9 | #include <android_image.h> |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <errno.h> |
Eugeniu Rosca | 1403f39 | 2019-04-08 17:35:27 +0200 | [diff] [blame] | 12 | #include <asm/unaligned.h> |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 13 | |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 14 | #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000 |
| 15 | |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 16 | static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1]; |
| 17 | |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 18 | static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr) |
| 19 | { |
| 20 | /* |
| 21 | * All the Android tools that generate a boot.img use this |
| 22 | * address as the default. |
| 23 | * |
| 24 | * Even though it doesn't really make a lot of sense, and it |
| 25 | * might be valid on some platforms, we treat that adress as |
| 26 | * the default value for this field, and try to execute the |
| 27 | * kernel in place in such a case. |
| 28 | * |
| 29 | * Otherwise, we will return the actual value set by the user. |
| 30 | */ |
| 31 | if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR) |
| 32 | return (ulong)hdr + hdr->page_size; |
| 33 | |
| 34 | return hdr->kernel_addr; |
| 35 | } |
| 36 | |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 37 | /** |
| 38 | * android_image_get_kernel() - processes kernel part of Android boot images |
| 39 | * @hdr: Pointer to image header, which is at the start |
| 40 | * of the image. |
| 41 | * @verify: Checksum verification flag. Currently unimplemented. |
| 42 | * @os_data: Pointer to a ulong variable, will hold os data start |
| 43 | * address. |
| 44 | * @os_len: Pointer to a ulong variable, will hold os data length. |
| 45 | * |
| 46 | * This function returns the os image's start address and length. Also, |
| 47 | * it appends the kernel command line to the bootargs env variable. |
| 48 | * |
| 49 | * Return: Zero, os start address and length on success, |
| 50 | * otherwise on failure. |
| 51 | */ |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 52 | int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify, |
| 53 | ulong *os_data, ulong *os_len) |
| 54 | { |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 55 | u32 kernel_addr = android_image_get_kernel_addr(hdr); |
Roman Stratiienko | 7326858 | 2019-06-03 15:38:13 +0300 | [diff] [blame] | 56 | const struct image_header *ihdr = (const struct image_header *) |
| 57 | ((uintptr_t)hdr + hdr->page_size); |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 58 | |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 59 | /* |
| 60 | * Not all Android tools use the id field for signing the image with |
| 61 | * sha1 (or anything) so we don't check it. It is not obvious that the |
| 62 | * string is null terminated so we take care of this. |
| 63 | */ |
| 64 | strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE); |
| 65 | andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0'; |
| 66 | if (strlen(andr_tmp_str)) |
| 67 | printf("Android's image name: %s\n", andr_tmp_str); |
| 68 | |
| 69 | printf("Kernel load addr 0x%08x size %u KiB\n", |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 70 | kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024)); |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 71 | |
| 72 | int len = 0; |
| 73 | if (*hdr->cmdline) { |
| 74 | printf("Kernel command line: %s\n", hdr->cmdline); |
| 75 | len += strlen(hdr->cmdline); |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 76 | } |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 77 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 78 | char *bootargs = env_get("bootargs"); |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 79 | if (bootargs) |
| 80 | len += strlen(bootargs); |
| 81 | |
| 82 | char *newbootargs = malloc(len + 2); |
| 83 | if (!newbootargs) { |
| 84 | puts("Error: malloc in android_image_get_kernel failed!\n"); |
| 85 | return -ENOMEM; |
| 86 | } |
| 87 | *newbootargs = '\0'; |
| 88 | |
| 89 | if (bootargs) { |
| 90 | strcpy(newbootargs, bootargs); |
| 91 | strcat(newbootargs, " "); |
| 92 | } |
| 93 | if (*hdr->cmdline) |
| 94 | strcat(newbootargs, hdr->cmdline); |
| 95 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 96 | env_set("bootargs", newbootargs); |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 97 | |
| 98 | if (os_data) { |
Roman Stratiienko | 7326858 | 2019-06-03 15:38:13 +0300 | [diff] [blame] | 99 | if (image_get_magic(ihdr) == IH_MAGIC) { |
| 100 | *os_data = image_get_data(ihdr); |
| 101 | } else { |
| 102 | *os_data = (ulong)hdr; |
| 103 | *os_data += hdr->page_size; |
| 104 | } |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 105 | } |
Roman Stratiienko | 7326858 | 2019-06-03 15:38:13 +0300 | [diff] [blame] | 106 | if (os_len) { |
| 107 | if (image_get_magic(ihdr) == IH_MAGIC) |
| 108 | *os_len = image_get_data_size(ihdr); |
| 109 | else |
| 110 | *os_len = hdr->kernel_size; |
| 111 | } |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int android_image_check_header(const struct andr_img_hdr *hdr) |
| 116 | { |
| 117 | return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE); |
| 118 | } |
| 119 | |
| 120 | ulong android_image_get_end(const struct andr_img_hdr *hdr) |
| 121 | { |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 122 | ulong end; |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 123 | /* |
| 124 | * The header takes a full page, the remaining components are aligned |
| 125 | * on page boundary |
| 126 | */ |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 127 | end = (ulong)hdr; |
| 128 | end += hdr->page_size; |
| 129 | end += ALIGN(hdr->kernel_size, hdr->page_size); |
| 130 | end += ALIGN(hdr->ramdisk_size, hdr->page_size); |
| 131 | end += ALIGN(hdr->second_size, hdr->page_size); |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 132 | |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 133 | return end; |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | ulong android_image_get_kload(const struct andr_img_hdr *hdr) |
| 137 | { |
Maxime Ripard | 17ef1f5 | 2015-04-24 12:53:12 +0200 | [diff] [blame] | 138 | return android_image_get_kernel_addr(hdr); |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 139 | } |
| 140 | |
Eugeniu Rosca | 1403f39 | 2019-04-08 17:35:27 +0200 | [diff] [blame] | 141 | ulong android_image_get_kcomp(const struct andr_img_hdr *hdr) |
| 142 | { |
| 143 | const void *p = (void *)((uintptr_t)hdr + hdr->page_size); |
| 144 | |
Roman Stratiienko | 7326858 | 2019-06-03 15:38:13 +0300 | [diff] [blame] | 145 | if (image_get_magic((image_header_t *)p) == IH_MAGIC) |
| 146 | return image_get_comp((image_header_t *)p); |
| 147 | else if (get_unaligned_le32(p) == LZ4F_MAGIC) |
Eugeniu Rosca | 1403f39 | 2019-04-08 17:35:27 +0200 | [diff] [blame] | 148 | return IH_COMP_LZ4; |
| 149 | else |
| 150 | return IH_COMP_NONE; |
| 151 | } |
| 152 | |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 153 | int android_image_get_ramdisk(const struct andr_img_hdr *hdr, |
| 154 | ulong *rd_data, ulong *rd_len) |
| 155 | { |
Rob Herring | d979878 | 2015-10-05 14:37:07 -0500 | [diff] [blame] | 156 | if (!hdr->ramdisk_size) { |
| 157 | *rd_data = *rd_len = 0; |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 158 | return -1; |
Rob Herring | d979878 | 2015-10-05 14:37:07 -0500 | [diff] [blame] | 159 | } |
Ahmad Draidi | 9a1cb5d | 2014-10-23 20:50:07 +0300 | [diff] [blame] | 160 | |
| 161 | printf("RAM disk load addr 0x%08x size %u KiB\n", |
| 162 | hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024)); |
| 163 | |
Sebastian Siewior | 686d667 | 2014-05-05 15:08:09 -0500 | [diff] [blame] | 164 | *rd_data = (unsigned long)hdr; |
| 165 | *rd_data += hdr->page_size; |
| 166 | *rd_data += ALIGN(hdr->kernel_size, hdr->page_size); |
| 167 | |
| 168 | *rd_len = hdr->ramdisk_size; |
| 169 | return 0; |
| 170 | } |
Michael Trimarchi | 44af20b | 2016-06-10 19:54:37 +0200 | [diff] [blame] | 171 | |
Bin Chen | 909f140 | 2018-01-27 16:59:08 +1100 | [diff] [blame] | 172 | int android_image_get_second(const struct andr_img_hdr *hdr, |
| 173 | ulong *second_data, ulong *second_len) |
| 174 | { |
| 175 | if (!hdr->second_size) { |
| 176 | *second_data = *second_len = 0; |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | *second_data = (unsigned long)hdr; |
| 181 | *second_data += hdr->page_size; |
| 182 | *second_data += ALIGN(hdr->kernel_size, hdr->page_size); |
| 183 | *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size); |
| 184 | |
| 185 | printf("second address is 0x%lx\n",*second_data); |
| 186 | |
| 187 | *second_len = hdr->second_size; |
| 188 | return 0; |
| 189 | } |
| 190 | |
Michael Trimarchi | 44af20b | 2016-06-10 19:54:37 +0200 | [diff] [blame] | 191 | #if !defined(CONFIG_SPL_BUILD) |
| 192 | /** |
| 193 | * android_print_contents - prints out the contents of the Android format image |
| 194 | * @hdr: pointer to the Android format image header |
| 195 | * |
| 196 | * android_print_contents() formats a multi line Android image contents |
| 197 | * description. |
| 198 | * The routine prints out Android image properties |
| 199 | * |
| 200 | * returns: |
| 201 | * no returned results |
| 202 | */ |
| 203 | void android_print_contents(const struct andr_img_hdr *hdr) |
| 204 | { |
| 205 | const char * const p = IMAGE_INDENT_STRING; |
Alex Deymo | 41f513a | 2017-04-02 01:49:47 -0700 | [diff] [blame] | 206 | /* os_version = ver << 11 | lvl */ |
| 207 | u32 os_ver = hdr->os_version >> 11; |
| 208 | u32 os_lvl = hdr->os_version & ((1U << 11) - 1); |
Michael Trimarchi | 44af20b | 2016-06-10 19:54:37 +0200 | [diff] [blame] | 209 | |
| 210 | printf("%skernel size: %x\n", p, hdr->kernel_size); |
| 211 | printf("%skernel address: %x\n", p, hdr->kernel_addr); |
| 212 | printf("%sramdisk size: %x\n", p, hdr->ramdisk_size); |
Eugeniu Rosca | 792631c | 2019-04-08 17:35:28 +0200 | [diff] [blame] | 213 | printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr); |
Michael Trimarchi | 44af20b | 2016-06-10 19:54:37 +0200 | [diff] [blame] | 214 | printf("%ssecond size: %x\n", p, hdr->second_size); |
| 215 | printf("%ssecond address: %x\n", p, hdr->second_addr); |
| 216 | printf("%stags address: %x\n", p, hdr->tags_addr); |
| 217 | printf("%spage size: %x\n", p, hdr->page_size); |
Alex Deymo | 41f513a | 2017-04-02 01:49:47 -0700 | [diff] [blame] | 218 | /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C) |
| 219 | * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */ |
| 220 | printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n", |
| 221 | p, hdr->os_version, |
| 222 | (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F, |
| 223 | (os_lvl >> 4) + 2000, os_lvl & 0x0F); |
Michael Trimarchi | 44af20b | 2016-06-10 19:54:37 +0200 | [diff] [blame] | 224 | printf("%sname: %s\n", p, hdr->name); |
| 225 | printf("%scmdline: %s\n", p, hdr->cmdline); |
| 226 | } |
| 227 | #endif |