]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/i386/libi386/smbios.c
Implement pci_enable_msi() and pci_disable_msi() in the LinuxKPI.
[FreeBSD/FreeBSD.git] / stand / i386 / libi386 / smbios.c
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
9  *      notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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$");
29
30 #include <stand.h>
31 #include <bootstrap.h>
32 #include <sys/endian.h>
33
34 #ifdef EFI
35 /* In EFI, we don't need PTOV(). */
36 #define PTOV(x)         (caddr_t)(x)
37 #else
38 #include "btxv86.h"
39 #endif
40 #include "smbios.h"
41
42 /*
43  * Detect SMBIOS and export information about the SMBIOS into the
44  * environment.
45  *
46  * System Management BIOS Reference Specification, v2.6 Final
47  * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf
48  */
49
50 /*
51  * 2.1.1 SMBIOS Structure Table Entry Point
52  *
53  * "On non-EFI systems, the SMBIOS Entry Point structure, described below, can
54  * be located by application software by searching for the anchor-string on
55  * paragraph (16-byte) boundaries within the physical memory address range
56  * 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate anchor
57  * string that is used by some existing DMI browsers."
58  */
59 #define SMBIOS_START            0xf0000
60 #define SMBIOS_LENGTH           0x10000
61 #define SMBIOS_STEP             0x10
62 #define SMBIOS_SIG              "_SM_"
63 #define SMBIOS_DMI_SIG          "_DMI_"
64
65 #define SMBIOS_GET8(base, off)  (*(uint8_t *)((base) + (off)))
66 #define SMBIOS_GET16(base, off) (*(uint16_t *)((base) + (off)))
67 #define SMBIOS_GET32(base, off) (*(uint32_t *)((base) + (off)))
68
69 #define SMBIOS_GETLEN(base)     SMBIOS_GET8(base, 0x01)
70 #define SMBIOS_GETSTR(base)     ((base) + SMBIOS_GETLEN(base))
71
72 struct smbios_attr {
73         int             probed;
74         caddr_t         addr;
75         size_t          length;
76         size_t          count;
77         int             major;
78         int             minor;
79         int             ver;
80         const char*     bios_vendor;
81         const char*     maker;
82         const char*     product;
83         uint32_t        enabled_memory;
84         uint32_t        old_enabled_memory;
85         uint8_t         enabled_sockets;
86         uint8_t         populated_sockets;
87 };
88
89 static struct smbios_attr smbios;
90
91 static uint8_t
92 smbios_checksum(const caddr_t addr, const uint8_t len)
93 {
94         uint8_t         sum;
95         int             i;
96
97         for (sum = 0, i = 0; i < len; i++)
98                 sum += SMBIOS_GET8(addr, i);
99         return (sum);
100 }
101
102 static caddr_t
103 smbios_sigsearch(const caddr_t addr, const uint32_t len)
104 {
105         caddr_t         cp;
106
107         /* Search on 16-byte boundaries. */
108         for (cp = addr; cp < addr + len; cp += SMBIOS_STEP)
109                 if (strncmp(cp, SMBIOS_SIG, 4) == 0 &&
110                     smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 &&
111                     strncmp(cp + 0x10, SMBIOS_DMI_SIG, 5) == 0 &&
112                     smbios_checksum(cp + 0x10, 0x0f) == 0)
113                         return (cp);
114         return (NULL);
115 }
116
117 static const char*
118 smbios_getstring(caddr_t addr, const int offset)
119 {
120         caddr_t         cp;
121         int             i, idx;
122
123         idx = SMBIOS_GET8(addr, offset);
124         if (idx != 0) {
125                 cp = SMBIOS_GETSTR(addr);
126                 for (i = 1; i < idx; i++)
127                         cp += strlen(cp) + 1;
128                 return cp;
129         }
130         return (NULL);
131 }
132
133 static void
134 smbios_setenv(const char *name, caddr_t addr, const int offset)
135 {
136         const char*     val;
137
138         val = smbios_getstring(addr, offset);
139         if (val != NULL)
140                 setenv(name, val, 1);
141 }
142
143 #ifdef SMBIOS_SERIAL_NUMBERS
144
145 #define UUID_SIZE               16
146 #define UUID_TYPE               uint32_t
147 #define UUID_STEP               sizeof(UUID_TYPE)
148 #define UUID_ALL_BITS           (UUID_SIZE / UUID_STEP)
149 #define UUID_GET(base, off)     (*(UUID_TYPE *)((base) + (off)))
150
151 static void
152 smbios_setuuid(const char *name, const caddr_t addr, const int ver)
153 {
154         char            uuid[37];
155         int             byteorder, i, ones, zeros;
156         UUID_TYPE       n;
157         uint32_t        f1;
158         uint16_t        f2, f3;
159
160         for (i = 0, ones = 0, zeros = 0; i < UUID_SIZE; i += UUID_STEP) {
161                 n = UUID_GET(addr, i) + 1;
162                 if (zeros == 0 && n == 0)
163                         ones++;
164                 else if (ones == 0 && n == 1)
165                         zeros++;
166                 else
167                         break;
168         }
169
170         if (ones != UUID_ALL_BITS && zeros != UUID_ALL_BITS) {
171                 /*
172                  * 3.3.2.1 System UUID
173                  *
174                  * "Although RFC 4122 recommends network byte order for all
175                  * fields, the PC industry (including the ACPI, UEFI, and
176                  * Microsoft specifications) has consistently used
177                  * little-endian byte encoding for the first three fields:
178                  * time_low, time_mid, time_hi_and_version. The same encoding,
179                  * also known as wire format, should also be used for the
180                  * SMBIOS representation of the UUID."
181                  *
182                  * Note: We use network byte order for backward compatibility
183                  * unless SMBIOS version is 2.6+ or little-endian is forced.
184                  */
185 #if defined(SMBIOS_LITTLE_ENDIAN_UUID)
186                 byteorder = LITTLE_ENDIAN;
187 #elif defined(SMBIOS_NETWORK_ENDIAN_UUID)
188                 byteorder = BIG_ENDIAN;
189 #else
190                 byteorder = ver < 0x0206 ? BIG_ENDIAN : LITTLE_ENDIAN;
191 #endif
192                 if (byteorder != LITTLE_ENDIAN) {
193                         f1 = ntohl(SMBIOS_GET32(addr, 0));
194                         f2 = ntohs(SMBIOS_GET16(addr, 4));
195                         f3 = ntohs(SMBIOS_GET16(addr, 6));
196                 } else {
197                         f1 = le32toh(SMBIOS_GET32(addr, 0));
198                         f2 = le16toh(SMBIOS_GET16(addr, 4));
199                         f3 = le16toh(SMBIOS_GET16(addr, 6));
200                 }
201                 sprintf(uuid,
202                     "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
203                     f1, f2, f3, SMBIOS_GET8(addr, 8), SMBIOS_GET8(addr, 9),
204                     SMBIOS_GET8(addr, 10), SMBIOS_GET8(addr, 11),
205                     SMBIOS_GET8(addr, 12), SMBIOS_GET8(addr, 13),
206                     SMBIOS_GET8(addr, 14), SMBIOS_GET8(addr, 15));
207                 setenv(name, uuid, 1);
208         }
209 }
210
211 #undef UUID_SIZE
212 #undef UUID_TYPE
213 #undef UUID_STEP
214 #undef UUID_ALL_BITS
215 #undef UUID_GET
216
217 #endif
218
219 static caddr_t
220 smbios_parse_table(const caddr_t addr)
221 {
222         caddr_t         cp;
223         int             proc, size, osize, type;
224
225         type = SMBIOS_GET8(addr, 0);    /* 3.1.2 Structure Header Format */
226         switch(type) {
227         case 0:         /* 3.3.1 BIOS Information (Type 0) */
228                 smbios_setenv("smbios.bios.vendor", addr, 0x04);
229                 smbios_setenv("smbios.bios.version", addr, 0x05);
230                 smbios_setenv("smbios.bios.reldate", addr, 0x08);
231                 break;
232
233         case 1:         /* 3.3.2 System Information (Type 1) */
234                 smbios_setenv("smbios.system.maker", addr, 0x04);
235                 smbios_setenv("smbios.system.product", addr, 0x05);
236                 smbios_setenv("smbios.system.version", addr, 0x06);
237 #ifdef SMBIOS_SERIAL_NUMBERS
238                 smbios_setenv("smbios.system.serial", addr, 0x07);
239                 smbios_setuuid("smbios.system.uuid", addr + 0x08, smbios.ver);
240 #endif
241                 if (smbios.major > 2 ||
242                     (smbios.major == 2 && smbios.minor >= 4)) {
243                         smbios_setenv("smbios.system.sku", addr, 0x19);
244                         smbios_setenv("smbios.system.family", addr, 0x1a);
245                 }
246                 break;
247
248         case 2:         /* 3.3.3 Base Board (or Module) Information (Type 2) */
249                 smbios_setenv("smbios.planar.maker", addr, 0x04);
250                 smbios_setenv("smbios.planar.product", addr, 0x05);
251                 smbios_setenv("smbios.planar.version", addr, 0x06);
252 #ifdef SMBIOS_SERIAL_NUMBERS
253                 smbios_setenv("smbios.planar.serial", addr, 0x07);
254                 smbios_setenv("smbios.planar.tag", addr, 0x08);
255 #endif
256                 smbios_setenv("smbios.planar.location", addr, 0x0a);
257                 break;
258
259         case 3:         /* 3.3.4 System Enclosure or Chassis (Type 3) */
260                 smbios_setenv("smbios.chassis.maker", addr, 0x04);
261                 smbios_setenv("smbios.chassis.version", addr, 0x06);
262 #ifdef SMBIOS_SERIAL_NUMBERS
263                 smbios_setenv("smbios.chassis.serial", addr, 0x07);
264                 smbios_setenv("smbios.chassis.tag", addr, 0x08);
265 #endif
266                 break;
267
268         case 4:         /* 3.3.5 Processor Information (Type 4) */
269                 /*
270                  * Offset 18h: Processor Status
271                  *
272                  * Bit 7        Reserved, must be 0
273                  * Bit 6        CPU Socket Populated
274                  *              1 - CPU Socket Populated
275                  *              0 - CPU Socket Unpopulated
276                  * Bit 5:3      Reserved, must be zero
277                  * Bit 2:0      CPU Status
278                  *              0h - Unknown
279                  *              1h - CPU Enabled
280                  *              2h - CPU Disabled by User via BIOS Setup
281                  *              3h - CPU Disabled by BIOS (POST Error)
282                  *              4h - CPU is Idle, waiting to be enabled
283                  *              5-6h - Reserved
284                  *              7h - Other
285                  */
286                 proc = SMBIOS_GET8(addr, 0x18);
287                 if ((proc & 0x07) == 1)
288                         smbios.enabled_sockets++;
289                 if ((proc & 0x40) != 0)
290                         smbios.populated_sockets++;
291                 break;
292
293         case 6:         /* 3.3.7 Memory Module Information (Type 6, Obsolete) */
294                 /*
295                  * Offset 0Ah: Enabled Size
296                  *
297                  * Bit 7        Bank connection
298                  *              1 - Double-bank connection
299                  *              0 - Single-bank connection
300                  * Bit 6:0      Size (n), where 2**n is the size in MB
301                  *              7Dh - Not determinable (Installed Size only)
302                  *              7Eh - Module is installed, but no memory
303                  *                    has been enabled
304                  *              7Fh - Not installed
305                  */
306                 osize = SMBIOS_GET8(addr, 0x0a) & 0x7f;
307                 if (osize > 0 && osize < 22)
308                         smbios.old_enabled_memory += 1 << (osize + 10);
309                 break;
310
311         case 17:        /* 3.3.18 Memory Device (Type 17) */
312                 /*
313                  * Offset 0Ch: Size
314                  *
315                  * Bit 15       Granularity
316                  *              1 - Value is in kilobytes units
317                  *              0 - Value is in megabytes units
318                  * Bit 14:0     Size
319                  */
320                 size = SMBIOS_GET16(addr, 0x0c);
321                 if (size != 0 && size != 0xffff)
322                         smbios.enabled_memory += (size & 0x8000) != 0 ?
323                             (size & 0x7fff) : (size << 10);
324                 break;
325
326         default:        /* skip other types */
327                 break;
328         }
329
330         /* Find structure terminator. */
331         cp = SMBIOS_GETSTR(addr);
332         while (SMBIOS_GET16(cp, 0) != 0)
333                 cp++;
334
335         return (cp + 2);
336 }
337
338 static caddr_t
339 smbios_find_struct(int type)
340 {
341         caddr_t         dmi;
342         size_t          i;
343
344         if (smbios.addr == NULL)
345                 return (NULL);
346
347         for (dmi = smbios.addr, i = 0;
348              dmi < smbios.addr + smbios.length && i < smbios.count; i++) {
349                 if (SMBIOS_GET8(dmi, 0) == type)
350                         return dmi;
351                 /* Find structure terminator. */
352                 dmi = SMBIOS_GETSTR(dmi);
353                 while (SMBIOS_GET16(dmi, 0) != 0)
354                         dmi++;
355                 dmi += 2;
356         }
357
358         return (NULL);
359 }
360
361 static void
362 smbios_probe(const caddr_t addr)
363 {
364         caddr_t         saddr, info;
365         uintptr_t       paddr;
366
367         if (smbios.probed)
368                 return;
369         smbios.probed = 1;
370
371         /* Search signatures and validate checksums. */
372         saddr = smbios_sigsearch(addr ? addr : PTOV(SMBIOS_START),
373             SMBIOS_LENGTH);
374         if (saddr == NULL)
375                 return;
376
377         smbios.length = SMBIOS_GET16(saddr, 0x16);      /* Structure Table Length */
378         paddr = SMBIOS_GET32(saddr, 0x18);              /* Structure Table Address */
379         smbios.count = SMBIOS_GET16(saddr, 0x1c);       /* No of SMBIOS Structures */
380         smbios.ver = SMBIOS_GET8(saddr, 0x1e);          /* SMBIOS BCD Revision */
381
382         if (smbios.ver != 0) {
383                 smbios.major = smbios.ver >> 4;
384                 smbios.minor = smbios.ver & 0x0f;
385                 if (smbios.major > 9 || smbios.minor > 9)
386                         smbios.ver = 0;
387         }
388         if (smbios.ver == 0) {
389                 smbios.major = SMBIOS_GET8(saddr, 0x06);/* SMBIOS Major Version */
390                 smbios.minor = SMBIOS_GET8(saddr, 0x07);/* SMBIOS Minor Version */
391         }
392         smbios.ver = (smbios.major << 8) | smbios.minor;
393         smbios.addr = PTOV(paddr);
394
395         /* Get system information from SMBIOS */
396         info = smbios_find_struct(0x00);
397         if (info != NULL) {
398                 smbios.bios_vendor = smbios_getstring(info, 0x04);
399         }
400         info = smbios_find_struct(0x01);
401         if (info != NULL) {
402                 smbios.maker = smbios_getstring(info, 0x04);
403                 smbios.product = smbios_getstring(info, 0x05);
404         }
405 }
406
407 void
408 smbios_detect(const caddr_t addr)
409 {
410         char            buf[16];
411         caddr_t         dmi;
412         size_t          i;
413
414         smbios_probe(addr);
415         if (smbios.addr == NULL)
416                 return;
417
418         for (dmi = smbios.addr, i = 0;
419              dmi < smbios.addr + smbios.length && i < smbios.count; i++)
420                 dmi = smbios_parse_table(dmi);
421
422         sprintf(buf, "%d.%d", smbios.major, smbios.minor);
423         setenv("smbios.version", buf, 1);
424         if (smbios.enabled_memory > 0 || smbios.old_enabled_memory > 0) {
425                 sprintf(buf, "%u", smbios.enabled_memory > 0 ?
426                     smbios.enabled_memory : smbios.old_enabled_memory);
427                 setenv("smbios.memory.enabled", buf, 1);
428         }
429         if (smbios.enabled_sockets > 0) {
430                 sprintf(buf, "%u", smbios.enabled_sockets);
431                 setenv("smbios.socket.enabled", buf, 1);
432         }
433         if (smbios.populated_sockets > 0) {
434                 sprintf(buf, "%u", smbios.populated_sockets);
435                 setenv("smbios.socket.populated", buf, 1);
436         }
437 }
438
439 static int
440 smbios_match_str(const char* s1, const char* s2)
441 {
442         return (s1 == NULL || (s2 != NULL && !strcmp(s1, s2)));
443 }
444
445 int
446 smbios_match(const char* bios_vendor, const char* maker,
447     const char* product)
448 {
449         /* XXXRP currently, only called from non-EFI. */
450         smbios_probe(NULL);
451         return (smbios_match_str(bios_vendor, smbios.bios_vendor) &&
452             smbios_match_str(maker, smbios.maker) &&
453             smbios_match_str(product, smbios.product));
454 }