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
;
107 uint64_t processor_id
;
109 .max_speed
= DEFAULT_CPU_SPEED
,
110 .current_speed
= DEFAULT_CPU_SPEED
,
120 const char *loc_pfx
, *bank
, *manufacturer
, *serial
, *asset
, *part
;
124 static QEnumLookup type41_kind_lookup
= {
125 .array
= (const char *const[]) {
139 struct type41_instance
{
140 const char *designation
, *pcidev
;
141 uint8_t instance
, kind
;
142 QTAILQ_ENTRY(type41_instance
) next
;
144 static QTAILQ_HEAD(, type41_instance
) type41
= QTAILQ_HEAD_INITIALIZER(type41
);
146 static QemuOptsList qemu_smbios_opts
= {
148 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts
.head
),
151 * no elements => accept any params
152 * validation will happen later
154 { /* end of list */ }
158 static const QemuOptDesc qemu_smbios_file_opts
[] = {
161 .type
= QEMU_OPT_STRING
,
162 .help
= "binary file containing an SMBIOS element",
164 { /* end of list */ }
167 static const QemuOptDesc qemu_smbios_type0_opts
[] = {
170 .type
= QEMU_OPT_NUMBER
,
171 .help
= "SMBIOS element type",
174 .type
= QEMU_OPT_STRING
,
175 .help
= "vendor name",
178 .type
= QEMU_OPT_STRING
,
179 .help
= "version number",
182 .type
= QEMU_OPT_STRING
,
183 .help
= "release date",
186 .type
= QEMU_OPT_STRING
,
187 .help
= "revision number",
190 .type
= QEMU_OPT_BOOL
,
191 .help
= "uefi support",
193 { /* end of list */ }
196 static const QemuOptDesc qemu_smbios_type1_opts
[] = {
199 .type
= QEMU_OPT_NUMBER
,
200 .help
= "SMBIOS element type",
202 .name
= "manufacturer",
203 .type
= QEMU_OPT_STRING
,
204 .help
= "manufacturer name",
207 .type
= QEMU_OPT_STRING
,
208 .help
= "product name",
211 .type
= QEMU_OPT_STRING
,
212 .help
= "version number",
215 .type
= QEMU_OPT_STRING
,
216 .help
= "serial number",
219 .type
= QEMU_OPT_STRING
,
223 .type
= QEMU_OPT_STRING
,
224 .help
= "SKU number",
227 .type
= QEMU_OPT_STRING
,
228 .help
= "family name",
230 { /* end of list */ }
233 static const QemuOptDesc qemu_smbios_type2_opts
[] = {
236 .type
= QEMU_OPT_NUMBER
,
237 .help
= "SMBIOS element type",
239 .name
= "manufacturer",
240 .type
= QEMU_OPT_STRING
,
241 .help
= "manufacturer name",
244 .type
= QEMU_OPT_STRING
,
245 .help
= "product name",
248 .type
= QEMU_OPT_STRING
,
249 .help
= "version number",
252 .type
= QEMU_OPT_STRING
,
253 .help
= "serial number",
256 .type
= QEMU_OPT_STRING
,
257 .help
= "asset tag number",
260 .type
= QEMU_OPT_STRING
,
261 .help
= "location in chassis",
263 { /* end of list */ }
266 static const QemuOptDesc qemu_smbios_type3_opts
[] = {
269 .type
= QEMU_OPT_NUMBER
,
270 .help
= "SMBIOS element type",
272 .name
= "manufacturer",
273 .type
= QEMU_OPT_STRING
,
274 .help
= "manufacturer name",
277 .type
= QEMU_OPT_STRING
,
278 .help
= "version number",
281 .type
= QEMU_OPT_STRING
,
282 .help
= "serial number",
285 .type
= QEMU_OPT_STRING
,
286 .help
= "asset tag number",
289 .type
= QEMU_OPT_STRING
,
290 .help
= "SKU number",
292 { /* end of list */ }
295 static const QemuOptDesc qemu_smbios_type4_opts
[] = {
298 .type
= QEMU_OPT_NUMBER
,
299 .help
= "SMBIOS element type",
302 .type
= QEMU_OPT_STRING
,
303 .help
= "socket designation string prefix",
305 .name
= "manufacturer",
306 .type
= QEMU_OPT_STRING
,
307 .help
= "manufacturer name",
310 .type
= QEMU_OPT_STRING
,
311 .help
= "version number",
314 .type
= QEMU_OPT_NUMBER
,
315 .help
= "max speed in MHz",
317 .name
= "current-speed",
318 .type
= QEMU_OPT_NUMBER
,
319 .help
= "speed at system boot in MHz",
322 .type
= QEMU_OPT_STRING
,
323 .help
= "serial number",
326 .type
= QEMU_OPT_STRING
,
327 .help
= "asset tag number",
330 .type
= QEMU_OPT_STRING
,
331 .help
= "part number",
333 .name
= "processor-id",
334 .type
= QEMU_OPT_NUMBER
,
335 .help
= "processor id",
337 { /* end of list */ }
340 static const QemuOptDesc qemu_smbios_type11_opts
[] = {
343 .type
= QEMU_OPT_STRING
,
344 .help
= "OEM string data",
348 .type
= QEMU_OPT_STRING
,
349 .help
= "OEM string data from file",
353 static const QemuOptDesc qemu_smbios_type17_opts
[] = {
356 .type
= QEMU_OPT_NUMBER
,
357 .help
= "SMBIOS element type",
360 .type
= QEMU_OPT_STRING
,
361 .help
= "device locator string prefix",
364 .type
= QEMU_OPT_STRING
,
365 .help
= "bank locator string",
367 .name
= "manufacturer",
368 .type
= QEMU_OPT_STRING
,
369 .help
= "manufacturer name",
372 .type
= QEMU_OPT_STRING
,
373 .help
= "serial number",
376 .type
= QEMU_OPT_STRING
,
377 .help
= "asset tag number",
380 .type
= QEMU_OPT_STRING
,
381 .help
= "part number",
384 .type
= QEMU_OPT_NUMBER
,
385 .help
= "maximum capable speed",
387 { /* end of list */ }
390 static const QemuOptDesc qemu_smbios_type41_opts
[] = {
393 .type
= QEMU_OPT_NUMBER
,
394 .help
= "SMBIOS element type",
396 .name
= "designation",
397 .type
= QEMU_OPT_STRING
,
398 .help
= "reference designation string",
401 .type
= QEMU_OPT_STRING
,
402 .help
= "device type",
403 .def_value_str
= "other",
406 .type
= QEMU_OPT_NUMBER
,
407 .help
= "device type instance",
410 .type
= QEMU_OPT_STRING
,
411 .help
= "PCI device",
413 { /* end of list */ }
416 static void smbios_register_config(void)
418 qemu_add_opts(&qemu_smbios_opts
);
421 opts_init(smbios_register_config
);
424 * The SMBIOS 2.1 "structure table length" field in the
425 * entry point uses a 16-bit integer, so we're limited
426 * in total table size
428 #define SMBIOS_21_MAX_TABLES_LEN 0xffff
430 static void smbios_validate_table(MachineState
*ms
)
432 uint32_t expect_t4_count
= smbios_legacy
?
433 ms
->smp
.cpus
: smbios_smp_sockets
;
435 if (smbios_type4_count
&& smbios_type4_count
!= expect_t4_count
) {
436 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
437 expect_t4_count
, smbios_type4_count
);
441 if (smbios_ep_type
== SMBIOS_ENTRY_POINT_TYPE_32
&&
442 smbios_tables_len
> SMBIOS_21_MAX_TABLES_LEN
) {
443 error_report("SMBIOS 2.1 table length %zu exceeds %d",
444 smbios_tables_len
, SMBIOS_21_MAX_TABLES_LEN
);
450 /* legacy setup functions for <= 2.0 machines */
451 static void smbios_add_field(int type
, int offset
, const void *data
, size_t len
)
453 struct smbios_field
*field
;
455 if (!smbios_entries
) {
456 smbios_entries_len
= sizeof(uint16_t);
457 smbios_entries
= g_malloc0(smbios_entries_len
);
459 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
460 sizeof(*field
) + len
);
461 field
= (struct smbios_field
*)(smbios_entries
+ smbios_entries_len
);
462 field
->header
.type
= SMBIOS_FIELD_ENTRY
;
463 field
->header
.length
= cpu_to_le16(sizeof(*field
) + len
);
466 field
->offset
= cpu_to_le16(offset
);
467 memcpy(field
->data
, data
, len
);
469 smbios_entries_len
+= sizeof(*field
) + len
;
470 (*(uint16_t *)smbios_entries
) =
471 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
474 static void smbios_maybe_add_str(int type
, int offset
, const char *data
)
477 smbios_add_field(type
, offset
, data
, strlen(data
) + 1);
481 static void smbios_build_type_0_fields(void)
483 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, vendor_str
),
485 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, bios_version_str
),
487 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
,
488 bios_release_date_str
),
490 if (type0
.have_major_minor
) {
491 smbios_add_field(0, offsetof(struct smbios_type_0
,
492 system_bios_major_release
),
494 smbios_add_field(0, offsetof(struct smbios_type_0
,
495 system_bios_minor_release
),
500 static void smbios_build_type_1_fields(void)
502 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, manufacturer_str
),
504 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, product_name_str
),
506 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, version_str
),
508 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, serial_number_str
),
510 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, sku_number_str
),
512 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, family_str
),
515 /* We don't encode the UUID in the "wire format" here because this
516 * function is for legacy mode and needs to keep the guest ABI, and
517 * because we don't know what's the SMBIOS version advertised by the
520 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
),
525 uint8_t *smbios_get_table_legacy(MachineState
*ms
, size_t *length
)
527 if (!smbios_legacy
) {
532 if (!smbios_immutable
) {
533 smbios_build_type_0_fields();
534 smbios_build_type_1_fields();
535 smbios_validate_table(ms
);
536 smbios_immutable
= true;
538 *length
= smbios_entries_len
;
539 return smbios_entries
;
541 /* end: legacy setup functions for <= 2.0 machines */
544 bool smbios_skip_table(uint8_t type
, bool required_table
)
546 if (test_bit(type
, have_binfile_bitmap
)) {
547 return true; /* user provided their own binary blob(s) */
549 if (test_bit(type
, have_fields_bitmap
)) {
550 return false; /* user provided fields via command line */
552 if (smbios_have_defaults
&& required_table
) {
553 return false; /* we're building tables, and this one's required */
558 #define T0_BASE 0x000
559 #define T1_BASE 0x100
560 #define T2_BASE 0x200
561 #define T3_BASE 0x300
562 #define T4_BASE 0x400
563 #define T11_BASE 0xe00
565 #define T16_BASE 0x1000
566 #define T17_BASE 0x1100
567 #define T19_BASE 0x1300
568 #define T32_BASE 0x2000
569 #define T41_BASE 0x2900
570 #define T127_BASE 0x7F00
572 static void smbios_build_type_0_table(void)
574 SMBIOS_BUILD_TABLE_PRE(0, T0_BASE
, false); /* optional, leave up to BIOS */
576 SMBIOS_TABLE_SET_STR(0, vendor_str
, type0
.vendor
);
577 SMBIOS_TABLE_SET_STR(0, bios_version_str
, type0
.version
);
579 t
->bios_starting_address_segment
= cpu_to_le16(0xE800); /* from SeaBIOS */
581 SMBIOS_TABLE_SET_STR(0, bios_release_date_str
, type0
.date
);
583 t
->bios_rom_size
= 0; /* hardcoded in SeaBIOS with FIXME comment */
585 t
->bios_characteristics
= cpu_to_le64(0x08); /* Not supported */
586 t
->bios_characteristics_extension_bytes
[0] = 0;
587 t
->bios_characteristics_extension_bytes
[1] = 0x14; /* TCD/SVVP | VM */
589 t
->bios_characteristics_extension_bytes
[1] |= 0x08; /* |= UEFI */
592 if (type0
.have_major_minor
) {
593 t
->system_bios_major_release
= type0
.major
;
594 t
->system_bios_minor_release
= type0
.minor
;
596 t
->system_bios_major_release
= 0;
597 t
->system_bios_minor_release
= 0;
600 /* hardcoded in SeaBIOS */
601 t
->embedded_controller_major_release
= 0xFF;
602 t
->embedded_controller_minor_release
= 0xFF;
604 SMBIOS_BUILD_TABLE_POST
;
607 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
608 * format specified by SMBIOS version 2.6.
610 static void smbios_encode_uuid(struct smbios_uuid
*uuid
, QemuUUID
*in
)
612 memcpy(uuid
, in
, 16);
613 if (smbios_uuid_encoded
) {
614 uuid
->time_low
= bswap32(uuid
->time_low
);
615 uuid
->time_mid
= bswap16(uuid
->time_mid
);
616 uuid
->time_hi_and_version
= bswap16(uuid
->time_hi_and_version
);
620 static void smbios_build_type_1_table(void)
622 SMBIOS_BUILD_TABLE_PRE(1, T1_BASE
, true); /* required */
624 SMBIOS_TABLE_SET_STR(1, manufacturer_str
, type1
.manufacturer
);
625 SMBIOS_TABLE_SET_STR(1, product_name_str
, type1
.product
);
626 SMBIOS_TABLE_SET_STR(1, version_str
, type1
.version
);
627 SMBIOS_TABLE_SET_STR(1, serial_number_str
, type1
.serial
);
629 smbios_encode_uuid(&t
->uuid
, &qemu_uuid
);
631 memset(&t
->uuid
, 0, 16);
633 t
->wake_up_type
= 0x06; /* power switch */
634 SMBIOS_TABLE_SET_STR(1, sku_number_str
, type1
.sku
);
635 SMBIOS_TABLE_SET_STR(1, family_str
, type1
.family
);
637 SMBIOS_BUILD_TABLE_POST
;
640 static void smbios_build_type_2_table(void)
642 SMBIOS_BUILD_TABLE_PRE(2, T2_BASE
, false); /* optional */
644 SMBIOS_TABLE_SET_STR(2, manufacturer_str
, type2
.manufacturer
);
645 SMBIOS_TABLE_SET_STR(2, product_str
, type2
.product
);
646 SMBIOS_TABLE_SET_STR(2, version_str
, type2
.version
);
647 SMBIOS_TABLE_SET_STR(2, serial_number_str
, type2
.serial
);
648 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str
, type2
.asset
);
649 t
->feature_flags
= 0x01; /* Motherboard */
650 SMBIOS_TABLE_SET_STR(2, location_str
, type2
.location
);
651 t
->chassis_handle
= cpu_to_le16(0x300); /* Type 3 (System enclosure) */
652 t
->board_type
= 0x0A; /* Motherboard */
653 t
->contained_element_count
= 0;
655 SMBIOS_BUILD_TABLE_POST
;
658 static void smbios_build_type_3_table(void)
660 SMBIOS_BUILD_TABLE_PRE(3, T3_BASE
, true); /* required */
662 SMBIOS_TABLE_SET_STR(3, manufacturer_str
, type3
.manufacturer
);
663 t
->type
= 0x01; /* Other */
664 SMBIOS_TABLE_SET_STR(3, version_str
, type3
.version
);
665 SMBIOS_TABLE_SET_STR(3, serial_number_str
, type3
.serial
);
666 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str
, type3
.asset
);
667 t
->boot_up_state
= 0x03; /* Safe */
668 t
->power_supply_state
= 0x03; /* Safe */
669 t
->thermal_state
= 0x03; /* Safe */
670 t
->security_status
= 0x02; /* Unknown */
671 t
->oem_defined
= cpu_to_le32(0);
673 t
->number_of_power_cords
= 0;
674 t
->contained_element_count
= 0;
675 t
->contained_element_record_length
= 0;
676 SMBIOS_TABLE_SET_STR(3, sku_number_str
, type3
.sku
);
678 SMBIOS_BUILD_TABLE_POST
;
681 static void smbios_build_type_4_table(MachineState
*ms
, unsigned instance
)
685 SMBIOS_BUILD_TABLE_PRE(4, T4_BASE
+ instance
, true); /* required */
687 snprintf(sock_str
, sizeof(sock_str
), "%s%2x", type4
.sock_pfx
, instance
);
688 SMBIOS_TABLE_SET_STR(4, socket_designation_str
, sock_str
);
689 t
->processor_type
= 0x03; /* CPU */
690 t
->processor_family
= 0x01; /* Other */
691 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str
, type4
.manufacturer
);
692 if (type4
.processor_id
== 0) {
693 t
->processor_id
[0] = cpu_to_le32(smbios_cpuid_version
);
694 t
->processor_id
[1] = cpu_to_le32(smbios_cpuid_features
);
696 t
->processor_id
[0] = cpu_to_le32((uint32_t)type4
.processor_id
);
697 t
->processor_id
[1] = cpu_to_le32(type4
.processor_id
>> 32);
699 SMBIOS_TABLE_SET_STR(4, processor_version_str
, type4
.version
);
701 t
->external_clock
= cpu_to_le16(0); /* Unknown */
702 t
->max_speed
= cpu_to_le16(type4
.max_speed
);
703 t
->current_speed
= cpu_to_le16(type4
.current_speed
);
704 t
->status
= 0x41; /* Socket populated, CPU enabled */
705 t
->processor_upgrade
= 0x01; /* Other */
706 t
->l1_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
707 t
->l2_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
708 t
->l3_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
709 SMBIOS_TABLE_SET_STR(4, serial_number_str
, type4
.serial
);
710 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str
, type4
.asset
);
711 SMBIOS_TABLE_SET_STR(4, part_number_str
, type4
.part
);
712 t
->core_count
= t
->core_enabled
= ms
->smp
.cores
;
713 t
->thread_count
= ms
->smp
.threads
;
714 t
->processor_characteristics
= cpu_to_le16(0x02); /* Unknown */
715 t
->processor_family2
= cpu_to_le16(0x01); /* Other */
717 SMBIOS_BUILD_TABLE_POST
;
718 smbios_type4_count
++;
721 static void smbios_build_type_11_table(void)
726 if (type11
.nvalues
== 0) {
730 SMBIOS_BUILD_TABLE_PRE(11, T11_BASE
, true); /* required */
732 snprintf(count_str
, sizeof(count_str
), "%zu", type11
.nvalues
);
733 t
->count
= type11
.nvalues
;
735 for (i
= 0; i
< type11
.nvalues
; i
++) {
736 SMBIOS_TABLE_SET_STR_LIST(11, type11
.values
[i
]);
737 g_free(type11
.values
[i
]);
738 type11
.values
[i
] = NULL
;
741 SMBIOS_BUILD_TABLE_POST
;
744 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
746 static void smbios_build_type_16_table(unsigned dimm_cnt
)
750 SMBIOS_BUILD_TABLE_PRE(16, T16_BASE
, true); /* required */
752 t
->location
= 0x01; /* Other */
753 t
->use
= 0x03; /* System memory */
754 t
->error_correction
= 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
755 size_kb
= QEMU_ALIGN_UP(current_machine
->ram_size
, KiB
) / KiB
;
756 if (size_kb
< MAX_T16_STD_SZ
) {
757 t
->maximum_capacity
= cpu_to_le32(size_kb
);
758 t
->extended_maximum_capacity
= cpu_to_le64(0);
760 t
->maximum_capacity
= cpu_to_le32(MAX_T16_STD_SZ
);
761 t
->extended_maximum_capacity
= cpu_to_le64(current_machine
->ram_size
);
763 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
764 t
->number_of_memory_devices
= cpu_to_le16(dimm_cnt
);
766 SMBIOS_BUILD_TABLE_POST
;
769 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
770 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
772 static void smbios_build_type_17_table(unsigned instance
, uint64_t size
)
777 SMBIOS_BUILD_TABLE_PRE(17, T17_BASE
+ instance
, true); /* required */
779 t
->physical_memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
780 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
781 t
->total_width
= cpu_to_le16(0xFFFF); /* Unknown */
782 t
->data_width
= cpu_to_le16(0xFFFF); /* Unknown */
783 size_mb
= QEMU_ALIGN_UP(size
, MiB
) / MiB
;
784 if (size_mb
< MAX_T17_STD_SZ
) {
785 t
->size
= cpu_to_le16(size_mb
);
786 t
->extended_size
= cpu_to_le32(0);
788 assert(size_mb
< MAX_T17_EXT_SZ
);
789 t
->size
= cpu_to_le16(MAX_T17_STD_SZ
);
790 t
->extended_size
= cpu_to_le32(size_mb
);
792 t
->form_factor
= 0x09; /* DIMM */
793 t
->device_set
= 0; /* Not in a set */
794 snprintf(loc_str
, sizeof(loc_str
), "%s %d", type17
.loc_pfx
, instance
);
795 SMBIOS_TABLE_SET_STR(17, device_locator_str
, loc_str
);
796 SMBIOS_TABLE_SET_STR(17, bank_locator_str
, type17
.bank
);
797 t
->memory_type
= 0x07; /* RAM */
798 t
->type_detail
= cpu_to_le16(0x02); /* Other */
799 t
->speed
= cpu_to_le16(type17
.speed
);
800 SMBIOS_TABLE_SET_STR(17, manufacturer_str
, type17
.manufacturer
);
801 SMBIOS_TABLE_SET_STR(17, serial_number_str
, type17
.serial
);
802 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str
, type17
.asset
);
803 SMBIOS_TABLE_SET_STR(17, part_number_str
, type17
.part
);
804 t
->attributes
= 0; /* Unknown */
805 t
->configured_clock_speed
= t
->speed
; /* reuse value for max speed */
806 t
->minimum_voltage
= cpu_to_le16(0); /* Unknown */
807 t
->maximum_voltage
= cpu_to_le16(0); /* Unknown */
808 t
->configured_voltage
= cpu_to_le16(0); /* Unknown */
810 SMBIOS_BUILD_TABLE_POST
;
813 static void smbios_build_type_19_table(unsigned instance
, unsigned offset
,
814 uint64_t start
, uint64_t size
)
816 uint64_t end
, start_kb
, end_kb
;
818 SMBIOS_BUILD_TABLE_PRE(19, T19_BASE
+ offset
+ instance
,
819 true); /* required */
821 end
= start
+ size
- 1;
823 start_kb
= start
/ KiB
;
825 if (start_kb
< UINT32_MAX
&& end_kb
< UINT32_MAX
) {
826 t
->starting_address
= cpu_to_le32(start_kb
);
827 t
->ending_address
= cpu_to_le32(end_kb
);
828 t
->extended_starting_address
=
829 t
->extended_ending_address
= cpu_to_le64(0);
831 t
->starting_address
= t
->ending_address
= cpu_to_le32(UINT32_MAX
);
832 t
->extended_starting_address
= cpu_to_le64(start
);
833 t
->extended_ending_address
= cpu_to_le64(end
);
835 t
->memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
836 t
->partition_width
= 1; /* One device per row */
838 SMBIOS_BUILD_TABLE_POST
;
841 static void smbios_build_type_32_table(void)
843 SMBIOS_BUILD_TABLE_PRE(32, T32_BASE
, true); /* required */
845 memset(t
->reserved
, 0, 6);
846 t
->boot_status
= 0; /* No errors detected */
848 SMBIOS_BUILD_TABLE_POST
;
851 static void smbios_build_type_41_table(Error
**errp
)
853 unsigned instance
= 0;
854 struct type41_instance
*t41
;
856 QTAILQ_FOREACH(t41
, &type41
, next
) {
857 SMBIOS_BUILD_TABLE_PRE(41, T41_BASE
+ instance
, true);
859 SMBIOS_TABLE_SET_STR(41, reference_designation_str
, t41
->designation
);
860 t
->device_type
= t41
->kind
;
861 t
->device_type_instance
= t41
->instance
;
862 t
->segment_group_number
= cpu_to_le16(0);
864 t
->device_number
= 0;
867 PCIDevice
*pdev
= NULL
;
868 int rc
= pci_qdev_find_device(t41
->pcidev
, &pdev
);
871 "No PCI device %s for SMBIOS type 41 entry %s",
872 t41
->pcidev
, t41
->designation
);
876 * We only handle the case were the device is attached to
877 * the PCI root bus. The general case is more complex as
878 * bridges are enumerated later and the table would need
879 * to be updated at this moment.
881 if (!pci_bus_is_root(pci_get_bus(pdev
))) {
883 "Cannot create type 41 entry for PCI device %s: "
884 "not attached to the root bus",
888 t
->segment_group_number
= cpu_to_le16(0);
889 t
->bus_number
= pci_dev_bus_num(pdev
);
890 t
->device_number
= pdev
->devfn
;
893 SMBIOS_BUILD_TABLE_POST
;
898 static void smbios_build_type_127_table(void)
900 SMBIOS_BUILD_TABLE_PRE(127, T127_BASE
, true); /* required */
901 SMBIOS_BUILD_TABLE_POST
;
904 void smbios_set_cpuid(uint32_t version
, uint32_t features
)
906 smbios_cpuid_version
= version
;
907 smbios_cpuid_features
= features
;
910 #define SMBIOS_SET_DEFAULT(field, value) \
915 void smbios_set_defaults(const char *manufacturer
, const char *product
,
916 const char *version
, bool legacy_mode
,
917 bool uuid_encoded
, SmbiosEntryPointType ep_type
)
919 smbios_have_defaults
= true;
920 smbios_legacy
= legacy_mode
;
921 smbios_uuid_encoded
= uuid_encoded
;
922 smbios_ep_type
= ep_type
;
924 /* drop unwanted version of command-line file blob(s) */
926 g_free(smbios_tables
);
927 /* in legacy mode, also complain if fields were given for types > 1 */
928 if (find_next_bit(have_fields_bitmap
,
929 SMBIOS_MAX_TYPE
+1, 2) < SMBIOS_MAX_TYPE
+1) {
930 error_report("can't process fields for smbios "
931 "types > 1 on machine versions < 2.1!");
935 g_free(smbios_entries
);
938 SMBIOS_SET_DEFAULT(type1
.manufacturer
, manufacturer
);
939 SMBIOS_SET_DEFAULT(type1
.product
, product
);
940 SMBIOS_SET_DEFAULT(type1
.version
, version
);
941 SMBIOS_SET_DEFAULT(type2
.manufacturer
, manufacturer
);
942 SMBIOS_SET_DEFAULT(type2
.product
, product
);
943 SMBIOS_SET_DEFAULT(type2
.version
, version
);
944 SMBIOS_SET_DEFAULT(type3
.manufacturer
, manufacturer
);
945 SMBIOS_SET_DEFAULT(type3
.version
, version
);
946 SMBIOS_SET_DEFAULT(type4
.sock_pfx
, "CPU");
947 SMBIOS_SET_DEFAULT(type4
.manufacturer
, manufacturer
);
948 SMBIOS_SET_DEFAULT(type4
.version
, version
);
949 SMBIOS_SET_DEFAULT(type17
.loc_pfx
, "DIMM");
950 SMBIOS_SET_DEFAULT(type17
.manufacturer
, manufacturer
);
953 static void smbios_entry_point_setup(void)
955 switch (smbios_ep_type
) {
956 case SMBIOS_ENTRY_POINT_TYPE_32
:
957 memcpy(ep
.ep21
.anchor_string
, "_SM_", 4);
958 memcpy(ep
.ep21
.intermediate_anchor_string
, "_DMI_", 5);
959 ep
.ep21
.length
= sizeof(struct smbios_21_entry_point
);
960 ep
.ep21
.entry_point_revision
= 0; /* formatted_area reserved */
961 memset(ep
.ep21
.formatted_area
, 0, 5);
963 /* compliant with smbios spec v2.8 */
964 ep
.ep21
.smbios_major_version
= 2;
965 ep
.ep21
.smbios_minor_version
= 8;
966 ep
.ep21
.smbios_bcd_revision
= 0x28;
968 /* set during table construction, but BIOS may override: */
969 ep
.ep21
.structure_table_length
= cpu_to_le16(smbios_tables_len
);
970 ep
.ep21
.max_structure_size
= cpu_to_le16(smbios_table_max
);
971 ep
.ep21
.number_of_structures
= cpu_to_le16(smbios_table_cnt
);
973 /* BIOS must recalculate */
974 ep
.ep21
.checksum
= 0;
975 ep
.ep21
.intermediate_checksum
= 0;
976 ep
.ep21
.structure_table_address
= cpu_to_le32(0);
979 case SMBIOS_ENTRY_POINT_TYPE_64
:
980 memcpy(ep
.ep30
.anchor_string
, "_SM3_", 5);
981 ep
.ep30
.length
= sizeof(struct smbios_30_entry_point
);
982 ep
.ep30
.entry_point_revision
= 1;
983 ep
.ep30
.reserved
= 0;
985 /* compliant with smbios spec 3.0 */
986 ep
.ep30
.smbios_major_version
= 3;
987 ep
.ep30
.smbios_minor_version
= 0;
988 ep
.ep30
.smbios_doc_rev
= 0;
990 /* set during table construct, but BIOS might override */
991 ep
.ep30
.structure_table_max_size
= cpu_to_le32(smbios_tables_len
);
993 /* BIOS must recalculate */
994 ep
.ep30
.checksum
= 0;
995 ep
.ep30
.structure_table_address
= cpu_to_le64(0);
1004 void smbios_get_tables(MachineState
*ms
,
1005 const struct smbios_phys_mem_area
*mem_array
,
1006 const unsigned int mem_array_size
,
1007 uint8_t **tables
, size_t *tables_len
,
1008 uint8_t **anchor
, size_t *anchor_len
,
1011 unsigned i
, dimm_cnt
, offset
;
1013 if (smbios_legacy
) {
1014 *tables
= *anchor
= NULL
;
1015 *tables_len
= *anchor_len
= 0;
1019 if (!smbios_immutable
) {
1020 smbios_build_type_0_table();
1021 smbios_build_type_1_table();
1022 smbios_build_type_2_table();
1023 smbios_build_type_3_table();
1025 smbios_smp_sockets
= DIV_ROUND_UP(ms
->smp
.cpus
,
1026 ms
->smp
.cores
* ms
->smp
.threads
);
1027 assert(smbios_smp_sockets
>= 1);
1029 for (i
= 0; i
< smbios_smp_sockets
; i
++) {
1030 smbios_build_type_4_table(ms
, i
);
1033 smbios_build_type_11_table();
1035 #define MAX_DIMM_SZ (16 * GiB)
1036 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
1037 : ((current_machine->ram_size - 1) % MAX_DIMM_SZ) + 1)
1039 dimm_cnt
= QEMU_ALIGN_UP(current_machine
->ram_size
, MAX_DIMM_SZ
) / MAX_DIMM_SZ
;
1042 * The offset determines if we need to keep additional space betweeen
1043 * table 17 and table 19 header handle numbers so that they do
1044 * not overlap. For example, for a VM with larger than 8 TB guest
1045 * memory and DIMM like chunks of 16 GiB, the default space between
1046 * the two tables (T19_BASE - T17_BASE = 512) is not enough.
1048 offset
= (dimm_cnt
> (T19_BASE
- T17_BASE
)) ? \
1049 dimm_cnt
- (T19_BASE
- T17_BASE
) : 0;
1051 smbios_build_type_16_table(dimm_cnt
);
1053 for (i
= 0; i
< dimm_cnt
; i
++) {
1054 smbios_build_type_17_table(i
, GET_DIMM_SZ
);
1057 for (i
= 0; i
< mem_array_size
; i
++) {
1058 smbios_build_type_19_table(i
, offset
, mem_array
[i
].address
,
1059 mem_array
[i
].length
);
1063 * make sure 16 bit handle numbers in the headers of tables 19
1064 * and 32 do not overlap.
1066 assert((mem_array_size
+ offset
) < (T32_BASE
- T19_BASE
));
1068 smbios_build_type_32_table();
1069 smbios_build_type_38_table();
1070 smbios_build_type_41_table(errp
);
1071 smbios_build_type_127_table();
1073 smbios_validate_table(ms
);
1074 smbios_entry_point_setup();
1075 smbios_immutable
= true;
1078 /* return tables blob and entry point (anchor), and their sizes */
1079 *tables
= smbios_tables
;
1080 *tables_len
= smbios_tables_len
;
1081 *anchor
= (uint8_t *)&ep
;
1083 /* calculate length based on anchor string */
1084 if (!strncmp((char *)&ep
, "_SM_", 4)) {
1085 *anchor_len
= sizeof(struct smbios_21_entry_point
);
1086 } else if (!strncmp((char *)&ep
, "_SM3_", 5)) {
1087 *anchor_len
= sizeof(struct smbios_30_entry_point
);
1093 static void save_opt(const char **dest
, QemuOpts
*opts
, const char *name
)
1095 const char *val
= qemu_opt_get(opts
, name
);
1108 static int save_opt_one(void *opaque
,
1109 const char *name
, const char *value
,
1112 struct opt_list
*opt
= opaque
;
1114 if (g_str_equal(name
, "path")) {
1115 g_autoptr(GByteArray
) data
= g_byte_array_new();
1116 g_autofree
char *buf
= g_new(char, 4096);
1118 int fd
= qemu_open(value
, O_RDONLY
, errp
);
1124 ret
= read(fd
, buf
, 4096);
1129 error_setg(errp
, "Unable to read from %s: %s",
1130 value
, strerror(errno
));
1134 if (memchr(buf
, '\0', ret
)) {
1135 error_setg(errp
, "NUL in OEM strings value in %s", value
);
1139 g_byte_array_append(data
, (guint8
*)buf
, ret
);
1144 *opt
->dest
= g_renew(char *, *opt
->dest
, (*opt
->ndest
) + 1);
1145 (*opt
->dest
)[*opt
->ndest
] = (char *)g_byte_array_free(data
, FALSE
);
1148 } else if (g_str_equal(name
, "value")) {
1149 *opt
->dest
= g_renew(char *, *opt
->dest
, (*opt
->ndest
) + 1);
1150 (*opt
->dest
)[*opt
->ndest
] = g_strdup(value
);
1152 } else if (!g_str_equal(name
, "type")) {
1153 error_setg(errp
, "Unexpected option %s", name
);
1160 static bool save_opt_list(size_t *ndest
, char ***dest
, QemuOpts
*opts
,
1163 struct opt_list opt
= {
1166 if (!qemu_opt_foreach(opts
, save_opt_one
, &opt
, errp
)) {
1172 void smbios_entry_add(QemuOpts
*opts
, Error
**errp
)
1176 assert(!smbios_immutable
);
1178 val
= qemu_opt_get(opts
, "file");
1180 struct smbios_structure_header
*header
;
1182 struct smbios_table
*table
; /* legacy mode only */
1184 if (!qemu_opts_validate(opts
, qemu_smbios_file_opts
, errp
)) {
1188 size
= get_image_size(val
);
1189 if (size
== -1 || size
< sizeof(struct smbios_structure_header
)) {
1190 error_setg(errp
, "Cannot read SMBIOS file %s", val
);
1195 * NOTE: standard double '\0' terminator expected, per smbios spec.
1196 * (except in legacy mode, where the second '\0' is implicit and
1197 * will be inserted by the BIOS).
1199 smbios_tables
= g_realloc(smbios_tables
, smbios_tables_len
+ size
);
1200 header
= (struct smbios_structure_header
*)(smbios_tables
+
1203 if (load_image_size(val
, (uint8_t *)header
, size
) != size
) {
1204 error_setg(errp
, "Failed to load SMBIOS file %s", val
);
1208 if (test_bit(header
->type
, have_fields_bitmap
)) {
1210 "can't load type %d struct, fields already specified!",
1214 set_bit(header
->type
, have_binfile_bitmap
);
1216 if (header
->type
== 4) {
1217 smbios_type4_count
++;
1220 smbios_tables_len
+= size
;
1221 if (size
> smbios_table_max
) {
1222 smbios_table_max
= size
;
1226 /* add a copy of the newly loaded blob to legacy smbios_entries */
1227 /* NOTE: This code runs before smbios_set_defaults(), so we don't
1228 * yet know which mode (legacy vs. aggregate-table) will be
1229 * required. We therefore add the binary blob to both legacy
1230 * (smbios_entries) and aggregate (smbios_tables) tables, and
1231 * delete the one we don't need from smbios_set_defaults(),
1232 * once we know which machine version has been requested.
1234 if (!smbios_entries
) {
1235 smbios_entries_len
= sizeof(uint16_t);
1236 smbios_entries
= g_malloc0(smbios_entries_len
);
1238 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
1239 size
+ sizeof(*table
));
1240 table
= (struct smbios_table
*)(smbios_entries
+ smbios_entries_len
);
1241 table
->header
.type
= SMBIOS_TABLE_ENTRY
;
1242 table
->header
.length
= cpu_to_le16(sizeof(*table
) + size
);
1243 memcpy(table
->data
, header
, size
);
1244 smbios_entries_len
+= sizeof(*table
) + size
;
1245 (*(uint16_t *)smbios_entries
) =
1246 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
1247 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1252 val
= qemu_opt_get(opts
, "type");
1254 unsigned long type
= strtoul(val
, NULL
, 0);
1256 if (type
> SMBIOS_MAX_TYPE
) {
1257 error_setg(errp
, "out of range!");
1261 if (test_bit(type
, have_binfile_bitmap
)) {
1262 error_setg(errp
, "can't add fields, binary file already loaded!");
1265 set_bit(type
, have_fields_bitmap
);
1269 if (!qemu_opts_validate(opts
, qemu_smbios_type0_opts
, errp
)) {
1272 save_opt(&type0
.vendor
, opts
, "vendor");
1273 save_opt(&type0
.version
, opts
, "version");
1274 save_opt(&type0
.date
, opts
, "date");
1275 type0
.uefi
= qemu_opt_get_bool(opts
, "uefi", false);
1277 val
= qemu_opt_get(opts
, "release");
1279 if (sscanf(val
, "%hhu.%hhu", &type0
.major
, &type0
.minor
) != 2) {
1280 error_setg(errp
, "Invalid release");
1283 type0
.have_major_minor
= true;
1287 if (!qemu_opts_validate(opts
, qemu_smbios_type1_opts
, errp
)) {
1290 save_opt(&type1
.manufacturer
, opts
, "manufacturer");
1291 save_opt(&type1
.product
, opts
, "product");
1292 save_opt(&type1
.version
, opts
, "version");
1293 save_opt(&type1
.serial
, opts
, "serial");
1294 save_opt(&type1
.sku
, opts
, "sku");
1295 save_opt(&type1
.family
, opts
, "family");
1297 val
= qemu_opt_get(opts
, "uuid");
1299 if (qemu_uuid_parse(val
, &qemu_uuid
) != 0) {
1300 error_setg(errp
, "Invalid UUID");
1303 qemu_uuid_set
= true;
1307 if (!qemu_opts_validate(opts
, qemu_smbios_type2_opts
, errp
)) {
1310 save_opt(&type2
.manufacturer
, opts
, "manufacturer");
1311 save_opt(&type2
.product
, opts
, "product");
1312 save_opt(&type2
.version
, opts
, "version");
1313 save_opt(&type2
.serial
, opts
, "serial");
1314 save_opt(&type2
.asset
, opts
, "asset");
1315 save_opt(&type2
.location
, opts
, "location");
1318 if (!qemu_opts_validate(opts
, qemu_smbios_type3_opts
, errp
)) {
1321 save_opt(&type3
.manufacturer
, opts
, "manufacturer");
1322 save_opt(&type3
.version
, opts
, "version");
1323 save_opt(&type3
.serial
, opts
, "serial");
1324 save_opt(&type3
.asset
, opts
, "asset");
1325 save_opt(&type3
.sku
, opts
, "sku");
1328 if (!qemu_opts_validate(opts
, qemu_smbios_type4_opts
, errp
)) {
1331 save_opt(&type4
.sock_pfx
, opts
, "sock_pfx");
1332 save_opt(&type4
.manufacturer
, opts
, "manufacturer");
1333 save_opt(&type4
.version
, opts
, "version");
1334 save_opt(&type4
.serial
, opts
, "serial");
1335 save_opt(&type4
.asset
, opts
, "asset");
1336 save_opt(&type4
.part
, opts
, "part");
1337 /* If the value is 0, it will take the value from the CPU model. */
1338 type4
.processor_id
= qemu_opt_get_number(opts
, "processor-id", 0);
1339 type4
.max_speed
= qemu_opt_get_number(opts
, "max-speed",
1341 type4
.current_speed
= qemu_opt_get_number(opts
, "current-speed",
1343 if (type4
.max_speed
> UINT16_MAX
||
1344 type4
.current_speed
> UINT16_MAX
) {
1345 error_setg(errp
, "SMBIOS CPU speed is too large (> %d)",
1350 if (!qemu_opts_validate(opts
, qemu_smbios_type11_opts
, errp
)) {
1353 if (!save_opt_list(&type11
.nvalues
, &type11
.values
, opts
, errp
)) {
1358 if (!qemu_opts_validate(opts
, qemu_smbios_type17_opts
, errp
)) {
1361 save_opt(&type17
.loc_pfx
, opts
, "loc_pfx");
1362 save_opt(&type17
.bank
, opts
, "bank");
1363 save_opt(&type17
.manufacturer
, opts
, "manufacturer");
1364 save_opt(&type17
.serial
, opts
, "serial");
1365 save_opt(&type17
.asset
, opts
, "asset");
1366 save_opt(&type17
.part
, opts
, "part");
1367 type17
.speed
= qemu_opt_get_number(opts
, "speed", 0);
1370 struct type41_instance
*t
;
1371 Error
*local_err
= NULL
;
1373 if (!qemu_opts_validate(opts
, qemu_smbios_type41_opts
, errp
)) {
1376 t
= g_new0(struct type41_instance
, 1);
1377 save_opt(&t
->designation
, opts
, "designation");
1378 t
->kind
= qapi_enum_parse(&type41_kind_lookup
,
1379 qemu_opt_get(opts
, "kind"),
1381 t
->kind
|= 0x80; /* enabled */
1382 if (local_err
!= NULL
) {
1383 error_propagate(errp
, local_err
);
1387 t
->instance
= qemu_opt_get_number(opts
, "instance", 1);
1388 save_opt(&t
->pcidev
, opts
, "pcidev");
1390 QTAILQ_INSERT_TAIL(&type41
, t
, next
);
1395 "Don't know how to build fields for SMBIOS type %ld",
1401 error_setg(errp
, "Must specify type= or file=");