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