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