1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "avb_ops_user.h"
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <linux/fs.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/ioctl.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include <cutils/properties.h>
38 #include <fs_mgr.h>
39
40 #include <libavb_ab/libavb_ab.h>
41
42 using android::fs_mgr::Fstab;
43 using android::fs_mgr::GetEntryForMountPoint;
44 using android::fs_mgr::ReadDefaultFstab;
45 using android::fs_mgr::ReadFstabFromFile;
46
47 /* Open the appropriate fstab file and fallback to /fstab.device if
48 * that's what's being used.
49 */
open_fstab(Fstab * fstab)50 static bool open_fstab(Fstab* fstab) {
51 return ReadDefaultFstab(fstab) || ReadFstabFromFile("/fstab.device", fstab);
52 }
53
open_partition(const char * name,int flags)54 static int open_partition(const char* name, int flags) {
55 char* path;
56 int fd;
57
58 /* Per https://android-review.googlesource.com/c/platform/system/core/+/674989
59 * Android now supports /dev/block/by-name/<partition_name> ... try that
60 * first.
61 */
62 path = avb_strdupv("/dev/block/by-name/", name, NULL);
63 if (path != NULL) {
64 fd = open(path, flags);
65 avb_free(path);
66 if (fd != -1) {
67 return fd;
68 }
69 }
70
71 /* OK, so /dev/block/by-name/<partition_name> didn't work... so we're
72 * falling back to what we used to do before that:
73 *
74 * We can't use fs_mgr to look up |name| because fstab doesn't list
75 * every slot partition (it uses the slotselect option to mask the
76 * suffix) and |slot| is expected to be of that form, e.g. boot_a.
77 *
78 * We can however assume that there's an entry for the /misc mount
79 * point and use that to get the device file for the misc
80 * partition. From there we'll assume that a by-name scheme is used
81 * so we can just replace the trailing "misc" by the given |name|,
82 * e.g.
83 *
84 * /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
85 * /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
86 *
87 * If needed, it's possible to relax this assumption in the future
88 * by trawling /sys/block looking for the appropriate sibling of
89 * misc and then finding an entry in /dev matching the sysfs entry.
90 */
91
92 Fstab fstab;
93 if (!open_fstab(&fstab)) {
94 return -1;
95 }
96 auto record = GetEntryForMountPoint(&fstab, "/misc");
97 if (record == nullptr) {
98 return -1;
99 }
100 if (strcmp(name, "misc") == 0) {
101 path = strdup(record->blk_device.c_str());
102 } else {
103 size_t trimmed_len, name_len;
104 const char* end_slash = strrchr(record->blk_device.c_str(), '/');
105 if (end_slash == NULL) {
106 return -1;
107 }
108 trimmed_len = end_slash - record->blk_device.c_str() + 1;
109 name_len = strlen(name);
110 path = static_cast<char*>(calloc(trimmed_len + name_len + 1, 1));
111 strncpy(path, record->blk_device.c_str(), trimmed_len);
112 strncpy(path + trimmed_len, name, name_len);
113 }
114
115 fd = open(path, flags);
116 free(path);
117
118 return fd;
119 }
120
read_from_partition(AvbOps * ops,const char * partition,int64_t offset,size_t num_bytes,void * buffer,size_t * out_num_read)121 static AvbIOResult read_from_partition(AvbOps* ops,
122 const char* partition,
123 int64_t offset,
124 size_t num_bytes,
125 void* buffer,
126 size_t* out_num_read) {
127 int fd;
128 off_t where;
129 ssize_t num_read;
130 AvbIOResult ret;
131
132 fd = open_partition(partition, O_RDONLY);
133 if (fd == -1) {
134 ret = AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
135 goto out;
136 }
137
138 if (offset < 0) {
139 uint64_t partition_size;
140 if (ioctl(fd, BLKGETSIZE64, &partition_size) != 0) {
141 avb_error("Error getting size of \"", partition, "\" partition.\n");
142 ret = AVB_IO_RESULT_ERROR_IO;
143 goto out;
144 }
145 offset = partition_size - (-offset);
146 }
147
148 where = lseek(fd, offset, SEEK_SET);
149 if (where == -1) {
150 avb_error("Error seeking to offset.\n");
151 ret = AVB_IO_RESULT_ERROR_IO;
152 goto out;
153 }
154 if (where != offset) {
155 avb_error("Error seeking to offset.\n");
156 ret = AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION;
157 goto out;
158 }
159
160 /* On Linux, we never get partial reads from block devices (except
161 * for EOF).
162 */
163 num_read = read(fd, buffer, num_bytes);
164 if (num_read == -1) {
165 avb_error("Error reading data.\n");
166 ret = AVB_IO_RESULT_ERROR_IO;
167 goto out;
168 }
169 if (out_num_read != NULL) {
170 *out_num_read = num_read;
171 }
172
173 ret = AVB_IO_RESULT_OK;
174
175 out:
176 if (fd != -1) {
177 if (close(fd) != 0) {
178 avb_error("Error closing file descriptor.\n");
179 }
180 }
181 return ret;
182 }
183
write_to_partition(AvbOps * ops,const char * partition,int64_t offset,size_t num_bytes,const void * buffer)184 static AvbIOResult write_to_partition(AvbOps* ops,
185 const char* partition,
186 int64_t offset,
187 size_t num_bytes,
188 const void* buffer) {
189 int fd;
190 off_t where;
191 ssize_t num_written;
192 AvbIOResult ret;
193
194 fd = open_partition(partition, O_WRONLY);
195 if (fd == -1) {
196 avb_error("Error opening \"", partition, "\" partition.\n");
197 ret = AVB_IO_RESULT_ERROR_IO;
198 goto out;
199 }
200
201 where = lseek(fd, offset, SEEK_SET);
202 if (where == -1) {
203 avb_error("Error seeking to offset.\n");
204 ret = AVB_IO_RESULT_ERROR_IO;
205 goto out;
206 }
207 if (where != offset) {
208 avb_error("Error seeking to offset.\n");
209 ret = AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION;
210 goto out;
211 }
212
213 /* On Linux, we never get partial writes on block devices. */
214 num_written = write(fd, buffer, num_bytes);
215 if (num_written == -1) {
216 avb_error("Error writing data.\n");
217 ret = AVB_IO_RESULT_ERROR_IO;
218 goto out;
219 }
220
221 ret = AVB_IO_RESULT_OK;
222
223 out:
224 if (fd != -1) {
225 if (close(fd) != 0) {
226 avb_error("Error closing file descriptor.\n");
227 }
228 }
229 return ret;
230 }
231
validate_vbmeta_public_key(AvbOps * ops,const uint8_t * public_key_data,size_t public_key_length,const uint8_t * public_key_metadata,size_t public_key_metadata_length,bool * out_is_trusted)232 static AvbIOResult validate_vbmeta_public_key(
233 AvbOps* ops,
234 const uint8_t* public_key_data,
235 size_t public_key_length,
236 const uint8_t* public_key_metadata,
237 size_t public_key_metadata_length,
238 bool* out_is_trusted) {
239 if (out_is_trusted != NULL) {
240 *out_is_trusted = true;
241 }
242 return AVB_IO_RESULT_OK;
243 }
244
read_rollback_index(AvbOps * ops,size_t rollback_index_location,uint64_t * out_rollback_index)245 static AvbIOResult read_rollback_index(AvbOps* ops,
246 size_t rollback_index_location,
247 uint64_t* out_rollback_index) {
248 if (out_rollback_index != NULL) {
249 *out_rollback_index = 0;
250 }
251 return AVB_IO_RESULT_OK;
252 }
253
write_rollback_index(AvbOps * ops,size_t rollback_index_location,uint64_t rollback_index)254 static AvbIOResult write_rollback_index(AvbOps* ops,
255 size_t rollback_index_location,
256 uint64_t rollback_index) {
257 return AVB_IO_RESULT_OK;
258 }
259
read_is_device_unlocked(AvbOps * ops,bool * out_is_unlocked)260 static AvbIOResult read_is_device_unlocked(AvbOps* ops, bool* out_is_unlocked) {
261 if (out_is_unlocked != NULL) {
262 *out_is_unlocked = true;
263 }
264 return AVB_IO_RESULT_OK;
265 }
266
get_size_of_partition(AvbOps * ops,const char * partition,uint64_t * out_size_in_bytes)267 static AvbIOResult get_size_of_partition(AvbOps* ops,
268 const char* partition,
269 uint64_t* out_size_in_bytes) {
270 int fd;
271 AvbIOResult ret;
272
273 fd = open_partition(partition, O_RDONLY);
274 if (fd == -1) {
275 avb_error("Error opening \"", partition, "\" partition.\n");
276 ret = AVB_IO_RESULT_ERROR_IO;
277 goto out;
278 }
279
280 if (out_size_in_bytes != NULL) {
281 if (ioctl(fd, BLKGETSIZE64, out_size_in_bytes) != 0) {
282 avb_error("Error getting size of \"", partition, "\" partition.\n");
283 ret = AVB_IO_RESULT_ERROR_IO;
284 goto out;
285 }
286 }
287
288 ret = AVB_IO_RESULT_OK;
289
290 out:
291 if (fd != -1) {
292 if (close(fd) != 0) {
293 avb_error("Error closing file descriptor.\n");
294 }
295 }
296 return ret;
297 }
298
get_unique_guid_for_partition(AvbOps * ops,const char * partition,char * guid_buf,size_t guid_buf_size)299 static AvbIOResult get_unique_guid_for_partition(AvbOps* ops,
300 const char* partition,
301 char* guid_buf,
302 size_t guid_buf_size) {
303 if (guid_buf != NULL && guid_buf_size > 0) {
304 guid_buf[0] = '\0';
305 }
306 return AVB_IO_RESULT_OK;
307 }
308
avb_ops_user_new(void)309 AvbOps* avb_ops_user_new(void) {
310 AvbOps* ops;
311
312 ops = static_cast<AvbOps*>(calloc(1, sizeof(AvbOps)));
313 if (ops == NULL) {
314 avb_error("Error allocating memory for AvbOps.\n");
315 goto out;
316 }
317
318 ops->ab_ops = static_cast<AvbABOps*>(calloc(1, sizeof(AvbABOps)));
319 if (ops->ab_ops == NULL) {
320 avb_error("Error allocating memory for AvbABOps.\n");
321 free(ops);
322 goto out;
323 }
324 ops->ab_ops->ops = ops;
325
326 ops->read_from_partition = read_from_partition;
327 ops->write_to_partition = write_to_partition;
328 ops->validate_vbmeta_public_key = validate_vbmeta_public_key;
329 ops->read_rollback_index = read_rollback_index;
330 ops->write_rollback_index = write_rollback_index;
331 ops->read_is_device_unlocked = read_is_device_unlocked;
332 ops->get_unique_guid_for_partition = get_unique_guid_for_partition;
333 ops->get_size_of_partition = get_size_of_partition;
334 ops->ab_ops->read_ab_metadata = avb_ab_data_read;
335 ops->ab_ops->write_ab_metadata = avb_ab_data_write;
336
337 out:
338 return ops;
339 }
340
avb_ops_user_free(AvbOps * ops)341 void avb_ops_user_free(AvbOps* ops) {
342 free(ops->ab_ops);
343 free(ops);
344 }
345