blob: 60bb01c3498a807092007fe7807a92261d511dff [file] [log] [blame]
Sam Protsenkod03e76a2018-08-16 23:34:13 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018 Linaro Ltd.
4 * Sam Protsenko <semen.protsenko@linaro.org>
Eugeniu Rosca4f731c72019-12-24 17:51:08 +01005 * Eugeniu Rosca <rosca.eugeniu@gmail.com>
Sam Protsenkod03e76a2018-08-16 23:34:13 +03006 */
7
Simon Glass9fb625c2019-08-01 09:46:51 -06008#include <env.h>
Sam Protsenkod03e76a2018-08-16 23:34:13 +03009#include <image-android-dt.h>
10#include <common.h>
11
Eugeniu Rosca4f731c72019-12-24 17:51:08 +010012#define OPT_INDEX "--index"
Sam Protsenkod03e76a2018-08-16 23:34:13 +030013
Eugeniu Rosca4f731c72019-12-24 17:51:08 +010014/*
15 * Current/working DTB/DTBO Android image address.
16 * Similar to 'working_fdt' variable in 'fdt' command.
17 */
18static ulong working_img;
19
20static int do_adtimg_addr(cmd_tbl_t *cmdtp, int flag, int argc,
Eugeniu Rosca4c6edc22019-12-24 17:51:07 +010021 char * const argv[])
Sam Protsenkod03e76a2018-08-16 23:34:13 +030022{
23 char *endp;
24 ulong hdr_addr;
25
26 if (argc != 2)
27 return CMD_RET_USAGE;
28
29 hdr_addr = simple_strtoul(argv[1], &endp, 16);
30 if (*endp != '\0') {
Eugeniu Rosca4f731c72019-12-24 17:51:08 +010031 printf("Error: Wrong image address '%s'\n", argv[1]);
Sam Protsenkod03e76a2018-08-16 23:34:13 +030032 return CMD_RET_FAILURE;
33 }
34
Eugeniu Rosca4f731c72019-12-24 17:51:08 +010035 /*
36 * Allow users to set an address prior to copying the DTB/DTBO
37 * image to that same address, i.e. skip header verification.
38 */
39
40 working_img = hdr_addr;
41 return CMD_RET_SUCCESS;
42}
43
44static int adtimg_check_working_img(void)
45{
46 if (!working_img) {
47 printf("Error: Please, call 'adtimg addr <addr>'. Aborting!\n");
Sam Protsenkod03e76a2018-08-16 23:34:13 +030048 return CMD_RET_FAILURE;
49 }
50
Eugeniu Rosca4f731c72019-12-24 17:51:08 +010051 if (!android_dt_check_header(working_img)) {
52 printf("Error: Invalid image header at 0x%lx\n", working_img);
53 return CMD_RET_FAILURE;
54 }
Sam Protsenkod03e76a2018-08-16 23:34:13 +030055
56 return CMD_RET_SUCCESS;
57}
58
Eugeniu Rosca4f731c72019-12-24 17:51:08 +010059static int do_adtimg_dump(cmd_tbl_t *cmdtp, int flag, int argc,
Sam Protsenkod03e76a2018-08-16 23:34:13 +030060 char * const argv[])
61{
Eugeniu Rosca4f731c72019-12-24 17:51:08 +010062 if (argc != 1)
63 return CMD_RET_USAGE;
64
65 if (adtimg_check_working_img() != CMD_RET_SUCCESS)
66 return CMD_RET_FAILURE;
67
68 android_dt_print_contents(working_img);
69
70 return CMD_RET_SUCCESS;
71}
72
73static int adtimg_getopt_u32(char * const opt, char * const name, u32 *optval)
74{
75 char *endp, *str;
76 u32 val;
77
78 if (!opt || !name || !optval)
79 return CMD_RET_FAILURE;
80
81 str = strchr(opt, '=');
82 if (!str) {
83 printf("Error: Option '%s' not followed by '='\n", name);
84 return CMD_RET_FAILURE;
85 }
86
87 if (*++str == '\0') {
88 printf("Error: Option '%s=' not followed by value\n", name);
89 return CMD_RET_FAILURE;
90 }
91
92 val = simple_strtoul(str, &endp, 0);
93 if (*endp != '\0') {
94 printf("Error: Wrong integer value '%s=%s'\n", name, str);
95 return CMD_RET_FAILURE;
96 }
97
98 *optval = val;
99 return CMD_RET_SUCCESS;
100}
101
102static int adtimg_getopt_index(int argc, char * const argv[], u32 *index,
103 char **avar, char **svar)
104{
105 int ret;
106
107 if (!argv || !avar || !svar)
108 return CMD_RET_FAILURE;
109
110 if (argc > 3) {
111 printf("Error: Unexpected argument '%s'\n", argv[3]);
112 return CMD_RET_FAILURE;
113 }
114
115 ret = adtimg_getopt_u32(argv[0], OPT_INDEX, index);
116 if (ret != CMD_RET_SUCCESS)
117 return ret;
118
119 if (argc > 1)
120 *avar = argv[1];
121 if (argc > 2)
122 *svar = argv[2];
123
124 return CMD_RET_SUCCESS;
125}
126
127static int adtimg_get_dt_by_index(int argc, char * const argv[])
128{
129 ulong addr;
130 u32 index, size;
131 int ret;
132 char *avar = NULL, *svar = NULL;
133
134 ret = adtimg_getopt_index(argc, argv, &index, &avar, &svar);
135 if (ret != CMD_RET_SUCCESS)
136 return ret;
137
138 if (!android_dt_get_fdt_by_index(working_img, index, &addr, &size))
139 return CMD_RET_FAILURE;
140
141 if (avar && svar) {
142 ret = env_set_hex(avar, addr);
143 if (ret) {
144 printf("Error: Can't set '%s' to 0x%lx\n", avar, addr);
145 return CMD_RET_FAILURE;
146 }
147 ret = env_set_hex(svar, size);
148 if (ret) {
149 printf("Error: Can't set '%s' to 0x%x\n", svar, size);
150 return CMD_RET_FAILURE;
151 }
152 } else if (avar) {
153 ret = env_set_hex(avar, addr);
154 if (ret) {
155 printf("Error: Can't set '%s' to 0x%lx\n", avar, addr);
156 return CMD_RET_FAILURE;
157 }
158 printf("0x%x (%d)\n", size, size);
159 } else {
160 printf("0x%lx, 0x%x (%d)\n", addr, size, size);
161 }
162
163 return CMD_RET_SUCCESS;
164}
165
166static int adtimg_get_dt(int argc, char * const argv[])
167{
168 if (argc < 2) {
169 printf("Error: No options passed to '%s'\n", argv[0]);
170 return CMD_RET_FAILURE;
171 }
172
173 /* Strip off leading 'dt' command argument */
174 argc--;
175 argv++;
176
177 if (!strncmp(argv[0], OPT_INDEX, sizeof(OPT_INDEX) - 1))
178 return adtimg_get_dt_by_index(argc, argv);
179
180 printf("Error: Option '%s' not supported\n", argv[0]);
181 return CMD_RET_FAILURE;
182}
183
184static int do_adtimg_get(cmd_tbl_t *cmdtp, int flag, int argc,
185 char * const argv[])
186{
187 if (argc < 2) {
188 printf("Error: No arguments passed to '%s'\n", argv[0]);
189 return CMD_RET_FAILURE;
190 }
191
192 if (adtimg_check_working_img() != CMD_RET_SUCCESS)
193 return CMD_RET_FAILURE;
194
195 /* Strip off leading 'get' command argument */
196 argc--;
197 argv++;
198
199 if (!strcmp(argv[0], "dt"))
200 return adtimg_get_dt(argc, argv);
201
202 printf("Error: Wrong argument '%s'\n", argv[0]);
203 return CMD_RET_FAILURE;
Sam Protsenkod03e76a2018-08-16 23:34:13 +0300204}
205
Eugeniu Rosca4c6edc22019-12-24 17:51:07 +0100206static cmd_tbl_t cmd_adtimg_sub[] = {
Eugeniu Rosca4f731c72019-12-24 17:51:08 +0100207 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, "", ""),
Sam Protsenkod03e76a2018-08-16 23:34:13 +0300210};
211
Eugeniu Rosca4c6edc22019-12-24 17:51:07 +0100212static int do_adtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Sam Protsenkod03e76a2018-08-16 23:34:13 +0300213{
214 cmd_tbl_t *cp;
215
Eugeniu Rosca4c6edc22019-12-24 17:51:07 +0100216 cp = find_cmd_tbl(argv[1], cmd_adtimg_sub, ARRAY_SIZE(cmd_adtimg_sub));
Sam Protsenkod03e76a2018-08-16 23:34:13 +0300217
Eugeniu Rosca4c6edc22019-12-24 17:51:07 +0100218 /* Strip off leading 'adtimg' command argument */
Sam Protsenkod03e76a2018-08-16 23:34:13 +0300219 argc--;
220 argv++;
221
222 if (!cp || argc > cp->maxargs)
223 return CMD_RET_USAGE;
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100224 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Sam Protsenkod03e76a2018-08-16 23:34:13 +0300225 return CMD_RET_SUCCESS;
226
227 return cp->cmd(cmdtp, flag, argc, argv);
228}
229
230U_BOOT_CMD(
Eugeniu Rosca4c6edc22019-12-24 17:51:07 +0100231 adtimg, CONFIG_SYS_MAXARGS, 0, do_adtimg,
Sam Protsenkod03e76a2018-08-16 23:34:13 +0300232 "manipulate dtb/dtbo Android image",
Eugeniu Rosca4f731c72019-12-24 17:51:08 +0100233 "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"
236 "\n"
237 "Legend:\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)"
Sam Protsenkod03e76a2018-08-16 23:34:13 +0300242);