Blame view

cmd/avb.c 11.1 KB
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
1
2
3
4
5
6
7
8
9
  
  /*
   * (C) Copyright 2018, Linaro Limited
   *
   * SPDX-License-Identifier:	GPL-2.0+
   */
  
  #include <avb_verify.h>
  #include <command.h>
9fb625ce0   Simon Glass   env: Move env_set...
10
  #include <env.h>
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
11
12
13
14
15
16
  #include <image.h>
  #include <malloc.h>
  #include <mmc.h>
  
  #define AVB_BOOTARGS	"avb_bootargs"
  static struct AvbOps *avb_ops;
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  int do_avb_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	unsigned long mmc_dev;
  
  	if (argc != 2)
  		return CMD_RET_USAGE;
  
  	mmc_dev = simple_strtoul(argv[1], NULL, 16);
  
  	if (avb_ops)
  		avb_ops_free(avb_ops);
  
  	avb_ops = avb_ops_alloc(mmc_dev);
  	if (avb_ops)
  		return CMD_RET_SUCCESS;
6d89902d7   Jens Wiklander   cmd: avb: print e...
32
33
  	printf("Failed to initialize avb2
  ");
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  	return CMD_RET_FAILURE;
  }
  
  int do_avb_read_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	const char *part;
  	s64 offset;
  	size_t bytes, bytes_read = 0;
  	void *buffer;
  
  	if (!avb_ops) {
  		printf("AVB 2.0 is not initialized, please run 'avb init'
  ");
  		return CMD_RET_USAGE;
  	}
  
  	if (argc != 5)
  		return CMD_RET_USAGE;
  
  	part = argv[1];
  	offset = simple_strtoul(argv[2], NULL, 16);
  	bytes = simple_strtoul(argv[3], NULL, 16);
  	buffer = (void *)simple_strtoul(argv[4], NULL, 16);
  
  	if (avb_ops->read_from_partition(avb_ops, part, offset, bytes,
  					 buffer, &bytes_read) ==
  					 AVB_IO_RESULT_OK) {
  		printf("Read %zu bytes
  ", bytes_read);
  		return CMD_RET_SUCCESS;
  	}
6d89902d7   Jens Wiklander   cmd: avb: print e...
65
66
  	printf("Failed to read from partition
  ");
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  	return CMD_RET_FAILURE;
  }
  
  int do_avb_read_part_hex(cmd_tbl_t *cmdtp, int flag, int argc,
  			 char *const argv[])
  {
  	const char *part;
  	s64 offset;
  	size_t bytes, bytes_read = 0;
  	char *buffer;
  
  	if (!avb_ops) {
  		printf("AVB 2.0 is not initialized, please run 'avb init'
  ");
  		return CMD_RET_USAGE;
  	}
  
  	if (argc != 4)
  		return CMD_RET_USAGE;
  
  	part = argv[1];
  	offset = simple_strtoul(argv[2], NULL, 16);
  	bytes = simple_strtoul(argv[3], NULL, 16);
  
  	buffer = malloc(bytes);
  	if (!buffer) {
  		printf("Failed to tlb_allocate buffer for data
  ");
  		return CMD_RET_FAILURE;
  	}
  	memset(buffer, 0, bytes);
  
  	if (avb_ops->read_from_partition(avb_ops, part, offset, bytes, buffer,
  					 &bytes_read) == AVB_IO_RESULT_OK) {
  		printf("Requested %zu, read %zu bytes
  ", bytes, bytes_read);
  		printf("Data: ");
  		for (int i = 0; i < bytes_read; i++)
  			printf("%02X", buffer[i]);
  
  		printf("
  ");
  
  		free(buffer);
  		return CMD_RET_SUCCESS;
  	}
6d89902d7   Jens Wiklander   cmd: avb: print e...
113
114
  	printf("Failed to read from partition
  ");
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  	free(buffer);
  	return CMD_RET_FAILURE;
  }
  
  int do_avb_write_part(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	const char *part;
  	s64 offset;
  	size_t bytes;
  	void *buffer;
  
  	if (!avb_ops) {
  		printf("AVB 2.0 is not initialized, run 'avb init' first
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	if (argc != 5)
  		return CMD_RET_USAGE;
  
  	part = argv[1];
  	offset = simple_strtoul(argv[2], NULL, 16);
  	bytes = simple_strtoul(argv[3], NULL, 16);
  	buffer = (void *)simple_strtoul(argv[4], NULL, 16);
  
  	if (avb_ops->write_to_partition(avb_ops, part, offset, bytes, buffer) ==
  	    AVB_IO_RESULT_OK) {
  		printf("Wrote %zu bytes
  ", bytes);
  		return CMD_RET_SUCCESS;
  	}
6d89902d7   Jens Wiklander   cmd: avb: print e...
146
147
  	printf("Failed to write in partition
  ");
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  	return CMD_RET_FAILURE;
  }
  
  int do_avb_read_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	size_t index;
  	u64 rb_idx;
  
  	if (!avb_ops) {
  		printf("AVB 2.0 is not initialized, run 'avb init' first
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	if (argc != 2)
  		return CMD_RET_USAGE;
  
  	index = (size_t)simple_strtoul(argv[1], NULL, 16);
  
  	if (avb_ops->read_rollback_index(avb_ops, index, &rb_idx) ==
  	    AVB_IO_RESULT_OK) {
ab2d73823   Jens Wiklander   cmd: avb read_rb:...
169
170
  		printf("Rollback index: %llx
  ", rb_idx);
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
171
172
  		return CMD_RET_SUCCESS;
  	}
6d89902d7   Jens Wiklander   cmd: avb: print e...
173
174
175
  
  	printf("Failed to read rollback index
  ");
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  	return CMD_RET_FAILURE;
  }
  
  int do_avb_write_rb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	size_t index;
  	u64 rb_idx;
  
  	if (!avb_ops) {
  		printf("AVB 2.0 is not initialized, run 'avb init' first
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	if (argc != 3)
  		return CMD_RET_USAGE;
  
  	index = (size_t)simple_strtoul(argv[1], NULL, 16);
  	rb_idx = simple_strtoul(argv[2], NULL, 16);
  
  	if (avb_ops->write_rollback_index(avb_ops, index, rb_idx) ==
  	    AVB_IO_RESULT_OK)
  		return CMD_RET_SUCCESS;
6d89902d7   Jens Wiklander   cmd: avb: print e...
199
200
  	printf("Failed to write rollback index
  ");
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  	return CMD_RET_FAILURE;
  }
  
  int do_avb_get_uuid(cmd_tbl_t *cmdtp, int flag,
  		    int argc, char * const argv[])
  {
  	const char *part;
  	char buffer[UUID_STR_LEN + 1];
  
  	if (!avb_ops) {
  		printf("AVB 2.0 is not initialized, run 'avb init' first
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	if (argc != 2)
  		return CMD_RET_USAGE;
  
  	part = argv[1];
  
  	if (avb_ops->get_unique_guid_for_partition(avb_ops, part, buffer,
  						   UUID_STR_LEN + 1) ==
  						   AVB_IO_RESULT_OK) {
  		printf("'%s' UUID: %s
  ", part, buffer);
  		return CMD_RET_SUCCESS;
  	}
6d89902d7   Jens Wiklander   cmd: avb: print e...
228
229
  	printf("Failed to read UUID
  ");
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
230
231
232
233
234
235
  	return CMD_RET_FAILURE;
  }
  
  int do_avb_verify_part(cmd_tbl_t *cmdtp, int flag,
  		       int argc, char *const argv[])
  {
bb43c2784   Sam Protsenko   cmd: avb: Fix req...
236
  	const char * const requested_partitions[] = {"boot", NULL};
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
237
238
  	AvbSlotVerifyResult slot_result;
  	AvbSlotVerifyData *out_data;
5d4fd8777   Igor Opaniuk   avb2.0: add boot ...
239
240
  	char *cmdline;
  	char *extra_args;
965ec3caa   Sam Protsenko   cmd: avb: Support...
241
  	char *slot_suffix = "";
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
242
243
244
245
246
247
248
249
250
  
  	bool unlocked = false;
  	int res = CMD_RET_FAILURE;
  
  	if (!avb_ops) {
  		printf("AVB 2.0 is not initialized, run 'avb init' first
  ");
  		return CMD_RET_FAILURE;
  	}
965ec3caa   Sam Protsenko   cmd: avb: Support...
251
  	if (argc < 1 || argc > 2)
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
252
  		return CMD_RET_USAGE;
965ec3caa   Sam Protsenko   cmd: avb: Support...
253
254
  	if (argc == 2)
  		slot_suffix = argv[1];
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  	printf("## Android Verified Boot 2.0 version %s
  ",
  	       avb_version_string());
  
  	if (avb_ops->read_is_device_unlocked(avb_ops, &unlocked) !=
  	    AVB_IO_RESULT_OK) {
  		printf("Can't determine device lock state.
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	slot_result =
  		avb_slot_verify(avb_ops,
  				requested_partitions,
965ec3caa   Sam Protsenko   cmd: avb: Support...
269
  				slot_suffix,
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
270
271
272
273
274
275
  				unlocked,
  				AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
  				&out_data);
  
  	switch (slot_result) {
  	case AVB_SLOT_VERIFY_RESULT_OK:
5d4fd8777   Igor Opaniuk   avb2.0: add boot ...
276
277
278
279
280
  		/* Until we don't have support of changing unlock states, we
  		 * assume that we are by default in locked state.
  		 * So in this case we can boot only when verification is
  		 * successful; we also supply in cmdline GREEN boot state
  		 */
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
281
282
283
284
  		printf("Verification passed successfully
  ");
  
  		/* export additional bootargs to AVB_BOOTARGS env var */
5d4fd8777   Igor Opaniuk   avb2.0: add boot ...
285
286
287
288
289
290
291
292
293
  
  		extra_args = avb_set_state(avb_ops, AVB_GREEN);
  		if (extra_args)
  			cmdline = append_cmd_line(out_data->cmdline,
  						  extra_args);
  		else
  			cmdline = out_data->cmdline;
  
  		env_set(AVB_BOOTARGS, cmdline);
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  
  		res = CMD_RET_SUCCESS;
  		break;
  	case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
  		printf("Verification failed
  ");
  		break;
  	case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
  		printf("I/O error occurred during verification
  ");
  		break;
  	case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
  		printf("OOM error occurred during verification
  ");
  		break;
  	case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
  		printf("Corrupted dm-verity metadata detected
  ");
  		break;
  	case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
  		printf("Unsupported version avbtool was used
  ");
  		break;
  	case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
  		printf("Checking rollback index failed
  ");
  		break;
  	case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
  		printf("Public key was rejected
  ");
  		break;
  	default:
  		printf("Unknown error occurred
  ");
  	}
  
  	return res;
  }
  
  int do_avb_is_unlocked(cmd_tbl_t *cmdtp, int flag,
  		       int argc, char * const argv[])
  {
  	bool unlock;
  
  	if (!avb_ops) {
  		printf("AVB not initialized, run 'avb init' first
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	if (argc != 1) {
  		printf("--%s(-1)
  ", __func__);
  		return CMD_RET_USAGE;
  	}
  
  	if (avb_ops->read_is_device_unlocked(avb_ops, &unlock) ==
  	    AVB_IO_RESULT_OK) {
  		printf("Unlocked = %d
  ", unlock);
  		return CMD_RET_SUCCESS;
  	}
6d89902d7   Jens Wiklander   cmd: avb: print e...
356
357
  	printf("Can't determine device lock state.
  ");
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
358
359
  	return CMD_RET_FAILURE;
  }
fc1fe01b0   Igor Opaniuk   avb: add support ...
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
  int do_avb_read_pvalue(cmd_tbl_t *cmdtp, int flag, int argc,
  		       char * const argv[])
  {
  	const char *name;
  	size_t bytes;
  	size_t bytes_read;
  	void *buffer;
  	char *endp;
  
  	if (!avb_ops) {
  		printf("AVB 2.0 is not initialized, run 'avb init' first
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	if (argc != 3)
  		return CMD_RET_USAGE;
  
  	name = argv[1];
  	bytes = simple_strtoul(argv[2], &endp, 10);
  	if (*endp && *endp != '
  ')
  		return CMD_RET_USAGE;
  
  	buffer = malloc(bytes);
  	if (!buffer)
  		return CMD_RET_FAILURE;
  
  	if (avb_ops->read_persistent_value(avb_ops, name, bytes, buffer,
  					   &bytes_read) == AVB_IO_RESULT_OK) {
97f3c0970   Sam Protsenko   cmd: avb: Fix com...
390
391
  		printf("Read %zu bytes, value = %s
  ", bytes_read,
fc1fe01b0   Igor Opaniuk   avb: add support ...
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  		       (char *)buffer);
  		free(buffer);
  		return CMD_RET_SUCCESS;
  	}
  
  	printf("Failed to read persistent value
  ");
  
  	free(buffer);
  
  	return CMD_RET_FAILURE;
  }
  
  int do_avb_write_pvalue(cmd_tbl_t *cmdtp, int flag, int argc,
  			char * const argv[])
  {
  	const char *name;
  	const char *value;
  
  	if (!avb_ops) {
  		printf("AVB 2.0 is not initialized, run 'avb init' first
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	if (argc != 3)
  		return CMD_RET_USAGE;
  
  	name = argv[1];
  	value = argv[2];
  
  	if (avb_ops->write_persistent_value(avb_ops, name, strlen(value) + 1,
  					    (const uint8_t *)value) ==
  	    AVB_IO_RESULT_OK) {
97f3c0970   Sam Protsenko   cmd: avb: Fix com...
426
427
  		printf("Wrote %zu bytes
  ", strlen(value) + 1);
fc1fe01b0   Igor Opaniuk   avb: add support ...
428
429
430
431
432
433
434
435
  		return CMD_RET_SUCCESS;
  	}
  
  	printf("Failed to write persistent value
  ");
  
  	return CMD_RET_FAILURE;
  }
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
436
437
438
439
440
441
442
443
444
  static cmd_tbl_t cmd_avb[] = {
  	U_BOOT_CMD_MKENT(init, 2, 0, do_avb_init, "", ""),
  	U_BOOT_CMD_MKENT(read_rb, 2, 0, do_avb_read_rb, "", ""),
  	U_BOOT_CMD_MKENT(write_rb, 3, 0, do_avb_write_rb, "", ""),
  	U_BOOT_CMD_MKENT(is_unlocked, 1, 0, do_avb_is_unlocked, "", ""),
  	U_BOOT_CMD_MKENT(get_uuid, 2, 0, do_avb_get_uuid, "", ""),
  	U_BOOT_CMD_MKENT(read_part, 5, 0, do_avb_read_part, "", ""),
  	U_BOOT_CMD_MKENT(read_part_hex, 4, 0, do_avb_read_part_hex, "", ""),
  	U_BOOT_CMD_MKENT(write_part, 5, 0, do_avb_write_part, "", ""),
965ec3caa   Sam Protsenko   cmd: avb: Support...
445
  	U_BOOT_CMD_MKENT(verify, 2, 0, do_avb_verify_part, "", ""),
fc1fe01b0   Igor Opaniuk   avb: add support ...
446
447
448
449
  #ifdef CONFIG_OPTEE_TA_AVB
  	U_BOOT_CMD_MKENT(read_pvalue, 3, 0, do_avb_read_pvalue, "", ""),
  	U_BOOT_CMD_MKENT(write_pvalue, 3, 0, do_avb_write_pvalue, "", ""),
  #endif
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
  };
  
  static int do_avb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	cmd_tbl_t *cp;
  
  	cp = find_cmd_tbl(argv[1], cmd_avb, ARRAY_SIZE(cmd_avb));
  
  	argc--;
  	argv++;
  
  	if (!cp || argc > cp->maxargs)
  		return CMD_RET_USAGE;
  
  	if (flag == CMD_FLAG_REPEAT)
  		return CMD_RET_FAILURE;
  
  	return cp->cmd(cmdtp, flag, argc, argv);
  }
  
  U_BOOT_CMD(
  	avb, 29, 0, do_avb,
  	"Provides commands for testing Android Verified Boot 2.0 functionality",
  	"init <dev> - initialize avb2 for <dev>
  "
  	"avb read_rb <num> - read rollback index at location <num>
  "
  	"avb write_rb <num> <rb> - write rollback index <rb> to <num>
  "
  	"avb is_unlocked - returns unlock status of the device
  "
  	"avb get_uuid <partname> - read and print uuid of partition <part>
  "
  	"avb read_part <partname> <offset> <num> <addr> - read <num> bytes from
  "
  	"    partition <partname> to buffer <addr>
  "
  	"avb read_part_hex <partname> <offset> <num> - read <num> bytes from
  "
  	"    partition <partname> and print to stdout
  "
  	"avb write_part <partname> <offset> <num> <addr> - write <num> bytes to
  "
  	"    <partname> by <offset> using data from <addr>
  "
fc1fe01b0   Igor Opaniuk   avb: add support ...
495
496
497
498
499
500
  #ifdef CONFIG_OPTEE_TA_AVB
  	"avb read_pvalue <name> <bytes> - read a persistent value <name>
  "
  	"avb write_pvalue <name> <value> - write a persistent value <name>
  "
  #endif
965ec3caa   Sam Protsenko   cmd: avb: Support...
501
502
  	"avb verify [slot_suffix] - run verification process using hash data
  "
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
503
504
  	"    from vbmeta structure
  "
965ec3caa   Sam Protsenko   cmd: avb: Support...
505
506
  	"    [slot_suffix] - _a, _b, etc (if vbmeta partition is slotted)
  "
60b2f9e7b   Igor Opaniuk   cmd: avb2.0: avb ...
507
  	);