Blame view

common/image-android.c 7.13 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
9ace3fc81   Sebastian Siewior   image: add suppor...
2
3
  /*
   * Copyright (c) 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9ace3fc81   Sebastian Siewior   image: add suppor...
4
5
6
   */
  
  #include <common.h>
9fb625ce0   Simon Glass   env: Move env_set...
7
  #include <env.h>
9ace3fc81   Sebastian Siewior   image: add suppor...
8
9
  #include <image.h>
  #include <android_image.h>
86f4695bd   Ahmad Draidi   image: Fix Androi...
10
11
  #include <malloc.h>
  #include <errno.h>
829ceb282   Eugeniu Rosca   image: android: a...
12
  #include <asm/unaligned.h>
9ace3fc81   Sebastian Siewior   image: add suppor...
13

87f02d5ae   Maxime Ripard   image: android: h...
14
  #define ANDROID_IMAGE_DEFAULT_KERNEL_ADDR	0x10008000
9ace3fc81   Sebastian Siewior   image: add suppor...
15
  static char andr_tmp_str[ANDR_BOOT_ARGS_SIZE + 1];
87f02d5ae   Maxime Ripard   image: android: h...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  static ulong android_image_get_kernel_addr(const struct andr_img_hdr *hdr)
  {
  	/*
  	 * All the Android tools that generate a boot.img use this
  	 * address as the default.
  	 *
  	 * Even though it doesn't really make a lot of sense, and it
  	 * might be valid on some platforms, we treat that adress as
  	 * the default value for this field, and try to execute the
  	 * kernel in place in such a case.
  	 *
  	 * Otherwise, we will return the actual value set by the user.
  	 */
  	if (hdr->kernel_addr == ANDROID_IMAGE_DEFAULT_KERNEL_ADDR)
  		return (ulong)hdr + hdr->page_size;
  
  	return hdr->kernel_addr;
  }
86f4695bd   Ahmad Draidi   image: Fix Androi...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  /**
   * android_image_get_kernel() - processes kernel part of Android boot images
   * @hdr:	Pointer to image header, which is at the start
   *			of the image.
   * @verify:	Checksum verification flag. Currently unimplemented.
   * @os_data:	Pointer to a ulong variable, will hold os data start
   *			address.
   * @os_len:	Pointer to a ulong variable, will hold os data length.
   *
   * This function returns the os image's start address and length. Also,
   * it appends the kernel command line to the bootargs env variable.
   *
   * Return: Zero, os start address and length on success,
   *		otherwise on failure.
   */
