1 // SPDX-License-Identifier: GPL-2.0+
4 * Sam Protsenko <joe.skb7@gmail.com>
7 #include <android_image.h>
11 #define abootimg_addr() \
12 (_abootimg_addr == -1 ? image_load_addr : _abootimg_addr)
14 /* Please use abootimg_addr() macro to obtain the boot image address */
15 static ulong _abootimg_addr
= -1;
17 static int abootimg_get_ver(int argc
, char * const argv
[])
19 const struct andr_img_hdr
*hdr
;
20 int res
= CMD_RET_SUCCESS
;
25 hdr
= map_sysmem(abootimg_addr(), sizeof(*hdr
));
26 if (android_image_check_header(hdr
)) {
27 printf("Error: Boot Image header is incorrect\n");
28 res
= CMD_RET_FAILURE
;
33 printf("%u\n", hdr
->header_version
);
35 env_set_ulong(argv
[0], hdr
->header_version
);
42 static int abootimg_get_recovery_dtbo(int argc
, char * const argv
[])
50 if (!android_image_get_dtbo(abootimg_addr(), &addr
, &size
))
51 return CMD_RET_FAILURE
;
54 printf("%lx\n", addr
);
56 env_set_hex(argv
[0], addr
);
58 env_set_hex(argv
[1], size
);
61 return CMD_RET_SUCCESS
;
64 static int abootimg_get_dtb_load_addr(int argc
, char * const argv
[])
66 const struct andr_img_hdr
*hdr
;
67 int res
= CMD_RET_SUCCESS
;
72 hdr
= map_sysmem(abootimg_addr(), sizeof(*hdr
));
73 if (android_image_check_header(hdr
)) {
74 printf("Error: Boot Image header is incorrect\n");
75 res
= CMD_RET_FAILURE
;
79 if (hdr
->header_version
< 2) {
80 printf("Error: header_version must be >= 2 for this\n");
81 res
= CMD_RET_FAILURE
;
86 printf("%lx\n", (ulong
)hdr
->dtb_addr
);
88 env_set_hex(argv
[0], (ulong
)hdr
->dtb_addr
);
95 static int abootimg_get_dtb_by_index(int argc
, char * const argv
[])
97 const char *index_str
;
103 if (argc
< 1 || argc
> 3)
104 return CMD_RET_USAGE
;
106 index_str
= argv
[0] + strlen("--index=");
107 if (index_str
[0] == '\0') {
108 printf("Error: Wrong index num\n");
109 return CMD_RET_FAILURE
;
112 num
= simple_strtoul(index_str
, &endp
, 0);
114 printf("Error: Wrong index num\n");
115 return CMD_RET_FAILURE
;
118 if (!android_image_get_dtb_by_index(abootimg_addr(), num
,
120 return CMD_RET_FAILURE
;
124 printf("%lx\n", addr
);
126 if (env_set_hex(argv
[1], addr
)) {
127 printf("Error: Can't set [addr_var]\n");
128 return CMD_RET_FAILURE
;
132 if (env_set_hex(argv
[2], size
)) {
133 printf("Error: Can't set [size_var]\n");
134 return CMD_RET_FAILURE
;
139 return CMD_RET_SUCCESS
;
142 static int abootimg_get_dtb(int argc
, char * const argv
[])
145 return CMD_RET_USAGE
;
147 if (strstr(argv
[0], "--index="))
148 return abootimg_get_dtb_by_index(argc
, argv
);
150 return CMD_RET_USAGE
;
153 static int do_abootimg_addr(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
160 return CMD_RET_USAGE
;
162 img_addr
= simple_strtoul(argv
[1], &endp
, 16);
164 printf("Error: Wrong image address\n");
165 return CMD_RET_FAILURE
;
168 _abootimg_addr
= img_addr
;
169 return CMD_RET_SUCCESS
;
172 static int do_abootimg_get(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
178 return CMD_RET_USAGE
;
183 if (!strcmp(param
, "ver"))
184 return abootimg_get_ver(argc
, argv
);
185 else if (!strcmp(param
, "recovery_dtbo"))
186 return abootimg_get_recovery_dtbo(argc
, argv
);
187 else if (!strcmp(param
, "dtb_load_addr"))
188 return abootimg_get_dtb_load_addr(argc
, argv
);
189 else if (!strcmp(param
, "dtb"))
190 return abootimg_get_dtb(argc
, argv
);
192 return CMD_RET_USAGE
;
195 static int do_abootimg_dump(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
199 return CMD_RET_USAGE
;
201 if (!strcmp(argv
[1], "dtb")) {
202 if (android_image_print_dtb_contents(abootimg_addr()))
203 return CMD_RET_FAILURE
;
205 return CMD_RET_USAGE
;
208 return CMD_RET_SUCCESS
;
211 static cmd_tbl_t cmd_abootimg_sub
[] = {
212 U_BOOT_CMD_MKENT(addr
, 2, 1, do_abootimg_addr
, "", ""),
213 U_BOOT_CMD_MKENT(dump
, 2, 1, do_abootimg_dump
, "", ""),
214 U_BOOT_CMD_MKENT(get
, 5, 1, do_abootimg_get
, "", ""),
217 static int do_abootimg(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
222 cp
= find_cmd_tbl(argv
[1], cmd_abootimg_sub
,
223 ARRAY_SIZE(cmd_abootimg_sub
));
225 /* Strip off leading 'abootimg' command argument */
229 if (!cp
|| argc
> cp
->maxargs
)
230 return CMD_RET_USAGE
;
231 if (flag
== CMD_FLAG_REPEAT
&& !cmd_is_repeatable(cp
))
232 return CMD_RET_SUCCESS
;
234 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
238 abootimg
, CONFIG_SYS_MAXARGS
, 0, do_abootimg
,
239 "manipulate Android Boot Image",
241 " - set the address in RAM where boot image is located\n"
242 " ($loadaddr is used by default)\n"
243 "abootimg dump dtb\n"
244 " - print info for all DT blobs in DTB area\n"
245 "abootimg get ver [varname]\n"
246 " - get header version\n"
247 "abootimg get recovery_dtbo [addr_var [size_var]]\n"
248 " - get address and size (hex) of recovery DTBO area in the image\n"
249 " [addr_var]: variable name to contain DTBO area address\n"
250 " [size_var]: variable name to contain DTBO area size\n"
251 "abootimg get dtb_load_addr [varname]\n"
252 " - get load address (hex) of DTB, from image header\n"
253 "abootimg get dtb --index=<num> [addr_var [size_var]]\n"
254 " - get address and size (hex) of DT blob in the image by index\n"
255 " <num>: index number of desired DT blob in DTB area\n"
256 " [addr_var]: variable name to contain DT blob address\n"
257 " [size_var]: variable name to contain DT blob size"