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>
10 #include <image-android-dt.h>
13 #define OPT_INDEX "--index"
16 * Current/working DTB/DTBO Android image address.
17 * Similar to 'working_fdt' variable in 'fdt' command.
19 static ulong working_img;
21 static int do_adtimg_addr(struct cmd_tbl *cmdtp, int flag, int argc,
30 hdr_addr = simple_strtoul(argv[1], &endp, 16);
32 printf("Error: Wrong image address '%s'\n", argv[1]);
33 return CMD_RET_FAILURE;
37 * Allow users to set an address prior to copying the DTB/DTBO
38 * image to that same address, i.e. skip header verification.
41 working_img = hdr_addr;
42 return CMD_RET_SUCCESS;
45 static int adtimg_check_working_img(void)
48 printf("Error: Please, call 'adtimg addr <addr>'. Aborting!\n");
49 return CMD_RET_FAILURE;
52 if (!android_dt_check_header(working_img)) {
53 printf("Error: Invalid image header at 0x%lx\n", working_img);
54 return CMD_RET_FAILURE;
57 return CMD_RET_SUCCESS;
60 static int do_adtimg_dump(struct cmd_tbl *cmdtp, int flag, int argc,
66 if (adtimg_check_working_img() != CMD_RET_SUCCESS)
67 return CMD_RET_FAILURE;
69 android_dt_print_contents(working_img);
71 return CMD_RET_SUCCESS;
74 static int adtimg_getopt_u32(char * const opt, char * const name, u32 *optval)
79 if (!opt || !name || !optval)
80 return CMD_RET_FAILURE;
82 str = strchr(opt, '=');
84 printf("Error: Option '%s' not followed by '='\n", name);
85 return CMD_RET_FAILURE;
89 printf("Error: Option '%s=' not followed by value\n", name);
90 return CMD_RET_FAILURE;
93 val = simple_strtoul(str, &endp, 0);
95 printf("Error: Wrong integer value '%s=%s'\n", name, str);
96 return CMD_RET_FAILURE;
100 return CMD_RET_SUCCESS;
103 static int adtimg_getopt_index(int argc, char *const argv[], u32 *index,
104 char **avar, char **svar)
108 if (!argv || !avar || !svar)
109 return CMD_RET_FAILURE;
112 printf("Error: Unexpected argument '%s'\n", argv[3]);
113 return CMD_RET_FAILURE;
116 ret = adtimg_getopt_u32(argv[0], OPT_INDEX, index);
117 if (ret != CMD_RET_SUCCESS)
125 return CMD_RET_SUCCESS;
128 static int adtimg_get_dt_by_index(int argc, char *const argv[])
133 char *avar = NULL, *svar = NULL;
135 ret = adtimg_getopt_index(argc, argv, &index, &avar, &svar);
136 if (ret != CMD_RET_SUCCESS)
139 if (!android_dt_get_fdt_by_index(working_img, index, &addr, &size))
140 return CMD_RET_FAILURE;
143 ret = env_set_hex(avar, addr);
145 printf("Error: Can't set '%s' to 0x%lx\n", avar, addr);
146 return CMD_RET_FAILURE;
148 ret = env_set_hex(svar, size);
150 printf("Error: Can't set '%s' to 0x%x\n", svar, size);
151 return CMD_RET_FAILURE;
154 ret = env_set_hex(avar, addr);
156 printf("Error: Can't set '%s' to 0x%lx\n", avar, addr);
157 return CMD_RET_FAILURE;
159 printf("0x%x (%d)\n", size, size);
161 printf("0x%lx, 0x%x (%d)\n", addr, size, size);
164 return CMD_RET_SUCCESS;
167 static int adtimg_get_dt(int argc, char *const argv[])
170 printf("Error: No options passed to '%s'\n", argv[0]);
171 return CMD_RET_FAILURE;
174 /* Strip off leading 'dt' command argument */
178 if (!strncmp(argv[0], OPT_INDEX, sizeof(OPT_INDEX) - 1))
179 return adtimg_get_dt_by_index(argc, argv);
181 printf("Error: Option '%s' not supported\n", argv[0]);
182 return CMD_RET_FAILURE;
185 static int do_adtimg_get(struct cmd_tbl *cmdtp, int flag, int argc,
189 printf("Error: No arguments passed to '%s'\n", argv[0]);
190 return CMD_RET_FAILURE;
193 if (adtimg_check_working_img() != CMD_RET_SUCCESS)
194 return CMD_RET_FAILURE;
196 /* Strip off leading 'get' command argument */
200 if (!strcmp(argv[0], "dt"))
201 return adtimg_get_dt(argc, argv);
203 printf("Error: Wrong argument '%s'\n", argv[0]);
204 return CMD_RET_FAILURE;
207 static struct cmd_tbl cmd_adtimg_sub[] = {
208 U_BOOT_CMD_MKENT(addr, CONFIG_SYS_MAXARGS, 1, do_adtimg_addr, "", ""),
209 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_adtimg_dump, "", ""),
210 U_BOOT_CMD_MKENT(get, CONFIG_SYS_MAXARGS, 1, do_adtimg_get, "", ""),
213 static int do_adtimg(struct cmd_tbl *cmdtp, int flag, int argc,
218 cp = find_cmd_tbl(argv[1], cmd_adtimg_sub, ARRAY_SIZE(cmd_adtimg_sub));
220 /* Strip off leading 'adtimg' command argument */
224 if (!cp || argc > cp->maxargs)
225 return CMD_RET_USAGE;
226 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
227 return CMD_RET_SUCCESS;
229 return cp->cmd(cmdtp, flag, argc, argv);
233 adtimg, CONFIG_SYS_MAXARGS, 0, do_adtimg,
234 "manipulate dtb/dtbo Android image",
235 "addr <addr> - Set image location to <addr>\n"
236 "adtimg dump - Print out image contents\n"
237 "adtimg get dt --index=<index> [avar [svar]] - Get DT address/size by index\n"
240 " - <addr>: DTB/DTBO image address (hex) in RAM\n"
241 " - <index>: index (hex/dec) of desired DT in the image\n"
242 " - <avar>: variable name to contain DT address (hex)\n"
243 " - <svar>: variable name to contain DT size (hex)"