Home | History | Annotate | Line # | Download | only in dev
smbios.c revision 1.2
      1  1.2  jmcneill /*	$NetBSD: smbios.c,v 1.2 2021/07/24 11:39:19 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.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: smbios.c,v 1.2 2021/07/24 11:39:19 jmcneill Exp $");
     90  1.1  jmcneill 
     91  1.1  jmcneill #include <sys/param.h>
     92  1.2  jmcneill #include <sys/conf.h>
     93  1.1  jmcneill #include <sys/systm.h>
     94  1.1  jmcneill #include <sys/device.h>
     95  1.1  jmcneill 
     96  1.2  jmcneill #include <uvm/uvm_extern.h>
     97  1.2  jmcneill 
     98  1.1  jmcneill #include <dev/smbiosvar.h>
     99  1.1  jmcneill 
    100  1.1  jmcneill #define	SMBIOS_MAKESIG(a, b, c, d)	\
    101  1.1  jmcneill 	((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
    102  1.1  jmcneill 
    103  1.1  jmcneill struct smbios_entry smbios_entry;
    104  1.1  jmcneill 
    105  1.2  jmcneill static dev_type_read(smbios_read);
    106  1.2  jmcneill 
    107  1.2  jmcneill const struct cdevsw smbios_cdevsw = {
    108  1.2  jmcneill 	.d_open		= nullopen,
    109  1.2  jmcneill 	.d_close	= nullclose,
    110  1.2  jmcneill 	.d_read		= smbios_read,
    111  1.2  jmcneill 	.d_write	= nowrite,
    112  1.2  jmcneill 	.d_ioctl	= noioctl,
    113  1.2  jmcneill 	.d_stop		= nostop,
    114  1.2  jmcneill 	.d_tty		= notty,
    115  1.2  jmcneill 	.d_poll		= nopoll,
    116  1.2  jmcneill 	.d_mmap		= nommap,
    117  1.2  jmcneill 	.d_kqfilter	= nokqfilter,
    118  1.2  jmcneill 	.d_discard	= nodiscard,
    119  1.2  jmcneill 	.d_flag		= D_OTHER | D_MPSAFE,
    120  1.2  jmcneill };
    121  1.2  jmcneill 
    122  1.2  jmcneill static void *
    123  1.2  jmcneill smbios_map_memory(paddr_t pa, size_t size)
    124  1.2  jmcneill {
    125  1.2  jmcneill 	paddr_t spa, epa, curpa;
    126  1.2  jmcneill 	vaddr_t va, curva;
    127  1.2  jmcneill 
    128  1.2  jmcneill 	spa = trunc_page(pa);
    129  1.2  jmcneill 	epa = round_page(pa + size);
    130  1.2  jmcneill 
    131  1.2  jmcneill 	va = uvm_km_alloc(kernel_map, epa - spa, 0, UVM_KMF_VAONLY);
    132  1.2  jmcneill 	if (va == 0) {
    133  1.2  jmcneill 		return NULL;
    134  1.2  jmcneill 	}
    135  1.2  jmcneill 
    136  1.2  jmcneill 	for (curpa = spa, curva = va; curpa < epa; curpa += PAGE_SIZE, curva += PAGE_SIZE) {
    137  1.2  jmcneill 		pmap_kenter_pa(curva, curpa, VM_PROT_READ, PMAP_WRITE_BACK);
    138  1.2  jmcneill 	}
    139  1.2  jmcneill 	pmap_update(pmap_kernel());
    140  1.2  jmcneill 
    141  1.2  jmcneill 	return (void *)(va + (pa - spa));
    142  1.2  jmcneill }
    143  1.2  jmcneill 
    144  1.2  jmcneill static void
    145  1.2  jmcneill smbios_unmap_memory(void *va, size_t size)
    146  1.2  jmcneill {
    147  1.2  jmcneill 	vaddr_t ova;
    148  1.2  jmcneill 	vsize_t osz;
    149  1.2  jmcneill 
    150  1.2  jmcneill 	ova = trunc_page((vaddr_t)va);
    151  1.2  jmcneill 	osz = round_page((vaddr_t)va + size) - ova;
    152  1.2  jmcneill 
    153  1.2  jmcneill 	pmap_kremove(ova, osz);
    154  1.2  jmcneill 	pmap_update(pmap_kernel());
    155  1.2  jmcneill 	uvm_km_free(kernel_map, ova, osz, UVM_KMF_VAONLY);
    156  1.2  jmcneill }
    157  1.2  jmcneill 
    158  1.2  jmcneill /*
    159  1.2  jmcneill  * smbios_read --
    160  1.2  jmcneill  *
    161  1.2  jmcneill  *	Read data from an SMBIOS table that resides in physical memory.
    162  1.2  jmcneill  */
    163  1.2  jmcneill static int
    164  1.2  jmcneill smbios_read(dev_t dev, struct uio *uio, int flag)
    165  1.2  jmcneill {
    166  1.2  jmcneill 	paddr_t pa;
    167  1.2  jmcneill 	uint8_t *data;
    168  1.2  jmcneill 	size_t len;
    169  1.2  jmcneill 	int error;
    170  1.2  jmcneill 
    171  1.2  jmcneill 	if (smbios_entry.addr == NULL) {
    172  1.2  jmcneill 		return EIO;
    173  1.2  jmcneill 	}
    174  1.2  jmcneill 	if (uio->uio_rw != UIO_READ) {
    175  1.2  jmcneill 		return EPERM;
    176  1.2  jmcneill 	}
    177  1.2  jmcneill 
    178  1.2  jmcneill 	pa = uio->uio_offset;
    179  1.2  jmcneill 	if (pa == smbios_entry.hdrphys) {
    180  1.2  jmcneill 		/* SMBIOS header */
    181  1.2  jmcneill 		len = uimin(0x20, uio->uio_resid);
    182  1.2  jmcneill 
    183  1.2  jmcneill 	} else {
    184  1.2  jmcneill 		/* Table data */
    185  1.2  jmcneill 		if (pa < smbios_entry.tabphys ||
    186  1.2  jmcneill 		    pa >= smbios_entry.tabphys + smbios_entry.len) {
    187  1.2  jmcneill 			return EFAULT;
    188  1.2  jmcneill 		}
    189  1.2  jmcneill 		len = uimin(smbios_entry.len - (pa - smbios_entry.tabphys),
    190  1.2  jmcneill 			    uio->uio_resid);
    191  1.2  jmcneill 	}
    192  1.2  jmcneill 
    193  1.2  jmcneill 	data = smbios_map_memory(pa, len);
    194  1.2  jmcneill 	if (data == NULL) {
    195  1.2  jmcneill 		return ENOMEM;
    196  1.2  jmcneill 	}
    197  1.2  jmcneill 	error = uiomove(data, len, uio);
    198  1.2  jmcneill 	smbios_unmap_memory(data, len);
    199  1.2  jmcneill 
    200  1.2  jmcneill 	return error;
    201  1.2  jmcneill }
    202  1.2  jmcneill 
    203  1.1  jmcneill int
    204  1.1  jmcneill smbios2_check_header(const uint8_t *p)
    205  1.1  jmcneill {
    206  1.1  jmcneill 	const struct smbhdr *sh = (const struct smbhdr *)p;
    207  1.1  jmcneill 	uint8_t chksum;
    208  1.1  jmcneill 	int i;
    209  1.1  jmcneill 
    210  1.1  jmcneill 	if (sh->sig != SMBIOS_MAKESIG('_', 'S', 'M', '_'))
    211  1.1  jmcneill 		return 0;
    212  1.1  jmcneill 	i = sh->len;
    213  1.1  jmcneill 	for (chksum = 0; i--; )
    214  1.1  jmcneill 		chksum += p[i];
    215  1.1  jmcneill 	if (chksum != 0)
    216  1.1  jmcneill 		return 0;
    217  1.1  jmcneill 	p += 0x10;
    218  1.1  jmcneill 	if (p[0] != '_' || p[1] != 'D' || p[2] != 'M' ||
    219  1.1  jmcneill 	    p[3] != 'I' || p[4] != '_')
    220  1.1  jmcneill 		return 0;
    221  1.1  jmcneill 	for (chksum = 0, i = 0xf; i--; )
    222  1.1  jmcneill 		chksum += p[i];
    223  1.1  jmcneill 	if (chksum != 0)
    224  1.1  jmcneill 		return 0;
    225  1.1  jmcneill 
    226  1.1  jmcneill 	return 1;
    227  1.1  jmcneill }
    228  1.1  jmcneill 
    229  1.1  jmcneill int
    230  1.1  jmcneill smbios3_check_header(const uint8_t *p)
    231  1.1  jmcneill {
    232  1.1  jmcneill 	const struct smb3hdr *sh = (const struct smb3hdr *)p;
    233  1.1  jmcneill 	uint8_t chksum;
    234  1.1  jmcneill 	int i;
    235  1.1  jmcneill 
    236  1.1  jmcneill 	if (p[0] != '_' || p[1] != 'S' || p[2] != 'M' ||
    237  1.1  jmcneill 	    p[3] != '3' || p[4] != '_')
    238  1.1  jmcneill 		return 0;
    239  1.1  jmcneill 	i = sh->len;
    240  1.1  jmcneill 	for (chksum = 0; i--; )
    241  1.1  jmcneill 		chksum += p[i];
    242  1.1  jmcneill 	if (chksum != 0)
    243  1.1  jmcneill 		return 0;
    244  1.1  jmcneill 	if (sh->eprev != SMBIOS3_EPREV_3_0)
    245  1.1  jmcneill 		return 0;
    246  1.1  jmcneill 
    247  1.1  jmcneill 	return 1;
    248  1.1  jmcneill }
    249  1.1  jmcneill 
    250  1.1  jmcneill /*
    251  1.1  jmcneill  * smbios_find_table() takes a caller supplied smbios struct type and
    252  1.1  jmcneill  * a pointer to a handle (struct smbtable) returning one if the structure
    253  1.1  jmcneill  * is successfully located and zero otherwise. Callers should take care
    254  1.1  jmcneill  * to initilize the cookie field of the smbtable structure to zero before
    255  1.1  jmcneill  * the first invocation of this function.
    256  1.1  jmcneill  * Multiple tables of the same type can be located by repeadtly calling
    257  1.1  jmcneill  * smbios_find_table with the same arguments.
    258  1.1  jmcneill  */
    259  1.1  jmcneill int
    260  1.1  jmcneill smbios_find_table(uint8_t type, struct smbtable *st)
    261  1.1  jmcneill {
    262  1.1  jmcneill 	uint8_t *va, *end;
    263  1.1  jmcneill 	struct smbtblhdr *hdr;
    264  1.1  jmcneill 	int ret = 0, tcount = 1;
    265  1.1  jmcneill 
    266  1.1  jmcneill 	if (smbios_entry.addr == NULL) {
    267  1.1  jmcneill 		return 0;
    268  1.1  jmcneill 	}
    269  1.1  jmcneill 
    270  1.1  jmcneill 	va = smbios_entry.addr;
    271  1.1  jmcneill 	end = va + smbios_entry.len;
    272  1.1  jmcneill 
    273  1.1  jmcneill 	/*
    274  1.1  jmcneill 	 * The cookie field of the smtable structure is used to locate
    275  1.1  jmcneill 	 * multiple instances of a table of an arbitrary type. Following the
    276  1.1  jmcneill 	 * sucessful location of a table, the type is encoded as bits 0:7 of
    277  1.1  jmcneill 	 * the cookie value, the offset in terms of the number of structures
    278  1.1  jmcneill 	 * preceding that referenced by the handle is encoded in bits 15:31.
    279  1.1  jmcneill 	 */
    280  1.1  jmcneill 	if ((st->cookie & 0xfff) == type && st->cookie >> 16) {
    281  1.1  jmcneill 		if ((uint8_t *)st->hdr >= va && (uint8_t *)st->hdr < end) {
    282  1.1  jmcneill 			hdr = st->hdr;
    283  1.1  jmcneill 			if (hdr->type == type) {
    284  1.1  jmcneill 				va = (uint8_t *)hdr + hdr->size;
    285  1.1  jmcneill 				for (; va + 1 < end; va++)
    286  1.1  jmcneill 					if (*va == 0 && *(va + 1) == 0)
    287  1.1  jmcneill 						break;
    288  1.1  jmcneill 				va+= 2;
    289  1.1  jmcneill 				tcount = st->cookie >> 16;
    290  1.1  jmcneill 			}
    291  1.1  jmcneill 		}
    292  1.1  jmcneill 	}
    293  1.1  jmcneill 	for (; va + sizeof(struct smbtblhdr) < end && tcount <=
    294  1.1  jmcneill 	    smbios_entry.count; tcount++) {
    295  1.1  jmcneill 		hdr = (struct smbtblhdr *)va;
    296  1.1  jmcneill 		if (hdr->type == type) {
    297  1.1  jmcneill 			ret = 1;
    298  1.1  jmcneill 			st->hdr = hdr;
    299  1.1  jmcneill 			st->tblhdr = va + sizeof(struct smbtblhdr);
    300  1.1  jmcneill 			st->cookie = (tcount + 1) << 16 | type;
    301  1.1  jmcneill 			break;
    302  1.1  jmcneill 		}
    303  1.1  jmcneill 		if (hdr->type == SMBIOS_TYPE_EOT)
    304  1.1  jmcneill 			break;
    305  1.1  jmcneill 		va+= hdr->size;
    306  1.1  jmcneill 		for (; va + 1 < end; va++)
    307  1.1  jmcneill 			if (*va == 0 && *(va + 1) == 0)
    308  1.1  jmcneill 				break;
    309  1.1  jmcneill 		va+=2;
    310  1.1  jmcneill 	}
    311  1.1  jmcneill 
    312  1.1  jmcneill 	return ret;
    313  1.1  jmcneill }
    314  1.1  jmcneill 
    315  1.1  jmcneill char *
    316  1.1  jmcneill smbios_get_string(struct smbtable *st, uint8_t indx, char *dest, size_t len)
    317  1.1  jmcneill {
    318  1.1  jmcneill 	uint8_t *va, *end;
    319  1.1  jmcneill 	char *ret = NULL;
    320  1.1  jmcneill 	int i;
    321  1.1  jmcneill 
    322  1.1  jmcneill 	KASSERT(smbios_entry.addr != NULL);
    323  1.1  jmcneill 
    324  1.1  jmcneill 	va = (uint8_t *)st->hdr + st->hdr->size;
    325  1.1  jmcneill 	end = smbios_entry.addr + smbios_entry.len;
    326  1.1  jmcneill 	for (i = 1; va < end && i < indx && *va; i++)
    327  1.1  jmcneill 		while (*va++)
    328  1.1  jmcneill 			;
    329  1.1  jmcneill 	if (i == indx) {
    330  1.1  jmcneill 		if (va + len < end) {
    331  1.1  jmcneill 			ret = dest;
    332  1.1  jmcneill 			memcpy(ret, va, len);
    333  1.1  jmcneill 			ret[len - 1] = '\0';
    334  1.1  jmcneill 		}
    335  1.1  jmcneill 	}
    336  1.1  jmcneill 
    337  1.1  jmcneill 	return ret;
    338  1.1  jmcneill }
    339