Module Name:    src
Committed By:   jmcneill
Date:           Wed Jul 21 23:16:09 UTC 2021

Modified Files:
        src/sys/arch/i386/stand/lib: exec_multiboot2.c
        src/sys/arch/x86/conf: files.x86
        src/sys/arch/x86/x86: bios32.c platform.c x86_ipmi.c
        src/sys/conf: files
        src/sys/dev: files.dev
Added Files:
        src/sys/arch/x86/include: smbios_machdep.h
        src/sys/dev: smbios.c smbiosvar.h
Removed Files:
        src/sys/arch/x86/include: smbiosvar.h

Log Message:
Separate MI smbios interface from MD specific code.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/i386/stand/lib/exec_multiboot2.c
cvs rdiff -u -r1.120 -r1.121 src/sys/arch/x86/conf/files.x86
cvs rdiff -u -r0 -r1.1 src/sys/arch/x86/include/smbios_machdep.h
cvs rdiff -u -r1.6 -r0 src/sys/arch/x86/include/smbiosvar.h
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/x86/x86/bios32.c
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/x86/x86/platform.c
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/x86/x86/x86_ipmi.c
cvs rdiff -u -r1.1286 -r1.1287 src/sys/conf/files
cvs rdiff -u -r1.6 -r1.7 src/sys/dev/files.dev
cvs rdiff -u -r0 -r1.1 src/sys/dev/smbios.c src/sys/dev/smbiosvar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/stand/lib/exec_multiboot2.c
diff -u src/sys/arch/i386/stand/lib/exec_multiboot2.c:1.4 src/sys/arch/i386/stand/lib/exec_multiboot2.c:1.5
--- src/sys/arch/i386/stand/lib/exec_multiboot2.c:1.4	Thu May 14 08:34:20 2020
+++ src/sys/arch/i386/stand/lib/exec_multiboot2.c	Wed Jul 21 23:16:08 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: exec_multiboot2.c,v 1.4 2020/05/14 08:34:20 msaitoh Exp $ */
+/* $NetBSD: exec_multiboot2.c,v 1.5 2021/07/21 23:16:08 jmcneill Exp $ */
 
 /*
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -34,7 +34,8 @@
 
 #include <dev/acpi/acpica.h>
 #include <x86/acpi_machdep.h>
-#include <x86/smbiosvar.h>
+#include <dev/smbiosvar.h>
+#include <x86/smbios_machdep.h>
 
 #include <lib/libsa/stand.h> 
 #include <lib/libkern/libkern.h> 

Index: src/sys/arch/x86/conf/files.x86
diff -u src/sys/arch/x86/conf/files.x86:1.120 src/sys/arch/x86/conf/files.x86:1.121
--- src/sys/arch/x86/conf/files.x86:1.120	Tue Oct 27 08:57:11 2020
+++ src/sys/arch/x86/conf/files.x86	Wed Jul 21 23:16:08 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: files.x86,v 1.120 2020/10/27 08:57:11 ryo Exp $
+#	$NetBSD: files.x86,v 1.121 2021/07/21 23:16:08 jmcneill Exp $
 
 # options for MP configuration through the MP spec
 defflag opt_mpbios.h MPBIOS MPDEBUG MPBIOS_SCANPCI
@@ -30,7 +30,7 @@ define	cpufeaturebus {}
 define  ioapicbus { [apid = -1] }
 
 # BIOS32 routines
-define	bios32
+define	bios32: smbios
 file	arch/x86/x86/bios32.c		bios32 needs-flag
 
 #

Index: src/sys/arch/x86/x86/bios32.c
diff -u src/sys/arch/x86/x86/bios32.c:1.4 src/sys/arch/x86/x86/bios32.c:1.5
--- src/sys/arch/x86/x86/bios32.c:1.4	Fri Dec 27 09:45:26 2019
+++ src/sys/arch/x86/x86/bios32.c	Wed Jul 21 23:16:09 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: bios32.c,v 1.4 2019/12/27 09:45:26 msaitoh Exp $	*/
+/*	$NetBSD: bios32.c,v 1.5 2021/07/21 23:16:09 jmcneill Exp $	*/
 
 /*
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -86,7 +86,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bios32.c,v 1.4 2019/12/27 09:45:26 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bios32.c,v 1.5 2021/07/21 23:16:09 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -97,7 +97,8 @@ __KERNEL_RCSID(0, "$NetBSD: bios32.c,v 1
 
 #include <machine/segments.h>
 #include <machine/bios32.h>
-#include <x86/smbiosvar.h>
+#include <dev/smbiosvar.h>
+#include <x86/smbios_machdep.h>
 #include <x86/efi.h>
 
 #include <uvm/uvm.h>
@@ -110,11 +111,8 @@ __KERNEL_RCSID(0, "$NetBSD: bios32.c,v 1
 #define	BIOS32_END	(BIOS32_START + BIOS32_SIZE - 0x10)
 
 struct bios32_entry bios32_entry;
-struct smbios_entry smbios_entry;
 
-static int smbios2_check_header(const uint8_t *);
 static void smbios2_map_kva(const uint8_t *);
-static int smbios3_check_header(const uint8_t *);
 static void smbios3_map_kva(const uint8_t *);
 
 /*
@@ -241,32 +239,6 @@ bios32_service(uint32_t service, bios32_
 	return 1;
 }
 
-static int
-smbios2_check_header(const uint8_t *p)
-{
-	const struct smbhdr *sh = (const struct smbhdr *)p;
-	uint8_t chksum;
-	int i;
-
-	if (sh->sig != BIOS32_MAKESIG('_', 'S', 'M', '_'))
-		return 0;
-	i = sh->len;
-	for (chksum = 0; i--; )
-		chksum += p[i];
-	if (chksum != 0)
-		return 0;
-	p += 0x10;
-	if (p[0] != '_' || p[1] != 'D' || p[2] != 'M' ||
-	    p[3] != 'I' || p[4] != '_')
-		return 0;
-	for (chksum = 0, i = 0xf; i--; )
-		chksum += p[i];
-	if (chksum != 0)
-		return 0;
-
-	return 1;
-}
-
 static void
 smbios2_map_kva(const uint8_t *p)
 {
@@ -300,27 +272,6 @@ smbios2_map_kva(const uint8_t *p)
 	    sh->majrev, sh->minrev, (u_long)sh->addr, sh->count);
 }
 
-static int
-smbios3_check_header(const uint8_t *p)
-{
-	const struct smb3hdr *sh = (const struct smb3hdr *)p;
-	uint8_t chksum;
-	int i;
-
-	if (p[0] != '_' || p[1] != 'S' || p[2] != 'M' ||
-	    p[3] != '3' || p[4] != '_')
-		return 0;
-	i = sh->len;
-	for (chksum = 0; i--; )
-		chksum += p[i];
-	if (chksum != 0)
-		return 0;
-	if (sh->eprev != SMBIOS3_EPREV_3_0)
-		return 0;
-
-	return 1;
-}
-
 static void
 smbios3_map_kva(const uint8_t *p)
 {
@@ -353,87 +304,3 @@ smbios3_map_kva(const uint8_t *p)
 	aprint_debug("SMBIOS rev. %d.%d.%d @ 0x%lx\n", sh->majrev,
 	    sh->minrev, sh->docrev, (u_long)sh->addr);
 }
-
-/*
- * smbios_find_table() takes a caller supplied smbios struct type and
- * a pointer to a handle (struct smbtable) returning one if the structure
- * is successfully located and zero otherwise. Callers should take care
- * to initilize the cookie field of the smbtable structure to zero before
- * the first invocation of this function.
- * Multiple tables of the same type can be located by repeadtly calling
- * smbios_find_table with the same arguments.
- */
-int
-smbios_find_table(uint8_t type, struct smbtable *st)
-{
-	uint8_t *va, *end;
-	struct smbtblhdr *hdr;
-	int ret = 0, tcount = 1;
-
-	va = smbios_entry.addr;
-	end = va + smbios_entry.len;
-
-	/*
-	 * The cookie field of the smtable structure is used to locate
-	 * multiple instances of a table of an arbitrary type. Following the
-	 * sucessful location of a table, the type is encoded as bits 0:7 of
-	 * the cookie value, the offset in terms of the number of structures
-	 * preceding that referenced by the handle is encoded in bits 15:31.
-	 */
-	if ((st->cookie & 0xfff) == type && st->cookie >> 16) {
-		if ((uint8_t *)st->hdr >= va && (uint8_t *)st->hdr < end) {
-			hdr = st->hdr;
-			if (hdr->type == type) {
-				va = (uint8_t *)hdr + hdr->size;
-				for (; va + 1 < end; va++)
-					if (*va == 0 && *(va + 1) == 0)
-						break;
-				va+= 2;
-				tcount = st->cookie >> 16;
-			}
-		}
-	}
-	for (; va + sizeof(struct smbtblhdr) < end && tcount <=
-	    smbios_entry.count; tcount++) {
-		hdr = (struct smbtblhdr *)va;
-		if (hdr->type == type) {
-			ret = 1;
-			st->hdr = hdr;
-			st->tblhdr = va + sizeof(struct smbtblhdr);
-			st->cookie = (tcount + 1) << 16 | type;
-			break;
-		}
-		if (hdr->type == SMBIOS_TYPE_EOT)
-			break;
-		va+= hdr->size;
-		for (; va + 1 < end; va++)
-			if (*va == 0 && *(va + 1) == 0)
-				break;
-		va+=2;
-	}
-
-	return ret;
-}
-
-char *
-smbios_get_string(struct smbtable *st, uint8_t indx, char *dest, size_t len)
-{
-	uint8_t *va, *end;
-	char *ret = NULL;
-	int i;
-
-	va = (uint8_t *)st->hdr + st->hdr->size;
-	end = smbios_entry.addr + smbios_entry.len;
-	for (i = 1; va < end && i < indx && *va; i++)
-		while (*va++)
-			;
-	if (i == indx) {
-		if (va + len < end) {
-			ret = dest;
-			memcpy(ret, va, len);
-			ret[len - 1] = '\0';
-		}
-	}
-
-	return ret;
-}

