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 "qapi/error.h"
20 #include "qemu/config-file.h"
21 #include "qemu/error-report.h"
22 #include "qemu/option.h"
23 #include "sysemu/sysemu.h"
24 #include "qemu/uuid.h"
25 #include "sysemu/cpus.h"
26 #include "hw/smbios/smbios.h"
27 #include "hw/loader.h"
28 #include "exec/cpu-common.h"
29 #include "smbios_build.h"
30 #include "hw/smbios/ipmi.h"
32 /* legacy structures and constants for <= 2.0 machines */
33 struct smbios_header
{
39 struct smbios_header header
;
46 struct smbios_header header
;
50 #define SMBIOS_FIELD_ENTRY 0
51 #define SMBIOS_TABLE_ENTRY 1
53 static uint8_t *smbios_entries
;
54 static size_t smbios_entries_len
;
55 static bool smbios_legacy
= true;
56 static bool smbios_uuid_encoded
= true;
57 /* end: legacy structures & constants for <= 2.0 machines */
60 uint8_t *smbios_tables
;
61 size_t smbios_tables_len
;
62 unsigned smbios_table_max
;
63 unsigned smbios_table_cnt
;
64 static SmbiosEntryPointType smbios_ep_type
= SMBIOS_ENTRY_POINT_21
;
66 static SmbiosEntryPoint ep
;
68 static int smbios_type4_count
= 0;
69 static bool smbios_immutable
;
70 static bool smbios_have_defaults
;
71 static uint32_t smbios_cpuid_version
, smbios_cpuid_features
, smbios_smp_sockets
;
73 static DECLARE_BITMAP(have_binfile_bitmap
, SMBIOS_MAX_TYPE
+1);
74 static DECLARE_BITMAP(have_fields_bitmap
, SMBIOS_MAX_TYPE
+1);
77 const char *vendor
, *version
, *date
;
78 bool have_major_minor
, uefi
;
83 const char *manufacturer
, *product
, *version
, *serial
, *sku
, *family
;
84 /* uuid is in qemu_uuid */
88 const char *manufacturer
, *product
, *version
, *serial
, *asset
, *location
;
92 const char *manufacturer
, *version
, *serial
, *asset
, *sku
;
96 const char *sock_pfx
, *manufacturer
, *version
, *serial
, *asset
, *part
;
105 const char *loc_pfx
, *bank
, *manufacturer
, *serial
, *asset
, *part
;
109 static QemuOptsList qemu_smbios_opts
= {
111 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts
.head
),
114 * no elements => accept any params
115 * validation will happen later
117 { /* end of list */ }
121 static const QemuOptDesc qemu_smbios_file_opts
[] = {
124 .type
= QEMU_OPT_STRING
,
125 .help
= "binary file containing an SMBIOS element",
127 { /* end of list */ }
130 static const QemuOptDesc qemu_smbios_type0_opts
[] = {
133 .type
= QEMU_OPT_NUMBER
,
134 .help
= "SMBIOS element type",
137 .type
= QEMU_OPT_STRING
,
138 .help
= "vendor name",
141 .type
= QEMU_OPT_STRING
,
142 .help
= "version number",
145 .type
= QEMU_OPT_STRING
,
146 .help
= "release date",
149 .type
= QEMU_OPT_STRING
,
150 .help
= "revision number",
153 .type
= QEMU_OPT_BOOL
,
154 .help
= "uefi support",
156 { /* end of list */ }
159 static const QemuOptDesc qemu_smbios_type1_opts
[] = {
162 .type
= QEMU_OPT_NUMBER
,
163 .help
= "SMBIOS element type",
165 .name
= "manufacturer",
166 .type
= QEMU_OPT_STRING
,
167 .help
= "manufacturer name",
170 .type
= QEMU_OPT_STRING
,
171 .help
= "product name",
174 .type
= QEMU_OPT_STRING
,
175 .help
= "version number",
178 .type
= QEMU_OPT_STRING
,
179 .help
= "serial number",
182 .type
= QEMU_OPT_STRING
,
186 .type
= QEMU_OPT_STRING
,
187 .help
= "SKU number",
190 .type
= QEMU_OPT_STRING
,
191 .help
= "family name",
193 { /* end of list */ }
196 static const QemuOptDesc qemu_smbios_type2_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
,
220 .help
= "asset tag number",
223 .type
= QEMU_OPT_STRING
,
224 .help
= "location in chassis",
226 { /* end of list */ }
229 static const QemuOptDesc qemu_smbios_type3_opts
[] = {
232 .type
= QEMU_OPT_NUMBER
,
233 .help
= "SMBIOS element type",
235 .name
= "manufacturer",
236 .type
= QEMU_OPT_STRING
,
237 .help
= "manufacturer name",
240 .type
= QEMU_OPT_STRING
,
241 .help
= "version number",
244 .type
= QEMU_OPT_STRING
,
245 .help
= "serial number",
248 .type
= QEMU_OPT_STRING
,
249 .help
= "asset tag number",
252 .type
= QEMU_OPT_STRING
,
253 .help
= "SKU number",
255 { /* end of list */ }
258 static const QemuOptDesc qemu_smbios_type4_opts
[] = {
261 .type
= QEMU_OPT_NUMBER
,
262 .help
= "SMBIOS element type",
265 .type
= QEMU_OPT_STRING
,
266 .help
= "socket designation string prefix",
268 .name
= "manufacturer",
269 .type
= QEMU_OPT_STRING
,
270 .help
= "manufacturer name",
273 .type
= QEMU_OPT_STRING
,
274 .help
= "version number",
277 .type
= QEMU_OPT_STRING
,
278 .help
= "serial number",
281 .type
= QEMU_OPT_STRING
,
282 .help
= "asset tag number",
285 .type
= QEMU_OPT_STRING
,
286 .help
= "part number",
288 { /* end of list */ }
291 static const QemuOptDesc qemu_smbios_type11_opts
[] = {
294 .type
= QEMU_OPT_STRING
,
295 .help
= "OEM string data",
299 static const QemuOptDesc qemu_smbios_type17_opts
[] = {
302 .type
= QEMU_OPT_NUMBER
,
303 .help
= "SMBIOS element type",
306 .type
= QEMU_OPT_STRING
,
307 .help
= "device locator string prefix",
310 .type
= QEMU_OPT_STRING
,
311 .help
= "bank locator string",
313 .name
= "manufacturer",
314 .type
= QEMU_OPT_STRING
,
315 .help
= "manufacturer name",
318 .type
= QEMU_OPT_STRING
,
319 .help
= "serial number",
322 .type
= QEMU_OPT_STRING
,
323 .help
= "asset tag number",
326 .type
= QEMU_OPT_STRING
,
327 .help
= "part number",
330 .type
= QEMU_OPT_NUMBER
,
331 .help
= "maximum capable speed",
333 { /* end of list */ }
336 static void smbios_register_config(void)
338 qemu_add_opts(&qemu_smbios_opts
);
341 opts_init(smbios_register_config
);
343 static void smbios_validate_table(void)
345 uint32_t expect_t4_count
= smbios_legacy
? smp_cpus
: smbios_smp_sockets
;
347 if (smbios_type4_count
&& smbios_type4_count
!= expect_t4_count
) {
348 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
349 expect_t4_count
, smbios_type4_count
);
355 /* legacy setup functions for <= 2.0 machines */
356 static void smbios_add_field(int type
, int offset
, const void *data
, size_t len
)
358 struct smbios_field
*field
;
360 if (!smbios_entries
) {
361 smbios_entries_len
= sizeof(uint16_t);
362 smbios_entries
= g_malloc0(smbios_entries_len
);
364 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
365 sizeof(*field
) + len
);
366 field
= (struct smbios_field
*)(smbios_entries
+ smbios_entries_len
);
367 field
->header
.type
= SMBIOS_FIELD_ENTRY
;
368 field
->header
.length
= cpu_to_le16(sizeof(*field
) + len
);
371 field
->offset
= cpu_to_le16(offset
);
372 memcpy(field
->data
, data
, len
);
374 smbios_entries_len
+= sizeof(*field
) + len
;
375 (*(uint16_t *)smbios_entries
) =
376 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
379 static void smbios_maybe_add_str(int type
, int offset
, const char *data
)
382 smbios_add_field(type
, offset
, data
, strlen(data
) + 1);
386 static void smbios_build_type_0_fields(void)
388 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, vendor_str
),
390 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, bios_version_str
),
392 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
,
393 bios_release_date_str
),
395 if (type0
.have_major_minor
) {
396 smbios_add_field(0, offsetof(struct smbios_type_0
,
397 system_bios_major_release
),
399 smbios_add_field(0, offsetof(struct smbios_type_0
,
400 system_bios_minor_release
),
405 static void smbios_build_type_1_fields(void)
407 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, manufacturer_str
),
409 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, product_name_str
),
411 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, version_str
),
413 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, serial_number_str
),
415 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, sku_number_str
),
417 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, family_str
),
420 /* We don't encode the UUID in the "wire format" here because this
421 * function is for legacy mode and needs to keep the guest ABI, and
422 * because we don't know what's the SMBIOS version advertised by the
425 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
),
430 uint8_t *smbios_get_table_legacy(size_t *length
)
432 if (!smbios_legacy
) {
437 if (!smbios_immutable
) {
438 smbios_build_type_0_fields();
439 smbios_build_type_1_fields();
440 smbios_validate_table();
441 smbios_immutable
= true;
443 *length
= smbios_entries_len
;
444 return smbios_entries
;
446 /* end: legacy setup functions for <= 2.0 machines */
449 bool smbios_skip_table(uint8_t type
, bool required_table
)
451 if (test_bit(type
, have_binfile_bitmap
)) {
452 return true; /* user provided their own binary blob(s) */
454 if (test_bit(type
, have_fields_bitmap
)) {
455 return false; /* user provided fields via command line */
457 if (smbios_have_defaults
&& required_table
) {
458 return false; /* we're building tables, and this one's required */
463 static void smbios_build_type_0_table(void)
465 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
467 SMBIOS_TABLE_SET_STR(0, vendor_str
, type0
.vendor
);
468 SMBIOS_TABLE_SET_STR(0, bios_version_str
, type0
.version
);
470 t
->bios_starting_address_segment
= cpu_to_le16(0xE800); /* from SeaBIOS */
472 SMBIOS_TABLE_SET_STR(0, bios_release_date_str
, type0
.date
);
474 t
->bios_rom_size
= 0; /* hardcoded in SeaBIOS with FIXME comment */
476 t
->bios_characteristics
= cpu_to_le64(0x08); /* Not supported */
477 t
->bios_characteristics_extension_bytes
[0] = 0;
478 t
->bios_characteristics_extension_bytes
[1] = 0x14; /* TCD/SVVP | VM */
480 t
->bios_characteristics_extension_bytes
[1] |= 0x08; /* |= UEFI */
483 if (type0
.have_major_minor
) {
484 t
->system_bios_major_release
= type0
.major
;
485 t
->system_bios_minor_release
= type0
.minor
;
487 t
->system_bios_major_release
= 0;
488 t
->system_bios_minor_release
= 0;
491 /* hardcoded in SeaBIOS */
492 t
->embedded_controller_major_release
= 0xFF;
493 t
->embedded_controller_minor_release
= 0xFF;
495 SMBIOS_BUILD_TABLE_POST
;
498 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
499 * format specified by SMBIOS version 2.6.
501 static void smbios_encode_uuid(struct smbios_uuid
*uuid
, QemuUUID
*in
)
503 memcpy(uuid
, in
, 16);
504 if (smbios_uuid_encoded
) {
505 uuid
->time_low
= bswap32(uuid
->time_low
);
506 uuid
->time_mid
= bswap16(uuid
->time_mid
);
507 uuid
->time_hi_and_version
= bswap16(uuid
->time_hi_and_version
);
511 static void smbios_build_type_1_table(void)
513 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
515 SMBIOS_TABLE_SET_STR(1, manufacturer_str
, type1
.manufacturer
);
516 SMBIOS_TABLE_SET_STR(1, product_name_str
, type1
.product
);
517 SMBIOS_TABLE_SET_STR(1, version_str
, type1
.version
);
518 SMBIOS_TABLE_SET_STR(1, serial_number_str
, type1
.serial
);
520 smbios_encode_uuid(&t
->uuid
, &qemu_uuid
);
522 memset(&t
->uuid
, 0, 16);
524 t
->wake_up_type
= 0x06; /* power switch */
525 SMBIOS_TABLE_SET_STR(1, sku_number_str
, type1
.sku
);
526 SMBIOS_TABLE_SET_STR(1, family_str
, type1
.family
);
528 SMBIOS_BUILD_TABLE_POST
;
531 static void smbios_build_type_2_table(void)
533 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
535 SMBIOS_TABLE_SET_STR(2, manufacturer_str
, type2
.manufacturer
);
536 SMBIOS_TABLE_SET_STR(2, product_str
, type2
.product
);
537 SMBIOS_TABLE_SET_STR(2, version_str
, type2
.version
);
538 SMBIOS_TABLE_SET_STR(2, serial_number_str
, type2
.serial
);
539 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str
, type2
.asset
);
540 t
->feature_flags
= 0x01; /* Motherboard */
541 SMBIOS_TABLE_SET_STR(2, location_str
, type2
.location
);
542 t
->chassis_handle
= cpu_to_le16(0x300); /* Type 3 (System enclosure) */
543 t
->board_type
= 0x0A; /* Motherboard */
544 t
->contained_element_count
= 0;
546 SMBIOS_BUILD_TABLE_POST
;
549 static void smbios_build_type_3_table(void)
551 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
553 SMBIOS_TABLE_SET_STR(3, manufacturer_str
, type3
.manufacturer
);
554 t
->type
= 0x01; /* Other */
555 SMBIOS_TABLE_SET_STR(3, version_str
, type3
.version
);
556 SMBIOS_TABLE_SET_STR(3, serial_number_str
, type3
.serial
);
557 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str
, type3
.asset
);
558 t
->boot_up_state
= 0x03; /* Safe */
559 t
->power_supply_state
= 0x03; /* Safe */
560 t
->thermal_state
= 0x03; /* Safe */
561 t
->security_status
= 0x02; /* Unknown */
562 t
->oem_defined
= cpu_to_le32(0);
564 t
->number_of_power_cords
= 0;
565 t
->contained_element_count
= 0;
566 SMBIOS_TABLE_SET_STR(3, sku_number_str
, type3
.sku
);
568 SMBIOS_BUILD_TABLE_POST
;
571 static void smbios_build_type_4_table(unsigned instance
)
575 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance
, true); /* required */
577 snprintf(sock_str
, sizeof(sock_str
), "%s%2x", type4
.sock_pfx
, instance
);
578 SMBIOS_TABLE_SET_STR(4, socket_designation_str
, sock_str
);
579 t
->processor_type
= 0x03; /* CPU */
580 t
->processor_family
= 0x01; /* Other */
581 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str
, type4
.manufacturer
);
582 t
->processor_id
[0] = cpu_to_le32(smbios_cpuid_version
);
583 t
->processor_id
[1] = cpu_to_le32(smbios_cpuid_features
);
584 SMBIOS_TABLE_SET_STR(4, processor_version_str
, type4
.version
);
586 t
->external_clock
= cpu_to_le16(0); /* Unknown */
587 /* SVVP requires max_speed and current_speed to not be unknown. */
588 t
->max_speed
= cpu_to_le16(2000); /* 2000 MHz */
589 t
->current_speed
= cpu_to_le16(2000); /* 2000 MHz */
590 t
->status
= 0x41; /* Socket populated, CPU enabled */
591 t
->processor_upgrade
= 0x01; /* Other */
592 t
->l1_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
593 t
->l2_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
594 t
->l3_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
595 SMBIOS_TABLE_SET_STR(4, serial_number_str
, type4
.serial
);
596 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str
, type4
.asset
);
597 SMBIOS_TABLE_SET_STR(4, part_number_str
, type4
.part
);
598 t
->core_count
= t
->core_enabled
= smp_cores
;
599 t
->thread_count
= smp_threads
;
600 t
->processor_characteristics
= cpu_to_le16(0x02); /* Unknown */
601 t
->processor_family2
= cpu_to_le16(0x01); /* Other */
603 SMBIOS_BUILD_TABLE_POST
;
604 smbios_type4_count
++;
607 static void smbios_build_type_11_table(void)
612 if (type11
.nvalues
== 0) {
616 SMBIOS_BUILD_TABLE_PRE(11, 0xe00, true); /* required */
618 snprintf(count_str
, sizeof(count_str
), "%zu", type11
.nvalues
);
619 t
->count
= type11
.nvalues
;
621 for (i
= 0; i
< type11
.nvalues
; i
++) {
622 SMBIOS_TABLE_SET_STR_LIST(11, type11
.values
[i
]);
625 SMBIOS_BUILD_TABLE_POST
;
628 #define ONE_KB ((ram_addr_t)1 << 10)
629 #define ONE_MB ((ram_addr_t)1 << 20)
630 #define ONE_GB ((ram_addr_t)1 << 30)
632 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
634 static void smbios_build_type_16_table(unsigned dimm_cnt
)
638 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
640 t
->location
= 0x01; /* Other */
641 t
->use
= 0x03; /* System memory */
642 t
->error_correction
= 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
643 size_kb
= QEMU_ALIGN_UP(ram_size
, ONE_KB
) / ONE_KB
;
644 if (size_kb
< MAX_T16_STD_SZ
) {
645 t
->maximum_capacity
= cpu_to_le32(size_kb
);
646 t
->extended_maximum_capacity
= cpu_to_le64(0);
648 t
->maximum_capacity
= cpu_to_le32(MAX_T16_STD_SZ
);
649 t
->extended_maximum_capacity
= cpu_to_le64(ram_size
);
651 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
652 t
->number_of_memory_devices
= cpu_to_le16(dimm_cnt
);
654 SMBIOS_BUILD_TABLE_POST
;
657 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
658 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
660 static void smbios_build_type_17_table(unsigned instance
, uint64_t size
)
665 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance
, true); /* required */
667 t
->physical_memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
668 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
669 t
->total_width
= cpu_to_le16(0xFFFF); /* Unknown */
670 t
->data_width
= cpu_to_le16(0xFFFF); /* Unknown */
671 size_mb
= QEMU_ALIGN_UP(size
, ONE_MB
) / ONE_MB
;
672 if (size_mb
< MAX_T17_STD_SZ
) {
673 t
->size
= cpu_to_le16(size_mb
);
674 t
->extended_size
= cpu_to_le32(0);
676 assert(size_mb
< MAX_T17_EXT_SZ
);
677 t
->size
= cpu_to_le16(MAX_T17_STD_SZ
);
678 t
->extended_size
= cpu_to_le32(size_mb
);
680 t
->form_factor
= 0x09; /* DIMM */
681 t
->device_set
= 0; /* Not in a set */
682 snprintf(loc_str
, sizeof(loc_str
), "%s %d", type17
.loc_pfx
, instance
);
683 SMBIOS_TABLE_SET_STR(17, device_locator_str
, loc_str
);
684 SMBIOS_TABLE_SET_STR(17, bank_locator_str
, type17
.bank
);
685 t
->memory_type
= 0x07; /* RAM */
686 t
->type_detail
= cpu_to_le16(0x02); /* Other */
687 t
->speed
= cpu_to_le16(type17
.speed
);
688 SMBIOS_TABLE_SET_STR(17, manufacturer_str
, type17
.manufacturer
);
689 SMBIOS_TABLE_SET_STR(17, serial_number_str
, type17
.serial
);
690 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str
, type17
.asset
);
691 SMBIOS_TABLE_SET_STR(17, part_number_str
, type17
.part
);
692 t
->attributes
= 0; /* Unknown */
693 t
->configured_clock_speed
= t
->speed
; /* reuse value for max speed */
694 t
->minimum_voltage
= cpu_to_le16(0); /* Unknown */
695 t
->maximum_voltage
= cpu_to_le16(0); /* Unknown */
696 t
->configured_voltage
= cpu_to_le16(0); /* Unknown */
698 SMBIOS_BUILD_TABLE_POST
;
701 static void smbios_build_type_19_table(unsigned instance
,
702 uint64_t start
, uint64_t size
)
704 uint64_t end
, start_kb
, end_kb
;
706 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance
, true); /* required */
708 end
= start
+ size
- 1;
710 start_kb
= start
/ ONE_KB
;
711 end_kb
= end
/ ONE_KB
;
712 if (start_kb
< UINT32_MAX
&& end_kb
< UINT32_MAX
) {
713 t
->starting_address
= cpu_to_le32(start_kb
);
714 t
->ending_address
= cpu_to_le32(end_kb
);
715 t
->extended_starting_address
=
716 t
->extended_ending_address
= cpu_to_le64(0);
718 t
->starting_address
= t
->ending_address
= cpu_to_le32(UINT32_MAX
);
719 t
->extended_starting_address
= cpu_to_le64(start
);
720 t
->extended_ending_address
= cpu_to_le64(end
);
722 t
->memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
723 t
->partition_width
= 1; /* One device per row */
725 SMBIOS_BUILD_TABLE_POST
;
728 static void smbios_build_type_32_table(void)
730 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
732 memset(t
->reserved
, 0, 6);
733 t
->boot_status
= 0; /* No errors detected */
735 SMBIOS_BUILD_TABLE_POST
;
738 static void smbios_build_type_127_table(void)
740 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
741 SMBIOS_BUILD_TABLE_POST
;
744 void smbios_set_cpuid(uint32_t version
, uint32_t features
)
746 smbios_cpuid_version
= version
;
747 smbios_cpuid_features
= features
;
750 #define SMBIOS_SET_DEFAULT(field, value) \
755 void smbios_set_defaults(const char *manufacturer
, const char *product
,
756 const char *version
, bool legacy_mode
,
757 bool uuid_encoded
, SmbiosEntryPointType ep_type
)
759 smbios_have_defaults
= true;
760 smbios_legacy
= legacy_mode
;
761 smbios_uuid_encoded
= uuid_encoded
;
762 smbios_ep_type
= ep_type
;
764 /* drop unwanted version of command-line file blob(s) */
766 g_free(smbios_tables
);
767 /* in legacy mode, also complain if fields were given for types > 1 */
768 if (find_next_bit(have_fields_bitmap
,
769 SMBIOS_MAX_TYPE
+1, 2) < SMBIOS_MAX_TYPE
+1) {
770 error_report("can't process fields for smbios "
771 "types > 1 on machine versions < 2.1!");
775 g_free(smbios_entries
);
778 SMBIOS_SET_DEFAULT(type1
.manufacturer
, manufacturer
);
779 SMBIOS_SET_DEFAULT(type1
.product
, product
);
780 SMBIOS_SET_DEFAULT(type1
.version
, version
);
781 SMBIOS_SET_DEFAULT(type2
.manufacturer
, manufacturer
);
782 SMBIOS_SET_DEFAULT(type2
.product
, product
);
783 SMBIOS_SET_DEFAULT(type2
.version
, version
);
784 SMBIOS_SET_DEFAULT(type3
.manufacturer
, manufacturer
);
785 SMBIOS_SET_DEFAULT(type3
.version
, version
);
786 SMBIOS_SET_DEFAULT(type4
.sock_pfx
, "CPU");
787 SMBIOS_SET_DEFAULT(type4
.manufacturer
, manufacturer
);
788 SMBIOS_SET_DEFAULT(type4
.version
, version
);
789 SMBIOS_SET_DEFAULT(type17
.loc_pfx
, "DIMM");
790 SMBIOS_SET_DEFAULT(type17
.manufacturer
, manufacturer
);
793 static void smbios_entry_point_setup(void)
795 switch (smbios_ep_type
) {
796 case SMBIOS_ENTRY_POINT_21
:
797 memcpy(ep
.ep21
.anchor_string
, "_SM_", 4);
798 memcpy(ep
.ep21
.intermediate_anchor_string
, "_DMI_", 5);
799 ep
.ep21
.length
= sizeof(struct smbios_21_entry_point
);
800 ep
.ep21
.entry_point_revision
= 0; /* formatted_area reserved */
801 memset(ep
.ep21
.formatted_area
, 0, 5);
803 /* compliant with smbios spec v2.8 */
804 ep
.ep21
.smbios_major_version
= 2;
805 ep
.ep21
.smbios_minor_version
= 8;
806 ep
.ep21
.smbios_bcd_revision
= 0x28;
808 /* set during table construction, but BIOS may override: */
809 ep
.ep21
.structure_table_length
= cpu_to_le16(smbios_tables_len
);
810 ep
.ep21
.max_structure_size
= cpu_to_le16(smbios_table_max
);
811 ep
.ep21
.number_of_structures
= cpu_to_le16(smbios_table_cnt
);
813 /* BIOS must recalculate */
814 ep
.ep21
.checksum
= 0;
815 ep
.ep21
.intermediate_checksum
= 0;
816 ep
.ep21
.structure_table_address
= cpu_to_le32(0);
819 case SMBIOS_ENTRY_POINT_30
:
820 memcpy(ep
.ep30
.anchor_string
, "_SM3_", 5);
821 ep
.ep30
.length
= sizeof(struct smbios_30_entry_point
);
822 ep
.ep30
.entry_point_revision
= 1;
823 ep
.ep30
.reserved
= 0;
825 /* compliant with smbios spec 3.0 */
826 ep
.ep30
.smbios_major_version
= 3;
827 ep
.ep30
.smbios_minor_version
= 0;
828 ep
.ep30
.smbios_doc_rev
= 0;
830 /* set during table construct, but BIOS might override */
831 ep
.ep30
.structure_table_max_size
= cpu_to_le32(smbios_tables_len
);
833 /* BIOS must recalculate */
834 ep
.ep30
.checksum
= 0;
835 ep
.ep30
.structure_table_address
= cpu_to_le64(0);
844 void smbios_get_tables(const struct smbios_phys_mem_area
*mem_array
,
845 const unsigned int mem_array_size
,
846 uint8_t **tables
, size_t *tables_len
,
847 uint8_t **anchor
, size_t *anchor_len
)
849 unsigned i
, dimm_cnt
;
852 *tables
= *anchor
= NULL
;
853 *tables_len
= *anchor_len
= 0;
857 if (!smbios_immutable
) {
858 smbios_build_type_0_table();
859 smbios_build_type_1_table();
860 smbios_build_type_2_table();
861 smbios_build_type_3_table();
863 smbios_smp_sockets
= DIV_ROUND_UP(smp_cpus
, smp_cores
* smp_threads
);
864 assert(smbios_smp_sockets
>= 1);
866 for (i
= 0; i
< smbios_smp_sockets
; i
++) {
867 smbios_build_type_4_table(i
);
870 smbios_build_type_11_table();
872 #define MAX_DIMM_SZ (16ll * ONE_GB)
873 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
874 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
876 dimm_cnt
= QEMU_ALIGN_UP(ram_size
, MAX_DIMM_SZ
) / MAX_DIMM_SZ
;
878 smbios_build_type_16_table(dimm_cnt
);
880 for (i
= 0; i
< dimm_cnt
; i
++) {
881 smbios_build_type_17_table(i
, GET_DIMM_SZ
);
884 for (i
= 0; i
< mem_array_size
; i
++) {
885 smbios_build_type_19_table(i
, mem_array
[i
].address
,
886 mem_array
[i
].length
);
889 smbios_build_type_32_table();
890 smbios_build_type_38_table();
891 smbios_build_type_127_table();
893 smbios_validate_table();
894 smbios_entry_point_setup();
895 smbios_immutable
= true;
898 /* return tables blob and entry point (anchor), and their sizes */
899 *tables
= smbios_tables
;
900 *tables_len
= smbios_tables_len
;
901 *anchor
= (uint8_t *)&ep
;
903 /* calculate length based on anchor string */
904 if (!strncmp((char *)&ep
, "_SM_", 4)) {
905 *anchor_len
= sizeof(struct smbios_21_entry_point
);
906 } else if (!strncmp((char *)&ep
, "_SM3_", 5)) {
907 *anchor_len
= sizeof(struct smbios_30_entry_point
);
913 static void save_opt(const char **dest
, QemuOpts
*opts
, const char *name
)
915 const char *val
= qemu_opt_get(opts
, name
);
929 static int save_opt_one(void *opaque
,
930 const char *name
, const char *value
,
933 struct opt_list
*opt
= opaque
;
935 if (!g_str_equal(name
, opt
->name
)) {
939 *opt
->dest
= g_renew(const char *, *opt
->dest
, (*opt
->ndest
) + 1);
940 (*opt
->dest
)[*opt
->ndest
] = value
;
945 static void save_opt_list(size_t *ndest
, const char ***dest
,
946 QemuOpts
*opts
, const char *name
)
948 struct opt_list opt
= {
951 qemu_opt_foreach(opts
, save_opt_one
, &opt
, NULL
);
954 void smbios_entry_add(QemuOpts
*opts
, Error
**errp
)
958 assert(!smbios_immutable
);
960 val
= qemu_opt_get(opts
, "file");
962 struct smbios_structure_header
*header
;
964 struct smbios_table
*table
; /* legacy mode only */
966 qemu_opts_validate(opts
, qemu_smbios_file_opts
, &error_fatal
);
968 size
= get_image_size(val
);
969 if (size
== -1 || size
< sizeof(struct smbios_structure_header
)) {
970 error_report("Cannot read SMBIOS file %s", val
);
975 * NOTE: standard double '\0' terminator expected, per smbios spec.
976 * (except in legacy mode, where the second '\0' is implicit and
977 * will be inserted by the BIOS).
979 smbios_tables
= g_realloc(smbios_tables
, smbios_tables_len
+ size
);
980 header
= (struct smbios_structure_header
*)(smbios_tables
+
983 if (load_image(val
, (uint8_t *)header
) != size
) {
984 error_report("Failed to load SMBIOS file %s", val
);
988 if (test_bit(header
->type
, have_fields_bitmap
)) {
989 error_report("can't load type %d struct, fields already specified!",
993 set_bit(header
->type
, have_binfile_bitmap
);
995 if (header
->type
== 4) {
996 smbios_type4_count
++;
999 smbios_tables_len
+= size
;
1000 if (size
> smbios_table_max
) {
1001 smbios_table_max
= size
;
1005 /* add a copy of the newly loaded blob to legacy smbios_entries */
1006 /* NOTE: This code runs before smbios_set_defaults(), so we don't
1007 * yet know which mode (legacy vs. aggregate-table) will be
1008 * required. We therefore add the binary blob to both legacy
1009 * (smbios_entries) and aggregate (smbios_tables) tables, and
1010 * delete the one we don't need from smbios_set_defaults(),
1011 * once we know which machine version has been requested.
1013 if (!smbios_entries
) {
1014 smbios_entries_len
= sizeof(uint16_t);
1015 smbios_entries
= g_malloc0(smbios_entries_len
);
1017 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
1018 size
+ sizeof(*table
));
1019 table
= (struct smbios_table
*)(smbios_entries
+ smbios_entries_len
);
1020 table
->header
.type
= SMBIOS_TABLE_ENTRY
;
1021 table
->header
.length
= cpu_to_le16(sizeof(*table
) + size
);
1022 memcpy(table
->data
, header
, size
);
1023 smbios_entries_len
+= sizeof(*table
) + size
;
1024 (*(uint16_t *)smbios_entries
) =
1025 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
1026 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1031 val
= qemu_opt_get(opts
, "type");
1033 unsigned long type
= strtoul(val
, NULL
, 0);
1035 if (type
> SMBIOS_MAX_TYPE
) {
1036 error_report("out of range!");
1040 if (test_bit(type
, have_binfile_bitmap
)) {
1041 error_report("can't add fields, binary file already loaded!");
1044 set_bit(type
, have_fields_bitmap
);
1048 qemu_opts_validate(opts
, qemu_smbios_type0_opts
, &error_fatal
);
1049 save_opt(&type0
.vendor
, opts
, "vendor");
1050 save_opt(&type0
.version
, opts
, "version");
1051 save_opt(&type0
.date
, opts
, "date");
1052 type0
.uefi
= qemu_opt_get_bool(opts
, "uefi", false);
1054 val
= qemu_opt_get(opts
, "release");
1056 if (sscanf(val
, "%hhu.%hhu", &type0
.major
, &type0
.minor
) != 2) {
1057 error_report("Invalid release");
1060 type0
.have_major_minor
= true;
1064 qemu_opts_validate(opts
, qemu_smbios_type1_opts
, &error_fatal
);
1065 save_opt(&type1
.manufacturer
, opts
, "manufacturer");
1066 save_opt(&type1
.product
, opts
, "product");
1067 save_opt(&type1
.version
, opts
, "version");
1068 save_opt(&type1
.serial
, opts
, "serial");
1069 save_opt(&type1
.sku
, opts
, "sku");
1070 save_opt(&type1
.family
, opts
, "family");
1072 val
= qemu_opt_get(opts
, "uuid");
1074 if (qemu_uuid_parse(val
, &qemu_uuid
) != 0) {
1075 error_report("Invalid UUID");
1078 qemu_uuid_set
= true;
1082 qemu_opts_validate(opts
, qemu_smbios_type2_opts
, &error_fatal
);
1083 save_opt(&type2
.manufacturer
, opts
, "manufacturer");
1084 save_opt(&type2
.product
, opts
, "product");
1085 save_opt(&type2
.version
, opts
, "version");
1086 save_opt(&type2
.serial
, opts
, "serial");
1087 save_opt(&type2
.asset
, opts
, "asset");
1088 save_opt(&type2
.location
, opts
, "location");
1091 qemu_opts_validate(opts
, qemu_smbios_type3_opts
, &error_fatal
);
1092 save_opt(&type3
.manufacturer
, opts
, "manufacturer");
1093 save_opt(&type3
.version
, opts
, "version");
1094 save_opt(&type3
.serial
, opts
, "serial");
1095 save_opt(&type3
.asset
, opts
, "asset");
1096 save_opt(&type3
.sku
, opts
, "sku");
1099 qemu_opts_validate(opts
, qemu_smbios_type4_opts
, &error_fatal
);
1100 save_opt(&type4
.sock_pfx
, opts
, "sock_pfx");
1101 save_opt(&type4
.manufacturer
, opts
, "manufacturer");
1102 save_opt(&type4
.version
, opts
, "version");
1103 save_opt(&type4
.serial
, opts
, "serial");
1104 save_opt(&type4
.asset
, opts
, "asset");
1105 save_opt(&type4
.part
, opts
, "part");
1108 qemu_opts_validate(opts
, qemu_smbios_type11_opts
, &error_fatal
);
1109 save_opt_list(&type11
.nvalues
, &type11
.values
, opts
, "value");
1112 qemu_opts_validate(opts
, qemu_smbios_type17_opts
, &error_fatal
);
1113 save_opt(&type17
.loc_pfx
, opts
, "loc_pfx");
1114 save_opt(&type17
.bank
, opts
, "bank");
1115 save_opt(&type17
.manufacturer
, opts
, "manufacturer");
1116 save_opt(&type17
.serial
, opts
, "serial");
1117 save_opt(&type17
.asset
, opts
, "asset");
1118 save_opt(&type17
.part
, opts
, "part");
1119 type17
.speed
= qemu_opt_get_number(opts
, "speed", 0);
1122 error_report("Don't know how to build fields for SMBIOS type %ld",
1128 error_report("Must specify type= or file=");