4 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5 * Copyright (C) 2013 Red Hat, Inc.
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/osdep.h"
19 #include "qemu/units.h"
20 #include "qapi/error.h"
21 #include "qemu/config-file.h"
22 #include "qemu/error-report.h"
23 #include "qemu/module.h"
24 #include "qemu/option.h"
25 #include "sysemu/sysemu.h"
26 #include "qemu/uuid.h"
27 #include "hw/firmware/smbios.h"
28 #include "hw/loader.h"
29 #include "hw/boards.h"
30 #include "hw/pci/pci_bus.h"
31 #include "smbios_build.h"
33 /* legacy structures and constants for <= 2.0 machines */
34 struct smbios_header
{
40 struct smbios_header header
;
47 struct smbios_header header
;
51 #define SMBIOS_FIELD_ENTRY 0
52 #define SMBIOS_TABLE_ENTRY 1
54 static uint8_t *smbios_entries
;
55 static size_t smbios_entries_len
;
56 static bool smbios_legacy
= true;
57 static bool smbios_uuid_encoded
= true;
58 /* end: legacy structures & constants for <= 2.0 machines */
61 uint8_t *smbios_tables
;
62 size_t smbios_tables_len
;
63 unsigned smbios_table_max
;
64 unsigned smbios_table_cnt
;
65 static SmbiosEntryPointType smbios_ep_type
= SMBIOS_ENTRY_POINT_TYPE_32
;
67 static SmbiosEntryPoint ep
;
69 static int smbios_type4_count
= 0;
70 static bool smbios_immutable
;
71 static bool smbios_have_defaults
;
72 static uint32_t smbios_cpuid_version
, smbios_cpuid_features
, smbios_smp_sockets
;
74 static DECLARE_BITMAP(have_binfile_bitmap
, SMBIOS_MAX_TYPE
+1);
75 static DECLARE_BITMAP(have_fields_bitmap
, SMBIOS_MAX_TYPE
+1);
78 const char *vendor
, *version
, *date
;
79 bool have_major_minor
, uefi
;
84 const char *manufacturer
, *product
, *version
, *serial
, *sku
, *family
;
85 /* uuid is in qemu_uuid */
89 const char *manufacturer
, *product
, *version
, *serial
, *asset
, *location
;
93 const char *manufacturer
, *version
, *serial
, *asset
, *sku
;
97 * SVVP requires max_speed and current_speed to be set and not being
98 * 0 which counts as unknown (SMBIOS 3.1.0/Table 21). Set the
99 * default value to 2000MHz as we did before.
101 #define DEFAULT_CPU_SPEED 2000
104 const char *sock_pfx
, *manufacturer
, *version
, *serial
, *asset
, *part
;
106 uint64_t current_speed
;
108 .max_speed
= DEFAULT_CPU_SPEED
,
109 .current_speed
= DEFAULT_CPU_SPEED
118 const char *loc_pfx
, *bank
, *manufacturer
, *serial
, *asset
, *part
;
122 static QEnumLookup type41_kind_lookup
= {
123 .array
= (const char *const[]) {
137 struct type41_instance
{
138 const char *designation
, *pcidev
;
139 uint8_t instance
, kind
;
140 QTAILQ_ENTRY(type41_instance
) next
;
142 static QTAILQ_HEAD(, type41_instance
) type41
= QTAILQ_HEAD_INITIALIZER(type41
);
144 static QemuOptsList qemu_smbios_opts
= {
146 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts
.head
),
149 * no elements => accept any params
150 * validation will happen later
152 { /* end of list */ }
156 static const QemuOptDesc qemu_smbios_file_opts
[] = {
159 .type
= QEMU_OPT_STRING
,
160 .help
= "binary file containing an SMBIOS element",
162 { /* end of list */ }
165 static const QemuOptDesc qemu_smbios_type0_opts
[] = {
168 .type
= QEMU_OPT_NUMBER
,
169 .help
= "SMBIOS element type",
172 .type
= QEMU_OPT_STRING
,
173 .help
= "vendor name",
176 .type
= QEMU_OPT_STRING
,
177 .help
= "version number",
180 .type
= QEMU_OPT_STRING
,
181 .help
= "release date",
184 .type
= QEMU_OPT_STRING
,
185 .help
= "revision number",
188 .type
= QEMU_OPT_BOOL
,
189 .help
= "uefi support",
191 { /* end of list */ }
194 static const QemuOptDesc qemu_smbios_type1_opts
[] = {
197 .type
= QEMU_OPT_NUMBER
,
198 .help
= "SMBIOS element type",
200 .name
= "manufacturer",
201 .type
= QEMU_OPT_STRING
,
202 .help
= "manufacturer name",
205 .type
= QEMU_OPT_STRING
,
206 .help
= "product name",
209 .type
= QEMU_OPT_STRING
,
210 .help
= "version number",
213 .type
= QEMU_OPT_STRING
,
214 .help
= "serial number",
217 .type
= QEMU_OPT_STRING
,
221 .type
= QEMU_OPT_STRING
,
222 .help
= "SKU number",
225 .type
= QEMU_OPT_STRING
,
226 .help
= "family name",
228 { /* end of list */ }
231 static const QemuOptDesc qemu_smbios_type2_opts
[] = {
234 .type
= QEMU_OPT_NUMBER
,
235 .help
= "SMBIOS element type",
237 .name
= "manufacturer",
238 .type
= QEMU_OPT_STRING
,
239 .help
= "manufacturer name",
242 .type
= QEMU_OPT_STRING
,
243 .help
= "product name",
246 .type
= QEMU_OPT_STRING
,
247 .help
= "version number",
250 .type
= QEMU_OPT_STRING
,
251 .help
= "serial number",
254 .type
= QEMU_OPT_STRING
,
255 .help
= "asset tag number",
258 .type
= QEMU_OPT_STRING
,
259 .help
= "location in chassis",
261 { /* end of list */ }
264 static const QemuOptDesc qemu_smbios_type3_opts
[] = {
267 .type
= QEMU_OPT_NUMBER
,
268 .help
= "SMBIOS element type",
270 .name
= "manufacturer",
271 .type
= QEMU_OPT_STRING
,
272 .help
= "manufacturer name",
275 .type
= QEMU_OPT_STRING
,
276 .help
= "version number",
279 .type
= QEMU_OPT_STRING
,
280 .help
= "serial number",
283 .type
= QEMU_OPT_STRING
,
284 .help
= "asset tag number",
287 .type
= QEMU_OPT_STRING
,
288 .help
= "SKU number",
290 { /* end of list */ }
293 static const QemuOptDesc qemu_smbios_type4_opts
[] = {
296 .type
= QEMU_OPT_NUMBER
,
297 .help
= "SMBIOS element type",
300 .type
= QEMU_OPT_STRING
,
301 .help
= "socket designation string prefix",
303 .name
= "manufacturer",
304 .type
= QEMU_OPT_STRING
,
305 .help
= "manufacturer name",
308 .type
= QEMU_OPT_STRING
,
309 .help
= "version number",
312 .type
= QEMU_OPT_NUMBER
,
313 .help
= "max speed in MHz",
315 .name
= "current-speed",
316 .type
= QEMU_OPT_NUMBER
,
317 .help
= "speed at system boot in MHz",
320 .type
= QEMU_OPT_STRING
,
321 .help
= "serial number",
324 .type
= QEMU_OPT_STRING
,
325 .help
= "asset tag number",
328 .type
= QEMU_OPT_STRING
,
329 .help
= "part number",
331 { /* end of list */ }
334 static const QemuOptDesc qemu_smbios_type11_opts
[] = {
337 .type
= QEMU_OPT_STRING
,
338 .help
= "OEM string data",
342 .type
= QEMU_OPT_STRING
,
343 .help
= "OEM string data from file",
347 static const QemuOptDesc qemu_smbios_type17_opts
[] = {
350 .type
= QEMU_OPT_NUMBER
,
351 .help
= "SMBIOS element type",
354 .type
= QEMU_OPT_STRING
,
355 .help
= "device locator string prefix",
358 .type
= QEMU_OPT_STRING
,
359 .help
= "bank locator string",
361 .name
= "manufacturer",
362 .type
= QEMU_OPT_STRING
,
363 .help
= "manufacturer name",
366 .type
= QEMU_OPT_STRING
,
367 .help
= "serial number",
370 .type
= QEMU_OPT_STRING
,
371 .help
= "asset tag number",
374 .type
= QEMU_OPT_STRING
,
375 .help
= "part number",
378 .type
= QEMU_OPT_NUMBER
,
379 .help
= "maximum capable speed",
381 { /* end of list */ }
384 static const QemuOptDesc qemu_smbios_type41_opts
[] = {
387 .type
= QEMU_OPT_NUMBER
,
388 .help
= "SMBIOS element type",
390 .name
= "designation",
391 .type
= QEMU_OPT_STRING
,
392 .help
= "reference designation string",
395 .type
= QEMU_OPT_STRING
,
396 .help
= "device type",
397 .def_value_str
= "other",
400 .type
= QEMU_OPT_NUMBER
,
401 .help
= "device type instance",
404 .type
= QEMU_OPT_STRING
,
405 .help
= "PCI device",
407 { /* end of list */ }
410 static void smbios_register_config(void)
412 qemu_add_opts(&qemu_smbios_opts
);
415 opts_init(smbios_register_config
);
418 * The SMBIOS 2.1 "structure table length" field in the
419 * entry point uses a 16-bit integer, so we're limited
420 * in total table size
422 #define SMBIOS_21_MAX_TABLES_LEN 0xffff
424 static void smbios_validate_table(MachineState
*ms
)
426 uint32_t expect_t4_count
= smbios_legacy
?
427 ms
->smp
.cpus
: smbios_smp_sockets
;
429 if (smbios_type4_count
&& smbios_type4_count
!= expect_t4_count
) {
430 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
431 expect_t4_count
, smbios_type4_count
);
435 if (smbios_ep_type
== SMBIOS_ENTRY_POINT_TYPE_32
&&
436 smbios_tables_len
> SMBIOS_21_MAX_TABLES_LEN
) {
437 error_report("SMBIOS 2.1 table length %zu exceeds %d",
438 smbios_tables_len
, SMBIOS_21_MAX_TABLES_LEN
);
444 /* legacy setup functions for <= 2.0 machines */
445 static void smbios_add_field(int type
, int offset
, const void *data
, size_t len
)
447 struct smbios_field
*field
;
449 if (!smbios_entries
) {
450 smbios_entries_len
= sizeof(uint16_t);
451 smbios_entries
= g_malloc0(smbios_entries_len
);
453 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
454 sizeof(*field
) + len
);
455 field
= (struct smbios_field
*)(smbios_entries
+ smbios_entries_len
);
456 field
->header
.type
= SMBIOS_FIELD_ENTRY
;
457 field
->header
.length
= cpu_to_le16(sizeof(*field
) + len
);
460 field
->offset
= cpu_to_le16(offset
);
461 memcpy(field
->data
, data
, len
);
463 smbios_entries_len
+= sizeof(*field
) + len
;
464 (*(uint16_t *)smbios_entries
) =
465 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
468 static void smbios_maybe_add_str(int type
, int offset
, const char *data
)
471 smbios_add_field(type
, offset
, data
, strlen(data
) + 1);
475 static void smbios_build_type_0_fields(void)
477 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, vendor_str
),
479 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, bios_version_str
),
481 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
,
482 bios_release_date_str
),
484 if (type0
.have_major_minor
) {
485 smbios_add_field(0, offsetof(struct smbios_type_0
,
486 system_bios_major_release
),
488 smbios_add_field(0, offsetof(struct smbios_type_0
,
489 system_bios_minor_release
),
494 static void smbios_build_type_1_fields(void)
496 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, manufacturer_str
),
498 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, product_name_str
),
500 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, version_str
),
502 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, serial_number_str
),
504 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, sku_number_str
),
506 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, family_str
),
509 /* We don't encode the UUID in the "wire format" here because this
510 * function is for legacy mode and needs to keep the guest ABI, and
511 * because we don't know what's the SMBIOS version advertised by the
514 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
),
519 uint8_t *smbios_get_table_legacy(MachineState
*ms
, size_t *length
)
521 if (!smbios_legacy
) {
526 if (!smbios_immutable
) {
527 smbios_build_type_0_fields();
528 smbios_build_type_1_fields();
529 smbios_validate_table(ms
);
530 smbios_immutable
= true;
532 *length
= smbios_entries_len
;
533 return smbios_entries
;
535 /* end: legacy setup functions for <= 2.0 machines */
538 bool smbios_skip_table(uint8_t type
, bool required_table
)
540 if (test_bit(type
, have_binfile_bitmap
)) {
541 return true; /* user provided their own binary blob(s) */
543 if (test_bit(type
, have_fields_bitmap
)) {
544 return false; /* user provided fields via command line */
546 if (smbios_have_defaults
&& required_table
) {
547 return false; /* we're building tables, and this one's required */
552 static void smbios_build_type_0_table(void)
554 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
556 SMBIOS_TABLE_SET_STR(0, vendor_str
, type0
.vendor
);
557 SMBIOS_TABLE_SET_STR(0, bios_version_str
, type0
.version
);
559 t
->bios_starting_address_segment
= cpu_to_le16(0xE800); /* from SeaBIOS */
561 SMBIOS_TABLE_SET_STR(0, bios_release_date_str
, type0
.date
);
563 t
->bios_rom_size
= 0; /* hardcoded in SeaBIOS with FIXME comment */
565 t
->bios_characteristics
= cpu_to_le64(0x08); /* Not supported */
566 t
->bios_characteristics_extension_bytes
[0] = 0;
567 t
->bios_characteristics_extension_bytes
[1] = 0x14; /* TCD/SVVP | VM */
569 t
->bios_characteristics_extension_bytes
[1] |= 0x08; /* |= UEFI */
572 if (type0
.have_major_minor
) {
573 t
->system_bios_major_release
= type0
.major
;
574 t
->system_bios_minor_release
= type0
.minor
;
576 t
->system_bios_major_release
= 0;
577 t
->system_bios_minor_release
= 0;
580 /* hardcoded in SeaBIOS */
581 t
->embedded_controller_major_release
= 0xFF;
582 t
->embedded_controller_minor_release
= 0xFF;
584 SMBIOS_BUILD_TABLE_POST
;
587 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
588 * format specified by SMBIOS version 2.6.
590 static void smbios_encode_uuid(struct smbios_uuid
*uuid
, QemuUUID
*in
)
592 memcpy(uuid
, in
, 16);
593 if (smbios_uuid_encoded
) {
594 uuid
->time_low
= bswap32(uuid
->time_low
);
595 uuid
->time_mid
= bswap16(uuid
->time_mid
);
596 uuid
->time_hi_and_version
= bswap16(uuid
->time_hi_and_version
);
600 static void smbios_build_type_1_table(void)
602 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
604 SMBIOS_TABLE_SET_STR(1, manufacturer_str
, type1
.manufacturer
);
605 SMBIOS_TABLE_SET_STR(1, product_name_str
, type1
.product
);
606 SMBIOS_TABLE_SET_STR(1, version_str
, type1
.version
);
607 SMBIOS_TABLE_SET_STR(1, serial_number_str
, type1
.serial
);
609 smbios_encode_uuid(&t
->uuid
, &qemu_uuid
);
611 memset(&t
->uuid
, 0, 16);
613 t
->wake_up_type
= 0x06; /* power switch */
614 SMBIOS_TABLE_SET_STR(1, sku_number_str
, type1
.sku
);
615 SMBIOS_TABLE_SET_STR(1, family_str
, type1
.family
);
617 SMBIOS_BUILD_TABLE_POST
;
620 static void smbios_build_type_2_table(void)
622 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
624 SMBIOS_TABLE_SET_STR(2, manufacturer_str
, type2
.manufacturer
);
625 SMBIOS_TABLE_SET_STR(2, product_str
, type2
.product
);
626 SMBIOS_TABLE_SET_STR(2, version_str
, type2
.version
);
627 SMBIOS_TABLE_SET_STR(2, serial_number_str
, type2
.serial
);
628 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str
, type2
.asset
);
629 t
->feature_flags
= 0x01; /* Motherboard */
630 SMBIOS_TABLE_SET_STR(2, location_str
, type2
.location
);
631 t
->chassis_handle
= cpu_to_le16(0x300); /* Type 3 (System enclosure) */
632 t
->board_type
= 0x0A; /* Motherboard */
633 t
->contained_element_count
= 0;
635 SMBIOS_BUILD_TABLE_POST
;
638 static void smbios_build_type_3_table(void)
640 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
642 SMBIOS_TABLE_SET_STR(3, manufacturer_str
, type3
.manufacturer
);
643 t
->type
= 0x01; /* Other */
644 SMBIOS_TABLE_SET_STR(3, version_str
, type3
.version
);
645 SMBIOS_TABLE_SET_STR(3, serial_number_str
, type3
.serial
);
646 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str
, type3
.asset
);
647 t
->boot_up_state
= 0x03; /* Safe */
648 t
->power_supply_state
= 0x03; /* Safe */
649 t
->thermal_state
= 0x03; /* Safe */
650 t
->security_status
= 0x02; /* Unknown */
651 t
->oem_defined
= cpu_to_le32(0);
653 t
->number_of_power_cords
= 0;
654 t
->contained_element_count
= 0;
655 t
->contained_element_record_length
= 0;
656 SMBIOS_TABLE_SET_STR(3, sku_number_str
, type3
.sku
);
658 SMBIOS_BUILD_TABLE_POST
;
661 static void smbios_build_type_4_table(MachineState
*ms
, unsigned instance
)
665 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance
, true); /* required */
667 snprintf(sock_str
, sizeof(sock_str
), "%s%2x", type4
.sock_pfx
, instance
);
668 SMBIOS_TABLE_SET_STR(4, socket_designation_str
, sock_str
);
669 t
->processor_type
= 0x03; /* CPU */
670 t
->processor_family
= 0x01; /* Other */
671 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str
, type4
.manufacturer
);
672 t
->processor_id
[0] = cpu_to_le32(smbios_cpuid_version
);
673 t
->processor_id
[1] = cpu_to_le32(smbios_cpuid_features
);
674 SMBIOS_TABLE_SET_STR(4, processor_version_str
, type4
.version
);
676 t
->external_clock
= cpu_to_le16(0); /* Unknown */
677 t
->max_speed
= cpu_to_le16(type4
.max_speed
);
678 t
->current_speed
= cpu_to_le16(type4
.current_speed
);
679 t
->status
= 0x41; /* Socket populated, CPU enabled */
680 t
->processor_upgrade
= 0x01; /* Other */
681 t
->l1_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
682 t
->l2_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
683 t
->l3_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
684 SMBIOS_TABLE_SET_STR(4, serial_number_str
, type4
.serial
);
685 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str
, type4
.asset
);
686 SMBIOS_TABLE_SET_STR(4, part_number_str
, type4
.part
);
687 t
->core_count
= t
->core_enabled
= ms
->smp
.cores
;
688 t
->thread_count
= ms
->smp
.threads
;
689 t
->processor_characteristics
= cpu_to_le16(0x02); /* Unknown */
690 t
->processor_family2
= cpu_to_le16(0x01); /* Other */
692 SMBIOS_BUILD_TABLE_POST
;
693 smbios_type4_count
++;
696 static void smbios_build_type_11_table(void)
701 if (type11
.nvalues
== 0) {
705 SMBIOS_BUILD_TABLE_PRE(11, 0xe00, true); /* required */
707 snprintf(count_str
, sizeof(count_str
), "%zu", type11
.nvalues
);
708 t
->count
= type11
.nvalues
;
710 for (i
= 0; i
< type11
.nvalues
; i
++) {
711 SMBIOS_TABLE_SET_STR_LIST(11, type11
.values
[i
]);
712 g_free(type11
.values
[i
]);
713 type11
.values
[i
] = NULL
;
716 SMBIOS_BUILD_TABLE_POST
;
719 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
721 static void smbios_build_type_16_table(unsigned dimm_cnt
)
725 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
727 t
->location
= 0x01; /* Other */
728 t
->use
= 0x03; /* System memory */
729 t
->error_correction
= 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
730 size_kb
= QEMU_ALIGN_UP(current_machine
->ram_size
, KiB
) / KiB
;
731 if (size_kb
< MAX_T16_STD_SZ
) {
732 t
->maximum_capacity
= cpu_to_le32(size_kb
);
733 t
->extended_maximum_capacity
= cpu_to_le64(0);
735 t
->maximum_capacity
= cpu_to_le32(MAX_T16_STD_SZ
);
736 t
->extended_maximum_capacity
= cpu_to_le64(current_machine
->ram_size
);
738 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
739 t
->number_of_memory_devices
= cpu_to_le16(dimm_cnt
);
741 SMBIOS_BUILD_TABLE_POST
;
744 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
745 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
747 static void smbios_build_type_17_table(unsigned instance
, uint64_t size
)
752 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance
, true); /* required */
754 t
->physical_memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
755 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
756 t
->total_width
= cpu_to_le16(0xFFFF); /* Unknown */
757 t
->data_width
= cpu_to_le16(0xFFFF); /* Unknown */
758 size_mb
= QEMU_ALIGN_UP(size
, MiB
) / MiB
;
759 if (size_mb
< MAX_T17_STD_SZ
) {
760 t
->size
= cpu_to_le16(size_mb
);
761 t
->extended_size
= cpu_to_le32(0);
763 assert(size_mb
< MAX_T17_EXT_SZ
);
764 t
->size
= cpu_to_le16(MAX_T17_STD_SZ
);
765 t
->extended_size
= cpu_to_le32(size_mb
);
767 t
->form_factor
= 0x09; /* DIMM */
768 t
->device_set
= 0; /* Not in a set */
769 snprintf(loc_str
, sizeof(loc_str
), "%s %d", type17
.loc_pfx
, instance
);
770 SMBIOS_TABLE_SET_STR(17, device_locator_str
, loc_str
);
771 SMBIOS_TABLE_SET_STR(17, bank_locator_str
, type17
.bank
);
772 t
->memory_type
= 0x07; /* RAM */
773 t
->type_detail
= cpu_to_le16(0x02); /* Other */
774 t
->speed
= cpu_to_le16(type17
.speed
);
775 SMBIOS_TABLE_SET_STR(17, manufacturer_str
, type17
.manufacturer
);
776 SMBIOS_TABLE_SET_STR(17, serial_number_str
, type17
.serial
);
777 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str
, type17
.asset
);
778 SMBIOS_TABLE_SET_STR(17, part_number_str
, type17
.part
);
779 t
->attributes
= 0; /* Unknown */
780 t
->configured_clock_speed
= t
->speed
; /* reuse value for max speed */
781 t
->minimum_voltage
= cpu_to_le16(0); /* Unknown */
782 t
->maximum_voltage
= cpu_to_le16(0); /* Unknown */
783 t
->configured_voltage
= cpu_to_le16(0); /* Unknown */
785 SMBIOS_BUILD_TABLE_POST
;
788 static void smbios_build_type_19_table(unsigned instance
,
789 uint64_t start
, uint64_t size
)
791 uint64_t end
, start_kb
, end_kb
;
793 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance
, true); /* required */
795 end
= start
+ size
- 1;
797 start_kb
= start
/ KiB
;
799 if (start_kb
< UINT32_MAX
&& end_kb
< UINT32_MAX
) {
800 t
->starting_address
= cpu_to_le32(start_kb
);
801 t
->ending_address
= cpu_to_le32(end_kb
);
802 t
->extended_starting_address
=
803 t
->extended_ending_address
= cpu_to_le64(0);
805 t
->starting_address
= t
->ending_address
= cpu_to_le32(UINT32_MAX
);
806 t
->extended_starting_address
= cpu_to_le64(start
);
807 t
->extended_ending_address
= cpu_to_le64(end
);
809 t
->memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
810 t
->partition_width
= 1; /* One device per row */
812 SMBIOS_BUILD_TABLE_POST
;
815 static void smbios_build_type_32_table(void)
817 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
819 memset(t
->reserved
, 0, 6);
820 t
->boot_status
= 0; /* No errors detected */
822 SMBIOS_BUILD_TABLE_POST
;
825 static void smbios_build_type_41_table(Error
**errp
)
827 unsigned instance
= 0;
828 struct type41_instance
*t41
;
830 QTAILQ_FOREACH(t41
, &type41
, next
) {
831 SMBIOS_BUILD_TABLE_PRE(41, 0x2900 + instance
, true);
833 SMBIOS_TABLE_SET_STR(41, reference_designation_str
, t41
->designation
);
834 t
->device_type
= t41
->kind
;
835 t
->device_type_instance
= t41
->instance
;
836 t
->segment_group_number
= cpu_to_le16(0);
838 t
->device_number
= 0;
841 PCIDevice
*pdev
= NULL
;
842 int rc
= pci_qdev_find_device(t41
->pcidev
, &pdev
);
845 "No PCI device %s for SMBIOS type 41 entry %s",
846 t41
->pcidev
, t41
->designation
);
850 * We only handle the case were the device is attached to
851 * the PCI root bus. The general case is more complex as
852 * bridges are enumerated later and the table would need
853 * to be updated at this moment.
855 if (!pci_bus_is_root(pci_get_bus(pdev
))) {
857 "Cannot create type 41 entry for PCI device %s: "
858 "not attached to the root bus",
862 t
->segment_group_number
= cpu_to_le16(0);
863 t
->bus_number
= pci_dev_bus_num(pdev
);
864 t
->device_number
= pdev
->devfn
;
867 SMBIOS_BUILD_TABLE_POST
;
872 static void smbios_build_type_127_table(void)
874 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
875 SMBIOS_BUILD_TABLE_POST
;
878 void smbios_set_cpuid(uint32_t version
, uint32_t features
)
880 smbios_cpuid_version
= version
;
881 smbios_cpuid_features
= features
;
884 #define SMBIOS_SET_DEFAULT(field, value) \
889 void smbios_set_defaults(const char *manufacturer
, const char *product
,
890 const char *version
, bool legacy_mode
,
891 bool uuid_encoded
, SmbiosEntryPointType ep_type
)
893 smbios_have_defaults
= true;
894 smbios_legacy
= legacy_mode
;
895 smbios_uuid_encoded
= uuid_encoded
;
896 smbios_ep_type
= ep_type
;
898 /* drop unwanted version of command-line file blob(s) */
900 g_free(smbios_tables
);
901 /* in legacy mode, also complain if fields were given for types > 1 */
902 if (find_next_bit(have_fields_bitmap
,
903 SMBIOS_MAX_TYPE
+1, 2) < SMBIOS_MAX_TYPE
+1) {
904 error_report("can't process fields for smbios "
905 "types > 1 on machine versions < 2.1!");
909 g_free(smbios_entries
);
912 SMBIOS_SET_DEFAULT(type1
.manufacturer
, manufacturer
);
913 SMBIOS_SET_DEFAULT(type1
.product
, product
);
914 SMBIOS_SET_DEFAULT(type1
.version
, version
);
915 SMBIOS_SET_DEFAULT(type2
.manufacturer
, manufacturer
);
916 SMBIOS_SET_DEFAULT(type2
.product
, product
);
917 SMBIOS_SET_DEFAULT(type2
.version
, version
);
918 SMBIOS_SET_DEFAULT(type3
.manufacturer
, manufacturer
);
919 SMBIOS_SET_DEFAULT(type3
.version
, version
);
920 SMBIOS_SET_DEFAULT(type4
.sock_pfx
, "CPU");
921 SMBIOS_SET_DEFAULT(type4
.manufacturer
, manufacturer
);
922 SMBIOS_SET_DEFAULT(type4
.version
, version
);
923 SMBIOS_SET_DEFAULT(type17
.loc_pfx
, "DIMM");
924 SMBIOS_SET_DEFAULT(type17
.manufacturer
, manufacturer
);
927 static void smbios_entry_point_setup(void)
929 switch (smbios_ep_type
) {
930 case SMBIOS_ENTRY_POINT_TYPE_32
:
931 memcpy(ep
.ep21
.anchor_string
, "_SM_", 4);
932 memcpy(ep
.ep21
.intermediate_anchor_string
, "_DMI_", 5);
933 ep
.ep21
.length
= sizeof(struct smbios_21_entry_point
);
934 ep
.ep21
.entry_point_revision
= 0; /* formatted_area reserved */
935 memset(ep
.ep21
.formatted_area
, 0, 5);
937 /* compliant with smbios spec v2.8 */
938 ep
.ep21
.smbios_major_version
= 2;
939 ep
.ep21
.smbios_minor_version
= 8;
940 ep
.ep21
.smbios_bcd_revision
= 0x28;
942 /* set during table construction, but BIOS may override: */
943 ep
.ep21
.structure_table_length
= cpu_to_le16(smbios_tables_len
);
944 ep
.ep21
.max_structure_size
= cpu_to_le16(smbios_table_max
);
945 ep
.ep21
.number_of_structures
= cpu_to_le16(smbios_table_cnt
);
947 /* BIOS must recalculate */
948 ep
.ep21
.checksum
= 0;
949 ep
.ep21
.intermediate_checksum
= 0;
950 ep
.ep21
.structure_table_address
= cpu_to_le32(0);
953 case SMBIOS_ENTRY_POINT_TYPE_64
:
954 memcpy(ep
.ep30
.anchor_string
, "_SM3_", 5);
955 ep
.ep30
.length
= sizeof(struct smbios_30_entry_point
);
956 ep
.ep30
.entry_point_revision
= 1;
957 ep
.ep30
.reserved
= 0;
959 /* compliant with smbios spec 3.0 */
960 ep
.ep30
.smbios_major_version
= 3;
961 ep
.ep30
.smbios_minor_version
= 0;
962 ep
.ep30
.smbios_doc_rev
= 0;
964 /* set during table construct, but BIOS might override */
965 ep
.ep30
.structure_table_max_size
= cpu_to_le32(smbios_tables_len
);
967 /* BIOS must recalculate */
968 ep
.ep30
.checksum
= 0;
969 ep
.ep30
.structure_table_address
= cpu_to_le64(0);
978 void smbios_get_tables(MachineState
*ms
,
979 const struct smbios_phys_mem_area
*mem_array
,
980 const unsigned int mem_array_size
,
981 uint8_t **tables
, size_t *tables_len
,
982 uint8_t **anchor
, size_t *anchor_len
,
985 unsigned i
, dimm_cnt
;
988 *tables
= *anchor
= NULL
;
989 *tables_len
= *anchor_len
= 0;
993 if (!smbios_immutable
) {
994 smbios_build_type_0_table();
995 smbios_build_type_1_table();
996 smbios_build_type_2_table();
997 smbios_build_type_3_table();
999 smbios_smp_sockets
= DIV_ROUND_UP(ms
->smp
.cpus
,
1000 ms
->smp
.cores
* ms
->smp
.threads
);
1001 assert(smbios_smp_sockets
>= 1);
1003 for (i
= 0; i
< smbios_smp_sockets
; i
++) {
1004 smbios_build_type_4_table(ms
, i
);
1007 smbios_build_type_11_table();
1009 #define MAX_DIMM_SZ (16 * GiB)
1010 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
1011 : ((current_machine->ram_size - 1) % MAX_DIMM_SZ) + 1)
1013 dimm_cnt
= QEMU_ALIGN_UP(current_machine
->ram_size
, MAX_DIMM_SZ
) / MAX_DIMM_SZ
;
1015 smbios_build_type_16_table(dimm_cnt
);
1017 for (i
= 0; i
< dimm_cnt
; i
++) {
1018 smbios_build_type_17_table(i
, GET_DIMM_SZ
);
1021 for (i
= 0; i
< mem_array_size
; i
++) {
1022 smbios_build_type_19_table(i
, mem_array
[i
].address
,
1023 mem_array
[i
].length
);
1026 smbios_build_type_32_table();
1027 smbios_build_type_38_table();
1028 smbios_build_type_41_table(errp
);
1029 smbios_build_type_127_table();
1031 smbios_validate_table(ms
);
1032 smbios_entry_point_setup();
1033 smbios_immutable
= true;
1036 /* return tables blob and entry point (anchor), and their sizes */
1037 *tables
= smbios_tables
;
1038 *tables_len
= smbios_tables_len
;
1039 *anchor
= (uint8_t *)&ep
;
1041 /* calculate length based on anchor string */
1042 if (!strncmp((char *)&ep
, "_SM_", 4)) {
1043 *anchor_len
= sizeof(struct smbios_21_entry_point
);
1044 } else if (!strncmp((char *)&ep
, "_SM3_", 5)) {
1045 *anchor_len
= sizeof(struct smbios_30_entry_point
);
1051 static void save_opt(const char **dest
, QemuOpts
*opts
, const char *name
)
1053 const char *val
= qemu_opt_get(opts
, name
);
1066 static int save_opt_one(void *opaque
,
1067 const char *name
, const char *value
,
1070 struct opt_list
*opt
= opaque
;
1072 if (g_str_equal(name
, "path")) {
1073 g_autoptr(GByteArray
) data
= g_byte_array_new();
1074 g_autofree
char *buf
= g_new(char, 4096);
1076 int fd
= qemu_open(value
, O_RDONLY
, errp
);
1082 ret
= read(fd
, buf
, 4096);
1087 error_setg(errp
, "Unable to read from %s: %s",
1088 value
, strerror(errno
));
1092 if (memchr(buf
, '\0', ret
)) {
1093 error_setg(errp
, "NUL in OEM strings value in %s", value
);
1097 g_byte_array_append(data
, (guint8
*)buf
, ret
);
1102 *opt
->dest
= g_renew(char *, *opt
->dest
, (*opt
->ndest
) + 1);
1103 (*opt
->dest
)[*opt
->ndest
] = (char *)g_byte_array_free(data
, FALSE
);
1106 } else if (g_str_equal(name
, "value")) {
1107 *opt
->dest
= g_renew(char *, *opt
->dest
, (*opt
->ndest
) + 1);
1108 (*opt
->dest
)[*opt
->ndest
] = g_strdup(value
);
1110 } else if (!g_str_equal(name
, "type")) {
1111 error_setg(errp
, "Unexpected option %s", name
);
1118 static bool save_opt_list(size_t *ndest
, char ***dest
, QemuOpts
*opts
,
1121 struct opt_list opt
= {
1124 if (!qemu_opt_foreach(opts
, save_opt_one
, &opt
, errp
)) {
1130 void smbios_entry_add(QemuOpts
*opts
, Error
**errp
)
1134 assert(!smbios_immutable
);
1136 val
= qemu_opt_get(opts
, "file");
1138 struct smbios_structure_header
*header
;
1140 struct smbios_table
*table
; /* legacy mode only */
1142 if (!qemu_opts_validate(opts
, qemu_smbios_file_opts
, errp
)) {
1146 size
= get_image_size(val
);
1147 if (size
== -1 || size
< sizeof(struct smbios_structure_header
)) {
1148 error_setg(errp
, "Cannot read SMBIOS file %s", val
);
1153 * NOTE: standard double '\0' terminator expected, per smbios spec.
1154 * (except in legacy mode, where the second '\0' is implicit and
1155 * will be inserted by the BIOS).
1157 smbios_tables
= g_realloc(smbios_tables
, smbios_tables_len
+ size
);
1158 header
= (struct smbios_structure_header
*)(smbios_tables
+
1161 if (load_image_size(val
, (uint8_t *)header
, size
) != size
) {
1162 error_setg(errp
, "Failed to load SMBIOS file %s", val
);
1166 if (test_bit(header
->type
, have_fields_bitmap
)) {
1168 "can't load type %d struct, fields already specified!",
1172 set_bit(header
->type
, have_binfile_bitmap
);
1174 if (header
->type
== 4) {
1175 smbios_type4_count
++;
1178 smbios_tables_len
+= size
;
1179 if (size
> smbios_table_max
) {
1180 smbios_table_max
= size
;
1184 /* add a copy of the newly loaded blob to legacy smbios_entries */
1185 /* NOTE: This code runs before smbios_set_defaults(), so we don't
1186 * yet know which mode (legacy vs. aggregate-table) will be
1187 * required. We therefore add the binary blob to both legacy
1188 * (smbios_entries) and aggregate (smbios_tables) tables, and
1189 * delete the one we don't need from smbios_set_defaults(),
1190 * once we know which machine version has been requested.
1192 if (!smbios_entries
) {
1193 smbios_entries_len
= sizeof(uint16_t);
1194 smbios_entries
= g_malloc0(smbios_entries_len
);
1196 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
1197 size
+ sizeof(*table
));
1198 table
= (struct smbios_table
*)(smbios_entries
+ smbios_entries_len
);
1199 table
->header
.type
= SMBIOS_TABLE_ENTRY
;
1200 table
->header
.length
= cpu_to_le16(sizeof(*table
) + size
);
1201 memcpy(table
->data
, header
, size
);
1202 smbios_entries_len
+= sizeof(*table
) + size
;
1203 (*(uint16_t *)smbios_entries
) =
1204 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
1205 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1210 val
= qemu_opt_get(opts
, "type");
1212 unsigned long type
= strtoul(val
, NULL
, 0);
1214 if (type
> SMBIOS_MAX_TYPE
) {
1215 error_setg(errp
, "out of range!");
1219 if (test_bit(type
, have_binfile_bitmap
)) {
1220 error_setg(errp
, "can't add fields, binary file already loaded!");
1223 set_bit(type
, have_fields_bitmap
);
1227 if (!qemu_opts_validate(opts
, qemu_smbios_type0_opts
, errp
)) {
1230 save_opt(&type0
.vendor
, opts
, "vendor");
1231 save_opt(&type0
.version
, opts
, "version");
1232 save_opt(&type0
.date
, opts
, "date");
1233 type0
.uefi
= qemu_opt_get_bool(opts
, "uefi", false);
1235 val
= qemu_opt_get(opts
, "release");
1237 if (sscanf(val
, "%hhu.%hhu", &type0
.major
, &type0
.minor
) != 2) {
1238 error_setg(errp
, "Invalid release");
1241 type0
.have_major_minor
= true;
1245 if (!qemu_opts_validate(opts
, qemu_smbios_type1_opts
, errp
)) {
1248 save_opt(&type1
.manufacturer
, opts
, "manufacturer");
1249 save_opt(&type1
.product
, opts
, "product");
1250 save_opt(&type1
.version
, opts
, "version");
1251 save_opt(&type1
.serial
, opts
, "serial");
1252 save_opt(&type1
.sku
, opts
, "sku");
1253 save_opt(&type1
.family
, opts
, "family");
1255 val
= qemu_opt_get(opts
, "uuid");
1257 if (qemu_uuid_parse(val
, &qemu_uuid
) != 0) {
1258 error_setg(errp
, "Invalid UUID");
1261 qemu_uuid_set
= true;
1265 if (!qemu_opts_validate(opts
, qemu_smbios_type2_opts
, errp
)) {
1268 save_opt(&type2
.manufacturer
, opts
, "manufacturer");
1269 save_opt(&type2
.product
, opts
, "product");
1270 save_opt(&type2
.version
, opts
, "version");
1271 save_opt(&type2
.serial
, opts
, "serial");
1272 save_opt(&type2
.asset
, opts
, "asset");
1273 save_opt(&type2
.location
, opts
, "location");
1276 if (!qemu_opts_validate(opts
, qemu_smbios_type3_opts
, errp
)) {
1279 save_opt(&type3
.manufacturer
, opts
, "manufacturer");
1280 save_opt(&type3
.version
, opts
, "version");
1281 save_opt(&type3
.serial
, opts
, "serial");
1282 save_opt(&type3
.asset
, opts
, "asset");
1283 save_opt(&type3
.sku
, opts
, "sku");
1286 if (!qemu_opts_validate(opts
, qemu_smbios_type4_opts
, errp
)) {
1289 save_opt(&type4
.sock_pfx
, opts
, "sock_pfx");
1290 save_opt(&type4
.manufacturer
, opts
, "manufacturer");
1291 save_opt(&type4
.version
, opts
, "version");
1292 save_opt(&type4
.serial
, opts
, "serial");
1293 save_opt(&type4
.asset
, opts
, "asset");
1294 save_opt(&type4
.part
, opts
, "part");
1295 type4
.max_speed
= qemu_opt_get_number(opts
, "max-speed",
1297 type4
.current_speed
= qemu_opt_get_number(opts
, "current-speed",
1299 if (type4
.max_speed
> UINT16_MAX
||
1300 type4
.current_speed
> UINT16_MAX
) {
1301 error_setg(errp
, "SMBIOS CPU speed is too large (> %d)",
1306 if (!qemu_opts_validate(opts
, qemu_smbios_type11_opts
, errp
)) {
1309 if (!save_opt_list(&type11
.nvalues
, &type11
.values
, opts
, errp
)) {
1314 if (!qemu_opts_validate(opts
, qemu_smbios_type17_opts
, errp
)) {
1317 save_opt(&type17
.loc_pfx
, opts
, "loc_pfx");
1318 save_opt(&type17
.bank
, opts
, "bank");
1319 save_opt(&type17
.manufacturer
, opts
, "manufacturer");
1320 save_opt(&type17
.serial
, opts
, "serial");
1321 save_opt(&type17
.asset
, opts
, "asset");
1322 save_opt(&type17
.part
, opts
, "part");
1323 type17
.speed
= qemu_opt_get_number(opts
, "speed", 0);
1326 struct type41_instance
*t
;
1327 Error
*local_err
= NULL
;
1329 if (!qemu_opts_validate(opts
, qemu_smbios_type41_opts
, errp
)) {
1332 t
= g_new0(struct type41_instance
, 1);
1333 save_opt(&t
->designation
, opts
, "designation");
1334 t
->kind
= qapi_enum_parse(&type41_kind_lookup
,
1335 qemu_opt_get(opts
, "kind"),
1337 t
->kind
|= 0x80; /* enabled */
1338 if (local_err
!= NULL
) {
1339 error_propagate(errp
, local_err
);
1343 t
->instance
= qemu_opt_get_number(opts
, "instance", 1);
1344 save_opt(&t
->pcidev
, opts
, "pcidev");
1346 QTAILQ_INSERT_TAIL(&type41
, t
, next
);
1351 "Don't know how to build fields for SMBIOS type %ld",
1357 error_setg(errp
, "Must specify type= or file=");