xref: /freebsd/sys/dev/bhnd/nvram/bhnd_nvram_data.h (revision 537d134373141c2d25bfb24af6d661d0e6102927)
1 /*-
2  * Copyright (c) 2015-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_DATA_H_
33 #define _BHND_NVRAM_BHND_NVRAM_DATA_H_
34 
35 #ifdef _KERNEL
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #else /* !_KERNEL */
39 #include <errno.h>
40 
41 #include <stdint.h>
42 #include <stdlib.h>
43 #endif /* _KERNEL */
44 
45 #include "bhnd_nvram.h"
46 #include "bhnd_nvram_io.h"
47 #include "bhnd_nvram_plist.h"
48 #include "bhnd_nvram_value.h"
49 
50 /* NVRAM data class */
51 typedef struct bhnd_nvram_data_class bhnd_nvram_data_class;
52 
53 /* NVRAM data instance */
54 struct bhnd_nvram_data;
55 
56 /** Declare a bhnd_nvram_data_class with name @p _n */
57 #define	BHND_NVRAM_DATA_CLASS_DECL(_n) \
58 	extern 	struct bhnd_nvram_data_class bhnd_nvram_ ## _n ## _class
59 
60 BHND_NVRAM_DATA_CLASS_DECL(bcm);
61 BHND_NVRAM_DATA_CLASS_DECL(bcmraw);
62 BHND_NVRAM_DATA_CLASS_DECL(tlv);
63 BHND_NVRAM_DATA_CLASS_DECL(btxt);
64 BHND_NVRAM_DATA_CLASS_DECL(sprom);
65 
66 /** bhnd_nvram_data capabilities */
67 enum {
68 	/** Supports efficient lookup of variables by name */
69 	BHND_NVRAM_DATA_CAP_INDEXED	= (1<<0),
70 
71 	/** Supports direct access to backing buffer */
72 	BHND_NVRAM_DATA_CAP_READ_PTR	= (1<<1),
73 
74 	/** Supports device path prefixed variables */
75 	BHND_NVRAM_DATA_CAP_DEVPATHS	= (1<<2),
76 };
77 
78 /**
79  * A standard set of probe priorities returned by bhnd_nvram_data_probe().
80  *
81  * Priority is defined in ascending order, with 0 being the highest priority.
82  * Return values greater than zero are interpreted as regular unix error codes.
83  */
84 enum {
85 	BHND_NVRAM_DATA_PROBE_MAYBE	= -40,	/**< Possible match */
86 	BHND_NVRAM_DATA_PROBE_DEFAULT	= -20,	/**< Definite match of a base
87 						     OS-supplied data class */
88 	BHND_NVRAM_DATA_PROBE_SPECIFIC	= 0,	/**< Terminate search and use
89 						     this data class for
90 						     parsing */
91 };
92 
93 const char		*bhnd_nvram_data_class_desc(bhnd_nvram_data_class *cls);
94 uint32_t		 bhnd_nvram_data_class_caps(bhnd_nvram_data_class *cls);
95 
96 int			 bhnd_nvram_data_serialize(bhnd_nvram_data_class *cls,
97 			     bhnd_nvram_plist *props, bhnd_nvram_plist *options,
98 			     void *outp, size_t *olen);
99 
100 int			 bhnd_nvram_data_probe(bhnd_nvram_data_class *cls,
101 			     struct bhnd_nvram_io *io);
102 int			 bhnd_nvram_data_probe_classes(
103 			     struct bhnd_nvram_data **data,
104 			     struct bhnd_nvram_io *io,
105 			     bhnd_nvram_data_class *classes[],
106 			     size_t num_classes);
107 
108 int			 bhnd_nvram_data_getvar_direct(
109 			     bhnd_nvram_data_class *cls,
110 			     struct bhnd_nvram_io *io, const char *name,
111 			     void *buf, size_t *len, bhnd_nvram_type type);
112 
113 int			 bhnd_nvram_data_new(bhnd_nvram_data_class *cls,
114 			     struct bhnd_nvram_data **nv,
115 			   struct bhnd_nvram_io *io);
116 
117 struct bhnd_nvram_data	*bhnd_nvram_data_retain(struct bhnd_nvram_data *nv);
118 void			 bhnd_nvram_data_release(struct bhnd_nvram_data *nv);
119 
120 bhnd_nvram_data_class	*bhnd_nvram_data_get_class(struct bhnd_nvram_data *nv);
121 
122 size_t			 bhnd_nvram_data_count(struct bhnd_nvram_data *nv);
123 bhnd_nvram_plist	*bhnd_nvram_data_options(struct bhnd_nvram_data *nv);
124 uint32_t		 bhnd_nvram_data_caps(struct bhnd_nvram_data *nv);
125 
126 const char		*bhnd_nvram_data_next(struct bhnd_nvram_data *nv,
127 			     void **cookiep);
128 void			*bhnd_nvram_data_find(struct bhnd_nvram_data *nv,
129 			     const char *name);
130 
131 int			 bhnd_nvram_data_getvar_order(
132 			     struct bhnd_nvram_data *nv, void *cookiep1,
133 			     void *cookiep2);
134 
135 int			 bhnd_nvram_data_getvar(struct bhnd_nvram_data *nv,
136 			     void *cookiep, void *buf, size_t *len,
137 			     bhnd_nvram_type type);
138 
139 const void		*bhnd_nvram_data_getvar_ptr(struct bhnd_nvram_data *nv,
140 			     void *cookiep, size_t *len, bhnd_nvram_type *type);
141 
142 const char		*bhnd_nvram_data_getvar_name(struct bhnd_nvram_data *nv,
143 			     void *cookiep);
144 
145 int			 bhnd_nvram_data_copy_val(struct bhnd_nvram_data *nv,
146 			     void *cookiep, bhnd_nvram_val **val);
147 
148 int			 bhnd_nvram_data_filter_setvar(
149 			     struct bhnd_nvram_data *nv, const char *name,
150 			     bhnd_nvram_val *value, bhnd_nvram_val **result);
151 int			 bhnd_nvram_data_filter_unsetvar(
152 			     struct bhnd_nvram_data *nv, const char *name);
153 
154 #endif /* _BHND_NVRAM_BHND_NVRAM_DATA_H_ */
155