]> git.sur5r.net Git - u-boot/blob - cmd/avb.c
cmd: avb2.0: avb command for performing verification
[u-boot] / cmd / avb.c
1
2 /*
3  * (C) Copyright 2018, Linaro Limited
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <avb_verify.h>
9 #include <command.h>
10 #include <image.h>
11 #include <malloc.h>
12 #include <mmc.h>
13
14 #define AVB_BOOTARGS    "avb_bootargs"
15 static struct AvbOps *avb_ops;
16
17 static const char * const requested_partitions[] = {"boot",
18                                              "system",
19                                              "vendor",
20                                              NULL};
21
22 int do_avb_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
23 {
24         unsigned long mmc_dev;
25
26         if (argc != 2)
27                 return CMD_RET_USAGE;
28
29         mmc_dev = simple_strtoul(argv[1], NULL, 16);
30
31         if (avb_ops)
32                 avb_ops_free(avb_ops);
33
34         avb_ops = avb_ops_alloc(mmc_dev);
35         if (avb_ops)
36                 return CMD_RET_SUCCESS;
37
38         return CMD_RET_FAILURE;
39 }
40
41 int do_avb_read_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
42 {
43         const char *part;
44         s64 offset;
45         size_t bytes, bytes_read = 0;
46         void *buffer;
47
48         if (!avb_ops) {
49                 printf("AVB 2.0 is not initialized, please run 'avb init'\n");
50                 return CMD_RET_USAGE;
51         }
52
53         if (argc != 5)
54                 return CMD_RET_USAGE;
55
56         part = argv[1];
57         offset = simple_strtoul(argv[2], NULL, 16);
58         bytes = simple_strtoul(argv[3], NULL, 16);
59         buffer = (void *)simple_strtoul(argv[4], NULL, 16);
60
61         if (avb_ops->read_from_partition(avb_ops, part, offset, bytes,
62                                          buffer, &bytes_read) ==
63                                          AVB_IO_RESULT_OK) {
64                 printf("Read %zu bytes\n", bytes_read);
65                 return CMD_RET_SUCCESS;
66         }
67
68         return CMD_RET_FAILURE;
69 }
70
71 int do_avb_read_part_hex(cmd_tbl_t *cmdtp, int flag, int argc,
72                          char *const argv[])
73 {
74         const char *part;
75         s64 offset;
76         size_t bytes, bytes_read = 0;
77         char *buffer;
78
79         if (!avb_ops) {
80                 printf("AVB 2.0 is not initialized, please run 'avb init'\n");
81                 return CMD_RET_USAGE;
82         }
83
84         if (argc != 4)
85                 return CMD_RET_USAGE;
86
87         part = argv[1];
88         offset = simple_strtoul(argv[2], NULL, 16);
89         bytes = simple_strtoul(argv[3], NULL, 16);
90
91         buffer = malloc(bytes);
92         if (!buffer) {
93                 printf("Failed to tlb_allocate buffer for data\n");
94                 return CMD_RET_FAILURE;
95         }
96         memset(buffer, 0, bytes);
97
98         if (avb_ops->read_from_partition(avb_ops, part, offset, bytes, buffer,
99                                          &bytes_read) == AVB_IO_RESULT_OK) {
100                 printf("Requested %zu, read %zu bytes\n", bytes, bytes_read);
101                 printf("Data: ");
102                 for (int i = 0; i < bytes_read; i++)
103                         printf("%02X", buffer[i]);
104
105                 printf("\n");
106
107                 free(buffer);
108                 return CMD_RET_SUCCESS;
109         }
110
111         free(buffer);
112         return CMD_RET_FAILURE;
113 }
114
115 int do_avb_write_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
116 {
117         const char *part;
118         s64 offset;
119         size_t bytes;
120         void *buffer;
121
122         if (!avb_ops) {
123                 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
124                 return CMD_RET_FAILURE;
125         }
126
127         if (argc != 5)
128                 return CMD_RET_USAGE;
129
130         part = argv[1];
131         offset = simple_strtoul(argv[2], NULL, 16);
132         bytes = simple_strtoul(argv[3], NULL, 16);
133         buffer = (void *)simple_strtoul(argv[4], NULL, 16);
134
135         if (avb_ops->write_to_partition(avb_ops, part, offset, bytes, buffer) ==
136             AVB_IO_RESULT_OK) {
137                 printf("Wrote %zu bytes\n", bytes);
138                 return CMD_RET_SUCCESS;
139         }
140
141         return CMD_RET_FAILURE;
142 }
143
144 int do_avb_read_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
145 {
146         size_t index;
147         u64 rb_idx;
148
149         if (!avb_ops) {
150                 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
151                 return CMD_RET_FAILURE;
152         }
153
154         if (argc != 2)
155                 return CMD_RET_USAGE;
156
157         index = (size_t)simple_strtoul(argv[1], NULL, 16);
158
159         if (avb_ops->read_rollback_index(avb_ops, index, &rb_idx) ==
160             AVB_IO_RESULT_OK) {
161                 printf("Rollback index: %llu\n", rb_idx);
162                 return CMD_RET_SUCCESS;
163         }
164         return CMD_RET_FAILURE;
165 }
166
167 int do_avb_write_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
168 {
169         size_t index;
170         u64 rb_idx;
171
172         if (!avb_ops) {
173                 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
174                 return CMD_RET_FAILURE;
175         }
176
177         if (argc != 3)
178                 return CMD_RET_USAGE;
179
180         index = (size_t)simple_strtoul(argv[1], NULL, 16);
181         rb_idx = simple_strtoul(argv[2], NULL, 16);
182
183         if (avb_ops->write_rollback_index(avb_ops, index, rb_idx) ==
184             AVB_IO_RESULT_OK)
185                 return CMD_RET_SUCCESS;
186
187         return CMD_RET_FAILURE;
188 }
189
190 int do_avb_get_uuid(cmd_tbl_t *cmdtp, int flag,
191                     int argc, char * const argv[])
192 {
193         const char *part;
194         char buffer[UUID_STR_LEN + 1];
195
196         if (!avb_ops) {
197                 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
198                 return CMD_RET_FAILURE;
199         }
200
201         if (argc != 2)
202                 return CMD_RET_USAGE;
203
204         part = argv[1];
205
206         if (avb_ops->get_unique_guid_for_partition(avb_ops, part, buffer,
207                                                    UUID_STR_LEN + 1) ==
208                                                    AVB_IO_RESULT_OK) {
209                 printf("'%s' UUID: %s\n", part, buffer);
210                 return CMD_RET_SUCCESS;
211         }
212
213         return CMD_RET_FAILURE;
214 }
215
216 int do_avb_verify_part(cmd_tbl_t *cmdtp, int flag,
217                        int argc, char *const argv[])
218 {
219         AvbSlotVerifyResult slot_result;
220         AvbSlotVerifyData *out_data;
221
222         bool unlocked = false;
223         int res = CMD_RET_FAILURE;
224
225         if (!avb_ops) {
226                 printf("AVB 2.0 is not initialized, run 'avb init' first\n");
227                 return CMD_RET_FAILURE;
228         }
229
230         if (argc != 1)
231                 return CMD_RET_USAGE;
232
233         printf("## Android Verified Boot 2.0 version %s\n",
234                avb_version_string());
235
236         if (avb_ops->read_is_device_unlocked(avb_ops, &unlocked) !=
237             AVB_IO_RESULT_OK) {
238                 printf("Can't determine device lock state.\n");
239                 return CMD_RET_FAILURE;
240         }
241
242         slot_result =
243                 avb_slot_verify(avb_ops,
244                                 requested_partitions,
245                                 "",
246                                 unlocked,
247                                 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
248                                 &out_data);
249
250         switch (slot_result) {
251         case AVB_SLOT_VERIFY_RESULT_OK:
252                 printf("Verification passed successfully\n");
253
254                 /* export additional bootargs to AVB_BOOTARGS env var */
255                 env_set(AVB_BOOTARGS, out_data->cmdline);
256
257                 res = CMD_RET_SUCCESS;
258                 break;
259         case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
260                 printf("Verification failed\n");
261                 break;
262         case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
263                 printf("I/O error occurred during verification\n");
264                 break;
265         case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
266                 printf("OOM error occurred during verification\n");
267                 break;
268         case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
269                 printf("Corrupted dm-verity metadata detected\n");
270                 break;
271         case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
272                 printf("Unsupported version avbtool was used\n");
273                 break;
274         case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
275                 printf("Checking rollback index failed\n");
276                 break;
277         case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
278                 printf("Public key was rejected\n");
279                 break;
280         default:
281                 printf("Unknown error occurred\n");
282         }
283
284         return res;
285 }
286
287 int do_avb_is_unlocked(cmd_tbl_t *cmdtp, int flag,
288                        int argc, char * const argv[])
289 {
290         bool unlock;
291
292         if (!avb_ops) {
293                 printf("AVB not initialized, run 'avb init' first\n");
294                 return CMD_RET_FAILURE;
295         }
296
297         if (argc != 1) {
298                 printf("--%s(-1)\n", __func__);
299                 return CMD_RET_USAGE;
300         }
301
302         if (avb_ops->read_is_device_unlocked(avb_ops, &unlock) ==
303             AVB_IO_RESULT_OK) {
304                 printf("Unlocked = %d\n", unlock);
305                 return CMD_RET_SUCCESS;
306         }
307
308         return CMD_RET_FAILURE;
309 }
310
311 static cmd_tbl_t cmd_avb[] = {
312         U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""),
313         U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""),
314         U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""),
315         U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""),
316         U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""),
317         U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""),
318         U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""),
319         U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""),
320         U_BOOT_CMD_MKENT(verify, 1, 0, do_avb_verify_part, "", ""),
321 };
322
323 static int do_avb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
324 {
325         cmd_tbl_t *cp;
326
327         cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb));
328
329         argc--;
330         argv++;
331
332         if (!cp || argc > cp->maxargs)
333                 return CMD_RET_USAGE;
334
335         if (flag == CMD_FLAG_REPEAT)
336                 return CMD_RET_FAILURE;
337
338         return cp->cmd(cmdtp, flag, argc, argv);
339 }
340
341 U_BOOT_CMD(
342         avb, 29, 0, do_avb,
343         "Provides commands for testing Android Verified Boot 2.0 functionality",
344         "init <dev> - initialize avb2 for <dev>\n"
345         "avb read_rb <num> - read rollback index at location <num>\n"
346         "avb write_rb <num> <rb> - write rollback index <rb> to <num>\n"
347         "avb is_unlocked - returns unlock status of the device\n"
348         "avb get_uuid <partname> - read and print uuid of partition <part>\n"
349         "avb read_part <partname> <offset> <num> <addr> - read <num> bytes from\n"
350         "    partition <partname> to buffer <addr>\n"
351         "avb read_part_hex <partname> <offset> <num> - read <num> bytes from\n"
352         "    partition <partname> and print to stdout\n"
353         "avb write_part <partname> <offset> <num> <addr> - write <num> bytes to\n"
354         "    <partname> by <offset> using data from <addr>\n"
355         "avb verify - run verification process using hash data\n"
356         "    from vbmeta structure\n"
357         );