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