forked from open-vela/external_avb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavb_user_verification.c
208 lines (182 loc) · 6.88 KB
/
avb_user_verification.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
65
66
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
113
114
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "avb_user_verification.h"
/* Maximum allow length (in bytes) of a partition name, including
* ab_suffix.
*/
#define AVB_PART_NAME_MAX_SIZE 32
/* Loads the toplevel AvbVBMetaImageHeader from the slot denoted by
* |ab_suffix| into |vbmeta_image|. No validation, verification, or
* byteswapping is performed.
*
* If successful, |true| is returned and the partition it was loaded
* from is returned in |out_partition_name| and the offset on said
* partition is returned in |out_vbmeta_offset|.
*/
static bool load_top_level_vbmeta_header(
AvbOps* ops,
const char* ab_suffix,
uint8_t vbmeta_image[AVB_VBMETA_IMAGE_HEADER_SIZE],
char out_partition_name[AVB_PART_NAME_MAX_SIZE],
uint64_t* out_vbmeta_offset) {
uint64_t vbmeta_offset = 0;
size_t num_read;
bool ret = false;
AvbIOResult io_res;
/* Construct full partition name. */
if (!avb_str_concat(out_partition_name,
AVB_PART_NAME_MAX_SIZE,
"vbmeta",
6,
ab_suffix,
avb_strlen(ab_suffix))) {
avb_error("Partition name and suffix does not fit.\n");
goto out;
}
/* Only read the header, not the entire struct. */
io_res = ops->read_from_partition(ops,
out_partition_name,
vbmeta_offset,
AVB_VBMETA_IMAGE_HEADER_SIZE,
vbmeta_image,
&num_read);
if (io_res == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION) {
AvbFooter footer;
/* Try looking for the vbmeta struct in 'boot' via the footer. */
if (!avb_str_concat(out_partition_name,
AVB_PART_NAME_MAX_SIZE,
"boot",
4,
ab_suffix,
avb_strlen(ab_suffix))) {
avb_error("Partition name and suffix does not fit.\n");
goto out;
}
io_res = ops->read_from_partition(ops,
out_partition_name,
-AVB_FOOTER_SIZE,
AVB_FOOTER_SIZE,
&footer,
&num_read);
if (io_res != AVB_IO_RESULT_OK) {
avb_error(
"Error loading footer from partition '", out_partition_name, "'\n");
goto out;
}
if (avb_memcmp(footer.magic, AVB_FOOTER_MAGIC, AVB_FOOTER_MAGIC_LEN) != 0) {
avb_error("Data from '",
out_partition_name,
"' does not look like a vbmeta footer.\n");
goto out;
}
vbmeta_offset = avb_be64toh(footer.vbmeta_offset);
io_res = ops->read_from_partition(ops,
out_partition_name,
vbmeta_offset,
AVB_VBMETA_IMAGE_HEADER_SIZE,
vbmeta_image,
&num_read);
}
if (io_res != AVB_IO_RESULT_OK) {
avb_error("Error loading from partition '", out_partition_name, "'\n");
goto out;
}
if (out_vbmeta_offset != NULL) {
*out_vbmeta_offset = vbmeta_offset;
}
ret = true;
out:
return ret;
}
bool avb_user_verification_get(AvbOps* ops,
const char* ab_suffix,
bool* out_verification_enabled) {
uint8_t vbmeta_image[AVB_VBMETA_IMAGE_HEADER_SIZE]; /* 256 bytes. */
char partition_name[AVB_PART_NAME_MAX_SIZE]; /* 32 bytes. */
AvbVBMetaImageHeader* header;
uint32_t flags;
bool ret = false;
if (!load_top_level_vbmeta_header(
ops, ab_suffix, vbmeta_image, partition_name, NULL)) {
goto out;
}
if (avb_memcmp(vbmeta_image, AVB_MAGIC, AVB_MAGIC_LEN) != 0) {
avb_error("Data from '",
partition_name,
"' does not look like a vbmeta header.\n");
goto out;
}
/* Set/clear the VERIFICATION_DISABLED bit, as requested. */
header = (AvbVBMetaImageHeader*)vbmeta_image;
flags = avb_be32toh(header->flags);
if (out_verification_enabled != NULL) {
*out_verification_enabled =
!(flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED);
}
ret = true;
out:
return ret;
}
bool avb_user_verification_set(AvbOps* ops,
const char* ab_suffix,
bool enable_verification) {
uint8_t vbmeta_image[AVB_VBMETA_IMAGE_HEADER_SIZE]; /* 256 bytes. */
char partition_name[AVB_PART_NAME_MAX_SIZE]; /* 32 bytes. */
uint64_t vbmeta_offset;
AvbIOResult io_res;
AvbVBMetaImageHeader* header;
uint32_t flags;
bool ret = false;
if (!load_top_level_vbmeta_header(
ops, ab_suffix, vbmeta_image, partition_name, &vbmeta_offset)) {
goto out;
}
if (avb_memcmp(vbmeta_image, AVB_MAGIC, AVB_MAGIC_LEN) != 0) {
avb_error("Data from '",
partition_name,
"' does not look like a vbmeta header.\n");
goto out;
}
/* Set/clear the VERIFICATION_DISABLED bit, as requested. */
header = (AvbVBMetaImageHeader*)vbmeta_image;
flags = avb_be32toh(header->flags);
flags &= ~AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED;
if (!enable_verification) {
flags |= AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED;
}
header->flags = avb_htobe32(flags);
/* Write the header. */
io_res = ops->write_to_partition(ops,
partition_name,
vbmeta_offset,
AVB_VBMETA_IMAGE_HEADER_SIZE,
vbmeta_image);
if (io_res != AVB_IO_RESULT_OK) {
avb_error("Error writing to partition '", partition_name, "'\n");
goto out;
}
ret = true;
out:
return ret;
}