FreeBSD/Linux Kernel Cross Reference
sys/x86/bios/vpd.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /*
33 * VPD decoder for IBM systems (Thinkpads)
34 * http://www-1.ibm.com/support/docview.wss?uid=psg1MIGR-45120
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42
43 #include <sys/module.h>
44 #include <sys/bus.h>
45
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <sys/rman.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52 #include <vm/pmap.h>
53 #include <machine/md_var.h>
54 #include <machine/pc/bios.h>
55
56 /*
57 * Vital Product Data
58 */
59 struct vpd {
60 u_int16_t Header; /* 0x55AA */
61 u_int8_t Signature[3]; /* Always 'VPD' */
62 u_int8_t Length; /* Sructure Length */
63
64 u_int8_t Reserved[7]; /* Reserved */
65
66 u_int8_t BuildID[9]; /* BIOS Build ID */
67 u_int8_t BoxSerial[7]; /* Box Serial Number */
68 u_int8_t PlanarSerial[11]; /* Motherboard Serial Number */
69 u_int8_t MachType[7]; /* Machine Type/Model */
70 u_int8_t Checksum; /* Checksum */
71 } __packed;
72
73 struct vpd_softc {
74 device_t dev;
75 struct resource * res;
76 int rid;
77
78 struct vpd * vpd;
79
80 struct sysctl_ctx_list ctx;
81
82 char BuildID[10];
83 char BoxSerial[8];
84 char PlanarSerial[12];
85 char MachineType[5];
86 char MachineModel[4];
87 };
88
89 #define VPD_START 0xf0000
90 #define VPD_STEP 0x10
91 #define VPD_OFF 2
92 #define VPD_LEN 3
93 #define VPD_SIG "VPD"
94
95 #define RES2VPD(res) ((struct vpd *)rman_get_virtual(res))
96 #define ADDR2VPD(addr) ((struct vpd *)BIOS_PADDRTOVADDR(addr))
97
98 static void vpd_identify (driver_t *, device_t);
99 static int vpd_probe (device_t);
100 static int vpd_attach (device_t);
101 static int vpd_detach (device_t);
102 static int vpd_modevent (module_t, int, void *);
103
104 static int vpd_cksum (struct vpd *);
105
106 static SYSCTL_NODE(_hw, OID_AUTO, vpd, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
107 NULL);
108 static SYSCTL_NODE(_hw_vpd, OID_AUTO, machine,
109 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
110 NULL);
111 static SYSCTL_NODE(_hw_vpd_machine, OID_AUTO, type,
112 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
113 NULL);
114 static SYSCTL_NODE(_hw_vpd_machine, OID_AUTO, model,
115 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
116 NULL);
117 static SYSCTL_NODE(_hw_vpd, OID_AUTO, build_id,
118 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
119 NULL);
120 static SYSCTL_NODE(_hw_vpd, OID_AUTO, serial,
121 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
122 NULL);
123 static SYSCTL_NODE(_hw_vpd_serial, OID_AUTO, box,
124 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
125 NULL);
126 static SYSCTL_NODE(_hw_vpd_serial, OID_AUTO, planar,
127 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
128 NULL);
129
130 static void
131 vpd_identify (driver_t *driver, device_t parent)
132 {
133 device_t child;
134 u_int32_t addr;
135 int length;
136 int rid;
137
138 if (!device_is_alive(parent))
139 return;
140
141 addr = bios_sigsearch(VPD_START, VPD_SIG, VPD_LEN, VPD_STEP, VPD_OFF);
142 if (addr != 0) {
143 rid = 0;
144 length = ADDR2VPD(addr)->Length;
145
146 child = BUS_ADD_CHILD(parent, 5, "vpd", -1);
147 device_set_driver(child, driver);
148 bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
149 device_set_desc(child, "Vital Product Data Area");
150 }
151
152 return;
153 }
154
155 static int
156 vpd_probe (device_t dev)
157 {
158 struct resource *res;
159 int rid;
160 int error;
161
162 error = 0;
163 rid = 0;
164 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
165 if (res == NULL) {
166 device_printf(dev, "Unable to allocate memory resource.\n");
167 error = ENOMEM;
168 goto bad;
169 }
170
171 if (vpd_cksum(RES2VPD(res)))
172 device_printf(dev, "VPD checksum failed. BIOS update may be required.\n");
173
174 bad:
175 if (res)
176 bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
177 return (error);
178 }
179
180 static int
181 vpd_attach (device_t dev)
182 {
183 struct vpd_softc *sc;
184 char unit[4];
185 int error;
186
187 sc = device_get_softc(dev);
188 error = 0;
189
190 sc->dev = dev;
191 sc->rid = 0;
192 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
193 RF_ACTIVE);
194 if (sc->res == NULL) {
195 device_printf(dev, "Unable to allocate memory resource.\n");
196 error = ENOMEM;
197 goto bad;
198 }
199 sc->vpd = RES2VPD(sc->res);
200
201 snprintf(unit, sizeof(unit), "%d", device_get_unit(sc->dev));
202 snprintf(sc->MachineType, 5, "%.4s", sc->vpd->MachType);
203 snprintf(sc->MachineModel, 4, "%.3s", sc->vpd->MachType+4);
204 snprintf(sc->BuildID, 10, "%.9s", sc->vpd->BuildID);
205 snprintf(sc->BoxSerial, 8, "%.7s", sc->vpd->BoxSerial);
206 snprintf(sc->PlanarSerial, 12, "%.11s", sc->vpd->PlanarSerial);
207
208 sysctl_ctx_init(&sc->ctx);
209 SYSCTL_ADD_STRING(&sc->ctx,
210 SYSCTL_STATIC_CHILDREN(_hw_vpd_machine_type), OID_AUTO,
211 unit, CTLFLAG_RD, sc->MachineType, 0, NULL);
212 SYSCTL_ADD_STRING(&sc->ctx,
213 SYSCTL_STATIC_CHILDREN(_hw_vpd_machine_model), OID_AUTO,
214 unit, CTLFLAG_RD, sc->MachineModel, 0, NULL);
215 SYSCTL_ADD_STRING(&sc->ctx,
216 SYSCTL_STATIC_CHILDREN(_hw_vpd_build_id), OID_AUTO,
217 unit, CTLFLAG_RD, sc->BuildID, 0, NULL);
218 SYSCTL_ADD_STRING(&sc->ctx,
219 SYSCTL_STATIC_CHILDREN(_hw_vpd_serial_box), OID_AUTO,
220 unit, CTLFLAG_RD, sc->BoxSerial, 0, NULL);
221 SYSCTL_ADD_STRING(&sc->ctx,
222 SYSCTL_STATIC_CHILDREN(_hw_vpd_serial_planar), OID_AUTO,
223 unit, CTLFLAG_RD, sc->PlanarSerial, 0, NULL);
224
225 device_printf(dev, "Machine Type: %.4s, Model: %.3s, Build ID: %.9s\n",
226 sc->MachineType, sc->MachineModel, sc->BuildID);
227 device_printf(dev, "Box Serial: %.7s, Planar Serial: %.11s\n",
228 sc->BoxSerial, sc->PlanarSerial);
229
230 return (0);
231 bad:
232 if (sc->res)
233 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
234 return (error);
235 }
236
237 static int
238 vpd_detach (device_t dev)
239 {
240 struct vpd_softc *sc;
241
242 sc = device_get_softc(dev);
243
244 if (sc->res)
245 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
246
247 sysctl_ctx_free(&sc->ctx);
248
249 return (0);
250 }
251
252 static int
253 vpd_modevent (module_t mod, int what, void *arg)
254 {
255 device_t * devs;
256 int count;
257 int i;
258
259 switch (what) {
260 case MOD_LOAD:
261 break;
262 case MOD_UNLOAD:
263 devclass_get_devices(devclass_find("vpd"), &devs, &count);
264 for (i = 0; i < count; i++) {
265 device_delete_child(device_get_parent(devs[i]), devs[i]);
266 }
267 break;
268 default:
269 break;
270 }
271
272 return (0);
273 }
274
275 static device_method_t vpd_methods[] = {
276 /* Device interface */
277 DEVMETHOD(device_identify, vpd_identify),
278 DEVMETHOD(device_probe, vpd_probe),
279 DEVMETHOD(device_attach, vpd_attach),
280 DEVMETHOD(device_detach, vpd_detach),
281 { 0, 0 }
282 };
283
284 static driver_t vpd_driver = {
285 "vpd",
286 vpd_methods,
287 sizeof(struct vpd_softc),
288 };
289
290 DRIVER_MODULE(vpd, nexus, vpd_driver, vpd_modevent, 0);
291 MODULE_VERSION(vpd, 1);
292
293 /*
294 * Perform a checksum over the VPD structure, starting with
295 * the BuildID. (Jean Delvare <khali@linux-fr.org>)
296 */
297 static int
298 vpd_cksum (struct vpd *v)
299 {
300 u_int8_t *ptr;
301 u_int8_t cksum;
302 int i;
303
304 ptr = (u_int8_t *)v;
305 cksum = 0;
306 for (i = offsetof(struct vpd, BuildID); i < v->Length ; i++)
307 cksum += ptr[i];
308 return (cksum);
309 }
Cache object: 9985c0d36f2849e90e82733232e1d335
|