2bd00c49d4aa778a1979d6c7f8786bb716247815
[coreboot.git] / src / arch / x86 / boot / smbios.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 Sven Schnelle <svens@stackframe.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; version 2 of
9  * the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <smbios.h>
25 #include <console/console.h>
26 #include <build.h>
27 #include <device/device.h>
28 #include <arch/cpu.h>
29 #include <cpu/x86/name.h>
30 #include <cbfs_core.h>
31 #include <arch/byteorder.h>
32
33 static u8 smbios_checksum(u8 *p, u32 length)
34 {
35         u8 ret = 0;
36         while (length--)
37                 ret += *p++;
38         return -ret;
39 }
40
41
42 int smbios_add_string(char *start, const char *str)
43 {
44         int i = 1;
45         char *p = start;
46
47         for(;;) {
48                 if (!*p) {
49                         strcpy(p, str);
50                         p += strlen(str);
51                         *p++ = '\0';
52                         *p++ = '\0';
53                         return i;
54                 }
55
56                 if (!strcmp(p, str))
57                         return i;
58
59                 p += strlen(p)+1;
60                 i++;
61         }
62 }
63
64 int smbios_string_table_len(char *start)
65 {
66         char *p = start;
67         int i, len = 0;
68
69         while(*p) {
70                 i = strlen(p) + 1;
71                 p += i;
72                 len += i;
73         }
74         return len + 1;
75 }
76
77 static int smbios_cpu_vendor(char *start)
78 {
79         char tmp[13] = "Unknown";
80         u32 *_tmp = (u32 *)tmp;
81         struct cpuid_result res;
82
83         if (cpu_have_cpuid()) {
84                 res = cpuid(0);
85                 _tmp[0] = res.ebx;
86                 _tmp[1] = res.edx;
87                 _tmp[2] = res.ecx;
88                 tmp[12] = '\0';
89         }
90
91         return smbios_add_string(start, tmp);
92 }
93
94 static int smbios_processor_name(char *start)
95 {
96         char tmp[49] = "Unknown Processor Name";
97         u32  *_tmp = (u32 *)tmp;
98         struct cpuid_result res;
99         int i;
100
101         if (cpu_have_cpuid()) {
102                 res = cpuid(0x80000000);
103                 if (res.eax >= 0x80000004) {
104                         for (i = 0; i < 3; i++) {
105                                 res = cpuid(0x80000002 + i);
106                                 _tmp[i * 4 + 0] = res.eax;
107                                 _tmp[i * 4 + 1] = res.ebx;
108                                 _tmp[i * 4 + 2] = res.ecx;
109                                 _tmp[i * 4 + 3] = res.edx;
110                         }
111                         tmp[48] = 0;
112                 }
113         }
114         return smbios_add_string(start, tmp);
115 }
116
117 static int smbios_write_type0(unsigned long *current, int handle)
118 {
119         struct cbfs_header *hdr;
120         struct smbios_type0 *t = (struct smbios_type0 *)*current;
121         int len = sizeof(struct smbios_type0);
122
123         memset(t, 0, sizeof(struct smbios_type0));
124         t->type = SMBIOS_BIOS_INFORMATION;
125         t->handle = handle;
126         t->length = len - 2;
127
128         t->vendor = smbios_add_string(t->eos, "coreboot");
129         t->bios_release_date = smbios_add_string(t->eos, COREBOOT_DMI_DATE);
130         t->bios_version = smbios_add_string(t->eos, COREBOOT_VERSION);
131
132         if ((hdr = get_cbfs_header()) != (struct cbfs_header *)0xffffffff)
133                 t->bios_rom_size = (ntohl(hdr->romsize) / 65535) - 1;
134         t->system_bios_major_release = 4;
135         t->bios_characteristics =
136                 BIOS_CHARACTERISTICS_PCI_SUPPORTED |
137 #if CONFIG_CARDBUS_PLUGIN_SUPPORT
138                 BIOS_CHARACTERISTICS_PC_CARD |
139 #endif
140                 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
141                 BIOS_CHARACTERISTICS_UPGRADEABLE;
142
143 #if CONFIG_GENERATE_ACPI_TABLES
144         t->bios_characteristics_ext1 = BIOS_EXT1_CHARACTERISTICS_ACPI;
145 #endif
146         t->bios_characteristics_ext2 = BIOS_EXT2_CHARACTERISTICS_TARGET;
147         len = t->length + smbios_string_table_len(t->eos);
148         *current += len;
149         return len;
150 }
151
152 static int smbios_write_type1(unsigned long *current, int handle)
153 {
154         struct smbios_type1 *t = (struct smbios_type1 *)*current;
155         int len = sizeof(struct smbios_type1);
156
157         memset(t, 0, sizeof(struct smbios_type1));
158         t->type = SMBIOS_SYSTEM_INFORMATION;
159         t->handle = handle;
160         t->length = len - 2;
161         t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
162         t->product_name = smbios_add_string(t->eos, CONFIG_MAINBOARD_PART_NUMBER);
163         len = t->length + smbios_string_table_len(t->eos);
164         *current += len;
165         return len;
166 }
167
168 static int smbios_write_type3(unsigned long *current, int handle)
169 {
170         struct smbios_type3 *t = (struct smbios_type3 *)*current;
171         int len = sizeof(struct smbios_type3);
172
173         memset(t, 0, sizeof(struct smbios_type3));
174         t->type = SMBIOS_SYSTEM_ENCLOSURE;
175         t->handle = handle;
176         t->length = len - 2;
177         t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
178         t->bootup_state = SMBIOS_STATE_SAFE;
179         t->power_supply_state = SMBIOS_STATE_SAFE;
180         t->thermal_state = SMBIOS_STATE_SAFE;
181         t->_type = 3;
182         t->security_status = SMBIOS_STATE_SAFE;
183         len = t->length + smbios_string_table_len(t->eos);
184         *current += len;
185         return len;
186 }
187
188 static int smbios_write_type4(unsigned long *current, int handle)
189 {
190         struct cpuid_result res;
191         struct smbios_type4 *t = (struct smbios_type4 *)*current;
192         int len = sizeof(struct smbios_type4);
193
194         /* Provide sane defaults even for CPU without CPUID */
195         res.eax = res.edx = 0;
196         res.ebx = 0x10000;
197
198         if (cpu_have_cpuid()) {
199                 res = cpuid(1);
200         }
201
202         memset(t, 0, sizeof(struct smbios_type4));
203         t->type = SMBIOS_PROCESSOR_INFORMATION;
204         t->handle = handle;
205         t->length = len - 2;
206         t->processor_id[0] = res.eax;
207         t->processor_id[1] = res.edx;
208         t->processor_manufacturer = smbios_cpu_vendor(t->eos);
209         t->processor_version = smbios_processor_name(t->eos);
210         t->processor_family = (res.eax > 0) ? 0x0c : 0x6;
211         t->processor_type = 3; /* System Processor */
212         t->processor_upgrade = 0x06;
213         t->core_count = (res.ebx >> 16) & 0xff;
214         t->l1_cache_handle = 0xffff;
215         t->l2_cache_handle = 0xffff;
216         t->l3_cache_handle = 0xffff;
217         t->processor_upgrade = 1;
218         len = t->length + smbios_string_table_len(t->eos);
219         *current += len;
220         return len;
221 }
222
223 static int smbios_write_type32(unsigned long *current, int handle)
224 {
225         struct smbios_type32 *t = (struct smbios_type32 *)*current;
226         int len = sizeof(struct smbios_type32);
227
228         memset(t, 0, sizeof(struct smbios_type32));
229         t->type = SMBIOS_SYSTEM_BOOT_INFORMATION;
230         t->handle = handle;
231         t->length = len - 2;
232         *current += len;
233         return len;
234 }
235
236 static int smbios_write_type127(unsigned long *current, int handle)
237 {
238         struct smbios_type127 *t = (struct smbios_type127 *)*current;
239         int len = sizeof(struct smbios_type127);
240
241         memset(t, 0, sizeof(struct smbios_type127));
242         t->type = SMBIOS_END_OF_TABLE;
243         t->handle = handle;
244         t->length = len - 2;
245         *current += len;
246         return len;
247 }
248
249 static int smbios_walk_device_tree(device_t tree, int *handle, unsigned long *current)
250 {
251         device_t dev;
252         int len = 0;
253
254         for(dev = tree; dev; dev = dev->next) {
255                 printk(BIOS_INFO, "%s (%s)\n", dev_path(dev), dev->chip_ops ? dev->chip_ops->name : "");
256
257                 if (dev->ops && dev->ops->get_smbios_data)
258                         len += dev->ops->get_smbios_data(dev, handle, current);
259
260                 if (dev->chip_ops && dev->chip_ops->get_smbios_data)
261                         len += dev->chip_ops->get_smbios_data(dev, handle, current);
262         }
263         return len;
264 }
265
266 unsigned long smbios_write_tables(unsigned long current)
267 {
268         struct smbios_entry *se;
269         unsigned long tables;
270         int len, handle = 0;
271
272         current = ALIGN(current, 16);
273         printk(BIOS_DEBUG, "%s: %08lx\n", __func__, current);
274
275         se = (struct smbios_entry *)current;
276         current += sizeof(struct smbios_entry);
277         current = ALIGN(current, 16);
278
279         tables = current;
280         len = smbios_write_type0(&current, handle++);
281         len += smbios_write_type1(&current, handle++);
282         len += smbios_write_type3(&current, handle++);
283         len += smbios_write_type4(&current, handle++);
284         len += smbios_write_type32(&current, handle++);
285
286         len += smbios_walk_device_tree(all_devices, &handle, &current);
287
288         len += smbios_write_type127(&current, handle++);
289
290         memset(se, 0, sizeof(struct smbios_entry));
291         memcpy(se->anchor, "_SM_", 4);
292         se->length = sizeof(struct smbios_entry);
293         se->major_version = 2;
294         se->minor_version = 7;
295         se->max_struct_size = 24;
296         se->struct_count = handle;
297         memcpy(se->intermediate_anchor_string, "_DMI_", 5);
298
299         se->struct_table_address = (u32)tables;
300         se->struct_table_length = len;
301
302         se->intermediate_checksum = smbios_checksum((u8 *)se + 0x10,
303                                                     sizeof(struct smbios_entry) - 0x10);
304         se->checksum = smbios_checksum((u8 *)se, sizeof(struct smbios_entry));
305         return current;
306 }