smbios.c revision 1.2 1 1.2 msaitoh /* $NetBSD: smbios.c,v 1.2 2019/12/27 09:45:27 msaitoh 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 msaitoh __KERNEL_RCSID(0, "$NetBSD: smbios.c,v 1.2 2019/12/27 09:45:27 msaitoh 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.1 jmcneill void
95 1.1 jmcneill smbios_init(uint8_t *p)
96 1.1 jmcneill {
97 1.1 jmcneill const struct smb3hdr *sh = (const struct smb3hdr *)p;
98 1.1 jmcneill
99 1.1 jmcneill smbios_entry.addr = (void *)(uintptr_t)sh->addr;
100 1.1 jmcneill smbios_entry.len = sh->size;
101 1.1 jmcneill smbios_entry.rev = sh->eprev;
102 1.1 jmcneill smbios_entry.mjr = sh->majrev;
103 1.1 jmcneill smbios_entry.min = sh->minrev;
104 1.1 jmcneill smbios_entry.doc = sh->docrev;
105 1.1 jmcneill smbios_entry.count = UINT16_MAX;
106 1.1 jmcneill }
107 1.1 jmcneill
108 1.1 jmcneill /*
109 1.1 jmcneill * smbios_find_table() takes a caller supplied smbios struct type and
110 1.1 jmcneill * a pointer to a handle (struct smbtable) returning one if the structure
111 1.2 msaitoh * is successfully located and zero otherwise. Callers should take care
112 1.1 jmcneill * to initilize the cookie field of the smbtable structure to zero before
113 1.1 jmcneill * the first invocation of this function.
114 1.1 jmcneill * Multiple tables of the same type can be located by repeadtly calling
115 1.1 jmcneill * smbios_find_table with the same arguments.
116 1.1 jmcneill */
117 1.1 jmcneill int
118 1.1 jmcneill smbios_find_table(uint8_t type, struct smbtable *st)
119 1.1 jmcneill {
120 1.1 jmcneill uint8_t *va, *end;
121 1.1 jmcneill struct smbtblhdr *hdr;
122 1.1 jmcneill int ret = 0, tcount = 1;
123 1.1 jmcneill
124 1.1 jmcneill va = smbios_entry.addr;
125 1.1 jmcneill end = va + smbios_entry.len;
126 1.1 jmcneill
127 1.1 jmcneill /*
128 1.1 jmcneill * The cookie field of the smtable structure is used to locate
129 1.1 jmcneill * multiple instances of a table of an arbitrary type. Following the
130 1.1 jmcneill * sucessful location of a table, the type is encoded as bits 0:7 of
131 1.1 jmcneill * the cookie value, the offset in terms of the number of structures
132 1.1 jmcneill * preceding that referenced by the handle is encoded in bits 15:31.
133 1.1 jmcneill */
134 1.1 jmcneill if ((st->cookie & 0xfff) == type && st->cookie >> 16) {
135 1.1 jmcneill if ((uint8_t *)st->hdr >= va && (uint8_t *)st->hdr < end) {
136 1.1 jmcneill hdr = st->hdr;
137 1.1 jmcneill if (hdr->type == type) {
138 1.1 jmcneill va = (uint8_t *)hdr + hdr->size;
139 1.1 jmcneill for (; va + 1 < end; va++)
140 1.1 jmcneill if (*va == 0 && *(va + 1) == 0)
141 1.1 jmcneill break;
142 1.1 jmcneill va+= 2;
143 1.1 jmcneill tcount = st->cookie >> 16;
144 1.1 jmcneill }
145 1.1 jmcneill }
146 1.1 jmcneill }
147 1.1 jmcneill for (; va + sizeof(struct smbtblhdr) < end && tcount <=
148 1.1 jmcneill smbios_entry.count; tcount++) {
149 1.1 jmcneill hdr = (struct smbtblhdr *)va;
150 1.1 jmcneill if (hdr->type == type) {
151 1.1 jmcneill ret = 1;
152 1.1 jmcneill st->hdr = hdr;
153 1.1 jmcneill st->tblhdr = va + sizeof(struct smbtblhdr);
154 1.1 jmcneill st->cookie = (tcount + 1) << 16 | type;
155 1.1 jmcneill break;
156 1.1 jmcneill }
157 1.1 jmcneill if (hdr->type == SMBIOS_TYPE_EOT)
158 1.1 jmcneill break;
159 1.1 jmcneill va+= hdr->size;
160 1.1 jmcneill for (; va + 1 < end; va++)
161 1.1 jmcneill if (*va == 0 && *(va + 1) == 0)
162 1.1 jmcneill break;
163 1.1 jmcneill va+=2;
164 1.1 jmcneill }
165 1.1 jmcneill
166 1.1 jmcneill return ret;
167 1.1 jmcneill }
168 1.1 jmcneill
169 1.1 jmcneill char *
170 1.1 jmcneill smbios_get_string(struct smbtable *st, uint8_t indx, char *dest, size_t len)
171 1.1 jmcneill {
172 1.1 jmcneill uint8_t *va, *end;
173 1.1 jmcneill char *ret = NULL;
174 1.1 jmcneill int i;
175 1.1 jmcneill
176 1.1 jmcneill va = (uint8_t *)st->hdr + st->hdr->size;
177 1.1 jmcneill end = smbios_entry.addr + smbios_entry.len;
178 1.1 jmcneill for (i = 1; va < end && i < indx && *va; i++)
179 1.1 jmcneill while (*va++)
180 1.1 jmcneill ;
181 1.1 jmcneill if (i == indx) {
182 1.1 jmcneill if (va + len < end) {
183 1.1 jmcneill ret = dest;
184 1.1 jmcneill memcpy(ret, va, len);
185 1.1 jmcneill ret[len - 1] = '\0';
186 1.1 jmcneill }
187 1.1 jmcneill }
188 1.1 jmcneill
189 1.1 jmcneill return ret;
190 1.1 jmcneill }
191