9ace3fc81   Sebastian Siewior   image: add suppor...
49
50
51
  int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
  			     ulong *os_data, ulong *os_len)
  {
87f02d5ae   Maxime Ripard   image: android: h...
52
  	u32 kernel_addr = android_image_get_kernel_addr(hdr);
39f790b03   Roman Stratiienko   image: android: a...
53
54
  	const struct image_header *ihdr = (const struct image_header *)
  		((uintptr_t)hdr + hdr->page_size);
87f02d5ae   Maxime Ripard   image: android: h...
55

9ace3fc81   Sebastian Siewior   image: add suppor...
56
57
58
59
60
61
62
63
64
65
66
67
68
  	/*
  	 * Not all Android tools use the id field for signing the image with
  	 * sha1 (or anything) so we don't check it. It is not obvious that the
  	 * string is null terminated so we take care of this.
  	 */
  	strncpy(andr_tmp_str, hdr->name, ANDR_BOOT_NAME_SIZE);
  	andr_tmp_str[ANDR_BOOT_NAME_SIZE] = '\0';
  	if (strlen(andr_tmp_str))
  		printf("Android's image name: %s
  ", andr_tmp_str);
  
  	printf("Kernel load addr 0x%08x size %u KiB
  ",
87f02d5ae   Maxime Ripard   image: android: h...
69
  	       kernel_addr, DIV_ROUND_UP(hdr->kernel_size, 1024));
86f4695bd   Ahmad Draidi   image: Fix Androi...
70
71
72
73
74
75
76
  
  	int len = 0;
  	if (*hdr->cmdline) {
  		printf("Kernel command line: %s
  ", hdr->cmdline);
  		len += strlen(hdr->cmdline);
  	}
00caae6d4   Simon Glass   env: Rename geten...
77
  	char *bootargs = env_get("bootargs");
86f4695bd   Ahmad Draidi   image: Fix Androi...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  	if (bootargs)
  		len += strlen(bootargs);
  
  	char *newbootargs = malloc(len + 2);
  	if (!newbootargs) {
  		puts("Error: malloc in android_image_get_kernel failed!
  ");
  		return -ENOMEM;
  	}
  	*newbootargs = '\0';
  
  	if (bootargs) {
  		strcpy(newbootargs, bootargs);
  		strcat(newbootargs, " ");
9ace3fc81   Sebastian Siewior   image: add suppor...
92
  	}
86f4695bd   Ahmad Draidi   image: Fix Androi...
93
94
  	if (*hdr->cmdline)
  		strcat(newbootargs, hdr->cmdline);
382bee57f   Simon Glass   env: Rename seten...
95
  	env_set("bootargs", newbootargs);
9ace3fc81   Sebastian Siewior   image: add suppor...
96
97
  
  	if (os_data) {
39f790b03   Roman Stratiienko   image: android: a...
98
99
100
101
102
103
104
105
106
107
108
109
  		if (image_get_magic(ihdr) == IH_MAGIC) {
  			*os_data = image_get_data(ihdr);
  		} else {
  			*os_data = (ulong)hdr;
  			*os_data += hdr->page_size;
  		}
  	}
  	if (os_len) {
  		if (image_get_magic(ihdr) == IH_MAGIC)
  			*os_len = image_get_data_size(ihdr);
  		else
  			*os_len = hdr->kernel_size;
9ace3fc81   Sebastian Siewior   image: add suppor...
110
  	}
9ace3fc81   Sebastian Siewior   image: add suppor...
111
112
113
114
115
116
117
118
119
120
  	return 0;
  }
  
  int android_image_check_header(const struct andr_img_hdr *hdr)
  {
  	return memcmp(ANDR_BOOT_MAGIC, hdr->magic, ANDR_BOOT_MAGIC_SIZE);
  }
  
  ulong android_image_get_end(const struct andr_img_hdr *hdr)
  {
86f4695bd   Ahmad Draidi   image: Fix Androi...
121
  	ulong end;
bb63363f2   Sam Protsenko   image: android: S...
122

9ace3fc81   Sebastian Siewior   image: add suppor...
123
124
125
126
  	/*
  	 * The header takes a full page, the remaining components are aligned
  	 * on page boundary
  	 */
86f4695bd   Ahmad Draidi   image: Fix Androi...
127
128
129
130
131
  	end = (ulong)hdr;
  	end += hdr->page_size;
  	end += ALIGN(hdr->kernel_size, hdr->page_size);
  	end += ALIGN(hdr->ramdisk_size, hdr->page_size);
  	end += ALIGN(hdr->second_size, hdr->page_size);
9ace3fc81   Sebastian Siewior   image: add suppor...
132

bb63363f2   Sam Protsenko   image: android: S...
133
134
135
136
137
  	if (hdr->header_version >= 1)
  		end += ALIGN(hdr->recovery_dtbo_size, hdr->page_size);
  
  	if (hdr->header_version >= 2)
  		end += ALIGN(hdr->dtb_size, hdr->page_size);
86f4695bd   Ahmad Draidi   image: Fix Androi...
138
  	return end;
9ace3fc81   Sebastian Siewior   image: add suppor...
139
140
141
142
  }
  
  ulong android_image_get_kload(const struct andr_img_hdr *hdr)
  {
87f02d5ae   Maxime Ripard   image: android: h...
143
  	return android_image_get_kernel_addr(hdr);
9ace3fc81   Sebastian Siewior   image: add suppor...
144
  }
829ceb282   Eugeniu Rosca   image: android: a...
145
146
147
  ulong android_image_get_kcomp(const struct andr_img_hdr *hdr)
  {
  	const void *p = (void *)((uintptr_t)hdr + hdr->page_size);
39f790b03   Roman Stratiienko   image: android: a...
148
149
150
  	if (image_get_magic((image_header_t *)p) == IH_MAGIC)
  		return image_get_comp((image_header_t *)p);
  	else if (get_unaligned_le32(p) == LZ4F_MAGIC)
829ceb282   Eugeniu Rosca   image: android: a...
151
152
153
154
  		return IH_COMP_LZ4;
  	else
  		return IH_COMP_NONE;
  }
9ace3fc81   Sebastian Siewior   image: add suppor...
155
156
157
  int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
  			      ulong *rd_data, ulong *rd_len)
  {
9950098e3   Rob Herring   image: fix suppor...
158
159
  	if (!hdr->ramdisk_size) {
  		*rd_data = *rd_len = 0;
9ace3fc81   Sebastian Siewior   image: add suppor...
160
  		return -1;
9950098e3   Rob Herring   image: fix suppor...
161
  	}
86f4695bd   Ahmad Draidi   image: Fix Androi...
162
163
164
165
  
  	printf("RAM disk load addr 0x%08x size %u KiB
  ",
  	       hdr->ramdisk_addr, DIV_ROUND_UP(hdr->ramdisk_size, 1024));
9ace3fc81   Sebastian Siewior   image: add suppor...
166
167
168
169
170
171
172
  	*rd_data = (unsigned long)hdr;
  	*rd_data += hdr->page_size;
  	*rd_data += ALIGN(hdr->kernel_size, hdr->page_size);
  
  	*rd_len = hdr->ramdisk_size;
  	return 0;
  }
4f1318b29   Michael Trimarchi   common: image: mi...
173

104816142   Bin Chen   parse the second ...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  int android_image_get_second(const struct andr_img_hdr *hdr,
  			      ulong *second_data, ulong *second_len)
  {
  	if (!hdr->second_size) {
  		*second_data = *second_len = 0;
  		return -1;
  	}
  
  	*second_data = (unsigned long)hdr;
  	*second_data += hdr->page_size;
  	*second_data += ALIGN(hdr->kernel_size, hdr->page_size);
  	*second_data += ALIGN(hdr->ramdisk_size, hdr->page_size);
  
  	printf("second address is 0x%lx
  ",*second_data);
  
  	*second_len = hdr->second_size;
  	return 0;
  }
4f1318b29   Michael Trimarchi   common: image: mi...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  #if !defined(CONFIG_SPL_BUILD)
  /**
   * android_print_contents - prints out the contents of the Android format image
   * @hdr: pointer to the Android format image header
   *
   * android_print_contents() formats a multi line Android image contents
   * description.
   * The routine prints out Android image properties
   *
   * returns:
   *     no returned results
   */
  void android_print_contents(const struct andr_img_hdr *hdr)
  {
  	const char * const p = IMAGE_INDENT_STRING;
210a7176e   Alex Deymo   image: Update inc...
208
209
210
  	/* os_version = ver << 11 | lvl */
  	u32 os_ver = hdr->os_version >> 11;
  	u32 os_lvl = hdr->os_version & ((1U << 11) - 1);
4f1318b29   Michael Trimarchi   common: image: mi...
211

bb63363f2   Sam Protsenko   image: android: S...
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  	printf("%skernel size:          %x
  ", p, hdr->kernel_size);
  	printf("%skernel address:       %x
  ", p, hdr->kernel_addr);
  	printf("%sramdisk size:         %x
  ", p, hdr->ramdisk_size);
  	printf("%sramdisk address:      %x
  ", p, hdr->ramdisk_addr);
  	printf("%ssecond size:          %x
  ", p, hdr->second_size);
  	printf("%ssecond address:       %x
  ", p, hdr->second_addr);
  	printf("%stags address:         %x
  ", p, hdr->tags_addr);
  	printf("%spage size:            %x
  ", p, hdr->page_size);
210a7176e   Alex Deymo   image: Update inc...
228
229
  	/* ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
  	 * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M) */
bb63363f2   Sam Protsenko   image: android: S...
230
231
  	printf("%sos_version:           %x (ver: %u.%u.%u, level: %u.%u)
  ",
210a7176e   Alex Deymo   image: Update inc...
232
233
234
  	       p, hdr->os_version,
  	       (os_ver >> 7) & 0x7F, (os_ver >> 14) & 0x7F, os_ver & 0x7F,
  	       (os_lvl >> 4) + 2000, os_lvl & 0x0F);
bb63363f2   Sam Protsenko   image: android: S...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  	printf("%sname:                 %s
  ", p, hdr->name);
  	printf("%scmdline:              %s
  ", p, hdr->cmdline);
  	printf("%sheader_version:       %d
  ", p, hdr->header_version);
  
  	if (hdr->header_version >= 1) {
  		printf("%srecovery dtbo size:   %x
  ", p,
  		       hdr->recovery_dtbo_size);
  		printf("%srecovery dtbo offset: %llx
  ", p,
  		       hdr->recovery_dtbo_offset);
  		printf("%sheader size:          %x
  ", p,
  		       hdr->header_size);
  	}
  
  	if (hdr->header_version >= 2) {
  		printf("%sdtb size:             %x
  ", p, hdr->dtb_size);
  		printf("%sdtb addr:             %llx
  ", p, hdr->dtb_addr);
  	}
4f1318b29   Michael Trimarchi   common: image: mi...
260
261
  }
  #endif