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 enum dmi_field slot;
81 int i;
82
83 for (i = 0; i < nitems(dsi->matches); i++) {
84 slot = dsi->matches[i].slot;
85 if (slot == DMI_NONE)
86 break;
87 if (slot >= DMI_STRING_MAX ||
88 dmi_data[slot] == NULL)
89 return (false);
90 if (dsi->matches[i].exact_match) {
91 if (dmi_match(slot, dsi->matches[i].substr))
92 continue;
93 } else if (strstr(dmi_data[slot],
94 dsi->matches[i].substr) != NULL) {
95 continue;
96 }
97 return (false);
98 }
99 return (true);
100 }
101
102 /* Return the string matching the field */
103 const char *
104 linux_dmi_get_system_info(int field)
105 {
106
107 if (field < DMI_STRING_MAX)
108 return (dmi_data[field]);
109 return (NULL);
110 }
111
112 /*
113 * Match a system against the structs list
114 * If a match is found return the corresponding structure.
115 */
116 const struct dmi_system_id *
117 linux_dmi_first_match(const struct dmi_system_id *list)
118 {
119 const struct dmi_system_id *dsi;
120
121 for (dsi = list; dsi->matches[0].slot != 0; dsi++) {
122 if (linux_dmi_matches(dsi))
123 return (dsi);
124 }
125
126 return (NULL);
127 }
128
129 /*
130 * Match a system against the structs list
131 * For each match call the callback with the corresponding data
132 * Return the number of matches.
133 */
134 int
135 linux_dmi_check_system(const struct dmi_system_id *sysid)
136 {
137 const struct dmi_system_id *dsi;
138 int matches = 0;
139
140 for (dsi = sysid; dsi->matches[0].slot != 0; dsi++) {
141 if (linux_dmi_matches(dsi)) {
142 matches++;
143 if (dsi->callback && dsi->callback(dsi))
144 break;
145 }
146 }
147
148 return (matches);
149 }
Cache object: 37e37fe0090af5c7e8cae967c8f79324
|