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