blob: 88c77d999290d12fa46b36383a35519130a19001 [file] [log] [blame]
Sam Protsenko035502e2020-01-24 17:53:42 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2020
4 * Sam Protsenko <joe.skb7@gmail.com>
5 */
6
7#include <android_image.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06009#include <image.h>
Sam Protsenko035502e2020-01-24 17:53:42 +020010#include <mapmem.h>
11
12#define abootimg_addr() \
13 (_abootimg_addr == -1 ? image_load_addr : _abootimg_addr)
14
15/* Please use abootimg_addr() macro to obtain the boot image address */
16static ulong _abootimg_addr = -1;
Safae Ouajih91da3632023-02-06 00:50:04 +010017static ulong _avendor_bootimg_addr = -1;
Sam Protsenko035502e2020-01-24 17:53:42 +020018
Safae Ouajih51c981b2023-02-06 00:50:17 +010019ulong get_abootimg_addr(void)
20{
21 return (_abootimg_addr == -1 ? image_load_addr : _abootimg_addr);
22}
23
24ulong get_avendor_bootimg_addr(void)
25{
26 return _avendor_bootimg_addr;
27}
28
Simon Glassed38aef2020-05-10 11:40:03 -060029static int abootimg_get_ver(int argc, char *const argv[])
Sam Protsenko035502e2020-01-24 17:53:42 +020030{
Safae Ouajih8656e382023-02-06 00:50:03 +010031 const struct andr_boot_img_hdr_v0 *hdr;
Sam Protsenko035502e2020-01-24 17:53:42 +020032 int res = CMD_RET_SUCCESS;
33
34 if (argc > 1)
35 return CMD_RET_USAGE;
36
37 hdr = map_sysmem(abootimg_addr(), sizeof(*hdr));
Safae Ouajih88ad0c12023-02-06 00:50:05 +010038 if (!is_android_boot_image_header(hdr)) {
Sam Protsenko035502e2020-01-24 17:53:42 +020039 printf("Error: Boot Image header is incorrect\n");
40 res = CMD_RET_FAILURE;
41 goto exit;
42 }
43
44 if (argc == 0)
45 printf("%u\n", hdr->header_version);
46 else
47 env_set_ulong(argv[0], hdr->header_version);
48
49exit:
50 unmap_sysmem(hdr);
51 return res;
52}
53
Simon Glassed38aef2020-05-10 11:40:03 -060054static int abootimg_get_recovery_dtbo(int argc, char *const argv[])
Sam Protsenko035502e2020-01-24 17:53:42 +020055{
56 ulong addr;
57 u32 size;
58
59 if (argc > 2)
60 return CMD_RET_USAGE;
61
62 if (!android_image_get_dtbo(abootimg_addr(), &addr, &size))
63 return CMD_RET_FAILURE;
64
65 if (argc == 0) {
66 printf("%lx\n", addr);
67 } else {
68 env_set_hex(argv[0], addr);
69 if (argc == 2)
70 env_set_hex(argv[1], size);
71 }
72
73 return CMD_RET_SUCCESS;
74}
75
Simon Glassed38aef2020-05-10 11:40:03 -060076static int abootimg_get_dtb_load_addr(int argc, char *const argv[])
Sam Protsenko035502e2020-01-24 17:53:42 +020077{
Sam Protsenko035502e2020-01-24 17:53:42 +020078 if (argc > 1)
79 return CMD_RET_USAGE;
Safae Ouajihbced1042023-02-06 00:50:08 +010080 struct andr_image_data img_data = {0};
81 const struct andr_boot_img_hdr_v0 *hdr;
Safae Ouajih51c981b2023-02-06 00:50:17 +010082 const struct andr_vnd_boot_img_hdr *vhdr;
Sam Protsenko035502e2020-01-24 17:53:42 +020083
84 hdr = map_sysmem(abootimg_addr(), sizeof(*hdr));
Safae Ouajih51c981b2023-02-06 00:50:17 +010085 if (get_avendor_bootimg_addr() != -1)
86 vhdr = map_sysmem(get_avendor_bootimg_addr(), sizeof(*vhdr));
87
88 if (!android_image_get_data(hdr, vhdr, &img_data)) {
89 if (get_avendor_bootimg_addr() != -1)
90 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +010091 unmap_sysmem(hdr);
92 return CMD_RET_FAILURE;
Sam Protsenko035502e2020-01-24 17:53:42 +020093 }
Safae Ouajih51c981b2023-02-06 00:50:17 +010094
95 if (get_avendor_bootimg_addr() != -1)
96 unmap_sysmem(vhdr);
Safae Ouajihbced1042023-02-06 00:50:08 +010097 unmap_sysmem(hdr);
Sam Protsenko035502e2020-01-24 17:53:42 +020098
Safae Ouajihbced1042023-02-06 00:50:08 +010099 if (img_data.header_version < 2) {
Sam Protsenko035502e2020-01-24 17:53:42 +0200100 printf("Error: header_version must be >= 2 for this\n");
Safae Ouajihbced1042023-02-06 00:50:08 +0100101 return CMD_RET_FAILURE;
Sam Protsenko035502e2020-01-24 17:53:42 +0200102 }
103
Safae Ouajihbced1042023-02-06 00:50:08 +0100104 if (!img_data.dtb_load_addr) {
105 printf("Error: failed to read dtb_load_addr\n");
106 return CMD_RET_FAILURE;
107 }
108
Sam Protsenko035502e2020-01-24 17:53:42 +0200109 if (argc == 0)
Safae Ouajihbced1042023-02-06 00:50:08 +0100110 printf("%lx\n", (ulong)img_data.dtb_load_addr);
Sam Protsenko035502e2020-01-24 17:53:42 +0200111 else
Safae Ouajihbced1042023-02-06 00:50:08 +0100112 env_set_hex(argv[0], (ulong)img_data.dtb_load_addr);
Sam Protsenko035502e2020-01-24 17:53:42 +0200113
Safae Ouajihbced1042023-02-06 00:50:08 +0100114 return CMD_RET_SUCCESS;
Sam Protsenko035502e2020-01-24 17:53:42 +0200115}
116
Simon Glassed38aef2020-05-10 11:40:03 -0600117static int abootimg_get_dtb_by_index(int argc, char *const argv[])
Sam Protsenko035502e2020-01-24 17:53:42 +0200118{
119 const char *index_str;
120 u32 num;
121 char *endp;
122 ulong addr;
123 u32 size;
124
125 if (argc < 1 || argc > 3)
126 return CMD_RET_USAGE;
127
128 index_str = argv[0] + strlen("--index=");
129 if (index_str[0] == '\0') {
130 printf("Error: Wrong index num\n");
131 return CMD_RET_FAILURE;
132 }
133
134 num = simple_strtoul(index_str, &endp, 0);
135 if (*endp != '\0') {
136 printf("Error: Wrong index num\n");
137 return CMD_RET_FAILURE;
138 }
139
Safae Ouajih51c981b2023-02-06 00:50:17 +0100140 if (!android_image_get_dtb_by_index(abootimg_addr(),
141 get_avendor_bootimg_addr(), num,
Sam Protsenko035502e2020-01-24 17:53:42 +0200142 &addr, &size)) {
143 return CMD_RET_FAILURE;
144 }
145
146 if (argc == 1) {
147 printf("%lx\n", addr);
148 } else {
149 if (env_set_hex(argv[1], addr)) {
150 printf("Error: Can't set [addr_var]\n");
151 return CMD_RET_FAILURE;
152 }
153
154 if (argc == 3) {
155 if (env_set_hex(argv[2], size)) {
156 printf("Error: Can't set [size_var]\n");
157 return CMD_RET_FAILURE;
158 }
159 }
160 }
161
162 return CMD_RET_SUCCESS;
163}
164
Simon Glassed38aef2020-05-10 11:40:03 -0600165static int abootimg_get_dtb(int argc, char *const argv[])
Sam Protsenko035502e2020-01-24 17:53:42 +0200166{
167 if (argc < 1)
168 return CMD_RET_USAGE;
169
170 if (strstr(argv[0], "--index="))
171 return abootimg_get_dtb_by_index(argc, argv);
172
173 return CMD_RET_USAGE;
174}
175
Simon Glassed38aef2020-05-10 11:40:03 -0600176static int do_abootimg_addr(struct cmd_tbl *cmdtp, int flag, int argc,
177 char *const argv[])
Sam Protsenko035502e2020-01-24 17:53:42 +0200178{
179 char *endp;
180 ulong img_addr;
181
Safae Ouajih91da3632023-02-06 00:50:04 +0100182 if (argc < 2 || argc > 3)
Sam Protsenko035502e2020-01-24 17:53:42 +0200183 return CMD_RET_USAGE;
184
Simon Glass3ff49ec2021-07-24 09:03:29 -0600185 img_addr = hextoul(argv[1], &endp);
Sam Protsenko035502e2020-01-24 17:53:42 +0200186 if (*endp != '\0') {
187 printf("Error: Wrong image address\n");
188 return CMD_RET_FAILURE;
189 }
190
191 _abootimg_addr = img_addr;
Safae Ouajih91da3632023-02-06 00:50:04 +0100192
193 if (argc == 3) {
194 img_addr = simple_strtoul(argv[2], &endp, 16);
195 if (*endp != '\0') {
196 printf("Error: Wrong vendor image address\n");
197 return CMD_RET_FAILURE;
198 }
199
200 _avendor_bootimg_addr = img_addr;
201 }
202
Sam Protsenko035502e2020-01-24 17:53:42 +0200203 return CMD_RET_SUCCESS;
204}
205
Simon Glassed38aef2020-05-10 11:40:03 -0600206static int do_abootimg_get(struct cmd_tbl *cmdtp, int flag, int argc,
207 char *const argv[])
Sam Protsenko035502e2020-01-24 17:53:42 +0200208{
209 const char *param;
210
211 if (argc < 2)
212 return CMD_RET_USAGE;
213
214 param = argv[1];
215 argc -= 2;
216 argv += 2;
217 if (!strcmp(param, "ver"))
218 return abootimg_get_ver(argc, argv);
219 else if (!strcmp(param, "recovery_dtbo"))
220 return abootimg_get_recovery_dtbo(argc, argv);
221 else if (!strcmp(param, "dtb_load_addr"))
222 return abootimg_get_dtb_load_addr(argc, argv);
223 else if (!strcmp(param, "dtb"))
224 return abootimg_get_dtb(argc, argv);
225
226 return CMD_RET_USAGE;
227}
228
Simon Glassed38aef2020-05-10 11:40:03 -0600229static int do_abootimg_dump(struct cmd_tbl *cmdtp, int flag, int argc,
230 char *const argv[])
Sam Protsenko035502e2020-01-24 17:53:42 +0200231{
232 if (argc != 2)
233 return CMD_RET_USAGE;
234
235 if (!strcmp(argv[1], "dtb")) {
236 if (android_image_print_dtb_contents(abootimg_addr()))
237 return CMD_RET_FAILURE;
238 } else {
239 return CMD_RET_USAGE;
240 }
241
242 return CMD_RET_SUCCESS;
243}
244
Simon Glassed38aef2020-05-10 11:40:03 -0600245static struct cmd_tbl cmd_abootimg_sub[] = {
Safae Ouajih91da3632023-02-06 00:50:04 +0100246 U_BOOT_CMD_MKENT(addr, 3, 1, do_abootimg_addr, "", ""),
Sam Protsenko035502e2020-01-24 17:53:42 +0200247 U_BOOT_CMD_MKENT(dump, 2, 1, do_abootimg_dump, "", ""),
248 U_BOOT_CMD_MKENT(get, 5, 1, do_abootimg_get, "", ""),
249};
250
Simon Glassed38aef2020-05-10 11:40:03 -0600251static int do_abootimg(struct cmd_tbl *cmdtp, int flag, int argc,
252 char *const argv[])
Sam Protsenko035502e2020-01-24 17:53:42 +0200253{
Simon Glassed38aef2020-05-10 11:40:03 -0600254 struct cmd_tbl *cp;
Sam Protsenko035502e2020-01-24 17:53:42 +0200255
256 cp = find_cmd_tbl(argv[1], cmd_abootimg_sub,
257 ARRAY_SIZE(cmd_abootimg_sub));
258
259 /* Strip off leading 'abootimg' command argument */
260 argc--;
261 argv++;
262
263 if (!cp || argc > cp->maxargs)
264 return CMD_RET_USAGE;
265 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
266 return CMD_RET_SUCCESS;
267
268 return cp->cmd(cmdtp, flag, argc, argv);
269}
270
271U_BOOT_CMD(
272 abootimg, CONFIG_SYS_MAXARGS, 0, do_abootimg,
273 "manipulate Android Boot Image",
Safae Ouajih91da3632023-02-06 00:50:04 +0100274 "addr <boot_img_addr> [<vendor_boot_img_addr>]>\n"
Sam Protsenko035502e2020-01-24 17:53:42 +0200275 " - set the address in RAM where boot image is located\n"
276 " ($loadaddr is used by default)\n"
277 "abootimg dump dtb\n"
278 " - print info for all DT blobs in DTB area\n"
279 "abootimg get ver [varname]\n"
280 " - get header version\n"
281 "abootimg get recovery_dtbo [addr_var [size_var]]\n"
282 " - get address and size (hex) of recovery DTBO area in the image\n"
283 " [addr_var]: variable name to contain DTBO area address\n"
284 " [size_var]: variable name to contain DTBO area size\n"
285 "abootimg get dtb_load_addr [varname]\n"
286 " - get load address (hex) of DTB, from image header\n"
287 "abootimg get dtb --index=<num> [addr_var [size_var]]\n"
288 " - get address and size (hex) of DT blob in the image by index\n"
289 " <num>: index number of desired DT blob in DTB area\n"
290 " [addr_var]: variable name to contain DT blob address\n"
291 " [size_var]: variable name to contain DT blob size"
292);