xref: /freebsd/sys/x86/bios/vpd.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 /*
31  * VPD decoder for IBM systems (Thinkpads)
32  * http://www-1.ibm.com/support/docview.wss?uid=psg1MIGR-45120
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 
41 #include <sys/module.h>
42 #include <sys/bus.h>
43 
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <sys/rman.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51 #include <machine/md_var.h>
52 #include <machine/pc/bios.h>
53 
54 /*
55  * Vital Product Data
56  */
57 struct vpd {
58 	u_int16_t	Header;			/* 0x55AA */
59 	u_int8_t	Signature[3];		/* Always 'VPD' */
60 	u_int8_t	Length;			/* Sructure Length */
61 
62 	u_int8_t	Reserved[7];		/* Reserved */
63 
64 	u_int8_t	BuildID[9];		/* BIOS Build ID */
65 	u_int8_t	BoxSerial[7];		/* Box Serial Number */
66 	u_int8_t	PlanarSerial[11];	/* Motherboard Serial Number */
67 	u_int8_t	MachType[7];		/* Machine Type/Model */
68 	u_int8_t	Checksum;		/* Checksum */
69 } __packed;
70 
71 struct vpd_softc {
72 	device_t		dev;
73 	struct resource *	res;
74 	int			rid;
75 
76 	struct vpd *		vpd;
77 
78 	struct sysctl_ctx_list  ctx;
79 
80 	char		BuildID[10];
81 	char		BoxSerial[8];
82 	char		PlanarSerial[12];
83 	char		MachineType[5];
84 	char		MachineModel[4];
85 };
86 
87 #define	VPD_START	0xf0000
88 #define	VPD_STEP	0x10
89 #define	VPD_OFF		2
90 #define	VPD_LEN		3
91 #define	VPD_SIG		"VPD"
92 
93 #define	RES2VPD(res)	((struct vpd *)rman_get_virtual(res))
94 #define	ADDR2VPD(addr)	((struct vpd *)BIOS_PADDRTOVADDR(addr))
95 
96 static void	vpd_identify	(driver_t *, device_t);
97 static int	vpd_probe	(device_t);
98 static int	vpd_attach	(device_t);
99 static int	vpd_detach	(device_t);
100 static int	vpd_modevent	(module_t, int, void *);
101 
102 static int	vpd_cksum	(struct vpd *);
103 
104 static SYSCTL_NODE(_hw, OID_AUTO, vpd, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
105     NULL);
106 static SYSCTL_NODE(_hw_vpd, OID_AUTO, machine,
107     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
108     NULL);
109 static SYSCTL_NODE(_hw_vpd_machine, OID_AUTO, type,
110     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
111     NULL);
112 static SYSCTL_NODE(_hw_vpd_machine, OID_AUTO, model,
113     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
114     NULL);
115 static SYSCTL_NODE(_hw_vpd, OID_AUTO, build_id,
116     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
117     NULL);
118 static SYSCTL_NODE(_hw_vpd, OID_AUTO, serial,
119     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
120     NULL);
121 static SYSCTL_NODE(_hw_vpd_serial, OID_AUTO, box,
122     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
123     NULL);
124 static SYSCTL_NODE(_hw_vpd_serial, OID_AUTO, planar,
125     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
126     NULL);
127 
128 static void
129 vpd_identify (driver_t *driver, device_t parent)
130 {
131 	device_t child;
132 	u_int32_t addr;
133 	int length;
134 	int rid;
135 
136 	if (!device_is_alive(parent))
137 		return;
138 
139 	addr = bios_sigsearch(VPD_START, VPD_SIG, VPD_LEN, VPD_STEP, VPD_OFF);
140 	if (addr != 0) {
141 		rid = 0;
142 		length = ADDR2VPD(addr)->Length;
143 
144 		child = BUS_ADD_CHILD(parent, 5, "vpd", -1);
145 		device_set_driver(child, driver);
146 		bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
147 		device_set_desc(child, "Vital Product Data Area");
148 	}
149 
150 	return;
151 }
152 
153 static int
154 vpd_probe (device_t dev)
155 {
156 	struct resource *res;
157 	int rid;
158 	int error;
159 
160 	error = 0;
161 	rid = 0;
162 	res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
163 	if (res == NULL) {
164 		device_printf(dev, "Unable to allocate memory resource.\n");
165 		error = ENOMEM;
166 		goto bad;
167 	}
168 
169 	if (vpd_cksum(RES2VPD(res)))
170 		device_printf(dev, "VPD checksum failed.  BIOS update may be required.\n");
171 
172 bad:
173 	if (res)
174 		bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
175 	return (error);
176 }
177 
178 static int
179 vpd_attach (device_t dev)
180 {
181 	struct vpd_softc *sc;
182 	char unit[4];
183 	int error;
184 
185 	sc = device_get_softc(dev);
186 	error = 0;
187 
188 	sc->dev = dev;
189 	sc->rid = 0;
190 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
191 		RF_ACTIVE);
192 	if (sc->res == NULL) {
193 		device_printf(dev, "Unable to allocate memory resource.\n");
194 		error = ENOMEM;
195 		goto bad;
196 	}
197 	sc->vpd = RES2VPD(sc->res);
198 
199 	snprintf(unit, sizeof(unit), "%d", device_get_unit(sc->dev));
200 	snprintf(sc->MachineType, 5, "%.4s", sc->vpd->MachType);
201 	snprintf(sc->MachineModel, 4, "%.3s", sc->vpd->MachType+4);
202 	snprintf(sc->BuildID, 10, "%.9s", sc->vpd->BuildID);
203 	snprintf(sc->BoxSerial, 8, "%.7s", sc->vpd->BoxSerial);
204 	snprintf(sc->PlanarSerial, 12, "%.11s", sc->vpd->PlanarSerial);
205 
206 	sysctl_ctx_init(&sc->ctx);
207 	SYSCTL_ADD_STRING(&sc->ctx,
208 		SYSCTL_STATIC_CHILDREN(_hw_vpd_machine_type), OID_AUTO,
209 		unit, CTLFLAG_RD, sc->MachineType, 0, NULL);
210 	SYSCTL_ADD_STRING(&sc->ctx,
211 		SYSCTL_STATIC_CHILDREN(_hw_vpd_machine_model), OID_AUTO,
212 		unit, CTLFLAG_RD, sc->MachineModel, 0, NULL);
213 	SYSCTL_ADD_STRING(&sc->ctx,
214 		SYSCTL_STATIC_CHILDREN(_hw_vpd_build_id), OID_AUTO,
215 		unit, CTLFLAG_RD, sc->BuildID, 0, NULL);
216 	SYSCTL_ADD_STRING(&sc->ctx,
217 		SYSCTL_STATIC_CHILDREN(_hw_vpd_serial_box), OID_AUTO,
218 		unit, CTLFLAG_RD, sc->BoxSerial, 0, NULL);
219 	SYSCTL_ADD_STRING(&sc->ctx,
220 		SYSCTL_STATIC_CHILDREN(_hw_vpd_serial_planar), OID_AUTO,
221 		unit, CTLFLAG_RD, sc->PlanarSerial, 0, NULL);
222 
223 	device_printf(dev, "Machine Type: %.4s, Model: %.3s, Build ID: %.9s\n",
224 		sc->MachineType, sc->MachineModel, sc->BuildID);
225 	device_printf(dev, "Box Serial: %.7s, Planar Serial: %.11s\n",
226 		sc->BoxSerial, sc->PlanarSerial);
227 
228 	return (0);
229 bad:
230 	if (sc->res)
231 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
232 	return (error);
233 }
234 
235 static int
236 vpd_detach (device_t dev)
237 {
238 	struct vpd_softc *sc;
239 
240 	sc = device_get_softc(dev);
241 
242 	if (sc->res)
243 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
244 
245 	sysctl_ctx_free(&sc->ctx);
246 
247 	return (0);
248 }
249 
250 static int
251 vpd_modevent (module_t mod, int what, void *arg)
252 {
253 	device_t *	devs;
254 	int		count;
255 	int		i;
256 
257 	switch (what) {
258 	case MOD_LOAD:
259 		break;
260 	case MOD_UNLOAD:
261 		devclass_get_devices(devclass_find("vpd"), &devs, &count);
262 		for (i = 0; i < count; i++) {
263 			device_delete_child(device_get_parent(devs[i]), devs[i]);
264 		}
265 		break;
266 	default:
267 		break;
268 	}
269 
270 	return (0);
271 }
272 
273 static device_method_t vpd_methods[] = {
274 	/* Device interface */
275 	DEVMETHOD(device_identify,      vpd_identify),
276 	DEVMETHOD(device_probe,         vpd_probe),
277 	DEVMETHOD(device_attach,        vpd_attach),
278 	DEVMETHOD(device_detach,        vpd_detach),
279 	{ 0, 0 }
280 };
281 
282 static driver_t vpd_driver = {
283 	"vpd",
284 	vpd_methods,
285 	sizeof(struct vpd_softc),
286 };
287 
288 DRIVER_MODULE(vpd, nexus, vpd_driver, vpd_modevent, 0);
289 MODULE_VERSION(vpd, 1);
290 
291 /*
292  * Perform a checksum over the VPD structure, starting with
293  * the BuildID.  (Jean Delvare <khali@linux-fr.org>)
294  */
295 static int
296 vpd_cksum (struct vpd *v)
297 {
298 	u_int8_t *ptr;
299 	u_int8_t cksum;
300 	int i;
301 
302 	ptr = (u_int8_t *)v;
303 	cksum = 0;
304 	for (i = offsetof(struct vpd, BuildID); i < v->Length ; i++)
305 		cksum += ptr[i];
306 	return (cksum);
307 }
308