blob: 6c9568a655d74f4e282a2ef29d6fad6d836ae426 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior686d6672014-05-05 15:08:09 -05002/*
3 * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Sebastian Siewior686d6672014-05-05 15:08:09 -05004 */
5
6#include <common.h>
7#include <image.h>
8#include <android_image.h>
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +03009#include <malloc.h>
10#include <errno.h>
Eugeniu Rosca1403f392019-04-08 17:35:27 +020011#include <asm/unaligned.h>
Sebastian Siewior686d6672014-05-05 15:08:09 -050012
Maxime Ripard17ef1f52015-04-24 12:53:12 +020013#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
14
Sebastian Siewior686d6672014-05-05 15:08:09 -050015static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
16
Maxime Ripard17ef1f52015-04-24 12:53:12 +020017static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
18{
19 /*
20 * All the Android tools that generate a boot.img use this
21 * address as the default.
22 *
23 * Even though it doesn't really make a lot of sense, and it
24 * might be valid on some platforms, we treat that adress as
25 * the default value for this field, and try to execute the
26 * kernel in place in such a case.
27 *
28 * Otherwise, we will return the actual value set by the user.
29 */
30 if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
31 return (ulong)hdr + hdr->page_size;
32
33 return hdr->kernel_addr;
34}
35
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030036/**
37 * android_image_get_kernel() - processes kernel part of Android boot images
38 * @hdr: Pointer to image header, which is at the start
39 * of the image.
40 * @verify: Checksum verification flag. Currently unimplemented.
41 * @os_data: Pointer to a ulong variable, will hold os data start
42 * address.
43 * @os_len: Pointer to a ulong variable, will hold os data length.
44 *
45 * This function returns the os image's start address and length. Also,
46 * it appends the kernel command line to the bootargs env variable.
47 *
48 * Return: Zero, os start address and length on success,
49 * otherwise on failure.
50 */
Sebastian Siewior686d6672014-05-05 15:08:09 -050051int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
52 ulong *os_data, ulong *os_len)
53{
Maxime Ripard17ef1f52015-04-24 12:53:12 +020054 u32 kernel_addr = android_image_get_kernel_addr(hdr);
Roman Stratiienko73268582019-06-03 15:38:13 +030055 const struct image_header *ihdr = (const struct image_header *)
56 ((uintptr_t)hdr + hdr->page_size);
Maxime Ripard17ef1f52015-04-24 12:53:12 +020057
Sebastian Siewior686d6672014-05-05 15:08:09 -050058 /*
59 * Not all Android tools use the id field for signing the image with
60 * sha1 (or anything) so we don't check it. It is not obvious that the
61 * string is null terminated so we take care of this.
62 */
63 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
64 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
65 if (strlen(andr_tmp_str))
66 printf("Android's image name: %s\n", andr_tmp_str);
67
68 printf("Kernel load addr 0x%08x size %u KiB\n",
Maxime Ripard17ef1f52015-04-24 12:53:12 +020069 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030070
71 int len = 0;
72 if (*hdr->cmdline) {
73 printf("Kernel command line: %s\n", hdr->cmdline);
74 len += strlen(hdr->cmdline);
Sebastian Siewior686d6672014-05-05 15:08:09 -050075 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030076
Simon Glass64b723f2017-08-03 12:22:12 -060077 char *bootargs = env_get("bootargs");
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +030078 if (bootargs)
79 len += strlen(bootargs);
80
81 char *newbootargs = malloc(len + 2);
82 if (!newbootargs) {
83 puts("Error: malloc in android_image_get_kernel failed!\n");
84 return -ENOMEM;
85 }
86 *newbootargs = '\0';
87
88 if (bootargs) {
89 strcpy(newbootargs, bootargs);
90 strcat(newbootargs, " ");
91 }
92 if (*hdr->cmdline)
93 strcat(newbootargs, hdr->cmdline);
94
Simon Glass6a38e412017-08-03 12:22:09 -060095 env_set("bootargs", newbootargs);
Sebastian Siewior686d6672014-05-05 15:08:09 -050096
97 if (os_data) {
Roman Stratiienko73268582019-06-03 15:38:13 +030098 if (image_get_magic(ihdr) == IH_MAGIC) {
99 *os_data = image_get_data(ihdr);
100 } else {
101 *os_data = (ulong)hdr;
102 *os_data += hdr->page_size;
103 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500104 }
Roman Stratiienko73268582019-06-03 15:38:13 +0300105 if (os_len) {
106 if (image_get_magic(ihdr) == IH_MAGIC)
107 *os_len = image_get_data_size(ihdr);
108 else
109 *os_len = hdr->kernel_size;
110 }
Sebastian Siewior686d6672014-05-05 15:08:09 -0500111 return 0;
112}
113
114int android_image_check_header(const struct andr_img_hdr *hdr)
115{
116 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
117}
118
119ulong android_image_get_end(const struct andr_img_hdr *hdr)
120{
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300121 ulong end;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500122 /*
123 * The header takes a full page, the remaining components are aligned
124 * on page boundary
125 */
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300126 end = (ulong)hdr;
127 end += hdr->page_size;
128 end += ALIGN(hdr->kernel_size, hdr->page_size);
129 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
130 end += ALIGN(hdr->second_size, hdr->page_size);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500131
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300132 return end;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500133}
134
135ulong android_image_get_kload(const struct andr_img_hdr *hdr)
136{
Maxime Ripard17ef1f52015-04-24 12:53:12 +0200137 return android_image_get_kernel_addr(hdr);
Sebastian Siewior686d6672014-05-05 15:08:09 -0500138}
139
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200140ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
141{
142 const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
143
Roman Stratiienko73268582019-06-03 15:38:13 +0300144 if (image_get_magic((image_header_t *)p) == IH_MAGIC)
145 return image_get_comp((image_header_t *)p);
146 else if (get_unaligned_le32(p) == LZ4F_MAGIC)
Eugeniu Rosca1403f392019-04-08 17:35:27 +0200147 return IH_COMP_LZ4;
148 else
149 return IH_COMP_NONE;
150}
151
Sebastian Siewior686d6672014-05-05 15:08:09 -0500152int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
153 ulong *rd_data, ulong *rd_len)
154{
Rob Herringd9798782015-10-05 14:37:07 -0500155 if (!hdr->ramdisk_size) {
156 *rd_data = *rd_len = 0;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500157 return -1;
Rob Herringd9798782015-10-05 14:37:07 -0500158 }
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +0300159
160 printf("RAM disk load addr 0x%08x size %u KiB\n",
161 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
162
Sebastian Siewior686d6672014-05-05 15:08:09 -0500163 *rd_data = (unsigned long)hdr;
164 *rd_data += hdr->page_size;
165 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
166
167 *rd_len = hdr->ramdisk_size;
168 return 0;
169}
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200170
Bin Chen909f1402018-01-27 16:59:08 +1100171int android_image_get_second(const struct andr_img_hdr *hdr,
172 ulong *second_data, ulong *second_len)
173{
174 if (!hdr->second_size) {
175 *second_data = *second_len = 0;
176 return -1;
177 }
178
179 *second_data = (unsigned long)hdr;
180 *second_data += hdr->page_size;
181 *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
182 *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
183
184 printf("second address is 0x%lx\n",*second_data);
185
186 *second_len = hdr->second_size;
187 return 0;
188}
189
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200190#if !defined(CONFIG_SPL_BUILD)
191/**
192 * android_print_contents - prints out the contents of the Android format image
193 * @hdr: pointer to the Android format image header
194 *
195 * android_print_contents() formats a multi line Android image contents
196 * description.
197 * The routine prints out Android image properties
198 *
199 * returns:
200 * no returned results
201 */
202void android_print_contents(const struct andr_img_hdr *hdr)
203{
204 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo41f513a2017-04-02 01:49:47 -0700205 /* os_version = ver << 11 | lvl */
206 u32 os_ver = hdr->os_version >> 11;
207 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200208
209 printf("%skernel size: %x\n", p, hdr->kernel_size);
210 printf("%skernel address: %x\n", p, hdr->kernel_addr);
211 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
Eugeniu Rosca792631c2019-04-08 17:35:28 +0200212 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200213 printf("%ssecond size: %x\n", p, hdr->second_size);
214 printf("%ssecond address: %x\n", p, hdr->second_addr);
215 printf("%stags address: %x\n", p, hdr->tags_addr);
216 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo41f513a2017-04-02 01:49:47 -0700217 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
218 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
219 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
220 p, hdr->os_version,
221 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
222 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Michael Trimarchi44af20b2016-06-10 19:54:37 +0200223 printf("%sname: %s\n", p, hdr->name);
224 printf("%scmdline: %s\n", p, hdr->cmdline);
225}
226#endif