blob: a74e1c0aaf97a960fe67faf3d69a18173c97ad30 [file] [log] [blame]
Monika Singhb15747d2017-09-25 14:01:13 +05301/*
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/* Return codes used for I/O operations.
39 *
40 * AVB_IO_RESULT_OK is returned if the requested operation was
41 * successful.
42 *
43 * AVB_IO_RESULT_ERROR_IO is returned if the underlying hardware (disk
44 * or other subsystem) encountered an I/O error.
45 *
46 * AVB_IO_RESULT_ERROR_OOM is returned if unable to allocate memory.
47 *
48 * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION is returned if the requested
49 * partition does not exist.
50 *
51 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION is returned if the
52 * range of bytes requested to be read or written is outside the range
53 * of the partition.
54 */
55typedef enum {
56 AVB_IO_RESULT_OK,
57 AVB_IO_RESULT_ERROR_OOM,
58 AVB_IO_RESULT_ERROR_IO,
59 AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION,
60 AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION
61} AvbIOResult;
62
63struct AvbOps;
64typedef struct AvbOps AvbOps;
65
Monika Singh5e170362018-03-14 00:48:36 +053066typedef struct {
67 uint32_t Data1;
68 uint16_t Data2;
69 uint16_t Data3;
70 uint8_t Data4[8];
71} EFI_GUID;
72
Monika Singhb15747d2017-09-25 14:01:13 +053073/* Forward-declaration of operations in libavb_ab. */
74struct AvbABOps;
75
76/* Forward-declaration of operations in libavb_atx. */
77struct AvbAtxOps;
78
79/* High-level operations/functions/methods that are platform
80 * dependent.
81 *
82 * Operations may be added in the future so when implementing it
83 * always make sure to zero out sizeof(AvbOps) bytes of the struct to
84 * ensure that unimplemented operations are set to NULL.
85 */
86struct AvbOps {
87 /* This pointer can be used by the application/bootloader using
88 * libavb and is typically used in each operation to get a pointer
89 * to platform-specific resources. It cannot be used by libraries.
90 */
91 void* user_data;
92
93 /* If libavb_ab is used, this should point to the
94 * AvbABOps. Otherwise it must be set to NULL.
95 */
96 struct AvbABOps* ab_ops;
97
98 /* If libavb_atx is used, this should point to the
99 * AvbAtxOps. Otherwise it must be set to NULL.
100 */
101 struct AvbAtxOps* atx_ops;
102
103 /* Reads |num_bytes| from offset |offset| from partition with name
104 * |partition| (NUL-terminated UTF-8 string). If |offset| is
105 * negative, its absolute value should be interpreted as the number
106 * of bytes from the end of the partition.
107 *
108 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
109 * there is no partition with the given name,
110 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
111 * |offset| is outside the partition, and AVB_IO_RESULT_ERROR_IO if
112 * there was an I/O error from the underlying I/O subsystem. If the
113 * operation succeeds as requested AVB_IO_RESULT_OK is returned and
114 * the data is available in |buffer|.
115 *
116 * The only time partial I/O may occur is if reading beyond the end
117 * of the partition. In this case the value returned in
118 * |out_num_read| may be smaller than |num_bytes|.
119 */
120 AvbIOResult (*read_from_partition)(AvbOps* ops,
121 const char* partition,
122 int64_t offset,
123 size_t num_bytes,
124 void* buffer,
125 size_t* out_num_read);
126
127 /* Writes |num_bytes| from |bffer| at offset |offset| to partition
128 * with name |partition| (NUL-terminated UTF-8 string). If |offset|
129 * is negative, its absolute value should be interpreted as the
130 * number of bytes from the end of the partition.
131 *
132 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
133 * there is no partition with the given name,
134 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
135 * byterange goes outside the partition, and AVB_IO_RESULT_ERROR_IO
136 * if there was an I/O error from the underlying I/O subsystem. If
137 * the operation succeeds as requested AVB_IO_RESULT_OK is
138 * returned.
139 *
140 * This function never does any partial I/O, it either transfers all
141 * of the requested bytes or returns an error.
142 */
143 AvbIOResult (*write_to_partition)(AvbOps* ops,
144 const char* partition,
145 int64_t offset,
146 size_t num_bytes,
147 const void* buffer);
148
149 /* Checks if the given public key used to sign the 'vbmeta'
150 * partition is trusted. Boot loaders typically compare this with
151 * embedded key material generated with 'avbtool
152 * extract_public_key'.
153 *
154 * The public key is in the array pointed to by |public_key_data|
155 * and is of |public_key_length| bytes.
156 *
157 * If there is no public key metadata (set with the avbtool option
158 * --public_key_metadata) then |public_key_metadata| will be set to
159 * NULL. Otherwise this field points to the data which is
160 * |public_key_metadata_length| bytes long.
161 *
162 * If AVB_IO_RESULT_OK is returned then |out_is_trusted| is set -
163 * true if trusted or false if untrusted.
164 */
165 AvbIOResult (*validate_vbmeta_public_key)(AvbOps* ops,
166 const uint8_t* public_key_data,
167 size_t public_key_length,
168 const uint8_t* public_key_metadata,
169 size_t public_key_metadata_length,
170 bool* out_is_trusted);
171
172 /* Gets the rollback index corresponding to the location given by
173 * |rollback_index_location|. The value is returned in
174 * |out_rollback_index|. Returns AVB_IO_RESULT_OK if the rollback
175 * index was retrieved, otherwise an error code.
176 *
177 * A device may have a limited amount of rollback index locations (say,
178 * one or four) so may error out if |rollback_index_location| exceeds
179 * this number.
180 */
181 AvbIOResult (*read_rollback_index)(AvbOps* ops,
182 size_t rollback_index_location,
183 uint64_t* out_rollback_index);
184
185 /* Sets the rollback index corresponding to the location given by
186 * |rollback_index_location| to |rollback_index|. Returns
187 * AVB_IO_RESULT_OK if the rollback index was set, otherwise an
188 * error code.
189 *
190 * A device may have a limited amount of rollback index locations (say,
191 * one or four) so may error out if |rollback_index_location| exceeds
192 * this number.
193 */
194 AvbIOResult (*write_rollback_index)(AvbOps* ops,
195 size_t rollback_index_location,
196 uint64_t rollback_index);
197
198 /* Gets whether the device is unlocked. The value is returned in
199 * |out_is_unlocked| (true if unlocked, false otherwise). Returns
200 * AVB_IO_RESULT_OK if the state was retrieved, otherwise an error
201 * code.
202 */
203 AvbIOResult (*read_is_device_unlocked)(AvbOps* ops, bool* out_is_unlocked);
204
205 /* Gets the unique partition GUID for a partition with name in
206 * |partition| (NUL-terminated UTF-8 string). The GUID is copied as
207 * a string into |guid_buf| of size |guid_buf_size| and will be NUL
208 * terminated. The string must be lower-case and properly
209 * hyphenated. For example:
210 *
211 * 527c1c6d-6361-4593-8842-3c78fcd39219
212 *
213 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
214 */
215 AvbIOResult (*get_unique_guid_for_partition)(AvbOps* ops,
216 const char* partition,
217 char* guid_buf,
218 size_t guid_buf_size);
219
220 /* Gets the size of a partition with the name in |partition|
221 * (NUL-terminated UTF-8 string). Returns the value in
222 * |out_size_num_bytes|.
223 *
Monika Singhe46cadf2019-05-08 15:52:10 +0530224 * If the partition doesn't exist the AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION
225 * error code should be returned.
226 *
Monika Singhb15747d2017-09-25 14:01:13 +0530227 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
228 */
229 AvbIOResult (*get_size_of_partition)(AvbOps* ops,
230 const char* partition,
231 uint64_t* out_size_num_bytes);
232};
233
234typedef struct {
235 BOOLEAN IsUserKey;
Monika Singh2c9d2b82018-05-01 12:24:59 +0530236 BOOLEAN IsMultiSlot;
Monika Singhb15747d2017-09-25 14:01:13 +0530237 UINTN PublicKeyLen;
238 CHAR8 PublicKey[MAX_USER_KEY_SIZE];
239} AvbOpsUserData;
240
241AvbOps *AvbOpsNew(VOID *UserData);
242
243VOID AvbOpsFree(AvbOps *Ops);
244
245#ifdef __cplusplus
246}
247#endif
248
249#endif /* AVB_OPS_H_ */