xref: /rk3399_rockchip-uboot/include/android_avb/avb_ops_user.h (revision f74d184a88a9a5ea742eb0beab7e6175fc80f461)
1 /*
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 #ifndef AVB_OPS_USER_H_
26 #define AVB_OPS_USER_H_
27 
28 #include <android_avb/libavb.h>
29 #include <android_avb/avb_ab_flow.h>
30 
31 #define PERM_ATTR_DIGEST_SIZE 32
32 #define PERM_ATTR_TOTAL_SIZE  1084
33 
34 /* Allocates an AvbOps instance suitable for use in Android userspace
35  * on the device. Returns NULL on OOM.
36  *
37  * The returned AvbOps has the following characteristics:
38  *
39  * - The read_from_partition(), write_to_partition(), and
40  *   get_size_of_partition() operations are implemented, however for
41  *   these operations to work the fstab file on the device must have a
42  *   /misc entry using a by-name device file scheme and the containing
43  *   by-name/ subdirectory must have files for other partitions.
44  *
45  * - The remaining operations are implemented and never fails and
46  *   return the following values:
47  *   - validate_vbmeta_public_key(): always returns |true|.
48  *   - read_rollback_index(): returns 0 for any roolback index.
49  *   - write_rollback_index(): no-op.
50  *   - read_is_device_unlocked(): always returns |true|.
51  *   - get_unique_guid_for_partition(): always returns the empty string.
52  *
53  * - The |ab_ops| member will point to a valid AvbABOps instance
54  *   implemented via libavb_ab/. This should only be used if the AVB
55  *   A/B stack is used on the device. This is what is used in
56  *   bootctrl.avb boot control implementation.
57  *
58  * Free with avb_ops_user_free().
59  */
60 AvbOps* avb_ops_user_new(void);
61 
62 /* Frees an AvbOps instance previously allocated with avb_ops_device_new(). */
63 void avb_ops_user_free(AvbOps* ops);
64 
65 /**
66  * Provided to fastboot to read how many slot in this system.
67  *
68  * @param slot_count  We use parameter slot_count to obtain
69  *                    how many slots in the system.
70  *
71  * @return 0 if the command succeeded, -1 if it failed
72  */
73 int avb_read_slot_count(char *slot_count);
74 
75 /**
76  * The android things supply many slots, their name like '_a', '_b'.
77  * We can use this function to read current slot is '_a' or '_b'.
78  *
79  * @slot_suffixes  read value '_a' or '_b'.
80  *
81  * @return 0 if the command succeeded, -1 if it failed
82  */
83 int avb_read_slot_suffixes(char *slot_suffixes);
84 
85 /**
86  * Use this function to set which slot boot first.
87  *
88  * @param slot_number set '0' or '1'
89  *
90  * @return 0 if the command succeeded, -1 if it failed
91  */
92 int avb_set_slot_active(unsigned int *slot_number);
93 
94 /**
95  * Get current slot: '_a' or '_b'.
96  *
97  * @param select_slot  obtain current slot.
98  *
99  * @return 0 if the command succeeded, -1 if it failed
100  */
101 int avb_get_current_slot(char *select_slot);
102 
103 /**
104  * The android things defines permanent attributes to
105  * store PSK_public, product id. We can use this function
106  * to read them.
107  *
108  * @param attributes  PSK_public, product id....
109  *
110  * @param size        The size of attributes.
111  *
112  * @return 0 if the command succeeded, -1 if it failed
113  */
114 int avb_read_permanent_attributes(uint8_t *attributes, uint32_t size);
115 
116 /**
117  * The android things defines permanent attributes to
118  * store PSK_public, product id. We can use this function
119  * to write them.
120  *
121  * @param attributes  PSK_public, product id....
122  *
123  * @param size        The size of attributes.
124  *
125  * @return 0 if the command succeeded, -1 if it failed
126  */
127 int avb_write_permanent_attributes(uint8_t *attributes, uint32_t size);
128 
129 /**
130  * The funtion can be use to read the device state to judge
131  * whether the device can be flash.
132  *
133  * @param flash_lock_state  A flag indicate the device flash state.
134  *
135  * @return 0 if the command succeeded, -1 if it failed
136  */
137 int avb_read_flash_lock_state(uint8_t *flash_lock_state);
138 
139 /**
140  * The function is provided to write device flash state.
141  *
142  * @param flash_lock_state   A flag indicate the device flash state.
143  *
144  * @return 0 if the command succeeded, -1 if it failed
145  */
146 int avb_write_flash_lock_state(uint8_t flash_lock_state);
147 
148 /**
149  * The android things use the flag of lock state to indicate
150  * whether the device can be booted when verified error.
151  *
152  * @param lock_state  A flag indicate the device lock state.
153  *
154  * @return 0 if the command succeeded, -1 if it failed
155  */
156 int avb_read_lock_state(uint8_t *lock_state);
157 
158 /**
159  * The android things use the flag of lock state to indicate
160  * whether the device can be booted when verified error.
161  *
162  * @param lock_state   A flag indicate the device lock state.
163  *
164  * @return 0 if the command succeeded, -1 if it failed
165  */
166 int avb_write_lock_state(uint8_t lock_state);
167 
168 /**
169  * The android things uses fastboot to flash the permanent attributes.
170  * And if them were written, there must have a flag to indicate.
171  *
172  * @param flag   indicate the permanent attributes have been written
173  *               or not.
174  *
175  * @return 0 if the command succeeded, -1 if it failed
176  */
177 int avb_read_perm_attr_flag(uint8_t *flag);
178 
179 /**
180  * The android things uses fastboot to flash the permanent attributes.
181  * And if them were written, there must have a flag to indicate.
182  *
183  * @param flag   We can call this function to write the flag '1'
184  *               to indicate the permanent attributes has been
185  *               written.
186  *
187  * @return 0 if the command succeeded, -1 if it failed
188  */
189 int avb_write_perm_attr_flag(uint8_t flag);
190 
191 #endif
192