Path: blob/main/sys/dev/bhnd/nvram/bhnd_nvram_value.h
39536 views
/*-1* Copyright (c) 2015-2016 Landon Fuller <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer,9* without modification.10* 2. Redistributions in binary form must reproduce at minimum a disclaimer11* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any12* redistribution must be conditioned upon including a substantially13* similar Disclaimer requirement for further binary redistribution.14*15* NO WARRANTY16* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY19* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL20* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,21* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF22* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS23* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER24* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)25* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF26* THE POSSIBILITY OF SUCH DAMAGES.27*28*/2930#ifndef _BHND_NVRAM_BHND_NVRAM_VALUE_H_31#define _BHND_NVRAM_BHND_NVRAM_VALUE_H_3233#include <sys/refcount.h>3435#ifdef _KERNEL36#include <sys/stdarg.h>37#else /* !_KERNEL */38#include <stdarg.h>39#endif /* _KERNEL */4041#include "bhnd_nvram.h"4243typedef struct bhnd_nvram_val_fmt bhnd_nvram_val_fmt;44typedef struct bhnd_nvram_val bhnd_nvram_val;4546const char *bhnd_nvram_val_fmt_name(47const bhnd_nvram_val_fmt *fmt);4849const bhnd_nvram_val_fmt *bhnd_nvram_val_default_fmt(50bhnd_nvram_type type);5152int bhnd_nvram_val_init(bhnd_nvram_val *value,53const bhnd_nvram_val_fmt *fmt,54const void *inp, size_t ilen,55bhnd_nvram_type itype, uint32_t flags);5657int bhnd_nvram_val_convert_init(58bhnd_nvram_val *value,59const bhnd_nvram_val_fmt *fmt,60bhnd_nvram_val *src, uint32_t flags);6162int bhnd_nvram_val_new(bhnd_nvram_val **value,63const bhnd_nvram_val_fmt *fmt,64const void *inp, size_t ilen,65bhnd_nvram_type itype, uint32_t flags);6667int bhnd_nvram_val_convert_new(68bhnd_nvram_val **value,69const bhnd_nvram_val_fmt *fmt,70bhnd_nvram_val *src, uint32_t flags);7172bhnd_nvram_val *bhnd_nvram_val_copy(bhnd_nvram_val *value);7374void bhnd_nvram_val_release(75bhnd_nvram_val *value);7677int bhnd_nvram_val_encode(bhnd_nvram_val *value,78void *outp, size_t *olen,79bhnd_nvram_type otype);8081int bhnd_nvram_val_encode_elem(82bhnd_nvram_val *value, const void *inp,83size_t ilen, void *outp, size_t *olen,84bhnd_nvram_type otype);8586int bhnd_nvram_val_printf(bhnd_nvram_val *value,87const char *fmt, char *outp, size_t *olen,88...);89int bhnd_nvram_val_vprintf(bhnd_nvram_val *value,90const char *fmt, char *outp, size_t *olen,91va_list ap);9293const void *bhnd_nvram_val_bytes(bhnd_nvram_val *value,94size_t *olen, bhnd_nvram_type *otype);9596bhnd_nvram_type bhnd_nvram_val_type(bhnd_nvram_val *value);97bhnd_nvram_type bhnd_nvram_val_elem_type(98bhnd_nvram_val *value);99100const void *bhnd_nvram_val_next(bhnd_nvram_val *value,101const void *prev, size_t *olen);102103size_t bhnd_nvram_val_nelem(bhnd_nvram_val *value);104105/**106* NVRAM value flags107*/108enum {109/**110* Do not allocate additional space for value data; all data must be111* represented inline within the value structure (default).112*/113BHND_NVRAM_VAL_FIXED = (0<<0),114115/**116* Automatically allocate additional space for value data if it cannot117* be represented within the value structure.118*/119BHND_NVRAM_VAL_DYNAMIC = (1<<0),120121/**122* Copy the value data upon initialization. (default).123*/124BHND_NVRAM_VAL_COPY_DATA = (0<<1),125126/**127* Do not perform an initial copy of the value data; the data must128* remain valid for the lifetime of the NVRAM value.129*130* Value data will still be copied if the value itself is copied to the131* heap.132*/133BHND_NVRAM_VAL_BORROW_DATA = (1<<1),134135/**136* Do not copy the value data when copying the value to the heap; the137* vlaue data is assumed to be statically allocated and must remain138* valid for the lifetime of the process.139*140* Implies BHND_NVRAM_VAL_BORROW_DATA.141*/142BHND_NVRAM_VAL_STATIC_DATA = (1<<2),143};144145/**146* @internal147*148* NVRAM value storage types.149*/150typedef enum {151/**152* The value structure has an automatic storage duration153* (e.g. it is stack allocated, or is otherwise externally managed),154* and no destructors will be run prior to deallocation of the value.155*156* When performing copy/retain, the existing structure must be copied157* to a new heap allocation.158*/159BHND_NVRAM_VAL_STORAGE_AUTO = 0,160161/**162* The value structure was heap allocated and is fully managed by the163* the NVRAM value API.164*165* When performing copy/retain, the existing structure may be retained166* as-is.167*/168BHND_NVRAM_VAL_STORAGE_DYNAMIC = 2,169170/**171* The value structure has a static storage duration, and will never172* be deallocated.173*174* When performing copy/retain, the existing structure may be referenced175* without modification.176*/177BHND_NVRAM_VAL_STORAGE_STATIC = 3,178} bhnd_nvram_val_storage;179180/**181* @internal182*183* NVRAM data storage types.184*/185typedef enum {186/** Value has no active representation. This is the default for187* zero-initialized value structures. */188BHND_NVRAM_VAL_DATA_NONE = 0,189190/** Value data is represented inline */191BHND_NVRAM_VAL_DATA_INLINE = 1,192193/**194* Value represented by an external reference to data with a static195* storage location. The data need not be copied if copying the value.196*/197BHND_NVRAM_VAL_DATA_EXT_STATIC = 2,198199/**200* Value represented by weak external reference, which must be copied201* if copying the value.202*/203BHND_NVRAM_VAL_DATA_EXT_WEAK = 3,204205/**206* Value represented by an external reference that must be deallocated207* when deallocating the value.208*/209BHND_NVRAM_VAL_DATA_EXT_ALLOC = 4,210} bhnd_nvram_val_data_storage;211212/**213* NVRAM value214*/215struct bhnd_nvram_val {216volatile u_int refs; /**< reference count */217bhnd_nvram_val_storage val_storage; /**< value structure storage */218const bhnd_nvram_val_fmt *fmt; /**< value format */219bhnd_nvram_val_data_storage data_storage; /**< data storage */220bhnd_nvram_type data_type; /**< data type */221size_t data_len; /**< data size */222223/** data representation */224union {225uint8_t u8[8]; /**< 8-bit unsigned data */226uint16_t u16[4]; /**< 16-bit unsigned data */227uint32_t u32[2]; /**< 32-bit unsigned data */228uint32_t u64[1]; /**< 64-bit unsigned data */229int8_t i8[8]; /**< 8-bit signed data */230int16_t i16[4]; /**< 16-bit signed data */231int32_t i32[2]; /**< 32-bit signed data */232int64_t i64[1]; /**< 64-bit signed data */233unsigned char ch[8]; /**< 8-bit character data */234bhnd_nvram_bool_t b[8]; /**< 8-bit boolean data */235const void *ptr; /**< external data */236} data;237};238239/** Declare a bhnd_nvram_val_fmt with name @p _n */240#define BHND_NVRAM_VAL_FMT_DECL(_n) \241extern const bhnd_nvram_val_fmt bhnd_nvram_val_ ## _n ## _fmt;242243BHND_NVRAM_VAL_FMT_DECL(bcm_decimal);244BHND_NVRAM_VAL_FMT_DECL(bcm_hex);245BHND_NVRAM_VAL_FMT_DECL(bcm_leddc);246BHND_NVRAM_VAL_FMT_DECL(bcm_macaddr);247BHND_NVRAM_VAL_FMT_DECL(bcm_string);248249BHND_NVRAM_VAL_FMT_DECL(uint8);250BHND_NVRAM_VAL_FMT_DECL(uint16);251BHND_NVRAM_VAL_FMT_DECL(uint32);252BHND_NVRAM_VAL_FMT_DECL(uint64);253BHND_NVRAM_VAL_FMT_DECL(int8);254BHND_NVRAM_VAL_FMT_DECL(int16);255BHND_NVRAM_VAL_FMT_DECL(int32);256BHND_NVRAM_VAL_FMT_DECL(int64);257BHND_NVRAM_VAL_FMT_DECL(char);258BHND_NVRAM_VAL_FMT_DECL(bool);259BHND_NVRAM_VAL_FMT_DECL(string);260BHND_NVRAM_VAL_FMT_DECL(data);261BHND_NVRAM_VAL_FMT_DECL(null);262263BHND_NVRAM_VAL_FMT_DECL(uint8_array);264BHND_NVRAM_VAL_FMT_DECL(uint16_array);265BHND_NVRAM_VAL_FMT_DECL(uint32_array);266BHND_NVRAM_VAL_FMT_DECL(uint64_array);267BHND_NVRAM_VAL_FMT_DECL(int8_array);268BHND_NVRAM_VAL_FMT_DECL(int16_array);269BHND_NVRAM_VAL_FMT_DECL(int32_array);270BHND_NVRAM_VAL_FMT_DECL(int64_array);271BHND_NVRAM_VAL_FMT_DECL(char_array);272BHND_NVRAM_VAL_FMT_DECL(bool_array);273BHND_NVRAM_VAL_FMT_DECL(string_array);274275/** Shared NULL value instance */276#define BHND_NVRAM_VAL_NULL (&bhnd_nvram_val_null)277extern bhnd_nvram_val bhnd_nvram_val_null;278279#endif /* _BHND_NVRAM_BHND_NVRAM_VALUE_H_ */280281282