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