smbios: add smbios 3.0 support
[qemu/ar7.git] / hw / smbios / smbios.c
blobb81a1d349d4f076669947d019ddf47c4e5b7ffd2
1 /*
2 * SMBIOS Support
4 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5 * Copyright (C) 2013 Red Hat, Inc.
7 * Authors:
8 * Alex Williamson <alex.williamson@hp.com>
9 * Markus Armbruster <armbru@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
18 #include "qemu/config-file.h"
19 #include "qemu/error-report.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/cpus.h"
22 #include "hw/smbios/smbios.h"
23 #include "hw/loader.h"
24 #include "exec/cpu-common.h"
26 /* legacy structures and constants for <= 2.0 machines */
27 struct smbios_header {
28 uint16_t length;
29 uint8_t type;
30 } QEMU_PACKED;
32 struct smbios_field {
33 struct smbios_header header;
34 uint8_t type;
35 uint16_t offset;
36 uint8_t data[];
37 } QEMU_PACKED;
39 struct smbios_table {
40 struct smbios_header header;
41 uint8_t data[];
42 } QEMU_PACKED;
44 #define SMBIOS_FIELD_ENTRY 0
45 #define SMBIOS_TABLE_ENTRY 1
47 static uint8_t *smbios_entries;
48 static size_t smbios_entries_len;
49 static bool smbios_legacy = true;
50 static bool smbios_uuid_encoded = true;
51 /* end: legacy structures & constants for <= 2.0 machines */
54 static uint8_t *smbios_tables;
55 static size_t smbios_tables_len;
56 static unsigned smbios_table_max;
57 static unsigned smbios_table_cnt;
58 static SmbiosEntryPointType smbios_ep_type = SMBIOS_ENTRY_POINT_21;
60 static SmbiosEntryPoint ep;
62 static int smbios_type4_count = 0;
63 static bool smbios_immutable;
64 static bool smbios_have_defaults;
65 static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
67 static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
68 static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
70 static struct {
71 const char *vendor, *version, *date;
72 bool have_major_minor, uefi;
73 uint8_t major, minor;
74 } type0;
76 static struct {
77 const char *manufacturer, *product, *version, *serial, *sku, *family;
78 /* uuid is in qemu_uuid[] */
79 } type1;
81 static struct {
82 const char *manufacturer, *product, *version, *serial, *asset, *location;
83 } type2;
85 static struct {
86 const char *manufacturer, *version, *serial, *asset, *sku;
87 } type3;
89 static struct {
90 const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
91 } type4;
93 static struct {
94 const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
95 uint16_t speed;
96 } type17;
98 static QemuOptsList qemu_smbios_opts = {
99 .name = "smbios",
100 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
101 .desc = {
103 * no elements => accept any params
104 * validation will happen later
106 { /* end of list */ }
110 static const QemuOptDesc qemu_smbios_file_opts[] = {
112 .name = "file",
113 .type = QEMU_OPT_STRING,
114 .help = "binary file containing an SMBIOS element",
116 { /* end of list */ }
119 static const QemuOptDesc qemu_smbios_type0_opts[] = {
121 .name = "type",
122 .type = QEMU_OPT_NUMBER,
123 .help = "SMBIOS element type",
125 .name = "vendor",
126 .type = QEMU_OPT_STRING,
127 .help = "vendor name",
129 .name = "version",
130 .type = QEMU_OPT_STRING,
131 .help = "version number",
133 .name = "date",
134 .type = QEMU_OPT_STRING,
135 .help = "release date",
137 .name = "release",
138 .type = QEMU_OPT_STRING,
139 .help = "revision number",
141 .name = "uefi",
142 .type = QEMU_OPT_BOOL,
143 .help = "uefi support",
145 { /* end of list */ }
148 static const QemuOptDesc qemu_smbios_type1_opts[] = {
150 .name = "type",
151 .type = QEMU_OPT_NUMBER,
152 .help = "SMBIOS element type",
154 .name = "manufacturer",
155 .type = QEMU_OPT_STRING,
156 .help = "manufacturer name",
158 .name = "product",
159 .type = QEMU_OPT_STRING,
160 .help = "product name",
162 .name = "version",
163 .type = QEMU_OPT_STRING,
164 .help = "version number",
166 .name = "serial",
167 .type = QEMU_OPT_STRING,
168 .help = "serial number",
170 .name = "uuid",
171 .type = QEMU_OPT_STRING,
172 .help = "UUID",
174 .name = "sku",
175 .type = QEMU_OPT_STRING,
176 .help = "SKU number",
178 .name = "family",
179 .type = QEMU_OPT_STRING,
180 .help = "family name",
182 { /* end of list */ }
185 static const QemuOptDesc qemu_smbios_type2_opts[] = {
187 .name = "type",
188 .type = QEMU_OPT_NUMBER,
189 .help = "SMBIOS element type",
191 .name = "manufacturer",
192 .type = QEMU_OPT_STRING,
193 .help = "manufacturer name",
195 .name = "product",
196 .type = QEMU_OPT_STRING,
197 .help = "product name",
199 .name = "version",
200 .type = QEMU_OPT_STRING,
201 .help = "version number",
203 .name = "serial",
204 .type = QEMU_OPT_STRING,
205 .help = "serial number",
207 .name = "asset",
208 .type = QEMU_OPT_STRING,
209 .help = "asset tag number",
211 .name = "location",
212 .type = QEMU_OPT_STRING,
213 .help = "location in chassis",
215 { /* end of list */ }
218 static const QemuOptDesc qemu_smbios_type3_opts[] = {
220 .name = "type",
221 .type = QEMU_OPT_NUMBER,
222 .help = "SMBIOS element type",
224 .name = "manufacturer",
225 .type = QEMU_OPT_STRING,
226 .help = "manufacturer name",
228 .name = "version",
229 .type = QEMU_OPT_STRING,
230 .help = "version number",
232 .name = "serial",
233 .type = QEMU_OPT_STRING,
234 .help = "serial number",
236 .name = "asset",
237 .type = QEMU_OPT_STRING,
238 .help = "asset tag number",
240 .name = "sku",
241 .type = QEMU_OPT_STRING,
242 .help = "SKU number",
244 { /* end of list */ }
247 static const QemuOptDesc qemu_smbios_type4_opts[] = {
249 .name = "type",
250 .type = QEMU_OPT_NUMBER,
251 .help = "SMBIOS element type",
253 .name = "sock_pfx",
254 .type = QEMU_OPT_STRING,
255 .help = "socket designation string prefix",
257 .name = "manufacturer",
258 .type = QEMU_OPT_STRING,
259 .help = "manufacturer name",
261 .name = "version",
262 .type = QEMU_OPT_STRING,
263 .help = "version number",
265 .name = "serial",
266 .type = QEMU_OPT_STRING,
267 .help = "serial number",
269 .name = "asset",
270 .type = QEMU_OPT_STRING,
271 .help = "asset tag number",
273 .name = "part",
274 .type = QEMU_OPT_STRING,
275 .help = "part number",
277 { /* end of list */ }
280 static const QemuOptDesc qemu_smbios_type17_opts[] = {
282 .name = "type",
283 .type = QEMU_OPT_NUMBER,
284 .help = "SMBIOS element type",
286 .name = "loc_pfx",
287 .type = QEMU_OPT_STRING,
288 .help = "device locator string prefix",
290 .name = "bank",
291 .type = QEMU_OPT_STRING,
292 .help = "bank locator string",
294 .name = "manufacturer",
295 .type = QEMU_OPT_STRING,
296 .help = "manufacturer name",
298 .name = "serial",
299 .type = QEMU_OPT_STRING,
300 .help = "serial number",
302 .name = "asset",
303 .type = QEMU_OPT_STRING,
304 .help = "asset tag number",
306 .name = "part",
307 .type = QEMU_OPT_STRING,
308 .help = "part number",
310 .name = "speed",
311 .type = QEMU_OPT_NUMBER,
312 .help = "maximum capable speed",
314 { /* end of list */ }
317 static void smbios_register_config(void)
319 qemu_add_opts(&qemu_smbios_opts);
322 machine_init(smbios_register_config);
324 static void smbios_validate_table(void)
326 uint32_t expect_t4_count = smbios_legacy ? smp_cpus : smbios_smp_sockets;
328 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
329 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
330 expect_t4_count, smbios_type4_count);
331 exit(1);
336 /* legacy setup functions for <= 2.0 machines */
337 static void smbios_add_field(int type, int offset, const void *data, size_t len)
339 struct smbios_field *field;
341 if (!smbios_entries) {
342 smbios_entries_len = sizeof(uint16_t);
343 smbios_entries = g_malloc0(smbios_entries_len);
345 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
346 sizeof(*field) + len);
347 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
348 field->header.type = SMBIOS_FIELD_ENTRY;
349 field->header.length = cpu_to_le16(sizeof(*field) + len);
351 field->type = type;
352 field->offset = cpu_to_le16(offset);
353 memcpy(field->data, data, len);
355 smbios_entries_len += sizeof(*field) + len;
356 (*(uint16_t *)smbios_entries) =
357 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
360 static void smbios_maybe_add_str(int type, int offset, const char *data)
362 if (data) {
363 smbios_add_field(type, offset, data, strlen(data) + 1);
367 static void smbios_build_type_0_fields(void)
369 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
370 type0.vendor);
371 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
372 type0.version);
373 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
374 bios_release_date_str),
375 type0.date);
376 if (type0.have_major_minor) {
377 smbios_add_field(0, offsetof(struct smbios_type_0,
378 system_bios_major_release),
379 &type0.major, 1);
380 smbios_add_field(0, offsetof(struct smbios_type_0,
381 system_bios_minor_release),
382 &type0.minor, 1);
386 static void smbios_build_type_1_fields(void)
388 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
389 type1.manufacturer);
390 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
391 type1.product);
392 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
393 type1.version);
394 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
395 type1.serial);
396 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
397 type1.sku);
398 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
399 type1.family);
400 if (qemu_uuid_set) {
401 /* We don't encode the UUID in the "wire format" here because this
402 * function is for legacy mode and needs to keep the guest ABI, and
403 * because we don't know what's the SMBIOS version advertised by the
404 * BIOS.
406 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
407 qemu_uuid, 16);
411 uint8_t *smbios_get_table_legacy(size_t *length)
413 if (!smbios_legacy) {
414 *length = 0;
415 return NULL;
418 if (!smbios_immutable) {
419 smbios_build_type_0_fields();
420 smbios_build_type_1_fields();
421 smbios_validate_table();
422 smbios_immutable = true;
424 *length = smbios_entries_len;
425 return smbios_entries;
427 /* end: legacy setup functions for <= 2.0 machines */
430 static bool smbios_skip_table(uint8_t type, bool required_table)
432 if (test_bit(type, have_binfile_bitmap)) {
433 return true; /* user provided their own binary blob(s) */
435 if (test_bit(type, have_fields_bitmap)) {
436 return false; /* user provided fields via command line */
438 if (smbios_have_defaults && required_table) {
439 return false; /* we're building tables, and this one's required */
441 return true;
444 #define SMBIOS_BUILD_TABLE_PRE(tbl_type, tbl_handle, tbl_required) \
445 struct smbios_type_##tbl_type *t; \
446 size_t t_off; /* table offset into smbios_tables */ \
447 int str_index = 0; \
448 do { \
449 /* should we skip building this table ? */ \
450 if (smbios_skip_table(tbl_type, tbl_required)) { \
451 return; \
454 /* use offset of table t within smbios_tables */ \
455 /* (pointer must be updated after each realloc) */ \
456 t_off = smbios_tables_len; \
457 smbios_tables_len += sizeof(*t); \
458 smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
459 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
461 t->header.type = tbl_type; \
462 t->header.length = sizeof(*t); \
463 t->header.handle = cpu_to_le16(tbl_handle); \
464 } while (0)
466 #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
467 do { \
468 int len = (value != NULL) ? strlen(value) + 1 : 0; \
469 if (len > 1) { \
470 smbios_tables = g_realloc(smbios_tables, \
471 smbios_tables_len + len); \
472 memcpy(smbios_tables + smbios_tables_len, value, len); \
473 smbios_tables_len += len; \
474 /* update pointer post-realloc */ \
475 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
476 t->field = ++str_index; \
477 } else { \
478 t->field = 0; \
480 } while (0)
482 #define SMBIOS_BUILD_TABLE_POST \
483 do { \
484 size_t term_cnt, t_size; \
486 /* add '\0' terminator (add two if no strings defined) */ \
487 term_cnt = (str_index == 0) ? 2 : 1; \
488 smbios_tables = g_realloc(smbios_tables, \
489 smbios_tables_len + term_cnt); \
490 memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
491 smbios_tables_len += term_cnt; \
493 /* update smbios max. element size */ \
494 t_size = smbios_tables_len - t_off; \
495 if (t_size > smbios_table_max) { \
496 smbios_table_max = t_size; \
499 /* update smbios element count */ \
500 smbios_table_cnt++; \
501 } while (0)
503 static void smbios_build_type_0_table(void)
505 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
507 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
508 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
510 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
512 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
514 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
516 t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
517 t->bios_characteristics_extension_bytes[0] = 0;
518 t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
519 if (type0.uefi) {
520 t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
523 if (type0.have_major_minor) {
524 t->system_bios_major_release = type0.major;
525 t->system_bios_minor_release = type0.minor;
526 } else {
527 t->system_bios_major_release = 0;
528 t->system_bios_minor_release = 0;
531 /* hardcoded in SeaBIOS */
532 t->embedded_controller_major_release = 0xFF;
533 t->embedded_controller_minor_release = 0xFF;
535 SMBIOS_BUILD_TABLE_POST;
538 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
539 * format specified by SMBIOS version 2.6.
541 static void smbios_encode_uuid(struct smbios_uuid *uuid, const uint8_t *buf)
543 memcpy(uuid, buf, 16);
544 if (smbios_uuid_encoded) {
545 uuid->time_low = bswap32(uuid->time_low);
546 uuid->time_mid = bswap16(uuid->time_mid);
547 uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
551 static void smbios_build_type_1_table(void)
553 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
555 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
556 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
557 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
558 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
559 if (qemu_uuid_set) {
560 smbios_encode_uuid(&t->uuid, qemu_uuid);
561 } else {
562 memset(&t->uuid, 0, 16);
564 t->wake_up_type = 0x06; /* power switch */
565 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
566 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
568 SMBIOS_BUILD_TABLE_POST;
571 static void smbios_build_type_2_table(void)
573 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
575 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
576 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
577 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
578 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
579 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
580 t->feature_flags = 0x01; /* Motherboard */
581 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
582 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
583 t->board_type = 0x0A; /* Motherboard */
584 t->contained_element_count = 0;
586 SMBIOS_BUILD_TABLE_POST;
589 static void smbios_build_type_3_table(void)
591 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
593 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
594 t->type = 0x01; /* Other */
595 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
596 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
597 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
598 t->boot_up_state = 0x03; /* Safe */
599 t->power_supply_state = 0x03; /* Safe */
600 t->thermal_state = 0x03; /* Safe */
601 t->security_status = 0x02; /* Unknown */
602 t->oem_defined = cpu_to_le32(0);
603 t->height = 0;
604 t->number_of_power_cords = 0;
605 t->contained_element_count = 0;
606 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
608 SMBIOS_BUILD_TABLE_POST;
611 static void smbios_build_type_4_table(unsigned instance)
613 char sock_str[128];
615 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
617 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
618 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
619 t->processor_type = 0x03; /* CPU */
620 t->processor_family = 0x01; /* Other */
621 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
622 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
623 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
624 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
625 t->voltage = 0;
626 t->external_clock = cpu_to_le16(0); /* Unknown */
627 /* SVVP requires max_speed and current_speed to not be unknown. */
628 t->max_speed = cpu_to_le16(2000); /* 2000 MHz */
629 t->current_speed = cpu_to_le16(2000); /* 2000 MHz */
630 t->status = 0x41; /* Socket populated, CPU enabled */
631 t->processor_upgrade = 0x01; /* Other */
632 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
633 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
634 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
635 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
636 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
637 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
638 t->core_count = t->core_enabled = smp_cores;
639 t->thread_count = smp_threads;
640 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
641 t->processor_family2 = cpu_to_le16(0x01); /* Other */
643 SMBIOS_BUILD_TABLE_POST;
644 smbios_type4_count++;
647 #define ONE_KB ((ram_addr_t)1 << 10)
648 #define ONE_MB ((ram_addr_t)1 << 20)
649 #define ONE_GB ((ram_addr_t)1 << 30)
651 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
653 static void smbios_build_type_16_table(unsigned dimm_cnt)
655 uint64_t size_kb;
657 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
659 t->location = 0x01; /* Other */
660 t->use = 0x03; /* System memory */
661 t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
662 size_kb = QEMU_ALIGN_UP(ram_size, ONE_KB) / ONE_KB;
663 if (size_kb < MAX_T16_STD_SZ) {
664 t->maximum_capacity = cpu_to_le32(size_kb);
665 t->extended_maximum_capacity = cpu_to_le64(0);
666 } else {
667 t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
668 t->extended_maximum_capacity = cpu_to_le64(ram_size);
670 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
671 t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
673 SMBIOS_BUILD_TABLE_POST;
676 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
677 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
679 static void smbios_build_type_17_table(unsigned instance, uint64_t size)
681 char loc_str[128];
682 uint64_t size_mb;
684 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
686 t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
687 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
688 t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
689 t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
690 size_mb = QEMU_ALIGN_UP(size, ONE_MB) / ONE_MB;
691 if (size_mb < MAX_T17_STD_SZ) {
692 t->size = cpu_to_le16(size_mb);
693 t->extended_size = cpu_to_le32(0);
694 } else {
695 assert(size_mb < MAX_T17_EXT_SZ);
696 t->size = cpu_to_le16(MAX_T17_STD_SZ);
697 t->extended_size = cpu_to_le32(size_mb);
699 t->form_factor = 0x09; /* DIMM */
700 t->device_set = 0; /* Not in a set */
701 snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
702 SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
703 SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
704 t->memory_type = 0x07; /* RAM */
705 t->type_detail = cpu_to_le16(0x02); /* Other */
706 t->speed = cpu_to_le16(type17.speed);
707 SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
708 SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
709 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
710 SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
711 t->attributes = 0; /* Unknown */
712 t->configured_clock_speed = t->speed; /* reuse value for max speed */
713 t->minimum_voltage = cpu_to_le16(0); /* Unknown */
714 t->maximum_voltage = cpu_to_le16(0); /* Unknown */
715 t->configured_voltage = cpu_to_le16(0); /* Unknown */
717 SMBIOS_BUILD_TABLE_POST;
720 static void smbios_build_type_19_table(unsigned instance,
721 uint64_t start, uint64_t size)
723 uint64_t end, start_kb, end_kb;
725 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
727 end = start + size - 1;
728 assert(end > start);
729 start_kb = start / ONE_KB;
730 end_kb = end / ONE_KB;
731 if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
732 t->starting_address = cpu_to_le32(start_kb);
733 t->ending_address = cpu_to_le32(end_kb);
734 t->extended_starting_address =
735 t->extended_ending_address = cpu_to_le64(0);
736 } else {
737 t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
738 t->extended_starting_address = cpu_to_le64(start);
739 t->extended_ending_address = cpu_to_le64(end);
741 t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
742 t->partition_width = 1; /* One device per row */
744 SMBIOS_BUILD_TABLE_POST;
747 static void smbios_build_type_32_table(void)
749 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
751 memset(t->reserved, 0, 6);
752 t->boot_status = 0; /* No errors detected */
754 SMBIOS_BUILD_TABLE_POST;
757 static void smbios_build_type_127_table(void)
759 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
760 SMBIOS_BUILD_TABLE_POST;
763 void smbios_set_cpuid(uint32_t version, uint32_t features)
765 smbios_cpuid_version = version;
766 smbios_cpuid_features = features;
769 #define SMBIOS_SET_DEFAULT(field, value) \
770 if (!field) { \
771 field = value; \
774 void smbios_set_defaults(const char *manufacturer, const char *product,
775 const char *version, bool legacy_mode,
776 bool uuid_encoded, SmbiosEntryPointType ep_type)
778 smbios_have_defaults = true;
779 smbios_legacy = legacy_mode;
780 smbios_uuid_encoded = uuid_encoded;
781 smbios_ep_type = ep_type;
783 /* drop unwanted version of command-line file blob(s) */
784 if (smbios_legacy) {
785 g_free(smbios_tables);
786 /* in legacy mode, also complain if fields were given for types > 1 */
787 if (find_next_bit(have_fields_bitmap,
788 SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
789 error_report("can't process fields for smbios "
790 "types > 1 on machine versions < 2.1!");
791 exit(1);
793 } else {
794 g_free(smbios_entries);
797 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
798 SMBIOS_SET_DEFAULT(type1.product, product);
799 SMBIOS_SET_DEFAULT(type1.version, version);
800 SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
801 SMBIOS_SET_DEFAULT(type2.product, product);
802 SMBIOS_SET_DEFAULT(type2.version, version);
803 SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
804 SMBIOS_SET_DEFAULT(type3.version, version);
805 SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
806 SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
807 SMBIOS_SET_DEFAULT(type4.version, version);
808 SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
809 SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
812 static void smbios_entry_point_setup(void)
814 switch (smbios_ep_type) {
815 case SMBIOS_ENTRY_POINT_21:
816 memcpy(ep.ep21.anchor_string, "_SM_", 4);
817 memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
818 ep.ep21.length = sizeof(struct smbios_21_entry_point);
819 ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
820 memset(ep.ep21.formatted_area, 0, 5);
822 /* compliant with smbios spec v2.8 */
823 ep.ep21.smbios_major_version = 2;
824 ep.ep21.smbios_minor_version = 8;
825 ep.ep21.smbios_bcd_revision = 0x28;
827 /* set during table construction, but BIOS may override: */
828 ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
829 ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
830 ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
832 /* BIOS must recalculate */
833 ep.ep21.checksum = 0;
834 ep.ep21.intermediate_checksum = 0;
835 ep.ep21.structure_table_address = cpu_to_le32(0);
837 break;
838 case SMBIOS_ENTRY_POINT_30:
839 memcpy(ep.ep30.anchor_string, "_SM3_", 5);
840 ep.ep30.length = sizeof(struct smbios_30_entry_point);
841 ep.ep30.entry_point_revision = 1;
842 ep.ep30.reserved = 0;
844 /* compliant with smbios spec 3.0 */
845 ep.ep30.smbios_major_version = 3;
846 ep.ep30.smbios_minor_version = 0;
847 ep.ep30.smbios_doc_rev = 0;
849 /* set during table construct, but BIOS might override */
850 ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
852 /* BIOS must recalculate */
853 ep.ep30.checksum = 0;
854 ep.ep30.structure_table_address = cpu_to_le64(0);
856 break;
857 default:
858 abort();
859 break;
863 void smbios_get_tables(const struct smbios_phys_mem_area *mem_array,
864 const unsigned int mem_array_size,
865 uint8_t **tables, size_t *tables_len,
866 uint8_t **anchor, size_t *anchor_len)
868 unsigned i, dimm_cnt;
870 if (smbios_legacy) {
871 *tables = *anchor = NULL;
872 *tables_len = *anchor_len = 0;
873 return;
876 if (!smbios_immutable) {
877 smbios_build_type_0_table();
878 smbios_build_type_1_table();
879 smbios_build_type_2_table();
880 smbios_build_type_3_table();
882 smbios_smp_sockets = DIV_ROUND_UP(smp_cpus, smp_cores * smp_threads);
883 assert(smbios_smp_sockets >= 1);
885 for (i = 0; i < smbios_smp_sockets; i++) {
886 smbios_build_type_4_table(i);
889 #define MAX_DIMM_SZ (16ll * ONE_GB)
890 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
891 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
893 dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
895 smbios_build_type_16_table(dimm_cnt);
897 for (i = 0; i < dimm_cnt; i++) {
898 smbios_build_type_17_table(i, GET_DIMM_SZ);
901 for (i = 0; i < mem_array_size; i++) {
902 smbios_build_type_19_table(i, mem_array[i].address,
903 mem_array[i].length);
906 smbios_build_type_32_table();
907 smbios_build_type_127_table();
909 smbios_validate_table();
910 smbios_entry_point_setup();
911 smbios_immutable = true;
914 /* return tables blob and entry point (anchor), and their sizes */
915 *tables = smbios_tables;
916 *tables_len = smbios_tables_len;
917 *anchor = (uint8_t *)&ep;
919 /* calculate length based on anchor string */
920 if (!strncmp((char *)&ep, "_SM_", 4)) {
921 *anchor_len = sizeof(struct smbios_21_entry_point);
922 } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
923 *anchor_len = sizeof(struct smbios_30_entry_point);
924 } else {
925 abort();
929 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
931 const char *val = qemu_opt_get(opts, name);
933 if (val) {
934 *dest = val;
938 void smbios_entry_add(QemuOpts *opts)
940 Error *local_err = NULL;
941 const char *val;
943 assert(!smbios_immutable);
945 val = qemu_opt_get(opts, "file");
946 if (val) {
947 struct smbios_structure_header *header;
948 int size;
949 struct smbios_table *table; /* legacy mode only */
951 qemu_opts_validate(opts, qemu_smbios_file_opts, &local_err);
952 if (local_err) {
953 error_report_err(local_err);
954 exit(1);
957 size = get_image_size(val);
958 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
959 error_report("Cannot read SMBIOS file %s", val);
960 exit(1);
964 * NOTE: standard double '\0' terminator expected, per smbios spec.
965 * (except in legacy mode, where the second '\0' is implicit and
966 * will be inserted by the BIOS).
968 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
969 header = (struct smbios_structure_header *)(smbios_tables +
970 smbios_tables_len);
972 if (load_image(val, (uint8_t *)header) != size) {
973 error_report("Failed to load SMBIOS file %s", val);
974 exit(1);
977 if (test_bit(header->type, have_fields_bitmap)) {
978 error_report("can't load type %d struct, fields already specified!",
979 header->type);
980 exit(1);
982 set_bit(header->type, have_binfile_bitmap);
984 if (header->type == 4) {
985 smbios_type4_count++;
988 smbios_tables_len += size;
989 if (size > smbios_table_max) {
990 smbios_table_max = size;
992 smbios_table_cnt++;
994 /* add a copy of the newly loaded blob to legacy smbios_entries */
995 /* NOTE: This code runs before smbios_set_defaults(), so we don't
996 * yet know which mode (legacy vs. aggregate-table) will be
997 * required. We therefore add the binary blob to both legacy
998 * (smbios_entries) and aggregate (smbios_tables) tables, and
999 * delete the one we don't need from smbios_set_defaults(),
1000 * once we know which machine version has been requested.
1002 if (!smbios_entries) {
1003 smbios_entries_len = sizeof(uint16_t);
1004 smbios_entries = g_malloc0(smbios_entries_len);
1006 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
1007 size + sizeof(*table));
1008 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
1009 table->header.type = SMBIOS_TABLE_ENTRY;
1010 table->header.length = cpu_to_le16(sizeof(*table) + size);
1011 memcpy(table->data, header, size);
1012 smbios_entries_len += sizeof(*table) + size;
1013 (*(uint16_t *)smbios_entries) =
1014 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
1015 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1017 return;
1020 val = qemu_opt_get(opts, "type");
1021 if (val) {
1022 unsigned long type = strtoul(val, NULL, 0);
1024 if (type > SMBIOS_MAX_TYPE) {
1025 error_report("out of range!");
1026 exit(1);
1029 if (test_bit(type, have_binfile_bitmap)) {
1030 error_report("can't add fields, binary file already loaded!");
1031 exit(1);
1033 set_bit(type, have_fields_bitmap);
1035 switch (type) {
1036 case 0:
1037 qemu_opts_validate(opts, qemu_smbios_type0_opts, &local_err);
1038 if (local_err) {
1039 error_report_err(local_err);
1040 exit(1);
1042 save_opt(&type0.vendor, opts, "vendor");
1043 save_opt(&type0.version, opts, "version");
1044 save_opt(&type0.date, opts, "date");
1045 type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1047 val = qemu_opt_get(opts, "release");
1048 if (val) {
1049 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1050 error_report("Invalid release");
1051 exit(1);
1053 type0.have_major_minor = true;
1055 return;
1056 case 1:
1057 qemu_opts_validate(opts, qemu_smbios_type1_opts, &local_err);
1058 if (local_err) {
1059 error_report_err(local_err);
1060 exit(1);
1062 save_opt(&type1.manufacturer, opts, "manufacturer");
1063 save_opt(&type1.product, opts, "product");
1064 save_opt(&type1.version, opts, "version");
1065 save_opt(&type1.serial, opts, "serial");
1066 save_opt(&type1.sku, opts, "sku");
1067 save_opt(&type1.family, opts, "family");
1069 val = qemu_opt_get(opts, "uuid");
1070 if (val) {
1071 if (qemu_uuid_parse(val, qemu_uuid) != 0) {
1072 error_report("Invalid UUID");
1073 exit(1);
1075 qemu_uuid_set = true;
1077 return;
1078 case 2:
1079 qemu_opts_validate(opts, qemu_smbios_type2_opts, &local_err);
1080 if (local_err) {
1081 error_report_err(local_err);
1082 exit(1);
1084 save_opt(&type2.manufacturer, opts, "manufacturer");
1085 save_opt(&type2.product, opts, "product");
1086 save_opt(&type2.version, opts, "version");
1087 save_opt(&type2.serial, opts, "serial");
1088 save_opt(&type2.asset, opts, "asset");
1089 save_opt(&type2.location, opts, "location");
1090 return;
1091 case 3:
1092 qemu_opts_validate(opts, qemu_smbios_type3_opts, &local_err);
1093 if (local_err) {
1094 error_report_err(local_err);
1095 exit(1);
1097 save_opt(&type3.manufacturer, opts, "manufacturer");
1098 save_opt(&type3.version, opts, "version");
1099 save_opt(&type3.serial, opts, "serial");
1100 save_opt(&type3.asset, opts, "asset");
1101 save_opt(&type3.sku, opts, "sku");
1102 return;
1103 case 4:
1104 qemu_opts_validate(opts, qemu_smbios_type4_opts, &local_err);
1105 if (local_err) {
1106 error_report_err(local_err);
1107 exit(1);
1109 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1110 save_opt(&type4.manufacturer, opts, "manufacturer");
1111 save_opt(&type4.version, opts, "version");
1112 save_opt(&type4.serial, opts, "serial");
1113 save_opt(&type4.asset, opts, "asset");
1114 save_opt(&type4.part, opts, "part");
1115 return;
1116 case 17:
1117 qemu_opts_validate(opts, qemu_smbios_type17_opts, &local_err);
1118 if (local_err) {
1119 error_report_err(local_err);
1120 exit(1);
1122 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1123 save_opt(&type17.bank, opts, "bank");
1124 save_opt(&type17.manufacturer, opts, "manufacturer");
1125 save_opt(&type17.serial, opts, "serial");
1126 save_opt(&type17.asset, opts, "asset");
1127 save_opt(&type17.part, opts, "part");
1128 type17.speed = qemu_opt_get_number(opts, "speed", 0);
1129 return;
1130 default:
1131 error_report("Don't know how to build fields for SMBIOS type %ld",
1132 type);
1133 exit(1);
1137 error_report("Must specify type= or file=");
1138 exit(1);