Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/dev/bhnd/nvram/bhnd_nvram_value.h
39536 views
1
/*-
2
* Copyright (c) 2015-2016 Landon Fuller <[email protected]>
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer,
10
* without modification.
11
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
12
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13
* redistribution must be conditioned upon including a substantially
14
* similar Disclaimer requirement for further binary redistribution.
15
*
16
* NO WARRANTY
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21
* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27
* THE POSSIBILITY OF SUCH DAMAGES.
28
*
29
*/
30
31
#ifndef _BHND_NVRAM_BHND_NVRAM_VALUE_H_
32
#define _BHND_NVRAM_BHND_NVRAM_VALUE_H_
33
34
#include <sys/refcount.h>
35
36
#ifdef _KERNEL
37
#include <sys/stdarg.h>
38
#else /* !_KERNEL */
39
#include <stdarg.h>
40
#endif /* _KERNEL */
41
42
#include "bhnd_nvram.h"
43
44
typedef struct bhnd_nvram_val_fmt bhnd_nvram_val_fmt;
45
typedef struct bhnd_nvram_val bhnd_nvram_val;
46
47
const char *bhnd_nvram_val_fmt_name(
48
const bhnd_nvram_val_fmt *fmt);
49
50
const bhnd_nvram_val_fmt *bhnd_nvram_val_default_fmt(
51
bhnd_nvram_type type);
52
53
int bhnd_nvram_val_init(bhnd_nvram_val *value,
54
const bhnd_nvram_val_fmt *fmt,
55
const void *inp, size_t ilen,
56
bhnd_nvram_type itype, uint32_t flags);
57
58
int bhnd_nvram_val_convert_init(
59
bhnd_nvram_val *value,
60
const bhnd_nvram_val_fmt *fmt,
61
bhnd_nvram_val *src, uint32_t flags);
62
63
int bhnd_nvram_val_new(bhnd_nvram_val **value,
64
const bhnd_nvram_val_fmt *fmt,
65
const void *inp, size_t ilen,
66
bhnd_nvram_type itype, uint32_t flags);
67
68
int bhnd_nvram_val_convert_new(
69
bhnd_nvram_val **value,
70
const bhnd_nvram_val_fmt *fmt,
71
bhnd_nvram_val *src, uint32_t flags);
72
73
bhnd_nvram_val *bhnd_nvram_val_copy(bhnd_nvram_val *value);
74
75
void bhnd_nvram_val_release(
76
bhnd_nvram_val *value);
77
78
int bhnd_nvram_val_encode(bhnd_nvram_val *value,
79
void *outp, size_t *olen,
80
bhnd_nvram_type otype);
81
82
int bhnd_nvram_val_encode_elem(
83
bhnd_nvram_val *value, const void *inp,
84
size_t ilen, void *outp, size_t *olen,
85
bhnd_nvram_type otype);
86
87
int bhnd_nvram_val_printf(bhnd_nvram_val *value,
88
const char *fmt, char *outp, size_t *olen,
89
...);
90
int bhnd_nvram_val_vprintf(bhnd_nvram_val *value,
91
const char *fmt, char *outp, size_t *olen,
92
va_list ap);
93
94
const void *bhnd_nvram_val_bytes(bhnd_nvram_val *value,
95
size_t *olen, bhnd_nvram_type *otype);
96
97
bhnd_nvram_type bhnd_nvram_val_type(bhnd_nvram_val *value);
98
bhnd_nvram_type bhnd_nvram_val_elem_type(
99
bhnd_nvram_val *value);
100
101
const void *bhnd_nvram_val_next(bhnd_nvram_val *value,
102
const void *prev, size_t *olen);
103
104
size_t bhnd_nvram_val_nelem(bhnd_nvram_val *value);
105
106
/**
107
* NVRAM value flags
108
*/
109
enum {
110
/**
111
* Do not allocate additional space for value data; all data must be
112
* represented inline within the value structure (default).
113
*/
114
BHND_NVRAM_VAL_FIXED = (0<<0),
115
116
/**
117
* Automatically allocate additional space for value data if it cannot
118
* be represented within the value structure.
119
*/
120
BHND_NVRAM_VAL_DYNAMIC = (1<<0),
121
122
/**
123
* Copy the value data upon initialization. (default).
124
*/
125
BHND_NVRAM_VAL_COPY_DATA = (0<<1),
126
127
/**
128
* Do not perform an initial copy of the value data; the data must
129
* remain valid for the lifetime of the NVRAM value.
130
*
131
* Value data will still be copied if the value itself is copied to the
132
* heap.
133
*/
134
BHND_NVRAM_VAL_BORROW_DATA = (1<<1),
135
136
/**
137
* Do not copy the value data when copying the value to the heap; the
138
* vlaue data is assumed to be statically allocated and must remain
139
* valid for the lifetime of the process.
140
*
141
* Implies BHND_NVRAM_VAL_BORROW_DATA.
142
*/
143
BHND_NVRAM_VAL_STATIC_DATA = (1<<2),
144
};
145
146
/**
147
* @internal
148
*
149
* NVRAM value storage types.
150
*/
151
typedef enum {
152
/**
153
* The value structure has an automatic storage duration
154
* (e.g. it is stack allocated, or is otherwise externally managed),
155
* and no destructors will be run prior to deallocation of the value.
156
*
157
* When performing copy/retain, the existing structure must be copied
158
* to a new heap allocation.
159
*/
160
BHND_NVRAM_VAL_STORAGE_AUTO = 0,
161
162
/**
163
* The value structure was heap allocated and is fully managed by the
164
* the NVRAM value API.
165
*
166
* When performing copy/retain, the existing structure may be retained
167
* as-is.
168
*/
169
BHND_NVRAM_VAL_STORAGE_DYNAMIC = 2,
170
171
/**
172
* The value structure has a static storage duration, and will never
173
* be deallocated.
174
*
175
* When performing copy/retain, the existing structure may be referenced
176
* without modification.
177
*/
178
BHND_NVRAM_VAL_STORAGE_STATIC = 3,
179
} bhnd_nvram_val_storage;
180
181
/**
182
* @internal
183
*
184
* NVRAM data storage types.
185
*/
186
typedef enum {
187
/** Value has no active representation. This is the default for
188
* zero-initialized value structures. */
189
BHND_NVRAM_VAL_DATA_NONE = 0,
190
191
/** Value data is represented inline */
192
BHND_NVRAM_VAL_DATA_INLINE = 1,
193
194
/**
195
* Value represented by an external reference to data with a static
196
* storage location. The data need not be copied if copying the value.
197
*/
198
BHND_NVRAM_VAL_DATA_EXT_STATIC = 2,
199
200
/**
201
* Value represented by weak external reference, which must be copied
202
* if copying the value.
203
*/
204
BHND_NVRAM_VAL_DATA_EXT_WEAK = 3,
205
206
/**
207
* Value represented by an external reference that must be deallocated
208
* when deallocating the value.
209
*/
210
BHND_NVRAM_VAL_DATA_EXT_ALLOC = 4,
211
} bhnd_nvram_val_data_storage;
212
213
/**
214
* NVRAM value
215
*/
216
struct bhnd_nvram_val {
217
volatile u_int refs; /**< reference count */
218
bhnd_nvram_val_storage val_storage; /**< value structure storage */
219
const bhnd_nvram_val_fmt *fmt; /**< value format */
220
bhnd_nvram_val_data_storage data_storage; /**< data storage */
221
bhnd_nvram_type data_type; /**< data type */
222
size_t data_len; /**< data size */
223
224
/** data representation */
225
union {
226
uint8_t u8[8]; /**< 8-bit unsigned data */
227
uint16_t u16[4]; /**< 16-bit unsigned data */
228
uint32_t u32[2]; /**< 32-bit unsigned data */
229
uint32_t u64[1]; /**< 64-bit unsigned data */
230
int8_t i8[8]; /**< 8-bit signed data */
231
int16_t i16[4]; /**< 16-bit signed data */
232
int32_t i32[2]; /**< 32-bit signed data */
233
int64_t i64[1]; /**< 64-bit signed data */
234
unsigned char ch[8]; /**< 8-bit character data */
235
bhnd_nvram_bool_t b[8]; /**< 8-bit boolean data */
236
const void *ptr; /**< external data */
237
} data;
238
};
239
240
/** Declare a bhnd_nvram_val_fmt with name @p _n */
241
#define BHND_NVRAM_VAL_FMT_DECL(_n) \
242
extern const bhnd_nvram_val_fmt bhnd_nvram_val_ ## _n ## _fmt;
243
244
BHND_NVRAM_VAL_FMT_DECL(bcm_decimal);
245
BHND_NVRAM_VAL_FMT_DECL(bcm_hex);
246
BHND_NVRAM_VAL_FMT_DECL(bcm_leddc);
247
BHND_NVRAM_VAL_FMT_DECL(bcm_macaddr);
248
BHND_NVRAM_VAL_FMT_DECL(bcm_string);
249
250
BHND_NVRAM_VAL_FMT_DECL(uint8);
251
BHND_NVRAM_VAL_FMT_DECL(uint16);
252
BHND_NVRAM_VAL_FMT_DECL(uint32);
253
BHND_NVRAM_VAL_FMT_DECL(uint64);
254
BHND_NVRAM_VAL_FMT_DECL(int8);
255
BHND_NVRAM_VAL_FMT_DECL(int16);
256
BHND_NVRAM_VAL_FMT_DECL(int32);
257
BHND_NVRAM_VAL_FMT_DECL(int64);
258
BHND_NVRAM_VAL_FMT_DECL(char);
259
BHND_NVRAM_VAL_FMT_DECL(bool);
260
BHND_NVRAM_VAL_FMT_DECL(string);
261
BHND_NVRAM_VAL_FMT_DECL(data);
262
BHND_NVRAM_VAL_FMT_DECL(null);
263
264
BHND_NVRAM_VAL_FMT_DECL(uint8_array);
265
BHND_NVRAM_VAL_FMT_DECL(uint16_array);
266
BHND_NVRAM_VAL_FMT_DECL(uint32_array);
267
BHND_NVRAM_VAL_FMT_DECL(uint64_array);
268
BHND_NVRAM_VAL_FMT_DECL(int8_array);
269
BHND_NVRAM_VAL_FMT_DECL(int16_array);
270
BHND_NVRAM_VAL_FMT_DECL(int32_array);
271
BHND_NVRAM_VAL_FMT_DECL(int64_array);
272
BHND_NVRAM_VAL_FMT_DECL(char_array);
273
BHND_NVRAM_VAL_FMT_DECL(bool_array);
274
BHND_NVRAM_VAL_FMT_DECL(string_array);
275
276
/** Shared NULL value instance */
277
#define BHND_NVRAM_VAL_NULL (&bhnd_nvram_val_null)
278
extern bhnd_nvram_val bhnd_nvram_val_null;
279
280
#endif /* _BHND_NVRAM_BHND_NVRAM_VALUE_H_ */
281
282