Index: src/sys/arch/x86/x86/platform.c
diff -u src/sys/arch/x86/x86/platform.c:1.16 src/sys/arch/x86/x86/platform.c:1.17
--- src/sys/arch/x86/x86/platform.c:1.16	Tue Dec 25 16:45:02 2018
+++ src/sys/arch/x86/x86/platform.c	Wed Jul 21 23:16:09 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: platform.c,v 1.16 2018/12/25 16:45:02 mlelstv Exp $ */
+/* $NetBSD: platform.c,v 1.17 2021/07/21 23:16:09 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2007 Jared D. McNeill <jmcne...@invisible.ca>
@@ -29,7 +29,7 @@
 #include "isa.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: platform.c,v 1.16 2018/12/25 16:45:02 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: platform.c,v 1.17 2021/07/21 23:16:09 jmcneill Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -42,7 +42,7 @@ __KERNEL_RCSID(0, "$NetBSD: platform.c,v
 #include <dev/isa/isavar.h>
 #endif
 
-#include <arch/x86/include/smbiosvar.h>
+#include <dev/smbiosvar.h>
 
 static int platform_dminode = CTL_EOL;
 

Index: src/sys/arch/x86/x86/x86_ipmi.c
diff -u src/sys/arch/x86/x86/x86_ipmi.c:1.1 src/sys/arch/x86/x86/x86_ipmi.c:1.2
--- src/sys/arch/x86/x86/x86_ipmi.c:1.1	Tue Dec 25 11:56:13 2018
+++ src/sys/arch/x86/x86/x86_ipmi.c	Wed Jul 21 23:16:09 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_ipmi.c,v 1.1 2018/12/25 11:56:13 mlelstv Exp $ */
+/*	$NetBSD: x86_ipmi.c,v 1.2 2021/07/21 23:16:09 jmcneill Exp $ */
 
 /*
  * Copyright (c) 2006 Manuel Bouyer.
@@ -52,7 +52,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: x86_ipmi.c,v 1.1 2018/12/25 11:56:13 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_ipmi.c,v 1.2 2021/07/21 23:16:09 jmcneill Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -62,7 +62,7 @@ __KERNEL_RCSID(0, "$NetBSD: x86_ipmi.c,v
 #include <sys/bus.h>
 #include <sys/intr.h>
 
-#include <x86/smbiosvar.h>
+#include <dev/smbiosvar.h>
 
 #include <dev/isa/isareg.h>
 #include <dev/isa/isavar.h>

Index: src/sys/conf/files
diff -u src/sys/conf/files:1.1286 src/sys/conf/files:1.1287
--- src/sys/conf/files:1.1286	Tue Jun  1 03:51:33 2021
+++ src/sys/conf/files	Wed Jul 21 23:16:09 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: files,v 1.1286 2021/06/01 03:51:33 yamaguchi Exp $
+#	$NetBSD: files,v 1.1287 2021/07/21 23:16:09 jmcneill Exp $
 #	@(#)files.newconf	7.5 (Berkeley) 5/10/93
 
 version 	20171118
@@ -378,6 +378,7 @@ define 	pckbport	{[slot = -1]}
 define	pckbport_machdep_cnattach
 define	firmload
 define	ipmibus		{ }
+define	smbios
 
 # speaker devices, attaches to audio or pcppi drivers
 device	spkr: wsbelldev

Index: src/sys/dev/files.dev
diff -u src/sys/dev/files.dev:1.6 src/sys/dev/files.dev:1.7
--- src/sys/dev/files.dev:1.6	Thu Apr 30 03:28:18 2020
+++ src/sys/dev/files.dev	Wed Jul 21 23:16:09 2021
@@ -1,4 +1,4 @@
-#	$NetBSD: files.dev,v 1.6 2020/04/30 03:28:18 riastradh Exp $
+#	$NetBSD: files.dev,v 1.7 2021/07/21 23:16:09 jmcneill Exp $
 
 file	dev/bio.c			bio			needs-flag
 file	dev/ccd.c			ccd
@@ -26,3 +26,4 @@ file	dev/sequencer.c			sequencer		needs-
 file	dev/video.c			video			needs-flag
 file	dev/vnd.c			vnd
 file	dev/ipmi.c			ipmi			needs-flag
+file	dev/smbios.c			smbios

Added files:

Index: src/sys/arch/x86/include/smbios_machdep.h
diff -u /dev/null src/sys/arch/x86/include/smbios_machdep.h:1.1
--- /dev/null	Wed Jul 21 23:16:09 2021
+++ src/sys/arch/x86/include/smbios_machdep.h	Wed Jul 21 23:16:09 2021
@@ -0,0 +1,35 @@
+/*	$NetBSD: smbios_machdep.h,v 1.1 2021/07/21 23:16:09 jmcneill Exp $ */
+/*
+ * Copyright (c) 2006 Gordon Willem Klok <gk...@cogeco.ca>
+ * Copyright (c) 2005 Jordan Hargrave
+ * 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.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, 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 DAMAGE.
+ */
+
+#ifndef _X86_SMBIOS_MACHDEP_H
+#define _X86_SMBIOS_MACHDEP_H
+
+#define SMBIOS_START			0xf0000
+#define SMBIOS_END			0xfffff
+
+#endif

