xref: /freebsd-11-stable/sys/dev/bhnd/nvram/nvram_subr.c (revision e83ce340355a3a75b0343b46e23fe95ed92157f4)
1 /*-
2  * Copyright (c) 2016 Landon Fuller <landon@landonf.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 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 
37 #include "bhnd_nvram_map_data.h"
38 
39 /*
40  * CRC-8 lookup table used to checksum SPROM and NVRAM data via
41  * bhnd_nvram_crc8().
42  *
43  * Generated with following parameters:
44  * 	polynomial:	CRC-8 (x^8 + x^7 + x^6 + x^4 + x^2 + 1)
45  * 	reflected bits:	false
46  * 	reversed:	true
47  */
48 const uint8_t bhnd_nvram_crc8_tab[] = {
49 	0x00, 0xf7, 0xb9, 0x4e, 0x25, 0xd2, 0x9c, 0x6b, 0x4a, 0xbd, 0xf3,
50 	0x04, 0x6f, 0x98, 0xd6, 0x21, 0x94, 0x63, 0x2d, 0xda, 0xb1, 0x46,
51 	0x08, 0xff, 0xde, 0x29, 0x67, 0x90, 0xfb, 0x0c, 0x42, 0xb5, 0x7f,
52 	0x88, 0xc6, 0x31, 0x5a, 0xad, 0xe3, 0x14, 0x35, 0xc2, 0x8c, 0x7b,
53 	0x10, 0xe7, 0xa9, 0x5e, 0xeb, 0x1c, 0x52, 0xa5, 0xce, 0x39, 0x77,
54 	0x80, 0xa1, 0x56, 0x18, 0xef, 0x84, 0x73, 0x3d, 0xca, 0xfe, 0x09,
55 	0x47, 0xb0, 0xdb, 0x2c, 0x62, 0x95, 0xb4, 0x43, 0x0d, 0xfa, 0x91,
56 	0x66, 0x28, 0xdf, 0x6a, 0x9d, 0xd3, 0x24, 0x4f, 0xb8, 0xf6, 0x01,
57 	0x20, 0xd7, 0x99, 0x6e, 0x05, 0xf2, 0xbc, 0x4b, 0x81, 0x76, 0x38,
58 	0xcf, 0xa4, 0x53, 0x1d, 0xea, 0xcb, 0x3c, 0x72, 0x85, 0xee, 0x19,
59 	0x57, 0xa0, 0x15, 0xe2, 0xac, 0x5b, 0x30, 0xc7, 0x89, 0x7e, 0x5f,
60 	0xa8, 0xe6, 0x11, 0x7a, 0x8d, 0xc3, 0x34, 0xab, 0x5c, 0x12, 0xe5,
61 	0x8e, 0x79, 0x37, 0xc0, 0xe1, 0x16, 0x58, 0xaf, 0xc4, 0x33, 0x7d,
62 	0x8a, 0x3f, 0xc8, 0x86, 0x71, 0x1a, 0xed, 0xa3, 0x54, 0x75, 0x82,
63 	0xcc, 0x3b, 0x50, 0xa7, 0xe9, 0x1e, 0xd4, 0x23, 0x6d, 0x9a, 0xf1,
64 	0x06, 0x48, 0xbf, 0x9e, 0x69, 0x27, 0xd0, 0xbb, 0x4c, 0x02, 0xf5,
65 	0x40, 0xb7, 0xf9, 0x0e, 0x65, 0x92, 0xdc, 0x2b, 0x0a, 0xfd, 0xb3,
66 	0x44, 0x2f, 0xd8, 0x96, 0x61, 0x55, 0xa2, 0xec, 0x1b, 0x70, 0x87,
67 	0xc9, 0x3e, 0x1f, 0xe8, 0xa6, 0x51, 0x3a, 0xcd, 0x83, 0x74, 0xc1,
68 	0x36, 0x78, 0x8f, 0xe4, 0x13, 0x5d, 0xaa, 0x8b, 0x7c, 0x32, 0xc5,
69 	0xae, 0x59, 0x17, 0xe0, 0x2a, 0xdd, 0x93, 0x64, 0x0f, 0xf8, 0xb6,
70 	0x41, 0x60, 0x97, 0xd9, 0x2e, 0x45, 0xb2, 0xfc, 0x0b, 0xbe, 0x49,
71 	0x07, 0xf0, 0x9b, 0x6c, 0x22, 0xd5, 0xf4, 0x03, 0x4d, 0xba, 0xd1,
72 	0x26, 0x68, 0x9f
73 };
74 
75 
76 /**
77  * Return the size of type @p dt.
78  *
79  * @param dt NVRAM data type.
80  * @result the byte width of @p dt.
81  */
82 size_t
bhnd_nvram_type_width(bhnd_nvram_dt dt)83 bhnd_nvram_type_width(bhnd_nvram_dt dt)
84 {
85 	switch (dt) {
86 	case BHND_NVRAM_DT_INT8:
87 	case BHND_NVRAM_DT_UINT8:
88 	case BHND_NVRAM_DT_CHAR:
89 		return (sizeof(uint8_t));
90 
91 	case BHND_NVRAM_DT_INT16:
92 	case BHND_NVRAM_DT_UINT16:
93 		return (sizeof(uint16_t));
94 
95 	case BHND_NVRAM_DT_INT32:
96 	case BHND_NVRAM_DT_UINT32:
97 		return (sizeof(uint32_t));
98 	}
99 
100 	/* Quiesce gcc4.2 */
101 	panic("bhnd nvram data type %u unknown", dt);
102 }
103 
104 
105 /**
106  * Return the variable definition for @p varname, if any.
107  *
108  * @param varname variable name
109  *
110  * @retval bhnd_nvram_var If a valid definition for @p varname is found.
111  * @retval NULL If no definition for @p varname is found.
112  */
113 const struct bhnd_nvram_var *
bhnd_nvram_var_defn(const char * varname)114 bhnd_nvram_var_defn(const char *varname)
115 {
116 	size_t	min, mid, max;
117 	int	order;
118 
119 	/*
120 	 * Locate the requested variable using a binary search.
121 	 *
122 	 * The variable table is guaranteed to be sorted in lexicographical
123 	 * order (using the 'C' locale for collation rules)
124 	 */
125 	min = 0;
126 	mid = 0;
127 	max = nitems(bhnd_nvram_vars) - 1;
128 
129 	while (max >= min) {
130 		/* Select midpoint */
131 		mid = (min + max) / 2;
132 
133 		/* Determine which side of the partition to search */
134 		order = strcmp(bhnd_nvram_vars[mid].name, varname);
135 		if (order < 0) {
136 			/* Search upper partition */
137 			min = mid + 1;
138 		} else if (order > 0) {
139 			/* Search lower partition */
140 			max = mid - 1;
141 		} else if (order == 0) {
142 			/* Match found */
143 			return (&bhnd_nvram_vars[mid]);
144 		}
145 	}
146 
147 	/* Not found */
148 	return (NULL);
149 }
150