Home | History | Annotate | Line # | Download | only in dev
smbios.c revision 1.1
      1  1.1  jmcneill /*	$NetBSD: smbios.c,v 1.1 2021/07/21 23:16:09 jmcneill Exp $	*/
      2  1.1  jmcneill 
      3  1.1  jmcneill /*
      4  1.1  jmcneill  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jmcneill  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  jmcneill  * NASA Ames Research Center.
     10  1.1  jmcneill  *
     11  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     12  1.1  jmcneill  * modification, are permitted provided that the following conditions
     13  1.1  jmcneill  * are met:
     14  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     15  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     16  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     19  1.1  jmcneill  *
     20  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1  jmcneill  */
     32  1.1  jmcneill 
     33  1.1  jmcneill /*
     34  1.1  jmcneill  * Copyright (c) 1999, by UCHIYAMA Yasushi
     35  1.1  jmcneill  * All rights reserved.
     36  1.1  jmcneill  *
     37  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     38  1.1  jmcneill  * modification, are permitted provided that the following conditions
     39  1.1  jmcneill  * are met:
     40  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     41  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     42  1.1  jmcneill  * 2. The name of the developer may NOT be used to endorse or promote products
     43  1.1  jmcneill  *    derived from this software without specific prior written permission.
     44  1.1  jmcneill  *
     45  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     46  1.1  jmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  1.1  jmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  1.1  jmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     49  1.1  jmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  1.1  jmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  1.1  jmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  1.1  jmcneill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  1.1  jmcneill  * SUCH DAMAGE.
     56  1.1  jmcneill  */
     57  1.1  jmcneill 
     58  1.1  jmcneill /*
     59  1.1  jmcneill  * Copyright (c) 1997-2001 Michael Shalayeff
     60  1.1  jmcneill  * All rights reserved.
     61  1.1  jmcneill  *
     62  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     63  1.1  jmcneill  * modification, are permitted provided that the following conditions
     64  1.1  jmcneill  * are met:
     65  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     66  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     67  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     68  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     69  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     70  1.1  jmcneill  *
     71  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     72  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     73  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     74  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     75  1.1  jmcneill  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     76  1.1  jmcneill  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     77  1.1  jmcneill  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     78  1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     79  1.1  jmcneill  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     80  1.1  jmcneill  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     81  1.1  jmcneill  * THE POSSIBILITY OF SUCH DAMAGE.
     82  1.1  jmcneill  */
     83  1.1  jmcneill 
     84  1.1  jmcneill /*
     85  1.1  jmcneill  * Basic interface to System Management BIOS (SMBIOS) tables.
     86  1.1  jmcneill  */
     87  1.1  jmcneill 
     88  1.1  jmcneill #include <sys/cdefs.h>
     89  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: smbios.c,v 1.1 2021/07/21 23:16:09 jmcneill Exp $");
     90  1.1  jmcneill 
     91  1.1  jmcneill #include <sys/param.h>
     92  1.1  jmcneill #include <sys/systm.h>
     93  1.1  jmcneill #include <sys/device.h>
     94  1.1  jmcneill 
     95  1.1  jmcneill #include <dev/smbiosvar.h>
     96  1.1  jmcneill 
     97  1.1  jmcneill #define	SMBIOS_MAKESIG(a, b, c, d)	\
     98  1.1  jmcneill 	((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
     99  1.1  jmcneill 
    100  1.1  jmcneill struct smbios_entry smbios_entry;
    101  1.1  jmcneill 
    102  1.1  jmcneill int
    103  1.1  jmcneill smbios2_check_header(const uint8_t *p)
    104  1.1  jmcneill {
    105  1.1  jmcneill 	const struct smbhdr *sh = (const struct smbhdr *)p;
    106  1.1  jmcneill 	uint8_t chksum;
    107  1.1  jmcneill 	int i;
    108  1.1  jmcneill 
    109  1.1  jmcneill 	if (sh->sig != SMBIOS_MAKESIG('_', 'S', 'M', '_'))
    110  1.1  jmcneill 		return 0;
    111  1.1  jmcneill 	i = sh->len;
    112  1.1  jmcneill 	for (chksum = 0; i--; )
    113  1.1  jmcneill 		chksum += p[i];
    114  1.1  jmcneill 	if (chksum != 0)
    115  1.1  jmcneill 		return 0;
    116  1.1  jmcneill 	p += 0x10;
    117  1.1  jmcneill 	if (p[0] != '_' || p[1] != 'D' || p[2] != 'M' ||
    118  1.1  jmcneill 	    p[3] != 'I' || p[4] != '_')
    119  1.1  jmcneill 		return 0;
    120  1.1  jmcneill 	for (chksum = 0, i = 0xf; i--; )
    121  1.1  jmcneill 		chksum += p[i];
    122  1.1  jmcneill 	if (chksum != 0)
    123  1.1  jmcneill 		return 0;
    124  1.1  jmcneill 
    125  1.1  jmcneill 	return 1;
    126  1.1  jmcneill }
    127  1.1  jmcneill 
    128  1.1  jmcneill int
    129  1.1  jmcneill smbios3_check_header(const uint8_t *p)
    130  1.1  jmcneill {
    131  1.1  jmcneill 	const struct smb3hdr *sh = (const struct smb3hdr *)p;
    132  1.1  jmcneill 	uint8_t chksum;
    133  1.1  jmcneill 	int i;
    134  1.1  jmcneill 
    135  1.1  jmcneill 	if (p[0] != '_' || p[1] != 'S' || p[2] != 'M' ||
    136  1.1  jmcneill 	    p[3] != '3' || p[4] != '_')
    137  1.1  jmcneill 		return 0;
    138  1.1  jmcneill 	i = sh->len;
    139  1.1  jmcneill 	for (chksum = 0; i--; )
    140  1.1  jmcneill 		chksum += p[i];
    141  1.1  jmcneill 	if (chksum != 0)
    142  1.1  jmcneill 		return 0;
    143  1.1  jmcneill 	if (sh->eprev != SMBIOS3_EPREV_3_0)
    144  1.1  jmcneill 		return 0;
    145  1.1  jmcneill 
    146  1.1  jmcneill 	return 1;
    147  1.1  jmcneill }
    148  1.1  jmcneill 
    149  1.1  jmcneill /*
    150  1.1  jmcneill  * smbios_find_table() takes a caller supplied smbios struct type and
    151  1.1  jmcneill  * a pointer to a handle (struct smbtable) returning one if the structure
    152  1.1  jmcneill  * is successfully located and zero otherwise. Callers should take care
    153  1.1  jmcneill  * to initilize the cookie field of the smbtable structure to zero before
    154  1.1  jmcneill  * the first invocation of this function.
    155  1.1  jmcneill  * Multiple tables of the same type can be located by repeadtly calling
    156  1.1  jmcneill  * smbios_find_table with the same arguments.
    157  1.1  jmcneill  */
    158  1.1  jmcneill int
    159  1.1  jmcneill smbios_find_table(uint8_t type, struct smbtable *st)
    160  1.1  jmcneill {
    161  1.1  jmcneill 	uint8_t *va, *end;
    162  1.1  jmcneill 	struct smbtblhdr *hdr;
    163  1.1  jmcneill 	int ret = 0, tcount = 1;
    164  1.1  jmcneill 
    165  1.1  jmcneill 	if (smbios_entry.addr == NULL) {
    166  1.1  jmcneill 		return 0;
    167  1.1  jmcneill 	}
    168  1.1  jmcneill 
    169  1.1  jmcneill 	va = smbios_entry.addr;
    170  1.1  jmcneill 	end = va + smbios_entry.len;
    171  1.1  jmcneill 
    172  1.1  jmcneill 	/*
    173  1.1  jmcneill 	 * The cookie field of the smtable structure is used to locate
    174  1.1  jmcneill 	 * multiple instances of a table of an arbitrary type. Following the
    175  1.1  jmcneill 	 * sucessful location of a table, the type is encoded as bits 0:7 of
    176  1.1  jmcneill 	 * the cookie value, the offset in terms of the number of structures
    177  1.1  jmcneill 	 * preceding that referenced by the handle is encoded in bits 15:31.
    178  1.1  jmcneill 	 */
    179  1.1  jmcneill 	if ((st->cookie & 0xfff) == type && st->cookie >> 16) {
    180  1.1  jmcneill 		if ((uint8_t *)st->hdr >= va && (uint8_t *)st->hdr < end) {
    181  1.1  jmcneill 			hdr = st->hdr;
    182  1.1  jmcneill 			if (hdr->type == type) {
    183  1.1  jmcneill 				va = (uint8_t *)hdr + hdr->size;
    184  1.1  jmcneill 				for (; va + 1 < end; va++)
    185  1.1  jmcneill 					if (*va == 0 && *(va + 1) == 0)
    186  1.1  jmcneill 						break;
    187  1.1  jmcneill 				va+= 2;
    188  1.1  jmcneill 				tcount = st->cookie >> 16;
    189  1.1  jmcneill 			}
    190  1.1  jmcneill 		}
    191  1.1  jmcneill 	}
    192  1.1  jmcneill 	for (; va + sizeof(struct smbtblhdr) < end && tcount <=
    193  1.1  jmcneill 	    smbios_entry.count; tcount++) {
    194  1.1  jmcneill 		hdr = (struct smbtblhdr *)va;
    195  1.1  jmcneill 		if (hdr->type == type) {
    196  1.1  jmcneill 			ret = 1;
    197  1.1  jmcneill 			st->hdr = hdr;
    198  1.1  jmcneill 			st->tblhdr = va + sizeof(struct smbtblhdr);
    199  1.1  jmcneill 			st->cookie = (tcount + 1) << 16 | type;
    200  1.1  jmcneill 			break;
    201  1.1  jmcneill 		}
    202  1.1  jmcneill 		if (hdr->type == SMBIOS_TYPE_EOT)
    203  1.1  jmcneill 			break;
    204  1.1  jmcneill 		va+= hdr->size;
    205  1.1  jmcneill 		for (; va + 1 < end; va++)
    206  1.1  jmcneill 			if (*va == 0 && *(va + 1) == 0)
    207  1.1  jmcneill 				break;
    208  1.1  jmcneill 		va+=2;
    209  1.1  jmcneill 	}
    210  1.1  jmcneill 
    211  1.1  jmcneill 	return ret;
    212  1.1  jmcneill }
    213  1.1  jmcneill 
    214  1.1  jmcneill char *
    215  1.1  jmcneill smbios_get_string(struct smbtable *st, uint8_t indx, char *dest, size_t len)
    216  1.1  jmcneill {
    217  1.1  jmcneill 	uint8_t *va, *end;
    218  1.1  jmcneill 	char *ret = NULL;
    219  1.1  jmcneill 	int i;
    220  1.1  jmcneill 
    221  1.1  jmcneill 	KASSERT(smbios_entry.addr != NULL);
    222  1.1  jmcneill 
    223  1.1  jmcneill 	va = (uint8_t *)st->hdr + st->hdr->size;
    224  1.1  jmcneill 	end = smbios_entry.addr + smbios_entry.len;
    225  1.1  jmcneill 	for (i = 1; va < end && i < indx && *va; i++)
    226  1.1  jmcneill 		while (*va++)
    227  1.1  jmcneill 			;
    228  1.1  jmcneill 	if (i == indx) {
    229  1.1  jmcneill 		if (va + len < end) {
    230  1.1  jmcneill 			ret = dest;
    231  1.1  jmcneill 			memcpy(ret, va, len);
    232  1.1  jmcneill 			ret[len - 1] = '\0';
    233  1.1  jmcneill 		}
    234  1.1  jmcneill 	}
    235  1.1  jmcneill 
    236  1.1  jmcneill 	return ret;
    237  1.1  jmcneill }
    238