blob: 0af5073c63e7f6919eddc37337be4b14ad7a9586 [file] [log] [blame]
Darren Krahna59143b2015-12-18 11:30:18 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HARDWARE_NVRAM_H
18#define ANDROID_HARDWARE_NVRAM_H
19
20#include <stdint.h>
21#include <sys/cdefs.h>
22
23#include <hardware/hardware.h>
Mattias Nisslerd2d0b672016-02-04 10:00:28 +010024#include <hardware/nvram_defs.h>
Darren Krahna59143b2015-12-18 11:30:18 -080025
26__BEGIN_DECLS
27
28/* The id of this module. */
29#define NVRAM_HARDWARE_MODULE_ID "nvram"
30#define NVRAM_HARDWARE_DEVICE_ID "nvram-dev"
31
32/* The version of this module. */
33#define NVRAM_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
34#define NVRAM_DEVICE_API_VERSION_0_1 HARDWARE_DEVICE_API_VERSION(0, 1)
35
Darren Krahna59143b2015-12-18 11:30:18 -080036struct nvram_module {
37 /**
38 * Common methods of the nvram_module. This *must* be the first member of
39 * nvram_module as users of this structure will cast a hw_module_t to
40 * nvram_module pointer in contexts where it's known the hw_module_t
41 * references a nvram_module.
42 */
43 hw_module_t common;
44
45 /* There are no module methods other than the common ones. */
46};
47
48struct nvram_device {
49 /**
50 * Common methods of the nvram_device. This *must* be the first member of
51 * nvram_device as users of this structure will cast a hw_device_t to
52 * nvram_device pointer in contexts where it's known the hw_device_t
53 * references a nvram_device.
54 */
55 struct hw_device_t common;
56
57 /**
58 * Outputs the total number of bytes available in NVRAM. This will
59 * always be at least 2048. If an implementation does not know the
60 * total size it may provide an estimate or 2048.
61 *
62 * device - The nvram_device instance.
63 * total_size - Receives the output. Cannot be NULL.
64 */
65 nvram_result_t (*get_total_size_in_bytes)(const struct nvram_device* device,
66 uint64_t* total_size);
67
68 /**
69 * Outputs the unallocated number of bytes available in NVRAM. If an
70 * implementation does not know the available size it may provide an
71 * estimate or the total size.
72 *
73 * device - The nvram_device instance.
74 * available_size - Receives the output. Cannot be NULL.
75 */
76 nvram_result_t (*get_available_size_in_bytes)(
77 const struct nvram_device* device, uint64_t* available_size);
78
79 /**
80 * Outputs the maximum total number of spaces that may be allocated.
81 * This will always be at least 8. Outputs NV_UNLIMITED_SPACES if any
82 * number of spaces are supported (limited only to available NVRAM
83 * bytes).
84 *
85 * device - The nvram_device instance.
86 * num_spaces - Receives the output. Cannot be NULL.
87 */
88 nvram_result_t (*get_max_spaces)(const struct nvram_device* device,
89 uint32_t* num_spaces);
90
91 /**
92 * Outputs a list of created space indices. If |max_list_size| is
93 * 0, only |list_size| is populated.
94 *
95 * device - The nvram_device instance.
96 * max_list_size - The number of items in the |space_index_list|
97 * array.
98 * space_index_list - Receives the list of created spaces up to the
99 * given |max_list_size|. May be NULL if
100 * |max_list_size| is 0.
101 * list_size - Receives the number of items populated in
102 * |space_index_list|, or the number of items available
103 * if |space_index_list| is NULL.
104 */
105 nvram_result_t (*get_space_list)(const struct nvram_device* device,
106 uint32_t max_list_size,
107 uint32_t* space_index_list,
108 uint32_t* list_size);
109
110 /**
111 * Outputs the size, in bytes, of a given space.
112 *
113 * device - The nvram_device instance.
114 * index - The space index.
115 * size - Receives the output. Cannot be NULL.
116 */
117 nvram_result_t (*get_space_size)(const struct nvram_device* device,
118 uint32_t index, uint64_t* size);
119
120 /**
121 * Outputs the list of controls associated with a given space.
122 *
123 * device - The nvram_device instance.
124 * index - The space index.
125 * max_list_size - The number of items in the |control_list| array.
126 * control_list - Receives the list of controls up to the given
127 * |max_list_size|. May be NULL if |max_list_size|
128 * is 0.
129 * list_size - Receives the number of items populated in
130 * |control_list|, or the number of items available if
131 * |control_list| is NULL.
132 */
133 nvram_result_t (*get_space_controls)(const struct nvram_device* device,
134 uint32_t index, uint32_t max_list_size,
135 nvram_control_t* control_list,
136 uint32_t* list_size);
137
138 /**
139 * Outputs whether locks are enabled for the given space. When a lock
140 * is enabled, the operation is disabled and any attempt to perform that
141 * operation will result in NV_RESULT_OPERATION_DISABLED.
142 *
143 * device - The nvram_device instance.
144 * index - The space index.
145 * write_lock_enabled - Will be set to non-zero iff write
146 * operations are currently disabled.
147 * read_lock_enabled - Will be set to non-zero iff read operations
148 * are currently disabled.
149 */
150 nvram_result_t (*is_space_locked)(const struct nvram_device* device,
151 uint32_t index, int* write_lock_enabled,
152 int* read_lock_enabled);
153
154 /**
155 * Creates a new space with the given index, size, controls, and
156 * authorization value.
157 *
158 * device - The nvram_device instance.
159 * index - An index for the new space. The index can be any 32-bit
160 * value but must not already be assigned to an existing
161 * space.
162 * size_in_bytes - The number of bytes to allocate for the space.
163 * control_list - An array of controls to enforce for the space.
164 * list_size - The number of items in |control_list|.
165 * authorization_value - If |control_list| contains
166 * NV_CONTROL_READ_AUTHORIZATION and / or
167 * NV_CONTROL_WRITE_AUTHORIZATION, then this
168 * parameter provides the authorization value
169 * for these policies (if both controls are
170 * set then this value applies to both).
171 * Otherwise, this value is ignored and may
172 * be NULL.
173 * authorization_value_size - The number of bytes in
174 * |authorization_value|.
175 */
176 nvram_result_t (*create_space)(const struct nvram_device* device,
177 uint32_t index, uint64_t size_in_bytes,
Darren Krahnab3ea642016-02-04 13:57:27 -0800178 const nvram_control_t* control_list,
Darren Krahna59143b2015-12-18 11:30:18 -0800179 uint32_t list_size,
Darren Krahnab3ea642016-02-04 13:57:27 -0800180 const uint8_t* authorization_value,
Darren Krahna59143b2015-12-18 11:30:18 -0800181 uint32_t authorization_value_size);
182
183 /**
184 * Deletes a space.
185 *
186 * device - The nvram_device instance.
187 * index - The space index.
188 * authorization_value - If the space has the
189 * NV_CONTROL_WRITE_AUTHORIZATION policy,
190 * then this parameter provides the
191 * authorization value. Otherwise, this value
192 * is ignored and may be NULL.
193 * authorization_value_size - The number of bytes in
194 * |authorization_value|.
195 */
196 nvram_result_t (*delete_space)(const struct nvram_device* device,
Darren Krahnab3ea642016-02-04 13:57:27 -0800197 uint32_t index,
198 const uint8_t* authorization_value,
Darren Krahna59143b2015-12-18 11:30:18 -0800199 uint32_t authorization_value_size);
200
201 /**
202 * Disables any further creation of spaces until the next full device
203 * reset (as in factory reset, not reboot). Subsequent calls to
204 * NV_CreateSpace should return NV_RESULT_OPERATION_DISABLED.
205 *
206 * device - The nvram_device instance.
207 */
208 nvram_result_t (*disable_create)(const struct nvram_device* device);
209
210 /**
211 * Writes the contents of a space. If the space is configured with
212 * NV_CONTROL_WRITE_EXTEND then the input data is used to extend the
213 * current data.
214 *
215 * device - The nvram_device instance.
216 * index - The space index.
217 * buffer - The data to write.
218 * buffer_size - The number of bytes in |buffer|. If this is less
219 * than the size of the space, the remaining bytes
220 * will be set to 0x00. If this is more than the size
221 * of the space, returns NV_RESULT_INVALID_PARAMETER.
222 * authorization_value - If the space has the
223 * NV_CONTROL_WRITE_AUTHORIZATION policy,
224 * then this parameter provides the
225 * authorization value. Otherwise, this value
226 * is ignored and may be NULL.
227 * authorization_value_size - The number of bytes in
228 * |authorization_value|.
229 */
230 nvram_result_t (*write_space)(const struct nvram_device* device,
231 uint32_t index, const uint8_t* buffer,
232 uint64_t buffer_size,
Darren Krahnab3ea642016-02-04 13:57:27 -0800233 const uint8_t* authorization_value,
Darren Krahna59143b2015-12-18 11:30:18 -0800234 uint32_t authorization_value_size);
235
236 /**
237 * Reads the contents of a space. If the space has never been
238 * written, all bytes read will be 0x00.
239 *
240 * device - The nvram_device instance.
241 * index - The space index.
242 * num_bytes_to_read - The number of bytes to read; |buffer| must
243 * be large enough to hold this many bytes. If
244 * this is more than the size of the space, the
245 * entire space is read. If this is less than
246 * the size of the space, the first bytes in
247 * the space are read.
248 * authorization_value - If the space has the
249 * NV_CONTROL_READ_AUTHORIZATION policy, then
250 * this parameter provides the authorization
251 * value. Otherwise, this value is ignored
252 * and may be NULL.
253 * authorization_value_size - The number of bytes in
254 * |authorization_value|.
255 * buffer - Receives the data read from the space. Must be at least
256 * |num_bytes_to_read| bytes in size.
257 * bytes_read - The number of bytes read. If NV_RESULT_SUCCESS is
258 * returned this will be set to the smaller of
259 * |num_bytes_to_read| or the size of the space.
260 */
261 nvram_result_t (*read_space)(const struct nvram_device* device,
262 uint32_t index, uint64_t num_bytes_to_read,
Darren Krahnab3ea642016-02-04 13:57:27 -0800263 const uint8_t* authorization_value,
Darren Krahna59143b2015-12-18 11:30:18 -0800264 uint32_t authorization_value_size,
265 uint8_t* buffer, uint64_t* bytes_read);
266
267 /**
268 * Enables a write lock for the given space according to its policy.
269 * If the space does not have NV_CONTROL_PERSISTENT_WRITE_LOCK or
270 * NV_CONTROL_BOOT_WRITE_LOCK set then this function has no effect
271 * and may return an error.
272 *
273 * device - The nvram_device instance.
274 * index - The space index.
275 * authorization_value - If the space has the
276 * NV_CONTROL_WRITE_AUTHORIZATION policy,
277 * then this parameter provides the
278 * authorization value. Otherwise, this value
279 * is ignored and may be NULL.
280 * authorization_value_size - The number of bytes in
281 * |authorization_value|.
282 */
283 nvram_result_t (*enable_write_lock)(const struct nvram_device* device,
284 uint32_t index,
Darren Krahnab3ea642016-02-04 13:57:27 -0800285 const uint8_t* authorization_value,
Darren Krahna59143b2015-12-18 11:30:18 -0800286 uint32_t authorization_value_size);
287
288 /**
289 * Enables a read lock for the given space according to its policy.
290 * If the space does not have NV_CONTROL_BOOT_READ_LOCK set then this
291 * function has no effect and may return an error.
292 *
293 * device - The nvram_device instance.
294 * index - The space index.
295 * authorization_value - If the space has the
296 * NV_CONTROL_READ_AUTHORIZATION policy, then
297 * this parameter provides the authorization
298 * value. (Note that there is no requirement
299 * for write access in order to lock for
300 * reading. A read lock is always volatile.)
301 * Otherwise, this value is ignored and may
302 * be NULL.
303 * authorization_value_size - The number of bytes in
304 * |authorization_value|.
305 */
306 nvram_result_t (*enable_read_lock)(const struct nvram_device* device,
307 uint32_t index,
Darren Krahnab3ea642016-02-04 13:57:27 -0800308 const uint8_t* authorization_value,
Darren Krahna59143b2015-12-18 11:30:18 -0800309 uint32_t authorization_value_size);
310};
311
312typedef struct nvram_device nvram_device_t;
313
314/* Convenience API for opening and closing nvram devices. */
315static inline int nvram_open(const struct hw_module_t* module,
316 nvram_device_t** device) {
317 return module->methods->open(module, NVRAM_HARDWARE_DEVICE_ID,
318 (struct hw_device_t**)device);
319}
320
321static inline int nvram_close(nvram_device_t* device) {
322 return device->common.close(&device->common);
323}
324
325__END_DECLS
326
327#endif // ANDROID_HARDWARE_NVRAM_H