blob: 535a95b1a82df3e466485119d5262f368a908c51 [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>
24
25__BEGIN_DECLS
26
27/* The id of this module. */
28#define NVRAM_HARDWARE_MODULE_ID "nvram"
29#define NVRAM_HARDWARE_DEVICE_ID "nvram-dev"
30
31/* The version of this module. */
32#define NVRAM_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
33#define NVRAM_DEVICE_API_VERSION_0_1 HARDWARE_DEVICE_API_VERSION(0, 1)
34
35/* Values returned by nvram_device methods. */
36typedef uint32_t nvram_result_t;
37
38const nvram_result_t NV_RESULT_SUCCESS = 0;
39const nvram_result_t NV_RESULT_INTERNAL_ERROR = 1;
40const nvram_result_t NV_RESULT_ACCESS_DENIED = 2;
41const nvram_result_t NV_RESULT_INVALID_PARAMETER = 3;
42const nvram_result_t NV_RESULT_SPACE_DOES_NOT_EXIST = 4;
43const nvram_result_t NV_RESULT_SPACE_ALREADY_EXISTS = 5;
44const nvram_result_t NV_RESULT_OPERATION_DISABLED = 6;
45
46/* Values describing available access controls. */
47typedef uint32_t nvram_control_t;
48
49const nvram_control_t NV_CONTROL_PERSISTENT_WRITE_LOCK = 1;
50const nvram_control_t NV_CONTROL_BOOT_WRITE_LOCK = 2;
51const nvram_control_t NV_CONTROL_BOOT_READ_LOCK = 3;
52const nvram_control_t NV_CONTROL_WRITE_AUTHORIZATION = 4;
53const nvram_control_t NV_CONTROL_READ_AUTHORIZATION = 5;
54const nvram_control_t NV_CONTROL_WRITE_EXTEND = 6;
55
56const uint32_t NV_UNLIMITED_SPACES = 0xFFFFFFFF;
57
58struct nvram_module {
59 /**
60 * Common methods of the nvram_module. This *must* be the first member of
61 * nvram_module as users of this structure will cast a hw_module_t to
62 * nvram_module pointer in contexts where it's known the hw_module_t
63 * references a nvram_module.
64 */
65 hw_module_t common;
66
67 /* There are no module methods other than the common ones. */
68};
69
70struct nvram_device {
71 /**
72 * Common methods of the nvram_device. This *must* be the first member of
73 * nvram_device as users of this structure will cast a hw_device_t to
74 * nvram_device pointer in contexts where it's known the hw_device_t
75 * references a nvram_device.
76 */
77 struct hw_device_t common;
78
79 /**
80 * Outputs the total number of bytes available in NVRAM. This will
81 * always be at least 2048. If an implementation does not know the
82 * total size it may provide an estimate or 2048.
83 *
84 * device - The nvram_device instance.
85 * total_size - Receives the output. Cannot be NULL.
86 */
87 nvram_result_t (*get_total_size_in_bytes)(const struct nvram_device* device,
88 uint64_t* total_size);
89
90 /**
91 * Outputs the unallocated number of bytes available in NVRAM. If an
92 * implementation does not know the available size it may provide an
93 * estimate or the total size.
94 *
95 * device - The nvram_device instance.
96 * available_size - Receives the output. Cannot be NULL.
97 */
98 nvram_result_t (*get_available_size_in_bytes)(
99 const struct nvram_device* device, uint64_t* available_size);
100
101 /**
102 * Outputs the maximum total number of spaces that may be allocated.
103 * This will always be at least 8. Outputs NV_UNLIMITED_SPACES if any
104 * number of spaces are supported (limited only to available NVRAM
105 * bytes).
106 *
107 * device - The nvram_device instance.
108 * num_spaces - Receives the output. Cannot be NULL.
109 */
110 nvram_result_t (*get_max_spaces)(const struct nvram_device* device,
111 uint32_t* num_spaces);
112
113 /**
114 * Outputs a list of created space indices. If |max_list_size| is
115 * 0, only |list_size| is populated.
116 *
117 * device - The nvram_device instance.
118 * max_list_size - The number of items in the |space_index_list|
119 * array.
120 * space_index_list - Receives the list of created spaces up to the
121 * given |max_list_size|. May be NULL if
122 * |max_list_size| is 0.
123 * list_size - Receives the number of items populated in
124 * |space_index_list|, or the number of items available
125 * if |space_index_list| is NULL.
126 */
127 nvram_result_t (*get_space_list)(const struct nvram_device* device,
128 uint32_t max_list_size,
129 uint32_t* space_index_list,
130 uint32_t* list_size);
131
132 /**
133 * Outputs the size, in bytes, of a given space.
134 *
135 * device - The nvram_device instance.
136 * index - The space index.
137 * size - Receives the output. Cannot be NULL.
138 */
139 nvram_result_t (*get_space_size)(const struct nvram_device* device,
140 uint32_t index, uint64_t* size);
141
142 /**
143 * Outputs the list of controls associated with a given space.
144 *
145 * device - The nvram_device instance.
146 * index - The space index.
147 * max_list_size - The number of items in the |control_list| array.
148 * control_list - Receives the list of controls up to the given
149 * |max_list_size|. May be NULL if |max_list_size|
150 * is 0.
151 * list_size - Receives the number of items populated in
152 * |control_list|, or the number of items available if
153 * |control_list| is NULL.
154 */
155 nvram_result_t (*get_space_controls)(const struct nvram_device* device,
156 uint32_t index, uint32_t max_list_size,
157 nvram_control_t* control_list,
158 uint32_t* list_size);
159
160 /**
161 * Outputs whether locks are enabled for the given space. When a lock
162 * is enabled, the operation is disabled and any attempt to perform that
163 * operation will result in NV_RESULT_OPERATION_DISABLED.
164 *
165 * device - The nvram_device instance.
166 * index - The space index.
167 * write_lock_enabled - Will be set to non-zero iff write
168 * operations are currently disabled.
169 * read_lock_enabled - Will be set to non-zero iff read operations
170 * are currently disabled.
171 */
172 nvram_result_t (*is_space_locked)(const struct nvram_device* device,
173 uint32_t index, int* write_lock_enabled,
174 int* read_lock_enabled);
175
176 /**
177 * Creates a new space with the given index, size, controls, and
178 * authorization value.
179 *
180 * device - The nvram_device instance.
181 * index - An index for the new space. The index can be any 32-bit
182 * value but must not already be assigned to an existing
183 * space.
184 * size_in_bytes - The number of bytes to allocate for the space.
185 * control_list - An array of controls to enforce for the space.
186 * list_size - The number of items in |control_list|.
187 * authorization_value - If |control_list| contains
188 * NV_CONTROL_READ_AUTHORIZATION and / or
189 * NV_CONTROL_WRITE_AUTHORIZATION, then this
190 * parameter provides the authorization value
191 * for these policies (if both controls are
192 * set then this value applies to both).
193 * Otherwise, this value is ignored and may
194 * be NULL.
195 * authorization_value_size - The number of bytes in
196 * |authorization_value|.
197 */
198 nvram_result_t (*create_space)(const struct nvram_device* device,
199 uint32_t index, uint64_t size_in_bytes,
200 nvram_control_t* control_list,
201 uint32_t list_size,
202 uint8_t* authorization_value,
203 uint32_t authorization_value_size);
204
205 /**
206 * Deletes a space.
207 *
208 * device - The nvram_device instance.
209 * index - The space index.
210 * authorization_value - If the space has the
211 * NV_CONTROL_WRITE_AUTHORIZATION policy,
212 * then this parameter provides the
213 * authorization value. Otherwise, this value
214 * is ignored and may be NULL.
215 * authorization_value_size - The number of bytes in
216 * |authorization_value|.
217 */
218 nvram_result_t (*delete_space)(const struct nvram_device* device,
219 uint32_t index, uint8_t* authorization_value,
220 uint32_t authorization_value_size);
221
222 /**
223 * Disables any further creation of spaces until the next full device
224 * reset (as in factory reset, not reboot). Subsequent calls to
225 * NV_CreateSpace should return NV_RESULT_OPERATION_DISABLED.
226 *
227 * device - The nvram_device instance.
228 */
229 nvram_result_t (*disable_create)(const struct nvram_device* device);
230
231 /**
232 * Writes the contents of a space. If the space is configured with
233 * NV_CONTROL_WRITE_EXTEND then the input data is used to extend the
234 * current data.
235 *
236 * device - The nvram_device instance.
237 * index - The space index.
238 * buffer - The data to write.
239 * buffer_size - The number of bytes in |buffer|. If this is less
240 * than the size of the space, the remaining bytes
241 * will be set to 0x00. If this is more than the size
242 * of the space, returns NV_RESULT_INVALID_PARAMETER.
243 * authorization_value - If the space has the
244 * NV_CONTROL_WRITE_AUTHORIZATION policy,
245 * then this parameter provides the
246 * authorization value. Otherwise, this value
247 * is ignored and may be NULL.
248 * authorization_value_size - The number of bytes in
249 * |authorization_value|.
250 */
251 nvram_result_t (*write_space)(const struct nvram_device* device,
252 uint32_t index, const uint8_t* buffer,
253 uint64_t buffer_size,
254 uint8_t* authorization_value,
255 uint32_t authorization_value_size);
256
257 /**
258 * Reads the contents of a space. If the space has never been
259 * written, all bytes read will be 0x00.
260 *
261 * device - The nvram_device instance.
262 * index - The space index.
263 * num_bytes_to_read - The number of bytes to read; |buffer| must
264 * be large enough to hold this many bytes. If
265 * this is more than the size of the space, the
266 * entire space is read. If this is less than
267 * the size of the space, the first bytes in
268 * the space are read.
269 * authorization_value - If the space has the
270 * NV_CONTROL_READ_AUTHORIZATION policy, then
271 * this parameter provides the authorization
272 * value. Otherwise, this value is ignored
273 * and may be NULL.
274 * authorization_value_size - The number of bytes in
275 * |authorization_value|.
276 * buffer - Receives the data read from the space. Must be at least
277 * |num_bytes_to_read| bytes in size.
278 * bytes_read - The number of bytes read. If NV_RESULT_SUCCESS is
279 * returned this will be set to the smaller of
280 * |num_bytes_to_read| or the size of the space.
281 */
282 nvram_result_t (*read_space)(const struct nvram_device* device,
283 uint32_t index, uint64_t num_bytes_to_read,
284 uint8_t* authorization_value,
285 uint32_t authorization_value_size,
286 uint8_t* buffer, uint64_t* bytes_read);
287
288 /**
289 * Enables a write lock for the given space according to its policy.
290 * If the space does not have NV_CONTROL_PERSISTENT_WRITE_LOCK or
291 * NV_CONTROL_BOOT_WRITE_LOCK set then this function has no effect
292 * and may return an error.
293 *
294 * device - The nvram_device instance.
295 * index - The space index.
296 * authorization_value - If the space has the
297 * NV_CONTROL_WRITE_AUTHORIZATION policy,
298 * then this parameter provides the
299 * authorization value. Otherwise, this value
300 * is ignored and may be NULL.
301 * authorization_value_size - The number of bytes in
302 * |authorization_value|.
303 */
304 nvram_result_t (*enable_write_lock)(const struct nvram_device* device,
305 uint32_t index,
306 uint8_t* authorization_value,
307 uint32_t authorization_value_size);
308
309 /**
310 * Enables a read lock for the given space according to its policy.
311 * If the space does not have NV_CONTROL_BOOT_READ_LOCK set then this
312 * function has no effect and may return an error.
313 *
314 * device - The nvram_device instance.
315 * index - The space index.
316 * authorization_value - If the space has the
317 * NV_CONTROL_READ_AUTHORIZATION policy, then
318 * this parameter provides the authorization
319 * value. (Note that there is no requirement
320 * for write access in order to lock for
321 * reading. A read lock is always volatile.)
322 * Otherwise, this value is ignored and may
323 * be NULL.
324 * authorization_value_size - The number of bytes in
325 * |authorization_value|.
326 */
327 nvram_result_t (*enable_read_lock)(const struct nvram_device* device,
328 uint32_t index,
329 uint8_t* authorization_value,
330 uint32_t authorization_value_size);
331};
332
333typedef struct nvram_device nvram_device_t;
334
335/* Convenience API for opening and closing nvram devices. */
336static inline int nvram_open(const struct hw_module_t* module,
337 nvram_device_t** device) {
338 return module->methods->open(module, NVRAM_HARDWARE_DEVICE_ID,
339 (struct hw_device_t**)device);
340}
341
342static inline int nvram_close(nvram_device_t* device) {
343 return device->common.close(&device->common);
344}
345
346__END_DECLS
347
348#endif // ANDROID_HARDWARE_NVRAM_H