smbios: Don't fill out firmware version on ChromeOS
[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 #if CONFIG_CHROMEOS
33 #include <vendorcode/google/chromeos/gnvs.h>
34 #endif
35
36 static u8 smbios_checksum(u8 *p, u32 length)
37 {
38         u8 ret = 0;
39         while (length--)
40                 ret += *p++;
41         return -ret;
42 }
43
44
45 int smbios_add_string(char *start, const char *str)
46 {
47         int i = 1;
48         char *p = start;
49
50         for(;;) {
51                 if (!*p) {
52                         strcpy(p, str);
53                         p += strlen(str);
54                         *p++ = '\0';
55                         *p++ = '\0';
56                         return i;
57                 }
58
59                 if (!strcmp(p, str))
60                         return i;
61
62                 p += strlen(p)+1;
63                 i++;
64         }
65 }
66
67 int smbios_string_table_len(char *start)
68 {
69         char *p = start;
70         int i, len = 0;
71
72         while(*p) {
73                 i = strlen(p) + 1;
74                 p += i;
75                 len += i;
76         }
77         return len + 1;
78 }
79
80 static int smbios_cpu_vendor(char *start)
81 {
82         char tmp[13] = "Unknown";
83         u32 *_tmp = (u32 *)tmp;
84         struct cpuid_result res;
85
86         if (cpu_have_cpuid()) {
87                 res = cpuid(0);
88                 _tmp[0] = res.ebx;
89                 _tmp[1] = res.edx;
90                 _tmp[2] = res.ecx;
91                 tmp[12] = '\0';
92         }
93
94         return smbios_add_string(start, tmp);
95 }
96
97 static int smbios_processor_name(char *start)
98 {
99         char tmp[49] = "Unknown Processor Name";
100         u32  *_tmp = (u32 *)tmp;
101         struct cpuid_result res;
102         int i;
103
104         if (cpu_have_cpuid()) {
105                 res = cpuid(0x80000000);
106                 if (res.eax >= 0x80000004) {
107                         for (i = 0; i < 3; i++) {
108                                 res = cpuid(0x80000002 + i);
109                                 _tmp[i * 4 + 0] = res.eax;
110                                 _tmp[i * 4 + 1] = res.ebx;
111                                 _tmp[i * 4 + 2] = res.ecx;
112                                 _tmp[i * 4 + 3] = res.edx;
113                         }
114                         tmp[48] = 0;
115                 }
116         }
117         return smbios_add_string(start, tmp);
118 }
119
120 static int smbios_write_type0(unsigned long *current, int handle)
121 {
122         struct cbfs_header *hdr;
123         struct smbios_type0 *t = (struct smbios_type0 *)*current;
124         int len = sizeof(struct smbios_type0);
125
126         memset(t, 0, sizeof(struct smbios_type0));
127         t->type = SMBIOS_BIOS_INFORMATION;
128         t->handle = handle;
129         t->length = len - 2;
130
131         t->vendor = smbios_add_string(t->eos, "coreboot");
132 #if !CONFIG_CHROMEOS
133         t->bios_release_date = smbios_add_string(t->eos, COREBOOT_DMI_DATE);
134         t->bios_version = smbios_add_string(t->eos, COREBOOT_VERSION);
135 #else
136 #define SPACES \
137         "                                                                  "
138         t->bios_release_date = smbios_add_string(t->eos, COREBOOT_DMI_DATE);
139         u32 version_offset = (u32)smbios_string_table_len(t->eos);
140         t->bios_version = smbios_add_string(t->eos, SPACES);
141         /* SMBIOS offsets start at 1 rather than 0 */
142         vboot_data->vbt10 = (u32)t->eos + (version_offset - 1);
143 #endif
144
145         if ((hdr = get_cbfs_header()) != (struct cbfs_header *)0xffffffff)
146                 t->bios_rom_size = (ntohl(hdr->romsize) / 65535) - 1;
147         t->system_bios_major_release = 4;
148         t->bios_characteristics =
149                 BIOS_CHARACTERISTICS_PCI_SUPPORTED |
150 #if CONFIG_CARDBUS_PLUGIN_SUPPORT
151                 BIOS_CHARACTERISTICS_PC_CARD |
152 #endif
153                 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
154                 BIOS_CHARACTERISTICS_UPGRADEABLE;
155
156 #if CONFIG_GENERATE_ACPI_TABLES
157         t->bios_characteristics_ext1 = BIOS_EXT1_CHARACTERISTICS_ACPI;
158 #endif
159         t->bios_characteristics_ext2 = BIOS_EXT2_CHARACTERISTICS_TARGET;
160         len = t->length + smbios_string_table_len(t->eos);
161         *current += len;
162         return len;
163 }
164
165 static int smbios_write_type1(unsigned long *current, int handle)
166 {
167         struct smbios_type1 *t = (struct smbios_type1 *)*current;
168         int len = sizeof(struct smbios_type1);
169
170         memset(t, 0, sizeof(struct smbios_type1));
171         t->type = SMBIOS_SYSTEM_INFORMATION;
172         t->handle = handle;
173         t->length = len - 2;
174         t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
175         t->product_name = smbios_add_string(t->eos, CONFIG_MAINBOARD_PART_NUMBER);
176         t->serial_number = smbios_add_string(t->eos, "123456789");
177         t->version = smbios_add_string(t->eos, "1.0");
178         len = t->length + smbios_string_table_len(t->eos);
179         *current += len;
180         return len;
181 }
182
183 static int smbios_write_type3(unsigned long *current, int handle)
184 {
185         struct smbios_type3 *t = (struct smbios_type3 *)*current;
186         int len = sizeof(struct smbios_type3);
187
188         memset(t, 0, sizeof(struct smbios_type3));
189         t->type = SMBIOS_SYSTEM_ENCLOSURE;
190         t->handle = handle;
191         t->length = len - 2;
192         t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
193         t->bootup_state = SMBIOS_STATE_SAFE;
194         t->power_supply_state = SMBIOS_STATE_SAFE;
195         t->thermal_state = SMBIOS_STATE_SAFE;
196         t->_type = 3;
197         t->security_status = SMBIOS_STATE_SAFE;
198         len = t->length + smbios_string_table_len(t->eos);
199         *current += len;
200         return len;
201 }
202
203 static int smbios_write_type4(unsigned long *current, int handle)
204 {
205         struct cpuid_result res;
206         struct smbios_type4 *t = (struct smbios_type4 *)*current;
207         int len = sizeof(struct smbios_type4);
208
209         /* Provide sane defaults even for CPU without CPUID */
210         res.eax = res.edx = 0;
211         res.ebx = 0x10000;
212
213         if (cpu_have_cpuid()) {
214                 res = cpuid(1);
215         }
216
217         memset(t, 0, sizeof(struct smbios_type4));
218         t->type = SMBIOS_PROCESSOR_INFORMATION;
219         t->handle = handle;
220         t->length = len - 2;
221         t->processor_id[0] = res.eax;
222         t->processor_id[1] = res.edx;
223         t->processor_manufacturer = smbios_cpu_vendor(t->eos);
224         t->processor_version = smbios_processor_name(t->eos);
225         t->processor_family = (res.eax > 0) ? 0x0c : 0x6;
226         t->processor_type = 3; /* System Processor */
227         t->processor_upgrade = 0x06;
228         t->core_count = (res.ebx >> 16) & 0xff;
229         t->l1_cache_handle = 0xffff;
230         t->l2_cache_handle = 0xffff;
231         t->l3_cache_handle = 0xffff;
232         t->processor_upgrade = 1;
233         len = t->length + smbios_string_table_len(t->eos);
234         *current += len;
235         return len;
236 }
237
238 static int smbios_write_type32(unsigned long *current, int handle)
239 {
240         struct smbios_type32 *t = (struct smbios_type32 *)*current;
241         int len = sizeof(struct smbios_type32);
242
243         memset(t, 0, sizeof(struct smbios_type32));
244         t->type = SMBIOS_SYSTEM_BOOT_INFORMATION;
245         t->handle = handle;
246         t->length = len - 2;
247         *current += len;
248         return len;
249 }
250
251 static int smbios_write_type127(unsigned long *current, int handle)
252 {
253         struct smbios_type127 *t = (struct smbios_type127 *)*current;
254         int len = sizeof(struct smbios_type127);
255
256         memset(t, 0, sizeof(struct smbios_type127));
257         t->type = SMBIOS_END_OF_TABLE;
258         t->handle = handle;
259         t->length = len - 2;
260         *current += len;
261         return len;
262 }
263
264 static int smbios_walk_device_tree(device_t tree, int *handle, unsigned long *current)
265 {
266         device_t dev;
267         int len = 0;
268
269         for(dev = tree; dev; dev = dev->next) {
270                 printk(BIOS_INFO, "%s (%s)\n", dev_path(dev), dev->chip_ops ? dev->chip_ops->name : "");
271
272                 if (dev->ops && dev->ops->get_smbios_data)
273                         len += dev->ops->get_smbios_data(dev, handle, current);
274
275                 if (dev->chip_ops && dev->chip_ops->get_smbios_data)
276                         len += dev->chip_ops->get_smbios_data(dev, handle, current);
277         }
278         return len;
279 }
280
281 unsigned long smbios_write_tables(unsigned long current)
282 {
283         struct smbios_entry *se;
284         unsigned long tables;
285         int len, handle = 0;
286
287         current = ALIGN(current, 16);
288         printk(BIOS_DEBUG, "%s: %08lx\n", __func__, current);
289
290         se = (struct smbios_entry *)current;
291         current += sizeof(struct smbios_entry);
292         current = ALIGN(current, 16);
293
294         tables = current;
295         len = smbios_write_type0(&current, handle++);
296         len += smbios_write_type1(&current, handle++);
297         len += smbios_write_type3(&current, handle++);
298         len += smbios_write_type4(&current, handle++);
299         len += smbios_write_type32(&current, handle++);
300
301         len += smbios_walk_device_tree(all_devices, &handle, &current);
302
303         len += smbios_write_type127(&current, handle++);
304
305         memset(se, 0, sizeof(struct smbios_entry));
306         memcpy(se->anchor, "_SM_", 4);
307         se->length = sizeof(struct smbios_entry);
308         se->major_version = 2;
309         se->minor_version = 7;
310         se->max_struct_size = 24;
311         se->struct_count = handle;
312         memcpy(se->intermediate_anchor_string, "_DMI_", 5);
313
314         se->struct_table_address = (u32)tables;
315         se->struct_table_length = len;
316
317         se->intermediate_checksum = smbios_checksum((u8 *)se + 0x10,
318                                                     sizeof(struct smbios_entry) - 0x10);
319         se->checksum = smbios_checksum((u8 *)se, sizeof(struct smbios_entry));
320         return current;
321 }