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