1 // SPDX-License-Identifier: GPL-2.0+
4 * Sam Protsenko <joe.skb7@gmail.com>
7 #include <android_image.h>
12 #define abootimg_addr() \
13 (_abootimg_addr == -1 ? image_load_addr : _abootimg_addr)
15 /* Please use abootimg_addr() macro to obtain the boot image address */
16 static ulong _abootimg_addr
= -1;
17 static ulong _ainit_bootimg_addr
= -1;
18 static ulong _avendor_bootimg_addr
= -1;
20 ulong
get_abootimg_addr(void)
22 return (_abootimg_addr
== -1 ? image_load_addr
: _abootimg_addr
);
25 void set_abootimg_addr(ulong addr
)
27 _abootimg_addr
= addr
;
30 ulong
get_ainit_bootimg_addr(void)
32 return _ainit_bootimg_addr
;
35 ulong
get_avendor_bootimg_addr(void)
37 return _avendor_bootimg_addr
;
40 void set_avendor_bootimg_addr(ulong addr
)
42 _avendor_bootimg_addr
= addr
;
45 static int abootimg_get_ver(int argc
, char *const argv
[])
47 const struct andr_boot_img_hdr_v0
*hdr
;
48 int res
= CMD_RET_SUCCESS
;
53 hdr
= map_sysmem(abootimg_addr(), sizeof(*hdr
));
54 if (!is_android_boot_image_header(hdr
)) {
55 printf("Error: Boot Image header is incorrect\n");
56 res
= CMD_RET_FAILURE
;
61 printf("%u\n", hdr
->header_version
);
63 env_set_ulong(argv
[0], hdr
->header_version
);
70 static int abootimg_get_recovery_dtbo(int argc
, char *const argv
[])
78 if (!android_image_get_dtbo(abootimg_addr(), &addr
, &size
))
79 return CMD_RET_FAILURE
;
82 printf("%lx\n", addr
);
84 env_set_hex(argv
[0], addr
);
86 env_set_hex(argv
[1], size
);
89 return CMD_RET_SUCCESS
;
92 static int abootimg_get_dtb_load_addr(int argc
, char *const argv
[])
96 struct andr_image_data img_data
= {0};
97 const struct andr_boot_img_hdr_v0
*hdr
;
98 const struct andr_vnd_boot_img_hdr
*vhdr
;
100 hdr
= map_sysmem(abootimg_addr(), sizeof(*hdr
));
101 if (get_avendor_bootimg_addr() != -1)
102 vhdr
= map_sysmem(get_avendor_bootimg_addr(), sizeof(*vhdr
));
104 if (!android_image_get_data(hdr
, vhdr
, &img_data
)) {
105 if (get_avendor_bootimg_addr() != -1)
108 return CMD_RET_FAILURE
;
111 if (get_avendor_bootimg_addr() != -1)
115 if (img_data
.header_version
< 2) {
116 printf("Error: header_version must be >= 2 for this\n");
117 return CMD_RET_FAILURE
;
120 if (!img_data
.dtb_load_addr
) {
121 printf("Error: failed to read dtb_load_addr\n");
122 return CMD_RET_FAILURE
;
126 printf("%lx\n", (ulong
)img_data
.dtb_load_addr
);
128 env_set_hex(argv
[0], (ulong
)img_data
.dtb_load_addr
);
130 return CMD_RET_SUCCESS
;
133 static int abootimg_get_dtb_by_index(int argc
, char *const argv
[])
135 const char *index_str
;
141 if (argc
< 1 || argc
> 3)
142 return CMD_RET_USAGE
;
144 index_str
= argv
[0] + strlen("--index=");
145 if (index_str
[0] == '\0') {
146 printf("Error: Wrong index num\n");
147 return CMD_RET_FAILURE
;
150 num
= simple_strtoul(index_str
, &endp
, 0);
152 printf("Error: Wrong index num\n");
153 return CMD_RET_FAILURE
;
156 if (!android_image_get_dtb_by_index(abootimg_addr(),
157 get_avendor_bootimg_addr(), num
,
159 return CMD_RET_FAILURE
;
163 printf("%lx\n", addr
);
165 if (env_set_hex(argv
[1], addr
)) {
166 printf("Error: Can't set [addr_var]\n");
167 return CMD_RET_FAILURE
;
171 if (env_set_hex(argv
[2], size
)) {
172 printf("Error: Can't set [size_var]\n");
173 return CMD_RET_FAILURE
;
178 return CMD_RET_SUCCESS
;
181 static int abootimg_get_dtb(int argc
, char *const argv
[])
184 return CMD_RET_USAGE
;
186 if (strstr(argv
[0], "--index="))
187 return abootimg_get_dtb_by_index(argc
, argv
);
189 return CMD_RET_USAGE
;
192 static int do_abootimg_addr(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
198 if (argc
< 2 || argc
> 4)
199 return CMD_RET_USAGE
;
201 img_addr
= hextoul(argv
[1], &endp
);
203 printf("Error: Wrong image address\n");
204 return CMD_RET_FAILURE
;
207 _abootimg_addr
= img_addr
;
210 img_addr
= simple_strtoul(argv
[2], &endp
, 16);
212 printf("Error: Wrong vendor_boot image address\n");
213 return CMD_RET_FAILURE
;
216 _avendor_bootimg_addr
= img_addr
;
220 img_addr
= simple_strtoul(argv
[3], &endp
, 16);
222 printf("Error: Wrong init_boot image address\n");
223 return CMD_RET_FAILURE
;
226 _ainit_bootimg_addr
= img_addr
;
229 return CMD_RET_SUCCESS
;
232 static int do_abootimg_get(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
238 return CMD_RET_USAGE
;
243 if (!strcmp(param
, "ver"))
244 return abootimg_get_ver(argc
, argv
);
245 else if (!strcmp(param
, "recovery_dtbo"))
246 return abootimg_get_recovery_dtbo(argc
, argv
);
247 else if (!strcmp(param
, "dtb_load_addr"))
248 return abootimg_get_dtb_load_addr(argc
, argv
);
249 else if (!strcmp(param
, "dtb"))
250 return abootimg_get_dtb(argc
, argv
);
252 return CMD_RET_USAGE
;
255 static int do_abootimg_dump(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
259 return CMD_RET_USAGE
;
261 if (!strcmp(argv
[1], "dtb")) {
262 if (android_image_print_dtb_contents(abootimg_addr()))
263 return CMD_RET_FAILURE
;
265 return CMD_RET_USAGE
;
268 return CMD_RET_SUCCESS
;
271 static struct cmd_tbl cmd_abootimg_sub
[] = {
272 U_BOOT_CMD_MKENT(addr
, 4, 1, do_abootimg_addr
, "", ""),
273 U_BOOT_CMD_MKENT(dump
, 2, 1, do_abootimg_dump
, "", ""),
274 U_BOOT_CMD_MKENT(get
, 5, 1, do_abootimg_get
, "", ""),
277 static int do_abootimg(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
282 cp
= find_cmd_tbl(argv
[1], cmd_abootimg_sub
,
283 ARRAY_SIZE(cmd_abootimg_sub
));
285 /* Strip off leading 'abootimg' command argument */
289 if (!cp
|| argc
> cp
->maxargs
)
290 return CMD_RET_USAGE
;
291 if (flag
== CMD_FLAG_REPEAT
&& !cmd_is_repeatable(cp
))
292 return CMD_RET_SUCCESS
;
294 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
298 abootimg
, CONFIG_SYS_MAXARGS
, 0, do_abootimg
,
299 "manipulate Android Boot Image",
300 "addr <boot_img_addr> [<vendor_boot_img_addr> [<init_boot_img_addr>]]\n"
301 " - set the address in RAM where boot image is located\n"
302 " ($loadaddr is used by default)\n"
303 "abootimg dump dtb\n"
304 " - print info for all DT blobs in DTB area\n"
305 "abootimg get ver [varname]\n"
306 " - get header version\n"
307 "abootimg get recovery_dtbo [addr_var [size_var]]\n"
308 " - get address and size (hex) of recovery DTBO area in the image\n"
309 " [addr_var]: variable name to contain DTBO area address\n"
310 " [size_var]: variable name to contain DTBO area size\n"
311 "abootimg get dtb_load_addr [varname]\n"
312 " - get load address (hex) of DTB, from image header\n"
313 "abootimg get dtb --index=<num> [addr_var [size_var]]\n"
314 " - get address and size (hex) of DT blob in the image by index\n"
315 " <num>: index number of desired DT blob in DTB area\n"
316 " [addr_var]: variable name to contain DT blob address\n"
317 " [size_var]: variable name to contain DT blob size"