2 * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 #ifndef _BHND_NVRAM_BHND_NVRAM_VALUE_H_
33 #define _BHND_NVRAM_BHND_NVRAM_VALUE_H_
36 #include <sys/refcount.h>
39 #include <machine/stdarg.h>
44 #include "bhnd_nvram.h"
46 typedef struct bhnd_nvram_val_fmt bhnd_nvram_val_fmt;
47 typedef struct bhnd_nvram_val bhnd_nvram_val;
49 const char *bhnd_nvram_val_fmt_name(
50 const bhnd_nvram_val_fmt *fmt);
52 const bhnd_nvram_val_fmt *bhnd_nvram_val_default_fmt(
53 bhnd_nvram_type type);
55 int bhnd_nvram_val_init(bhnd_nvram_val *value,
56 const bhnd_nvram_val_fmt *fmt,
57 const void *inp, size_t ilen,
58 bhnd_nvram_type itype, uint32_t flags);
60 int bhnd_nvram_val_convert_init(
61 bhnd_nvram_val *value,
62 const bhnd_nvram_val_fmt *fmt,
63 bhnd_nvram_val *src, uint32_t flags);
65 int bhnd_nvram_val_new(bhnd_nvram_val **value,
66 const bhnd_nvram_val_fmt *fmt,
67 const void *inp, size_t ilen,
68 bhnd_nvram_type itype, uint32_t flags);
70 int bhnd_nvram_val_convert_new(
71 bhnd_nvram_val **value,
72 const bhnd_nvram_val_fmt *fmt,
73 bhnd_nvram_val *src, uint32_t flags);
75 bhnd_nvram_val *bhnd_nvram_val_copy(bhnd_nvram_val *value);
77 void bhnd_nvram_val_release(
78 bhnd_nvram_val *value);
80 int bhnd_nvram_val_encode(bhnd_nvram_val *value,
81 void *outp, size_t *olen,
82 bhnd_nvram_type otype);
84 int bhnd_nvram_val_encode_elem(
85 bhnd_nvram_val *value, const void *inp,
86 size_t ilen, void *outp, size_t *olen,
87 bhnd_nvram_type otype);
89 int bhnd_nvram_val_printf(bhnd_nvram_val *value,
90 const char *fmt, char *outp, size_t *olen,
92 int bhnd_nvram_val_vprintf(bhnd_nvram_val *value,
93 const char *fmt, char *outp, size_t *olen,
97 const void *bhnd_nvram_val_bytes(bhnd_nvram_val *value,
98 size_t *olen, bhnd_nvram_type *otype);
100 bhnd_nvram_type bhnd_nvram_val_type(bhnd_nvram_val *value);
101 bhnd_nvram_type bhnd_nvram_val_elem_type(
102 bhnd_nvram_val *value);
104 const void *bhnd_nvram_val_next(bhnd_nvram_val *value,
105 const void *prev, size_t *olen);
107 size_t bhnd_nvram_val_nelem(bhnd_nvram_val *value);
114 * Do not allocate additional space for value data; all data must be
115 * represented inline within the value structure (default).
117 BHND_NVRAM_VAL_FIXED = (0<<0),
120 * Automatically allocate additional space for value data if it cannot
121 * be represented within the value structure.
123 BHND_NVRAM_VAL_DYNAMIC = (1<<0),
126 * Copy the value data upon initialization. (default).
128 BHND_NVRAM_VAL_COPY_DATA = (0<<1),
131 * Do not perform an initial copy of the value data; the data must
132 * remain valid for the lifetime of the NVRAM value.
134 * Value data will still be copied if the value itself is copied to the
137 BHND_NVRAM_VAL_BORROW_DATA = (1<<1),
140 * Do not copy the value data when copying the value to the heap; the
141 * vlaue data is assumed to be statically allocated and must remain
142 * valid for the lifetime of the process.
144 * Implies BHND_NVRAM_VAL_BORROW_DATA.
146 BHND_NVRAM_VAL_STATIC_DATA = (1<<2),
152 * NVRAM value storage types.
156 * The value structure has an automatic storage duration
157 * (e.g. it is stack allocated, or is otherwise externally managed),
158 * and no destructors will be run prior to deallocation of the value.
160 * When performing copy/retain, the existing structure must be copied
161 * to a new heap allocation.
163 BHND_NVRAM_VAL_STORAGE_AUTO = 0,
166 * The value structure was heap allocated and is fully managed by the
167 * the NVRAM value API.
169 * When performing copy/retain, the existing structure may be retained
172 BHND_NVRAM_VAL_STORAGE_DYNAMIC = 2,
175 * The value structure has a static storage duration, and will never
178 * When performing copy/retain, the existing structure may be referenced
179 * without modification.
181 BHND_NVRAM_VAL_STORAGE_STATIC = 3,
182 } bhnd_nvram_val_storage;
187 * NVRAM data storage types.
190 /** Value has no active representation. This is the default for
191 * zero-initialized value structures. */
192 BHND_NVRAM_VAL_DATA_NONE = 0,
194 /** Value data is represented inline */
195 BHND_NVRAM_VAL_DATA_INLINE = 1,
198 * Value represented by an external reference to data with a static
199 * storage location. The data need not be copied if copying the value.
201 BHND_NVRAM_VAL_DATA_EXT_STATIC = 2,
204 * Value represented by weak external reference, which must be copied
205 * if copying the value.
207 BHND_NVRAM_VAL_DATA_EXT_WEAK = 3,
210 * Value represented by an external reference that must be deallocated
211 * when deallocating the value.
213 BHND_NVRAM_VAL_DATA_EXT_ALLOC = 4,
214 } bhnd_nvram_val_data_storage;
219 struct bhnd_nvram_val {
220 volatile u_int refs; /**< reference count */
221 bhnd_nvram_val_storage val_storage; /**< value structure storage */
222 const bhnd_nvram_val_fmt *fmt; /**< value format */
223 bhnd_nvram_val_data_storage data_storage; /**< data storage */
224 bhnd_nvram_type data_type; /**< data type */
225 size_t data_len; /**< data size */
227 /** data representation */
229 uint8_t u8[8]; /**< 8-bit unsigned data */
230 uint16_t u16[4]; /**< 16-bit unsigned data */
231 uint32_t u32[2]; /**< 32-bit unsigned data */
232 uint32_t u64[1]; /**< 64-bit unsigned data */
233 int8_t i8[8]; /**< 8-bit signed data */
234 int16_t i16[4]; /**< 16-bit signed data */
235 int32_t i32[2]; /**< 32-bit signed data */
236 int64_t i64[1]; /**< 64-bit signed data */
237 unsigned char ch[8]; /**< 8-bit character data */
238 bhnd_nvram_bool_t b[8]; /**< 8-bit boolean data */
239 const void *ptr; /**< external data */
243 /** Declare a bhnd_nvram_val_fmt with name @p _n */
244 #define BHND_NVRAM_VAL_FMT_DECL(_n) \
245 extern const bhnd_nvram_val_fmt bhnd_nvram_val_ ## _n ## _fmt;
247 BHND_NVRAM_VAL_FMT_DECL(bcm_decimal);
248 BHND_NVRAM_VAL_FMT_DECL(bcm_hex);
249 BHND_NVRAM_VAL_FMT_DECL(bcm_leddc);
250 BHND_NVRAM_VAL_FMT_DECL(bcm_macaddr);
251 BHND_NVRAM_VAL_FMT_DECL(bcm_string);
253 BHND_NVRAM_VAL_FMT_DECL(uint8);
254 BHND_NVRAM_VAL_FMT_DECL(uint16);
255 BHND_NVRAM_VAL_FMT_DECL(uint32);
256 BHND_NVRAM_VAL_FMT_DECL(uint64);
257 BHND_NVRAM_VAL_FMT_DECL(int8);
258 BHND_NVRAM_VAL_FMT_DECL(int16);
259 BHND_NVRAM_VAL_FMT_DECL(int32);
260 BHND_NVRAM_VAL_FMT_DECL(int64);
261 BHND_NVRAM_VAL_FMT_DECL(char);
262 BHND_NVRAM_VAL_FMT_DECL(bool);
263 BHND_NVRAM_VAL_FMT_DECL(string);
264 BHND_NVRAM_VAL_FMT_DECL(data);
265 BHND_NVRAM_VAL_FMT_DECL(null);
267 BHND_NVRAM_VAL_FMT_DECL(uint8_array);
268 BHND_NVRAM_VAL_FMT_DECL(uint16_array);
269 BHND_NVRAM_VAL_FMT_DECL(uint32_array);
270 BHND_NVRAM_VAL_FMT_DECL(uint64_array);
271 BHND_NVRAM_VAL_FMT_DECL(int8_array);
272 BHND_NVRAM_VAL_FMT_DECL(int16_array);
273 BHND_NVRAM_VAL_FMT_DECL(int32_array);
274 BHND_NVRAM_VAL_FMT_DECL(int64_array);
275 BHND_NVRAM_VAL_FMT_DECL(char_array);
276 BHND_NVRAM_VAL_FMT_DECL(bool_array);
277 BHND_NVRAM_VAL_FMT_DECL(string_array);
279 /** Shared NULL value instance */
280 #define BHND_NVRAM_VAL_NULL (&bhnd_nvram_val_null)
281 extern bhnd_nvram_val bhnd_nvram_val_null;
283 #endif /* _BHND_NVRAM_BHND_NVRAM_VALUE_H_ */