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