xref: /freebsd/sys/dev/bhnd/nvram/bhnd_nvram_data.h (revision 7f9dff23d3092aa33ad45b2b63e52469b3c13a6e)
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 
48 /* NVRAM data class */
49 typedef struct bhnd_nvram_data_class bhnd_nvram_data_class_t;
50 
51 /* NVRAM data instance */
52 struct bhnd_nvram_data;
53 
54 /** Declare a bhnd_nvram_data_class with name @p _n */
55 #define	BHND_NVRAM_DATA_CLASS_DECL(_n) \
56 	extern 	struct bhnd_nvram_data_class bhnd_nvram_ ## _n ## _class
57 
58 BHND_NVRAM_DATA_CLASS_DECL(bcm);
59 BHND_NVRAM_DATA_CLASS_DECL(bcmraw);
60 BHND_NVRAM_DATA_CLASS_DECL(tlv);
61 BHND_NVRAM_DATA_CLASS_DECL(btxt);
62 BHND_NVRAM_DATA_CLASS_DECL(sprom);
63 
64 /** bhnd_nvram_data capabilities */
65 enum {
66 	/** Supports efficient lookup of variables by name */
67 	BHND_NVRAM_DATA_CAP_INDEXED	= (1<<0),
68 
69 	/** Supports direct access to backing buffer */
70 	BHND_NVRAM_DATA_CAP_READ_PTR	= (1<<1),
71 
72 	/** Supports device path prefixed variables */
73 	BHND_NVRAM_DATA_CAP_DEVPATHS	= (1<<2),
74 };
75 
76 /**
77  * A standard set of probe priorities returned by bhnd_nvram_data_probe().
78  *
79  * Priority is defined in ascending order, with 0 being the highest priority.
80  * Return values greater than zero are interpreted as regular unix error codes.
81  */
82 enum {
83 	BHND_NVRAM_DATA_PROBE_MAYBE	= -40,	/**< Possible match */
84 	BHND_NVRAM_DATA_PROBE_DEFAULT	= -20,	/**< Definite match of a base
85 						     OS-supplied data class */
86 	BHND_NVRAM_DATA_PROBE_SPECIFIC	= 0,	/**< Terminate search and use
87 						     this data class for
88 						     parsing */
89 };
90 
91 const char		*bhnd_nvram_data_class_desc(
92 			     bhnd_nvram_data_class_t *cls);
93 
94 int			 bhnd_nvram_data_probe(bhnd_nvram_data_class_t *cls,
95 			     struct bhnd_nvram_io *io);
96 int			 bhnd_nvram_data_probe_classes(
97 			     struct bhnd_nvram_data **data,
98 			     struct bhnd_nvram_io *io,
99 			     bhnd_nvram_data_class_t *classes[],
100 			     size_t num_classes);
101 
102 int			 bhnd_nvram_data_new(bhnd_nvram_data_class_t *cls,
103 			     struct bhnd_nvram_data **nv,
104 			   struct bhnd_nvram_io *io);
105 
106 struct bhnd_nvram_data	*bhnd_nvram_data_retain(struct bhnd_nvram_data *nv);
107 void			 bhnd_nvram_data_release(struct bhnd_nvram_data *nv);
108 
109 bhnd_nvram_data_class_t	*bhnd_nvram_data_class(struct bhnd_nvram_data *nv);
110 
111 size_t			 bhnd_nvram_data_count(struct bhnd_nvram_data *nv);
112 
113 int			 bhnd_nvram_data_size(struct bhnd_nvram_data *nv,
114 			     size_t *size);
115 
116 int			 bhnd_nvram_data_serialize(struct bhnd_nvram_data *nv,
117 			     void *buf, size_t *len);
118 
119 uint32_t		 bhnd_nvram_data_caps(struct bhnd_nvram_data *nv);
120 
121 const char		*bhnd_nvram_data_next(struct bhnd_nvram_data *nv,
122 			     void **cookiep);
123 
124 void			*bhnd_nvram_data_find(struct bhnd_nvram_data *nv,
125 			     const char *name);
126 
127 int			 bhnd_nvram_data_getvar(struct bhnd_nvram_data *nv,
128 			     void *cookiep, void *buf, size_t *len,
129 			     bhnd_nvram_type type);
130 
131 const void		*bhnd_nvram_data_getvar_ptr(struct bhnd_nvram_data *nv,
132 			     void *cookiep, size_t *len, bhnd_nvram_type *type);
133 
134 const char		*bhnd_nvram_data_getvar_name(struct bhnd_nvram_data *nv,
135 			     void *cookiep);
136 
137 #endif /* _BHND_NVRAM_BHND_NVRAM_DATA_H_ */
138