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