e5156d9de786f0773305def08d73175304b8e275
[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];
80         u32 *_tmp = (u32 *)tmp;
81         struct cpuid_result res = cpuid(0);
82
83         _tmp[0] = res.ebx;
84         _tmp[1] = res.edx;
85         _tmp[2] = res.ecx;
86         tmp[12] = '\0';
87         return smbios_add_string(start, tmp);
88
89 }
90
91 static int smbios_processor_name(char *start)
92 {
93         char tmp[49];
94         u32  *_tmp = (u32 *)tmp;
95         struct cpuid_result res;
96         int i;
97
98         for (i = 0; i < 3; i++) {
99                 res = cpuid(0x80000002 + i);
100                 _tmp[i * 4 + 0] = res.eax;
101                 _tmp[i * 4 + 1] = res.ebx;
102                 _tmp[i * 4 + 2] = res.ecx;
103                 _tmp[i * 4 + 3] = res.edx;
104         }
105
106         tmp[48] = 0;
107         return smbios_add_string(start, tmp);
108 }
109
110 static int smbios_write_type0(unsigned long *current, int handle)
111 {
112         struct cbfs_header *hdr;
113         struct smbios_type0 *t = (struct smbios_type0 *)*current;
114         int len = sizeof(struct smbios_type0);
115
116         memset(t, 0, sizeof(struct smbios_type0));
117         t->type = SMBIOS_BIOS_INFORMATION;
118         t->handle = handle;
119         t->length = len - 2;
120
121         t->vendor = smbios_add_string(t->eos, "coreboot");
122         t->bios_release_date = smbios_add_string(t->eos, COREBOOT_DMI_DATE);
123         t->bios_version = smbios_add_string(t->eos, COREBOOT_VERSION);
124
125         if ((hdr = get_cbfs_header()) != (struct cbfs_header *)0xffffffff)
126                 t->bios_rom_size = (ntohl(hdr->romsize) / 65535) - 1;
127         t->system_bios_major_release = 4;
128         t->bios_characteristics =
129                 BIOS_CHARACTERISTICS_PCI_SUPPORTED |
130 #if CONFIG_CARDBUS_PLUGIN_SUPPORT
131                 BIOS_CHARACTERISTICS_PC_CARD |
132 #endif
133                 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
134                 BIOS_CHARACTERISTICS_UPGRADEABLE;
135
136 #if CONFIG_GENERATE_ACPI_TABLES
137         t->bios_characteristics_ext1 = BIOS_EXT1_CHARACTERISTICS_ACPI;
138 #endif
139         t->bios_characteristics_ext2 = BIOS_EXT2_CHARACTERISTICS_TARGET;
140         len = t->length + smbios_string_table_len(t->eos);
141         *current += len;
142         return len;
143 }
144
145 static int smbios_write_type1(unsigned long *current, int handle)
146 {
147         struct smbios_type1 *t = (struct smbios_type1 *)*current;
148         int len = sizeof(struct smbios_type1);
149
150         memset(t, 0, sizeof(struct smbios_type1));
151         t->type = SMBIOS_SYSTEM_INFORMATION;
152         t->handle = handle;
153         t->length = len - 2;
154         t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
155         t->product_name = smbios_add_string(t->eos, CONFIG_MAINBOARD_PART_NUMBER);
156         len = t->length + smbios_string_table_len(t->eos);
157         *current += len;
158         return len;
159 }
160
161 static int smbios_write_type3(unsigned long *current, int handle)
162 {
163         struct smbios_type3 *t = (struct smbios_type3 *)*current;
164         int len = sizeof(struct smbios_type3);
165
166         memset(t, 0, sizeof(struct smbios_type3));
167         t->type = SMBIOS_SYSTEM_ENCLOSURE;
168         t->handle = handle;
169         t->length = len - 2;
170         t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
171         t->bootup_state = SMBIOS_STATE_SAFE;
172         t->power_supply_state = SMBIOS_STATE_SAFE;
173         t->thermal_state = SMBIOS_STATE_SAFE;
174         t->_type = 3;
175         t->security_status = SMBIOS_STATE_SAFE;
176         len = t->length + smbios_string_table_len(t->eos);
177         *current += len;
178         return len;
179 }
180
181 static int smbios_write_type4(unsigned long *current, int handle)
182 {
183         struct cpuid_result res;
184         struct smbios_type4 *t = (struct smbios_type4 *)*current;
185         int len = sizeof(struct smbios_type4);
186
187         res = cpuid(1);
188
189         memset(t, 0, sizeof(struct smbios_type4));
190         t->type = SMBIOS_PROCESSOR_INFORMATION;
191         t->handle = handle;
192         t->length = len - 2;
193         t->processor_id[0] = res.eax;
194         t->processor_id[1] = res.edx;
195         t->processor_manufacturer = smbios_cpu_vendor(t->eos);
196         t->processor_version = smbios_processor_name(t->eos);
197         t->processor_family = 0x0c;
198         t->processor_type = 3; /* System Processor */
199         t->processor_upgrade = 0x06;
200         t->core_count = (res.ebx >> 16) & 0xff;
201         t->l1_cache_handle = 0xffff;
202         t->l2_cache_handle = 0xffff;
203         t->l3_cache_handle = 0xffff;
204         t->processor_upgrade = 1;
205         len = t->length + smbios_string_table_len(t->eos);
206         *current += len;
207         return len;
208 }
209
210 static int smbios_write_type32(unsigned long *current, int handle)
211 {
212         struct smbios_type32 *t = (struct smbios_type32 *)*current;
213         int len = sizeof(struct smbios_type32);
214
215         memset(t, 0, sizeof(struct smbios_type32));
216         t->type = SMBIOS_SYSTEM_BOOT_INFORMATION;
217         t->handle = handle;
218         t->length = len - 2;
219         *current += len;
220         return len;
221 }
222
223 static int smbios_write_type127(unsigned long *current, int handle)
224 {
225         struct smbios_type127 *t = (struct smbios_type127 *)*current;
226         int len = sizeof(struct smbios_type127);
227
228         memset(t, 0, sizeof(struct smbios_type127));
229         t->type = SMBIOS_END_OF_TABLE;
230         t->handle = handle;
231         t->length = len - 2;
232         *current += len;
233         return len;
234 }
235
236 static int smbios_walk_device_tree(device_t tree, int *handle, unsigned long *current)
237 {
238         device_t dev;
239         int len = 0;
240
241         for(dev = tree; dev; dev = dev->next) {
242                 printk(BIOS_INFO, "%s (%s)\n", dev_path(dev), dev->chip_ops ? dev->chip_ops->name : "");
243
244                 if (dev->ops && dev->ops->get_smbios_data)
245                         len += dev->ops->get_smbios_data(dev, handle, current);
246
247                 if (dev->chip_ops && dev->chip_ops->get_smbios_data)
248                         len += dev->chip_ops->get_smbios_data(dev, handle, current);
249         }
250         return len;
251 }
252
253 unsigned long smbios_write_tables(unsigned long current)
254 {
255         struct smbios_entry *se;
256         unsigned long tables;
257         int len, handle = 0;
258
259         current = ALIGN(current, 16);
260         printk(BIOS_DEBUG, "%s: %08lx\n", __func__, current);
261
262         se = (struct smbios_entry *)current;
263         current += sizeof(struct smbios_entry);
264         current = ALIGN(current, 16);
265
266         tables = current;
267         len = smbios_write_type0(&current, handle++);
268         len += smbios_write_type1(&current, handle++);
269         len += smbios_write_type3(&current, handle++);
270         len += smbios_write_type4(&current, handle++);
271         len += smbios_write_type32(&current, handle++);
272
273         len += smbios_walk_device_tree(all_devices, &handle, &current);
274
275         len += smbios_write_type127(&current, handle++);
276
277         memset(se, 0, sizeof(struct smbios_entry));
278         memcpy(se->anchor, "_SM_", 4);
279         se->length = sizeof(struct smbios_entry);
280         se->major_version = 2;
281         se->minor_version = 7;
282         se->max_struct_size = 24;
283         se->struct_count = handle;
284         memcpy(se->intermediate_anchor_string, "_DMI_", 5);
285
286         se->struct_table_address = (u32)tables;
287         se->struct_table_length = len;
288
289         se->intermediate_checksum = smbios_checksum((u8 *)se + 0x10,
290                                                     sizeof(struct smbios_entry) - 0x10);
291         se->checksum = smbios_checksum((u8 *)se, sizeof(struct smbios_entry));
292         return current;
293 }