blob: 8b0f6b3b8babe5b50578324e1dd023ad46a1185d [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>
7#include <image.h>
8#include <android_image.h>
Ahmad Draidi86f46952014-10-23 20:50:07 +03009#include <malloc.h>
10#include <errno.h>
Eugeniu Rosca829ceb22019-04-08 17:35:27 +020011#include <asm/unaligned.h>
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050012
Maxime Ripard87f02d52015-04-24 12:53:12 +020013#define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR 0x10008000
14
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050015static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
16
Maxime Ripard87f02d52015-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 Draidi86f46952014-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 Siewior9ace3fc2014-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 Ripard87f02d52015-04-24 12:53:12 +020054 u32 kernel_addr = android_image_get_kernel_addr(hdr);
55
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050056 /*
57 * Not all Android tools use the id field for signing the image with
58 * sha1 (or anything) so we don't check it. It is not obvious that the
59 * string is null terminated so we take care of this.
60 */
61 strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
62 andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
63 if (strlen(andr_tmp_str))
64 printf("Android's image name: %s\n", andr_tmp_str);
65
66 printf("Kernel load addr 0x%08x size %u KiB\n",
Maxime Ripard87f02d52015-04-24 12:53:12 +020067 kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
Ahmad Draidi86f46952014-10-23 20:50:07 +030068
69 int len = 0;
70 if (*hdr->cmdline) {
71 printf("Kernel command line: %s\n", hdr->cmdline);
72 len += strlen(hdr->cmdline);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050073 }
Ahmad Draidi86f46952014-10-23 20:50:07 +030074
Simon Glass00caae62017-08-03 12:22:12 -060075 char *bootargs = env_get("bootargs");
Ahmad Draidi86f46952014-10-23 20:50:07 +030076 if (bootargs)
77 len += strlen(bootargs);
78
79 char *newbootargs = malloc(len + 2);
80 if (!newbootargs) {
81 puts("Error: malloc in android_image_get_kernel failed!\n");
82 return -ENOMEM;
83 }
84 *newbootargs = '\0';
85
86 if (bootargs) {
87 strcpy(newbootargs, bootargs);
88 strcat(newbootargs, " ");
89 }
90 if (*hdr->cmdline)
91 strcat(newbootargs, hdr->cmdline);
92
Simon Glass382bee52017-08-03 12:22:09 -060093 env_set("bootargs", newbootargs);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -050094
95 if (os_data) {
96 *os_data = (ulong)hdr;
97 *os_data += hdr->page_size;
98 }
99 if (os_len)
100 *os_len = hdr->kernel_size;
101 return 0;
102}
103
104int android_image_check_header(const struct andr_img_hdr *hdr)
105{
106 return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
107}
108
109ulong android_image_get_end(const struct andr_img_hdr *hdr)
110{
Ahmad Draidi86f46952014-10-23 20:50:07 +0300111 ulong end;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500112 /*
113 * The header takes a full page, the remaining components are aligned
114 * on page boundary
115 */
Ahmad Draidi86f46952014-10-23 20:50:07 +0300116 end = (ulong)hdr;
117 end += hdr->page_size;
118 end += ALIGN(hdr->kernel_size, hdr->page_size);
119 end += ALIGN(hdr->ramdisk_size, hdr->page_size);
120 end += ALIGN(hdr->second_size, hdr->page_size);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500121
Ahmad Draidi86f46952014-10-23 20:50:07 +0300122 return end;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500123}
124
125ulong android_image_get_kload(const struct andr_img_hdr *hdr)
126{
Maxime Ripard87f02d52015-04-24 12:53:12 +0200127 return android_image_get_kernel_addr(hdr);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500128}
129
Eugeniu Rosca829ceb22019-04-08 17:35:27 +0200130ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
131{
132 const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
133
134 if (get_unaligned_le32(p) == LZ4F_MAGIC)
135 return IH_COMP_LZ4;
136 else
137 return IH_COMP_NONE;
138}
139
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500140int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
141 ulong *rd_data, ulong *rd_len)
142{
Rob Herring99500982015-10-05 14:37:07 -0500143 if (!hdr->ramdisk_size) {
144 *rd_data = *rd_len = 0;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500145 return -1;
Rob Herring99500982015-10-05 14:37:07 -0500146 }
Ahmad Draidi86f46952014-10-23 20:50:07 +0300147
148 printf("RAM disk load addr 0x%08x size %u KiB\n",
149 hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
150
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500151 *rd_data = (unsigned long)hdr;
152 *rd_data += hdr->page_size;
153 *rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
154
155 *rd_len = hdr->ramdisk_size;
156 return 0;
157}
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200158
Bin Chen10481612018-01-27 16:59:08 +1100159int android_image_get_second(const struct andr_img_hdr *hdr,
160 ulong *second_data, ulong *second_len)
161{
162 if (!hdr->second_size) {
163 *second_data = *second_len = 0;
164 return -1;
165 }
166
167 *second_data = (unsigned long)hdr;
168 *second_data += hdr->page_size;
169 *second_data += ALIGN(hdr->kernel_size, hdr->page_size);
170 *second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
171
172 printf("second address is 0x%lx\n",*second_data);
173
174 *second_len = hdr->second_size;
175 return 0;
176}
177
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200178#if !defined(CONFIG_SPL_BUILD)
179/**
180 * android_print_contents - prints out the contents of the Android format image
181 * @hdr: pointer to the Android format image header
182 *
183 * android_print_contents() formats a multi line Android image contents
184 * description.
185 * The routine prints out Android image properties
186 *
187 * returns:
188 * no returned results
189 */
190void android_print_contents(const struct andr_img_hdr *hdr)
191{
192 const char * const p = IMAGE_INDENT_STRING;
Alex Deymo210a7172017-04-02 01:49:47 -0700193 /* os_version = ver << 11 | lvl */
194 u32 os_ver = hdr->os_version >> 11;
195 u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200196
197 printf("%skernel size: %x\n", p, hdr->kernel_size);
198 printf("%skernel address: %x\n", p, hdr->kernel_addr);
199 printf("%sramdisk size: %x\n", p, hdr->ramdisk_size);
Eugeniu Rosca74a7e002019-04-08 17:35:28 +0200200 printf("%sramdisk address: %x\n", p, hdr->ramdisk_addr);
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200201 printf("%ssecond size: %x\n", p, hdr->second_size);
202 printf("%ssecond address: %x\n", p, hdr->second_addr);
203 printf("%stags address: %x\n", p, hdr->tags_addr);
204 printf("%spage size: %x\n", p, hdr->page_size);
Alex Deymo210a7172017-04-02 01:49:47 -0700205 /* ver = A << 14 | B << 7 | C (7 bits for each of A, B, C)
206 * lvl = ((Y - 2000) & 127) << 4 | M (7 bits for Y, 4 bits for M) */
207 printf("%sos_version: %x (ver: %u.%u.%u, level: %u.%u)\n",
208 p, hdr->os_version,
209 (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
210 (os_lvl >> 4) + 2000, os_lvl & 0x0F);
Michael Trimarchi4f1318b2016-06-10 19:54:37 +0200211 printf("%sname: %s\n", p, hdr->name);
212 printf("%scmdline: %s\n", p, hdr->cmdline);
213}
214#endif