xref: /freebsd/sys/compat/linuxkpi/common/src/linux_dmi.c (revision 809922b01004daf627ad4b8d92c7f98eb579043c)
1 /*-
2  * Copyright (c) 2020 The FreeBSD Foundation
3  *
4  * This software was developed by Emmanuel Vadot under sponsorship
5  * from the FreeBSD Foundation.
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  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 
35 #include <linux/dmi.h>
36 
37 static char *dmi_data[DMI_STRING_MAX];
38 
39 static void
40 linux_dmi_preload(void *arg)
41 {
42 
43 	dmi_data[DMI_BIOS_VENDOR] = kern_getenv("smbios.bios.vendor");
44 	dmi_data[DMI_BIOS_VERSION] = kern_getenv("smbios.bios.version");
45 	dmi_data[DMI_BIOS_DATE] = kern_getenv("smbios.bios.reldate");
46 	dmi_data[DMI_SYS_VENDOR] = kern_getenv("smbios.system.maker");
47 	dmi_data[DMI_PRODUCT_NAME] = kern_getenv("smbios.system.product");
48 	dmi_data[DMI_PRODUCT_VERSION] = kern_getenv("smbios.system.version");
49 	dmi_data[DMI_PRODUCT_SERIAL] = kern_getenv("smbios.system.serial");
50 	dmi_data[DMI_PRODUCT_UUID] = kern_getenv("smbios.system.uuid");
51 	dmi_data[DMI_BOARD_VENDOR] = kern_getenv("smbios.planar.maker");
52 	dmi_data[DMI_BOARD_NAME] = kern_getenv("smbios.planar.product");
53 	dmi_data[DMI_BOARD_VERSION] = kern_getenv("smbios.planar.version");
54 	dmi_data[DMI_BOARD_SERIAL] = kern_getenv("smbios.planar.serial");
55 	dmi_data[DMI_BOARD_ASSET_TAG] = kern_getenv("smbios.planar.tag");
56 	dmi_data[DMI_CHASSIS_VENDOR] = kern_getenv("smbios.chassis.maker");
57 	dmi_data[DMI_CHASSIS_TYPE] = kern_getenv("smbios.chassis.type");
58 	dmi_data[DMI_CHASSIS_VERSION] = kern_getenv("smbios.chassis.version");
59 	dmi_data[DMI_CHASSIS_SERIAL] = kern_getenv("smbios.chassis.serial");
60 	dmi_data[DMI_CHASSIS_ASSET_TAG] = kern_getenv("smbios.chassis.tag");
61 }
62 SYSINIT(linux_dmi_preload, SI_SUB_DRIVERS, SI_ORDER_ANY, linux_dmi_preload, NULL);
63 
64 /* Match a system against a field */
65 bool
66 linux_dmi_match(enum dmi_field f, const char *str)
67 {
68 
69 	if (f < DMI_STRING_MAX &&
70 	    dmi_data[f] != NULL &&
71 	    strcmp(dmi_data[f], str) == 0)
72 		return(true);
73 	return (false);
74 }
75 
76 /* Match a system against the struct, all matches must be ok */
77 static bool
78 linux_dmi_matches(const struct dmi_system_id *dsi)
79 {
80 	int i;
81 
82 	for (i = 0; i < nitems(dsi->matches); i++) {
83 		if (dsi->matches[i].slot == DMI_NONE)
84 			break;
85 		if (dsi->matches[i].exact_match) {
86 			return (dmi_match(dsi->matches[i].slot,
87 			    dsi->matches[i].substr));
88 		} else {
89 			return (strstr(dmi_data[dsi->matches[i].slot],
90 				    dsi->matches[i].substr) != NULL);
91 		}
92 	}
93 
94 	return (true);
95 }
96 
97 /* Return the string matching the field */
98 const char *
99 linux_dmi_get_system_info(int field)
100 {
101 
102 	if (field < DMI_STRING_MAX)
103 		return (dmi_data[field]);
104 	return (NULL);
105 }
106 
107 /*
108  * Match a system against the structs list
109  * If a match is found return the corresponding structure.
110  */
111 const struct dmi_system_id *
112 linux_dmi_first_match(const struct dmi_system_id *list)
113 {
114 	const struct dmi_system_id *dsi;
115 
116 	for (dsi = list; dsi->matches[0].slot != 0; dsi++) {
117 		if (linux_dmi_matches(dsi))
118 			return (dsi);
119 	}
120 
121 	return (NULL);
122 }
123 
124 /*
125  * Match a system against the structs list
126  * For each match call the callback with the corresponding data
127  * Return the number of matches.
128  */
129 int
130 linux_dmi_check_system(const struct dmi_system_id *sysid)
131 {
132 	const struct dmi_system_id *dsi;
133 	int matches = 0;
134 
135 	for (dsi = sysid; dsi->matches[0].slot != 0; dsi++) {
136 		if (linux_dmi_matches(dsi)) {
137 			matches++;
138 			if (dsi->callback && dsi->callback(dsi))
139 				break;
140 		}
141 	}
142 
143 	return (matches);
144 }
145