1 /* ----------------------------------------------------------------------- *
3 * Copyright 2011 Intel Corporation; author: H. Peter Anvin
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
29 * Search DMI information for specific data or strings
34 #include <sys/bitops.h>
36 #include <syslinux/sysappend.h>
55 struct smbios_header
{
65 struct dmi_header dmi
;
68 static const struct dmi_header
*dmi
;
70 static uint8_t checksum(const void *buf
, size_t len
)
72 const uint8_t *p
= buf
;
81 static bool is_old_dmi(size_t dptr
)
83 const struct dmi_header
*dmi
= (void *)dptr
;
85 return !memcmp(dmi
->signature
, "_DMI_", 5) &&
90 static bool is_smbios(size_t dptr
)
92 const struct smbios_header
*smb
= (void *)dptr
;
94 return !memcmp(smb
->signature
, "_SM_", 4) &&
95 !checksum(smb
, smb
->len
) &&
100 * Find the root structure
102 static void dmi_find_header(void)
106 /* Search for _SM_ or _DMI_ structure */
107 for (dptr
= 0xf0000 ; dptr
< 0x100000 ; dptr
+= 16) {
108 if (is_smbios(dptr
)) {
109 dmi
= (const struct dmi_header
*)(dptr
+ 16);
111 } else if (is_old_dmi(dptr
)) {
112 dmi
= (const struct dmi_header
*)dptr
;
119 * Return a specific data element in a specific table, and verify
120 * that it is within the bounds of the table.
122 static const void *dmi_find_data(uint8_t type
, uint8_t base
, uint8_t length
)
124 const struct dmi_table
*table
;
126 unsigned int tblcount
;
137 tblcount
= dmi
->nstruc
;
139 while (offset
+6 <= dmi
->tbllen
&& tblcount
--) {
140 table
= (const struct dmi_table
*)(dmi
->tbladdr
+ offset
);
142 if (table
->type
== 127) /* End of table */
145 if (table
->length
< sizeof *table
)
146 break; /* Invalid length */
148 offset
+= table
->length
;
150 if (table
->type
== type
&& end
<= table
->length
)
151 return (const char *)table
+ base
;
153 /* Search for a double NUL terminating the string table */
154 while (offset
+2 <= dmi
->tbllen
&&
155 *(const uint16_t *)(dmi
->tbladdr
+ offset
) != 0)
165 * Return a specific string in a specific table.
167 static const char *dmi_find_string(uint8_t type
, uint8_t base
)
169 const struct dmi_table
*table
;
171 unsigned int tblcount
;
180 tblcount
= dmi
->nstruc
;
182 while (offset
+6 <= dmi
->tbllen
&& tblcount
--) {
183 table
= (const struct dmi_table
*)(dmi
->tbladdr
+ offset
);
185 if (table
->type
== 127) /* End of table */
188 if (table
->length
< sizeof *table
)
189 break; /* Invalid length */
191 offset
+= table
->length
;
193 if (table
->type
== type
&& base
< table
->length
) {
194 uint8_t index
= ((const uint8_t *)table
)[base
];
195 const char *p
= (const char *)table
+ table
->length
;
200 return NULL
; /* String not present */
207 if (offset
++ >= dmi
->tbllen
)
213 /* Make sure the string is null-terminated */
216 if (offset
++ >= dmi
->tbllen
)
223 /* Search for a double NUL terminating the string table */
224 while (offset
+2 <= dmi
->tbllen
&&
225 *(const uint16_t *)(dmi
->tbladdr
+ offset
) != 0)
234 struct sysappend_dmi_strings
{
236 enum syslinux_sysappend sa
;
241 static const struct sysappend_dmi_strings dmi_strings
[] = {
242 { "SYSVENDOR=", SYSAPPEND_SYSVENDOR
, 1, 0x04 },
243 { "SYSPRODUCT=", SYSAPPEND_SYSPRODUCT
, 1, 0x05 },
244 { "SYSVERSION=", SYSAPPEND_SYSVERSION
, 1, 0x06 },
245 { "SYSSERIAL=", SYSAPPEND_SYSSERIAL
, 1, 0x07 },
246 { "SYSSKU=", SYSAPPEND_SYSSKU
, 1, 0x19 },
247 { "SYSFAMILY=", SYSAPPEND_SYSFAMILY
, 1, 0x1a },
248 { "MBVENDOR=", SYSAPPEND_MBVENDOR
, 2, 0x04 },
249 { "MBPRODUCT=", SYSAPPEND_MBPRODUCT
, 2, 0x05 },
250 { "MBVERSION=", SYSAPPEND_MBVERSION
, 2, 0x06 },
251 { "MBSERIAL=", SYSAPPEND_MBSERIAL
, 2, 0x07 },
252 { "MBASSET=", SYSAPPEND_MBASSET
, 2, 0x08 },
253 { "BIOSVENDOR=", SYSAPPEND_BIOSVENDOR
, 0, 0x04 },
254 { "BIOSVERSION=", SYSAPPEND_BIOSVERSION
, 0, 0x05 },
259 * Install the string in the string table, if nonempty, after
260 * removing leading and trailing whitespace.
262 static bool is_ctl_or_whitespace(char c
)
264 return (c
<= ' ' || c
== '\x7f');
267 static const char *dmi_install_string(const char *pfx
, const char *str
)
276 while (*str
&& is_ctl_or_whitespace(*str
))
284 if (!is_ctl_or_whitespace(*p
))
289 pfxlen
= strlen(pfx
);
290 q
= nstr
= malloc(pfxlen
+ (ep
-str
) + 1);
293 memcpy(q
, pfx
, pfxlen
);
295 memcpy(q
, str
, ep
-str
);
302 static void sysappend_set_sysff(const uint8_t *type
)
304 static char sysff_str
[] = "SYSFF=000";
309 sprintf(sysff_str
+6, "%u", *type
& 0x7f);
310 sysappend_strings
[SYSAPPEND_SYSFF
] = sysff_str
;
318 static void sysappend_set_cpu(void)
320 static char cpu_str
[6+6] = "CPU=";
321 char *p
= cpu_str
+ 4;
322 static const struct cpuflag cpuflags
[] = {
323 { 0*32+ 6, 'P' }, /* PAE */
324 { 1*32+ 5, 'V' }, /* VMX */
325 { 1*32+ 6, 'T' }, /* SMX (TXT) */
326 { 2*32+20, 'X' }, /* XD/NX */
327 { 2*32+29, 'L' }, /* Long mode (x86-64) */
328 { 3*32+ 2, 'S' }, /* SVM */
331 const struct cpuflag
*cf
;
333 /* Not technically from DMI, but it fit here... */
335 if (!cpu_has_eflag(EFLAGS_ID
)) {
337 *p
++ = cpu_has_eflag(EFLAGS_AC
) ? '4' : '3';
339 uint32_t flags
[4], eax
, ebx
, family
;
342 cpuid(1, &eax
, &ebx
, &flags
[1], &flags
[0]);
343 family
= (eax
& 0x0ff00f00) >> 8;
344 *p
++ = family
>= 6 ? '6' : family
+ '0';
346 ext_level
= cpuid_eax(0x80000000);
347 if (ext_level
>= 0x80000001 && ext_level
<= 0x8000ffff) {
348 cpuid(0x80000001, &eax
, &ebx
, &flags
[3], &flags
[2]);
350 flags
[2] = flags
[3] = 0;
353 for (cf
= cpuflags
; cf
->flag
; cf
++) {
354 if (test_bit(cf
->bit
, flags
))
361 sysappend_strings
[SYSAPPEND_CPU
] = cpu_str
;
366 const struct sysappend_dmi_strings
*ds
;
374 sysappend_set_uuid(dmi_find_data(1, 0x08, 16));
375 sysappend_set_sysff(dmi_find_data(3, 0x05, 1));
377 for (ds
= dmi_strings
; ds
->prefix
; ds
++) {
378 if (!sysappend_strings
[ds
->sa
]) {
379 const char *str
= dmi_find_string(ds
->index
, ds
->offset
);
380 sysappend_strings
[ds
->sa
] = dmi_install_string(ds
->prefix
, str
);