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/config-file.h"
19 #include "qemu/error-report.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/cpus.h"
22 #include "hw/smbios/smbios.h"
23 #include "hw/loader.h"
24 #include "exec/cpu-common.h"
26 /* legacy structures and constants for <= 2.0 machines */
27 struct smbios_header
{
33 struct smbios_header header
;
40 struct smbios_header header
;
44 #define SMBIOS_FIELD_ENTRY 0
45 #define SMBIOS_TABLE_ENTRY 1
47 static uint8_t *smbios_entries
;
48 static size_t smbios_entries_len
;
49 static bool smbios_legacy
= true;
50 static bool smbios_uuid_encoded
= true;
51 /* end: legacy structures & constants for <= 2.0 machines */
54 static uint8_t *smbios_tables
;
55 static size_t smbios_tables_len
;
56 static unsigned smbios_table_max
;
57 static unsigned smbios_table_cnt
;
58 static struct smbios_entry_point ep
;
60 static int smbios_type4_count
= 0;
61 static bool smbios_immutable
;
62 static bool smbios_have_defaults
;
63 static uint32_t smbios_cpuid_version
, smbios_cpuid_features
, smbios_smp_sockets
;
65 static DECLARE_BITMAP(have_binfile_bitmap
, SMBIOS_MAX_TYPE
+1);
66 static DECLARE_BITMAP(have_fields_bitmap
, SMBIOS_MAX_TYPE
+1);
69 const char *vendor
, *version
, *date
;
70 bool have_major_minor
, uefi
;
75 const char *manufacturer
, *product
, *version
, *serial
, *sku
, *family
;
76 /* uuid is in qemu_uuid[] */
80 const char *manufacturer
, *product
, *version
, *serial
, *asset
, *location
;
84 const char *manufacturer
, *version
, *serial
, *asset
, *sku
;
88 const char *sock_pfx
, *manufacturer
, *version
, *serial
, *asset
, *part
;
92 const char *loc_pfx
, *bank
, *manufacturer
, *serial
, *asset
, *part
;
96 static QemuOptsList qemu_smbios_opts
= {
98 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts
.head
),
101 * no elements => accept any params
102 * validation will happen later
104 { /* end of list */ }
108 static const QemuOptDesc qemu_smbios_file_opts
[] = {
111 .type
= QEMU_OPT_STRING
,
112 .help
= "binary file containing an SMBIOS element",
114 { /* end of list */ }
117 static const QemuOptDesc qemu_smbios_type0_opts
[] = {
120 .type
= QEMU_OPT_NUMBER
,
121 .help
= "SMBIOS element type",
124 .type
= QEMU_OPT_STRING
,
125 .help
= "vendor name",
128 .type
= QEMU_OPT_STRING
,
129 .help
= "version number",
132 .type
= QEMU_OPT_STRING
,
133 .help
= "release date",
136 .type
= QEMU_OPT_STRING
,
137 .help
= "revision number",
140 .type
= QEMU_OPT_BOOL
,
141 .help
= "uefi support",
143 { /* end of list */ }
146 static const QemuOptDesc qemu_smbios_type1_opts
[] = {
149 .type
= QEMU_OPT_NUMBER
,
150 .help
= "SMBIOS element type",
152 .name
= "manufacturer",
153 .type
= QEMU_OPT_STRING
,
154 .help
= "manufacturer name",
157 .type
= QEMU_OPT_STRING
,
158 .help
= "product name",
161 .type
= QEMU_OPT_STRING
,
162 .help
= "version number",
165 .type
= QEMU_OPT_STRING
,
166 .help
= "serial number",
169 .type
= QEMU_OPT_STRING
,
173 .type
= QEMU_OPT_STRING
,
174 .help
= "SKU number",
177 .type
= QEMU_OPT_STRING
,
178 .help
= "family name",
180 { /* end of list */ }
183 static const QemuOptDesc qemu_smbios_type2_opts
[] = {
186 .type
= QEMU_OPT_NUMBER
,
187 .help
= "SMBIOS element type",
189 .name
= "manufacturer",
190 .type
= QEMU_OPT_STRING
,
191 .help
= "manufacturer name",
194 .type
= QEMU_OPT_STRING
,
195 .help
= "product name",
198 .type
= QEMU_OPT_STRING
,
199 .help
= "version number",
202 .type
= QEMU_OPT_STRING
,
203 .help
= "serial number",
206 .type
= QEMU_OPT_STRING
,
207 .help
= "asset tag number",
210 .type
= QEMU_OPT_STRING
,
211 .help
= "location in chassis",
213 { /* end of list */ }
216 static const QemuOptDesc qemu_smbios_type3_opts
[] = {
219 .type
= QEMU_OPT_NUMBER
,
220 .help
= "SMBIOS element type",
222 .name
= "manufacturer",
223 .type
= QEMU_OPT_STRING
,
224 .help
= "manufacturer name",
227 .type
= QEMU_OPT_STRING
,
228 .help
= "version number",
231 .type
= QEMU_OPT_STRING
,
232 .help
= "serial number",
235 .type
= QEMU_OPT_STRING
,
236 .help
= "asset tag number",
239 .type
= QEMU_OPT_STRING
,
240 .help
= "SKU number",
242 { /* end of list */ }
245 static const QemuOptDesc qemu_smbios_type4_opts
[] = {
248 .type
= QEMU_OPT_NUMBER
,
249 .help
= "SMBIOS element type",
252 .type
= QEMU_OPT_STRING
,
253 .help
= "socket designation string prefix",
255 .name
= "manufacturer",
256 .type
= QEMU_OPT_STRING
,
257 .help
= "manufacturer name",
260 .type
= QEMU_OPT_STRING
,
261 .help
= "version number",
264 .type
= QEMU_OPT_STRING
,
265 .help
= "serial number",
268 .type
= QEMU_OPT_STRING
,
269 .help
= "asset tag number",
272 .type
= QEMU_OPT_STRING
,
273 .help
= "part number",
275 { /* end of list */ }
278 static const QemuOptDesc qemu_smbios_type17_opts
[] = {
281 .type
= QEMU_OPT_NUMBER
,
282 .help
= "SMBIOS element type",
285 .type
= QEMU_OPT_STRING
,
286 .help
= "device locator string prefix",
289 .type
= QEMU_OPT_STRING
,
290 .help
= "bank locator string",
292 .name
= "manufacturer",
293 .type
= QEMU_OPT_STRING
,
294 .help
= "manufacturer name",
297 .type
= QEMU_OPT_STRING
,
298 .help
= "serial number",
301 .type
= QEMU_OPT_STRING
,
302 .help
= "asset tag number",
305 .type
= QEMU_OPT_STRING
,
306 .help
= "part number",
309 .type
= QEMU_OPT_NUMBER
,
310 .help
= "maximum capable speed",
312 { /* end of list */ }
315 static void smbios_register_config(void)
317 qemu_add_opts(&qemu_smbios_opts
);
320 machine_init(smbios_register_config
);
322 static void smbios_validate_table(void)
324 uint32_t expect_t4_count
= smbios_legacy
? smp_cpus
: smbios_smp_sockets
;
326 if (smbios_type4_count
&& smbios_type4_count
!= expect_t4_count
) {
327 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
328 expect_t4_count
, smbios_type4_count
);
334 /* legacy setup functions for <= 2.0 machines */
335 static void smbios_add_field(int type
, int offset
, const void *data
, size_t len
)
337 struct smbios_field
*field
;
339 if (!smbios_entries
) {
340 smbios_entries_len
= sizeof(uint16_t);
341 smbios_entries
= g_malloc0(smbios_entries_len
);
343 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
344 sizeof(*field
) + len
);
345 field
= (struct smbios_field
*)(smbios_entries
+ smbios_entries_len
);
346 field
->header
.type
= SMBIOS_FIELD_ENTRY
;
347 field
->header
.length
= cpu_to_le16(sizeof(*field
) + len
);
350 field
->offset
= cpu_to_le16(offset
);
351 memcpy(field
->data
, data
, len
);
353 smbios_entries_len
+= sizeof(*field
) + len
;
354 (*(uint16_t *)smbios_entries
) =
355 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
358 static void smbios_maybe_add_str(int type
, int offset
, const char *data
)
361 smbios_add_field(type
, offset
, data
, strlen(data
) + 1);
365 static void smbios_build_type_0_fields(void)
367 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, vendor_str
),
369 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, bios_version_str
),
371 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
,
372 bios_release_date_str
),
374 if (type0
.have_major_minor
) {
375 smbios_add_field(0, offsetof(struct smbios_type_0
,
376 system_bios_major_release
),
378 smbios_add_field(0, offsetof(struct smbios_type_0
,
379 system_bios_minor_release
),
384 static void smbios_build_type_1_fields(void)
386 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, manufacturer_str
),
388 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, product_name_str
),
390 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, version_str
),
392 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, serial_number_str
),
394 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, sku_number_str
),
396 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, family_str
),
399 /* We don't encode the UUID in the "wire format" here because this
400 * function is for legacy mode and needs to keep the guest ABI, and
401 * because we don't know what's the SMBIOS version advertised by the
404 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
),
409 uint8_t *smbios_get_table_legacy(size_t *length
)
411 if (!smbios_legacy
) {
416 if (!smbios_immutable
) {
417 smbios_build_type_0_fields();
418 smbios_build_type_1_fields();
419 smbios_validate_table();
420 smbios_immutable
= true;
422 *length
= smbios_entries_len
;
423 return smbios_entries
;
425 /* end: legacy setup functions for <= 2.0 machines */
428 static bool smbios_skip_table(uint8_t type
, bool required_table
)
430 if (test_bit(type
, have_binfile_bitmap
)) {
431 return true; /* user provided their own binary blob(s) */
433 if (test_bit(type
, have_fields_bitmap
)) {
434 return false; /* user provided fields via command line */
436 if (smbios_have_defaults
&& required_table
) {
437 return false; /* we're building tables, and this one's required */
442 #define SMBIOS_BUILD_TABLE_PRE(tbl_type, tbl_handle, tbl_required) \
443 struct smbios_type_##tbl_type *t; \
444 size_t t_off; /* table offset into smbios_tables */ \
447 /* should we skip building this table ? */ \
448 if (smbios_skip_table(tbl_type, tbl_required)) { \
452 /* use offset of table t within smbios_tables */ \
453 /* (pointer must be updated after each realloc) */ \
454 t_off = smbios_tables_len; \
455 smbios_tables_len += sizeof(*t); \
456 smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
457 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
459 t->header.type = tbl_type; \
460 t->header.length = sizeof(*t); \
461 t->header.handle = cpu_to_le16(tbl_handle); \
464 #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
466 int len = (value != NULL) ? strlen(value) + 1 : 0; \
468 smbios_tables = g_realloc(smbios_tables, \
469 smbios_tables_len + len); \
470 memcpy(smbios_tables + smbios_tables_len, value, len); \
471 smbios_tables_len += len; \
472 /* update pointer post-realloc */ \
473 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
474 t->field = ++str_index; \
480 #define SMBIOS_BUILD_TABLE_POST \
482 size_t term_cnt, t_size; \
484 /* add '\0' terminator (add two if no strings defined) */ \
485 term_cnt = (str_index == 0) ? 2 : 1; \
486 smbios_tables = g_realloc(smbios_tables, \
487 smbios_tables_len + term_cnt); \
488 memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
489 smbios_tables_len += term_cnt; \
491 /* update smbios max. element size */ \
492 t_size = smbios_tables_len - t_off; \
493 if (t_size > smbios_table_max) { \
494 smbios_table_max = t_size; \
497 /* update smbios element count */ \
498 smbios_table_cnt++; \
501 static void smbios_build_type_0_table(void)
503 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
505 SMBIOS_TABLE_SET_STR(0, vendor_str
, type0
.vendor
);
506 SMBIOS_TABLE_SET_STR(0, bios_version_str
, type0
.version
);
508 t
->bios_starting_address_segment
= cpu_to_le16(0xE800); /* from SeaBIOS */
510 SMBIOS_TABLE_SET_STR(0, bios_release_date_str
, type0
.date
);
512 t
->bios_rom_size
= 0; /* hardcoded in SeaBIOS with FIXME comment */
514 t
->bios_characteristics
= cpu_to_le64(0x08); /* Not supported */
515 t
->bios_characteristics_extension_bytes
[0] = 0;
516 t
->bios_characteristics_extension_bytes
[1] = 0x14; /* TCD/SVVP | VM */
518 t
->bios_characteristics_extension_bytes
[1] |= 0x08; /* |= UEFI */
521 if (type0
.have_major_minor
) {
522 t
->system_bios_major_release
= type0
.major
;
523 t
->system_bios_minor_release
= type0
.minor
;
525 t
->system_bios_major_release
= 0;
526 t
->system_bios_minor_release
= 0;
529 /* hardcoded in SeaBIOS */
530 t
->embedded_controller_major_release
= 0xFF;
531 t
->embedded_controller_minor_release
= 0xFF;
533 SMBIOS_BUILD_TABLE_POST
;
536 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
537 * format specified by SMBIOS version 2.6.
539 static void smbios_encode_uuid(struct smbios_uuid
*uuid
, const uint8_t *buf
)
541 memcpy(uuid
, buf
, 16);
542 if (smbios_uuid_encoded
) {
543 uuid
->time_low
= bswap32(uuid
->time_low
);
544 uuid
->time_mid
= bswap16(uuid
->time_mid
);
545 uuid
->time_hi_and_version
= bswap16(uuid
->time_hi_and_version
);
549 static void smbios_build_type_1_table(void)
551 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
553 SMBIOS_TABLE_SET_STR(1, manufacturer_str
, type1
.manufacturer
);
554 SMBIOS_TABLE_SET_STR(1, product_name_str
, type1
.product
);
555 SMBIOS_TABLE_SET_STR(1, version_str
, type1
.version
);
556 SMBIOS_TABLE_SET_STR(1, serial_number_str
, type1
.serial
);
558 smbios_encode_uuid(&t
->uuid
, qemu_uuid
);
560 memset(&t
->uuid
, 0, 16);
562 t
->wake_up_type
= 0x06; /* power switch */
563 SMBIOS_TABLE_SET_STR(1, sku_number_str
, type1
.sku
);
564 SMBIOS_TABLE_SET_STR(1, family_str
, type1
.family
);
566 SMBIOS_BUILD_TABLE_POST
;
569 static void smbios_build_type_2_table(void)
571 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
573 SMBIOS_TABLE_SET_STR(2, manufacturer_str
, type2
.manufacturer
);
574 SMBIOS_TABLE_SET_STR(2, product_str
, type2
.product
);
575 SMBIOS_TABLE_SET_STR(2, version_str
, type2
.version
);
576 SMBIOS_TABLE_SET_STR(2, serial_number_str
, type2
.serial
);
577 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str
, type2
.asset
);
578 t
->feature_flags
= 0x01; /* Motherboard */
579 SMBIOS_TABLE_SET_STR(2, location_str
, type2
.location
);
580 t
->chassis_handle
= cpu_to_le16(0x300); /* Type 3 (System enclosure) */
581 t
->board_type
= 0x0A; /* Motherboard */
582 t
->contained_element_count
= 0;
584 SMBIOS_BUILD_TABLE_POST
;
587 static void smbios_build_type_3_table(void)
589 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
591 SMBIOS_TABLE_SET_STR(3, manufacturer_str
, type3
.manufacturer
);
592 t
->type
= 0x01; /* Other */
593 SMBIOS_TABLE_SET_STR(3, version_str
, type3
.version
);
594 SMBIOS_TABLE_SET_STR(3, serial_number_str
, type3
.serial
);
595 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str
, type3
.asset
);
596 t
->boot_up_state
= 0x03; /* Safe */
597 t
->power_supply_state
= 0x03; /* Safe */
598 t
->thermal_state
= 0x03; /* Safe */
599 t
->security_status
= 0x02; /* Unknown */
600 t
->oem_defined
= cpu_to_le32(0);
602 t
->number_of_power_cords
= 0;
603 t
->contained_element_count
= 0;
604 SMBIOS_TABLE_SET_STR(3, sku_number_str
, type3
.sku
);
606 SMBIOS_BUILD_TABLE_POST
;
609 static void smbios_build_type_4_table(unsigned instance
)
613 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance
, true); /* required */
615 snprintf(sock_str
, sizeof(sock_str
), "%s%2x", type4
.sock_pfx
, instance
);
616 SMBIOS_TABLE_SET_STR(4, socket_designation_str
, sock_str
);
617 t
->processor_type
= 0x03; /* CPU */
618 t
->processor_family
= 0x01; /* Other */
619 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str
, type4
.manufacturer
);
620 t
->processor_id
[0] = cpu_to_le32(smbios_cpuid_version
);
621 t
->processor_id
[1] = cpu_to_le32(smbios_cpuid_features
);
622 SMBIOS_TABLE_SET_STR(4, processor_version_str
, type4
.version
);
624 t
->external_clock
= cpu_to_le16(0); /* Unknown */
625 /* SVVP requires max_speed and current_speed to not be unknown. */
626 t
->max_speed
= cpu_to_le16(2000); /* 2000 MHz */
627 t
->current_speed
= cpu_to_le16(2000); /* 2000 MHz */
628 t
->status
= 0x41; /* Socket populated, CPU enabled */
629 t
->processor_upgrade
= 0x01; /* Other */
630 t
->l1_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
631 t
->l2_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
632 t
->l3_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
633 SMBIOS_TABLE_SET_STR(4, serial_number_str
, type4
.serial
);
634 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str
, type4
.asset
);
635 SMBIOS_TABLE_SET_STR(4, part_number_str
, type4
.part
);
636 t
->core_count
= t
->core_enabled
= smp_cores
;
637 t
->thread_count
= smp_threads
;
638 t
->processor_characteristics
= cpu_to_le16(0x02); /* Unknown */
639 t
->processor_family2
= cpu_to_le16(0x01); /* Other */
641 SMBIOS_BUILD_TABLE_POST
;
642 smbios_type4_count
++;
645 #define ONE_KB ((ram_addr_t)1 << 10)
646 #define ONE_MB ((ram_addr_t)1 << 20)
647 #define ONE_GB ((ram_addr_t)1 << 30)
649 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
651 static void smbios_build_type_16_table(unsigned dimm_cnt
)
655 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
657 t
->location
= 0x01; /* Other */
658 t
->use
= 0x03; /* System memory */
659 t
->error_correction
= 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
660 size_kb
= QEMU_ALIGN_UP(ram_size
, ONE_KB
) / ONE_KB
;
661 if (size_kb
< MAX_T16_STD_SZ
) {
662 t
->maximum_capacity
= cpu_to_le32(size_kb
);
663 t
->extended_maximum_capacity
= cpu_to_le64(0);
665 t
->maximum_capacity
= cpu_to_le32(MAX_T16_STD_SZ
);
666 t
->extended_maximum_capacity
= cpu_to_le64(ram_size
);
668 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
669 t
->number_of_memory_devices
= cpu_to_le16(dimm_cnt
);
671 SMBIOS_BUILD_TABLE_POST
;
674 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
675 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
677 static void smbios_build_type_17_table(unsigned instance
, uint64_t size
)
682 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance
, true); /* required */
684 t
->physical_memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
685 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
686 t
->total_width
= cpu_to_le16(0xFFFF); /* Unknown */
687 t
->data_width
= cpu_to_le16(0xFFFF); /* Unknown */
688 size_mb
= QEMU_ALIGN_UP(size
, ONE_MB
) / ONE_MB
;
689 if (size_mb
< MAX_T17_STD_SZ
) {
690 t
->size
= cpu_to_le16(size_mb
);
691 t
->extended_size
= cpu_to_le32(0);
693 assert(size_mb
< MAX_T17_EXT_SZ
);
694 t
->size
= cpu_to_le16(MAX_T17_STD_SZ
);
695 t
->extended_size
= cpu_to_le32(size_mb
);
697 t
->form_factor
= 0x09; /* DIMM */
698 t
->device_set
= 0; /* Not in a set */
699 snprintf(loc_str
, sizeof(loc_str
), "%s %d", type17
.loc_pfx
, instance
);
700 SMBIOS_TABLE_SET_STR(17, device_locator_str
, loc_str
);
701 SMBIOS_TABLE_SET_STR(17, bank_locator_str
, type17
.bank
);
702 t
->memory_type
= 0x07; /* RAM */
703 t
->type_detail
= cpu_to_le16(0x02); /* Other */
704 t
->speed
= cpu_to_le16(type17
.speed
);
705 SMBIOS_TABLE_SET_STR(17, manufacturer_str
, type17
.manufacturer
);
706 SMBIOS_TABLE_SET_STR(17, serial_number_str
, type17
.serial
);
707 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str
, type17
.asset
);
708 SMBIOS_TABLE_SET_STR(17, part_number_str
, type17
.part
);
709 t
->attributes
= 0; /* Unknown */
710 t
->configured_clock_speed
= t
->speed
; /* reuse value for max speed */
711 t
->minimum_voltage
= cpu_to_le16(0); /* Unknown */
712 t
->maximum_voltage
= cpu_to_le16(0); /* Unknown */
713 t
->configured_voltage
= cpu_to_le16(0); /* Unknown */
715 SMBIOS_BUILD_TABLE_POST
;
718 static void smbios_build_type_19_table(unsigned instance
,
719 uint64_t start
, uint64_t size
)
721 uint64_t end
, start_kb
, end_kb
;
723 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance
, true); /* required */
725 end
= start
+ size
- 1;
727 start_kb
= start
/ ONE_KB
;
728 end_kb
= end
/ ONE_KB
;
729 if (start_kb
< UINT32_MAX
&& end_kb
< UINT32_MAX
) {
730 t
->starting_address
= cpu_to_le32(start_kb
);
731 t
->ending_address
= cpu_to_le32(end_kb
);
732 t
->extended_starting_address
=
733 t
->extended_ending_address
= cpu_to_le64(0);
735 t
->starting_address
= t
->ending_address
= cpu_to_le32(UINT32_MAX
);
736 t
->extended_starting_address
= cpu_to_le64(start
);
737 t
->extended_ending_address
= cpu_to_le64(end
);
739 t
->memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
740 t
->partition_width
= 1; /* One device per row */
742 SMBIOS_BUILD_TABLE_POST
;
745 static void smbios_build_type_32_table(void)
747 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
749 memset(t
->reserved
, 0, 6);
750 t
->boot_status
= 0; /* No errors detected */
752 SMBIOS_BUILD_TABLE_POST
;
755 static void smbios_build_type_127_table(void)
757 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
758 SMBIOS_BUILD_TABLE_POST
;
761 void smbios_set_cpuid(uint32_t version
, uint32_t features
)
763 smbios_cpuid_version
= version
;
764 smbios_cpuid_features
= features
;
767 #define SMBIOS_SET_DEFAULT(field, value) \
772 void smbios_set_defaults(const char *manufacturer
, const char *product
,
773 const char *version
, bool legacy_mode
,
776 smbios_have_defaults
= true;
777 smbios_legacy
= legacy_mode
;
778 smbios_uuid_encoded
= uuid_encoded
;
780 /* drop unwanted version of command-line file blob(s) */
782 g_free(smbios_tables
);
783 /* in legacy mode, also complain if fields were given for types > 1 */
784 if (find_next_bit(have_fields_bitmap
,
785 SMBIOS_MAX_TYPE
+1, 2) < SMBIOS_MAX_TYPE
+1) {
786 error_report("can't process fields for smbios "
787 "types > 1 on machine versions < 2.1!");
791 g_free(smbios_entries
);
794 SMBIOS_SET_DEFAULT(type1
.manufacturer
, manufacturer
);
795 SMBIOS_SET_DEFAULT(type1
.product
, product
);
796 SMBIOS_SET_DEFAULT(type1
.version
, version
);
797 SMBIOS_SET_DEFAULT(type2
.manufacturer
, manufacturer
);
798 SMBIOS_SET_DEFAULT(type2
.product
, product
);
799 SMBIOS_SET_DEFAULT(type2
.version
, version
);
800 SMBIOS_SET_DEFAULT(type3
.manufacturer
, manufacturer
);
801 SMBIOS_SET_DEFAULT(type3
.version
, version
);
802 SMBIOS_SET_DEFAULT(type4
.sock_pfx
, "CPU");
803 SMBIOS_SET_DEFAULT(type4
.manufacturer
, manufacturer
);
804 SMBIOS_SET_DEFAULT(type4
.version
, version
);
805 SMBIOS_SET_DEFAULT(type17
.loc_pfx
, "DIMM");
806 SMBIOS_SET_DEFAULT(type17
.manufacturer
, manufacturer
);
809 static void smbios_entry_point_setup(void)
811 memcpy(ep
.anchor_string
, "_SM_", 4);
812 memcpy(ep
.intermediate_anchor_string
, "_DMI_", 5);
813 ep
.length
= sizeof(struct smbios_entry_point
);
814 ep
.entry_point_revision
= 0; /* formatted_area reserved, per spec v2.1+ */
815 memset(ep
.formatted_area
, 0, 5);
817 /* compliant with smbios spec v2.8 */
818 ep
.smbios_major_version
= 2;
819 ep
.smbios_minor_version
= 8;
820 ep
.smbios_bcd_revision
= 0x28;
822 /* set during table construction, but BIOS may override: */
823 ep
.structure_table_length
= cpu_to_le16(smbios_tables_len
);
824 ep
.max_structure_size
= cpu_to_le16(smbios_table_max
);
825 ep
.number_of_structures
= cpu_to_le16(smbios_table_cnt
);
827 /* BIOS must recalculate: */
829 ep
.intermediate_checksum
= 0;
830 ep
.structure_table_address
= cpu_to_le32(0);
833 void smbios_get_tables(const struct smbios_phys_mem_area
*mem_array
,
834 const unsigned int mem_array_size
,
835 uint8_t **tables
, size_t *tables_len
,
836 uint8_t **anchor
, size_t *anchor_len
)
838 unsigned i
, dimm_cnt
;
841 *tables
= *anchor
= NULL
;
842 *tables_len
= *anchor_len
= 0;
846 if (!smbios_immutable
) {
847 smbios_build_type_0_table();
848 smbios_build_type_1_table();
849 smbios_build_type_2_table();
850 smbios_build_type_3_table();
852 smbios_smp_sockets
= DIV_ROUND_UP(smp_cpus
, smp_cores
* smp_threads
);
853 assert(smbios_smp_sockets
>= 1);
855 for (i
= 0; i
< smbios_smp_sockets
; i
++) {
856 smbios_build_type_4_table(i
);
859 #define MAX_DIMM_SZ (16ll * ONE_GB)
860 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
861 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
863 dimm_cnt
= QEMU_ALIGN_UP(ram_size
, MAX_DIMM_SZ
) / MAX_DIMM_SZ
;
865 smbios_build_type_16_table(dimm_cnt
);
867 for (i
= 0; i
< dimm_cnt
; i
++) {
868 smbios_build_type_17_table(i
, GET_DIMM_SZ
);
871 for (i
= 0; i
< mem_array_size
; i
++) {
872 smbios_build_type_19_table(i
, mem_array
[i
].address
,
873 mem_array
[i
].length
);
876 smbios_build_type_32_table();
877 smbios_build_type_127_table();
879 smbios_validate_table();
880 smbios_entry_point_setup();
881 smbios_immutable
= true;
884 /* return tables blob and entry point (anchor), and their sizes */
885 *tables
= smbios_tables
;
886 *tables_len
= smbios_tables_len
;
887 *anchor
= (uint8_t *)&ep
;
888 *anchor_len
= sizeof(struct smbios_entry_point
);
891 static void save_opt(const char **dest
, QemuOpts
*opts
, const char *name
)
893 const char *val
= qemu_opt_get(opts
, name
);
900 void smbios_entry_add(QemuOpts
*opts
)
902 Error
*local_err
= NULL
;
905 assert(!smbios_immutable
);
907 val
= qemu_opt_get(opts
, "file");
909 struct smbios_structure_header
*header
;
911 struct smbios_table
*table
; /* legacy mode only */
913 qemu_opts_validate(opts
, qemu_smbios_file_opts
, &local_err
);
915 error_report_err(local_err
);
919 size
= get_image_size(val
);
920 if (size
== -1 || size
< sizeof(struct smbios_structure_header
)) {
921 error_report("Cannot read SMBIOS file %s", val
);
926 * NOTE: standard double '\0' terminator expected, per smbios spec.
927 * (except in legacy mode, where the second '\0' is implicit and
928 * will be inserted by the BIOS).
930 smbios_tables
= g_realloc(smbios_tables
, smbios_tables_len
+ size
);
931 header
= (struct smbios_structure_header
*)(smbios_tables
+
934 if (load_image(val
, (uint8_t *)header
) != size
) {
935 error_report("Failed to load SMBIOS file %s", val
);
939 if (test_bit(header
->type
, have_fields_bitmap
)) {
940 error_report("can't load type %d struct, fields already specified!",
944 set_bit(header
->type
, have_binfile_bitmap
);
946 if (header
->type
== 4) {
947 smbios_type4_count
++;
950 smbios_tables_len
+= size
;
951 if (size
> smbios_table_max
) {
952 smbios_table_max
= size
;
956 /* add a copy of the newly loaded blob to legacy smbios_entries */
957 /* NOTE: This code runs before smbios_set_defaults(), so we don't
958 * yet know which mode (legacy vs. aggregate-table) will be
959 * required. We therefore add the binary blob to both legacy
960 * (smbios_entries) and aggregate (smbios_tables) tables, and
961 * delete the one we don't need from smbios_set_defaults(),
962 * once we know which machine version has been requested.
964 if (!smbios_entries
) {
965 smbios_entries_len
= sizeof(uint16_t);
966 smbios_entries
= g_malloc0(smbios_entries_len
);
968 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
969 size
+ sizeof(*table
));
970 table
= (struct smbios_table
*)(smbios_entries
+ smbios_entries_len
);
971 table
->header
.type
= SMBIOS_TABLE_ENTRY
;
972 table
->header
.length
= cpu_to_le16(sizeof(*table
) + size
);
973 memcpy(table
->data
, header
, size
);
974 smbios_entries_len
+= sizeof(*table
) + size
;
975 (*(uint16_t *)smbios_entries
) =
976 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
977 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
982 val
= qemu_opt_get(opts
, "type");
984 unsigned long type
= strtoul(val
, NULL
, 0);
986 if (type
> SMBIOS_MAX_TYPE
) {
987 error_report("out of range!");
991 if (test_bit(type
, have_binfile_bitmap
)) {
992 error_report("can't add fields, binary file already loaded!");
995 set_bit(type
, have_fields_bitmap
);
999 qemu_opts_validate(opts
, qemu_smbios_type0_opts
, &local_err
);
1001 error_report_err(local_err
);
1004 save_opt(&type0
.vendor
, opts
, "vendor");
1005 save_opt(&type0
.version
, opts
, "version");
1006 save_opt(&type0
.date
, opts
, "date");
1007 type0
.uefi
= qemu_opt_get_bool(opts
, "uefi", false);
1009 val
= qemu_opt_get(opts
, "release");
1011 if (sscanf(val
, "%hhu.%hhu", &type0
.major
, &type0
.minor
) != 2) {
1012 error_report("Invalid release");
1015 type0
.have_major_minor
= true;
1019 qemu_opts_validate(opts
, qemu_smbios_type1_opts
, &local_err
);
1021 error_report_err(local_err
);
1024 save_opt(&type1
.manufacturer
, opts
, "manufacturer");
1025 save_opt(&type1
.product
, opts
, "product");
1026 save_opt(&type1
.version
, opts
, "version");
1027 save_opt(&type1
.serial
, opts
, "serial");
1028 save_opt(&type1
.sku
, opts
, "sku");
1029 save_opt(&type1
.family
, opts
, "family");
1031 val
= qemu_opt_get(opts
, "uuid");
1033 if (qemu_uuid_parse(val
, qemu_uuid
) != 0) {
1034 error_report("Invalid UUID");
1037 qemu_uuid_set
= true;
1041 qemu_opts_validate(opts
, qemu_smbios_type2_opts
, &local_err
);
1043 error_report_err(local_err
);
1046 save_opt(&type2
.manufacturer
, opts
, "manufacturer");
1047 save_opt(&type2
.product
, opts
, "product");
1048 save_opt(&type2
.version
, opts
, "version");
1049 save_opt(&type2
.serial
, opts
, "serial");
1050 save_opt(&type2
.asset
, opts
, "asset");
1051 save_opt(&type2
.location
, opts
, "location");
1054 qemu_opts_validate(opts
, qemu_smbios_type3_opts
, &local_err
);
1056 error_report_err(local_err
);
1059 save_opt(&type3
.manufacturer
, opts
, "manufacturer");
1060 save_opt(&type3
.version
, opts
, "version");
1061 save_opt(&type3
.serial
, opts
, "serial");
1062 save_opt(&type3
.asset
, opts
, "asset");
1063 save_opt(&type3
.sku
, opts
, "sku");
1066 qemu_opts_validate(opts
, qemu_smbios_type4_opts
, &local_err
);
1068 error_report_err(local_err
);
1071 save_opt(&type4
.sock_pfx
, opts
, "sock_pfx");
1072 save_opt(&type4
.manufacturer
, opts
, "manufacturer");
1073 save_opt(&type4
.version
, opts
, "version");
1074 save_opt(&type4
.serial
, opts
, "serial");
1075 save_opt(&type4
.asset
, opts
, "asset");
1076 save_opt(&type4
.part
, opts
, "part");
1079 qemu_opts_validate(opts
, qemu_smbios_type17_opts
, &local_err
);
1081 error_report_err(local_err
);
1084 save_opt(&type17
.loc_pfx
, opts
, "loc_pfx");
1085 save_opt(&type17
.bank
, opts
, "bank");
1086 save_opt(&type17
.manufacturer
, opts
, "manufacturer");
1087 save_opt(&type17
.serial
, opts
, "serial");
1088 save_opt(&type17
.asset
, opts
, "asset");
1089 save_opt(&type17
.part
, opts
, "part");
1090 type17
.speed
= qemu_opt_get_number(opts
, "speed", 0);
1093 error_report("Don't know how to build fields for SMBIOS type %ld",
1099 error_report("Must specify type= or file=");