1 // smbios table generation (on emulators)
2 // DO NOT ADD NEW FEATURES HERE. (See paravirt.c / biostables.c instead.)
4 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
5 // Copyright (C) 2006 Fabrice Bellard
7 // This file may be distributed under the terms of the GNU LGPLv3 license.
9 #include "config.h" // CONFIG_*
10 #include "malloc.h" // free
11 #include "output.h" // dprintf
12 #include "paravirt.h" // RamSize
13 #include "romfile.h" // romfile_findprefix
14 #include "std/smbios.h" // struct smbios_entry_point
15 #include "string.h" // memset
16 #include "util.h" // MaxCountCPUs
17 #include "x86.h" // cpuid
20 smbios_entry_point_setup(u16 max_structure_size,
21 u16 structure_table_length,
22 void *structure_table_address,
23 u16 number_of_structures)
26 if (structure_table_length <= BUILD_MAX_SMBIOS_FSEG)
27 // Table is small enough for f-seg - allocate there. This
28 // works around a bug in JunOS (at least for small SMBIOS tables).
29 finaltable = malloc_fseg(structure_table_length);
31 finaltable = malloc_high(structure_table_length);
36 memcpy(finaltable, structure_table_address, structure_table_length);
38 struct smbios_entry_point ep;
39 memset(&ep, 0, sizeof(ep));
40 ep.signature = SMBIOS_SIGNATURE;
42 ep.smbios_major_version = 2;
43 ep.smbios_minor_version = 4;
44 ep.max_structure_size = max_structure_size;
45 memcpy(ep.intermediate_anchor_string, "_DMI_", 5);
47 ep.structure_table_length = structure_table_length;
48 ep.structure_table_address = (u32)finaltable;
49 ep.number_of_structures = number_of_structures;
50 ep.smbios_bcd_revision = 0x24;
52 ep.checksum -= checksum(&ep, 0x10);
54 ep.intermediate_checksum -= checksum((void*)&ep + 0x10, ep.length - 0x10);
60 get_field(int type, int offset, void *dest)
63 snprintf(name, sizeof(name), "smbios/field%d-%d", type, offset);
64 struct romfile_s *file = romfile_find(name);
67 file->copy(file, dest, file->size);
72 get_external(int type, char **p, unsigned *nr_structs,
73 unsigned *max_struct_size, char *end)
75 static u64 used_bitmap[4] = { 0 };
78 /* Check if we've already reported these tables */
79 if (used_bitmap[(type >> 6) & 0x3] & (1ULL << (type & 0x3f)))
82 /* Don't introduce spurious end markers */
87 snprintf(prefix, sizeof(prefix), "smbios/table%d-", type);
88 struct romfile_s *file = NULL;
90 file = romfile_findprefix(prefix, file);
94 if (end - *p < file->size) {
99 struct smbios_structure_header *header = (void*)*p;
100 file->copy(file, header, file->size);
103 /* Entries end with a double NULL char, if there's a string at
104 * the end (length is greater than formatted length), the string
105 * terminator provides the first NULL. */
108 if (header->length >= file->size) {
114 if (*p - (char*)header > *max_struct_size)
115 *max_struct_size = *p - (char*)header;
121 /* Mark that we've reported on this type */
122 used_bitmap[(type >> 6) & 0x3] |= (1ULL << (type & 0x3f));
126 #define load_str_field_with_default(type, field, def) \
128 size = get_field(type, offsetof(struct smbios_type_##type, \
131 /* zero-length string, skip to avoid bogus end marker */ \
133 } else if (size > 1) { \
135 p->field = ++str_index; \
137 memcpy(end, def, sizeof(def)); \
138 end += sizeof(def); \
139 p->field = ++str_index; \
143 #define load_str_field_or_skip(type, field) \
145 size = get_field(type, offsetof(struct smbios_type_##type, \
149 p->field = ++str_index; \
155 #define set_field_with_default(type, field, def) \
157 if (!get_field(type, offsetof(struct smbios_type_##type, \
158 field), &p->field)) { \
163 /* Type 0 -- BIOS Information */
164 #define RELEASE_DATE_STR "01/01/2011"
166 smbios_init_type_0(void *start)
168 struct smbios_type_0 *p = (struct smbios_type_0 *)start;
169 char *end = (char *)start + sizeof(struct smbios_type_0);
174 p->header.length = sizeof(struct smbios_type_0);
175 p->header.handle = 0;
177 load_str_field_with_default(0, vendor_str, BUILD_APPNAME);
178 load_str_field_with_default(0, bios_version_str, BUILD_APPNAME);
180 p->bios_starting_address_segment = 0xe800;
182 load_str_field_with_default(0, bios_release_date_str, RELEASE_DATE_STR);
184 p->bios_rom_size = 0; /* FIXME */
186 if (!get_field(0, offsetof(struct smbios_type_0, bios_characteristics),
187 &p->bios_characteristics)) {
188 memset(p->bios_characteristics, 0, 8);
189 /* BIOS characteristics not supported */
190 p->bios_characteristics[0] = 0x08;
193 if (!get_field(0, offsetof(struct smbios_type_0,
194 bios_characteristics_extension_bytes),
195 &p->bios_characteristics_extension_bytes)) {
196 p->bios_characteristics_extension_bytes[0] = 0;
197 /* Enable targeted content distribution. Needed for SVVP */
198 p->bios_characteristics_extension_bytes[1] = 4;
201 set_field_with_default(0, system_bios_major_release, 1);
202 set_field_with_default(0, system_bios_minor_release, 0);
203 set_field_with_default(0, embedded_controller_major_release, 0xff);
204 set_field_with_default(0, embedded_controller_minor_release, 0xff);
212 /* Type 1 -- System Information */
214 smbios_init_type_1(void *start)
216 struct smbios_type_1 *p = (struct smbios_type_1 *)start;
217 char *end = (char *)start + sizeof(struct smbios_type_1);
222 p->header.length = sizeof(struct smbios_type_1);
223 p->header.handle = 0x100;
225 load_str_field_with_default(1, manufacturer_str, BUILD_APPNAME);
226 load_str_field_with_default(1, product_name_str, BUILD_APPNAME);
227 load_str_field_or_skip(1, version_str);
228 load_str_field_or_skip(1, serial_number_str);
230 if (!get_field(1, offsetof(struct smbios_type_1, uuid), &p->uuid))
231 memset(p->uuid, 0, 16);
233 set_field_with_default(1, wake_up_type, 0x06); /* power switch */
235 load_str_field_or_skip(1, sku_number_str);
236 load_str_field_or_skip(1, family_str);
248 /* Type 3 -- System Enclosure */
250 smbios_init_type_3(void *start)
252 struct smbios_type_3 *p = (struct smbios_type_3 *)start;
253 char *end = (char *)start + sizeof(struct smbios_type_3);
258 p->header.length = sizeof(struct smbios_type_3);
259 p->header.handle = 0x300;
261 load_str_field_with_default(3, manufacturer_str, BUILD_APPNAME);
262 set_field_with_default(3, type, 0x01); /* other */
264 load_str_field_or_skip(3, version_str);
265 load_str_field_or_skip(3, serial_number_str);
266 load_str_field_or_skip(3, asset_tag_number_str);
268 set_field_with_default(3, boot_up_state, 0x03); /* safe */
269 set_field_with_default(3, power_supply_state, 0x03); /* safe */
270 set_field_with_default(3, thermal_state, 0x03); /* safe */
271 set_field_with_default(3, security_status, 0x02); /* unknown */
273 set_field_with_default(3, oem_defined, 0);
274 set_field_with_default(3, height, 0);
275 set_field_with_default(3, number_of_power_cords, 0);
276 set_field_with_default(3, contained_element_count, 0);
288 /* Type 4 -- Processor Information */
290 smbios_init_type_4(void *start, unsigned int cpu_number)
292 struct smbios_type_4 *p = (struct smbios_type_4 *)start;
293 char *end = (char *)start + sizeof(struct smbios_type_4);
299 p->header.length = sizeof(struct smbios_type_4);
300 p->header.handle = 0x400 + cpu_number;
302 size = get_field(4, offsetof(struct smbios_type_4, socket_designation_str),
305 snprintf(name + size - 1, sizeof(name) - size, "%2x", cpu_number);
307 snprintf(name, sizeof(name), "CPU%2x", cpu_number);
309 memcpy(end, name, strlen(name) + 1);
310 end += strlen(name) + 1;
311 p->socket_designation_str = ++str_index;
313 set_field_with_default(4, processor_type, 0x03); /* CPU */
314 set_field_with_default(4, processor_family, 0x01); /* other */
316 load_str_field_with_default(4, processor_manufacturer_str, BUILD_APPNAME);
318 if (!get_field(4, offsetof(struct smbios_type_4, processor_id)
319 , p->processor_id)) {
320 u32 cpuid_signature, ebx, ecx, cpuid_features;
321 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
322 p->processor_id[0] = cpuid_signature;
323 p->processor_id[1] = cpuid_features;
326 load_str_field_or_skip(4, processor_version_str);
327 set_field_with_default(4, voltage, 0);
328 set_field_with_default(4, external_clock, 0);
330 set_field_with_default(4, max_speed, 2000);
331 set_field_with_default(4, current_speed, 2000);
333 set_field_with_default(4, status, 0x41); /* socket populated, CPU enabled */
334 set_field_with_default(4, processor_upgrade, 0x01); /* other */
336 /* cache information structure not provided */
337 p->l1_cache_handle = 0xffff;
338 p->l2_cache_handle = 0xffff;
339 p->l3_cache_handle = 0xffff;
351 /* Type 16 -- Physical Memory Array */
353 smbios_init_type_16(void *start, u32 memory_size_mb, int nr_mem_devs)
355 struct smbios_type_16 *p = (struct smbios_type_16*)start;
358 p->header.length = sizeof(struct smbios_type_16);
359 p->header.handle = 0x1000;
361 set_field_with_default(16, location, 0x01); /* other */
362 set_field_with_default(16, use, 0x03); /* system memory */
363 /* Multi-bit ECC to make Microsoft happy */
364 set_field_with_default(16, error_correction, 0x06);
365 /* 0x80000000 = unknown, accept sizes < 2TB - TODO multiple arrays */
366 p->maximum_capacity = memory_size_mb < 2 << 20 ?
367 memory_size_mb << 10 : 0x80000000;
368 p->memory_error_information_handle = 0xfffe; /* none provided */
369 p->number_of_memory_devices = nr_mem_devs;
371 start += sizeof(struct smbios_type_16);
377 /* Type 17 -- Memory Device */
379 smbios_init_type_17(void *start, u32 size_mb, int instance)
381 struct smbios_type_17 *p = (struct smbios_type_17 *)start;
382 char *end = (char *)start + sizeof(struct smbios_type_17);
388 p->header.length = sizeof(struct smbios_type_17);
389 p->header.handle = 0x1100 + instance;
391 p->physical_memory_array_handle = 0x1000;
392 set_field_with_default(17, total_width, 64);
393 set_field_with_default(17, data_width, 64);
394 /* TODO: should assert in case something is wrong ASSERT((memory_size_mb & ~0x7fff) == 0); */
396 set_field_with_default(17, form_factor, 0x09); /* DIMM */
399 size = get_field(17, offsetof(struct smbios_type_17, device_locator_str),
402 snprintf(name + size - 1, sizeof(name) - size, "%d", instance);
404 snprintf(name, sizeof(name), "DIMM %d", instance);
406 memcpy(end, name, strlen(name) + 1);
407 end += strlen(name) + 1;
408 p->device_locator_str = ++str_index;
410 load_str_field_or_skip(17, bank_locator_str);
411 set_field_with_default(17, memory_type, 0x07); /* RAM */
412 set_field_with_default(17, type_detail, 0);
424 /* Type 19 -- Memory Array Mapped Address */
426 smbios_init_type_19(void *start, u32 start_mb, u32 size_mb, int instance)
428 struct smbios_type_19 *p = (struct smbios_type_19 *)start;
431 p->header.length = sizeof(struct smbios_type_19);
432 p->header.handle = 0x1300 + instance;
434 p->starting_address = start_mb << 10;
435 p->ending_address = p->starting_address + (size_mb << 10) - 1;
436 p->memory_array_handle = 0x1000;
437 p->partition_width = 1;
439 start += sizeof(struct smbios_type_19);
445 /* Type 20 -- Memory Device Mapped Address */
447 smbios_init_type_20(void *start, u32 start_mb, u32 size_mb, int instance,
448 int dev_handle, int array_handle)
450 struct smbios_type_20 *p = (struct smbios_type_20 *)start;
453 p->header.length = sizeof(struct smbios_type_20);
454 p->header.handle = 0x1400 + instance;
456 p->starting_address = start_mb << 10;
457 p->ending_address = p->starting_address + (size_mb << 10) - 1;
458 p->memory_device_handle = 0x1100 + dev_handle;
459 p->memory_array_mapped_address_handle = 0x1300 + array_handle;
460 p->partition_row_position = 1;
461 p->interleave_position = 0;
462 p->interleaved_data_depth = 0;
464 start += sizeof(struct smbios_type_20);
470 /* Type 32 -- System Boot Information */
472 smbios_init_type_32(void *start)
474 struct smbios_type_32 *p = (struct smbios_type_32 *)start;
477 p->header.length = sizeof(struct smbios_type_32);
478 p->header.handle = 0x2000;
479 memset(p->reserved, 0, 6);
480 set_field_with_default(32, boot_status, 0); /* no errors detected */
482 start += sizeof(struct smbios_type_32);
488 /* Type 127 -- End of Table */
490 smbios_init_type_127(void *start)
492 struct smbios_type_127 *p = (struct smbios_type_127 *)start;
494 p->header.type = 127;
495 p->header.length = sizeof(struct smbios_type_127);
496 p->header.handle = 0x7f00;
498 start += sizeof(struct smbios_type_127);
504 #define TEMPSMBIOSSIZE (32 * 1024)
507 smbios_legacy_setup(void)
512 dprintf(3, "init SMBIOS tables\n");
514 char *start = malloc_tmphigh(TEMPSMBIOSSIZE);
519 memset(start, 0, TEMPSMBIOSSIZE);
521 u32 nr_structs = 0, max_struct_size = 0;
523 char *end = start + TEMPSMBIOSSIZE - sizeof(struct smbios_type_127);
525 #define add_struct(type, args...) \
527 if (!get_external(type, &p, &nr_structs, &max_struct_size, end)) { \
528 q = smbios_init_type_##type(args); \
530 if ((q - p) > max_struct_size) \
531 max_struct_size = q - p; \
541 for (cpu_num = 1; cpu_num <= MaxCountCPUs; cpu_num++)
542 add_struct(4, p, cpu_num);
544 int ram_mb = (RamSize + RamSizeOver4G) >> 20;
545 int nr_mem_devs = (ram_mb + 0x3fff) >> 14;
546 add_struct(16, p, ram_mb, nr_mem_devs);
549 for (i = 0; i < nr_mem_devs; i++) {
550 u32 dev_mb = ((i == (nr_mem_devs - 1))
551 ? (((ram_mb - 1) & 0x3fff) + 1)
553 add_struct(17, p, dev_mb, i);
556 add_struct(19, p, 0, RamSize >> 20, 0);
558 add_struct(19, p, 4096, RamSizeOver4G >> 20, 1);
560 add_struct(20, p, 0, RamSize >> 20, 0, 0, 0);
563 for (j = 1, i = 0; i < nr_mem_devs; i++, j++) {
564 u32 dev_mb = ((i == (nr_mem_devs - 1))
565 ? (((ram_mb - 1) & 0x3fff) + 1)
568 dev_mb -= RamSize >> 20;
570 add_struct(20, p, start_mb, dev_mb, j, i, 1);
576 /* Add any remaining provided entries before the end marker */
577 for (i = 0; i < 256; i++)
578 get_external(i, &p, &nr_structs, &max_struct_size, end);
583 smbios_entry_point_setup(max_struct_size, p - start, start, nr_structs);