blob: 3564a64221d2dd37de4095112b171ddb1f47fd7d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05002/*
3 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05004 */
5
6#include <common.h>
Simon Glass9fb625c2019-08-01 09:46:51 -06007#include <env.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05008#include <image.h>
9#include <android_image.h>
Ahmad Draidi86f46952014-10-23 20:50:07 +030010#include <malloc.h>
11#include <errno.h>
Eugeniu Rosca829ceb22019-04-08 17:35:27 +020012#include <asm/unaligned.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050013
Maxime Ripard87f02d52015-04-24 12:53:12 +020014#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
15
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050016static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
17
Maxime Ripard87f02d52015-04-24 12:53:12 +020018static 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 Draidi86f46952014-10-23 20:50:07 +030037/**
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 Siewior9ace3fc2014-05-05 15:08:09 -050052int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
53 ulong *os_data, ulong *os_len)
54{
Maxime Ripard87f02d52015-04-24 12:53:12 +020055 u32 kernel_addr = android_image_get_kernel_addr(hdr);
Roman Stratiienko39f790b2019-06-03 15:38:13 +030056 const struct image_header *ihdr = (const struct image_header *)
57 ((uintptr_t)hdr + hdr->page_size);
Maxime Ripard87f02d52015-04-24 12:53:12 +020058
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050059 /*
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 Ripard87f02d52015-04-24 12:53:12 +020070 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
Ahmad Draidi86f46952014-10-23 20:50:07 +030071
72 int len = 0;
73 if (*hdr->cmdline) {
74 printf("Kernel command line: %s\n", hdr->cmdline);
75 len += strlen(hdr->cmdline);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050076 }
Ahmad Draidi86f46952014-10-23 20:50:07 +030077
Simon Glass00caae62017-08-03 12:22:12 -060078 char *bootargs = env_get("bootargs");
Ahmad Draidi86f46952014-10-23 20:50:07 +030079 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 Glass382bee52017-08-03 12:22:09 -060096 env_set("bootargs", newbootargs);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050097
98 if (os_data) {
Roman Stratiienko39f790b2019-06-03 15:38:13 +030099 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 Siewior9ace3fc2014-05-05 15:08:09 -0500105 }
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300106 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 Siewior9ace3fc2014-05-05 15:08:09 -0500112 return 0;
113}
114
115int 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
120ulong android_image_get_end(const struct andr_img_hdr *hdr)
121{
Ahmad Draidi86f46952014-10-23 20:50:07 +0300122 ulong end;
Sam Protsenkobb633632019-08-15 20:25:07 +0300123
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500124 /*
125 * The header takes a full page, the remaining components are aligned
126 * on page boundary
127 */
Ahmad Draidi86f46952014-10-23 20:50:07 +0300128 end = (ulong)hdr;
129 end += hdr->page_size;
130 end += ALIGN(hdr->kernel_size, hdr->page_size);
131 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
132 end += ALIGN(hdr->second_size, hdr->page_size);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500133
Sam Protsenkobb633632019-08-15 20:25:07 +0300134 if (hdr->header_version >= 1)
135 end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
136
137 if (hdr->header_version >= 2)
138 end += ALIGN(hdr->dtb_size, hdr->page_size);
139
Ahmad Draidi86f46952014-10-23 20:50:07 +0300140 return end;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500141}
142
143ulong android_image_get_kload(const struct andr_img_hdr *hdr)
144{
Maxime Ripard87f02d52015-04-24 12:53:12 +0200145 return android_image_get_kernel_addr(hdr);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500146}
147
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200148ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
149{
150 const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
151
Roman Stratiienko39f790b2019-06-03 15:38:13 +0300152 if (image_get_magic((image_header_t *)p) == IH_MAGIC)
153 return image_get_comp((image_header_t *)p);
154 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200155 return IH_COMP_LZ4;
156 else
157 return IH_COMP_NONE;
158}
159
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500160int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
161 ulong *rd_data, ulong *rd_len)
162{
Rob Herring99500982015-10-05 14:37:07 -0500163 if (!hdr->ramdisk_size) {
164 *rd_data = *rd_len = 0;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500165 return -1;
Rob Herring99500982015-10-05 14:37:07 -0500166 }
Ahmad Draidi86f46952014-10-23 20:50:07 +0300167
168 printf("RAM disk load addr 0x%08x size %u KiB\n",
169 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
170
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500171 *rd_data = (unsigned long)hdr;
172 *rd_data += hdr->page_size;
173 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
174
175 *rd_len = hdr->ramdisk_size;
176 return 0;
177}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200178
Bin Chen10481612018-01-27 16:59:08 +1100179int android_image_get_second(const struct andr_img_hdr *hdr,
180 ulong *second_data, ulong *second_len)
181{
182 if (!hdr->second_size) {
183 *second_data = *second_len = 0;
184 return -1;
185 }
186
187 *second_data = (unsigned long)hdr;
188 *second_data += hdr->page_size;
189 *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
190 *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
191
192 printf("second address is 0x%lx\n",*second_data);
193
194 *second_len = hdr->second_size;
195 return 0;
196}
197
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200198#if !defined(CONFIG_SPL_BUILD)
199/**
200 * android_print_contents - prints out the contents of the Android format image
201 * @hdr: pointer to the Android format image header
202 *
203 * android_print_contents() formats a multi line Android image contents
204 * description.
205 * The routine prints out Android image properties
206 *
207 * returns:
208 * no returned results
209 */
210void android_print_contents(const struct andr_img_hdr *hdr)
211{
212 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo210a7172017-04-02 01:49:47 -0700213 /* os_version = ver << 11 | lvl */
214 u32 os_ver = hdr->os_version >> 11;
215 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200216
Sam Protsenkobb633632019-08-15 20:25:07 +0300217 printf("%skernel size: %x\n", p, hdr->kernel_size);
218 printf("%skernel address: %x\n", p, hdr->kernel_addr);
219 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
220 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
221 printf("%ssecond size: %x\n", p, hdr->second_size);
222 printf("%ssecond address: %x\n", p, hdr->second_addr);
223 printf("%stags address: %x\n", p, hdr->tags_addr);
224 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo210a7172017-04-02 01:49:47 -0700225 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
226 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
Sam Protsenkobb633632019-08-15 20:25:07 +0300227 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
Alex Deymo210a7172017-04-02 01:49:47 -0700228 p, hdr->os_version,
229 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
230 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Sam Protsenkobb633632019-08-15 20:25:07 +0300231 printf("%sname: %s\n", p, hdr->name);
232 printf("%scmdline: %s\n", p, hdr->cmdline);
233 printf("%sheader_version: %d\n", p, hdr->header_version);
234
235 if (hdr->header_version >= 1) {
236 printf("%srecovery dtbo size: %x\n", p,
237 hdr->recovery_dtbo_size);
238 printf("%srecovery dtbo offset: %llx\n", p,
239 hdr->recovery_dtbo_offset);
240 printf("%sheader size: %x\n", p,
241 hdr->header_size);
242 }
243
244 if (hdr->header_version >= 2) {
245 printf("%sdtb size: %x\n", p, hdr->dtb_size);
246 printf("%sdtb addr: %llx\n", p, hdr->dtb_addr);
247 }
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200248}
249#endif