1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Linaro Ltd.
4 * Sam Protsenko <semen.protsenko@linaro.org>
5 * Eugeniu Rosca <rosca.eugeniu@gmail.com>
9 #include <image-android-dt.h>
12 #define OPT_INDEX "--index"
15 * Current/working DTB/DTBO Android image address.
16 * Similar to 'working_fdt' variable in 'fdt' command.
18 static ulong working_img;
20 static int do_adtimg_addr(cmd_tbl_t *cmdtp, int flag, int argc,
29 hdr_addr = simple_strtoul(argv[1], &endp, 16);
31 printf("Error: Wrong image address '%s'\n", argv[1]);
32 return CMD_RET_FAILURE;
36 * Allow users to set an address prior to copying the DTB/DTBO
37 * image to that same address, i.e. skip header verification.
40 working_img = hdr_addr;
41 return CMD_RET_SUCCESS;
44 static int adtimg_check_working_img(void)
47 printf("Error: Please, call 'adtimg addr <addr>'. Aborting!\n");
48 return CMD_RET_FAILURE;
51 if (!android_dt_check_header(working_img)) {
52 printf("Error: Invalid image header at 0x%lx\n", working_img);
53 return CMD_RET_FAILURE;
56 return CMD_RET_SUCCESS;
59 static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
65 if (adtimg_check_working_img() != CMD_RET_SUCCESS)
66 return CMD_RET_FAILURE;
68 android_dt_print_contents(working_img);
70 return CMD_RET_SUCCESS;
73 static int adtimg_getopt_u32(char * const opt, char * const name, u32 *optval)
78 if (!opt || !name || !optval)
79 return CMD_RET_FAILURE;
81 str = strchr(opt, '=');
83 printf("Error: Option '%s' not followed by '='\n", name);
84 return CMD_RET_FAILURE;
88 printf("Error: Option '%s=' not followed by value\n", name);
89 return CMD_RET_FAILURE;
92 val = simple_strtoul(str, &endp, 0);
94 printf("Error: Wrong integer value '%s=%s'\n", name, str);
95 return CMD_RET_FAILURE;
99 return CMD_RET_SUCCESS;
102 static int adtimg_getopt_index(int argc, char * const argv[], u32 *index,
103 char **avar, char **svar)
107 if (!argv || !avar || !svar)
108 return CMD_RET_FAILURE;
111 printf("Error: Unexpected argument '%s'\n", argv[3]);
112 return CMD_RET_FAILURE;
115 ret = adtimg_getopt_u32(argv[0], OPT_INDEX, index);
116 if (ret != CMD_RET_SUCCESS)
124 return CMD_RET_SUCCESS;
127 static int adtimg_get_dt_by_index(int argc, char * const argv[])
132 char *avar = NULL, *svar = NULL;
134 ret = adtimg_getopt_index(argc, argv, &index, &avar, &svar);
135 if (ret != CMD_RET_SUCCESS)
138 if (!android_dt_get_fdt_by_index(working_img, index, &addr, &size))
139 return CMD_RET_FAILURE;
142 ret = env_set_hex(avar, addr);
144 printf("Error: Can't set '%s' to 0x%lx\n", avar, addr);
145 return CMD_RET_FAILURE;
147 ret = env_set_hex(svar, size);
149 printf("Error: Can't set '%s' to 0x%x\n", svar, size);
150 return CMD_RET_FAILURE;
153 ret = env_set_hex(avar, addr);
155 printf("Error: Can't set '%s' to 0x%lx\n", avar, addr);
156 return CMD_RET_FAILURE;
158 printf("0x%x (%d)\n", size, size);
160 printf("0x%lx, 0x%x (%d)\n", addr, size, size);
163 return CMD_RET_SUCCESS;
166 static int adtimg_get_dt(int argc, char * const argv[])
169 printf("Error: No options passed to '%s'\n", argv[0]);
170 return CMD_RET_FAILURE;
173 /* Strip off leading 'dt' command argument */
177 if (!strncmp(argv[0], OPT_INDEX, sizeof(OPT_INDEX) - 1))
178 return adtimg_get_dt_by_index(argc, argv);
180 printf("Error: Option '%s' not supported\n", argv[0]);
181 return CMD_RET_FAILURE;
184 static int do_adtimg_get(cmd_tbl_t *cmdtp, int flag, int argc,
188 printf("Error: No arguments passed to '%s'\n", argv[0]);
189 return CMD_RET_FAILURE;
192 if (adtimg_check_working_img() != CMD_RET_SUCCESS)
193 return CMD_RET_FAILURE;
195 /* Strip off leading 'get' command argument */
199 if (!strcmp(argv[0], "dt"))
200 return adtimg_get_dt(argc, argv);
202 printf("Error: Wrong argument '%s'\n", argv[0]);
203 return CMD_RET_FAILURE;
206 static cmd_tbl_t cmd_adtimg_sub[] = {
207 U_BOOT_CMD_MKENT(addr, CONFIG_SYS_MAXARGS, 1, do_adtimg_addr, "", ""),
208 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_adtimg_dump, "", ""),
209 U_BOOT_CMD_MKENT(get, CONFIG_SYS_MAXARGS, 1, do_adtimg_get, "", ""),
212 static int do_adtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
216 cp = find_cmd_tbl(argv[1], cmd_adtimg_sub, ARRAY_SIZE(cmd_adtimg_sub));
218 /* Strip off leading 'adtimg' command argument */
222 if (!cp || argc > cp->maxargs)
223 return CMD_RET_USAGE;
224 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
225 return CMD_RET_SUCCESS;
227 return cp->cmd(cmdtp, flag, argc, argv);
231 adtimg, CONFIG_SYS_MAXARGS, 0, do_adtimg,
232 "manipulate dtb/dtbo Android image",
233 "addr <addr> - Set image location to <addr>\n"
234 "adtimg dump - Print out image contents\n"
235 "adtimg get dt --index=<index> [avar [svar]] - Get DT address/size by index\n"
238 " - <addr>: DTB/DTBO image address (hex) in RAM\n"
239 " - <index>: index (hex/dec) of desired DT in the image\n"
240 " - <avar>: variable name to contain DT address (hex)\n"
241 " - <svar>: variable name to contain DT size (hex)"