Deleted Added
full compact
smbios.c (190813) smbios.c (190814)
1/*-
2 * Copyright (c) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/boot/i386/libi386/smbios.c 190813 2009-04-07 17:24:25Z jkim $");
28__FBSDID("$FreeBSD: head/sys/boot/i386/libi386/smbios.c 190814 2009-04-07 17:58:15Z jkim $");
29
30#include <stand.h>
31#include <bootstrap.h>
29
30#include <stand.h>
31#include <bootstrap.h>
32#include <sys/endian.h>
32
33#include "btxv86.h"
34#include "libi386.h"
35
36/*
37 * Detect SMBIOS and export information about the SMBIOS into the
38 * environment.
39 *
33
34#include "btxv86.h"
35#include "libi386.h"
36
37/*
38 * Detect SMBIOS and export information about the SMBIOS into the
39 * environment.
40 *
40 * System Management BIOS Reference Specification, v2.4 Final
41 * http://www.dmtf.org/standards/published_documents/DSP0134.pdf
41 * System Management BIOS Reference Specification, v2.6 Final
42 * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf
42 */
43
44/*
43 */
44
45/*
45 * Spec. 2.1.1 SMBIOS Structure Table Entry Point
46 * 2.1.1 SMBIOS Structure Table Entry Point
46 *
47 *
47 * 'The SMBIOS Entry Point structure, described below, can be located by
48 * application software by searching for the anchor-string on paragraph
49 * (16-byte) boundaries within the physical memory address range
50 * 000F0000h to 000FFFFFh.'
48 * "On non-EFI systems, the SMBIOS Entry Point structure, described below, can
49 * be located by application software by searching for the anchor-string on
50 * paragraph (16-byte) boundaries within the physical memory address range
51 * 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate anchor
52 * string that is used by some existing DMI browsers."
51 */
52#define SMBIOS_START 0xf0000
53#define SMBIOS_LENGTH 0x10000
54#define SMBIOS_STEP 0x10
55#define SMBIOS_SIG "_SM_"
56#define SMBIOS_DMI_SIG "_DMI_"
57
53 */
54#define SMBIOS_START 0xf0000
55#define SMBIOS_LENGTH 0x10000
56#define SMBIOS_STEP 0x10
57#define SMBIOS_SIG "_SM_"
58#define SMBIOS_DMI_SIG "_DMI_"
59
60#define SMBIOS_GET8(base, off) (*(uint8_t *)((base) + (off)))
61#define SMBIOS_GET16(base, off) (*(uint16_t *)((base) + (off)))
62#define SMBIOS_GET32(base, off) (*(uint32_t *)((base) + (off)))
63
64#define SMBIOS_GETLEN(base) SMBIOS_GET8(base, 0x01)
65#define SMBIOS_GETSTR(base) ((base) + SMBIOS_GETLEN(base))
66
58static uint32_t smbios_enabled_memory = 0;
59static uint32_t smbios_old_enabled_memory = 0;
60static uint8_t smbios_enabled_sockets = 0;
61static uint8_t smbios_populated_sockets = 0;
62
67static uint32_t smbios_enabled_memory = 0;
68static uint32_t smbios_old_enabled_memory = 0;
69static uint8_t smbios_enabled_sockets = 0;
70static uint8_t smbios_populated_sockets = 0;
71
63static uint8_t *smbios_parse_table(const uint8_t *dmi);
64static void smbios_setenv(const char *name, const uint8_t *dmi,
65 const int offset);
66static uint8_t smbios_checksum(const caddr_t addr, const uint8_t len);
67static uint8_t *smbios_sigsearch(const caddr_t addr, const uint32_t len);
72static uint8_t
73smbios_checksum(const caddr_t addr, const uint8_t len)
74{
75 uint8_t sum;
76 int i;
68
77
69#ifdef SMBIOS_SERIAL_NUMBERS
70static void smbios_setuuid(const char *name, const uint8_t *dmi,
71 const int offset);
72#endif
78 for (sum = 0, i = 0; i < len; i++)
79 sum += SMBIOS_GET8(addr, i);
80 return (sum);
81}
73
82
74void
75smbios_detect(void)
83static caddr_t
84smbios_sigsearch(const caddr_t addr, const uint32_t len)
76{
85{
77 uint8_t *smbios, *dmi, *addr;
78 uint16_t i, length, count;
79 uint32_t paddr;
80 char buf[16];
86 caddr_t cp;
81
87
82 /* locate and validate the SMBIOS */
83 smbios = smbios_sigsearch(PTOV(SMBIOS_START), SMBIOS_LENGTH);
84 if (smbios == NULL)
85 return;
88 /* Search on 16-byte boundaries. */
89 for (cp = addr; cp < addr + len; cp += SMBIOS_STEP)
90 if (strncmp(cp, SMBIOS_SIG, 4) == 0 &&
91 smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 &&
92 strncmp(cp + 0x10, SMBIOS_DMI_SIG, 5) == 0 &&
93 smbios_checksum(cp + 0x10, 0x0f) == 0)
94 return (cp);
95 return (NULL);
96}
86
97
87 length = *(uint16_t *)(smbios + 0x16); /* Structure Table Length */
88 paddr = *(uint32_t *)(smbios + 0x18); /* Structure Table Address */
89 count = *(uint16_t *)(smbios + 0x1c); /* No of SMBIOS Structures */
98static void
99smbios_setenv(const char *name, caddr_t addr, const int offset)
100{
101 caddr_t cp;
102 int i, idx;
90
103
91 for (dmi = addr = PTOV(paddr), i = 0;
92 dmi - addr < length && i < count; i++)
93 dmi = smbios_parse_table(dmi);
94 if (smbios_enabled_memory > 0 || smbios_old_enabled_memory > 0) {
95 sprintf(buf, "%u", smbios_enabled_memory > 0 ?
96 smbios_enabled_memory : smbios_old_enabled_memory);
97 setenv("smbios.memory.enabled", buf, 1);
104 idx = SMBIOS_GET8(addr, offset);
105 if (idx != 0) {
106 cp = SMBIOS_GETSTR(addr);
107 for (i = 1; i < idx; i++)
108 cp += strlen(cp) + 1;
109 setenv(name, cp, 1);
98 }
110 }
99 if (smbios_enabled_sockets > 0) {
100 sprintf(buf, "%u", smbios_enabled_sockets);
101 setenv("smbios.socket.enabled", buf, 1);
111}
112
113#ifdef SMBIOS_SERIAL_NUMBERS
114
115#define UUID_SIZE 16
116#define UUID_TYPE uint32_t
117#define UUID_STEP sizeof(UUID_TYPE)
118#define UUID_ALL_BITS (UUID_SIZE / UUID_STEP)
119#define UUID_GET(base, off) (*(UUID_TYPE *)((base) + (off)))
120
121static void
122smbios_setuuid(const char *name, const caddr_t addr, const int ver)
123{
124 char uuid[37];
125 int i, ones, zeros;
126 UUID_TYPE n;
127 uint32_t f1;
128 uint16_t f2, f3;
129
130 for (i = 0, ones = 0, zeros = 0; i < UUID_SIZE; i += UUID_STEP) {
131 n = UUID_GET(addr, i) + 1;
132 if (zeros == 0 && n == 0)
133 ones++;
134 else if (ones == 0 && n == 1)
135 zeros++;
136 else
137 break;
102 }
138 }
103 if (smbios_populated_sockets > 0) {
104 sprintf(buf, "%u", smbios_populated_sockets);
105 setenv("smbios.socket.populated", buf, 1);
139
140 if (ones != UUID_ALL_BITS && zeros != UUID_ALL_BITS) {
141 /*
142 * 3.3.2.1 System UUID
143 *
144 * "Although RFC 4122 recommends network byte order for all
145 * fields, the PC industry (including the ACPI, UEFI, and
146 * Microsoft specifications) has consistently used
147 * little-endian byte encoding for the first three fields:
148 * time_low, time_mid, time_hi_and_version. The same encoding,
149 * also known as wire format, should also be used for the
150 * SMBIOS representation of the UUID."
151 *
152 * Note: We use network byte order for backward compatibility
153 * unless SMBIOS version is 2.6+ or little-endian is forced.
154 */
155#ifndef SMBIOS_LITTLE_ENDIAN_UUID
156 if (ver < 0x0206) {
157 f1 = ntohl(SMBIOS_GET32(addr, 0));
158 f2 = ntohs(SMBIOS_GET16(addr, 4));
159 f3 = ntohs(SMBIOS_GET16(addr, 6));
160 } else
161#endif
162 {
163 f1 = le32toh(SMBIOS_GET32(addr, 0));
164 f2 = le16toh(SMBIOS_GET16(addr, 4));
165 f3 = le16toh(SMBIOS_GET16(addr, 6));
166 }
167 sprintf(uuid,
168 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
169 f1, f2, f3, SMBIOS_GET8(addr, 8), SMBIOS_GET8(addr, 9),
170 SMBIOS_GET8(addr, 10), SMBIOS_GET8(addr, 11),
171 SMBIOS_GET8(addr, 12), SMBIOS_GET8(addr, 13),
172 SMBIOS_GET8(addr, 14), SMBIOS_GET8(addr, 15));
173 setenv(name, uuid, 1);
106 }
107}
108
174 }
175}
176
109static uint8_t *
110smbios_parse_table(const uint8_t *dmi)
177#undef UUID_SIZE
178#undef UUID_TYPE
179#undef UUID_STEP
180#undef UUID_ALL_BITS
181#undef UUID_GET
182
183#endif
184
185static caddr_t
186smbios_parse_table(const caddr_t addr, const int ver)
111{
187{
112 uint8_t *dp;
113 uint16_t size;
114 uint8_t osize;
188 caddr_t cp;
189 int proc, size, osize, type;
115
190
116 switch(dmi[0]) {
117 case 0: /* Type 0: BIOS */
118 smbios_setenv("smbios.bios.vendor", dmi, 0x04);
119 smbios_setenv("smbios.bios.version", dmi, 0x05);
120 smbios_setenv("smbios.bios.reldate", dmi, 0x08);
191 type = SMBIOS_GET8(addr, 0); /* 3.1.2 Structure Header Format */
192 switch(type) {
193 case 0: /* 3.3.1 BIOS Information (Type 0) */
194 smbios_setenv("smbios.bios.vendor", addr, 0x04);
195 smbios_setenv("smbios.bios.version", addr, 0x05);
196 smbios_setenv("smbios.bios.reldate", addr, 0x08);
121 break;
122
197 break;
198
123 case 1: /* Type 1: System */
124 smbios_setenv("smbios.system.maker", dmi, 0x04);
125 smbios_setenv("smbios.system.product", dmi, 0x05);
126 smbios_setenv("smbios.system.version", dmi, 0x06);
199 case 1: /* 3.3.2 System Information (Type 1) */
200 smbios_setenv("smbios.system.maker", addr, 0x04);
201 smbios_setenv("smbios.system.product", addr, 0x05);
202 smbios_setenv("smbios.system.version", addr, 0x06);
127#ifdef SMBIOS_SERIAL_NUMBERS
203#ifdef SMBIOS_SERIAL_NUMBERS
128 smbios_setenv("smbios.system.serial", dmi, 0x07);
129 smbios_setuuid("smbios.system.uuid", dmi, 0x08);
204 smbios_setenv("smbios.system.serial", addr, 0x07);
205 smbios_setuuid("smbios.system.uuid", addr + 0x08, ver);
130#endif
131 break;
132
206#endif
207 break;
208
133 case 2: /* Type 2: Base Board (or Module) */
134 smbios_setenv("smbios.planar.maker", dmi, 0x04);
135 smbios_setenv("smbios.planar.product", dmi, 0x05);
136 smbios_setenv("smbios.planar.version", dmi, 0x06);
209 case 2: /* 3.3.3 Base Board (or Module) Information (Type 2) */
210 smbios_setenv("smbios.planar.maker", addr, 0x04);
211 smbios_setenv("smbios.planar.product", addr, 0x05);
212 smbios_setenv("smbios.planar.version", addr, 0x06);
137#ifdef SMBIOS_SERIAL_NUMBERS
213#ifdef SMBIOS_SERIAL_NUMBERS
138 smbios_setenv("smbios.planar.serial", dmi, 0x07);
214 smbios_setenv("smbios.planar.serial", addr, 0x07);
139#endif
140 break;
141
215#endif
216 break;
217
142 case 3: /* Type 3: System Enclosure or Chassis */
143 smbios_setenv("smbios.chassis.maker", dmi, 0x04);
144 smbios_setenv("smbios.chassis.version", dmi, 0x06);
218 case 3: /* 3.3.4 System Enclosure or Chassis (Type 3) */
219 smbios_setenv("smbios.chassis.maker", addr, 0x04);
220 smbios_setenv("smbios.chassis.version", addr, 0x06);
145#ifdef SMBIOS_SERIAL_NUMBERS
221#ifdef SMBIOS_SERIAL_NUMBERS
146 smbios_setenv("smbios.chassis.serial", dmi, 0x07);
147 smbios_setenv("smbios.chassis.tag", dmi, 0x08);
222 smbios_setenv("smbios.chassis.serial", addr, 0x07);
223 smbios_setenv("smbios.chassis.tag", addr, 0x08);
148#endif
149 break;
150
224#endif
225 break;
226
151 case 4: /* Type 4: Processor Information */
227 case 4: /* 3.3.5 Processor Information (Type 4) */
152 /*
153 * Offset 18h: Processor Status
154 *
155 * Bit 7 Reserved, must be 0
156 * Bit 6 CPU Socket Populated
157 * 1 - CPU Socket Populated
158 * 0 - CPU Socket Unpopulated
159 * Bit 5:3 Reserved, must be zero
160 * Bit 2:0 CPU Status
161 * 0h - Unknown
162 * 1h - CPU Enabled
163 * 2h - CPU Disabled by User via BIOS Setup
164 * 3h - CPU Disabled by BIOS (POST Error)
165 * 4h - CPU is Idle, waiting to be enabled
166 * 5-6h - Reserved
167 * 7h - Other
168 */
228 /*
229 * Offset 18h: Processor Status
230 *
231 * Bit 7 Reserved, must be 0
232 * Bit 6 CPU Socket Populated
233 * 1 - CPU Socket Populated
234 * 0 - CPU Socket Unpopulated
235 * Bit 5:3 Reserved, must be zero
236 * Bit 2:0 CPU Status
237 * 0h - Unknown
238 * 1h - CPU Enabled
239 * 2h - CPU Disabled by User via BIOS Setup
240 * 3h - CPU Disabled by BIOS (POST Error)
241 * 4h - CPU is Idle, waiting to be enabled
242 * 5-6h - Reserved
243 * 7h - Other
244 */
169 if ((dmi[0x18] & 0x07) == 1)
245 proc = SMBIOS_GET8(addr, 0x18);
246 if ((proc & 0x07) == 1)
170 smbios_enabled_sockets++;
247 smbios_enabled_sockets++;
171 if (dmi[0x18] & 0x40)
248 if ((proc & 0x40) != 0)
172 smbios_populated_sockets++;
173 break;
174
249 smbios_populated_sockets++;
250 break;
251
175 case 6: /* Type 6: Memory Module Information (Obsolete) */
252 case 6: /* 3.3.7 Memory Module Information (Type 6, Obsolete) */
176 /*
177 * Offset 0Ah: Enabled Size
178 *
179 * Bit 7 Bank connection
180 * 1 - Double-bank connection
181 * 0 - Single-bank connection
182 * Bit 6:0 Size (n), where 2**n is the size in MB
183 * 7Dh - Not determinable (Installed Size only)
184 * 7Eh - Module is installed, but no memory
185 * has been enabled
186 * 7Fh - Not installed
187 */
253 /*
254 * Offset 0Ah: Enabled Size
255 *
256 * Bit 7 Bank connection
257 * 1 - Double-bank connection
258 * 0 - Single-bank connection
259 * Bit 6:0 Size (n), where 2**n is the size in MB
260 * 7Dh - Not determinable (Installed Size only)
261 * 7Eh - Module is installed, but no memory
262 * has been enabled
263 * 7Fh - Not installed
264 */
188 osize = dmi[0x0a] & 0x7f;
265 osize = SMBIOS_GET8(addr, 0x0a) & 0x7f;
189 if (osize > 0 && osize < 22)
266 if (osize > 0 && osize < 22)
190 smbios_old_enabled_memory += (1U << (osize + 10));
267 smbios_old_enabled_memory += 1 << (osize + 10);
191 break;
192
268 break;
269
193 case 17: /* Type 17: Memory Device */
270 case 17: /* 3.3.18 Memory Device (Type 17) */
194 /*
195 * Offset 0Ch: Size
196 *
197 * Bit 15 Granularity
198 * 1 - Value is in kilobytes units
199 * 0 - Value is in megabytes units
200 * Bit 14:0 Size
201 */
271 /*
272 * Offset 0Ch: Size
273 *
274 * Bit 15 Granularity
275 * 1 - Value is in kilobytes units
276 * 0 - Value is in megabytes units
277 * Bit 14:0 Size
278 */
202 size = *(uint16_t *)(dmi + 0x0c);
279 size = SMBIOS_GET16(addr, 0x0c);
203 if (size != 0 && size != 0xffff)
204 smbios_enabled_memory += (size & 0x8000) != 0 ?
280 if (size != 0 && size != 0xffff)
281 smbios_enabled_memory += (size & 0x8000) != 0 ?
205 (size & 0x7fff) : ((uint32_t)size << 10);
282 (size & 0x7fff) : (size << 10);
206 break;
207
283 break;
284
208 default: /* skip other types */
285 default: /* skip other types */
209 break;
210 }
211
286 break;
287 }
288
212 /* find structure terminator */
213 dp = __DECONST(uint8_t *, dmi + dmi[1]);
214 while (dp[0] != 0 || dp[1] != 0)
215 dp++;
289 /* Find structure terminator. */
290 cp = SMBIOS_GETSTR(addr);
291 while (SMBIOS_GET16(cp, 0) != 0)
292 cp++;
216
293
217 return(dp + 2);
294 return (cp + 2);
218}
219
295}
296
220static void
221smbios_setenv(const char *name, const uint8_t *dmi, const int offset)
297void
298smbios_detect(void)
222{
299{
223 char *cp = __DECONST(char *, dmi + dmi[1]);
224 int i;
300 char buf[16];
301 caddr_t addr, dmi, smbios;
302 size_t count, length;
303 uint32_t paddr;
304 int i, major, minor, ver;
225
305
226 /* skip undefined string */
227 if (dmi[offset] == 0)
306 /* Search signatures and validate checksums. */
307 smbios = smbios_sigsearch(PTOV(SMBIOS_START), SMBIOS_LENGTH);
308 if (smbios == NULL)
228 return;
229
309 return;
310
230 for (i = 0; i < dmi[offset] - 1; i++)
231 cp += strlen(cp) + 1;
232 setenv(name, cp, 1);
233}
311 length = SMBIOS_GET16(smbios, 0x16); /* Structure Table Length */
312 paddr = SMBIOS_GET32(smbios, 0x18); /* Structure Table Address */
313 count = SMBIOS_GET16(smbios, 0x1c); /* No of SMBIOS Structures */
314 ver = SMBIOS_GET8(smbios, 0x1e); /* SMBIOS BCD Revision */
234
315
235static uint8_t
236smbios_checksum(const caddr_t addr, const uint8_t len)
237{
238 const uint8_t *cp = addr;
239 uint8_t sum;
240 int i;
241
242 for (sum = 0, i = 0; i < len; i++)
243 sum += cp[i];
244
245 return(sum);
246}
247
248static uint8_t *
249smbios_sigsearch(const caddr_t addr, const uint32_t len)
250{
251 caddr_t cp;
252
253 /* search on 16-byte boundaries */
254 for (cp = addr; cp < addr + len; cp += SMBIOS_STEP) {
255 /* compare signature, validate checksum */
256 if (!strncmp(cp, SMBIOS_SIG, 4)) {
257 if (smbios_checksum(cp, *(uint8_t *)(cp + 0x05)))
258 continue;
259 if (strncmp(cp + 0x10, SMBIOS_DMI_SIG, 5))
260 continue;
261 if (smbios_checksum(cp + 0x10, 0x0f))
262 continue;
263
264 return(cp);
265 }
316 if (ver != 0) {
317 major = ver >> 4;
318 minor = ver & 0x0f;
319 if (major > 9 || minor > 9)
320 ver = 0;
266 }
321 }
322 if (ver == 0) {
323 major = SMBIOS_GET8(smbios, 0x06); /* SMBIOS Major Version */
324 minor = SMBIOS_GET8(smbios, 0x07); /* SMBIOS Minor Version */
325 }
326 ver = (major << 8) | minor;
267
327
268 return(NULL);
269}
328 addr = PTOV(paddr);
329 for (dmi = addr, i = 0; dmi < addr + length && i < count; i++)
330 dmi = smbios_parse_table(dmi, ver);
270
331
271#ifdef SMBIOS_SERIAL_NUMBERS
272static void
273smbios_setuuid(const char *name, const uint8_t *dmi, const int offset)
274{
275 const uint8_t *idp = dmi + offset;
276 int i, f = 0, z = 0;
277 char uuid[37];
278
279 for (i = 0; i < 16; i++) {
280 if (idp[i] == 0xff)
281 f++;
282 else if (idp[i] == 0x00)
283 z++;
284 else
285 break;
332 sprintf(buf, "%d.%d", major, minor);
333 setenv("smbios.version", buf, 1);
334 if (smbios_enabled_memory > 0 || smbios_old_enabled_memory > 0) {
335 sprintf(buf, "%u", smbios_enabled_memory > 0 ?
336 smbios_enabled_memory : smbios_old_enabled_memory);
337 setenv("smbios.memory.enabled", buf, 1);
286 }
338 }
287 if (f != 16 && z != 16) {
288 sprintf(uuid, "%02x%02x%02x%02x-"
289 "%02x%02x-%02x%02x-%02x%02x-"
290 "%02x%02x%02x%02x%02x%02x",
291 idp[0], idp[1], idp[2], idp[3],
292 idp[4], idp[5], idp[6], idp[7], idp[8], idp[9],
293 idp[10], idp[11], idp[12], idp[13], idp[14], idp[15]);
294 setenv(name, uuid, 1);
339 if (smbios_enabled_sockets > 0) {
340 sprintf(buf, "%u", smbios_enabled_sockets);
341 setenv("smbios.socket.enabled", buf, 1);
295 }
342 }
343 if (smbios_populated_sockets > 0) {
344 sprintf(buf, "%u", smbios_populated_sockets);
345 setenv("smbios.socket.populated", buf, 1);
346 }
296}
347}
297#endif