1/*- 2 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org> 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_DATAVAR_H_ 32#define _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_ 33 34#include <sys/param.h> 35#include <sys/linker_set.h> 36#include <sys/refcount.h> 37 38#include "bhnd_nvram_io.h" 39 40#include "bhnd_nvram_data.h" 41 42/** Registered NVRAM parser class instances. */ 43SET_DECLARE(bhnd_nvram_data_class_set, bhnd_nvram_data_class); 44 45void *bhnd_nvram_data_generic_find( 46 struct bhnd_nvram_data *nv, const char *name); 47int bhnd_nvram_data_generic_rp_getvar( 48 struct bhnd_nvram_data *nv, void *cookiep, 49 void *outp, size_t *olen, bhnd_nvram_type otype); 50int bhnd_nvram_data_generic_rp_copy_val( 51 struct bhnd_nvram_data *nv, void *cookiep, 52 bhnd_nvram_val **val); 53 54/** @see bhnd_nvram_data_probe() */ 55typedef int (bhnd_nvram_data_op_probe)(struct bhnd_nvram_io *io); 56 57/** @see bhnd_nvram_data_probe() */ 58typedef int (bhnd_nvram_data_op_getvar_direct)( 59 struct bhnd_nvram_io *io, const char *name, 60 void *outp, size_t *olen, bhnd_nvram_type otype); 61 62/** @see bhnd_nvram_data_serialize() */ 63typedef int (bhnd_nvram_data_op_serialize)( 64 bhnd_nvram_data_class *cls, 65 bhnd_nvram_plist *props, 66 bhnd_nvram_plist *options, void *outp, 67 size_t *olen); 68 69/** @see bhnd_nvram_data_new() */ 70typedef int (bhnd_nvram_data_op_new)(struct bhnd_nvram_data *nv, 71 struct bhnd_nvram_io *io); 72 73/** Free all resources associated with @p nv. Called by 74 * bhnd_nvram_data_release() when the reference count reaches zero. */ 75typedef void (bhnd_nvram_data_op_free)(struct bhnd_nvram_data *nv); 76 77/** @see bhnd_nvram_data_count() */ 78typedef size_t (bhnd_nvram_data_op_count)(struct bhnd_nvram_data *nv); 79 80/** @see bhnd_nvram_data_options() */ 81typedef bhnd_nvram_plist*(bhnd_nvram_data_op_options)( 82 struct bhnd_nvram_data *nv); 83 84/** @see bhnd_nvram_data_caps() */ 85typedef uint32_t (bhnd_nvram_data_op_caps)(struct bhnd_nvram_data *nv); 86 87/** @see bhnd_nvram_data_next() */ 88typedef const char *(bhnd_nvram_data_op_next)(struct bhnd_nvram_data *nv, 89 void **cookiep); 90 91/** @see bhnd_nvram_data_find() */ 92typedef void *(bhnd_nvram_data_op_find)(struct bhnd_nvram_data *nv, 93 const char *name); 94 95/** @see bhnd_nvram_data_copy_val() */ 96typedef int (bhnd_nvram_data_op_copy_val)( 97 struct bhnd_nvram_data *nv, void *cookiep, 98 bhnd_nvram_val **value); 99 100/** @see bhnd_nvram_data_getvar_order() */ 101typedef int (bhnd_nvram_data_op_getvar_order)( 102 struct bhnd_nvram_data *nv, void *cookiep1, 103 void *cookiep2); 104 105/** @see bhnd_nvram_data_getvar_name() */ 106typedef const char *(bhnd_nvram_data_op_getvar_name)( 107 struct bhnd_nvram_data *nv, 108 void *cookiep); 109 110/** @see bhnd_nvram_data_getvar() */ 111typedef int (bhnd_nvram_data_op_getvar)(struct bhnd_nvram_data *nv, 112 void *cookiep, void *buf, size_t *len, 113 bhnd_nvram_type type); 114 115/** @see bhnd_nvram_data_getvar_ptr() */ 116typedef const void *(bhnd_nvram_data_op_getvar_ptr)( 117 struct bhnd_nvram_data *nv, void *cookiep, 118 size_t *len, bhnd_nvram_type *type); 119 120/** @see bhnd_nvram_data_filter_setvar() */ 121typedef int (bhnd_nvram_data_op_filter_setvar)( 122 struct bhnd_nvram_data *nv, const char *name, 123 bhnd_nvram_val *value, bhnd_nvram_val **result); 124 125/** @see bhnd_nvram_data_filter_unsetvar() */ 126typedef int (bhnd_nvram_data_op_filter_unsetvar)( 127 struct bhnd_nvram_data *nv, const char *name); 128 129/** 130 * NVRAM data class. 131 */ 132struct bhnd_nvram_data_class { 133 const char *desc; /**< description */ 134 uint32_t caps; /**< capabilities (BHND_NVRAM_DATA_CAP_*) */ 135 size_t size; /**< instance size */ 136 137 bhnd_nvram_data_op_probe *op_probe; 138 bhnd_nvram_data_op_getvar_direct *op_getvar_direct; 139 bhnd_nvram_data_op_serialize *op_serialize; 140 bhnd_nvram_data_op_new *op_new; 141 bhnd_nvram_data_op_free *op_free; 142 bhnd_nvram_data_op_count *op_count; 143 bhnd_nvram_data_op_options *op_options; 144 bhnd_nvram_data_op_caps *op_caps; 145 bhnd_nvram_data_op_next *op_next; 146 bhnd_nvram_data_op_find *op_find; 147 bhnd_nvram_data_op_copy_val *op_copy_val; 148 bhnd_nvram_data_op_getvar_order *op_getvar_order; 149 bhnd_nvram_data_op_getvar *op_getvar; 150 bhnd_nvram_data_op_getvar_ptr *op_getvar_ptr; 151 bhnd_nvram_data_op_getvar_name *op_getvar_name; 152 bhnd_nvram_data_op_filter_setvar *op_filter_setvar; 153 bhnd_nvram_data_op_filter_unsetvar *op_filter_unsetvar; 154}; 155 156/** 157 * NVRAM data instance. 158 */ 159struct bhnd_nvram_data { 160 struct bhnd_nvram_data_class *cls; 161 volatile u_int refs; 162}; 163 164/* 165 * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN(). 166 * 167 * Declares a bhnd_nvram_data_class method implementation with class name 168 * _cname and method name _mname 169 */ 170#define BHND_NVRAM_DATA_CLASS_DECL_METHOD(_cname, _mname) \ 171 static bhnd_nvram_data_op_ ## _mname \ 172 bhnd_nvram_ ## _cname ## _ ## _mname; \ 173 174/* 175 * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN(). 176 * 177 * Assign a bhnd_nvram_data_class method implementation with class name 178 * @p _cname and method name @p _mname 179 */ 180#define BHND_NVRAM_DATA_CLASS_ASSIGN_METHOD(_cname, _mname) \ 181 .op_ ## _mname = bhnd_nvram_ ## _cname ## _ ## _mname, 182 183/* 184 * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN(). 185 * 186 * Iterate over all bhnd_nvram_data_class method names, calling 187 * _macro with the class name _cname as the first argument, and 188 * a bhnd_nvram_data_class method name as the second. 189 */ 190#define BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname, _macro) \ 191 _macro(_cname, probe) \ 192 _macro(_cname, getvar_direct) \ 193 _macro(_cname, serialize) \ 194 _macro(_cname, new) \ 195 _macro(_cname, free) \ 196 _macro(_cname, count) \ 197 _macro(_cname, options) \ 198 _macro(_cname, caps) \ 199 _macro(_cname, next) \ 200 _macro(_cname, find) \ 201 _macro(_cname, copy_val) \ 202 _macro(_cname, getvar_order) \ 203 _macro(_cname, getvar) \ 204 _macro(_cname, getvar_ptr) \ 205 _macro(_cname, getvar_name) \ 206 _macro(_cname, filter_setvar) \ 207 _macro(_cname, filter_unsetvar) 208 209/** 210 * Define a bhnd_nvram_data_class with class name @p _n and description 211 * @p _desc, and register with bhnd_nvram_data_class_set. 212 */ 213#define BHND_NVRAM_DATA_CLASS_DEFN(_cname, _desc, _caps, _size) \ 214 BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname, \ 215 BHND_NVRAM_DATA_CLASS_DECL_METHOD) \ 216 \ 217 struct bhnd_nvram_data_class bhnd_nvram_## _cname ## _class = { \ 218 .desc = (_desc), \ 219 .caps = (_caps), \ 220 .size = (_size), \ 221 BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname, \ 222 BHND_NVRAM_DATA_CLASS_ASSIGN_METHOD) \ 223 }; \ 224 \ 225 DATA_SET(bhnd_nvram_data_class_set, \ 226 bhnd_nvram_## _cname ## _class); 227 228#endif /* _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_ */ 229