xref: /netbsd-src/sys/stand/efiboot/smbios.c (revision b58602814f44cdb10d1ce70b9ef75bf29672fcc0)
1*b5860281Sandvar /*	$NetBSD: smbios.c,v 1.4 2021/09/16 22:19:11 andvar Exp $	*/
2652b3556Sjmcneill 
3652b3556Sjmcneill /*
4652b3556Sjmcneill  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5652b3556Sjmcneill  * All rights reserved.
6652b3556Sjmcneill  *
7652b3556Sjmcneill  * This code is derived from software contributed to The NetBSD Foundation
8652b3556Sjmcneill  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9652b3556Sjmcneill  * NASA Ames Research Center.
10652b3556Sjmcneill  *
11652b3556Sjmcneill  * Redistribution and use in source and binary forms, with or without
12652b3556Sjmcneill  * modification, are permitted provided that the following conditions
13652b3556Sjmcneill  * are met:
14652b3556Sjmcneill  * 1. Redistributions of source code must retain the above copyright
15652b3556Sjmcneill  *    notice, this list of conditions and the following disclaimer.
16652b3556Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
17652b3556Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
18652b3556Sjmcneill  *    documentation and/or other materials provided with the distribution.
19652b3556Sjmcneill  *
20652b3556Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21652b3556Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22652b3556Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23652b3556Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24652b3556Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25652b3556Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26652b3556Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27652b3556Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28652b3556Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29652b3556Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30652b3556Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
31652b3556Sjmcneill  */
32652b3556Sjmcneill 
33652b3556Sjmcneill /*
34652b3556Sjmcneill  * Copyright (c) 1999, by UCHIYAMA Yasushi
35652b3556Sjmcneill  * All rights reserved.
36652b3556Sjmcneill  *
37652b3556Sjmcneill  * Redistribution and use in source and binary forms, with or without
38652b3556Sjmcneill  * modification, are permitted provided that the following conditions
39652b3556Sjmcneill  * are met:
40652b3556Sjmcneill  * 1. Redistributions of source code must retain the above copyright
41652b3556Sjmcneill  *    notice, this list of conditions and the following disclaimer.
42652b3556Sjmcneill  * 2. The name of the developer may NOT be used to endorse or promote products
43652b3556Sjmcneill  *    derived from this software without specific prior written permission.
44652b3556Sjmcneill  *
45652b3556Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46652b3556Sjmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47652b3556Sjmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48652b3556Sjmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49652b3556Sjmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50652b3556Sjmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51652b3556Sjmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52652b3556Sjmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53652b3556Sjmcneill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54652b3556Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55652b3556Sjmcneill  * SUCH DAMAGE.
56652b3556Sjmcneill  */
57652b3556Sjmcneill 
58652b3556Sjmcneill /*
59652b3556Sjmcneill  * Copyright (c) 1997-2001 Michael Shalayeff
60652b3556Sjmcneill  * All rights reserved.
61652b3556Sjmcneill  *
62652b3556Sjmcneill  * Redistribution and use in source and binary forms, with or without
63652b3556Sjmcneill  * modification, are permitted provided that the following conditions
64652b3556Sjmcneill  * are met:
65652b3556Sjmcneill  * 1. Redistributions of source code must retain the above copyright
66652b3556Sjmcneill  *    notice, this list of conditions and the following disclaimer.
67652b3556Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
68652b3556Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
69652b3556Sjmcneill  *    documentation and/or other materials provided with the distribution.
70652b3556Sjmcneill  *
71652b3556Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
72652b3556Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
73652b3556Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
74652b3556Sjmcneill  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
75652b3556Sjmcneill  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
76652b3556Sjmcneill  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77652b3556Sjmcneill  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78652b3556Sjmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
79652b3556Sjmcneill  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
80652b3556Sjmcneill  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
81652b3556Sjmcneill  * THE POSSIBILITY OF SUCH DAMAGE.
82652b3556Sjmcneill  */
83652b3556Sjmcneill 
84652b3556Sjmcneill #include <sys/cdefs.h>
85*b5860281Sandvar __KERNEL_RCSID(0, "$NetBSD: smbios.c,v 1.4 2021/09/16 22:19:11 andvar Exp $");
86652b3556Sjmcneill 
87652b3556Sjmcneill #include <sys/param.h>
88652b3556Sjmcneill 
89652b3556Sjmcneill #include "efiboot.h"
90652b3556Sjmcneill #include "smbios.h"
91652b3556Sjmcneill 
92652b3556Sjmcneill struct smbios_entry smbios_entry;
93652b3556Sjmcneill 
94a3c7cf62Sjmcneill static void
smbios2_init(uint8_t * p)95a3c7cf62Sjmcneill smbios2_init(uint8_t *p)
96a3c7cf62Sjmcneill {
97a3c7cf62Sjmcneill 	const struct smbhdr *sh = (const struct smbhdr *)p;
98a3c7cf62Sjmcneill 
99a3c7cf62Sjmcneill 	smbios_entry.addr = (void *)(uintptr_t)sh->addr;
100a3c7cf62Sjmcneill 	smbios_entry.len = sh->size;
101a3c7cf62Sjmcneill 	smbios_entry.rev = 0;
102a3c7cf62Sjmcneill 	smbios_entry.mjr = sh->majrev;
103a3c7cf62Sjmcneill 	smbios_entry.min = sh->minrev;
104a3c7cf62Sjmcneill 	smbios_entry.doc = 0;
105a3c7cf62Sjmcneill 	smbios_entry.count = sh->count;
106a3c7cf62Sjmcneill }
107a3c7cf62Sjmcneill 
108a3c7cf62Sjmcneill static void
smbios3_init(uint8_t * p)109a3c7cf62Sjmcneill smbios3_init(uint8_t *p)
110652b3556Sjmcneill {
111652b3556Sjmcneill 	const struct smb3hdr *sh = (const struct smb3hdr *)p;
112652b3556Sjmcneill 
113652b3556Sjmcneill 	smbios_entry.addr = (void *)(uintptr_t)sh->addr;
114652b3556Sjmcneill 	smbios_entry.len = sh->size;
115652b3556Sjmcneill 	smbios_entry.rev = sh->eprev;
116652b3556Sjmcneill 	smbios_entry.mjr = sh->majrev;
117652b3556Sjmcneill 	smbios_entry.min = sh->minrev;
118652b3556Sjmcneill 	smbios_entry.doc = sh->docrev;
119652b3556Sjmcneill 	smbios_entry.count = UINT16_MAX;
120652b3556Sjmcneill }
121652b3556Sjmcneill 
122a3c7cf62Sjmcneill void
smbios_init(uint8_t * p)123a3c7cf62Sjmcneill smbios_init(uint8_t *p)
124a3c7cf62Sjmcneill {
125a3c7cf62Sjmcneill 	if (memcmp(p, "_SM3_", 5) == 0) {
126a3c7cf62Sjmcneill 		smbios3_init(p);
127a3c7cf62Sjmcneill 	} else if (memcmp(p, "_SM_", 4) == 0) {
128a3c7cf62Sjmcneill 		smbios2_init(p);
129a3c7cf62Sjmcneill 	}
130a3c7cf62Sjmcneill }
131a3c7cf62Sjmcneill 
132652b3556Sjmcneill /*
133652b3556Sjmcneill  * smbios_find_table() takes a caller supplied smbios struct type and
134652b3556Sjmcneill  * a pointer to a handle (struct smbtable) returning one if the structure
135ba5c90c4Smsaitoh  * is successfully located and zero otherwise. Callers should take care
136652b3556Sjmcneill  * to initilize the cookie field of the smbtable structure to zero before
137652b3556Sjmcneill  * the first invocation of this function.
138652b3556Sjmcneill  * Multiple tables of the same type can be located by repeadtly calling
139652b3556Sjmcneill  * smbios_find_table with the same arguments.
140652b3556Sjmcneill  */
141652b3556Sjmcneill int
smbios_find_table(uint8_t type,struct smbtable * st)142652b3556Sjmcneill smbios_find_table(uint8_t type, struct smbtable *st)
143652b3556Sjmcneill {
144652b3556Sjmcneill 	uint8_t *va, *end;
145652b3556Sjmcneill 	struct smbtblhdr *hdr;
146652b3556Sjmcneill 	int ret = 0, tcount = 1;
147652b3556Sjmcneill 
148a3c7cf62Sjmcneill 	if (smbios_entry.addr == 0) {
149a3c7cf62Sjmcneill 		return 0;
150a3c7cf62Sjmcneill 	}
151a3c7cf62Sjmcneill 
152652b3556Sjmcneill 	va = smbios_entry.addr;
153652b3556Sjmcneill 	end = va + smbios_entry.len;
154652b3556Sjmcneill 
155652b3556Sjmcneill 	/*
156652b3556Sjmcneill 	 * The cookie field of the smtable structure is used to locate
157652b3556Sjmcneill 	 * multiple instances of a table of an arbitrary type. Following the
158*b5860281Sandvar 	 * successful location of a table, the type is encoded as bits 0:7 of
159652b3556Sjmcneill 	 * the cookie value, the offset in terms of the number of structures
160652b3556Sjmcneill 	 * preceding that referenced by the handle is encoded in bits 15:31.
161652b3556Sjmcneill 	 */
162652b3556Sjmcneill 	if ((st->cookie & 0xfff) == type && st->cookie >> 16) {
163652b3556Sjmcneill 		if ((uint8_t *)st->hdr >= va && (uint8_t *)st->hdr < end) {
164652b3556Sjmcneill 			hdr = st->hdr;
165652b3556Sjmcneill 			if (hdr->type == type) {
166652b3556Sjmcneill 				va = (uint8_t *)hdr + hdr->size;
167652b3556Sjmcneill 				for (; va + 1 < end; va++)
168652b3556Sjmcneill 					if (*va == 0 && *(va + 1) == 0)
169652b3556Sjmcneill 						break;
170652b3556Sjmcneill 				va+= 2;
171652b3556Sjmcneill 				tcount = st->cookie >> 16;
172652b3556Sjmcneill 			}
173652b3556Sjmcneill 		}
174652b3556Sjmcneill 	}
175652b3556Sjmcneill 	for (; va + sizeof(struct smbtblhdr) < end && tcount <=
176652b3556Sjmcneill 	    smbios_entry.count; tcount++) {
177652b3556Sjmcneill 		hdr = (struct smbtblhdr *)va;
178652b3556Sjmcneill 		if (hdr->type == type) {
179652b3556Sjmcneill 			ret = 1;
180652b3556Sjmcneill 			st->hdr = hdr;
181652b3556Sjmcneill 			st->tblhdr = va + sizeof(struct smbtblhdr);
182652b3556Sjmcneill 			st->cookie = (tcount + 1) << 16 | type;
183652b3556Sjmcneill 			break;
184652b3556Sjmcneill 		}
185652b3556Sjmcneill 		if (hdr->type == SMBIOS_TYPE_EOT)
186652b3556Sjmcneill 			break;
187652b3556Sjmcneill 		va+= hdr->size;
188652b3556Sjmcneill 		for (; va + 1 < end; va++)
189652b3556Sjmcneill 			if (*va == 0 && *(va + 1) == 0)
190652b3556Sjmcneill 				break;
191652b3556Sjmcneill 		va+=2;
192652b3556Sjmcneill 	}
193652b3556Sjmcneill 
194652b3556Sjmcneill 	return ret;
195652b3556Sjmcneill }
196652b3556Sjmcneill 
197652b3556Sjmcneill char *
smbios_get_string(struct smbtable * st,uint8_t indx,char * dest,size_t len)198652b3556Sjmcneill smbios_get_string(struct smbtable *st, uint8_t indx, char *dest, size_t len)
199652b3556Sjmcneill {
200652b3556Sjmcneill 	uint8_t *va, *end;
201652b3556Sjmcneill 	char *ret = NULL;
202652b3556Sjmcneill 	int i;
203652b3556Sjmcneill 
204a3c7cf62Sjmcneill 	if (smbios_entry.addr == 0) {
205a3c7cf62Sjmcneill 		return NULL;
206a3c7cf62Sjmcneill 	}
207a3c7cf62Sjmcneill 
208652b3556Sjmcneill 	va = (uint8_t *)st->hdr + st->hdr->size;
209652b3556Sjmcneill 	end = smbios_entry.addr + smbios_entry.len;
210652b3556Sjmcneill 	for (i = 1; va < end && i < indx && *va; i++)
211652b3556Sjmcneill 		while (*va++)
212652b3556Sjmcneill 			;
213652b3556Sjmcneill 	if (i == indx) {
214652b3556Sjmcneill 		if (va + len < end) {
215652b3556Sjmcneill 			ret = dest;
216652b3556Sjmcneill 			memcpy(ret, va, len);
217652b3556Sjmcneill 			ret[len - 1] = '\0';
218652b3556Sjmcneill 		}
219652b3556Sjmcneill 	}
220652b3556Sjmcneill 
221652b3556Sjmcneill 	return ret;
222652b3556Sjmcneill }
223