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 | /*␊ |
28 | * Detect SMBIOS and export information about the SMBIOS into the␊ |
29 | * environment.␊ |
30 | *␊ |
31 | * System Management BIOS Reference Specification, v2.6 Final␊ |
32 | * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf␊ |
33 | */␊ |
34 | ␊ |
35 | /*␊ |
36 | * 2.1.1 SMBIOS Structure Table Entry Point␊ |
37 | *␊ |
38 | * "On non-EFI systems, the SMBIOS Entry Point structure, described below, can␊ |
39 | * be located by application software by searching for the anchor-string on␊ |
40 | * paragraph (16-byte) boundaries within the physical memory address range␊ |
41 | * 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate anchor␊ |
42 | * string that is used by some existing DMI browsers."␊ |
43 | */␊ |
44 | ␊ |
45 | #include "libsaio.h"␊ |
46 | ␊ |
47 | #define SMBIOS_START 0xf0000␊ |
48 | #define SMBIOS_LENGTH 0x10000␊ |
49 | #define SMBIOS_STEP 0x10␊ |
50 | #define SMBIOS_SIG "_SM_"␊ |
51 | #define SMBIOS_DMI_SIG "_DMI_"␊ |
52 | ␊ |
53 | #define SMBIOS_GET8(base, off) (*(uint8_t *)((base) + (off)))␊ |
54 | #define SMBIOS_GET16(base, off) (*(uint16_t *)((base) + (off)))␊ |
55 | #define SMBIOS_GET32(base, off) (*(uint32_t *)((base) + (off)))␊ |
56 | ␊ |
57 | #define SMBIOS_GETLEN(base) SMBIOS_GET8(base, 0x01)␊ |
58 | #define SMBIOS_GETSTR(base) ((base) + SMBIOS_GETLEN(base))␊ |
59 | ␊ |
60 | static uint32_t smbios_enabled_memory = 0;␊ |
61 | static uint32_t smbios_old_enabled_memory = 0;␊ |
62 | static uint8_t smbios_enabled_sockets = 0;␊ |
63 | static uint8_t smbios_populated_sockets = 0;␊ |
64 | typedef char* caddr_t;␊ |
65 | ␊ |
66 | static uint8_t␊ |
67 | smbios_checksum(const caddr_t addr, const uint8_t len)␊ |
68 | {␊ |
69 | ␉uint8_t sum;␊ |
70 | ␉int i;␊ |
71 | ␉␊ |
72 | ␉for (sum = 0, i = 0; i < len; i++)␊ |
73 | ␉␉sum += SMBIOS_GET8(addr, i);␊ |
74 | ␉return (sum);␊ |
75 | }␊ |
76 | ␊ |
77 | static caddr_t␊ |
78 | smbios_sigsearch(const caddr_t addr, const uint32_t len)␊ |
79 | {␊ |
80 | ␉caddr_t cp;␊ |
81 | ␉␊ |
82 | ␉/* Search on 16-byte boundaries. */␊ |
83 | ␉for (cp = addr; cp < addr + len; cp += SMBIOS_STEP)␊ |
84 | ␉␉if (strncmp(cp, SMBIOS_SIG, 4) == 0 &&␊ |
85 | ␉␉␉smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 &&␊ |
86 | ␉␉␉strncmp(cp + 0x10, SMBIOS_DMI_SIG, 5) == 0 &&␊ |
87 | ␉␉␉smbios_checksum(cp + 0x10, 0x0f) == 0)␊ |
88 | ␉␉␉return (cp);␊ |
89 | ␉return (NULL);␊ |
90 | }␊ |
91 | ␊ |
92 | void␊ |
93 | smbios_detect(void)␊ |
94 | {␊ |
95 | ␉char buf[16];␊ |
96 | ␉caddr_t addr, dmi, smbios;␊ |
97 | ␉size_t count, length;␊ |
98 | ␉uint32_t paddr;␊ |
99 | ␉int i, major, minor, ver;␊ |
100 | ␉␊ |
101 | ␉/* Search signatures and validate checksums. */␊ |
102 | ␉smbios = smbios_sigsearch(PTOV(SMBIOS_START), SMBIOS_LENGTH);␊ |
103 | ␉if (smbios == NULL)␊ |
104 | ␉␉return;␊ |
105 | ␉␊ |
106 | ␉length = SMBIOS_GET16(smbios, 0x16); /* Structure Table Length */␊ |
107 | ␉paddr = SMBIOS_GET32(smbios, 0x18); /* Structure Table Address */␊ |
108 | ␉count = SMBIOS_GET16(smbios, 0x1c); /* No of SMBIOS Structures */␊ |
109 | ␉ver = SMBIOS_GET8(smbios, 0x1e); /* SMBIOS BCD Revision */␊ |
110 | ␉␊ |
111 | ␉if (ver != 0) {␊ |
112 | ␉␉major = ver >> 4;␊ |
113 | ␉␉minor = ver & 0x0f;␊ |
114 | ␉␉if (major > 9 || minor > 9)␊ |
115 | ␉␉␉ver = 0;␊ |
116 | ␉}␊ |
117 | ␉if (ver == 0) {␊ |
118 | ␉␉major = SMBIOS_GET8(smbios, 0x06); /* SMBIOS Major Version */␊ |
119 | ␉␉minor = SMBIOS_GET8(smbios, 0x07); /* SMBIOS Minor Version */␊ |
120 | ␉}␊ |
121 | ␉ver = (major << 8) | minor;␊ |
122 | ␉␊ |
123 | ␉addr = PTOV(paddr);␊ |
124 | ␉for (dmi = addr, i = 0; dmi < addr + length && i < count; i++)␊ |
125 | ␉␉dmi = smbios_parse_table(dmi, ver);␊ |
126 | ␉␊ |
127 | ␉sprintf(buf, "%d.%d", major, minor);␊ |
128 | ␉setenv("smbios.version", buf, 1);␊ |
129 | ␉if (smbios_enabled_memory > 0 || smbios_old_enabled_memory > 0) {␊ |
130 | ␉␉sprintf(buf, "%u", smbios_enabled_memory > 0 ?␊ |
131 | ␉␉␉␉336 smbios_enabled_memory : smbios_old_enabled_memory);␊ |
132 | ␉␉setenv("smbios.memory.enabled", buf, 1);␊ |
133 | ␉}␊ |
134 | ␉if (smbios_enabled_sockets > 0) {␊ |
135 | ␉␉sprintf(buf, "%u", smbios_enabled_sockets);␊ |
136 | ␉␉setenv("smbios.socket.enabled", buf, 1);␊ |
137 | ␉}␊ |
138 | ␉if (smbios_populated_sockets > 0) {␊ |
139 | ␉␉sprintf(buf, "%u", smbios_populated_sockets);␊ |
140 | ␉␉setenv("smbios.socket.populated", buf, 1);␊ |
141 | ␉}␊ |
142 | }␊ |
143 | |