aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/bhnd/nvram/bhnd_nvram_common.h
blob: 3d25b2b3419f33ddbd88a67f81e590f949e20b89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*-
 * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 *    redistribution must be conditioned upon including a substantially
 *    similar Disclaimer requirement for further binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * $FreeBSD$
 */

#ifndef _BHND_NVRAM_BHND_NVRAM_COMMON_H_
#define _BHND_NVRAM_BHND_NVRAM_COMMON_H_

#include <sys/param.h>
#include <sys/malloc.h>

#include "bhnd_nvram.h"

struct bhnd_nvram_tuple;
struct bhnd_nvram_varmap;

struct bhnd_nvram_vardefn;

MALLOC_DECLARE(M_BHND_NVRAM);

extern const uint8_t bhnd_nvram_crc8_tab[];

#define	BHND_NVRAM_CRC8_INITIAL	0xFF		/**< Initial bhnd_nvram_crc8 value */
#define	BHND_NVRAM_CRC8_VALID	0x9F		/**< Valid CRC-8 checksum */
#define	BHND_SPROMREV_MAX	UINT8_MAX	/**< maximum supported SPROM revision */


/** NVRAM data type string representations */
typedef enum {
	BHND_NVRAM_SFMT_HEX	= 1,	/**< hex format */
	BHND_NVRAM_SFMT_DEC	= 2,	/**< decimal format */
	BHND_NVRAM_SFMT_MACADDR	= 3,	/**< mac address (canonical form, hex octets,
					     separated with ':') */
	BHND_NVRAM_SFMT_LEDDC	= 4,	/**< LED PWM duty-cycle (2 bytes -- on/off) */
	BHND_NVRAM_SFMT_CCODE	= 5	/**< count code format (2-3 ASCII chars, or hex string) */
} bhnd_nvram_sfmt;

size_t				 bhnd_nvram_type_width(bhnd_nvram_type type);
const char			*bhnd_nvram_type_fmt(bhnd_nvram_type type,
				     bhnd_nvram_sfmt sfmt, size_t elem_num);

const struct bhnd_nvram_vardefn	*bhnd_nvram_find_vardefn(const char *varname);

bool				 bhnd_nvram_validate_name(const char *name,
				     size_t name_len);
int				 bhnd_nvram_parse_octet_string(
				     const char *value, size_t value_len,
				     void *buf, size_t *len,
				     bhnd_nvram_type type);

int				 bhnd_nvram_varmap_init(
				     struct bhnd_nvram_varmap *map,
				     size_t nelements, int flags);
void				 bhnd_nvram_varmap_free(
				     struct bhnd_nvram_varmap *map);
int				 bhnd_nvram_varmap_add(
				     struct bhnd_nvram_varmap *map,
				     const char *name, const char *value,
				     size_t value_len);
int				 bhnd_nvram_varmap_remove(
				     struct bhnd_nvram_varmap *map,
				     const char *name);
struct bhnd_nvram_tuple		*bhnd_nvram_varmap_find(
				    struct bhnd_nvram_varmap *map,
				    const char *name, size_t name_len);
bool				 bhnd_nvram_varmap_contains(
				    struct bhnd_nvram_varmap *map,
				    const char *name, size_t name_len);

struct bhnd_nvram_tuple		*bhnd_nvram_tuple_alloc(const char *name,
				     const char *value);
void				 bhnd_nvram_tuple_free(
				     struct bhnd_nvram_tuple *tuple);

/** NVRAM variable flags */
enum {
	BHND_NVRAM_VF_ARRAY	= (1<<0),	/**< variable is an array */
	BHND_NVRAM_VF_MFGINT	= (1<<1),	/**< mfg-internal variable; should not be externally visible */
	BHND_NVRAM_VF_IGNALL1	= (1<<2)	/**< hide variable if its value has all bits set. */
};

/** SPROM revision compatibility declaration */
struct bhnd_sprom_compat {
	uint8_t		first;	/**< first compatible SPROM revision */
	uint8_t		last;	/**< last compatible SPROM revision, or BHND_SPROMREV_MAX */
};

/** SPROM value descriptor */
struct bhnd_sprom_offset {
	uint16_t	offset;	/**< byte offset within SPROM */
	bool		cont:1;	/**< value should be bitwise OR'd with the
				  *  previous offset descriptor */
	bhnd_nvram_type	type:7;	/**< data type */
	int8_t		shift;	/**< shift to be applied to the value */
	uint32_t	mask;	/**< mask to be applied to the value(s) */
};

/** SPROM-specific variable definition */
struct bhnd_sprom_vardefn {
	struct bhnd_sprom_compat	 compat;	/**< sprom compatibility declaration */
	const struct bhnd_sprom_offset	*offsets;	/**< offset descriptors */
	size_t				 num_offsets;	/**< number of offset descriptors */
};

/** NVRAM variable definition */
struct bhnd_nvram_vardefn {
	const char		*name;	  	/**< variable name */
	bhnd_nvram_type		 type;	 	/**< base data type */
	bhnd_nvram_sfmt		 sfmt;		/**< string format */
	uint32_t		 flags;		/**< BHND_NVRAM_VF_* flags */

	const struct bhnd_sprom_vardefn	*sp_defs;	/**< SPROM-specific variable definitions */
	size_t				 num_sp_defs;	/**< number of sprom definitions */
};

/**
 * NVRAM value tuple.
 */
struct bhnd_nvram_tuple {
	char	*name;		/**< variable name. */
	size_t	 name_len;	/**< variable length. */
	char	*value;		/**< value, or NULL if this tuple represents variable
				     deletion */
	size_t	 value_len;	/**< value length. */

	LIST_ENTRY(bhnd_nvram_tuple) t_link;
};

LIST_HEAD(bhnd_nvram_tuples, bhnd_nvram_tuple);

/** NVRAM tuple hash table */
struct bhnd_nvram_varmap {
	struct bhnd_nvram_tuples	*table;		/**< hash buckets */
	u_long				 mask;		/**< hash index mask */
};

/**
 * Calculate CRC-8 over @p buf.
 * 
 * @param buf input buffer
 * @param size buffer size
 * @param crc last computed crc, or BHND_NVRAM_CRC8_INITIAL
 */
static inline uint8_t
bhnd_nvram_crc8(const void *buf, size_t size, uint8_t crc)
{
	const uint8_t *p = (const uint8_t *)buf;
	while (size--)
		crc = bhnd_nvram_crc8_tab[(crc ^ *p++)];

	return (crc);
}

#endif /* _BHND_NVRAM_BHND_NVRAM_COMMON_H_ */