177cb4d3eSLandon J. Fuller /*- 277cb4d3eSLandon J. Fuller * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org> 377cb4d3eSLandon J. Fuller * All rights reserved. 477cb4d3eSLandon J. Fuller * 577cb4d3eSLandon J. Fuller * Redistribution and use in source and binary forms, with or without 677cb4d3eSLandon J. Fuller * modification, are permitted provided that the following conditions 777cb4d3eSLandon J. Fuller * are met: 877cb4d3eSLandon J. Fuller * 1. Redistributions of source code must retain the above copyright 977cb4d3eSLandon J. Fuller * notice, this list of conditions and the following disclaimer, 1077cb4d3eSLandon J. Fuller * without modification. 1177cb4d3eSLandon J. Fuller * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1277cb4d3eSLandon J. Fuller * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 1377cb4d3eSLandon J. Fuller * redistribution must be conditioned upon including a substantially 1477cb4d3eSLandon J. Fuller * similar Disclaimer requirement for further binary redistribution. 1577cb4d3eSLandon J. Fuller * 1677cb4d3eSLandon J. Fuller * NO WARRANTY 1777cb4d3eSLandon J. Fuller * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1877cb4d3eSLandon J. Fuller * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1977cb4d3eSLandon J. Fuller * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 2077cb4d3eSLandon J. Fuller * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2177cb4d3eSLandon J. Fuller * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 2277cb4d3eSLandon J. Fuller * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2377cb4d3eSLandon J. Fuller * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2477cb4d3eSLandon J. Fuller * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 2577cb4d3eSLandon J. Fuller * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2677cb4d3eSLandon J. Fuller * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 2777cb4d3eSLandon J. Fuller * THE POSSIBILITY OF SUCH DAMAGES. 2877cb4d3eSLandon J. Fuller * 2977cb4d3eSLandon J. Fuller */ 3077cb4d3eSLandon J. Fuller 3177cb4d3eSLandon J. Fuller #ifndef _BHND_NVRAM_BHND_NVRAM_VALUE_H_ 3277cb4d3eSLandon J. Fuller #define _BHND_NVRAM_BHND_NVRAM_VALUE_H_ 3377cb4d3eSLandon J. Fuller 3477cb4d3eSLandon J. Fuller #include <sys/refcount.h> 3577cb4d3eSLandon J. Fuller 3677cb4d3eSLandon J. Fuller #ifdef _KERNEL 37*e453e498SBrooks Davis #include <sys/stdarg.h> 3877cb4d3eSLandon J. Fuller #else /* !_KERNEL */ 3977cb4d3eSLandon J. Fuller #include <stdarg.h> 4077cb4d3eSLandon J. Fuller #endif /* _KERNEL */ 4177cb4d3eSLandon J. Fuller 4277cb4d3eSLandon J. Fuller #include "bhnd_nvram.h" 4377cb4d3eSLandon J. Fuller 4458efe686SLandon J. Fuller typedef struct bhnd_nvram_val_fmt bhnd_nvram_val_fmt; 4558efe686SLandon J. Fuller typedef struct bhnd_nvram_val bhnd_nvram_val; 4677cb4d3eSLandon J. Fuller 479be0790dSLandon J. Fuller const char *bhnd_nvram_val_fmt_name( 489be0790dSLandon J. Fuller const bhnd_nvram_val_fmt *fmt); 499be0790dSLandon J. Fuller 509be0790dSLandon J. Fuller const bhnd_nvram_val_fmt *bhnd_nvram_val_default_fmt( 519be0790dSLandon J. Fuller bhnd_nvram_type type); 529be0790dSLandon J. Fuller 5358efe686SLandon J. Fuller int bhnd_nvram_val_init(bhnd_nvram_val *value, 5458efe686SLandon J. Fuller const bhnd_nvram_val_fmt *fmt, 5577cb4d3eSLandon J. Fuller const void *inp, size_t ilen, 5677cb4d3eSLandon J. Fuller bhnd_nvram_type itype, uint32_t flags); 5777cb4d3eSLandon J. Fuller 589be0790dSLandon J. Fuller int bhnd_nvram_val_convert_init( 599be0790dSLandon J. Fuller bhnd_nvram_val *value, 609be0790dSLandon J. Fuller const bhnd_nvram_val_fmt *fmt, 619be0790dSLandon J. Fuller bhnd_nvram_val *src, uint32_t flags); 629be0790dSLandon J. Fuller 6358efe686SLandon J. Fuller int bhnd_nvram_val_new(bhnd_nvram_val **value, 6458efe686SLandon J. Fuller const bhnd_nvram_val_fmt *fmt, 6577cb4d3eSLandon J. Fuller const void *inp, size_t ilen, 6677cb4d3eSLandon J. Fuller bhnd_nvram_type itype, uint32_t flags); 6777cb4d3eSLandon J. Fuller 689be0790dSLandon J. Fuller int bhnd_nvram_val_convert_new( 699be0790dSLandon J. Fuller bhnd_nvram_val **value, 709be0790dSLandon J. Fuller const bhnd_nvram_val_fmt *fmt, 719be0790dSLandon J. Fuller bhnd_nvram_val *src, uint32_t flags); 729be0790dSLandon J. Fuller 7358efe686SLandon J. Fuller bhnd_nvram_val *bhnd_nvram_val_copy(bhnd_nvram_val *value); 7477cb4d3eSLandon J. Fuller 7577cb4d3eSLandon J. Fuller void bhnd_nvram_val_release( 7658efe686SLandon J. Fuller bhnd_nvram_val *value); 7777cb4d3eSLandon J. Fuller 7858efe686SLandon J. Fuller int bhnd_nvram_val_encode(bhnd_nvram_val *value, 7977cb4d3eSLandon J. Fuller void *outp, size_t *olen, 8077cb4d3eSLandon J. Fuller bhnd_nvram_type otype); 8177cb4d3eSLandon J. Fuller 8277cb4d3eSLandon J. Fuller int bhnd_nvram_val_encode_elem( 8358efe686SLandon J. Fuller bhnd_nvram_val *value, const void *inp, 8477cb4d3eSLandon J. Fuller size_t ilen, void *outp, size_t *olen, 8577cb4d3eSLandon J. Fuller bhnd_nvram_type otype); 8677cb4d3eSLandon J. Fuller 8758efe686SLandon J. Fuller int bhnd_nvram_val_printf(bhnd_nvram_val *value, 8877cb4d3eSLandon J. Fuller const char *fmt, char *outp, size_t *olen, 8977cb4d3eSLandon J. Fuller ...); 9058efe686SLandon J. Fuller int bhnd_nvram_val_vprintf(bhnd_nvram_val *value, 9177cb4d3eSLandon J. Fuller const char *fmt, char *outp, size_t *olen, 9277cb4d3eSLandon J. Fuller va_list ap); 9377cb4d3eSLandon J. Fuller 9458efe686SLandon J. Fuller const void *bhnd_nvram_val_bytes(bhnd_nvram_val *value, 959be0790dSLandon J. Fuller size_t *olen, bhnd_nvram_type *otype); 9677cb4d3eSLandon J. Fuller 97eb686149SLandon J. Fuller bhnd_nvram_type bhnd_nvram_val_type(bhnd_nvram_val *value); 9877cb4d3eSLandon J. Fuller bhnd_nvram_type bhnd_nvram_val_elem_type( 9958efe686SLandon J. Fuller bhnd_nvram_val *value); 10077cb4d3eSLandon J. Fuller 10158efe686SLandon J. Fuller const void *bhnd_nvram_val_next(bhnd_nvram_val *value, 1029be0790dSLandon J. Fuller const void *prev, size_t *olen); 10377cb4d3eSLandon J. Fuller 10458efe686SLandon J. Fuller size_t bhnd_nvram_val_nelem(bhnd_nvram_val *value); 10577cb4d3eSLandon J. Fuller 10677cb4d3eSLandon J. Fuller /** 10777cb4d3eSLandon J. Fuller * NVRAM value flags 10877cb4d3eSLandon J. Fuller */ 10977cb4d3eSLandon J. Fuller enum { 11077cb4d3eSLandon J. Fuller /** 11177cb4d3eSLandon J. Fuller * Do not allocate additional space for value data; all data must be 11277cb4d3eSLandon J. Fuller * represented inline within the value structure (default). 11377cb4d3eSLandon J. Fuller */ 11477cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_FIXED = (0<<0), 11577cb4d3eSLandon J. Fuller 11677cb4d3eSLandon J. Fuller /** 11777cb4d3eSLandon J. Fuller * Automatically allocate additional space for value data if it cannot 11877cb4d3eSLandon J. Fuller * be represented within the value structure. 11977cb4d3eSLandon J. Fuller */ 12077cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_DYNAMIC = (1<<0), 12177cb4d3eSLandon J. Fuller 12277cb4d3eSLandon J. Fuller /** 12377cb4d3eSLandon J. Fuller * Copy the value data upon initialization. (default). 12477cb4d3eSLandon J. Fuller */ 12577cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_COPY_DATA = (0<<1), 12677cb4d3eSLandon J. Fuller 12777cb4d3eSLandon J. Fuller /** 12877cb4d3eSLandon J. Fuller * Do not perform an initial copy of the value data; the data must 12977cb4d3eSLandon J. Fuller * remain valid for the lifetime of the NVRAM value. 13077cb4d3eSLandon J. Fuller * 13177cb4d3eSLandon J. Fuller * Value data will still be copied if the value itself is copied to the 13277cb4d3eSLandon J. Fuller * heap. 13377cb4d3eSLandon J. Fuller */ 13477cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_BORROW_DATA = (1<<1), 13577cb4d3eSLandon J. Fuller 13677cb4d3eSLandon J. Fuller /** 13777cb4d3eSLandon J. Fuller * Do not copy the value data when copying the value to the heap; the 13877cb4d3eSLandon J. Fuller * vlaue data is assumed to be statically allocated and must remain 13977cb4d3eSLandon J. Fuller * valid for the lifetime of the process. 14077cb4d3eSLandon J. Fuller * 14177cb4d3eSLandon J. Fuller * Implies BHND_NVRAM_VAL_BORROW_DATA. 14277cb4d3eSLandon J. Fuller */ 14377cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_STATIC_DATA = (1<<2), 14477cb4d3eSLandon J. Fuller }; 14577cb4d3eSLandon J. Fuller 14677cb4d3eSLandon J. Fuller /** 14777cb4d3eSLandon J. Fuller * @internal 14877cb4d3eSLandon J. Fuller * 14977cb4d3eSLandon J. Fuller * NVRAM value storage types. 15077cb4d3eSLandon J. Fuller */ 15177cb4d3eSLandon J. Fuller typedef enum { 15277cb4d3eSLandon J. Fuller /** 1539be0790dSLandon J. Fuller * The value structure has an automatic storage duration 15477cb4d3eSLandon J. Fuller * (e.g. it is stack allocated, or is otherwise externally managed), 15577cb4d3eSLandon J. Fuller * and no destructors will be run prior to deallocation of the value. 15677cb4d3eSLandon J. Fuller * 15777cb4d3eSLandon J. Fuller * When performing copy/retain, the existing structure must be copied 15877cb4d3eSLandon J. Fuller * to a new heap allocation. 15977cb4d3eSLandon J. Fuller */ 16077cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_STORAGE_AUTO = 0, 16177cb4d3eSLandon J. Fuller 16277cb4d3eSLandon J. Fuller /** 16377cb4d3eSLandon J. Fuller * The value structure was heap allocated and is fully managed by the 16477cb4d3eSLandon J. Fuller * the NVRAM value API. 16577cb4d3eSLandon J. Fuller * 16677cb4d3eSLandon J. Fuller * When performing copy/retain, the existing structure may be retained 16777cb4d3eSLandon J. Fuller * as-is. 16877cb4d3eSLandon J. Fuller */ 16977cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_STORAGE_DYNAMIC = 2, 1709be0790dSLandon J. Fuller 1719be0790dSLandon J. Fuller /** 1729be0790dSLandon J. Fuller * The value structure has a static storage duration, and will never 1739be0790dSLandon J. Fuller * be deallocated. 1749be0790dSLandon J. Fuller * 1759be0790dSLandon J. Fuller * When performing copy/retain, the existing structure may be referenced 1769be0790dSLandon J. Fuller * without modification. 1779be0790dSLandon J. Fuller */ 1789be0790dSLandon J. Fuller BHND_NVRAM_VAL_STORAGE_STATIC = 3, 17958efe686SLandon J. Fuller } bhnd_nvram_val_storage; 18077cb4d3eSLandon J. Fuller 18177cb4d3eSLandon J. Fuller /** 18277cb4d3eSLandon J. Fuller * @internal 18377cb4d3eSLandon J. Fuller * 18477cb4d3eSLandon J. Fuller * NVRAM data storage types. 18577cb4d3eSLandon J. Fuller */ 18677cb4d3eSLandon J. Fuller typedef enum { 18777cb4d3eSLandon J. Fuller /** Value has no active representation. This is the default for 18877cb4d3eSLandon J. Fuller * zero-initialized value structures. */ 18977cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_DATA_NONE = 0, 19077cb4d3eSLandon J. Fuller 19177cb4d3eSLandon J. Fuller /** Value data is represented inline */ 19277cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_DATA_INLINE = 1, 19377cb4d3eSLandon J. Fuller 19477cb4d3eSLandon J. Fuller /** 19577cb4d3eSLandon J. Fuller * Value represented by an external reference to data with a static 19677cb4d3eSLandon J. Fuller * storage location. The data need not be copied if copying the value. 19777cb4d3eSLandon J. Fuller */ 19877cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_DATA_EXT_STATIC = 2, 19977cb4d3eSLandon J. Fuller 20077cb4d3eSLandon J. Fuller /** 20177cb4d3eSLandon J. Fuller * Value represented by weak external reference, which must be copied 2029be0790dSLandon J. Fuller * if copying the value. 2039be0790dSLandon J. Fuller */ 20477cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_DATA_EXT_WEAK = 3, 20577cb4d3eSLandon J. Fuller 20677cb4d3eSLandon J. Fuller /** 20777cb4d3eSLandon J. Fuller * Value represented by an external reference that must be deallocated 2089be0790dSLandon J. Fuller * when deallocating the value. 20977cb4d3eSLandon J. Fuller */ 21077cb4d3eSLandon J. Fuller BHND_NVRAM_VAL_DATA_EXT_ALLOC = 4, 21158efe686SLandon J. Fuller } bhnd_nvram_val_data_storage; 21277cb4d3eSLandon J. Fuller 21377cb4d3eSLandon J. Fuller /** 21477cb4d3eSLandon J. Fuller * NVRAM value 21577cb4d3eSLandon J. Fuller */ 21677cb4d3eSLandon J. Fuller struct bhnd_nvram_val { 21777cb4d3eSLandon J. Fuller volatile u_int refs; /**< reference count */ 21858efe686SLandon J. Fuller bhnd_nvram_val_storage val_storage; /**< value structure storage */ 2199be0790dSLandon J. Fuller const bhnd_nvram_val_fmt *fmt; /**< value format */ 22058efe686SLandon J. Fuller bhnd_nvram_val_data_storage data_storage; /**< data storage */ 22177cb4d3eSLandon J. Fuller bhnd_nvram_type data_type; /**< data type */ 22277cb4d3eSLandon J. Fuller size_t data_len; /**< data size */ 22377cb4d3eSLandon J. Fuller 22477cb4d3eSLandon J. Fuller /** data representation */ 22577cb4d3eSLandon J. Fuller union { 22677cb4d3eSLandon J. Fuller uint8_t u8[8]; /**< 8-bit unsigned data */ 22777cb4d3eSLandon J. Fuller uint16_t u16[4]; /**< 16-bit unsigned data */ 22877cb4d3eSLandon J. Fuller uint32_t u32[2]; /**< 32-bit unsigned data */ 22977cb4d3eSLandon J. Fuller uint32_t u64[1]; /**< 64-bit unsigned data */ 23077cb4d3eSLandon J. Fuller int8_t i8[8]; /**< 8-bit signed data */ 23177cb4d3eSLandon J. Fuller int16_t i16[4]; /**< 16-bit signed data */ 23277cb4d3eSLandon J. Fuller int32_t i32[2]; /**< 32-bit signed data */ 23377cb4d3eSLandon J. Fuller int64_t i64[1]; /**< 64-bit signed data */ 23477cb4d3eSLandon J. Fuller unsigned char ch[8]; /**< 8-bit character data */ 2356cffadf0SLandon J. Fuller bhnd_nvram_bool_t b[8]; /**< 8-bit boolean data */ 23677cb4d3eSLandon J. Fuller const void *ptr; /**< external data */ 23777cb4d3eSLandon J. Fuller } data; 23877cb4d3eSLandon J. Fuller }; 23977cb4d3eSLandon J. Fuller 24077cb4d3eSLandon J. Fuller /** Declare a bhnd_nvram_val_fmt with name @p _n */ 2419be0790dSLandon J. Fuller #define BHND_NVRAM_VAL_FMT_DECL(_n) \ 24258efe686SLandon J. Fuller extern const bhnd_nvram_val_fmt bhnd_nvram_val_ ## _n ## _fmt; 24377cb4d3eSLandon J. Fuller 2449be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(bcm_decimal); 2459be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(bcm_hex); 2469be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(bcm_leddc); 2479be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(bcm_macaddr); 2489be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(bcm_string); 2499be0790dSLandon J. Fuller 2509be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(uint8); 2519be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(uint16); 2529be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(uint32); 2539be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(uint64); 2549be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(int8); 2559be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(int16); 2569be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(int32); 2579be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(int64); 2589be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(char); 2596cffadf0SLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(bool); 2609be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(string); 2616cffadf0SLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(data); 2626cffadf0SLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(null); 2639be0790dSLandon J. Fuller 2649be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(uint8_array); 2659be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(uint16_array); 2669be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(uint32_array); 2679be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(uint64_array); 2689be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(int8_array); 2699be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(int16_array); 2709be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(int32_array); 2719be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(int64_array); 2729be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(char_array); 2736cffadf0SLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(bool_array); 2749be0790dSLandon J. Fuller BHND_NVRAM_VAL_FMT_DECL(string_array); 27577cb4d3eSLandon J. Fuller 2766cffadf0SLandon J. Fuller /** Shared NULL value instance */ 2776cffadf0SLandon J. Fuller #define BHND_NVRAM_VAL_NULL (&bhnd_nvram_val_null) 2786cffadf0SLandon J. Fuller extern bhnd_nvram_val bhnd_nvram_val_null; 2796cffadf0SLandon J. Fuller 28077cb4d3eSLandon J. Fuller #endif /* _BHND_NVRAM_BHND_NVRAM_VALUE_H_ */ 281