xref: /rk3399_rockchip-uboot/include/android_avb/rk_avb_ops_user.h (revision 87e4c6020eff05133e40ab8b7b0e37e6a2be37e4)
1 /*
2  * (C) Copyright 2017 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #ifndef RK_AVB_OPS_USER_H_
8 #define RK_AVB_OPS_USER_H_
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /* rk used */
15 #define PERM_ATTR_DIGEST_SIZE 32
16 #define PERM_ATTR_TOTAL_SIZE  1052
17 #define VBOOT_KEY_HASH_SIZE   32
18 #define ANDROID_VBOOT_LOCK 0
19 #define ANDROID_VBOOT_UNLOCK 1
20 #define SLOT_NUM 2
21 #define CURR_SYSTEM_SLOT_SUFFIX "ab"
22 #define VBMETA_MAX_SIZE 65536
23 #define ROLLBACK_MAX_SIZE 20
24 #define LOCK_MASK 0x1
25 #define UNLOCK_DISABLE_MASK 0x2
26 #define VBOOT_STATE_SIZE    1000
27 #define PERM_ATTR_SUCCESS_FLAG 1
28 /* soc-v use the rsa2048 */
29 #define VBOOT_KEY_SIZE   256
30 #define RPMB_BASE_ADDR		(64*1024/256)
31 #define UBOOT_RB_INDEX_OFFSET 24
32 #define TRUST_RB_INDEX_OFFSET 28
33 #define ROCHCHIP_RSA_PARAMETER_SIZE 64
34 
35 struct rk_pub_key {
36 	u_int32_t rsa_n[ROCHCHIP_RSA_PARAMETER_SIZE];
37 	u_int32_t rsa_e[ROCHCHIP_RSA_PARAMETER_SIZE];
38 	u_int32_t rsa_c[ROCHCHIP_RSA_PARAMETER_SIZE];
39 };
40 
41 /**
42  * Provided to fastboot to read how many slot in this system.
43  *
44  * @param slot_count  We use parameter slot_count to obtain
45  *                    how many slots in the system.
46  *
47  * @return 0 if the command succeeded, -1 if it failed
48  */
49 int rk_avb_read_slot_count(char *slot_count);
50 
51 /**
52  * The android things supply many slots, their name like '_a', '_b'.
53  * We can use this function to read current slot is '_a' or '_b'.
54  *
55  * @slot_suffixes  read value '_a' or '_b'.
56  *
57  * @return 0 if the command succeeded, -1 if it failed
58  */
59 int rk_avb_read_slot_suffixes(char *slot_suffixes);
60 
61 /**
62  * Use this function to set which slot boot first.
63  *
64  * @param slot_number set '0' or '1'
65  *
66  * @return 0 if the command succeeded, -1 if it failed
67  */
68 int rk_avb_set_slot_active(unsigned int *slot_number);
69 
70 /**
71  * Get current slot: '_a' or '_b'.
72  *
73  * @param select_slot  obtain current slot.
74  *
75  * @return 0 if the command succeeded, -1 if it failed
76  */
77 int rk_avb_get_current_slot(char *select_slot);
78 
79 /**
80  * The android things defines permanent attributes to
81  * store PSK_public, product id. We can use this function
82  * to read them.
83  *
84  * @param attributes  PSK_public, product id....
85  *
86  * @param size        The size of attributes.
87  *
88  * @return 0 if the command succeeded, -1 if it failed
89  */
90 int rk_avb_read_permanent_attributes(uint8_t *attributes, uint32_t size);
91 
92 /**
93  * The android things defines permanent attributes to
94  * store PSK_public, product id. We can use this function
95  * to write them.
96  *
97  * @param attributes  PSK_public, product id....
98  *
99  * @param size        The size of attributes.
100  *
101  * @return 0 if the command succeeded, -1 if it failed
102  */
103 int rk_avb_write_permanent_attributes(uint8_t *attributes, uint32_t size);
104 
105 /**
106  * The funtion can be use to read the device state to judge
107  * whether the device can be flash.
108  *
109  * @param flash_lock_state  A flag indicate the device flash state.
110  *
111  * @return 0 if the command succeeded, -1 if it failed
112  */
113 int rk_avb_read_flash_lock_state(uint8_t *flash_lock_state);
114 
115 /**
116  * The function is provided to write device flash state.
117  *
118  * @param flash_lock_state   A flag indicate the device flash state.
119  *
120  * @return 0 if the command succeeded, -1 if it failed
121  */
122 int rk_avb_write_flash_lock_state(uint8_t flash_lock_state);
123 
124 /**
125  * The android things use the flag of lock state to indicate
126  * whether the device can be booted when verified error.
127  *
128  * @param lock_state  A flag indicate the device lock state.
129  *
130  * @return 0 if the command succeeded, -1 if it failed
131  */
132 int rk_avb_read_lock_state(uint8_t *lock_state);
133 
134 /**
135  * The android things use the flag of lock state to indicate
136  * whether the device can be booted when verified error.
137  *
138  * @param lock_state   A flag indicate the device lock state.
139  *
140  * @return 0 if the command succeeded, -1 if it failed
141  */
142 int rk_avb_write_lock_state(uint8_t lock_state);
143 
144 /**
145  * The android things uses fastboot to flash the permanent attributes.
146  * And if them were written, there must have a flag to indicate.
147  *
148  * @param flag   indicate the permanent attributes have been written
149  *               or not.
150  *
151  * @return 0 if the command succeeded, -1 if it failed
152  */
153 int rk_avb_read_perm_attr_flag(uint8_t *flag);
154 
155 /**
156  * The android things uses fastboot to flash the permanent attributes.
157  * And if them were written, there must have a flag to indicate.
158  *
159  * @param flag   We can call this function to write the flag '1'
160  *               to indicate the permanent attributes has been
161  *               written.
162  *
163  * @return 0 if the command succeeded, -1 if it failed
164  */
165 int rk_avb_write_perm_attr_flag(uint8_t flag);
166 
167 /**
168  * The android things require the soc-v key hash to be flashed
169  * using the fastboot. So the function can be used in fastboot
170  * to flash the key hash.
171  *
172  * @param buf    The vboot key hash data.
173  *
174  * @param length The length of key hash.
175  *
176  * @return 0 if the command succeeded, -1 if it failed
177  */
178 int rk_avb_read_vbootkey_hash(uint8_t *buf, uint8_t length);
179 
180 /**
181  * The android things require the soc-v key hash to be flashed
182  * using the fastboot. So the function can be used in fastboot
183  * to flash the key hash.
184  *
185  * @param buf    The vboot key hash data.
186  *
187  * @param length The length of key hash.
188  *
189  * @return 0 if the command succeeded, -1 if it failed
190  */
191 int rk_avb_write_vbootkey_hash(uint8_t *buf, uint8_t length);
192 
193 /**
194  * U-boot close the optee client when start kernel
195  * to prevent the optee client being invoking by other
196  * program.
197  *
198  * @return 0 if the command succeeded, -1 if it failed
199  */
200 int rk_avb_close_optee_client(void);
201 
202 /**
203  * read the permanent attributes hash.
204  *
205  * @param buf    The permanent attributes hash data.
206  *
207  * @param length The length of permanent attributes hash.
208  *
209  * @return 0 if the command succeeded, -1 if it failed
210  */
211 int rk_avb_read_attribute_hash(uint8_t *buf, uint8_t length);
212 
213 /**
214  * Write the permanent attributes hash.
215  *
216  * @param buf    The permanent attributes hash data.
217  *
218  * @param length The length of permanent attributes hash.
219  *
220  * @return 0 if the command succeeded, -1 if it failed
221  */
222 int rk_avb_write_attribute_hash(uint8_t *buf, uint8_t length);
223 
224 /**
225  * Get the avb vboot state
226  *
227  * @param buf    store the vboot state.
228  *
229  * @return 0 if the command succeeded, -1 if it failed
230  */
231 void rk_avb_get_at_vboot_state(char *buf);
232 
233 /**
234  * Get ab information from misc partition
235  *
236  * @param ab_data    the struct of ab information
237  *
238  * @return 0 if the command succeeded, -1 if it failed
239  */
240 int rk_avb_get_ab_info(AvbABData* ab_data);
241 
242 /**
243  * Get the information whether the partition has slot
244  *
245  * @param the partition name
246  *
247  * @return 0 if the partition has slot, -1 if not
248  */
249 int rk_avb_get_part_has_slot_info(const char *base_name);
250 
251 AvbABFlowResult rk_avb_ab_slot_select(AvbABOps* ab_ops,char select_slot[]);
252 
253 /**
254  * authenticated unlock
255  *
256  * @param buffer: AvbAtxUnlockCredential
257  *
258  * @param out_is_trusted: true or false
259  *
260  * @return 0 if authenticated unlock OK, -1 if not
261  */
262 int rk_auth_unlock(void *buffer, char *out_is_trusted);
263 
264 /**
265  * generate unlock challenge
266  *
267  * @param buffer: AvbAtxUnlockChallenge
268  *
269  * @param challenge_len: Challenge length
270  *
271  * @param out_is_trusted: true or false
272  *
273  * @return 0 if generate unlock challenge OK, -1 if not
274  */
275 int rk_generate_unlock_challenge(void *buffer, uint32_t *challenge_len);
276 
277 /**
278  * Get last boot slot
279  *
280  * @return 0 is slot A; 1 is slot B; -1 is error
281  */
282 int rk_get_lastboot(void);
283 
284 /**
285  * Get permanent attribute certificate
286  *
287  * @param cer: certificate data
288  *
289  * @param size: certificate size
290  */
291 int rk_avb_get_perm_attr_cer(uint8_t *cer, uint32_t size);
292 
293 /**
294  * Set permanent attribute certificate
295  *
296  * @param cer: certificate data
297  *
298  * @param size: certificate size
299  */
300 int rk_avb_set_perm_attr_cer(uint8_t *cer, uint32_t size);
301 
302 /**
303  * Get public key
304  *
305  * @param pub_key: public key data
306  */
307 int rk_avb_get_pub_key(struct rk_pub_key *pub_key);
308 
309 /**
310  * init ab metadata
311  */
312 int rk_avb_init_ab_metadata(void);
313 
314 #ifdef __cplusplus
315 }
316 #endif
317 
318 #endif /* RK_AVB_OPS_USER_H_ */
319