blob: 62a3ee18e7f99c6406b943eaa7954a5d11461a13 [file] [log] [blame]
Igor Opaniuk29cb8862024-02-09 20:20:40 +01001// SPDX-License-Identifier: GPL-2.0+
Igor Opaniuk60291192018-06-03 21:56:39 +03002/*
3 * (C) Copyright 2018, Linaro Limited
Igor Opaniuk60291192018-06-03 21:56:39 +03004 */
5
6#include <avb_verify.h>
7#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06008#include <env.h>
Igor Opaniuk60291192018-06-03 21:56:39 +03009#include <image.h>
10#include <malloc.h>
11#include <mmc.h>
12
13#define AVB_BOOTARGS "avb_bootargs"
Igor Opaniuka4669d72024-02-09 20:20:42 +010014
Igor Opaniuk60291192018-06-03 21:56:39 +030015static struct AvbOps *avb_ops;
16
Simon Glassed38aef2020-05-10 11:40:03 -060017int do_avb_init(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Igor Opaniuk60291192018-06-03 21:56:39 +030018{
19 unsigned long mmc_dev;
20
21 if (argc != 2)
22 return CMD_RET_USAGE;
23
Simon Glass3ff49ec2021-07-24 09:03:29 -060024 mmc_dev = hextoul(argv[1], NULL);
Igor Opaniuk60291192018-06-03 21:56:39 +030025
26 if (avb_ops)
27 avb_ops_free(avb_ops);
28
29 avb_ops = avb_ops_alloc(mmc_dev);
30 if (avb_ops)
31 return CMD_RET_SUCCESS;
Igor Opaniuka4669d72024-02-09 20:20:42 +010032 else
33 printf("Can't allocate AvbOps");
Igor Opaniuk60291192018-06-03 21:56:39 +030034
Igor Opaniuka4669d72024-02-09 20:20:42 +010035 printf("Failed to initialize AVB\n");
Jens Wiklander6815afc2018-09-25 16:40:07 +020036
Igor Opaniuk60291192018-06-03 21:56:39 +030037 return CMD_RET_FAILURE;
38}
39
Simon Glassed38aef2020-05-10 11:40:03 -060040int do_avb_read_part(struct cmd_tbl *cmdtp, int flag, int argc,
41 char *const argv[])
Igor Opaniuk60291192018-06-03 21:56:39 +030042{
43 const char *part;
44 s64 offset;
45 size_t bytes, bytes_read = 0;
46 void *buffer;
Igor Opaniuka4669d72024-02-09 20:20:42 +010047 int ret;
Igor Opaniuk60291192018-06-03 21:56:39 +030048
49 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +010050 printf("AVB is not initialized, please run 'avb init <id>'\n");
51 return CMD_RET_FAILURE;
Igor Opaniuk60291192018-06-03 21:56:39 +030052 }
53
54 if (argc != 5)
55 return CMD_RET_USAGE;
56
57 part = argv[1];
Simon Glass3ff49ec2021-07-24 09:03:29 -060058 offset = hextoul(argv[2], NULL);
59 bytes = hextoul(argv[3], NULL);
60 buffer = (void *)hextoul(argv[4], NULL);
Igor Opaniuk60291192018-06-03 21:56:39 +030061
Igor Opaniuka4669d72024-02-09 20:20:42 +010062 ret = avb_ops->read_from_partition(avb_ops, part, offset,
63 bytes, buffer, &bytes_read);
64 if (ret == AVB_IO_RESULT_OK) {
Igor Opaniuk60291192018-06-03 21:56:39 +030065 printf("Read %zu bytes\n", bytes_read);
66 return CMD_RET_SUCCESS;
67 }
68
Igor Opaniuka4669d72024-02-09 20:20:42 +010069 printf("Failed to read from partition '%s', err = %d\n",
70 part, ret);
Jens Wiklander6815afc2018-09-25 16:40:07 +020071
Igor Opaniuk60291192018-06-03 21:56:39 +030072 return CMD_RET_FAILURE;
73}
74
Simon Glassed38aef2020-05-10 11:40:03 -060075int do_avb_read_part_hex(struct cmd_tbl *cmdtp, int flag, int argc,
Igor Opaniuk60291192018-06-03 21:56:39 +030076 char *const argv[])
77{
78 const char *part;
79 s64 offset;
80 size_t bytes, bytes_read = 0;
81 char *buffer;
Igor Opaniuka4669d72024-02-09 20:20:42 +010082 int ret;
Igor Opaniuk60291192018-06-03 21:56:39 +030083
84 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +010085 printf("AVB is not initialized, please run 'avb init <id>'\n");
86 return CMD_RET_FAILURE;
Igor Opaniuk60291192018-06-03 21:56:39 +030087 }
88
89 if (argc != 4)
90 return CMD_RET_USAGE;
91
92 part = argv[1];
Simon Glass3ff49ec2021-07-24 09:03:29 -060093 offset = hextoul(argv[2], NULL);
94 bytes = hextoul(argv[3], NULL);
Igor Opaniuk60291192018-06-03 21:56:39 +030095
96 buffer = malloc(bytes);
97 if (!buffer) {
98 printf("Failed to tlb_allocate buffer for data\n");
99 return CMD_RET_FAILURE;
100 }
101 memset(buffer, 0, bytes);
102
Igor Opaniuka4669d72024-02-09 20:20:42 +0100103 ret = avb_ops->read_from_partition(avb_ops, part, offset,
104 bytes, buffer, &bytes_read);
105 if (ret == AVB_IO_RESULT_OK) {
Igor Opaniuk60291192018-06-03 21:56:39 +0300106 printf("Requested %zu, read %zu bytes\n", bytes, bytes_read);
107 printf("Data: ");
108 for (int i = 0; i < bytes_read; i++)
109 printf("%02X", buffer[i]);
110
111 printf("\n");
112
113 free(buffer);
114 return CMD_RET_SUCCESS;
115 }
116
Igor Opaniuka4669d72024-02-09 20:20:42 +0100117 printf("Failed to read from partition '%s', err = %d\n",
118 part, ret);
Jens Wiklander6815afc2018-09-25 16:40:07 +0200119
Igor Opaniuk60291192018-06-03 21:56:39 +0300120 free(buffer);
121 return CMD_RET_FAILURE;
122}
123
Simon Glassed38aef2020-05-10 11:40:03 -0600124int do_avb_write_part(struct cmd_tbl *cmdtp, int flag, int argc,
125 char *const argv[])
Igor Opaniuk60291192018-06-03 21:56:39 +0300126{
127 const char *part;
128 s64 offset;
129 size_t bytes;
130 void *buffer;
Igor Opaniuka4669d72024-02-09 20:20:42 +0100131 int ret;
Igor Opaniuk60291192018-06-03 21:56:39 +0300132
133 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +0100134 printf("AVB is not initialized, please run 'avb init <id>'\n");
Igor Opaniuk60291192018-06-03 21:56:39 +0300135 return CMD_RET_FAILURE;
136 }
137
138 if (argc != 5)
139 return CMD_RET_USAGE;
140
141 part = argv[1];
Simon Glass3ff49ec2021-07-24 09:03:29 -0600142 offset = hextoul(argv[2], NULL);
143 bytes = hextoul(argv[3], NULL);
144 buffer = (void *)hextoul(argv[4], NULL);
Igor Opaniuk60291192018-06-03 21:56:39 +0300145
Igor Opaniuka4669d72024-02-09 20:20:42 +0100146 ret = avb_ops->write_to_partition(avb_ops, part, offset,
147 bytes, buffer);
148 if (ret == AVB_IO_RESULT_OK) {
Igor Opaniuk60291192018-06-03 21:56:39 +0300149 printf("Wrote %zu bytes\n", bytes);
150 return CMD_RET_SUCCESS;
151 }
152
Igor Opaniuka4669d72024-02-09 20:20:42 +0100153 printf("Failed to write in partition '%s', err = %d\n",
154 part, ret);
Jens Wiklander6815afc2018-09-25 16:40:07 +0200155
Igor Opaniuk60291192018-06-03 21:56:39 +0300156 return CMD_RET_FAILURE;
157}
158
Simon Glassed38aef2020-05-10 11:40:03 -0600159int do_avb_read_rb(struct cmd_tbl *cmdtp, int flag, int argc,
160 char *const argv[])
Igor Opaniuk60291192018-06-03 21:56:39 +0300161{
162 size_t index;
163 u64 rb_idx;
Igor Opaniuka4669d72024-02-09 20:20:42 +0100164 int ret;
Igor Opaniuk60291192018-06-03 21:56:39 +0300165
166 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +0100167 printf("AVB is not initialized, please run 'avb init <id>'\n");
Igor Opaniuk60291192018-06-03 21:56:39 +0300168 return CMD_RET_FAILURE;
169 }
170
171 if (argc != 2)
172 return CMD_RET_USAGE;
173
Simon Glass3ff49ec2021-07-24 09:03:29 -0600174 index = (size_t)hextoul(argv[1], NULL);
Igor Opaniuk60291192018-06-03 21:56:39 +0300175
Igor Opaniuka4669d72024-02-09 20:20:42 +0100176 ret = avb_ops->read_rollback_index(avb_ops, index, &rb_idx);
177 if (ret == AVB_IO_RESULT_OK) {
Jens Wiklander5f51dcc2018-09-25 16:40:06 +0200178 printf("Rollback index: %llx\n", rb_idx);
Igor Opaniuk60291192018-06-03 21:56:39 +0300179 return CMD_RET_SUCCESS;
180 }
Jens Wiklander6815afc2018-09-25 16:40:07 +0200181
Igor Opaniuka4669d72024-02-09 20:20:42 +0100182 printf("Failed to read rollback index id = %zu, err = %d\n",
183 index, ret);
Jens Wiklander6815afc2018-09-25 16:40:07 +0200184
Igor Opaniuk60291192018-06-03 21:56:39 +0300185 return CMD_RET_FAILURE;
186}
187
Simon Glassed38aef2020-05-10 11:40:03 -0600188int do_avb_write_rb(struct cmd_tbl *cmdtp, int flag, int argc,
189 char *const argv[])
Igor Opaniuk60291192018-06-03 21:56:39 +0300190{
191 size_t index;
192 u64 rb_idx;
Igor Opaniuka4669d72024-02-09 20:20:42 +0100193 int ret;
Igor Opaniuk60291192018-06-03 21:56:39 +0300194
195 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +0100196 printf("AVB is not initialized, please run 'avb init <id>'\n");
Igor Opaniuk60291192018-06-03 21:56:39 +0300197 return CMD_RET_FAILURE;
198 }
199
200 if (argc != 3)
201 return CMD_RET_USAGE;
202
Simon Glass3ff49ec2021-07-24 09:03:29 -0600203 index = (size_t)hextoul(argv[1], NULL);
204 rb_idx = hextoul(argv[2], NULL);
Igor Opaniuk60291192018-06-03 21:56:39 +0300205
Igor Opaniuka4669d72024-02-09 20:20:42 +0100206 ret = avb_ops->write_rollback_index(avb_ops, index, rb_idx);
207 if (ret == AVB_IO_RESULT_OK)
Igor Opaniuk60291192018-06-03 21:56:39 +0300208 return CMD_RET_SUCCESS;
209
Igor Opaniuka4669d72024-02-09 20:20:42 +0100210 printf("Failed to write rollback index id = %zu, err = %d\n",
211 index, ret);
Jens Wiklander6815afc2018-09-25 16:40:07 +0200212
Igor Opaniuk60291192018-06-03 21:56:39 +0300213 return CMD_RET_FAILURE;
214}
215
Simon Glassed38aef2020-05-10 11:40:03 -0600216int do_avb_get_uuid(struct cmd_tbl *cmdtp, int flag,
217 int argc, char *const argv[])
Igor Opaniuk60291192018-06-03 21:56:39 +0300218{
219 const char *part;
220 char buffer[UUID_STR_LEN + 1];
Igor Opaniuka4669d72024-02-09 20:20:42 +0100221 int ret;
Igor Opaniuk60291192018-06-03 21:56:39 +0300222
223 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +0100224 printf("AVB is not initialized, please run 'avb init <id>'\n");
Igor Opaniuk60291192018-06-03 21:56:39 +0300225 return CMD_RET_FAILURE;
226 }
227
228 if (argc != 2)
229 return CMD_RET_USAGE;
230
231 part = argv[1];
232
Igor Opaniuka4669d72024-02-09 20:20:42 +0100233 ret = avb_ops->get_unique_guid_for_partition(avb_ops, part,
234 buffer,
235 UUID_STR_LEN + 1);
236 if (ret == AVB_IO_RESULT_OK) {
Igor Opaniuk60291192018-06-03 21:56:39 +0300237 printf("'%s' UUID: %s\n", part, buffer);
238 return CMD_RET_SUCCESS;
239 }
240
Igor Opaniuka4669d72024-02-09 20:20:42 +0100241 printf("Failed to read partition '%s' UUID, err = %d\n",
242 part, ret);
Jens Wiklander6815afc2018-09-25 16:40:07 +0200243
Igor Opaniuk60291192018-06-03 21:56:39 +0300244 return CMD_RET_FAILURE;
245}
246
Simon Glassed38aef2020-05-10 11:40:03 -0600247int do_avb_verify_part(struct cmd_tbl *cmdtp, int flag,
Igor Opaniuk60291192018-06-03 21:56:39 +0300248 int argc, char *const argv[])
249{
Sam Protsenkoac80a982019-08-15 20:49:47 +0300250 const char * const requested_partitions[] = {"boot", NULL};
Igor Opaniuk60291192018-06-03 21:56:39 +0300251 AvbSlotVerifyResult slot_result;
252 AvbSlotVerifyData *out_data;
Igor Opaniukf0f3bfe2018-06-03 21:56:40 +0300253 char *cmdline;
254 char *extra_args;
Sam Protsenkof6b91bf2019-10-21 13:55:16 +0300255 char *slot_suffix = "";
Igor Opaniuka4669d72024-02-09 20:20:42 +0100256 int ret;
Igor Opaniuk60291192018-06-03 21:56:39 +0300257
258 bool unlocked = false;
259 int res = CMD_RET_FAILURE;
260
261 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +0100262 printf("AVB is not initialized, please run 'avb init <id>'\n");
Igor Opaniuk60291192018-06-03 21:56:39 +0300263 return CMD_RET_FAILURE;
264 }
265
Sam Protsenkof6b91bf2019-10-21 13:55:16 +0300266 if (argc < 1 || argc > 2)
Igor Opaniuk60291192018-06-03 21:56:39 +0300267 return CMD_RET_USAGE;
268
Sam Protsenkof6b91bf2019-10-21 13:55:16 +0300269 if (argc == 2)
270 slot_suffix = argv[1];
271
Igor Opaniuk60291192018-06-03 21:56:39 +0300272 printf("## Android Verified Boot 2.0 version %s\n",
273 avb_version_string());
274
Igor Opaniuka4669d72024-02-09 20:20:42 +0100275 ret = avb_ops->read_is_device_unlocked(avb_ops, &unlocked);
276 if (ret != AVB_IO_RESULT_OK) {
277 printf("Can't determine device lock state, err = %d\n",
278 ret);
Igor Opaniuk60291192018-06-03 21:56:39 +0300279 return CMD_RET_FAILURE;
280 }
281
282 slot_result =
283 avb_slot_verify(avb_ops,
284 requested_partitions,
Sam Protsenkof6b91bf2019-10-21 13:55:16 +0300285 slot_suffix,
Igor Opaniuk60291192018-06-03 21:56:39 +0300286 unlocked,
287 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
288 &out_data);
289
290 switch (slot_result) {
291 case AVB_SLOT_VERIFY_RESULT_OK:
Igor Opaniukf0f3bfe2018-06-03 21:56:40 +0300292 /* Until we don't have support of changing unlock states, we
293 * assume that we are by default in locked state.
294 * So in this case we can boot only when verification is
295 * successful; we also supply in cmdline GREEN boot state
296 */
Igor Opaniuk60291192018-06-03 21:56:39 +0300297 printf("Verification passed successfully\n");
298
299 /* export additional bootargs to AVB_BOOTARGS env var */
Igor Opaniukf0f3bfe2018-06-03 21:56:40 +0300300
301 extra_args = avb_set_state(avb_ops, AVB_GREEN);
302 if (extra_args)
303 cmdline = append_cmd_line(out_data->cmdline,
304 extra_args);
305 else
306 cmdline = out_data->cmdline;
307
308 env_set(AVB_BOOTARGS, cmdline);
Igor Opaniuk60291192018-06-03 21:56:39 +0300309
310 res = CMD_RET_SUCCESS;
311 break;
312 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
313 printf("Verification failed\n");
314 break;
315 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
316 printf("I/O error occurred during verification\n");
317 break;
318 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
319 printf("OOM error occurred during verification\n");
320 break;
321 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
322 printf("Corrupted dm-verity metadata detected\n");
323 break;
324 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
Igor Opaniuka4669d72024-02-09 20:20:42 +0100325 printf("Unsupported version of avbtool was used\n");
Igor Opaniuk60291192018-06-03 21:56:39 +0300326 break;
327 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
Igor Opaniuka4669d72024-02-09 20:20:42 +0100328 printf("Rollback index check failed\n");
Igor Opaniuk60291192018-06-03 21:56:39 +0300329 break;
330 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
331 printf("Public key was rejected\n");
332 break;
333 default:
334 printf("Unknown error occurred\n");
335 }
336
Gary Bissonb12df3a2020-05-11 12:11:53 +0200337 if (out_data)
338 avb_slot_verify_data_free(out_data);
339
Igor Opaniuk60291192018-06-03 21:56:39 +0300340 return res;
341}
342
Simon Glassed38aef2020-05-10 11:40:03 -0600343int do_avb_is_unlocked(struct cmd_tbl *cmdtp, int flag,
344 int argc, char *const argv[])
Igor Opaniuk60291192018-06-03 21:56:39 +0300345{
346 bool unlock;
Igor Opaniuka4669d72024-02-09 20:20:42 +0100347 int ret;
Igor Opaniuk60291192018-06-03 21:56:39 +0300348
349 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +0100350 printf("AVB is not initialized, please run 'avb init <id>'\n");
Igor Opaniuk60291192018-06-03 21:56:39 +0300351 return CMD_RET_FAILURE;
352 }
353
354 if (argc != 1) {
355 printf("--%s(-1)\n", __func__);
356 return CMD_RET_USAGE;
357 }
358
Igor Opaniuka4669d72024-02-09 20:20:42 +0100359 ret = avb_ops->read_is_device_unlocked(avb_ops, &unlock);
360 if (ret == AVB_IO_RESULT_OK) {
Igor Opaniuk60291192018-06-03 21:56:39 +0300361 printf("Unlocked = %d\n", unlock);
362 return CMD_RET_SUCCESS;
363 }
364
Igor Opaniuka4669d72024-02-09 20:20:42 +0100365 printf("Can't determine device lock state, err = %d\n",
366 ret);
Jens Wiklander6815afc2018-09-25 16:40:07 +0200367
Igor Opaniuk60291192018-06-03 21:56:39 +0300368 return CMD_RET_FAILURE;
369}
370
Simon Glassed38aef2020-05-10 11:40:03 -0600371int do_avb_read_pvalue(struct cmd_tbl *cmdtp, int flag, int argc,
372 char *const argv[])
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200373{
374 const char *name;
375 size_t bytes;
376 size_t bytes_read;
377 void *buffer;
378 char *endp;
Igor Opaniuka4669d72024-02-09 20:20:42 +0100379 int ret;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200380
381 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +0100382 printf("AVB is not initialized, please run 'avb init <id>'\n");
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200383 return CMD_RET_FAILURE;
384 }
385
386 if (argc != 3)
387 return CMD_RET_USAGE;
388
389 name = argv[1];
Simon Glassff9b9032021-07-24 09:03:30 -0600390 bytes = dectoul(argv[2], &endp);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200391 if (*endp && *endp != '\n')
392 return CMD_RET_USAGE;
393
394 buffer = malloc(bytes);
395 if (!buffer)
396 return CMD_RET_FAILURE;
397
Igor Opaniuka4669d72024-02-09 20:20:42 +0100398 ret = avb_ops->read_persistent_value(avb_ops, name, bytes,
399 buffer, &bytes_read);
400 if (ret == AVB_IO_RESULT_OK) {
Sam Protsenko69543512019-07-31 19:59:09 +0300401 printf("Read %zu bytes, value = %s\n", bytes_read,
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200402 (char *)buffer);
403 free(buffer);
404 return CMD_RET_SUCCESS;
405 }
406
Igor Opaniuka4669d72024-02-09 20:20:42 +0100407 printf("Failed to read persistent value, err = %d\n", ret);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200408
409 free(buffer);
410
411 return CMD_RET_FAILURE;
412}
413
Simon Glassed38aef2020-05-10 11:40:03 -0600414int do_avb_write_pvalue(struct cmd_tbl *cmdtp, int flag, int argc,
415 char *const argv[])
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200416{
417 const char *name;
418 const char *value;
Igor Opaniuka4669d72024-02-09 20:20:42 +0100419 int ret;
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200420
421 if (!avb_ops) {
Igor Opaniuka4669d72024-02-09 20:20:42 +0100422 printf("AVB is not initialized, please run 'avb init <id>'\n");
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200423 return CMD_RET_FAILURE;
424 }
425
426 if (argc != 3)
427 return CMD_RET_USAGE;
428
429 name = argv[1];
430 value = argv[2];
431
Igor Opaniuka4669d72024-02-09 20:20:42 +0100432 ret = avb_ops->write_persistent_value(avb_ops, name,
433 strlen(value) + 1,
434 (const uint8_t *)value);
435 if (ret == AVB_IO_RESULT_OK) {
Sam Protsenko69543512019-07-31 19:59:09 +0300436 printf("Wrote %zu bytes\n", strlen(value) + 1);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200437 return CMD_RET_SUCCESS;
438 }
439
Igor Opaniuka4669d72024-02-09 20:20:42 +0100440 printf("Failed to write persistent value `%s` = `%s`, err = %d\n",
441 name, value, ret);
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200442
443 return CMD_RET_FAILURE;
444}
445
Simon Glassed38aef2020-05-10 11:40:03 -0600446static struct cmd_tbl cmd_avb[] = {
Igor Opaniuk60291192018-06-03 21:56:39 +0300447 U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""),
448 U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""),
449 U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""),
450 U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""),
451 U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""),
452 U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""),
453 U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""),
454 U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""),
Sam Protsenkof6b91bf2019-10-21 13:55:16 +0300455 U_BOOT_CMD_MKENT(verify, 2, 0, do_avb_verify_part, "", ""),
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200456#ifdef CONFIG_OPTEE_TA_AVB
457 U_BOOT_CMD_MKENT(read_pvalue, 3, 0, do_avb_read_pvalue, "", ""),
458 U_BOOT_CMD_MKENT(write_pvalue, 3, 0, do_avb_write_pvalue, "", ""),
459#endif
Igor Opaniuk60291192018-06-03 21:56:39 +0300460};
461
Simon Glassed38aef2020-05-10 11:40:03 -0600462static int do_avb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Igor Opaniuk60291192018-06-03 21:56:39 +0300463{
Simon Glassed38aef2020-05-10 11:40:03 -0600464 struct cmd_tbl *cp;
Igor Opaniuk60291192018-06-03 21:56:39 +0300465
466 cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb));
467
468 argc--;
469 argv++;
470
471 if (!cp || argc > cp->maxargs)
472 return CMD_RET_USAGE;
473
474 if (flag == CMD_FLAG_REPEAT)
475 return CMD_RET_FAILURE;
476
477 return cp->cmd(cmdtp, flag, argc, argv);
478}
479
480U_BOOT_CMD(
481 avb, 29, 0, do_avb,
482 "Provides commands for testing Android Verified Boot 2.0 functionality",
483 "init <dev> - initialize avb2 for <dev>\n"
484 "avb read_rb <num> - read rollback index at location <num>\n"
485 "avb write_rb <num> <rb> - write rollback index <rb> to <num>\n"
486 "avb is_unlocked - returns unlock status of the device\n"
487 "avb get_uuid <partname> - read and print uuid of partition <part>\n"
488 "avb read_part <partname> <offset> <num> <addr> - read <num> bytes from\n"
489 " partition <partname> to buffer <addr>\n"
490 "avb read_part_hex <partname> <offset> <num> - read <num> bytes from\n"
491 " partition <partname> and print to stdout\n"
492 "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n"
493 " <partname> by <offset> using data from <addr>\n"
Igor Opaniuk78b0b9d2019-04-09 15:38:14 +0200494#ifdef CONFIG_OPTEE_TA_AVB
495 "avb read_pvalue <name> <bytes> - read a persistent value <name>\n"
496 "avb write_pvalue <name> <value> - write a persistent value <name>\n"
497#endif
Sam Protsenkof6b91bf2019-10-21 13:55:16 +0300498 "avb verify [slot_suffix] - run verification process using hash data\n"
Igor Opaniuk60291192018-06-03 21:56:39 +0300499 " from vbmeta structure\n"
Sam Protsenkof6b91bf2019-10-21 13:55:16 +0300500 " [slot_suffix] - _a, _b, etc (if vbmeta partition is slotted)\n"
Igor Opaniuk60291192018-06-03 21:56:39 +0300501 );