1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2020
4  * Sam Protsenko <joe.skb7@gmail.com>
5  */
6 
7 #include <android_image.h>
8 #include <common.h>
9 #include <command.h>
10 #include <image.h>
11 #include <mapmem.h>
12 
13 #define abootimg_addr() \
14 	(_abootimg_addr == -1 ? image_load_addr : _abootimg_addr)
15 
16 /* Please use abootimg_addr() macro to obtain the boot image address */
17 static ulong _abootimg_addr = -1;
18 
abootimg_get_ver(int argc,char * const argv[])19 static int abootimg_get_ver(int argc, char *const argv[])
20 {
21 	const struct andr_img_hdr *hdr;
22 	int res = CMD_RET_SUCCESS;
23 
24 	if (argc > 1)
25 		return CMD_RET_USAGE;
26 
27 	hdr = map_sysmem(abootimg_addr(), sizeof(*hdr));
28 	if (android_image_check_header(hdr)) {
29 		printf("Error: Boot Image header is incorrect\n");
30 		res = CMD_RET_FAILURE;
31 		goto exit;
32 	}
33 
34 	if (argc == 0)
35 		printf("%u\n", hdr->header_version);
36 	else
37 		env_set_ulong(argv[0], hdr->header_version);
38 
39 exit:
40 	unmap_sysmem(hdr);
41 	return res;
42 }
43 
abootimg_get_recovery_dtbo(int argc,char * const argv[])44 static int abootimg_get_recovery_dtbo(int argc, char *const argv[])
45 {
46 	ulong addr;
47 	u32 size;
48 
49 	if (argc > 2)
50 		return CMD_RET_USAGE;
51 
52 	if (!android_image_get_dtbo(abootimg_addr(), &addr, &size))
53 		return CMD_RET_FAILURE;
54 
55 	if (argc == 0) {
56 		printf("%lx\n", addr);
57 	} else {
58 		env_set_hex(argv[0], addr);
59 		if (argc == 2)
60 			env_set_hex(argv[1], size);
61 	}
62 
63 	return CMD_RET_SUCCESS;
64 }
65 
abootimg_get_dtb_load_addr(int argc,char * const argv[])66 static int abootimg_get_dtb_load_addr(int argc, char *const argv[])
67 {
68 	const struct andr_img_hdr *hdr;
69 	int res = CMD_RET_SUCCESS;
70 
71 	if (argc > 1)
72 		return CMD_RET_USAGE;
73 
74 	hdr = map_sysmem(abootimg_addr(), sizeof(*hdr));
75 	if (android_image_check_header(hdr)) {
76 		printf("Error: Boot Image header is incorrect\n");
77 		res = CMD_RET_FAILURE;
78 		goto exit;
79 	}
80 
81 	if (hdr->header_version < 2) {
82 		printf("Error: header_version must be >= 2 for this\n");
83 		res = CMD_RET_FAILURE;
84 		goto exit;
85 	}
86 
87 	if (argc == 0)
88 		printf("%lx\n", (ulong)hdr->dtb_addr);
89 	else
90 		env_set_hex(argv[0], (ulong)hdr->dtb_addr);
91 
92 exit:
93 	unmap_sysmem(hdr);
94 	return res;
95 }
96 
abootimg_get_dtb_by_index(int argc,char * const argv[])97 static int abootimg_get_dtb_by_index(int argc, char *const argv[])
98 {
99 	const char *index_str;
100 	u32 num;
101 	char *endp;
102 	ulong addr;
103 	u32 size;
104 
105 	if (argc < 1 || argc > 3)
106 		return CMD_RET_USAGE;
107 
108 	index_str = argv[0] + strlen("--index=");
109 	if (index_str[0] == '\0') {
110 		printf("Error: Wrong index num\n");
111 		return CMD_RET_FAILURE;
112 	}
113 
114 	num = simple_strtoul(index_str, &endp, 0);
115 	if (*endp != '\0') {
116 		printf("Error: Wrong index num\n");
117 		return CMD_RET_FAILURE;
118 	}
119 
120 	if (!android_image_get_dtb_by_index(abootimg_addr(), num,
121 					    &addr, &size)) {
122 		return CMD_RET_FAILURE;
123 	}
124 
125 	if (argc == 1) {
126 		printf("%lx\n", addr);
127 	} else {
128 		if (env_set_hex(argv[1], addr)) {
129 			printf("Error: Can't set [addr_var]\n");
130 			return CMD_RET_FAILURE;
131 		}
132 
133 		if (argc == 3) {
134 			if (env_set_hex(argv[2], size)) {
135 				printf("Error: Can't set [size_var]\n");
136 				return CMD_RET_FAILURE;
137 			}
138 		}
139 	}
140 
141 	return CMD_RET_SUCCESS;
142 }
143 
abootimg_get_dtb(int argc,char * const argv[])144 static int abootimg_get_dtb(int argc, char *const argv[])
145 {
146 	if (argc < 1)
147 		return CMD_RET_USAGE;
148 
149 	if (strstr(argv[0], "--index="))
150 		return abootimg_get_dtb_by_index(argc, argv);
151 
152 	return CMD_RET_USAGE;
153 }
154 
do_abootimg_addr(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])155 static int do_abootimg_addr(struct cmd_tbl *cmdtp, int flag, int argc,
156 			    char *const argv[])
157 {
158 	char *endp;
159 	ulong img_addr;
160 
161 	if (argc != 2)
162 		return CMD_RET_USAGE;
163 
164 	img_addr = simple_strtoul(argv[1], &endp, 16);
165 	if (*endp != '\0') {
166 		printf("Error: Wrong image address\n");
167 		return CMD_RET_FAILURE;
168 	}
169 
170 	_abootimg_addr = img_addr;
171 	return CMD_RET_SUCCESS;
172 }
173 
do_abootimg_get(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])174 static int do_abootimg_get(struct cmd_tbl *cmdtp, int flag, int argc,
175 			   char *const argv[])
176 {
177 	const char *param;
178 
179 	if (argc < 2)
180 		return CMD_RET_USAGE;
181 
182 	param = argv[1];
183 	argc -= 2;
184 	argv += 2;
185 	if (!strcmp(param, "ver"))
186 		return abootimg_get_ver(argc, argv);
187 	else if (!strcmp(param, "recovery_dtbo"))
188 		return abootimg_get_recovery_dtbo(argc, argv);
189 	else if (!strcmp(param, "dtb_load_addr"))
190 		return abootimg_get_dtb_load_addr(argc, argv);
191 	else if (!strcmp(param, "dtb"))
192 		return abootimg_get_dtb(argc, argv);
193 
194 	return CMD_RET_USAGE;
195 }
196 
do_abootimg_dump(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])197 static int do_abootimg_dump(struct cmd_tbl *cmdtp, int flag, int argc,
198 			    char *const argv[])
199 {
200 	if (argc != 2)
201 		return CMD_RET_USAGE;
202 
203 	if (!strcmp(argv[1], "dtb")) {
204 		if (android_image_print_dtb_contents(abootimg_addr()))
205 			return CMD_RET_FAILURE;
206 	} else {
207 		return CMD_RET_USAGE;
208 	}
209 
210 	return CMD_RET_SUCCESS;
211 }
212 
213 static struct cmd_tbl cmd_abootimg_sub[] = {
214 	U_BOOT_CMD_MKENT(addr, 2, 1, do_abootimg_addr, "", ""),
215 	U_BOOT_CMD_MKENT(dump, 2, 1, do_abootimg_dump, "", ""),
216 	U_BOOT_CMD_MKENT(get, 5, 1, do_abootimg_get, "", ""),
217 };
218 
do_abootimg(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])219 static int do_abootimg(struct cmd_tbl *cmdtp, int flag, int argc,
220 		       char *const argv[])
221 {
222 	struct cmd_tbl *cp;
223 
224 	cp = find_cmd_tbl(argv[1], cmd_abootimg_sub,
225 			  ARRAY_SIZE(cmd_abootimg_sub));
226 
227 	/* Strip off leading 'abootimg' command argument */
228 	argc--;
229 	argv++;
230 
231 	if (!cp || argc > cp->maxargs)
232 		return CMD_RET_USAGE;
233 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
234 		return CMD_RET_SUCCESS;
235 
236 	return cp->cmd(cmdtp, flag, argc, argv);
237 }
238 
239 U_BOOT_CMD(
240 	abootimg, CONFIG_SYS_MAXARGS, 0, do_abootimg,
241 	"manipulate Android Boot Image",
242 	"addr <addr>\n"
243 	"    - set the address in RAM where boot image is located\n"
244 	"      ($loadaddr is used by default)\n"
245 	"abootimg dump dtb\n"
246 	"    - print info for all DT blobs in DTB area\n"
247 	"abootimg get ver [varname]\n"
248 	"    - get header version\n"
249 	"abootimg get recovery_dtbo [addr_var [size_var]]\n"
250 	"    - get address and size (hex) of recovery DTBO area in the image\n"
251 	"      [addr_var]: variable name to contain DTBO area address\n"
252 	"      [size_var]: variable name to contain DTBO area size\n"
253 	"abootimg get dtb_load_addr [varname]\n"
254 	"    - get load address (hex) of DTB, from image header\n"
255 	"abootimg get dtb --index=<num> [addr_var [size_var]]\n"
256 	"    - get address and size (hex) of DT blob in the image by index\n"
257 	"      <num>: index number of desired DT blob in DTB area\n"
258 	"      [addr_var]: variable name to contain DT blob address\n"
259 	"      [size_var]: variable name to contain DT blob size"
260 );
261