1 // SPDX-License-Identifier: GPL-2.0+
4 * Sam Protsenko <joe.skb7@gmail.com>
7 #include <android_image.h>
13 #define abootimg_addr() \
14 (_abootimg_addr == -1 ? image_load_addr : _abootimg_addr)
16 /* Please use abootimg_addr() macro to obtain the boot image address */
17 static ulong _abootimg_addr = -1;
19 static int abootimg_get_ver(int argc, char *const argv[])
21 const struct andr_img_hdr *hdr;
22 int res = CMD_RET_SUCCESS;
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;
35 printf("%u\n", hdr->header_version);
37 env_set_ulong(argv[0], hdr->header_version);
44 static int abootimg_get_recovery_dtbo(int argc, char *const argv[])
52 if (!android_image_get_dtbo(abootimg_addr(), &addr, &size))
53 return CMD_RET_FAILURE;
56 printf("%lx\n", addr);
58 env_set_hex(argv[0], addr);
60 env_set_hex(argv[1], size);
63 return CMD_RET_SUCCESS;
66 static int abootimg_get_dtb_load_addr(int argc, char *const argv[])
68 const struct andr_img_hdr *hdr;
69 int res = CMD_RET_SUCCESS;
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;
81 if (hdr->header_version < 2) {
82 printf("Error: header_version must be >= 2 for this\n");
83 res = CMD_RET_FAILURE;
88 printf("%lx\n", (ulong)hdr->dtb_addr);
90 env_set_hex(argv[0], (ulong)hdr->dtb_addr);
97 static int abootimg_get_dtb_by_index(int argc, char *const argv[])
99 const char *index_str;
105 if (argc < 1 || argc > 3)
106 return CMD_RET_USAGE;
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;
114 num = simple_strtoul(index_str, &endp, 0);
116 printf("Error: Wrong index num\n");
117 return CMD_RET_FAILURE;
120 if (!android_image_get_dtb_by_index(abootimg_addr(), num,
122 return CMD_RET_FAILURE;
126 printf("%lx\n", addr);
128 if (env_set_hex(argv[1], addr)) {
129 printf("Error: Can't set [addr_var]\n");
130 return CMD_RET_FAILURE;
134 if (env_set_hex(argv[2], size)) {
135 printf("Error: Can't set [size_var]\n");
136 return CMD_RET_FAILURE;
141 return CMD_RET_SUCCESS;
144 static int abootimg_get_dtb(int argc, char *const argv[])
147 return CMD_RET_USAGE;
149 if (strstr(argv[0], "--index="))
150 return abootimg_get_dtb_by_index(argc, argv);
152 return CMD_RET_USAGE;
155 static int do_abootimg_addr(struct cmd_tbl *cmdtp, int flag, int argc,
162 return CMD_RET_USAGE;
164 img_addr = simple_strtoul(argv[1], &endp, 16);
166 printf("Error: Wrong image address\n");
167 return CMD_RET_FAILURE;
170 _abootimg_addr = img_addr;
171 return CMD_RET_SUCCESS;
174 static int do_abootimg_get(struct cmd_tbl *cmdtp, int flag, int argc,
180 return CMD_RET_USAGE;
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);
194 return CMD_RET_USAGE;
197 static int do_abootimg_dump(struct cmd_tbl *cmdtp, int flag, int argc,
201 return CMD_RET_USAGE;
203 if (!strcmp(argv[1], "dtb")) {
204 if (android_image_print_dtb_contents(abootimg_addr()))
205 return CMD_RET_FAILURE;
207 return CMD_RET_USAGE;
210 return CMD_RET_SUCCESS;
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, "", ""),
219 static int do_abootimg(struct cmd_tbl *cmdtp, int flag, int argc,
224 cp = find_cmd_tbl(argv[1], cmd_abootimg_sub,
225 ARRAY_SIZE(cmd_abootimg_sub));
227 /* Strip off leading 'abootimg' command argument */
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;
236 return cp->cmd(cmdtp, flag, argc, argv);
240 abootimg, CONFIG_SYS_MAXARGS, 0, do_abootimg,
241 "manipulate Android Boot Image",
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"