xref: /rk3399_rockchip-uboot/include/android_avb/avb_ops.h (revision 5b69db0720b90f33ecb7fb666b196bfc90404185)
1*5b69db07SJason Zhu /*
2*5b69db07SJason Zhu  * Copyright (C) 2016 The Android Open Source Project
3*5b69db07SJason Zhu  *
4*5b69db07SJason Zhu  * Permission is hereby granted, free of charge, to any person
5*5b69db07SJason Zhu  * obtaining a copy of this software and associated documentation
6*5b69db07SJason Zhu  * files (the "Software"), to deal in the Software without
7*5b69db07SJason Zhu  * restriction, including without limitation the rights to use, copy,
8*5b69db07SJason Zhu  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*5b69db07SJason Zhu  * of the Software, and to permit persons to whom the Software is
10*5b69db07SJason Zhu  * furnished to do so, subject to the following conditions:
11*5b69db07SJason Zhu  *
12*5b69db07SJason Zhu  * The above copyright notice and this permission notice shall be
13*5b69db07SJason Zhu  * included in all copies or substantial portions of the Software.
14*5b69db07SJason Zhu  *
15*5b69db07SJason Zhu  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*5b69db07SJason Zhu  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*5b69db07SJason Zhu  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*5b69db07SJason Zhu  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*5b69db07SJason Zhu  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*5b69db07SJason Zhu  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*5b69db07SJason Zhu  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*5b69db07SJason Zhu  * SOFTWARE.
23*5b69db07SJason Zhu  */
24*5b69db07SJason Zhu 
25*5b69db07SJason Zhu /*
26*5b69db07SJason Zhu #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
27*5b69db07SJason Zhu #error "Never include this file directly, include libavb.h instead."
28*5b69db07SJason Zhu #endif
29*5b69db07SJason Zhu */
30*5b69db07SJason Zhu 
31*5b69db07SJason Zhu #ifndef AVB_OPS_H_
32*5b69db07SJason Zhu #define AVB_OPS_H_
33*5b69db07SJason Zhu 
34*5b69db07SJason Zhu #include <android_avb/avb_sysdeps.h>
35*5b69db07SJason Zhu 
36*5b69db07SJason Zhu #ifdef __cplusplus
37*5b69db07SJason Zhu extern "C" {
38*5b69db07SJason Zhu #endif
39*5b69db07SJason Zhu 
40*5b69db07SJason Zhu /* Return codes used for I/O operations.
41*5b69db07SJason Zhu  *
42*5b69db07SJason Zhu  * AVB_IO_RESULT_OK is returned if the requested operation was
43*5b69db07SJason Zhu  * successful.
44*5b69db07SJason Zhu  *
45*5b69db07SJason Zhu  * AVB_IO_RESULT_ERROR_IO is returned if the underlying hardware (disk
46*5b69db07SJason Zhu  * or other subsystem) encountered an I/O error.
47*5b69db07SJason Zhu  *
48*5b69db07SJason Zhu  * AVB_IO_RESULT_ERROR_OOM is returned if unable to allocate memory.
49*5b69db07SJason Zhu  *
50*5b69db07SJason Zhu  * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION is returned if the requested
51*5b69db07SJason Zhu  * partition does not exist.
52*5b69db07SJason Zhu  *
53*5b69db07SJason Zhu  * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION is returned if the
54*5b69db07SJason Zhu  * range of bytes requested to be read or written is outside the range
55*5b69db07SJason Zhu  * of the partition.
56*5b69db07SJason Zhu  */
57*5b69db07SJason Zhu typedef enum {
58*5b69db07SJason Zhu 	AVB_IO_RESULT_OK,
59*5b69db07SJason Zhu 	AVB_IO_RESULT_ERROR_OOM,
60*5b69db07SJason Zhu 	AVB_IO_RESULT_ERROR_IO,
61*5b69db07SJason Zhu 	AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION,
62*5b69db07SJason Zhu 	AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION
63*5b69db07SJason Zhu } AvbIOResult;
64*5b69db07SJason Zhu 
65*5b69db07SJason Zhu struct AvbOps;
66*5b69db07SJason Zhu typedef struct AvbOps AvbOps;
67*5b69db07SJason Zhu 
68*5b69db07SJason Zhu /* Forward-declaration of operations in libavb_ab. */
69*5b69db07SJason Zhu struct AvbABOps;
70*5b69db07SJason Zhu 
71*5b69db07SJason Zhu /* Forward-declaration of operations in libavb_atx. */
72*5b69db07SJason Zhu struct AvbAtxOps;
73*5b69db07SJason Zhu 
74*5b69db07SJason Zhu /* High-level operations/functions/methods that are platform
75*5b69db07SJason Zhu  * dependent.
76*5b69db07SJason Zhu  *
77*5b69db07SJason Zhu  * Operations may be added in the future so when implementing it
78*5b69db07SJason Zhu  * always make sure to zero out sizeof(AvbOps) bytes of the struct to
79*5b69db07SJason Zhu  * ensure that unimplemented operations are set to NULL.
80*5b69db07SJason Zhu  */
81*5b69db07SJason Zhu struct AvbOps {
82*5b69db07SJason Zhu 	/* This pointer can be used by the application/bootloader using
83*5b69db07SJason Zhu 	 * libavb and is typically used in each operation to get a pointer
84*5b69db07SJason Zhu 	 * to platform-specific resources. It cannot be used by libraries.
85*5b69db07SJason Zhu 	 */
86*5b69db07SJason Zhu 	void* user_data;
87*5b69db07SJason Zhu 
88*5b69db07SJason Zhu 	/* If libavb_ab is used, this should point to the
89*5b69db07SJason Zhu 	 * AvbABOps. Otherwise it must be set to NULL.
90*5b69db07SJason Zhu 	 */
91*5b69db07SJason Zhu 	struct AvbABOps* ab_ops;
92*5b69db07SJason Zhu 
93*5b69db07SJason Zhu 	/* If libavb_atx is used, this should point to the
94*5b69db07SJason Zhu 	 * AvbAtxOps. Otherwise it must be set to NULL.
95*5b69db07SJason Zhu 	 */
96*5b69db07SJason Zhu 	struct AvbAtxOps* atx_ops;
97*5b69db07SJason Zhu 
98*5b69db07SJason Zhu 	/* Reads |num_bytes| from offset |offset| from partition with name
99*5b69db07SJason Zhu 	 * |partition| (NUL-terminated UTF-8 string). If |offset| is
100*5b69db07SJason Zhu 	 * negative, its absolute value should be interpreted as the number
101*5b69db07SJason Zhu 	 * of bytes from the end of the partition.
102*5b69db07SJason Zhu 	 *
103*5b69db07SJason Zhu 	 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
104*5b69db07SJason Zhu 	 * there is no partition with the given name,
105*5b69db07SJason Zhu 	 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
106*5b69db07SJason Zhu 	 * |offset| is outside the partition, and AVB_IO_RESULT_ERROR_IO if
107*5b69db07SJason Zhu 	 * there was an I/O error from the underlying I/O subsystem.  If the
108*5b69db07SJason Zhu 	 * operation succeeds as requested AVB_IO_RESULT_OK is returned and
109*5b69db07SJason Zhu 	 * the data is available in |buffer|.
110*5b69db07SJason Zhu 	 *
111*5b69db07SJason Zhu 	 * The only time partial I/O may occur is if reading beyond the end
112*5b69db07SJason Zhu 	 * of the partition. In this case the value returned in
113*5b69db07SJason Zhu 	 * |out_num_read| may be smaller than |num_bytes|.
114*5b69db07SJason Zhu 	 */
115*5b69db07SJason Zhu 	AvbIOResult (*read_from_partition)(AvbOps* ops,
116*5b69db07SJason Zhu                                        const char* partition,
117*5b69db07SJason Zhu                                        int64_t offset,
118*5b69db07SJason Zhu                                        size_t num_bytes,
119*5b69db07SJason Zhu                                        void* buffer,
120*5b69db07SJason Zhu                                        size_t* out_num_read);
121*5b69db07SJason Zhu 
122*5b69db07SJason Zhu 	/* Writes |num_bytes| from |bffer| at offset |offset| to partition
123*5b69db07SJason Zhu 	 * with name |partition| (NUL-terminated UTF-8 string). If |offset|
124*5b69db07SJason Zhu 	 * is negative, its absolute value should be interpreted as the
125*5b69db07SJason Zhu 	 * number of bytes from the end of the partition.
126*5b69db07SJason Zhu 	 *
127*5b69db07SJason Zhu 	 * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
128*5b69db07SJason Zhu 	 * there is no partition with the given name,
129*5b69db07SJason Zhu 	 * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
130*5b69db07SJason Zhu 	 * byterange goes outside the partition, and AVB_IO_RESULT_ERROR_IO
131*5b69db07SJason Zhu 	 * if there was an I/O error from the underlying I/O subsystem.  If
132*5b69db07SJason Zhu 	 * the operation succeeds as requested AVB_IO_RESULT_OK is
133*5b69db07SJason Zhu 	 * returned.
134*5b69db07SJason Zhu 	 *
135*5b69db07SJason Zhu 	 * This function never does any partial I/O, it either transfers all
136*5b69db07SJason Zhu 	 * of the requested bytes or returns an error.
137*5b69db07SJason Zhu 	 */
138*5b69db07SJason Zhu 	AvbIOResult (*write_to_partition)(AvbOps* ops,
139*5b69db07SJason Zhu                                       const char* partition,
140*5b69db07SJason Zhu                                       int64_t offset,
141*5b69db07SJason Zhu                                       size_t num_bytes,
142*5b69db07SJason Zhu                                       const void* buffer);
143*5b69db07SJason Zhu 
144*5b69db07SJason Zhu 	/* Checks if the given public key used to sign the 'vbmeta'
145*5b69db07SJason Zhu 	 * partition is trusted. Boot loaders typically compare this with
146*5b69db07SJason Zhu 	 * embedded key material generated with 'avbtool
147*5b69db07SJason Zhu 	 * extract_public_key'.
148*5b69db07SJason Zhu 	 *
149*5b69db07SJason Zhu 	 * The public key is in the array pointed to by |public_key_data|
150*5b69db07SJason Zhu 	 * and is of |public_key_length| bytes.
151*5b69db07SJason Zhu 	 *
152*5b69db07SJason Zhu 	 * If there is no public key metadata (set with the avbtool option
153*5b69db07SJason Zhu 	 * --public_key_metadata) then |public_key_metadata| will be set to
154*5b69db07SJason Zhu 	 * NULL. Otherwise this field points to the data which is
155*5b69db07SJason Zhu 	 * |public_key_metadata_length| bytes long.
156*5b69db07SJason Zhu 	 *
157*5b69db07SJason Zhu 	 * If AVB_IO_RESULT_OK is returned then |out_is_trusted| is set -
158*5b69db07SJason Zhu 	 * true if trusted or false if untrusted.
159*5b69db07SJason Zhu 	 */
160*5b69db07SJason Zhu 	AvbIOResult (*validate_vbmeta_public_key)(AvbOps* ops,
161*5b69db07SJason Zhu                                             	  const uint8_t* public_key_data,
162*5b69db07SJason Zhu                                             	  size_t public_key_length,
163*5b69db07SJason Zhu                                             	  const uint8_t* public_key_metadata,
164*5b69db07SJason Zhu                                             	  size_t public_key_metadata_length,
165*5b69db07SJason Zhu                                             	  bool* out_is_trusted);
166*5b69db07SJason Zhu 
167*5b69db07SJason Zhu 	/* Gets the rollback index corresponding to the location given by
168*5b69db07SJason Zhu 	 * |rollback_index_location|. The value is returned in
169*5b69db07SJason Zhu 	 * |out_rollback_index|. Returns AVB_IO_RESULT_OK if the rollback
170*5b69db07SJason Zhu 	 * index was retrieved, otherwise an error code.
171*5b69db07SJason Zhu 	 *
172*5b69db07SJason Zhu 	 * A device may have a limited amount of rollback index locations (say,
173*5b69db07SJason Zhu 	 * one or four) so may error out if |rollback_index_location| exceeds
174*5b69db07SJason Zhu 	 * this number.
175*5b69db07SJason Zhu 	 */
176*5b69db07SJason Zhu 	AvbIOResult (*read_rollback_index)(AvbOps* ops,
177*5b69db07SJason Zhu                                      	   size_t rollback_index_location,
178*5b69db07SJason Zhu                                      	   uint64_t* out_rollback_index);
179*5b69db07SJason Zhu 
180*5b69db07SJason Zhu 	/* Sets the rollback index corresponding to the location given by
181*5b69db07SJason Zhu 	 * |rollback_index_location| to |rollback_index|. Returns
182*5b69db07SJason Zhu 	 * AVB_IO_RESULT_OK if the rollback index was set, otherwise an
183*5b69db07SJason Zhu 	 * error code.
184*5b69db07SJason Zhu 	 *
185*5b69db07SJason Zhu 	 * A device may have a limited amount of rollback index locations (say,
186*5b69db07SJason Zhu 	 * one or four) so may error out if |rollback_index_location| exceeds
187*5b69db07SJason Zhu 	 * this number.
188*5b69db07SJason Zhu 	 */
189*5b69db07SJason Zhu 	AvbIOResult (*write_rollback_index)(AvbOps* ops,
190*5b69db07SJason Zhu                                       	    size_t rollback_index_location,
191*5b69db07SJason Zhu                                       	    uint64_t rollback_index);
192*5b69db07SJason Zhu 
193*5b69db07SJason Zhu 	/* Gets whether the device is unlocked. The value is returned in
194*5b69db07SJason Zhu 	 * |out_is_unlocked| (true if unlocked, false otherwise). Returns
195*5b69db07SJason Zhu 	 * AVB_IO_RESULT_OK if the state was retrieved, otherwise an error
196*5b69db07SJason Zhu 	 * code.
197*5b69db07SJason Zhu 	 */
198*5b69db07SJason Zhu 	AvbIOResult (*read_is_device_unlocked)(AvbOps* ops, bool* out_is_unlocked);
199*5b69db07SJason Zhu 	AvbIOResult (*write_is_device_unlocked)(AvbOps* ops, bool* out_is_unlocked);
200*5b69db07SJason Zhu 
201*5b69db07SJason Zhu 	/* Gets the unique partition GUID for a partition with name in
202*5b69db07SJason Zhu 	 * |partition| (NUL-terminated UTF-8 string). The GUID is copied as
203*5b69db07SJason Zhu 	 * a string into |guid_buf| of size |guid_buf_size| and will be NUL
204*5b69db07SJason Zhu 	 * terminated. The string must be lower-case and properly
205*5b69db07SJason Zhu 	 * hyphenated. For example:
206*5b69db07SJason Zhu 	 *
207*5b69db07SJason Zhu 	 *  527c1c6d-6361-4593-8842-3c78fcd39219
208*5b69db07SJason Zhu 	 *
209*5b69db07SJason Zhu 	 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
210*5b69db07SJason Zhu 	 */
211*5b69db07SJason Zhu 	 AvbIOResult (*get_unique_guid_for_partition)(AvbOps* ops,
212*5b69db07SJason Zhu                                                       const char* partition,
213*5b69db07SJason Zhu                                                       char* guid_buf,
214*5b69db07SJason Zhu                                                       size_t guid_buf_size);
215*5b69db07SJason Zhu 
216*5b69db07SJason Zhu 	/* Gets the size of a partition with the name in |partition|
217*5b69db07SJason Zhu 	 * (NUL-terminated UTF-8 string). Returns the value in
218*5b69db07SJason Zhu 	 * |out_size_num_bytes|.
219*5b69db07SJason Zhu 	 *
220*5b69db07SJason Zhu 	 * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
221*5b69db07SJason Zhu 	 */
222*5b69db07SJason Zhu 	AvbIOResult (*get_size_of_partition)(AvbOps* ops,
223*5b69db07SJason Zhu                                        	     const char* partition,
224*5b69db07SJason Zhu                                              uint64_t* out_size_num_bytes);
225*5b69db07SJason Zhu };
226*5b69db07SJason Zhu 
227*5b69db07SJason Zhu #ifdef __cplusplus
228*5b69db07SJason Zhu }
229*5b69db07SJason Zhu #endif
230*5b69db07SJason Zhu 
231*5b69db07SJason Zhu #endif /* AVB_OPS_H_ */
232