blob: 08245627c668f5819adcd6572fd4eb4318f8dc4e [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2016 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#if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
26#error "Never include this file directly, include libavb.h instead."
27#endif
28
29#ifndef AVB_OPS_H_
30#define AVB_OPS_H_
31
32#include "avb_sysdeps.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/* Well-known names of named persistent values. */
39#define AVB_NPV_PERSISTENT_DIGEST_PREFIX "avb.persistent_digest."
40
41/* Return codes used for I/O operations.
42 *
43 * AVB_IO_RESULT_OK is returned if the requested operation was
44 * successful.
45 *
46 * AVB_IO_RESULT_ERROR_IO is returned if the underlying hardware (disk
47 * or other subsystem) encountered an I/O error.
48 *
49 * AVB_IO_RESULT_ERROR_OOM is returned if unable to allocate memory.
50 *
51 * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION is returned if the requested
52 * partition does not exist.
53 *
54 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION is returned if the
55 * range of bytes requested to be read or written is outside the range
56 * of the partition.
57 *
58 * AVB_IO_RESULT_ERROR_NO_SUCH_VALUE is returned if a named persistent value
59 * does not exist.
60 *
61 * AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE is returned if a named persistent
62 * value size is not supported or does not match the expected size.
63 *
64 * AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE is returned if a buffer is too small
65 * for the requested operation.
66 */
67typedef enum {
68 AVB_IO_RESULT_OK,
69 AVB_IO_RESULT_ERROR_OOM,
70 AVB_IO_RESULT_ERROR_IO,
71 AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION,
72 AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION,
73 AVB_IO_RESULT_ERROR_NO_SUCH_VALUE,
74 AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE,
75 AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE,
76}
77AvbIOResult;
78
79struct AvbOps;
80typedef struct AvbOps AvbOps;
81
82/* Forward-declaration of operations in libavb_ab. */
83struct AvbABOps;
84
85/* Forward-declaration of operations in libavb_atx. */
86struct AvbAtxOps;
87
88/* High-level operations/functions/methods that are platform
89 * dependent.
90 *
91 * Operations may be added in the future so when implementing it
92 * always make sure to zero out sizeof(AvbOps) bytes of the struct to
93 * ensure that unimplemented operations are set to NULL.
94 */
95struct AvbOps {
96 /* This pointer can be used by the application/bootloader using
97 * libavb and is typically used in each operation to get a pointer
98 * to platform-specific resources. It cannot be used by libraries.
99 */
100 void *user_data;
101
102 /* If libavb_ab is used, this should point to the
103 * AvbABOps. Otherwise it must be set to NULL.
104 */
105 struct AvbABOps *ab_ops;
106
107 /* If libavb_atx is used, this should point to the
108 * AvbAtxOps. Otherwise it must be set to NULL.
109 */
110 struct AvbAtxOps *atx_ops;
111
112 /* Reads |num_bytes| from offset |offset| from partition with name
113 * |partition| (NUL-terminated UTF-8 string). If |offset| is
114 * negative, its absolute value should be interpreted as the number
115 * of bytes from the end of the partition.
116 *
117 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
118 * there is no partition with the given name,
119 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
120 * |offset| is outside the partition, and AVB_IO_RESULT_ERROR_IO if
121 * there was an I/O error from the underlying I/O subsystem. If the
122 * operation succeeds as requested AVB_IO_RESULT_OK is returned and
123 * the data is available in |buffer|.
124 *
125 * The only time partial I/O may occur is if reading beyond the end
126 * of the partition. In this case the value returned in
127 * |out_num_read| may be smaller than |num_bytes|.
128 */
129 AvbIOResult (*read_from_partition)(AvbOps *ops,
130 const char *partition,
131 int64_t offset,
132 size_t num_bytes,
133 void *buffer,
134 size_t *out_num_read);
135
136 /* Gets the starting pointer of a partition that is pre-loaded in memory, and
137 * save it to |out_pointer|. The preloaded partition is expected to be
138 * |num_bytes|, where the actual preloaded byte count is returned in
139 * |out_num_bytes_preloaded|. |out_num_bytes_preloaded| must be no larger than
140 * |num_bytes|.
141 *
142 * This provides an alternative way to access a partition that is preloaded
143 * into memory without a full memory copy. When this function pointer is not
144 * set (has value NULL), or when the |out_pointer| is set to NULL as a result,
145 * |read_from_partition| will be used as the fallback. This function is mainly
146 * used for accessing the entire partition content to calculate its hash.
147 *
148 * Preloaded partition data must outlive the lifespan of the
149 * |AvbSlotVerifyData| structure that |avb_slot_verify| outputs.
150 */
151 AvbIOResult (*get_preloaded_partition)(AvbOps *ops,
152 const char *partition,
153 size_t num_bytes,
154 uint8_t **out_pointer,
155 size_t *out_num_bytes_preloaded);
156
157 /* Writes |num_bytes| from |bffer| at offset |offset| to partition
158 * with name |partition| (NUL-terminated UTF-8 string). If |offset|
159 * is negative, its absolute value should be interpreted as the
160 * number of bytes from the end of the partition.
161 *
162 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
163 * there is no partition with the given name,
164 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
165 * byterange goes outside the partition, and AVB_IO_RESULT_ERROR_IO
166 * if there was an I/O error from the underlying I/O subsystem. If
167 * the operation succeeds as requested AVB_IO_RESULT_OK is
168 * returned.
169 *
170 * This function never does any partial I/O, it either transfers all
171 * of the requested bytes or returns an error.
172 */
173 AvbIOResult (*write_to_partition)(AvbOps *ops,
174 const char *partition,
175 int64_t offset,
176 size_t num_bytes,
177 const void *buffer);
178
179 /* Checks if the given public key used to sign the 'vbmeta'
180 * partition is trusted. Boot loaders typically compare this with
181 * embedded key material generated with 'avbtool
182 * extract_public_key'.
183 *
184 * The public key is in the array pointed to by |public_key_data|
185 * and is of |public_key_length| bytes.
186 *
187 * If there is no public key metadata (set with the avbtool option
188 * --public_key_metadata) then |public_key_metadata| will be set to
189 * NULL. Otherwise this field points to the data which is
190 * |public_key_metadata_length| bytes long.
191 *
192 * If AVB_IO_RESULT_OK is returned then |out_is_trusted| is set -
193 * true if trusted or false if untrusted.
194 */
195 AvbIOResult (*validate_vbmeta_public_key)(AvbOps *ops,
196 const uint8_t *public_key_data,
197 size_t public_key_length,
198 const uint8_t *public_key_metadata,
199 size_t public_key_metadata_length,
200 bool *out_is_trusted);
201
202 /* Gets the rollback index corresponding to the location given by
203 * |rollback_index_location|. The value is returned in
204 * |out_rollback_index|. Returns AVB_IO_RESULT_OK if the rollback
205 * index was retrieved, otherwise an error code.
206 *
207 * A device may have a limited amount of rollback index locations (say,
208 * one or four) so may error out if |rollback_index_location| exceeds
209 * this number.
210 */
211 AvbIOResult (*read_rollback_index)(AvbOps *ops,
212 size_t rollback_index_location,
213 uint64_t *out_rollback_index);
214
215 /* Sets the rollback index corresponding to the location given by
216 * |rollback_index_location| to |rollback_index|. Returns
217 * AVB_IO_RESULT_OK if the rollback index was set, otherwise an
218 * error code.
219 *
220 * A device may have a limited amount of rollback index locations (say,
221 * one or four) so may error out if |rollback_index_location| exceeds
222 * this number.
223 */
224 AvbIOResult (*write_rollback_index)(AvbOps *ops,
225 size_t rollback_index_location,
226 uint64_t rollback_index);
227
228 /* Gets whether the device is unlocked. The value is returned in
229 * |out_is_unlocked| (true if unlocked, false otherwise). Returns
230 * AVB_IO_RESULT_OK if the state was retrieved, otherwise an error
231 * code.
232 */
233 AvbIOResult (*read_is_device_unlocked)(AvbOps *ops, bool *out_is_unlocked);
234
235 /* Gets the unique partition GUID for a partition with name in
236 * |partition| (NUL-terminated UTF-8 string). The GUID is copied as
237 * a string into |guid_buf| of size |guid_buf_size| and will be NUL
238 * terminated. The string must be lower-case and properly
239 * hyphenated. For example:
240 *
241 * 527c1c6d-6361-4593-8842-3c78fcd39219
242 *
243 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
244 */
245 AvbIOResult (*get_unique_guid_for_partition)(AvbOps *ops,
246 const char *partition,
247 char *guid_buf,
248 size_t guid_buf_size);
249
250 /* Gets the size of a partition with the name in |partition|
251 * (NUL-terminated UTF-8 string). Returns the value in
252 * |out_size_num_bytes|.
253 *
254 * If the partition doesn't exist the AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION
255 * error code should be returned.
256 *
257 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
258 */
259 AvbIOResult (*get_size_of_partition)(AvbOps *ops,
260 const char *partition,
261 uint64_t *out_size_num_bytes);
262
263 /* Reads a persistent value corresponding to the given |name|. The value is
264 * returned in |out_buffer| which must point to |buffer_size| bytes. On
265 * success |out_num_bytes_read| contains the number of bytes read into
266 * |out_buffer|. If AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE is returned,
267 * |out_num_bytes_read| contains the number of bytes that would have been read
268 * which can be used to allocate a buffer.
269 *
270 * The |buffer_size| may be zero and the |out_buffer| may be NULL, but if
271 * |out_buffer| is NULL then |buffer_size| *must* be zero.
272 *
273 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
274 *
275 * If the value does not exist, is not supported, or is not populated, returns
276 * AVB_IO_RESULT_ERROR_NO_SUCH_VALUE. If |buffer_size| is smaller than the
277 * size of the stored value, returns AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE.
278 *
279 * This operation is currently only used to support persistent digests. If a
280 * device does not use persistent digests this function pointer can be set to
281 * NULL.
282 */
283 AvbIOResult (*read_persistent_value)(AvbOps *ops,
284 const char *name,
285 size_t buffer_size,
286 uint8_t *out_buffer,
287 size_t *out_num_bytes_read);
288
289 /* Writes a persistent value corresponding to the given |name|. The value is
290 * supplied in |value| which must point to |value_size| bytes. Any existing
291 * value with the same name is overwritten. If |value_size| is zero, future
292 * calls to |read_persistent_value| will return
293 * AVB_IO_RESULT_ERROR_NO_SUCH_VALUE.
294 *
295 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
296 *
297 * If the value |name| is not supported, returns
298 * AVB_IO_RESULT_ERROR_NO_SUCH_VALUE. If the |value_size| is not supported,
299 * returns AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE.
300 *
301 * This operation is currently only used to support persistent digests. If a
302 * device does not use persistent digests this function pointer can be set to
303 * NULL.
304 */
305 AvbIOResult (*write_persistent_value)(AvbOps *ops,
306 const char *name,
307 size_t value_size,
308 const uint8_t *value);
309};
310
311#ifdef __cplusplus
312}
313#endif
314
315#endif /* AVB_OPS_H_ */