Index: src/sys/dev/smbios.c
diff -u /dev/null src/sys/dev/smbios.c:1.1
--- /dev/null	Wed Jul 21 23:16:09 2021
+++ src/sys/dev/smbios.c	Wed Jul 21 23:16:09 2021
@@ -0,0 +1,237 @@
+/*	$NetBSD: smbios.c,v 1.1 2021/07/21 23:16:09 jmcneill Exp $	*/
+
+/*
+ * Copyright (c) 1999 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
+ * NASA Ames Research Center.
+ *
+ * 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.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 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 DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1999, by UCHIYAMA Yasushi
+ * 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.
+ * 2. The name of the developer may NOT be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, 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 DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997-2001 Michael Shalayeff
+ * 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.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF MIND, 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 DAMAGE.
+ */
+
+/*
+ * Basic interface to System Management BIOS (SMBIOS) tables.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: smbios.c,v 1.1 2021/07/21 23:16:09 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+
+#include <dev/smbiosvar.h>
+
+#define	SMBIOS_MAKESIG(a, b, c, d)	\
+	((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
+
+struct smbios_entry smbios_entry;
+
+int
+smbios2_check_header(const uint8_t *p)
+{
+	const struct smbhdr *sh = (const struct smbhdr *)p;
+	uint8_t chksum;
+	int i;
+
+	if (sh->sig != SMBIOS_MAKESIG('_', 'S', 'M', '_'))
+		return 0;
+	i = sh->len;
+	for (chksum = 0; i--; )
+		chksum += p[i];
+	if (chksum != 0)
+		return 0;
+	p += 0x10;
+	if (p[0] != '_' || p[1] != 'D' || p[2] != 'M' ||
+	    p[3] != 'I' || p[4] != '_')
+		return 0;
+	for (chksum = 0, i = 0xf; i--; )
+		chksum += p[i];
+	if (chksum != 0)
+		return 0;
+
+	return 1;
+}
+
+int
+smbios3_check_header(const uint8_t *p)
+{
+	const struct smb3hdr *sh = (const struct smb3hdr *)p;
+	uint8_t chksum;
+	int i;
+
+	if (p[0] != '_' || p[1] != 'S' || p[2] != 'M' ||
+	    p[3] != '3' || p[4] != '_')
+		return 0;
+	i = sh->len;
+	for (chksum = 0; i--; )
+		chksum += p[i];
+	if (chksum != 0)
+		return 0;
+	if (sh->eprev != SMBIOS3_EPREV_3_0)
+		return 0;
+
+	return 1;
+}
+
+/*
+ * smbios_find_table() takes a caller supplied smbios struct type and
+ * a pointer to a handle (struct smbtable) returning one if the structure
+ * is successfully located and zero otherwise. Callers should take care
+ * to initilize the cookie field of the smbtable structure to zero before
+ * the first invocation of this function.
+ * Multiple tables of the same type can be located by repeadtly calling
+ * smbios_find_table with the same arguments.
+ */
+int
+smbios_find_table(uint8_t type, struct smbtable *st)
+{
+	uint8_t *va, *end;
+	struct smbtblhdr *hdr;
+	int ret = 0, tcount = 1;
+
+	if (smbios_entry.addr == NULL) {
+		return 0;
+	}
+
+	va = smbios_entry.addr;
+	end = va + smbios_entry.len;
+
+	/*
+	 * The cookie field of the smtable structure is used to locate
+	 * multiple instances of a table of an arbitrary type. Following the
+	 * sucessful location of a table, the type is encoded as bits 0:7 of
+	 * the cookie value, the offset in terms of the number of structures
+	 * preceding that referenced by the handle is encoded in bits 15:31.
+	 */
+	if ((st->cookie & 0xfff) == type && st->cookie >> 16) {
+		if ((uint8_t *)st->hdr >= va && (uint8_t *)st->hdr < end) {
+			hdr = st->hdr;
+			if (hdr->type == type) {
+				va = (uint8_t *)hdr + hdr->size;
+				for (; va + 1 < end; va++)
+					if (*va == 0 && *(va + 1) == 0)
+						break;
+				va+= 2;
+				tcount = st->cookie >> 16;
+			}
+		}
+	}
+	for (; va + sizeof(struct smbtblhdr) < end && tcount <=
+	    smbios_entry.count; tcount++) {
+		hdr = (struct smbtblhdr *)va;
+		if (hdr->type == type) {
+			ret = 1;
+			st->hdr = hdr;
+			st->tblhdr = va + sizeof(struct smbtblhdr);
+			st->cookie = (tcount + 1) << 16 | type;
+			break;
+		}
+		if (hdr->type == SMBIOS_TYPE_EOT)
+			break;
+		va+= hdr->size;
+		for (; va + 1 < end; va++)
+			if (*va == 0 && *(va + 1) == 0)
+				break;
+		va+=2;
+	}
+
+	return ret;
+}
+
+char *
+smbios_get_string(struct smbtable *st, uint8_t indx, char *dest, size_t len)
+{
+	uint8_t *va, *end;
+	char *ret = NULL;
+	int i;
+
+	KASSERT(smbios_entry.addr != NULL);
+
+	va = (uint8_t *)st->hdr + st->hdr->size;
+	end = smbios_entry.addr + smbios_entry.len;
+	for (i = 1; va < end && i < indx && *va; i++)
+		while (*va++)
+			;
+	if (i == indx) {
+		if (va + len < end) {
+			ret = dest;
+			memcpy(ret, va, len);
+			ret[len - 1] = '\0';
+		}
+	}
+
+	return ret;
+}
Index: src/sys/dev/smbiosvar.h
diff -u /dev/null src/sys/dev/smbiosvar.h:1.1
--- /dev/null	Wed Jul 21 23:16:09 2021
+++ src/sys/dev/smbiosvar.h	Wed Jul 21 23:16:09 2021
@@ -0,0 +1,295 @@
+/*	$NetBSD: smbiosvar.h,v 1.1 2021/07/21 23:16:09 jmcneill Exp $ */
+/*
+ * Copyright (c) 2006 Gordon Willem Klok <gk...@cogeco.ca>
+ * Copyright (c) 2005 Jordan Hargrave
+ * 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.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, 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 DAMAGE.
+ */
+
+#ifndef _DEV_SMBIOSVAR_H
+#define _DEV_SMBIOSVAR_H
+
+#define SMBIOS_UUID_NPRESENT		0x1
+#define SMBIOS_UUID_NSET		0x2
+
+/*
+ * Section 3.5 of "UUIDs and GUIDs" found at
+ * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
+ * specifies the string repersentation of a UUID.
+ */
+#define SMBIOS_UUID_REP "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
+#define SMBIOS_UUID_REPLEN 37 /* 16 zero padded values, 4 hyphens, 1 null */
+
+struct smbios_entry {
+	uint8_t 	rev;
+	uint8_t 	mjr;
+	uint8_t 	min;
+	uint8_t 	doc;
+	uint8_t 	*addr;
+	uint32_t	len;
+	uint16_t	count;
+};
+
+struct smbhdr {
+	uint32_t	sig;		/* "_SM_" */
+	uint8_t 	checksum;	/* Entry point checksum */
+	uint8_t 	len;		/* Entry point structure length */
+	uint8_t 	majrev;		/* Specification major revision */
+	uint8_t 	minrev;		/* Specification minor revision */
+	uint16_t	mss;		/* Maximum Structure Size */
+	uint8_t 	epr;		/* Entry Point Revision */
+	uint8_t 	fa[5];		/* value determined by EPR */
+	uint8_t 	sasig[5];	/* Secondary Anchor "_DMI_" */
+	uint8_t 	sachecksum;	/* Secondary Checksum */
+	uint16_t	size;		/* Length of structure table in bytes */
+	uint32_t	addr;		/* Structure table address */
+	uint16_t	count;		/* Number of SMBIOS structures */
+	uint8_t 	rev;		/* BCD revision */
+} __packed;
+
+struct smb3hdr {
+	uint8_t 	sig[5];		/* "_SM3_" */
+	uint8_t 	checksum;	/* Entry point structure checksum */
+	uint8_t 	len;		/* Entry point structure length */
+	uint8_t 	majrev;		/* Specification major revision */
+	uint8_t 	minrev;		/* Specification minor revision */
+	uint8_t 	docrev;		/* docrec of Specification */
+	uint8_t 	eprev;		/* Entry point structure revision */
+#define	SMBIOS3_EPREV_RESERVED	0
+#define	SMBIOS3_EPREV_3_0	1	/* SMBIOS 3.0 */
+	uint8_t 	reverved;
+	uint32_t	size;		/* Length of structure table in bytes */
+	uint64_t	addr;		/* Structure table address */
+} __packed;
+
+struct smbtblhdr {
+	uint8_t	type;
+	uint8_t	size;
+	uint16_t	handle;
+} __packed;
+
+struct smbtable {
+	struct smbtblhdr *hdr;
+	void		 *tblhdr;
+	uint32_t	 cookie;
+};
+
+#define	SMBIOS_TYPE_BIOS		0
+#define	SMBIOS_TYPE_SYSTEM		1
+#define	SMBIOS_TYPE_BASEBOARD		2
+#define	SMBIOS_TYPE_ENCLOSURE		3
+#define	SMBIOS_TYPE_PROCESSOR		4
+#define	SMBIOS_TYPE_MEMCTRL		5
+#define	SMBIOS_TYPE_MEMMOD		6
+#define	SMBIOS_TYPE_CACHE		7
+#define	SMBIOS_TYPE_PORT		8
+#define	SMBIOS_TYPE_SLOTS		9
+#define	SMBIOS_TYPE_OBD			10
+#define	SMBIOS_TYPE_OEM			11
+#define	SMBIOS_TYPE_SYSCONFOPT		12
+#define	SMBIOS_TYPE_BIOSLANG		13
+#define	SMBIOS_TYPE_GROUPASSOC		14
+#define	SMBIOS_TYPE_SYSEVENTLOG		15
+#define	SMBIOS_TYPE_PHYMEM		16
+#define	SMBIOS_TYPE_MEMDEV		17
+#define	SMBIOS_TYPE_ECCINFO32		18
+#define	SMBIOS_TYPE_MEMMAPARRAYADDR	19
+#define	SMBIOS_TYPE_MEMMAPDEVADDR	20
+#define	SMBIOS_TYPE_INBUILTPOINT	21
+#define	SMBIOS_TYPE_PORTBATT		22
+#define	SMBIOS_TYPE_SYSRESET		23
+#define	SMBIOS_TYPE_HWSECUIRTY		24
+#define	SMBIOS_TYPE_PWRCTRL		25
+#define	SMBIOS_TYPE_VOLTPROBE		26
+#define	SMBIOS_TYPE_COOLING		27
+#define	SMBIOS_TYPE_TEMPPROBE		28
+#define	SMBIOS_TYPE_CURRENTPROBE	29
+#define	SMBIOS_TYPE_OOB_REMOTEACCESS	30
+#define	SMBIOS_TYPE_BIS			31
+#define	SMBIOS_TYPE_SBI			32
+#define	SMBIOS_TYPE_ECCINFO64		33
+#define	SMBIOS_TYPE_MGMTDEV		34
+#define	SMBIOS_TYPE_MGTDEVCOMP		35
+#define	SMBIOS_TYPE_MGTDEVTHRESH	36
+#define	SMBIOS_TYPE_MEMCHANNEL		37
+#define	SMBIOS_TYPE_IPMIDEV		38
+#define	SMBIOS_TYPE_SPS			39
+#define	SMBIOS_TYPE_INACTIVE		126
+#define	SMBIOS_TYPE_EOT			127
+
+/*
+ * SMBIOS Structure Type 0 "BIOS Information"
+ * DMTF Specification DSP0134 Section: 3.3.1 p.g. 34
+ */
+struct smbios_struct_bios {
+	uint8_t	vendor;		/* string */
+	uint8_t	version;	/* string */
+	uint16_t	startaddr;
+	uint8_t	release;	/* string */
+	uint8_t	romsize;
+	uint64_t	characteristics;
+	uint32_t	charext;
+	uint8_t	major_rel;
+	uint8_t	minor_rel;
+	uint8_t	ecf_mjr_rel;	/* embedded controller firmware */
+	uint8_t 	ecf_min_rel;	/* embedded controller firmware */
+} __packed;
+
+/*
+ * SMBIOS Structure Type 1 "System Information"
+ * DMTF Specification DSP0134 Section 3.3.2 p.g. 35
+ */
+
+struct smbios_sys {
+/* SMBIOS spec 2.0+ */
+	uint8_t	vendor;		/* string */
+	uint8_t	product;	/* string */
+	uint8_t	version;	/* string */
+	uint8_t	serial;		/* string */
+/* SMBIOS spec 2.1+ */
+	uint8_t	uuid[16];
+	uint8_t	wakeup;
+/* SMBIOS spec 2.4+ */
+	uint8_t	sku;		/* string */
+	uint8_t	family;		/* string */
+} __packed;
+
+/*
+ * SMBIOS Structure Type 2 "Base Board (Module) Information"
+ * DMTF Specification DSP0134 Section 3.3.3 p.g. 37
+ */
+struct smbios_board {
+	uint8_t	vendor;		/* string */
+	uint8_t	product;	/* string */
+	uint8_t	version;	/* string */
+	uint8_t	serial;		/* string */
+	uint8_t	asset;		/* string */
+	uint8_t	feature;	/* feature flags */
+	uint8_t	location;	/* location in chassis */
+	uint16_t	handle;		/* chassis handle */
+	uint8_t	type;		/* board type */
+	uint8_t	noc;		/* number of contained objects */
+} __packed;
+
+/*
+ * SMBIOS Structure Type 3 "System Enclosure or Chassis"
+ * DMTF Specification DSP0134 Section 3.1.1 p.g. 37
+ */
+struct smbios_chassis {
+	uint8_t	vendor;		/* string */
+	uint8_t	shape;
+	uint8_t	version;	/* string */
+	uint8_t	serial;		/* string */
+	uint8_t	asset;		/* string */
+	uint8_t	bustate;
+	uint8_t	psstate;
+	uint8_t	thstate;
+	uint8_t	security;
+	uint32_t	oemdata;
+	uint8_t	height;
+	uint8_t	powercords;
+	uint8_t	noc;		/* number of contained objects */
+} __packed;
+
+/*
+ * SMBIOS Structure Type 4 "Processor Information"
+ * DMTF Specification DSP0134 Section 3.1.1 p.g. 42
+ */
+struct smbios_processor {
+	uint8_t socket;		/* string */
+	uint8_t	type;
+	uint8_t	family;
+	uint8_t	vendor;		/* string */
+	uint64_t	cpuid;
+	uint8_t	version;	/* string */
+	uint8_t	voltage;
+	uint16_t	clkspeed;
+	uint16_t	maxspeed;
+	uint16_t	curspeed;
+	uint8_t	status;
+	uint8_t	upgrade;
+	uint8_t	l1cache;
+	uint8_t	l2cache;
+	uint8_t	l3cache;
+	uint8_t	serial;		/* string */
+	uint8_t	asset;		/* string */
+	uint8_t	part;		/* string */
+	uint8_t	cores;		/* cores per socket */
+	uint8_t	enabled;	/* enabled cores per socket */
+	uint8_t	threads;	/* threads per socket */
+	uint16_t	characteristics;
+	uint16_t	family2;	/* for values >= 255 */
+	uint16_t	cores2;		/* for values >= 255 */
+	uint16_t	enabled2;	/* for values >= 255 */
+	uint16_t	threads2;	/* for values >= 255 */
+} __packed;
+
+/*
+ * SMBIOS Structure Type 9 "Expansion slot"
+ */
+struct smbios_slot {
+	uint8_t		designation;
+	uint8_t		type;
+	uint8_t		width;
+	uint8_t		usage;
+	uint8_t		length;
+	uint8_t		slotid[2];
+	uint8_t		characteristics[2];
+} __packed;
+
+#define	SMBIOS_SLOT_ISA		0x03
+#define	SMBIOS_SLOT_EISA	0x05
+
+/*
+ * SMBIOS Structure Type 38 "IPMI Information"
+ * DMTF Specification DSP0134 Section 3.3.39 p.g. 91
+ */
+struct smbios_ipmi {
+        uint8_t        smipmi_if_type;         /* IPMI Interface Type */
+        uint8_t        smipmi_if_rev;          /* BCD IPMI Revision */
+        uint8_t        smipmi_i2c_address;     /* I2C address of BMC */
+        uint8_t        smipmi_nvram_address;   /* I2C address of NVRAM
+						 * storage */
+        uint64_t       smipmi_base_address;    /* Base address of BMC (BAR
+						 * format */
+        uint8_t        smipmi_base_flags;      /* Flags field:
+						 * bit 7:6 : register spacing
+						 *   00 = byte
+						 *   01 = dword
+						 *   02 = word
+						 * bit 4 : Lower bit BAR
+						 * bit 3 : IRQ valid
+						 * bit 2 : N/A
+						 * bit 1 : Interrupt polarity
+						 * bit 0 : Interrupt trigger */
+        uint8_t        smipmi_irq;             /* IRQ if applicable */
+} __packed;
+
+extern struct smbios_entry smbios_entry;
+
+int smbios2_check_header(const uint8_t *);
+int smbios3_check_header(const uint8_t *);
+int smbios_find_table(uint8_t, struct smbtable *);
+char *smbios_get_string(struct smbtable *, uint8_t, char *, size_t);
+
+#endif

Reply via email to