xref: /rk3399_rockchip-uboot/common/image-android.c (revision 65413a00f94435b88ee3f5b4afd5619f74e5a2a5)
1 /*
2  * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <image.h>
9 #include <android_image.h>
10 #include <malloc.h>
11 #include <mapmem.h>
12 #include <errno.h>
13 #ifdef CONFIG_RKIMG_BOOTLOADER
14 #include <asm/arch/resource_img.h>
15 #endif
16 
17 #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR	0x10008000
18 #define ANDROID_ARG_FDT_FILENAME "rk-kernel.dtb"
19 
20 static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
21 static u32 android_kernel_comp_type = IH_COMP_NONE;
22 
23 static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
24 {
25 	/*
26 	 * All the Android tools that generate a boot.img use this
27 	 * address as the default.
28 	 *
29 	 * Even though it doesn't really make a lot of sense, and it
30 	 * might be valid on some platforms, we treat that adress as
31 	 * the default value for this field, and try to execute the
32 	 * kernel in place in such a case.
33 	 *
34 	 * Otherwise, we will return the actual value set by the user.
35 	 */
36 	if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
37 		return (ulong)hdr + hdr->page_size;
38 
39 	return hdr->kernel_addr;
40 }
41 
42 void android_image_set_comp(struct andr_img_hdr *hdr, u32 comp)
43 {
44 	android_kernel_comp_type = comp;
45 }
46 
47 u32 android_image_get_comp(const struct andr_img_hdr *hdr)
48 {
49 	return android_kernel_comp_type;
50 }
51 
52 int android_image_parse_kernel_comp(const struct andr_img_hdr *hdr)
53 {
54 	ulong kaddr = android_image_get_kernel_addr(hdr);
55 	return bootm_parse_comp((const unsigned char *)kaddr);
56 }
57 
58 /**
59  * android_image_get_kernel() - processes kernel part of Android boot images
60  * @hdr:	Pointer to image header, which is at the start
61  *			of the image.
62  * @verify:	Checksum verification flag. Currently unimplemented.
63  * @os_data:	Pointer to a ulong variable, will hold os data start
64  *			address.
65  * @os_len:	Pointer to a ulong variable, will hold os data length.
66  *
67  * This function returns the os image's start address and length. Also,
68  * it appends the kernel command line to the bootargs env variable.
69  *
70  * Return: Zero, os start address and length on success,
71  *		otherwise on failure.
72  */
73 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
74 			     ulong *os_data, ulong *os_len)
75 {
76 	u32 kernel_addr = android_image_get_kernel_addr(hdr);
77 
78 	/*
79 	 * Not all Android tools use the id field for signing the image with
80 	 * sha1 (or anything) so we don't check it. It is not obvious that the
81 	 * string is null terminated so we take care of this.
82 	 */
83 	strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
84 	andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
85 	if (strlen(andr_tmp_str))
86 		printf("Android's image name: %s\n", andr_tmp_str);
87 
88 	printf("Kernel load addr 0x%08x size %u KiB\n",
89 	       kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
90 
91 	int len = 0;
92 	if (*hdr->cmdline) {
93 		printf("Kernel command line: %s\n", hdr->cmdline);
94 		len += strlen(hdr->cmdline);
95 	}
96 
97 	char *bootargs = env_get("bootargs");
98 	if (bootargs)
99 		len += strlen(bootargs);
100 
101 	char *newbootargs = malloc(len + 2);
102 	if (!newbootargs) {
103 		puts("Error: malloc in android_image_get_kernel failed!\n");
104 		return -ENOMEM;
105 	}
106 	*newbootargs = '\0';
107 
108 	if (bootargs) {
109 		strcpy(newbootargs, bootargs);
110 		strcat(newbootargs, " ");
111 	}
112 	if (*hdr->cmdline)
113 		strcat(newbootargs, hdr->cmdline);
114 
115 	env_set("bootargs", newbootargs);
116 
117 	if (os_data) {
118 		*os_data = (ulong)hdr;
119 		*os_data += hdr->page_size;
120 	}
121 	if (os_len)
122 		*os_len = hdr->kernel_size;
123 	return 0;
124 }
125 
126 int android_image_check_header(const struct andr_img_hdr *hdr)
127 {
128 	return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
129 }
130 
131 ulong android_image_get_end(const struct andr_img_hdr *hdr)
132 {
133 	ulong end;
134 	/*
135 	 * The header takes a full page, the remaining components are aligned
136 	 * on page boundary
137 	 */
138 	end = (ulong)hdr;
139 	end += hdr->page_size;
140 	end += ALIGN(hdr->kernel_size, hdr->page_size);
141 	end += ALIGN(hdr->ramdisk_size, hdr->page_size);
142 	end += ALIGN(hdr->second_size, hdr->page_size);
143 
144 	return end;
145 }
146 
147 u32 android_image_get_ksize(const struct andr_img_hdr *hdr)
148 {
149 	return hdr->kernel_size;
150 }
151 
152 void android_image_set_kload(struct andr_img_hdr *hdr, u32 load_address)
153 {
154 	hdr->kernel_addr = load_address;
155 }
156 
157 ulong android_image_get_kload(const struct andr_img_hdr *hdr)
158 {
159 	return android_image_get_kernel_addr(hdr);
160 }
161 
162 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
163 			      ulong *rd_data, ulong *rd_len)
164 {
165 	if (!hdr->ramdisk_size) {
166 		*rd_data = *rd_len = 0;
167 		return -1;
168 	}
169 
170 	printf("RAM disk load addr 0x%08x size %u KiB\n",
171 	       hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
172 
173 	*rd_data = (unsigned long)hdr;
174 	*rd_data += hdr->page_size;
175 	*rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
176 
177 	*rd_len = hdr->ramdisk_size;
178 	return 0;
179 }
180 
181 int android_image_get_fdt(const struct andr_img_hdr *hdr,
182 			      ulong *rd_data)
183 {
184 	if (!hdr->second_size) {
185 		*rd_data = 0;
186 		return -1;
187 	}
188 
189 	printf("FDT load addr 0x%08x size %u KiB\n",
190 	       hdr->second_addr, DIV_ROUND_UP(hdr->second_size, 1024));
191 
192 	*rd_data = (unsigned long)hdr;
193 	*rd_data += hdr->page_size;
194 	*rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
195 	*rd_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
196 #ifdef CONFIG_RKIMG_BOOTLOADER
197 	*rd_data += (rockchip_get_resource_file((void *)*rd_data,
198 		     ANDROID_ARG_FDT_FILENAME))
199 			* 512;
200 #endif
201 	return 0;
202 }
203 
204 long android_image_load(struct blk_desc *dev_desc,
205 			const disk_partition_t *part_info,
206 			unsigned long load_address,
207 			unsigned long max_size) {
208 	void *buf;
209 	long blk_cnt = 0;
210 	long blk_read = 0;
211 	u32 comp;
212 	u32 kload_addr;
213 
214 	if (max_size < part_info->blksz)
215 		return -1;
216 
217 	/* We don't know the size of the Android image before reading the header
218 	 * so we don't limit the size of the mapped memory.
219 	 */
220 	buf = map_sysmem(load_address, 0 /* size */);
221 
222 	/* Read the Android boot.img header and a few parts of
223 	 * the head of kernel image.
224 	 */
225 	if (blk_dread(dev_desc, part_info->start, 8, buf) != 8)
226 		blk_read = -1;
227 
228 	if (!blk_read && android_image_check_header(buf) != 0) {
229 		printf("** Invalid Android Image header **\n");
230 		blk_read = -1;
231 	}
232 
233 
234 	if (!blk_read) {
235 		blk_cnt = (android_image_get_end(buf) - (ulong)buf +
236 			   part_info->blksz - 1) / part_info->blksz;
237 		comp = android_image_parse_kernel_comp(buf);
238 		/*
239 		 * We should load a compressed kernel Image
240 		 * to high memory
241 		 */
242 		if (comp != IH_COMP_NONE) {
243 			load_address += android_image_get_ksize(buf) * 3;
244 			load_address = env_get_ulong("kernel_addr_c", 16, load_address);
245 			unmap_sysmem(buf);
246 			buf = map_sysmem(load_address, 0 /* size */);
247 		}
248 
249 		if (blk_cnt * part_info->blksz > max_size) {
250 			debug("Android Image too big (%lu bytes, max %lu)\n",
251 			      android_image_get_end(buf) - (ulong)buf,
252 			      max_size);
253 			blk_read = -1;
254 		} else {
255 			debug("Loading Android Image (%lu blocks) to 0x%lx... ",
256 			      blk_cnt, load_address);
257 			blk_read = blk_dread(dev_desc, part_info->start,
258 					     blk_cnt, buf);
259 		}
260 
261 		/*
262 		 * zImage is not need to decompress
263 		 * kernel will handle decompress itself
264 		 */
265 		if (comp != IH_COMP_NONE && comp != IH_COMP_ZIMAGE) {
266 			kload_addr = env_get_ulong("kernel_addr_r", 16, 0x02080000);
267 			android_image_set_kload(buf, kload_addr);
268 			android_image_set_comp(buf, comp);
269 		} else {
270 			android_image_set_comp(buf, IH_COMP_NONE);
271 		}
272 
273 	}
274 
275 	unmap_sysmem(buf);
276 
277 	debug("%lu blocks read: %s\n",
278 	      blk_read, (blk_read == blk_cnt) ? "OK" : "ERROR");
279 	if (blk_read != blk_cnt)
280 		return -1;
281 
282 	return load_address;
283 }
284 
285 #if !defined(CONFIG_SPL_BUILD)
286 /**
287  * android_print_contents - prints out the contents of the Android format image
288  * @hdr: pointer to the Android format image header
289  *
290  * android_print_contents() formats a multi line Android image contents
291  * description.
292  * The routine prints out Android image properties
293  *
294  * returns:
295  *     no returned results
296  */
297 void android_print_contents(const struct andr_img_hdr *hdr)
298 {
299 	const char * const p = IMAGE_INDENT_STRING;
300 	/* os_version = ver << 11 | lvl */
301 	u32 os_ver = hdr->os_version >> 11;
302 	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
303 
304 	printf("%skernel size:      %x\n", p, hdr->kernel_size);
305 	printf("%skernel address:   %x\n", p, hdr->kernel_addr);
306 	printf("%sramdisk size:     %x\n", p, hdr->ramdisk_size);
307 	printf("%sramdisk addrress: %x\n", p, hdr->ramdisk_addr);
308 	printf("%ssecond size:      %x\n", p, hdr->second_size);
309 	printf("%ssecond address:   %x\n", p, hdr->second_addr);
310 	printf("%stags address:     %x\n", p, hdr->tags_addr);
311 	printf("%spage size:        %x\n", p, hdr->page_size);
312 	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
313 	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
314 	printf("%sos_version:       %x (ver: %u.%u.%u, level: %u.%u)\n",
315 	       p, hdr->os_version,
316 	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
317 	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
318 	printf("%sname:             %s\n", p, hdr->name);
319 	printf("%scmdline:          %s\n", p, hdr->cmdline);
320 }
321 #endif
322