xref: /freebsd/stand/libsa/smbios.c (revision d3bfcd66409befc2d545e5449963b41c25c369a9)
1 /*-
2  * Copyright (c) 2005-2009 Jung-uk Kim <jkim@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  * 2. Redistributions in binary form must reproduce the above copyright
11  *	notice, this list of conditions and the following disclaimer in the
12  *	documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <stand.h>
28 #include <sys/endian.h>
29 
30 #define PTOV(x)		ptov(x)
31 
32 /*
33  * Detect SMBIOS and export information about the SMBIOS into the
34  * environment.
35  *
36  * System Management BIOS Reference Specification, v2.6 Final
37  * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf
38  *
39  * System Management BIOS (SMBIOS) Reference Specification, 3.6.0
40  * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.6.0.pdf
41  */
42 
43 /*
44  * The first quoted paragraph below can also be found in section 2.1.1 SMBIOS
45  * Structure Table Entry Point of System Management BIOS Reference
46  * Specification, v2.6 Final
47  *
48  * (From System Management BIOS (SMBIOS) Reference Specification, 3.6.0)
49  * 5.2.1 SMBIOS 2.1 (32-bit) Entry Point
50  *
51  * "On non-UEFI systems, the 32-bit SMBIOS Entry Point structure, can be
52  * located by application software by searching for the anchor-string on
53  * paragraph (16-byte) boundaries within the physical memory address
54  * range 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate
55  * anchor string that is used by some existing DMI browsers.
56  *
57  * On UEFI-based systems, the SMBIOS Entry Point structure can be located by
58  * looking in the EFI Configuration Table for the SMBIOS GUID
59  * (SMBIOS_TABLE_GUID, {EB9D2D31-2D88-11D3-9A16-0090273FC14D}) and using the
60  * associated pointer. See section 4.6 of the UEFI Specification for details.
61  * See section 2.3 of the UEFI Specification for how to report the containing
62  * memory type.
63  *
64  * NOTE While the SMBIOS Major and Minor Versions (offsets 06h and 07h)
65  * currently duplicate the information that is present in the SMBIOS BCD
66  * Revision (offset 1Eh), they provide a path for future growth in this
67  * specification. The BCD Revision, for example, provides only a single digit
68  * for each of the major and minor version numbers."
69  *
70  * 5.2.2 SMBIOS 860 3.0 (64-bit) Entry Point
71  *
72  * "On non-UEFI systems, the 64-bit SMBIOS Entry Point structure can be located
73  * by application software by searching for the anchor-string on paragraph
74  * (16-byte) boundaries within the physical memory address range 000F0000h to
75  * 000FFFFFh.
76  *
77  * On UEFI-based systems, the SMBIOS Entry Point structure can be located by
78  * looking in the EFI Configuration Table for the SMBIOS 3.x GUID
79  * (SMBIOS3_TABLE_GUID, {F2FD1544-9794-4A2C-992E-E5BBCF20E394}) and using the
80  * associated pointer. See section 4.6 of the UEFI Specification for details.
81  * See section 2.3 of the UEFI Specification for how to report the containing
82  * memory type."
83  */
84 #define	SMBIOS_START		0xf0000
85 #define	SMBIOS_LENGTH		0x10000
86 #define	SMBIOS_STEP		0x10
87 #define	SMBIOS_SIG		"_SM_"
88 #define	SMBIOS3_SIG		"_SM3_"
89 #define	SMBIOS_DMI_SIG		"_DMI_"
90 #define	SMBIOS_EOT_TYPE		0x7f
91 
92 /*
93  * 5.1 General
94  *...
95  * NOTE The Entry Point Structure and all SMBIOS structures assume a
96  * little-endian ordering convention...
97  * ...
98  *
99  * We use memcpy to avoid unaligned access to memory. To normal memory, this is
100  * fine, but the memory we are using might be mmap'd /dev/mem which under Linux
101  * on aarch64 doesn't allow unaligned access. leXdec and friends can't be used
102  * because those can optimize to an unaligned load (which often is fine, but not
103  * for mmap'd /dev/mem which has special memory attributes).
104  */
105 static inline uint8_t
SMBIOS_GET8(const caddr_t base,int off)106 SMBIOS_GET8(const caddr_t base, int off)
107 {
108 	return (base[off]);
109 }
110 
111 static inline uint16_t
SMBIOS_GET16(const caddr_t base,int off)112 SMBIOS_GET16(const caddr_t base, int off)
113 {
114 	uint16_t v;
115 
116 	memcpy(&v, base + off, sizeof(v));
117 	return (le16toh(v));
118 }
119 
120 static inline uint32_t
SMBIOS_GET32(const caddr_t base,int off)121 SMBIOS_GET32(const caddr_t base, int off)
122 {
123 	uint32_t v;
124 
125 	memcpy(&v, base + off, sizeof(v));
126 	return (le32toh(v));
127 }
128 
129 static inline uint64_t
SMBIOS_GET64(const caddr_t base,int off)130 SMBIOS_GET64(const caddr_t base, int off)
131 {
132 	uint64_t v;
133 
134 	memcpy(&v, base + off, sizeof(v));
135 	return (le64toh(v));
136 }
137 
138 #define	SMBIOS_GETLEN(base)	SMBIOS_GET8(base, 0x01)
139 #define	SMBIOS_GETSTR(base)	((base) + SMBIOS_GETLEN(base))
140 
141 struct smbios_attr {
142 	int		is_64bit_ep;
143 	caddr_t		addr;
144 	size_t		length;
145 	size_t		count;
146 	int		major;
147 	int		minor;
148 	int		ver;
149 	const char*	bios_vendor;
150 	const char*	maker;
151 	const char*	product;
152 	uint32_t	enabled_memory;
153 	uint32_t	old_enabled_memory;
154 	uint8_t		enabled_sockets;
155 	uint8_t		populated_sockets;
156 };
157 
158 static struct smbios_attr smbios;
159 
160 static uint8_t
smbios_checksum(const caddr_t addr,const uint8_t len)161 smbios_checksum(const caddr_t addr, const uint8_t len)
162 {
163 	uint8_t		sum;
164 	int		i;
165 
166 	for (sum = 0, i = 0; i < len; i++)
167 		sum += SMBIOS_GET8(addr, i);
168 	return (sum);
169 }
170 
171 static caddr_t
smbios_sigsearch(const caddr_t addr,const uint32_t len)172 smbios_sigsearch(const caddr_t addr, const uint32_t len)
173 {
174 	caddr_t		cp;
175 	caddr_t		v2_p = NULL;
176 
177 	/* Search on 16-byte boundaries. */
178 	for (cp = addr; cp < addr + len; cp += SMBIOS_STEP) {
179 		/* v3.0, 64-bit Entry point */
180 		if (strncmp(cp, SMBIOS3_SIG, sizeof(SMBIOS3_SIG) - 1) == 0 &&
181 		    /*
182 		     * The specification only guarantees the presence of the
183 		     * Structure Table Maximum Size and Address Entry fields at
184 		     * offsets 0x0c and 0x10 if the Entry Point Revision is not
185 		     * 0.
186 		     */
187 		    SMBIOS_GET8(cp, 0x0a) != 0 &&
188 		    smbios_checksum(cp, SMBIOS_GET8(cp, 0x06)) == 0) {
189 #if __SIZEOF_SIZE_T__ < 8
190 			uint64_t end_addr;
191 
192 			end_addr = SMBIOS_GET64(cp, 0x10) + /* Start address. */
193 			    SMBIOS_GET32(cp, 0x0c); /* Maximum size. */
194 			/*
195 			 * Is the table (or part of it) located above what we
196 			 * can address?
197 			 */
198 			if ((size_t)end_addr != end_addr)
199 				/* Yes, give it up. */
200 				continue;
201 #endif
202 			smbios.is_64bit_ep = 1;
203 			return (cp);
204 		}
205 
206 		/* v2.1, 32-bit Entry point */
207 		if (strncmp(cp, SMBIOS_SIG, sizeof(SMBIOS_SIG) - 1) == 0 &&
208 		    smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 &&
209 		    strncmp(cp + 0x10, SMBIOS_DMI_SIG, 5) == 0 &&
210 		    smbios_checksum(cp + 0x10, 0x0f) == 0) {
211 			/*
212 			 * Note that we saw this entry point, but don't return
213 			 * it right now as we favor the 64-bit one if present.
214 			 */
215 			v2_p = cp;
216 		}
217 	}
218 	return (v2_p);
219 }
220 
221 static const char*
smbios_getstring(caddr_t addr,const int offset)222 smbios_getstring(caddr_t addr, const int offset)
223 {
224 	caddr_t		cp;
225 	int		i, idx;
226 
227 	idx = SMBIOS_GET8(addr, offset);
228 	if (idx != 0) {
229 		cp = SMBIOS_GETSTR(addr);
230 		for (i = 1; i < idx; i++)
231 			cp += strlen(cp) + 1;
232 		return cp;
233 	}
234 	return (NULL);
235 }
236 
237 static void
smbios_setenv(const char * name,caddr_t addr,const int offset)238 smbios_setenv(const char *name, caddr_t addr, const int offset)
239 {
240 	const char*	val;
241 
242 	val = smbios_getstring(addr, offset);
243 	if (val != NULL)
244 		setenv(name, val, 1);
245 }
246 
247 #ifdef SMBIOS_SERIAL_NUMBERS
248 
249 #define	UUID_SIZE		16
250 #define	UUID_TYPE		uint32_t
251 #define	UUID_STEP		sizeof(UUID_TYPE)
252 #define	UUID_ALL_BITS		(UUID_SIZE / UUID_STEP)
253 #define	UUID_GET(base, off)	SMBIOS_GET32(base, off)
254 
255 static void
smbios_setuuid(const char * name,const caddr_t addr,const int ver __unused)256 smbios_setuuid(const char *name, const caddr_t addr, const int ver __unused)
257 {
258 	char		uuid[37];
259 	int		byteorder, i, ones, zeros;
260 	UUID_TYPE	n;
261 	uint32_t	f1;
262 	uint16_t	f2, f3;
263 
264 	for (i = 0, ones = 0, zeros = 0; i < UUID_SIZE; i += UUID_STEP) {
265 		n = UUID_GET(addr, i) + 1;
266 		if (zeros == 0 && n == 0)
267 			ones++;
268 		else if (ones == 0 && n == 1)
269 			zeros++;
270 		else
271 			break;
272 	}
273 
274 	if (ones != UUID_ALL_BITS && zeros != UUID_ALL_BITS) {
275 		/*
276 		 * 3.3.2.1 System UUID
277 		 *
278 		 * "Although RFC 4122 recommends network byte order for all
279 		 * fields, the PC industry (including the ACPI, UEFI, and
280 		 * Microsoft specifications) has consistently used
281 		 * little-endian byte encoding for the first three fields:
282 		 * time_low, time_mid, time_hi_and_version. The same encoding,
283 		 * also known as wire format, should also be used for the
284 		 * SMBIOS representation of the UUID."
285 		 *
286 		 * Note: We use network byte order for backward compatibility
287 		 * unless SMBIOS version is 2.6+ or little-endian is forced.
288 		 */
289 #if defined(SMBIOS_LITTLE_ENDIAN_UUID)
290 		byteorder = LITTLE_ENDIAN;
291 #elif defined(SMBIOS_NETWORK_ENDIAN_UUID)
292 		byteorder = BIG_ENDIAN;
293 #else
294 		byteorder = ver < 0x0206 ? BIG_ENDIAN : LITTLE_ENDIAN;
295 #endif
296 		if (byteorder != LITTLE_ENDIAN) {
297 			f1 = ntohl(SMBIOS_GET32(addr, 0));
298 			f2 = ntohs(SMBIOS_GET16(addr, 4));
299 			f3 = ntohs(SMBIOS_GET16(addr, 6));
300 		} else {
301 			f1 = le32toh(SMBIOS_GET32(addr, 0));
302 			f2 = le16toh(SMBIOS_GET16(addr, 4));
303 			f3 = le16toh(SMBIOS_GET16(addr, 6));
304 		}
305 		sprintf(uuid,
306 		    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
307 		    f1, f2, f3, SMBIOS_GET8(addr, 8), SMBIOS_GET8(addr, 9),
308 		    SMBIOS_GET8(addr, 10), SMBIOS_GET8(addr, 11),
309 		    SMBIOS_GET8(addr, 12), SMBIOS_GET8(addr, 13),
310 		    SMBIOS_GET8(addr, 14), SMBIOS_GET8(addr, 15));
311 		setenv(name, uuid, 1);
312 	}
313 }
314 
315 #undef UUID_SIZE
316 #undef UUID_TYPE
317 #undef UUID_STEP
318 #undef UUID_ALL_BITS
319 #undef UUID_GET
320 
321 #endif
322 
323 static const char *
smbios_parse_chassis_type(caddr_t addr)324 smbios_parse_chassis_type(caddr_t addr)
325 {
326 	int		type;
327 
328 	type = SMBIOS_GET8(addr, 0x5);
329 	switch (type) {
330 	case 0x1:
331 		return ("Other");
332 	case 0x2:
333 		return ("Unknown");
334 	case 0x3:
335 		return ("Desktop");
336 	case 0x4:
337 		return ("Low Profile Desktop");
338 	case 0x5:
339 		return ("Pizza Box");
340 	case 0x6:
341 		return ("Mini Tower");
342 	case 0x7:
343 		return ("Tower");
344 	case 0x8:
345 		return ("Portable");
346 	case 0x9:
347 		return ("Laptop");
348 	case 0xA:
349 		return ("Notebook");
350 	case 0xB:
351 		return ("Hand Held");
352 	case 0xC:
353 		return ("Docking Station");
354 	case 0xD:
355 		return ("All in One");
356 	case 0xE:
357 		return ("Sub Notebook");
358 	case 0xF:
359 		return ("Lunch Box");
360 	case 0x10:
361 		return ("Space-saving");
362 	case 0x11:
363 		return ("Main Server Chassis");
364 	case 0x12:
365 		return ("Expansion Chassis");
366 	case 0x13:
367 		return ("SubChassis");
368 	case 0x14:
369 		return ("Bus Expansion Chassis");
370 	case 0x15:
371 		return ("Peripheral Chassis");
372 	case 0x16:
373 		return ("RAID Chassis");
374 	case 0x17:
375 		return ("Rack Mount Chassis");
376 	case 0x18:
377 		return ("Sealed-case PC");
378 	case 0x19:
379 		return ("Multi-system chassis");
380 	case 0x1A:
381 		return ("Compact PCI");
382 	case 0x1B:
383 		return ("Advanced TCA");
384 	case 0x1C:
385 		return ("Blade");
386 	case 0x1D:
387 		return ("Blade Enclosure");
388 	case 0x1E:
389 		return ("Tablet");
390 	case 0x1F:
391 		return ("Convertible");
392 	case 0x20:
393 		return ("Detachable");
394 	case 0x21:
395 		return ("IoT Gateway");
396 	case 0x22:
397 		return ("Embedded PC");
398 	case 0x23:
399 		return ("Mini PC");
400 	case 0x24:
401 		return ("Stick PC");
402 	}
403 
404 	return ("Undefined");
405 }
406 
407 static caddr_t
smbios_parse_table(const caddr_t addr)408 smbios_parse_table(const caddr_t addr)
409 {
410 	caddr_t		cp;
411 	int		proc, size, osize, type;
412 	uint8_t		bios_minor, bios_major;
413 	char		buf[16];
414 
415 	type = SMBIOS_GET8(addr, 0);	/* 3.1.2 Structure Header Format */
416 	switch(type) {
417 	case 0:		/* 3.3.1 BIOS Information (Type 0) */
418 		smbios_setenv("smbios.bios.vendor", addr, 0x04);
419 		smbios_setenv("smbios.bios.version", addr, 0x05);
420 		smbios_setenv("smbios.bios.reldate", addr, 0x08);
421 		bios_major = SMBIOS_GET8(addr, 0x14);
422 		bios_minor = SMBIOS_GET8(addr, 0x15);
423 		if (bios_minor != 0xFF && bios_major != 0xFF) {
424 			snprintf(buf, sizeof(buf), "%u.%u",
425 			    bios_major, bios_minor);
426 			setenv("smbios.bios.revision", buf, 1);
427 		}
428 		break;
429 
430 	case 1:		/* 3.3.2 System Information (Type 1) */
431 		smbios_setenv("smbios.system.maker", addr, 0x04);
432 		smbios_setenv("smbios.system.product", addr, 0x05);
433 		smbios_setenv("smbios.system.version", addr, 0x06);
434 #ifdef SMBIOS_SERIAL_NUMBERS
435 		smbios_setenv("smbios.system.serial", addr, 0x07);
436 		smbios_setuuid("smbios.system.uuid", addr + 0x08, smbios.ver);
437 #endif
438 		if (smbios.major > 2 ||
439 		    (smbios.major == 2 && smbios.minor >= 4)) {
440 			smbios_setenv("smbios.system.sku", addr, 0x19);
441 			smbios_setenv("smbios.system.family", addr, 0x1a);
442 		}
443 		break;
444 
445 	case 2:		/* 3.3.3 Base Board (or Module) Information (Type 2) */
446 		smbios_setenv("smbios.planar.maker", addr, 0x04);
447 		smbios_setenv("smbios.planar.product", addr, 0x05);
448 		smbios_setenv("smbios.planar.version", addr, 0x06);
449 #ifdef SMBIOS_SERIAL_NUMBERS
450 		smbios_setenv("smbios.planar.serial", addr, 0x07);
451 		smbios_setenv("smbios.planar.tag", addr, 0x08);
452 #endif
453 		smbios_setenv("smbios.planar.location", addr, 0x0a);
454 		break;
455 
456 	case 3:		/* 3.3.4 System Enclosure or Chassis (Type 3) */
457 		smbios_setenv("smbios.chassis.maker", addr, 0x04);
458 		setenv("smbios.chassis.type", smbios_parse_chassis_type(addr), 1);
459 		smbios_setenv("smbios.chassis.version", addr, 0x06);
460 #ifdef SMBIOS_SERIAL_NUMBERS
461 		smbios_setenv("smbios.chassis.serial", addr, 0x07);
462 		smbios_setenv("smbios.chassis.tag", addr, 0x08);
463 #endif
464 		break;
465 
466 	case 4:		/* 3.3.5 Processor Information (Type 4) */
467 		smbios_setenv("smbios.processor.version", addr, 0x10);
468 		/*
469 		 * Offset 18h: Processor Status
470 		 *
471 		 * Bit 7	Reserved, must be 0
472 		 * Bit 6	CPU Socket Populated
473 		 *		1 - CPU Socket Populated
474 		 *		0 - CPU Socket Unpopulated
475 		 * Bit 5:3	Reserved, must be zero
476 		 * Bit 2:0	CPU Status
477 		 *		0h - Unknown
478 		 *		1h - CPU Enabled
479 		 *		2h - CPU Disabled by User via BIOS Setup
480 		 *		3h - CPU Disabled by BIOS (POST Error)
481 		 *		4h - CPU is Idle, waiting to be enabled
482 		 *		5-6h - Reserved
483 		 *		7h - Other
484 		 */
485 		proc = SMBIOS_GET8(addr, 0x18);
486 		if ((proc & 0x07) == 1)
487 			smbios.enabled_sockets++;
488 		if ((proc & 0x40) != 0)
489 			smbios.populated_sockets++;
490 		break;
491 
492 	case 6:		/* 3.3.7 Memory Module Information (Type 6, Obsolete) */
493 		/*
494 		 * Offset 0Ah: Enabled Size
495 		 *
496 		 * Bit 7	Bank connection
497 		 *		1 - Double-bank connection
498 		 *		0 - Single-bank connection
499 		 * Bit 6:0	Size (n), where 2**n is the size in MB
500 		 *		7Dh - Not determinable (Installed Size only)
501 		 *		7Eh - Module is installed, but no memory
502 		 *		      has been enabled
503 		 *		7Fh - Not installed
504 		 */
505 		osize = SMBIOS_GET8(addr, 0x0a) & 0x7f;
506 		if (osize > 0 && osize < 22)
507 			smbios.old_enabled_memory += 1 << (osize + 10);
508 		break;
509 
510 	case 17:	/* 3.3.18 Memory Device (Type 17) */
511 		/*
512 		 * Offset 0Ch: Size
513 		 *
514 		 * Bit 15	Granularity
515 		 *		1 - Value is in kilobytes units
516 		 *		0 - Value is in megabytes units
517 		 * Bit 14:0	Size
518 		 */
519 		size = SMBIOS_GET16(addr, 0x0c);
520 		if (size != 0 && size != 0xffff)
521 			smbios.enabled_memory += (size & 0x8000) != 0 ?
522 			    (size & 0x7fff) : (size << 10);
523 		break;
524 
525 	case SMBIOS_EOT_TYPE:	/* 3.3.42 End-of-Table (Type 127) */
526 		return (NULL);
527 
528 	default:	/* skip other types */
529 		break;
530 	}
531 
532 	/* Find structure terminator. */
533 	cp = SMBIOS_GETSTR(addr);
534 	while (SMBIOS_GET16(cp, 0) != 0)
535 		cp++;
536 
537 	return (cp + 2);
538 }
539 
540 static caddr_t
smbios_find_struct(int type)541 smbios_find_struct(int type)
542 {
543 	caddr_t		dmi;
544 	size_t		i;
545 	caddr_t		ep;
546 
547 	if (smbios.addr == NULL)
548 		return (NULL);
549 
550 	ep = smbios.addr + smbios.length;
551 	for (dmi = smbios.addr, i = 0;
552 	     dmi < ep && i < smbios.count; i++) {
553 		const uint8_t seen_type = SMBIOS_GET8(dmi, 0);
554 
555 		if (seen_type == type)
556 			return (dmi);
557 		if (seen_type == SMBIOS_EOT_TYPE)
558 			/* End of table. */
559 			break;
560 		/* Find structure terminator. */
561 		dmi = SMBIOS_GETSTR(dmi);
562 		while (SMBIOS_GET16(dmi, 0) != 0 && dmi < ep)
563 			dmi++;
564 		/* Skip it. */
565 		dmi += 2;
566 	}
567 
568 	return (NULL);
569 }
570 
571 static void
smbios_probe(const caddr_t addr)572 smbios_probe(const caddr_t addr)
573 {
574 	caddr_t		saddr, info;
575 	uintptr_t	paddr;
576 	int		maj_off;
577 	int		min_off;
578 
579 	/* Search signatures and validate checksums. */
580 	saddr = addr != NULL ? smbios_sigsearch(addr, 1) :
581 	    smbios_sigsearch(PTOV(SMBIOS_START), SMBIOS_LENGTH);
582 	if (saddr == NULL)
583 		return;
584 
585 	if (smbios.is_64bit_ep) {
586 		/* Structure Table Length */
587 		smbios.length = SMBIOS_GET32(saddr, 0x0c);
588 		/* Structure Table Address */
589 		paddr = SMBIOS_GET64(saddr, 0x10);
590 		/* Not present in V3, set it to the maximum value (no limit). */
591 		smbios.count = -1;
592 		/*
593 		 * No BCD revision in V3, we'll determine the version thanks to
594 		 * the major and minor fields below.
595 		 */
596 		smbios.ver = 0;
597 		maj_off = 0x07;
598 		min_off = 0x08;
599 	} else {
600 		/* Structure Table Length */
601 		smbios.length = SMBIOS_GET16(saddr, 0x16);
602 		/* Structure Table Address */
603 		paddr = SMBIOS_GET32(saddr, 0x18);
604 		/* No. of SMBIOS Structures */
605 		smbios.count = SMBIOS_GET16(saddr, 0x1c);
606 		/* SMBIOS BCD Revision */
607 		smbios.ver = SMBIOS_GET8(saddr, 0x1e);
608 		if (smbios.ver != 0) {
609 			smbios.major = smbios.ver >> 4;
610 			smbios.minor = smbios.ver & 0x0f;
611 			if (smbios.major > 9 || smbios.minor > 9)
612 				smbios.ver = 0;
613 		}
614 		maj_off = 0x06;
615 		min_off = 0x07;
616 	}
617 
618 
619 	if (smbios.ver == 0) {
620 		/*
621 		 * v3 table, or v2 with BCD revision being 0 or bad.  Use the
622 		 * major and minor version fields.
623 		 */
624 		smbios.major = SMBIOS_GET8(saddr, maj_off);
625 		smbios.minor = SMBIOS_GET8(saddr, min_off);
626 	}
627 	smbios.ver = (smbios.major << 8) | smbios.minor;
628 	smbios.addr = PTOV(paddr);
629 
630 	/* Get system information from SMBIOS */
631 	info = smbios_find_struct(0x00);
632 	if (info != NULL) {
633 		smbios.bios_vendor = smbios_getstring(info, 0x04);
634 	}
635 	info = smbios_find_struct(0x01);
636 	if (info != NULL) {
637 		smbios.maker = smbios_getstring(info, 0x04);
638 		smbios.product = smbios_getstring(info, 0x05);
639 	}
640 }
641 
642 caddr_t
smbios_detect(const caddr_t addr)643 smbios_detect(const caddr_t addr)
644 {
645 	char		buf[16];
646 	caddr_t		dmi;
647 	size_t		i;
648 
649 	smbios_probe(addr);
650 	if (smbios.addr == NULL)
651 		return (NULL);
652 
653 	for (dmi = smbios.addr, i = 0; dmi != NULL &&
654 	    dmi < smbios.addr + smbios.length && i < smbios.count; i++)
655 		dmi = smbios_parse_table(dmi);
656 
657 	setenv("smbios.entry_point_type", smbios.is_64bit_ep ?
658 	    "v3 (64-bit)" : "v2.1 (32-bit)", 1);
659 	sprintf(buf, "%d.%d", smbios.major, smbios.minor);
660 	setenv("smbios.version", buf, 1);
661 	if (smbios.enabled_memory > 0 || smbios.old_enabled_memory > 0) {
662 		sprintf(buf, "%u", smbios.enabled_memory > 0 ?
663 		    smbios.enabled_memory : smbios.old_enabled_memory);
664 		setenv("smbios.memory.enabled", buf, 1);
665 	}
666 	if (smbios.enabled_sockets > 0) {
667 		sprintf(buf, "%u", smbios.enabled_sockets);
668 		setenv("smbios.socket.enabled", buf, 1);
669 	}
670 	if (smbios.populated_sockets > 0) {
671 		sprintf(buf, "%u", smbios.populated_sockets);
672 		setenv("smbios.socket.populated", buf, 1);
673 	}
674 
675 	return (smbios.addr);
676 }
677 
678 static int
smbios_match_str(const char * s1,const char * s2)679 smbios_match_str(const char* s1, const char* s2)
680 {
681 	return (s1 == NULL || (s2 != NULL && !strcmp(s1, s2)));
682 }
683 
684 int
smbios_match(const char * bios_vendor,const char * maker,const char * product)685 smbios_match(const char* bios_vendor, const char* maker,
686     const char* product)
687 {
688 	static bool probed = false;
689 
690 	/*
691 	 * This routine is called only from non-EFI loaders on determining the
692 	 * amount of usable memory.  In particular, it is so before malloc() can
693 	 * be used, so before smbios_detect() can be called (as it uses
694 	 * setenv()).  Consequently, since smbios_probe() is not exported, we
695 	 * ensure it has been called beforehand to fetch into the static
696 	 * 'smbios' structure the metadata that is to be matched.
697 	 */
698 	if (!probed) {
699 		probed = true;
700 		smbios_probe(NULL);
701 	}
702 
703 	return (smbios_match_str(bios_vendor, smbios.bios_vendor) &&
704 	    smbios_match_str(maker, smbios.maker) &&
705 	    smbios_match_str(product, smbios.product));
706 }
707