xref: /freebsd/stand/libsa/smbios.c (revision c5e433b99ed3ddef0eb4fa937f38c34d4a3c4ae0)
1a64f0b83SWarner Losh /*-
2a64f0b83SWarner Losh  * Copyright (c) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
3a64f0b83SWarner Losh  * All rights reserved.
4a64f0b83SWarner Losh  *
5a64f0b83SWarner Losh  * Redistribution and use in source and binary forms, with or without
6a64f0b83SWarner Losh  * modification, are permitted provided that the following conditions
7a64f0b83SWarner Losh  * are met:
8a64f0b83SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9a64f0b83SWarner Losh  *	notice, this list of conditions and the following disclaimer.
10a64f0b83SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11a64f0b83SWarner Losh  *	notice, this list of conditions and the following disclaimer in the
12a64f0b83SWarner Losh  *	documentation and/or other materials provided with the distribution.
13a64f0b83SWarner Losh  *
14a64f0b83SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a64f0b83SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a64f0b83SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a64f0b83SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a64f0b83SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a64f0b83SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a64f0b83SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a64f0b83SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a64f0b83SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a64f0b83SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a64f0b83SWarner Losh  * SUCH DAMAGE.
25a64f0b83SWarner Losh  */
26a64f0b83SWarner Losh 
27a64f0b83SWarner Losh #include <sys/cdefs.h>
28a64f0b83SWarner Losh __FBSDID("$FreeBSD$");
29a64f0b83SWarner Losh 
30a64f0b83SWarner Losh #include <stand.h>
31a64f0b83SWarner Losh #include <sys/endian.h>
32a64f0b83SWarner Losh 
33a64f0b83SWarner Losh #define PTOV(x)		ptov(x)
34a64f0b83SWarner Losh 
35ee97f198SJohn-Mark Gurney /* Only enable 64-bit entry point if it makes sense */
36ee97f198SJohn-Mark Gurney #if __SIZEOF_POINTER__ > 4
37ee97f198SJohn-Mark Gurney #define	HAS_SMBV3	1
38ee97f198SJohn-Mark Gurney #endif
39ee97f198SJohn-Mark Gurney 
40a64f0b83SWarner Losh /*
41a64f0b83SWarner Losh  * Detect SMBIOS and export information about the SMBIOS into the
42a64f0b83SWarner Losh  * environment.
43a64f0b83SWarner Losh  *
44a64f0b83SWarner Losh  * System Management BIOS Reference Specification, v2.6 Final
45a64f0b83SWarner Losh  * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf
46b5c3ade7SStephen J. Kiernan  *
47b5c3ade7SStephen J. Kiernan  * System Management BIOS (SMBIOS) Reference Specification, 3.6.0
48b5c3ade7SStephen J. Kiernan  * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.6.0.pdf
49a64f0b83SWarner Losh  */
50a64f0b83SWarner Losh 
51a64f0b83SWarner Losh /*
52b5c3ade7SStephen J. Kiernan  * The first quoted paragraph below can also be found in section 2.1.1 SMBIOS
53b5c3ade7SStephen J. Kiernan  * Structure Table Entry Point of System Management BIOS Reference
54b5c3ade7SStephen J. Kiernan  * Specification, v2.6 Final
55a64f0b83SWarner Losh  *
56b5c3ade7SStephen J. Kiernan  * (From System Management BIOS (SMBIOS) Reference Specification, 3.6.0)
57b5c3ade7SStephen J. Kiernan  * 5.2.1 SMBIOS 2.1 (32-bit) Entry Point
58b5c3ade7SStephen J. Kiernan  *
59b5c3ade7SStephen J. Kiernan  * "On non-UEFI systems, the 32-bit SMBIOS Entry Point structure, can be
60b5c3ade7SStephen J. Kiernan  * located by application software by searching for the anchor-string on
61b5c3ade7SStephen J. Kiernan  * paragraph (16-byte) boundaries within the physical memory address
62b5c3ade7SStephen J. Kiernan  * range 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate
63b5c3ade7SStephen J. Kiernan  * anchor string that is used by some existing DMI browsers.
64b5c3ade7SStephen J. Kiernan  *
65b5c3ade7SStephen J. Kiernan  * On UEFI-based systems, the SMBIOS Entry Point structure can be located by
66b5c3ade7SStephen J. Kiernan  * looking in the EFI Configuration Table for the SMBIOS GUID
67b5c3ade7SStephen J. Kiernan  * (SMBIOS_TABLE_GUID, {EB9D2D31-2D88-11D3-9A16-0090273FC14D}) and using the
68b5c3ade7SStephen J. Kiernan  * associated pointer. See section 4.6 of the UEFI Specification for details.
69b5c3ade7SStephen J. Kiernan  * See section 2.3 of the UEFI Specification for how to report the containing
70b5c3ade7SStephen J. Kiernan  * memory type.
71b5c3ade7SStephen J. Kiernan  *
72b5c3ade7SStephen J. Kiernan  * NOTE While the SMBIOS Major and Minor Versions (offsets 06h and 07h)
73b5c3ade7SStephen J. Kiernan  * currently duplicate the information that is present in the SMBIOS BCD
74b5c3ade7SStephen J. Kiernan  * Revision (offset 1Eh), they provide a path for future growth in this
75b5c3ade7SStephen J. Kiernan  * specification. The BCD Revision, for example, provides only a single digit
76b5c3ade7SStephen J. Kiernan  * for each of the major and minor version numbers."
77b5c3ade7SStephen J. Kiernan  *
78b5c3ade7SStephen J. Kiernan  * 5.2.2 SMBIOS 860 3.0 (64-bit) Entry Point
79b5c3ade7SStephen J. Kiernan  *
80b5c3ade7SStephen J. Kiernan  * "On non-UEFI systems, the 64-bit SMBIOS Entry Point structure can be located
81b5c3ade7SStephen J. Kiernan  * by application software by searching for the anchor-string on paragraph
82b5c3ade7SStephen J. Kiernan  * (16-byte) boundaries within the physical memory address range 000F0000h to
83b5c3ade7SStephen J. Kiernan  * 000FFFFFh.
84b5c3ade7SStephen J. Kiernan  *
85b5c3ade7SStephen J. Kiernan  * On UEFI-based systems, the SMBIOS Entry Point structure can be located by
86b5c3ade7SStephen J. Kiernan  * looking in the EFI Configuration Table for the SMBIOS 3.x GUID
87b5c3ade7SStephen J. Kiernan  * (SMBIOS3_TABLE_GUID, {F2FD1544-9794-4A2C-992E-E5BBCF20E394}) and using the
88b5c3ade7SStephen J. Kiernan  * associated pointer. See section 4.6 of the UEFI Specification for details.
89b5c3ade7SStephen J. Kiernan  * See section 2.3 of the UEFI Specification for how to report the containing
90b5c3ade7SStephen J. Kiernan  * memory type."
91a64f0b83SWarner Losh  */
92a64f0b83SWarner Losh #define	SMBIOS_START		0xf0000
93a64f0b83SWarner Losh #define	SMBIOS_LENGTH		0x10000
94a64f0b83SWarner Losh #define	SMBIOS_STEP		0x10
95a64f0b83SWarner Losh #define	SMBIOS_SIG		"_SM_"
96ee97f198SJohn-Mark Gurney #define	SMBIOS3_SIG		"_SM3_"
97a64f0b83SWarner Losh #define	SMBIOS_DMI_SIG		"_DMI_"
98a64f0b83SWarner Losh 
99*c5e433b9SWarner Losh /*
100*c5e433b9SWarner Losh  * 5.1 General
101*c5e433b9SWarner Losh  *...
102*c5e433b9SWarner Losh  * NOTE The Entry Point Structure and all SMBIOS structures assume a
103*c5e433b9SWarner Losh  * little-endian ordering convention...
104*c5e433b9SWarner Losh  * ...
105*c5e433b9SWarner Losh  *
106*c5e433b9SWarner Losh  * We use memcpy to avoid unaligned access to memory. To normal memory, this is
107*c5e433b9SWarner Losh  * fine, but the memory we are using might be mmap'd /dev/mem which under Linux
108*c5e433b9SWarner Losh  * on aarch64 doesn't allow unaligned access. leXdec and friends can't be used
109*c5e433b9SWarner Losh  * because those can optimize to an unaligned load (which often is fine, but not
110*c5e433b9SWarner Losh  * for mmap'd /dev/mem which has special memory attributes).
111*c5e433b9SWarner Losh  */
112*c5e433b9SWarner Losh static inline uint8_t SMBIOS_GET8(const caddr_t base, int off) { return (base[off]); }
113*c5e433b9SWarner Losh 
114*c5e433b9SWarner Losh static inline uint16_t
115*c5e433b9SWarner Losh SMBIOS_GET16(const caddr_t base, int off)
116*c5e433b9SWarner Losh {
117*c5e433b9SWarner Losh 	uint16_t v;
118*c5e433b9SWarner Losh 
119*c5e433b9SWarner Losh 	memcpy(&v, base + off, sizeof(v));
120*c5e433b9SWarner Losh 	return (le16toh(v));
121*c5e433b9SWarner Losh }
122*c5e433b9SWarner Losh 
123*c5e433b9SWarner Losh static inline uint32_t
124*c5e433b9SWarner Losh SMBIOS_GET32(const caddr_t base, int off)
125*c5e433b9SWarner Losh {
126*c5e433b9SWarner Losh 	uint32_t v;
127*c5e433b9SWarner Losh 
128*c5e433b9SWarner Losh 	memcpy(&v, base + off, sizeof(v));
129*c5e433b9SWarner Losh 	return (le32toh(v));
130*c5e433b9SWarner Losh }
131*c5e433b9SWarner Losh 
132*c5e433b9SWarner Losh static inline uint64_t
133*c5e433b9SWarner Losh SMBIOS_GET64(const caddr_t base, int off)
134*c5e433b9SWarner Losh {
135*c5e433b9SWarner Losh 	uint64_t v;
136*c5e433b9SWarner Losh 
137*c5e433b9SWarner Losh 	memcpy(&v, base + off, sizeof(v));
138*c5e433b9SWarner Losh 	return (le64toh(v));
139*c5e433b9SWarner Losh }
140a64f0b83SWarner Losh 
141a64f0b83SWarner Losh #define	SMBIOS_GETLEN(base)	SMBIOS_GET8(base, 0x01)
142a64f0b83SWarner Losh #define	SMBIOS_GETSTR(base)	((base) + SMBIOS_GETLEN(base))
143a64f0b83SWarner Losh 
144a64f0b83SWarner Losh struct smbios_attr {
145a64f0b83SWarner Losh 	int		probed;
146a64f0b83SWarner Losh 	caddr_t 	addr;
147a64f0b83SWarner Losh 	size_t		length;
148a64f0b83SWarner Losh 	size_t		count;
149a64f0b83SWarner Losh 	int		major;
150a64f0b83SWarner Losh 	int		minor;
151a64f0b83SWarner Losh 	int		ver;
152a64f0b83SWarner Losh 	const char*	bios_vendor;
153a64f0b83SWarner Losh 	const char*	maker;
154a64f0b83SWarner Losh 	const char*	product;
155a64f0b83SWarner Losh 	uint32_t	enabled_memory;
156a64f0b83SWarner Losh 	uint32_t	old_enabled_memory;
157a64f0b83SWarner Losh 	uint8_t		enabled_sockets;
158a64f0b83SWarner Losh 	uint8_t		populated_sockets;
159a64f0b83SWarner Losh };
160a64f0b83SWarner Losh 
161a64f0b83SWarner Losh static struct smbios_attr smbios;
162ee97f198SJohn-Mark Gurney #ifdef HAS_SMBV3
163ee97f198SJohn-Mark Gurney static int isv3;
164ee97f198SJohn-Mark Gurney #endif
165a64f0b83SWarner Losh 
166a64f0b83SWarner Losh static uint8_t
167a64f0b83SWarner Losh smbios_checksum(const caddr_t addr, const uint8_t len)
168a64f0b83SWarner Losh {
169a64f0b83SWarner Losh 	uint8_t		sum;
170a64f0b83SWarner Losh 	int		i;
171a64f0b83SWarner Losh 
172a64f0b83SWarner Losh 	for (sum = 0, i = 0; i < len; i++)
173a64f0b83SWarner Losh 		sum += SMBIOS_GET8(addr, i);
174a64f0b83SWarner Losh 	return (sum);
175a64f0b83SWarner Losh }
176a64f0b83SWarner Losh 
177a64f0b83SWarner Losh static caddr_t
178a64f0b83SWarner Losh smbios_sigsearch(const caddr_t addr, const uint32_t len)
179a64f0b83SWarner Losh {
180a64f0b83SWarner Losh 	caddr_t		cp;
181a64f0b83SWarner Losh 
182a64f0b83SWarner Losh 	/* Search on 16-byte boundaries. */
183ee97f198SJohn-Mark Gurney 	for (cp = addr; cp < addr + len; cp += SMBIOS_STEP) {
184ee97f198SJohn-Mark Gurney 		/* v2.1, 32-bit Entry point */
185ee97f198SJohn-Mark Gurney 		if (strncmp(cp, SMBIOS_SIG, sizeof(SMBIOS_SIG) - 1) == 0 &&
186a64f0b83SWarner Losh 		    smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 &&
187a64f0b83SWarner Losh 		    strncmp(cp + 0x10, SMBIOS_DMI_SIG, 5) == 0 &&
188a64f0b83SWarner Losh 		    smbios_checksum(cp + 0x10, 0x0f) == 0)
189a64f0b83SWarner Losh 			return (cp);
190ee97f198SJohn-Mark Gurney 
191ee97f198SJohn-Mark Gurney #ifdef HAS_SMBV3
192ee97f198SJohn-Mark Gurney 		/* v3.0, 64-bit Entry point */
193ee97f198SJohn-Mark Gurney 		if (strncmp(cp, SMBIOS3_SIG, sizeof(SMBIOS3_SIG) - 1) == 0 &&
194ee97f198SJohn-Mark Gurney 		    smbios_checksum(cp, SMBIOS_GET8(cp, 0x06)) == 0) {
195ee97f198SJohn-Mark Gurney 			isv3 = 1;
196ee97f198SJohn-Mark Gurney 			return (cp);
197ee97f198SJohn-Mark Gurney 		}
198ee97f198SJohn-Mark Gurney #endif
199ee97f198SJohn-Mark Gurney 	}
200a64f0b83SWarner Losh 	return (NULL);
201a64f0b83SWarner Losh }
202a64f0b83SWarner Losh 
203a64f0b83SWarner Losh static const char*
204a64f0b83SWarner Losh smbios_getstring(caddr_t addr, const int offset)
205a64f0b83SWarner Losh {
206a64f0b83SWarner Losh 	caddr_t		cp;
207a64f0b83SWarner Losh 	int		i, idx;
208a64f0b83SWarner Losh 
209a64f0b83SWarner Losh 	idx = SMBIOS_GET8(addr, offset);
210a64f0b83SWarner Losh 	if (idx != 0) {
211a64f0b83SWarner Losh 		cp = SMBIOS_GETSTR(addr);
212a64f0b83SWarner Losh 		for (i = 1; i < idx; i++)
213a64f0b83SWarner Losh 			cp += strlen(cp) + 1;
214a64f0b83SWarner Losh 		return cp;
215a64f0b83SWarner Losh 	}
216a64f0b83SWarner Losh 	return (NULL);
217a64f0b83SWarner Losh }
218a64f0b83SWarner Losh 
219a64f0b83SWarner Losh static void
220a64f0b83SWarner Losh smbios_setenv(const char *name, caddr_t addr, const int offset)
221a64f0b83SWarner Losh {
222a64f0b83SWarner Losh 	const char*	val;
223a64f0b83SWarner Losh 
224a64f0b83SWarner Losh 	val = smbios_getstring(addr, offset);
225a64f0b83SWarner Losh 	if (val != NULL)
226a64f0b83SWarner Losh 		setenv(name, val, 1);
227a64f0b83SWarner Losh }
228a64f0b83SWarner Losh 
229a64f0b83SWarner Losh #ifdef SMBIOS_SERIAL_NUMBERS
230a64f0b83SWarner Losh 
231a64f0b83SWarner Losh #define	UUID_SIZE		16
232a64f0b83SWarner Losh #define	UUID_TYPE		uint32_t
233a64f0b83SWarner Losh #define	UUID_STEP		sizeof(UUID_TYPE)
234a64f0b83SWarner Losh #define	UUID_ALL_BITS		(UUID_SIZE / UUID_STEP)
235*c5e433b9SWarner Losh #define	UUID_GET(base, off)	SMBIOS_GET32(base, off)
236a64f0b83SWarner Losh 
237a64f0b83SWarner Losh static void
23816e9ec44SWarner Losh smbios_setuuid(const char *name, const caddr_t addr, const int ver __unused)
239a64f0b83SWarner Losh {
240a64f0b83SWarner Losh 	char		uuid[37];
241a64f0b83SWarner Losh 	int		byteorder, i, ones, zeros;
242a64f0b83SWarner Losh 	UUID_TYPE	n;
243a64f0b83SWarner Losh 	uint32_t	f1;
244a64f0b83SWarner Losh 	uint16_t	f2, f3;
245a64f0b83SWarner Losh 
246a64f0b83SWarner Losh 	for (i = 0, ones = 0, zeros = 0; i < UUID_SIZE; i += UUID_STEP) {
247a64f0b83SWarner Losh 		n = UUID_GET(addr, i) + 1;
248a64f0b83SWarner Losh 		if (zeros == 0 && n == 0)
249a64f0b83SWarner Losh 			ones++;
250a64f0b83SWarner Losh 		else if (ones == 0 && n == 1)
251a64f0b83SWarner Losh 			zeros++;
252a64f0b83SWarner Losh 		else
253a64f0b83SWarner Losh 			break;
254a64f0b83SWarner Losh 	}
255a64f0b83SWarner Losh 
256a64f0b83SWarner Losh 	if (ones != UUID_ALL_BITS && zeros != UUID_ALL_BITS) {
257a64f0b83SWarner Losh 		/*
258a64f0b83SWarner Losh 		 * 3.3.2.1 System UUID
259a64f0b83SWarner Losh 		 *
260a64f0b83SWarner Losh 		 * "Although RFC 4122 recommends network byte order for all
261a64f0b83SWarner Losh 		 * fields, the PC industry (including the ACPI, UEFI, and
262a64f0b83SWarner Losh 		 * Microsoft specifications) has consistently used
263a64f0b83SWarner Losh 		 * little-endian byte encoding for the first three fields:
264a64f0b83SWarner Losh 		 * time_low, time_mid, time_hi_and_version. The same encoding,
265a64f0b83SWarner Losh 		 * also known as wire format, should also be used for the
266a64f0b83SWarner Losh 		 * SMBIOS representation of the UUID."
267a64f0b83SWarner Losh 		 *
268a64f0b83SWarner Losh 		 * Note: We use network byte order for backward compatibility
269a64f0b83SWarner Losh 		 * unless SMBIOS version is 2.6+ or little-endian is forced.
270a64f0b83SWarner Losh 		 */
271a64f0b83SWarner Losh #if defined(SMBIOS_LITTLE_ENDIAN_UUID)
272a64f0b83SWarner Losh 		byteorder = LITTLE_ENDIAN;
273a64f0b83SWarner Losh #elif defined(SMBIOS_NETWORK_ENDIAN_UUID)
274a64f0b83SWarner Losh 		byteorder = BIG_ENDIAN;
275a64f0b83SWarner Losh #else
276a64f0b83SWarner Losh 		byteorder = ver < 0x0206 ? BIG_ENDIAN : LITTLE_ENDIAN;
277a64f0b83SWarner Losh #endif
278a64f0b83SWarner Losh 		if (byteorder != LITTLE_ENDIAN) {
279a64f0b83SWarner Losh 			f1 = ntohl(SMBIOS_GET32(addr, 0));
280a64f0b83SWarner Losh 			f2 = ntohs(SMBIOS_GET16(addr, 4));
281a64f0b83SWarner Losh 			f3 = ntohs(SMBIOS_GET16(addr, 6));
282a64f0b83SWarner Losh 		} else {
283a64f0b83SWarner Losh 			f1 = le32toh(SMBIOS_GET32(addr, 0));
284a64f0b83SWarner Losh 			f2 = le16toh(SMBIOS_GET16(addr, 4));
285a64f0b83SWarner Losh 			f3 = le16toh(SMBIOS_GET16(addr, 6));
286a64f0b83SWarner Losh 		}
287a64f0b83SWarner Losh 		sprintf(uuid,
288a64f0b83SWarner Losh 		    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
289a64f0b83SWarner Losh 		    f1, f2, f3, SMBIOS_GET8(addr, 8), SMBIOS_GET8(addr, 9),
290a64f0b83SWarner Losh 		    SMBIOS_GET8(addr, 10), SMBIOS_GET8(addr, 11),
291a64f0b83SWarner Losh 		    SMBIOS_GET8(addr, 12), SMBIOS_GET8(addr, 13),
292a64f0b83SWarner Losh 		    SMBIOS_GET8(addr, 14), SMBIOS_GET8(addr, 15));
293a64f0b83SWarner Losh 		setenv(name, uuid, 1);
294a64f0b83SWarner Losh 	}
295a64f0b83SWarner Losh }
296a64f0b83SWarner Losh 
297a64f0b83SWarner Losh #undef UUID_SIZE
298a64f0b83SWarner Losh #undef UUID_TYPE
299a64f0b83SWarner Losh #undef UUID_STEP
300a64f0b83SWarner Losh #undef UUID_ALL_BITS
301a64f0b83SWarner Losh #undef UUID_GET
302a64f0b83SWarner Losh 
303a64f0b83SWarner Losh #endif
304a64f0b83SWarner Losh 
3059060f2c3SEmmanuel Vadot static const char *
3069060f2c3SEmmanuel Vadot smbios_parse_chassis_type(caddr_t addr)
3079060f2c3SEmmanuel Vadot {
3089060f2c3SEmmanuel Vadot 	int		type;
3099060f2c3SEmmanuel Vadot 
3109060f2c3SEmmanuel Vadot 	type = SMBIOS_GET8(addr, 0x5);
3119060f2c3SEmmanuel Vadot 	switch (type) {
3129060f2c3SEmmanuel Vadot 	case 0x1:
3139060f2c3SEmmanuel Vadot 		return ("Other");
3149060f2c3SEmmanuel Vadot 	case 0x2:
3159060f2c3SEmmanuel Vadot 		return ("Unknown");
3169060f2c3SEmmanuel Vadot 	case 0x3:
3179060f2c3SEmmanuel Vadot 		return ("Desktop");
3189060f2c3SEmmanuel Vadot 	case 0x4:
3199060f2c3SEmmanuel Vadot 		return ("Low Profile Desktop");
3209060f2c3SEmmanuel Vadot 	case 0x5:
3219060f2c3SEmmanuel Vadot 		return ("Pizza Box");
3229060f2c3SEmmanuel Vadot 	case 0x6:
3239060f2c3SEmmanuel Vadot 		return ("Mini Tower");
3249060f2c3SEmmanuel Vadot 	case 0x7:
3259060f2c3SEmmanuel Vadot 		return ("Tower");
3269060f2c3SEmmanuel Vadot 	case 0x8:
3279060f2c3SEmmanuel Vadot 		return ("Portable");
3289060f2c3SEmmanuel Vadot 	case 0x9:
3299060f2c3SEmmanuel Vadot 		return ("Laptop");
3309060f2c3SEmmanuel Vadot 	case 0xA:
3319060f2c3SEmmanuel Vadot 		return ("Notebook");
3329060f2c3SEmmanuel Vadot 	case 0xB:
3339060f2c3SEmmanuel Vadot 		return ("Hand Held");
3349060f2c3SEmmanuel Vadot 	case 0xC:
3359060f2c3SEmmanuel Vadot 		return ("Docking Station");
3369060f2c3SEmmanuel Vadot 	case 0xD:
3379060f2c3SEmmanuel Vadot 		return ("All in One");
3389060f2c3SEmmanuel Vadot 	case 0xE:
3399060f2c3SEmmanuel Vadot 		return ("Sub Notebook");
3409060f2c3SEmmanuel Vadot 	case 0xF:
3419060f2c3SEmmanuel Vadot 		return ("Lunch Box");
3429060f2c3SEmmanuel Vadot 	case 0x10:
3439060f2c3SEmmanuel Vadot 		return ("Space-saving");
3449060f2c3SEmmanuel Vadot 	case 0x11:
3459060f2c3SEmmanuel Vadot 		return ("Main Server Chassis");
3469060f2c3SEmmanuel Vadot 	case 0x12:
3479060f2c3SEmmanuel Vadot 		return ("Expansion Chassis");
3489060f2c3SEmmanuel Vadot 	case 0x13:
3499060f2c3SEmmanuel Vadot 		return ("SubChassis");
3509060f2c3SEmmanuel Vadot 	case 0x14:
3519060f2c3SEmmanuel Vadot 		return ("Bus Expansion Chassis");
3529060f2c3SEmmanuel Vadot 	case 0x15:
3539060f2c3SEmmanuel Vadot 		return ("Peripheral Chassis");
3549060f2c3SEmmanuel Vadot 	case 0x16:
3559060f2c3SEmmanuel Vadot 		return ("RAID Chassis");
3569060f2c3SEmmanuel Vadot 	case 0x17:
3579060f2c3SEmmanuel Vadot 		return ("Rack Mount Chassis");
3589060f2c3SEmmanuel Vadot 	case 0x18:
3599060f2c3SEmmanuel Vadot 		return ("Sealed-case PC");
3609060f2c3SEmmanuel Vadot 	case 0x19:
3619060f2c3SEmmanuel Vadot 		return ("Multi-system chassis");
3629060f2c3SEmmanuel Vadot 	case 0x1A:
3639060f2c3SEmmanuel Vadot 		return ("Compact PCI");
3649060f2c3SEmmanuel Vadot 	case 0x1B:
3659060f2c3SEmmanuel Vadot 		return ("Advanced TCA");
3669060f2c3SEmmanuel Vadot 	case 0x1C:
3679060f2c3SEmmanuel Vadot 		return ("Blade");
3689060f2c3SEmmanuel Vadot 	case 0x1D:
3699060f2c3SEmmanuel Vadot 		return ("Blade Enclosure");
3709060f2c3SEmmanuel Vadot 	case 0x1E:
3719060f2c3SEmmanuel Vadot 		return ("Tablet");
3729060f2c3SEmmanuel Vadot 	case 0x1F:
3739060f2c3SEmmanuel Vadot 		return ("Convertible");
3749060f2c3SEmmanuel Vadot 	case 0x20:
3759060f2c3SEmmanuel Vadot 		return ("Detachable");
3769060f2c3SEmmanuel Vadot 	case 0x21:
3779060f2c3SEmmanuel Vadot 		return ("IoT Gateway");
3789060f2c3SEmmanuel Vadot 	case 0x22:
3799060f2c3SEmmanuel Vadot 		return ("Embedded PC");
3809060f2c3SEmmanuel Vadot 	case 0x23:
3819060f2c3SEmmanuel Vadot 		return ("Mini PC");
3829060f2c3SEmmanuel Vadot 	case 0x24:
3839060f2c3SEmmanuel Vadot 		return ("Stick PC");
3849060f2c3SEmmanuel Vadot 	}
3859060f2c3SEmmanuel Vadot 
3869060f2c3SEmmanuel Vadot 	return ("Undefined");
3879060f2c3SEmmanuel Vadot }
3889060f2c3SEmmanuel Vadot 
389a64f0b83SWarner Losh static caddr_t
390a64f0b83SWarner Losh smbios_parse_table(const caddr_t addr)
391a64f0b83SWarner Losh {
392a64f0b83SWarner Losh 	caddr_t		cp;
393a64f0b83SWarner Losh 	int		proc, size, osize, type;
39466c73af7SKornel Dulęba 	uint8_t		bios_minor, bios_major;
39566c73af7SKornel Dulęba 	char		buf[16];
396a64f0b83SWarner Losh 
397a64f0b83SWarner Losh 	type = SMBIOS_GET8(addr, 0);	/* 3.1.2 Structure Header Format */
398a64f0b83SWarner Losh 	switch(type) {
399a64f0b83SWarner Losh 	case 0:		/* 3.3.1 BIOS Information (Type 0) */
400a64f0b83SWarner Losh 		smbios_setenv("smbios.bios.vendor", addr, 0x04);
401a64f0b83SWarner Losh 		smbios_setenv("smbios.bios.version", addr, 0x05);
402a64f0b83SWarner Losh 		smbios_setenv("smbios.bios.reldate", addr, 0x08);
40366c73af7SKornel Dulęba 		bios_major = SMBIOS_GET8(addr, 0x14);
40466c73af7SKornel Dulęba 		bios_minor = SMBIOS_GET8(addr, 0x15);
40566c73af7SKornel Dulęba 		if (bios_minor != 0xFF && bios_major != 0xFF) {
40666c73af7SKornel Dulęba 			snprintf(buf, sizeof(buf), "%u.%u",
40766c73af7SKornel Dulęba 			    bios_major, bios_minor);
40866c73af7SKornel Dulęba 			setenv("smbios.bios.revision", buf, 1);
40966c73af7SKornel Dulęba 		}
410a64f0b83SWarner Losh 		break;
411a64f0b83SWarner Losh 
412a64f0b83SWarner Losh 	case 1:		/* 3.3.2 System Information (Type 1) */
413a64f0b83SWarner Losh 		smbios_setenv("smbios.system.maker", addr, 0x04);
414a64f0b83SWarner Losh 		smbios_setenv("smbios.system.product", addr, 0x05);
415a64f0b83SWarner Losh 		smbios_setenv("smbios.system.version", addr, 0x06);
416a64f0b83SWarner Losh #ifdef SMBIOS_SERIAL_NUMBERS
417a64f0b83SWarner Losh 		smbios_setenv("smbios.system.serial", addr, 0x07);
418a64f0b83SWarner Losh 		smbios_setuuid("smbios.system.uuid", addr + 0x08, smbios.ver);
419a64f0b83SWarner Losh #endif
420a64f0b83SWarner Losh 		if (smbios.major > 2 ||
421a64f0b83SWarner Losh 		    (smbios.major == 2 && smbios.minor >= 4)) {
422a64f0b83SWarner Losh 			smbios_setenv("smbios.system.sku", addr, 0x19);
423a64f0b83SWarner Losh 			smbios_setenv("smbios.system.family", addr, 0x1a);
424a64f0b83SWarner Losh 		}
425a64f0b83SWarner Losh 		break;
426a64f0b83SWarner Losh 
427a64f0b83SWarner Losh 	case 2:		/* 3.3.3 Base Board (or Module) Information (Type 2) */
428a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.maker", addr, 0x04);
429a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.product", addr, 0x05);
430a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.version", addr, 0x06);
431a64f0b83SWarner Losh #ifdef SMBIOS_SERIAL_NUMBERS
432a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.serial", addr, 0x07);
433a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.tag", addr, 0x08);
434a64f0b83SWarner Losh #endif
435a64f0b83SWarner Losh 		smbios_setenv("smbios.planar.location", addr, 0x0a);
436a64f0b83SWarner Losh 		break;
437a64f0b83SWarner Losh 
438a64f0b83SWarner Losh 	case 3:		/* 3.3.4 System Enclosure or Chassis (Type 3) */
439a64f0b83SWarner Losh 		smbios_setenv("smbios.chassis.maker", addr, 0x04);
4409060f2c3SEmmanuel Vadot 		setenv("smbios.chassis.type", smbios_parse_chassis_type(addr), 1);
441a64f0b83SWarner Losh 		smbios_setenv("smbios.chassis.version", addr, 0x06);
442a64f0b83SWarner Losh #ifdef SMBIOS_SERIAL_NUMBERS
443a64f0b83SWarner Losh 		smbios_setenv("smbios.chassis.serial", addr, 0x07);
444a64f0b83SWarner Losh 		smbios_setenv("smbios.chassis.tag", addr, 0x08);
445a64f0b83SWarner Losh #endif
446a64f0b83SWarner Losh 		break;
447a64f0b83SWarner Losh 
448a64f0b83SWarner Losh 	case 4:		/* 3.3.5 Processor Information (Type 4) */
449a64f0b83SWarner Losh 		/*
450a64f0b83SWarner Losh 		 * Offset 18h: Processor Status
451a64f0b83SWarner Losh 		 *
452a64f0b83SWarner Losh 		 * Bit 7	Reserved, must be 0
453a64f0b83SWarner Losh 		 * Bit 6	CPU Socket Populated
454a64f0b83SWarner Losh 		 *		1 - CPU Socket Populated
455a64f0b83SWarner Losh 		 *		0 - CPU Socket Unpopulated
456a64f0b83SWarner Losh 		 * Bit 5:3	Reserved, must be zero
457a64f0b83SWarner Losh 		 * Bit 2:0	CPU Status
458a64f0b83SWarner Losh 		 *		0h - Unknown
459a64f0b83SWarner Losh 		 *		1h - CPU Enabled
460a64f0b83SWarner Losh 		 *		2h - CPU Disabled by User via BIOS Setup
461a64f0b83SWarner Losh 		 *		3h - CPU Disabled by BIOS (POST Error)
462a64f0b83SWarner Losh 		 *		4h - CPU is Idle, waiting to be enabled
463a64f0b83SWarner Losh 		 *		5-6h - Reserved
464a64f0b83SWarner Losh 		 *		7h - Other
465a64f0b83SWarner Losh 		 */
466a64f0b83SWarner Losh 		proc = SMBIOS_GET8(addr, 0x18);
467a64f0b83SWarner Losh 		if ((proc & 0x07) == 1)
468a64f0b83SWarner Losh 			smbios.enabled_sockets++;
469a64f0b83SWarner Losh 		if ((proc & 0x40) != 0)
470a64f0b83SWarner Losh 			smbios.populated_sockets++;
471a64f0b83SWarner Losh 		break;
472a64f0b83SWarner Losh 
473a64f0b83SWarner Losh 	case 6:		/* 3.3.7 Memory Module Information (Type 6, Obsolete) */
474a64f0b83SWarner Losh 		/*
475a64f0b83SWarner Losh 		 * Offset 0Ah: Enabled Size
476a64f0b83SWarner Losh 		 *
477a64f0b83SWarner Losh 		 * Bit 7	Bank connection
478a64f0b83SWarner Losh 		 *		1 - Double-bank connection
479a64f0b83SWarner Losh 		 *		0 - Single-bank connection
480a64f0b83SWarner Losh 		 * Bit 6:0	Size (n), where 2**n is the size in MB
481a64f0b83SWarner Losh 		 *		7Dh - Not determinable (Installed Size only)
482a64f0b83SWarner Losh 		 *		7Eh - Module is installed, but no memory
483a64f0b83SWarner Losh 		 *		      has been enabled
484a64f0b83SWarner Losh 		 *		7Fh - Not installed
485a64f0b83SWarner Losh 		 */
486a64f0b83SWarner Losh 		osize = SMBIOS_GET8(addr, 0x0a) & 0x7f;
487a64f0b83SWarner Losh 		if (osize > 0 && osize < 22)
488a64f0b83SWarner Losh 			smbios.old_enabled_memory += 1 << (osize + 10);
489a64f0b83SWarner Losh 		break;
490a64f0b83SWarner Losh 
491a64f0b83SWarner Losh 	case 17:	/* 3.3.18 Memory Device (Type 17) */
492a64f0b83SWarner Losh 		/*
493a64f0b83SWarner Losh 		 * Offset 0Ch: Size
494a64f0b83SWarner Losh 		 *
495a64f0b83SWarner Losh 		 * Bit 15	Granularity
496a64f0b83SWarner Losh 		 *		1 - Value is in kilobytes units
497a64f0b83SWarner Losh 		 *		0 - Value is in megabytes units
498a64f0b83SWarner Losh 		 * Bit 14:0	Size
499a64f0b83SWarner Losh 		 */
500a64f0b83SWarner Losh 		size = SMBIOS_GET16(addr, 0x0c);
501a64f0b83SWarner Losh 		if (size != 0 && size != 0xffff)
502a64f0b83SWarner Losh 			smbios.enabled_memory += (size & 0x8000) != 0 ?
503a64f0b83SWarner Losh 			    (size & 0x7fff) : (size << 10);
504a64f0b83SWarner Losh 		break;
505a64f0b83SWarner Losh 
506a64f0b83SWarner Losh 	default:	/* skip other types */
507a64f0b83SWarner Losh 		break;
508a64f0b83SWarner Losh 	}
509a64f0b83SWarner Losh 
510a64f0b83SWarner Losh 	/* Find structure terminator. */
511a64f0b83SWarner Losh 	cp = SMBIOS_GETSTR(addr);
512a64f0b83SWarner Losh 	while (SMBIOS_GET16(cp, 0) != 0)
513a64f0b83SWarner Losh 		cp++;
514a64f0b83SWarner Losh 
515a64f0b83SWarner Losh 	return (cp + 2);
516a64f0b83SWarner Losh }
517a64f0b83SWarner Losh 
518a64f0b83SWarner Losh static caddr_t
519a64f0b83SWarner Losh smbios_find_struct(int type)
520a64f0b83SWarner Losh {
521a64f0b83SWarner Losh 	caddr_t		dmi;
522a64f0b83SWarner Losh 	size_t		i;
523a64f0b83SWarner Losh 
524a64f0b83SWarner Losh 	if (smbios.addr == NULL)
525a64f0b83SWarner Losh 		return (NULL);
526a64f0b83SWarner Losh 
527a64f0b83SWarner Losh 	for (dmi = smbios.addr, i = 0;
528a64f0b83SWarner Losh 	     dmi < smbios.addr + smbios.length && i < smbios.count; i++) {
529a64f0b83SWarner Losh 		if (SMBIOS_GET8(dmi, 0) == type)
530a64f0b83SWarner Losh 			return dmi;
531a64f0b83SWarner Losh 		/* Find structure terminator. */
532a64f0b83SWarner Losh 		dmi = SMBIOS_GETSTR(dmi);
533a64f0b83SWarner Losh 		while (SMBIOS_GET16(dmi, 0) != 0)
534a64f0b83SWarner Losh 			dmi++;
535a64f0b83SWarner Losh 		dmi += 2;
536a64f0b83SWarner Losh 	}
537a64f0b83SWarner Losh 
538a64f0b83SWarner Losh 	return (NULL);
539a64f0b83SWarner Losh }
540a64f0b83SWarner Losh 
541a64f0b83SWarner Losh static void
542a64f0b83SWarner Losh smbios_probe(const caddr_t addr)
543a64f0b83SWarner Losh {
544a64f0b83SWarner Losh 	caddr_t		saddr, info;
545a64f0b83SWarner Losh 	uintptr_t	paddr;
546ee97f198SJohn-Mark Gurney 	int		maj_off;
547ee97f198SJohn-Mark Gurney 	int		min_off;
548a64f0b83SWarner Losh 
549a64f0b83SWarner Losh 	if (smbios.probed)
550a64f0b83SWarner Losh 		return;
551a64f0b83SWarner Losh 	smbios.probed = 1;
552a64f0b83SWarner Losh 
553a64f0b83SWarner Losh 	/* Search signatures and validate checksums. */
554a64f0b83SWarner Losh 	saddr = smbios_sigsearch(addr ? addr : PTOV(SMBIOS_START),
555a64f0b83SWarner Losh 	    SMBIOS_LENGTH);
556a64f0b83SWarner Losh 	if (saddr == NULL)
557a64f0b83SWarner Losh 		return;
558a64f0b83SWarner Losh 
559ee97f198SJohn-Mark Gurney #ifdef HAS_SMBV3
560ee97f198SJohn-Mark Gurney 	if (isv3) {
56113597be9SJohn-Mark Gurney 		smbios.length = SMBIOS_GET32(saddr, 0x0c);	/* Structure Table Length */
562ee97f198SJohn-Mark Gurney 		paddr = SMBIOS_GET64(saddr, 0x10);		/* Structure Table Address */
563ee97f198SJohn-Mark Gurney 		smbios.count = -1;				/* not present in V3 */
564ee97f198SJohn-Mark Gurney 		smbios.ver = 0;					/* not present in V3 */
565ee97f198SJohn-Mark Gurney 		maj_off = 0x07;
566ee97f198SJohn-Mark Gurney 		min_off = 0x08;
567ee97f198SJohn-Mark Gurney 	} else
568ee97f198SJohn-Mark Gurney #endif
569ee97f198SJohn-Mark Gurney 	{
570a64f0b83SWarner Losh 		smbios.length = SMBIOS_GET16(saddr, 0x16);	/* Structure Table Length */
571a64f0b83SWarner Losh 		paddr = SMBIOS_GET32(saddr, 0x18);		/* Structure Table Address */
572a64f0b83SWarner Losh 		smbios.count = SMBIOS_GET16(saddr, 0x1c);	/* No of SMBIOS Structures */
573a64f0b83SWarner Losh 		smbios.ver = SMBIOS_GET8(saddr, 0x1e);		/* SMBIOS BCD Revision */
574ee97f198SJohn-Mark Gurney 		maj_off = 0x06;
575ee97f198SJohn-Mark Gurney 		min_off = 0x07;
576ee97f198SJohn-Mark Gurney 	}
577ee97f198SJohn-Mark Gurney 
578a64f0b83SWarner Losh 
579a64f0b83SWarner Losh 	if (smbios.ver != 0) {
580a64f0b83SWarner Losh 		smbios.major = smbios.ver >> 4;
581a64f0b83SWarner Losh 		smbios.minor = smbios.ver & 0x0f;
582a64f0b83SWarner Losh 		if (smbios.major > 9 || smbios.minor > 9)
583a64f0b83SWarner Losh 			smbios.ver = 0;
584a64f0b83SWarner Losh 	}
585a64f0b83SWarner Losh 	if (smbios.ver == 0) {
586ee97f198SJohn-Mark Gurney 		smbios.major = SMBIOS_GET8(saddr, maj_off);/* SMBIOS Major Version */
587ee97f198SJohn-Mark Gurney 		smbios.minor = SMBIOS_GET8(saddr, min_off);/* SMBIOS Minor Version */
588a64f0b83SWarner Losh 	}
589a64f0b83SWarner Losh 	smbios.ver = (smbios.major << 8) | smbios.minor;
590a64f0b83SWarner Losh 	smbios.addr = PTOV(paddr);
591a64f0b83SWarner Losh 
592a64f0b83SWarner Losh 	/* Get system information from SMBIOS */
593a64f0b83SWarner Losh 	info = smbios_find_struct(0x00);
594a64f0b83SWarner Losh 	if (info != NULL) {
595a64f0b83SWarner Losh 		smbios.bios_vendor = smbios_getstring(info, 0x04);
596a64f0b83SWarner Losh 	}
597a64f0b83SWarner Losh 	info = smbios_find_struct(0x01);
598a64f0b83SWarner Losh 	if (info != NULL) {
599a64f0b83SWarner Losh 		smbios.maker = smbios_getstring(info, 0x04);
600a64f0b83SWarner Losh 		smbios.product = smbios_getstring(info, 0x05);
601a64f0b83SWarner Losh 	}
602a64f0b83SWarner Losh }
603a64f0b83SWarner Losh 
604a64f0b83SWarner Losh void
605a64f0b83SWarner Losh smbios_detect(const caddr_t addr)
606a64f0b83SWarner Losh {
607a64f0b83SWarner Losh 	char		buf[16];
608a64f0b83SWarner Losh 	caddr_t		dmi;
609a64f0b83SWarner Losh 	size_t		i;
610a64f0b83SWarner Losh 
611a64f0b83SWarner Losh 	smbios_probe(addr);
612a64f0b83SWarner Losh 	if (smbios.addr == NULL)
613a64f0b83SWarner Losh 		return;
614a64f0b83SWarner Losh 
615a64f0b83SWarner Losh 	for (dmi = smbios.addr, i = 0;
616a64f0b83SWarner Losh 	     dmi < smbios.addr + smbios.length && i < smbios.count; i++)
617a64f0b83SWarner Losh 		dmi = smbios_parse_table(dmi);
618a64f0b83SWarner Losh 
619a64f0b83SWarner Losh 	sprintf(buf, "%d.%d", smbios.major, smbios.minor);
620a64f0b83SWarner Losh 	setenv("smbios.version", buf, 1);
621a64f0b83SWarner Losh 	if (smbios.enabled_memory > 0 || smbios.old_enabled_memory > 0) {
622a64f0b83SWarner Losh 		sprintf(buf, "%u", smbios.enabled_memory > 0 ?
623a64f0b83SWarner Losh 		    smbios.enabled_memory : smbios.old_enabled_memory);
624a64f0b83SWarner Losh 		setenv("smbios.memory.enabled", buf, 1);
625a64f0b83SWarner Losh 	}
626a64f0b83SWarner Losh 	if (smbios.enabled_sockets > 0) {
627a64f0b83SWarner Losh 		sprintf(buf, "%u", smbios.enabled_sockets);
628a64f0b83SWarner Losh 		setenv("smbios.socket.enabled", buf, 1);
629a64f0b83SWarner Losh 	}
630a64f0b83SWarner Losh 	if (smbios.populated_sockets > 0) {
631a64f0b83SWarner Losh 		sprintf(buf, "%u", smbios.populated_sockets);
632a64f0b83SWarner Losh 		setenv("smbios.socket.populated", buf, 1);
633a64f0b83SWarner Losh 	}
634a64f0b83SWarner Losh }
635a64f0b83SWarner Losh 
636a64f0b83SWarner Losh static int
637a64f0b83SWarner Losh smbios_match_str(const char* s1, const char* s2)
638a64f0b83SWarner Losh {
639a64f0b83SWarner Losh 	return (s1 == NULL || (s2 != NULL && !strcmp(s1, s2)));
640a64f0b83SWarner Losh }
641a64f0b83SWarner Losh 
642a64f0b83SWarner Losh int
643a64f0b83SWarner Losh smbios_match(const char* bios_vendor, const char* maker,
644a64f0b83SWarner Losh     const char* product)
645a64f0b83SWarner Losh {
646a64f0b83SWarner Losh 	/* XXXRP currently, only called from non-EFI. */
647a64f0b83SWarner Losh 	smbios_probe(NULL);
648a64f0b83SWarner Losh 	return (smbios_match_str(bios_vendor, smbios.bios_vendor) &&
649a64f0b83SWarner Losh 	    smbios_match_str(maker, smbios.maker) &&
650a64f0b83SWarner Losh 	    smbios_match_str(product, smbios.product));
651a64f0b83SWarner Losh }
652