blob: f2f998d7bc3cc845078d37a6640ca72fd04b031b [file] [log] [blame]
Sven Schnelle164bcfd2011-08-14 20:56:34 +02001/*
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>
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +020026#include <version.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020027#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>
Duncan Laurie472ec9c2012-06-23 16:13:42 -070032#include <elog.h>
Stefan Reinauerc6b21662012-04-03 16:02:54 -070033#if CONFIG_CHROMEOS
34#include <vendorcode/google/chromeos/gnvs.h>
35#endif
Sven Schnelle164bcfd2011-08-14 20:56:34 +020036
37static u8 smbios_checksum(u8 *p, u32 length)
38{
39 u8 ret = 0;
40 while (length--)
41 ret += *p++;
42 return -ret;
43}
44
45
46int smbios_add_string(char *start, const char *str)
47{
48 int i = 1;
49 char *p = start;
50
51 for(;;) {
52 if (!*p) {
53 strcpy(p, str);
54 p += strlen(str);
55 *p++ = '\0';
56 *p++ = '\0';
57 return i;
58 }
59
60 if (!strcmp(p, str))
61 return i;
62
63 p += strlen(p)+1;
64 i++;
65 }
66}
67
68int smbios_string_table_len(char *start)
69{
70 char *p = start;
71 int i, len = 0;
72
73 while(*p) {
74 i = strlen(p) + 1;
75 p += i;
76 len += i;
77 }
78 return len + 1;
79}
80
81static int smbios_cpu_vendor(char *start)
82{
Rudolf Marek06253cd2012-02-25 23:51:12 +010083 char tmp[13] = "Unknown";
Sven Schnelle164bcfd2011-08-14 20:56:34 +020084 u32 *_tmp = (u32 *)tmp;
Rudolf Marek06253cd2012-02-25 23:51:12 +010085 struct cpuid_result res;
Sven Schnelle164bcfd2011-08-14 20:56:34 +020086
Rudolf Marek06253cd2012-02-25 23:51:12 +010087 if (cpu_have_cpuid()) {
88 res = cpuid(0);
89 _tmp[0] = res.ebx;
90 _tmp[1] = res.edx;
91 _tmp[2] = res.ecx;
92 tmp[12] = '\0';
93 }
94
Sven Schnelle164bcfd2011-08-14 20:56:34 +020095 return smbios_add_string(start, tmp);
Sven Schnelle164bcfd2011-08-14 20:56:34 +020096}
97
98static int smbios_processor_name(char *start)
99{
Rudolf Marek06253cd2012-02-25 23:51:12 +0100100 char tmp[49] = "Unknown Processor Name";
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200101 u32 *_tmp = (u32 *)tmp;
102 struct cpuid_result res;
103 int i;
104
Rudolf Marek06253cd2012-02-25 23:51:12 +0100105 if (cpu_have_cpuid()) {
106 res = cpuid(0x80000000);
107 if (res.eax >= 0x80000004) {
108 for (i = 0; i < 3; i++) {
109 res = cpuid(0x80000002 + i);
110 _tmp[i * 4 + 0] = res.eax;
111 _tmp[i * 4 + 1] = res.ebx;
112 _tmp[i * 4 + 2] = res.ecx;
113 _tmp[i * 4 + 3] = res.edx;
114 }
115 tmp[48] = 0;
116 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200117 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200118 return smbios_add_string(start, tmp);
119}
120
Vladimir Serbinenko63acd222014-06-01 00:26:48 +0200121const char *__attribute__((weak)) smbios_mainboard_bios_version(void)
122{
123 if (strlen(CONFIG_LOCALVERSION))
124 return CONFIG_LOCALVERSION;
125 else
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +0200126 return coreboot_version;
Vladimir Serbinenko63acd222014-06-01 00:26:48 +0200127}
128
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200129static int smbios_write_type0(unsigned long *current, int handle)
130{
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200131 struct smbios_type0 *t = (struct smbios_type0 *)*current;
132 int len = sizeof(struct smbios_type0);
133
134 memset(t, 0, sizeof(struct smbios_type0));
135 t->type = SMBIOS_BIOS_INFORMATION;
136 t->handle = handle;
137 t->length = len - 2;
138
139 t->vendor = smbios_add_string(t->eos, "coreboot");
Stefan Reinauerc6b21662012-04-03 16:02:54 -0700140#if !CONFIG_CHROMEOS
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +0200141 t->bios_release_date = smbios_add_string(t->eos, coreboot_dmi_date);
Christian Gmeiner5e272a42013-02-04 16:22:46 +0100142
Vladimir Serbinenko63acd222014-06-01 00:26:48 +0200143 t->bios_version = smbios_add_string(t->eos, smbios_mainboard_bios_version());
Stefan Reinauerc6b21662012-04-03 16:02:54 -0700144#else
145#define SPACES \
146 " "
Kyösti Mälkkic36af7b2014-11-18 12:41:16 +0200147 t->bios_release_date = smbios_add_string(t->eos, coreboot_dmi_date);
Stefan Reinauerc6b21662012-04-03 16:02:54 -0700148 u32 version_offset = (u32)smbios_string_table_len(t->eos);
149 t->bios_version = smbios_add_string(t->eos, SPACES);
150 /* SMBIOS offsets start at 1 rather than 0 */
151 vboot_data->vbt10 = (u32)t->eos + (version_offset - 1);
152#endif
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200153
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800154 {
155 const struct cbfs_header *header;
156 u32 romsize = CONFIG_ROM_SIZE;
157 header = cbfs_get_header(CBFS_DEFAULT_MEDIA);
158 if (header != CBFS_HEADER_INVALID_ADDRESS)
159 romsize = ntohl(header->romsize);
160 t->bios_rom_size = (romsize / 65535) - 1;
161 }
162
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200163 t->system_bios_major_release = 4;
164 t->bios_characteristics =
165 BIOS_CHARACTERISTICS_PCI_SUPPORTED |
166#if CONFIG_CARDBUS_PLUGIN_SUPPORT
167 BIOS_CHARACTERISTICS_PC_CARD |
168#endif
169 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
170 BIOS_CHARACTERISTICS_UPGRADEABLE;
171
Vladimir Serbinenko822bc652014-01-03 15:55:40 +0100172#if CONFIG_HAVE_ACPI_TABLES
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200173 t->bios_characteristics_ext1 = BIOS_EXT1_CHARACTERISTICS_ACPI;
174#endif
175 t->bios_characteristics_ext2 = BIOS_EXT2_CHARACTERISTICS_TARGET;
176 len = t->length + smbios_string_table_len(t->eos);
177 *current += len;
178 return len;
179}
180
Christian Gmeinerac3aa092012-07-25 13:42:40 +0200181const char *__attribute__((weak)) smbios_mainboard_serial_number(void)
182{
183 return CONFIG_MAINBOARD_SERIAL_NUMBER;
184}
185
186const char *__attribute__((weak)) smbios_mainboard_version(void)
187{
188 return CONFIG_MAINBOARD_VERSION;
189}
190
Gerd Hoffmann06262742013-11-13 13:37:23 +0100191const char *__attribute__((weak)) smbios_mainboard_manufacturer(void)
192{
193 return CONFIG_MAINBOARD_SMBIOS_MANUFACTURER;
194}
195
196const char *__attribute__((weak)) smbios_mainboard_product_name(void)
197{
198 return CONFIG_MAINBOARD_SMBIOS_PRODUCT_NAME;
199}
200
201void __attribute__((weak)) smbios_mainboard_set_uuid(u8 *uuid)
202{
203 /* leave all zero */
204}
205
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200206static int smbios_write_type1(unsigned long *current, int handle)
207{
208 struct smbios_type1 *t = (struct smbios_type1 *)*current;
209 int len = sizeof(struct smbios_type1);
210
211 memset(t, 0, sizeof(struct smbios_type1));
212 t->type = SMBIOS_SYSTEM_INFORMATION;
213 t->handle = handle;
214 t->length = len - 2;
Gerd Hoffmann06262742013-11-13 13:37:23 +0100215 t->manufacturer = smbios_add_string(t->eos, smbios_mainboard_manufacturer());
216 t->product_name = smbios_add_string(t->eos, smbios_mainboard_product_name());
Christian Gmeinerac3aa092012-07-25 13:42:40 +0200217 t->serial_number = smbios_add_string(t->eos, smbios_mainboard_serial_number());
218 t->version = smbios_add_string(t->eos, smbios_mainboard_version());
Gerd Hoffmann06262742013-11-13 13:37:23 +0100219 smbios_mainboard_set_uuid(t->uuid);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200220 len = t->length + smbios_string_table_len(t->eos);
221 *current += len;
222 return len;
223}
224
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100225static int smbios_write_type2(unsigned long *current, int handle)
226{
227 struct smbios_type2 *t = (struct smbios_type2 *)*current;
228 int len = sizeof(struct smbios_type2);
229
230 memset(t, 0, sizeof(struct smbios_type2));
231 t->type = SMBIOS_BOARD_INFORMATION;
232 t->handle = handle;
233 t->length = len - 2;
234 t->manufacturer = smbios_add_string(t->eos, smbios_mainboard_manufacturer());
235 t->product_name = smbios_add_string(t->eos, smbios_mainboard_product_name());
236 t->serial_number = smbios_add_string(t->eos, smbios_mainboard_serial_number());
237 t->version = smbios_add_string(t->eos, smbios_mainboard_version());
238 len = t->length + smbios_string_table_len(t->eos);
239 *current += len;
240 return len;
241}
242
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200243static int smbios_write_type3(unsigned long *current, int handle)
244{
245 struct smbios_type3 *t = (struct smbios_type3 *)*current;
246 int len = sizeof(struct smbios_type3);
247
248 memset(t, 0, sizeof(struct smbios_type3));
249 t->type = SMBIOS_SYSTEM_ENCLOSURE;
250 t->handle = handle;
251 t->length = len - 2;
Peter Stuge4e7385b2012-10-04 21:18:13 +0200252 t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_SMBIOS_MANUFACTURER);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200253 t->bootup_state = SMBIOS_STATE_SAFE;
254 t->power_supply_state = SMBIOS_STATE_SAFE;
255 t->thermal_state = SMBIOS_STATE_SAFE;
Vladimir Serbinenkoa9db82f2014-10-16 13:21:47 +0200256 if(IS_ENABLED(CONFIG_SYSTEM_TYPE_LAPTOP)) {
257 t->_type = SMBIOS_ENCLOSURE_NOTEBOOK;
258 } else {
259 t->_type = SMBIOS_ENCLOSURE_DESKTOP;
260 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200261 t->security_status = SMBIOS_STATE_SAFE;
262 len = t->length + smbios_string_table_len(t->eos);
263 *current += len;
264 return len;
265}
266
267static int smbios_write_type4(unsigned long *current, int handle)
268{
269 struct cpuid_result res;
270 struct smbios_type4 *t = (struct smbios_type4 *)*current;
271 int len = sizeof(struct smbios_type4);
272
Rudolf Marek06253cd2012-02-25 23:51:12 +0100273 /* Provide sane defaults even for CPU without CPUID */
274 res.eax = res.edx = 0;
275 res.ebx = 0x10000;
276
277 if (cpu_have_cpuid()) {
278 res = cpuid(1);
279 }
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200280
281 memset(t, 0, sizeof(struct smbios_type4));
282 t->type = SMBIOS_PROCESSOR_INFORMATION;
283 t->handle = handle;
284 t->length = len - 2;
285 t->processor_id[0] = res.eax;
286 t->processor_id[1] = res.edx;
287 t->processor_manufacturer = smbios_cpu_vendor(t->eos);
288 t->processor_version = smbios_processor_name(t->eos);
Rudolf Marek06253cd2012-02-25 23:51:12 +0100289 t->processor_family = (res.eax > 0) ? 0x0c : 0x6;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200290 t->processor_type = 3; /* System Processor */
291 t->processor_upgrade = 0x06;
292 t->core_count = (res.ebx >> 16) & 0xff;
293 t->l1_cache_handle = 0xffff;
294 t->l2_cache_handle = 0xffff;
295 t->l3_cache_handle = 0xffff;
296 t->processor_upgrade = 1;
297 len = t->length + smbios_string_table_len(t->eos);
298 *current += len;
299 return len;
300}
301
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200302static int smbios_write_type11(unsigned long *current, int *handle)
Peter Stugec392b642013-07-06 19:51:12 +0200303{
304 struct smbios_type11 *t = (struct smbios_type11 *)*current;
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200305 int len;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100306 struct device *dev;
Peter Stugec392b642013-07-06 19:51:12 +0200307
308 memset(t, 0, sizeof *t);
309 t->type = SMBIOS_OEM_STRINGS;
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200310 t->handle = *handle;
Peter Stugec392b642013-07-06 19:51:12 +0200311 t->length = len = sizeof *t - 2;
312
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200313 for(dev = all_devices; dev; dev = dev->next) {
314 if (dev->ops && dev->ops->get_smbios_strings)
315 dev->ops->get_smbios_strings(dev, t);
316 }
317
318 if (t->count == 0) {
319 memset(t, 0, sizeof *t);
320 return 0;
321 }
Peter Stugec392b642013-07-06 19:51:12 +0200322
323 len += smbios_string_table_len(t->eos);
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200324
Peter Stugec392b642013-07-06 19:51:12 +0200325 *current += len;
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200326 (*handle)++;
Peter Stugec392b642013-07-06 19:51:12 +0200327 return len;
328}
329
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200330static int smbios_write_type32(unsigned long *current, int handle)
331{
332 struct smbios_type32 *t = (struct smbios_type32 *)*current;
333 int len = sizeof(struct smbios_type32);
334
335 memset(t, 0, sizeof(struct smbios_type32));
336 t->type = SMBIOS_SYSTEM_BOOT_INFORMATION;
337 t->handle = handle;
338 t->length = len - 2;
339 *current += len;
340 return len;
341}
342
Duncan Laurie21a78702013-05-23 14:17:05 -0700343int smbios_write_type41(unsigned long *current, int *handle,
344 const char *name, u8 instance, u16 segment,
345 u8 bus, u8 device, u8 function)
346{
347 struct smbios_type41 *t = (struct smbios_type41 *)*current;
348 int len = sizeof(struct smbios_type41);
349
350 memset(t, 0, sizeof(struct smbios_type41));
351 t->type = SMBIOS_ONBOARD_DEVICES_EXTENDED_INFORMATION;
352 t->handle = *handle;
353 t->length = len - 2;
354 t->reference_designation = smbios_add_string(t->eos, name);
355 t->device_type = SMBIOS_DEVICE_TYPE_OTHER;
356 t->device_status = 1;
357 t->device_type_instance = instance;
358 t->segment_group_number = segment;
359 t->bus_number = bus;
360 t->device_number = device;
361 t->function_number = function;
362
363 len = t->length + smbios_string_table_len(t->eos);
364 *current += len;
365 *handle += 1;
366 return len;
367}
368
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200369static int smbios_write_type127(unsigned long *current, int handle)
370{
371 struct smbios_type127 *t = (struct smbios_type127 *)*current;
372 int len = sizeof(struct smbios_type127);
373
374 memset(t, 0, sizeof(struct smbios_type127));
375 t->type = SMBIOS_END_OF_TABLE;
376 t->handle = handle;
377 t->length = len - 2;
378 *current += len;
379 return len;
380}
381
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100382static int smbios_walk_device_tree(struct device *tree, int *handle, unsigned long *current)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200383{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100384 struct device *dev;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200385 int len = 0;
386
387 for(dev = tree; dev; dev = dev->next) {
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200388 printk(BIOS_INFO, "%s (%s)\n", dev_path(dev), dev_name(dev));
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200389
390 if (dev->ops && dev->ops->get_smbios_data)
391 len += dev->ops->get_smbios_data(dev, handle, current);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200392 }
393 return len;
394}
395
396unsigned long smbios_write_tables(unsigned long current)
397{
398 struct smbios_entry *se;
399 unsigned long tables;
400 int len, handle = 0;
401
402 current = ALIGN(current, 16);
403 printk(BIOS_DEBUG, "%s: %08lx\n", __func__, current);
404
405 se = (struct smbios_entry *)current;
406 current += sizeof(struct smbios_entry);
407 current = ALIGN(current, 16);
408
409 tables = current;
410 len = smbios_write_type0(&current, handle++);
411 len += smbios_write_type1(&current, handle++);
Vladimir Serbinenko47089f22014-03-02 19:14:44 +0100412 len += smbios_write_type2(&current, handle++);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200413 len += smbios_write_type3(&current, handle++);
414 len += smbios_write_type4(&current, handle++);
Vladimir Serbinenko6abb33c2014-08-27 23:42:45 +0200415 len += smbios_write_type11(&current, &handle);
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700416#if CONFIG_ELOG
417 len += elog_smbios_write_type15(&current, handle++);
418#endif
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200419 len += smbios_write_type32(&current, handle++);
420
421 len += smbios_walk_device_tree(all_devices, &handle, &current);
422
423 len += smbios_write_type127(&current, handle++);
424
425 memset(se, 0, sizeof(struct smbios_entry));
426 memcpy(se->anchor, "_SM_", 4);
427 se->length = sizeof(struct smbios_entry);
428 se->major_version = 2;
429 se->minor_version = 7;
430 se->max_struct_size = 24;
431 se->struct_count = handle;
432 memcpy(se->intermediate_anchor_string, "_DMI_", 5);
433
434 se->struct_table_address = (u32)tables;
435 se->struct_table_length = len;
436
437 se->intermediate_checksum = smbios_checksum((u8 *)se + 0x10,
438 sizeof(struct smbios_entry) - 0x10);
439 se->checksum = smbios_checksum((u8 *)se, sizeof(struct smbios_entry));
440 return current;
441}