Home | History | Annotate | Line # | Download | only in efiboot
smbios.c revision 1.2.14.1
      1  1.2.14.1   thorpej /*	$NetBSD: smbios.c,v 1.2.14.1 2021/08/01 22:42:44 thorpej 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 #include <sys/cdefs.h>
     85  1.2.14.1   thorpej __KERNEL_RCSID(0, "$NetBSD: smbios.c,v 1.2.14.1 2021/08/01 22:42:44 thorpej Exp $");
     86       1.1  jmcneill 
     87       1.1  jmcneill #include <sys/param.h>
     88       1.1  jmcneill 
     89       1.1  jmcneill #include "efiboot.h"
     90       1.1  jmcneill #include "smbios.h"
     91       1.1  jmcneill 
     92       1.1  jmcneill struct smbios_entry smbios_entry;
     93       1.1  jmcneill 
     94  1.2.14.1   thorpej static void
     95  1.2.14.1   thorpej smbios2_init(uint8_t *p)
     96  1.2.14.1   thorpej {
     97  1.2.14.1   thorpej 	const struct smbhdr *sh = (const struct smbhdr *)p;
     98  1.2.14.1   thorpej 
     99  1.2.14.1   thorpej 	smbios_entry.addr = (void *)(uintptr_t)sh->addr;
    100  1.2.14.1   thorpej 	smbios_entry.len = sh->size;
    101  1.2.14.1   thorpej 	smbios_entry.rev = 0;
    102  1.2.14.1   thorpej 	smbios_entry.mjr = sh->majrev;
    103  1.2.14.1   thorpej 	smbios_entry.min = sh->minrev;
    104  1.2.14.1   thorpej 	smbios_entry.doc = 0;
    105  1.2.14.1   thorpej 	smbios_entry.count = sh->count;
    106  1.2.14.1   thorpej }
    107  1.2.14.1   thorpej 
    108  1.2.14.1   thorpej static void
    109  1.2.14.1   thorpej smbios3_init(uint8_t *p)
    110       1.1  jmcneill {
    111       1.1  jmcneill 	const struct smb3hdr *sh = (const struct smb3hdr *)p;
    112       1.1  jmcneill 
    113       1.1  jmcneill 	smbios_entry.addr = (void *)(uintptr_t)sh->addr;
    114       1.1  jmcneill 	smbios_entry.len = sh->size;
    115       1.1  jmcneill 	smbios_entry.rev = sh->eprev;
    116       1.1  jmcneill 	smbios_entry.mjr = sh->majrev;
    117       1.1  jmcneill 	smbios_entry.min = sh->minrev;
    118       1.1  jmcneill 	smbios_entry.doc = sh->docrev;
    119       1.1  jmcneill 	smbios_entry.count = UINT16_MAX;
    120       1.1  jmcneill }
    121       1.1  jmcneill 
    122  1.2.14.1   thorpej void
    123  1.2.14.1   thorpej smbios_init(uint8_t *p)
    124  1.2.14.1   thorpej {
    125  1.2.14.1   thorpej 	if (memcmp(p, "_SM3_", 5) == 0) {
    126  1.2.14.1   thorpej 		smbios3_init(p);
    127  1.2.14.1   thorpej 	} else if (memcmp(p, "_SM_", 4) == 0) {
    128  1.2.14.1   thorpej 		smbios2_init(p);
    129  1.2.14.1   thorpej 	}
    130  1.2.14.1   thorpej }
    131  1.2.14.1   thorpej 
    132       1.1  jmcneill /*
    133       1.1  jmcneill  * smbios_find_table() takes a caller supplied smbios struct type and
    134       1.1  jmcneill  * a pointer to a handle (struct smbtable) returning one if the structure
    135       1.2   msaitoh  * is successfully located and zero otherwise. Callers should take care
    136       1.1  jmcneill  * to initilize the cookie field of the smbtable structure to zero before
    137       1.1  jmcneill  * the first invocation of this function.
    138       1.1  jmcneill  * Multiple tables of the same type can be located by repeadtly calling
    139       1.1  jmcneill  * smbios_find_table with the same arguments.
    140       1.1  jmcneill  */
    141       1.1  jmcneill int
    142       1.1  jmcneill smbios_find_table(uint8_t type, struct smbtable *st)
    143       1.1  jmcneill {
    144       1.1  jmcneill 	uint8_t *va, *end;
    145       1.1  jmcneill 	struct smbtblhdr *hdr;
    146       1.1  jmcneill 	int ret = 0, tcount = 1;
    147       1.1  jmcneill 
    148  1.2.14.1   thorpej 	if (smbios_entry.addr == 0) {
    149  1.2.14.1   thorpej 		return 0;
    150  1.2.14.1   thorpej 	}
    151  1.2.14.1   thorpej 
    152       1.1  jmcneill 	va = smbios_entry.addr;
    153       1.1  jmcneill 	end = va + smbios_entry.len;
    154       1.1  jmcneill 
    155       1.1  jmcneill 	/*
    156       1.1  jmcneill 	 * The cookie field of the smtable structure is used to locate
    157       1.1  jmcneill 	 * multiple instances of a table of an arbitrary type. Following the
    158       1.1  jmcneill 	 * sucessful location of a table, the type is encoded as bits 0:7 of
    159       1.1  jmcneill 	 * the cookie value, the offset in terms of the number of structures
    160       1.1  jmcneill 	 * preceding that referenced by the handle is encoded in bits 15:31.
    161       1.1  jmcneill 	 */
    162       1.1  jmcneill 	if ((st->cookie & 0xfff) == type && st->cookie >> 16) {
    163       1.1  jmcneill 		if ((uint8_t *)st->hdr >= va && (uint8_t *)st->hdr < end) {
    164       1.1  jmcneill 			hdr = st->hdr;
    165       1.1  jmcneill 			if (hdr->type == type) {
    166       1.1  jmcneill 				va = (uint8_t *)hdr + hdr->size;
    167       1.1  jmcneill 				for (; va + 1 < end; va++)
    168       1.1  jmcneill 					if (*va == 0 && *(va + 1) == 0)
    169       1.1  jmcneill 						break;
    170       1.1  jmcneill 				va+= 2;
    171       1.1  jmcneill 				tcount = st->cookie >> 16;
    172       1.1  jmcneill 			}
    173       1.1  jmcneill 		}
    174       1.1  jmcneill 	}
    175       1.1  jmcneill 	for (; va + sizeof(struct smbtblhdr) < end && tcount <=
    176       1.1  jmcneill 	    smbios_entry.count; tcount++) {
    177       1.1  jmcneill 		hdr = (struct smbtblhdr *)va;
    178       1.1  jmcneill 		if (hdr->type == type) {
    179       1.1  jmcneill 			ret = 1;
    180       1.1  jmcneill 			st->hdr = hdr;
    181       1.1  jmcneill 			st->tblhdr = va + sizeof(struct smbtblhdr);
    182       1.1  jmcneill 			st->cookie = (tcount + 1) << 16 | type;
    183       1.1  jmcneill 			break;
    184       1.1  jmcneill 		}
    185       1.1  jmcneill 		if (hdr->type == SMBIOS_TYPE_EOT)
    186       1.1  jmcneill 			break;
    187       1.1  jmcneill 		va+= hdr->size;
    188       1.1  jmcneill 		for (; va + 1 < end; va++)
    189       1.1  jmcneill 			if (*va == 0 && *(va + 1) == 0)
    190       1.1  jmcneill 				break;
    191       1.1  jmcneill 		va+=2;
    192       1.1  jmcneill 	}
    193       1.1  jmcneill 
    194       1.1  jmcneill 	return ret;
    195       1.1  jmcneill }
    196       1.1  jmcneill 
    197       1.1  jmcneill char *
    198       1.1  jmcneill smbios_get_string(struct smbtable *st, uint8_t indx, char *dest, size_t len)
    199       1.1  jmcneill {
    200       1.1  jmcneill 	uint8_t *va, *end;
    201       1.1  jmcneill 	char *ret = NULL;
    202       1.1  jmcneill 	int i;
    203       1.1  jmcneill 
    204  1.2.14.1   thorpej 	if (smbios_entry.addr == 0) {
    205  1.2.14.1   thorpej 		return NULL;
    206  1.2.14.1   thorpej 	}
    207  1.2.14.1   thorpej 
    208       1.1  jmcneill 	va = (uint8_t *)st->hdr + st->hdr->size;
    209       1.1  jmcneill 	end = smbios_entry.addr + smbios_entry.len;
    210       1.1  jmcneill 	for (i = 1; va < end && i < indx && *va; i++)
    211       1.1  jmcneill 		while (*va++)
    212       1.1  jmcneill 			;
    213       1.1  jmcneill 	if (i == indx) {
    214       1.1  jmcneill 		if (va + len < end) {
    215       1.1  jmcneill 			ret = dest;
    216       1.1  jmcneill 			memcpy(ret, va, len);
    217       1.1  jmcneill 			ret[len - 1] = '\0';
    218       1.1  jmcneill 		}
    219       1.1  jmcneill 	}
    220       1.1  jmcneill 
    221       1.1  jmcneill 	return ret;
    222       1.1  jmcneill }
    223