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/i386/pc.h"
23 #include "hw/i386/smbios.h"
24 #include "hw/loader.h"
27 /* legacy structures and constants for <= 2.0 machines */
28 struct smbios_header
{
34 struct smbios_header header
;
41 struct smbios_header header
;
45 #define SMBIOS_FIELD_ENTRY 0
46 #define SMBIOS_TABLE_ENTRY 1
48 static uint8_t *smbios_entries
;
49 static size_t smbios_entries_len
;
50 static bool smbios_legacy
= true;
51 static bool smbios_uuid_encoded
= true;
52 /* end: legacy structures & constants for <= 2.0 machines */
55 static uint8_t *smbios_tables
;
56 static size_t smbios_tables_len
;
57 static unsigned smbios_table_max
;
58 static unsigned smbios_table_cnt
;
59 static struct smbios_entry_point ep
;
61 static int smbios_type4_count
= 0;
62 static bool smbios_immutable
;
63 static bool smbios_have_defaults
;
64 static uint32_t smbios_cpuid_version
, smbios_cpuid_features
, smbios_smp_sockets
;
66 static DECLARE_BITMAP(have_binfile_bitmap
, SMBIOS_MAX_TYPE
+1);
67 static DECLARE_BITMAP(have_fields_bitmap
, SMBIOS_MAX_TYPE
+1);
70 const char *vendor
, *version
, *date
;
71 bool have_major_minor
, uefi
;
76 const char *manufacturer
, *product
, *version
, *serial
, *sku
, *family
;
77 /* uuid is in qemu_uuid[] */
81 const char *manufacturer
, *product
, *version
, *serial
, *asset
, *location
;
85 const char *manufacturer
, *version
, *serial
, *asset
, *sku
;
89 const char *sock_pfx
, *manufacturer
, *version
, *serial
, *asset
, *part
;
93 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",
308 { /* end of list */ }
311 static void smbios_register_config(void)
313 qemu_add_opts(&qemu_smbios_opts
);
316 machine_init(smbios_register_config
);
318 static void smbios_validate_table(void)
320 uint32_t expect_t4_count
= smbios_legacy
? smp_cpus
: smbios_smp_sockets
;
322 if (smbios_type4_count
&& smbios_type4_count
!= expect_t4_count
) {
323 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
324 expect_t4_count
, smbios_type4_count
);
330 /* legacy setup functions for <= 2.0 machines */
331 static void smbios_add_field(int type
, int offset
, const void *data
, size_t len
)
333 struct smbios_field
*field
;
335 if (!smbios_entries
) {
336 smbios_entries_len
= sizeof(uint16_t);
337 smbios_entries
= g_malloc0(smbios_entries_len
);
339 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
340 sizeof(*field
) + len
);
341 field
= (struct smbios_field
*)(smbios_entries
+ smbios_entries_len
);
342 field
->header
.type
= SMBIOS_FIELD_ENTRY
;
343 field
->header
.length
= cpu_to_le16(sizeof(*field
) + len
);
346 field
->offset
= cpu_to_le16(offset
);
347 memcpy(field
->data
, data
, len
);
349 smbios_entries_len
+= sizeof(*field
) + len
;
350 (*(uint16_t *)smbios_entries
) =
351 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
354 static void smbios_maybe_add_str(int type
, int offset
, const char *data
)
357 smbios_add_field(type
, offset
, data
, strlen(data
) + 1);
361 static void smbios_build_type_0_fields(void)
363 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, vendor_str
),
365 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
, bios_version_str
),
367 smbios_maybe_add_str(0, offsetof(struct smbios_type_0
,
368 bios_release_date_str
),
370 if (type0
.have_major_minor
) {
371 smbios_add_field(0, offsetof(struct smbios_type_0
,
372 system_bios_major_release
),
374 smbios_add_field(0, offsetof(struct smbios_type_0
,
375 system_bios_minor_release
),
380 static void smbios_build_type_1_fields(void)
382 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, manufacturer_str
),
384 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, product_name_str
),
386 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, version_str
),
388 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, serial_number_str
),
390 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, sku_number_str
),
392 smbios_maybe_add_str(1, offsetof(struct smbios_type_1
, family_str
),
395 /* We don't encode the UUID in the "wire format" here because this
396 * function is for legacy mode and needs to keep the guest ABI, and
397 * because we don't know what's the SMBIOS version advertised by the
400 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
),
405 uint8_t *smbios_get_table_legacy(size_t *length
)
407 if (!smbios_legacy
) {
412 if (!smbios_immutable
) {
413 smbios_build_type_0_fields();
414 smbios_build_type_1_fields();
415 smbios_validate_table();
416 smbios_immutable
= true;
418 *length
= smbios_entries_len
;
419 return smbios_entries
;
421 /* end: legacy setup functions for <= 2.0 machines */
424 static bool smbios_skip_table(uint8_t type
, bool required_table
)
426 if (test_bit(type
, have_binfile_bitmap
)) {
427 return true; /* user provided their own binary blob(s) */
429 if (test_bit(type
, have_fields_bitmap
)) {
430 return false; /* user provided fields via command line */
432 if (smbios_have_defaults
&& required_table
) {
433 return false; /* we're building tables, and this one's required */
438 #define SMBIOS_BUILD_TABLE_PRE(tbl_type, tbl_handle, tbl_required) \
439 struct smbios_type_##tbl_type *t; \
440 size_t t_off; /* table offset into smbios_tables */ \
443 /* should we skip building this table ? */ \
444 if (smbios_skip_table(tbl_type, tbl_required)) { \
448 /* use offset of table t within smbios_tables */ \
449 /* (pointer must be updated after each realloc) */ \
450 t_off = smbios_tables_len; \
451 smbios_tables_len += sizeof(*t); \
452 smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
453 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
455 t->header.type = tbl_type; \
456 t->header.length = sizeof(*t); \
457 t->header.handle = cpu_to_le16(tbl_handle); \
460 #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
462 int len = (value != NULL) ? strlen(value) + 1 : 0; \
464 smbios_tables = g_realloc(smbios_tables, \
465 smbios_tables_len + len); \
466 memcpy(smbios_tables + smbios_tables_len, value, len); \
467 smbios_tables_len += len; \
468 /* update pointer post-realloc */ \
469 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
470 t->field = ++str_index; \
476 #define SMBIOS_BUILD_TABLE_POST \
478 size_t term_cnt, t_size; \
480 /* add '\0' terminator (add two if no strings defined) */ \
481 term_cnt = (str_index == 0) ? 2 : 1; \
482 smbios_tables = g_realloc(smbios_tables, \
483 smbios_tables_len + term_cnt); \
484 memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
485 smbios_tables_len += term_cnt; \
487 /* update smbios max. element size */ \
488 t_size = smbios_tables_len - t_off; \
489 if (t_size > smbios_table_max) { \
490 smbios_table_max = t_size; \
493 /* update smbios element count */ \
494 smbios_table_cnt++; \
497 static void smbios_build_type_0_table(void)
499 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
501 SMBIOS_TABLE_SET_STR(0, vendor_str
, type0
.vendor
);
502 SMBIOS_TABLE_SET_STR(0, bios_version_str
, type0
.version
);
504 t
->bios_starting_address_segment
= cpu_to_le16(0xE800); /* from SeaBIOS */
506 SMBIOS_TABLE_SET_STR(0, bios_release_date_str
, type0
.date
);
508 t
->bios_rom_size
= 0; /* hardcoded in SeaBIOS with FIXME comment */
510 t
->bios_characteristics
= cpu_to_le64(0x08); /* Not supported */
511 t
->bios_characteristics_extension_bytes
[0] = 0;
512 t
->bios_characteristics_extension_bytes
[1] = 0x14; /* TCD/SVVP | VM */
514 t
->bios_characteristics_extension_bytes
[1] |= 0x08; /* |= UEFI */
517 if (type0
.have_major_minor
) {
518 t
->system_bios_major_release
= type0
.major
;
519 t
->system_bios_minor_release
= type0
.minor
;
521 t
->system_bios_major_release
= 0;
522 t
->system_bios_minor_release
= 0;
525 /* hardcoded in SeaBIOS */
526 t
->embedded_controller_major_release
= 0xFF;
527 t
->embedded_controller_minor_release
= 0xFF;
529 SMBIOS_BUILD_TABLE_POST
;
532 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
533 * format specified by SMBIOS version 2.6.
535 static void smbios_encode_uuid(struct smbios_uuid
*uuid
, const uint8_t *buf
)
537 memcpy(uuid
, buf
, 16);
538 if (smbios_uuid_encoded
) {
539 uuid
->time_low
= bswap32(uuid
->time_low
);
540 uuid
->time_mid
= bswap16(uuid
->time_mid
);
541 uuid
->time_hi_and_version
= bswap16(uuid
->time_hi_and_version
);
545 static void smbios_build_type_1_table(void)
547 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
549 SMBIOS_TABLE_SET_STR(1, manufacturer_str
, type1
.manufacturer
);
550 SMBIOS_TABLE_SET_STR(1, product_name_str
, type1
.product
);
551 SMBIOS_TABLE_SET_STR(1, version_str
, type1
.version
);
552 SMBIOS_TABLE_SET_STR(1, serial_number_str
, type1
.serial
);
554 smbios_encode_uuid(&t
->uuid
, qemu_uuid
);
556 memset(&t
->uuid
, 0, 16);
558 t
->wake_up_type
= 0x06; /* power switch */
559 SMBIOS_TABLE_SET_STR(1, sku_number_str
, type1
.sku
);
560 SMBIOS_TABLE_SET_STR(1, family_str
, type1
.family
);
562 SMBIOS_BUILD_TABLE_POST
;
565 static void smbios_build_type_2_table(void)
567 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
569 SMBIOS_TABLE_SET_STR(2, manufacturer_str
, type2
.manufacturer
);
570 SMBIOS_TABLE_SET_STR(2, product_str
, type2
.product
);
571 SMBIOS_TABLE_SET_STR(2, version_str
, type2
.version
);
572 SMBIOS_TABLE_SET_STR(2, serial_number_str
, type2
.serial
);
573 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str
, type2
.asset
);
574 t
->feature_flags
= 0x01; /* Motherboard */
575 SMBIOS_TABLE_SET_STR(2, location_str
, type2
.location
);
576 t
->chassis_handle
= cpu_to_le16(0x300); /* Type 3 (System enclosure) */
577 t
->board_type
= 0x0A; /* Motherboard */
578 t
->contained_element_count
= 0;
580 SMBIOS_BUILD_TABLE_POST
;
583 static void smbios_build_type_3_table(void)
585 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
587 SMBIOS_TABLE_SET_STR(3, manufacturer_str
, type3
.manufacturer
);
588 t
->type
= 0x01; /* Other */
589 SMBIOS_TABLE_SET_STR(3, version_str
, type3
.version
);
590 SMBIOS_TABLE_SET_STR(3, serial_number_str
, type3
.serial
);
591 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str
, type3
.asset
);
592 t
->boot_up_state
= 0x03; /* Safe */
593 t
->power_supply_state
= 0x03; /* Safe */
594 t
->thermal_state
= 0x03; /* Safe */
595 t
->security_status
= 0x02; /* Unknown */
596 t
->oem_defined
= cpu_to_le32(0);
598 t
->number_of_power_cords
= 0;
599 t
->contained_element_count
= 0;
600 SMBIOS_TABLE_SET_STR(3, sku_number_str
, type3
.sku
);
602 SMBIOS_BUILD_TABLE_POST
;
605 static void smbios_build_type_4_table(unsigned instance
)
609 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance
, true); /* required */
611 snprintf(sock_str
, sizeof(sock_str
), "%s%2x", type4
.sock_pfx
, instance
);
612 SMBIOS_TABLE_SET_STR(4, socket_designation_str
, sock_str
);
613 t
->processor_type
= 0x03; /* CPU */
614 t
->processor_family
= 0x01; /* Other */
615 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str
, type4
.manufacturer
);
616 t
->processor_id
[0] = cpu_to_le32(smbios_cpuid_version
);
617 t
->processor_id
[1] = cpu_to_le32(smbios_cpuid_features
);
618 SMBIOS_TABLE_SET_STR(4, processor_version_str
, type4
.version
);
620 t
->external_clock
= cpu_to_le16(0); /* Unknown */
621 t
->max_speed
= cpu_to_le16(0); /* Unknown */
622 t
->current_speed
= cpu_to_le16(0); /* Unknown */
623 t
->status
= 0x41; /* Socket populated, CPU enabled */
624 t
->processor_upgrade
= 0x01; /* Other */
625 t
->l1_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
626 t
->l2_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
627 t
->l3_cache_handle
= cpu_to_le16(0xFFFF); /* N/A */
628 SMBIOS_TABLE_SET_STR(4, serial_number_str
, type4
.serial
);
629 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str
, type4
.asset
);
630 SMBIOS_TABLE_SET_STR(4, part_number_str
, type4
.part
);
631 t
->core_count
= t
->core_enabled
= smp_cores
;
632 t
->thread_count
= smp_threads
;
633 t
->processor_characteristics
= cpu_to_le16(0x02); /* Unknown */
634 t
->processor_family2
= cpu_to_le16(0x01); /* Other */
636 SMBIOS_BUILD_TABLE_POST
;
637 smbios_type4_count
++;
640 #define ONE_KB ((ram_addr_t)1 << 10)
641 #define ONE_MB ((ram_addr_t)1 << 20)
642 #define ONE_GB ((ram_addr_t)1 << 30)
644 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
646 static void smbios_build_type_16_table(unsigned dimm_cnt
)
650 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
652 t
->location
= 0x01; /* Other */
653 t
->use
= 0x03; /* System memory */
654 t
->error_correction
= 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
655 size_kb
= QEMU_ALIGN_UP(ram_size
, ONE_KB
) / ONE_KB
;
656 if (size_kb
< MAX_T16_STD_SZ
) {
657 t
->maximum_capacity
= cpu_to_le32(size_kb
);
658 t
->extended_maximum_capacity
= cpu_to_le64(0);
660 t
->maximum_capacity
= cpu_to_le32(MAX_T16_STD_SZ
);
661 t
->extended_maximum_capacity
= cpu_to_le64(ram_size
);
663 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
664 t
->number_of_memory_devices
= cpu_to_le16(dimm_cnt
);
666 SMBIOS_BUILD_TABLE_POST
;
669 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
670 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
672 static void smbios_build_type_17_table(unsigned instance
, uint64_t size
)
677 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance
, true); /* required */
679 t
->physical_memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
680 t
->memory_error_information_handle
= cpu_to_le16(0xFFFE); /* Not provided */
681 t
->total_width
= cpu_to_le16(0xFFFF); /* Unknown */
682 t
->data_width
= cpu_to_le16(0xFFFF); /* Unknown */
683 size_mb
= QEMU_ALIGN_UP(size
, ONE_MB
) / ONE_MB
;
684 if (size_mb
< MAX_T17_STD_SZ
) {
685 t
->size
= cpu_to_le16(size_mb
);
686 t
->extended_size
= cpu_to_le32(0);
688 assert(size_mb
< MAX_T17_EXT_SZ
);
689 t
->size
= cpu_to_le16(MAX_T17_STD_SZ
);
690 t
->extended_size
= cpu_to_le32(size_mb
);
692 t
->form_factor
= 0x09; /* DIMM */
693 t
->device_set
= 0; /* Not in a set */
694 snprintf(loc_str
, sizeof(loc_str
), "%s %d", type17
.loc_pfx
, instance
);
695 SMBIOS_TABLE_SET_STR(17, device_locator_str
, loc_str
);
696 SMBIOS_TABLE_SET_STR(17, bank_locator_str
, type17
.bank
);
697 t
->memory_type
= 0x07; /* RAM */
698 t
->type_detail
= cpu_to_le16(0x02); /* Other */
699 t
->speed
= cpu_to_le16(0); /* Unknown */
700 SMBIOS_TABLE_SET_STR(17, manufacturer_str
, type17
.manufacturer
);
701 SMBIOS_TABLE_SET_STR(17, serial_number_str
, type17
.serial
);
702 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str
, type17
.asset
);
703 SMBIOS_TABLE_SET_STR(17, part_number_str
, type17
.part
);
704 t
->attributes
= 0; /* Unknown */
705 t
->configured_clock_speed
= cpu_to_le16(0); /* Unknown */
706 t
->minimum_voltage
= cpu_to_le16(0); /* Unknown */
707 t
->maximum_voltage
= cpu_to_le16(0); /* Unknown */
708 t
->configured_voltage
= cpu_to_le16(0); /* Unknown */
710 SMBIOS_BUILD_TABLE_POST
;
713 static void smbios_build_type_19_table(unsigned instance
,
714 uint64_t start
, uint64_t size
)
716 uint64_t end
, start_kb
, end_kb
;
718 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance
, true); /* required */
720 end
= start
+ size
- 1;
722 start_kb
= start
/ ONE_KB
;
723 end_kb
= end
/ ONE_KB
;
724 if (start_kb
< UINT32_MAX
&& end_kb
< UINT32_MAX
) {
725 t
->starting_address
= cpu_to_le32(start_kb
);
726 t
->ending_address
= cpu_to_le32(end_kb
);
727 t
->extended_starting_address
=
728 t
->extended_ending_address
= cpu_to_le64(0);
730 t
->starting_address
= t
->ending_address
= cpu_to_le32(UINT32_MAX
);
731 t
->extended_starting_address
= cpu_to_le64(start
);
732 t
->extended_ending_address
= cpu_to_le64(end
);
734 t
->memory_array_handle
= cpu_to_le16(0x1000); /* Type 16 above */
735 t
->partition_width
= 1; /* One device per row */
737 SMBIOS_BUILD_TABLE_POST
;
740 static void smbios_build_type_32_table(void)
742 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
744 memset(t
->reserved
, 0, 6);
745 t
->boot_status
= 0; /* No errors detected */
747 SMBIOS_BUILD_TABLE_POST
;
750 static void smbios_build_type_127_table(void)
752 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
753 SMBIOS_BUILD_TABLE_POST
;
756 void smbios_set_cpuid(uint32_t version
, uint32_t features
)
758 smbios_cpuid_version
= version
;
759 smbios_cpuid_features
= features
;
762 #define SMBIOS_SET_DEFAULT(field, value) \
767 void smbios_set_defaults(const char *manufacturer
, const char *product
,
768 const char *version
, bool legacy_mode
,
771 smbios_have_defaults
= true;
772 smbios_legacy
= legacy_mode
;
773 smbios_uuid_encoded
= uuid_encoded
;
775 /* drop unwanted version of command-line file blob(s) */
777 g_free(smbios_tables
);
778 /* in legacy mode, also complain if fields were given for types > 1 */
779 if (find_next_bit(have_fields_bitmap
,
780 SMBIOS_MAX_TYPE
+1, 2) < SMBIOS_MAX_TYPE
+1) {
781 error_report("can't process fields for smbios "
782 "types > 1 on machine versions < 2.1!");
786 g_free(smbios_entries
);
789 SMBIOS_SET_DEFAULT(type1
.manufacturer
, manufacturer
);
790 SMBIOS_SET_DEFAULT(type1
.product
, product
);
791 SMBIOS_SET_DEFAULT(type1
.version
, version
);
792 SMBIOS_SET_DEFAULT(type2
.manufacturer
, manufacturer
);
793 SMBIOS_SET_DEFAULT(type2
.product
, product
);
794 SMBIOS_SET_DEFAULT(type2
.version
, version
);
795 SMBIOS_SET_DEFAULT(type3
.manufacturer
, manufacturer
);
796 SMBIOS_SET_DEFAULT(type3
.version
, version
);
797 SMBIOS_SET_DEFAULT(type4
.sock_pfx
, "CPU");
798 SMBIOS_SET_DEFAULT(type4
.manufacturer
, manufacturer
);
799 SMBIOS_SET_DEFAULT(type4
.version
, version
);
800 SMBIOS_SET_DEFAULT(type17
.loc_pfx
, "DIMM");
801 SMBIOS_SET_DEFAULT(type17
.manufacturer
, manufacturer
);
804 static void smbios_entry_point_setup(void)
806 memcpy(ep
.anchor_string
, "_SM_", 4);
807 memcpy(ep
.intermediate_anchor_string
, "_DMI_", 5);
808 ep
.length
= sizeof(struct smbios_entry_point
);
809 ep
.entry_point_revision
= 0; /* formatted_area reserved, per spec v2.1+ */
810 memset(ep
.formatted_area
, 0, 5);
812 /* compliant with smbios spec v2.8 */
813 ep
.smbios_major_version
= 2;
814 ep
.smbios_minor_version
= 8;
815 ep
.smbios_bcd_revision
= 0x28;
817 /* set during table construction, but BIOS may override: */
818 ep
.structure_table_length
= cpu_to_le16(smbios_tables_len
);
819 ep
.max_structure_size
= cpu_to_le16(smbios_table_max
);
820 ep
.number_of_structures
= cpu_to_le16(smbios_table_cnt
);
822 /* BIOS must recalculate: */
824 ep
.intermediate_checksum
= 0;
825 ep
.structure_table_address
= cpu_to_le32(0);
828 void smbios_get_tables(uint8_t **tables
, size_t *tables_len
,
829 uint8_t **anchor
, size_t *anchor_len
)
831 unsigned i
, dimm_cnt
, instance
;
834 *tables
= *anchor
= NULL
;
835 *tables_len
= *anchor_len
= 0;
839 if (!smbios_immutable
) {
840 smbios_build_type_0_table();
841 smbios_build_type_1_table();
842 smbios_build_type_2_table();
843 smbios_build_type_3_table();
845 smbios_smp_sockets
= DIV_ROUND_UP(smp_cpus
, smp_cores
* smp_threads
);
846 assert(smbios_smp_sockets
>= 1);
848 for (i
= 0; i
< smbios_smp_sockets
; i
++) {
849 smbios_build_type_4_table(i
);
852 #define MAX_DIMM_SZ (16ll * ONE_GB)
853 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ : ram_size % MAX_DIMM_SZ)
855 dimm_cnt
= QEMU_ALIGN_UP(ram_size
, MAX_DIMM_SZ
) / MAX_DIMM_SZ
;
857 smbios_build_type_16_table(dimm_cnt
);
859 for (i
= 0; i
< dimm_cnt
; i
++) {
860 smbios_build_type_17_table(i
, GET_DIMM_SZ
);
863 for (i
= 0, instance
= 0; i
< e820_get_num_entries(); i
++) {
864 uint64_t address
, length
;
865 if (e820_get_entry(i
, E820_RAM
, &address
, &length
)) {
866 smbios_build_type_19_table(instance
++, address
, length
);
870 smbios_build_type_32_table();
871 smbios_build_type_127_table();
873 smbios_validate_table();
874 smbios_entry_point_setup();
875 smbios_immutable
= true;
878 /* return tables blob and entry point (anchor), and their sizes */
879 *tables
= smbios_tables
;
880 *tables_len
= smbios_tables_len
;
881 *anchor
= (uint8_t *)&ep
;
882 *anchor_len
= sizeof(struct smbios_entry_point
);
885 static void save_opt(const char **dest
, QemuOpts
*opts
, const char *name
)
887 const char *val
= qemu_opt_get(opts
, name
);
894 void smbios_entry_add(QemuOpts
*opts
)
896 Error
*local_err
= NULL
;
899 assert(!smbios_immutable
);
901 val
= qemu_opt_get(opts
, "file");
903 struct smbios_structure_header
*header
;
905 struct smbios_table
*table
; /* legacy mode only */
907 qemu_opts_validate(opts
, qemu_smbios_file_opts
, &local_err
);
909 error_report("%s", error_get_pretty(local_err
));
913 size
= get_image_size(val
);
914 if (size
== -1 || size
< sizeof(struct smbios_structure_header
)) {
915 error_report("Cannot read SMBIOS file %s", val
);
920 * NOTE: standard double '\0' terminator expected, per smbios spec.
921 * (except in legacy mode, where the second '\0' is implicit and
922 * will be inserted by the BIOS).
924 smbios_tables
= g_realloc(smbios_tables
, smbios_tables_len
+ size
);
925 header
= (struct smbios_structure_header
*)(smbios_tables
+
928 if (load_image(val
, (uint8_t *)header
) != size
) {
929 error_report("Failed to load SMBIOS file %s", val
);
933 if (test_bit(header
->type
, have_fields_bitmap
)) {
934 error_report("can't load type %d struct, fields already specified!",
938 set_bit(header
->type
, have_binfile_bitmap
);
940 if (header
->type
== 4) {
941 smbios_type4_count
++;
944 smbios_tables_len
+= size
;
945 if (size
> smbios_table_max
) {
946 smbios_table_max
= size
;
950 /* add a copy of the newly loaded blob to legacy smbios_entries */
951 /* NOTE: This code runs before smbios_set_defaults(), so we don't
952 * yet know which mode (legacy vs. aggregate-table) will be
953 * required. We therefore add the binary blob to both legacy
954 * (smbios_entries) and aggregate (smbios_tables) tables, and
955 * delete the one we don't need from smbios_set_defaults(),
956 * once we know which machine version has been requested.
958 if (!smbios_entries
) {
959 smbios_entries_len
= sizeof(uint16_t);
960 smbios_entries
= g_malloc0(smbios_entries_len
);
962 smbios_entries
= g_realloc(smbios_entries
, smbios_entries_len
+
963 size
+ sizeof(*table
));
964 table
= (struct smbios_table
*)(smbios_entries
+ smbios_entries_len
);
965 table
->header
.type
= SMBIOS_TABLE_ENTRY
;
966 table
->header
.length
= cpu_to_le16(sizeof(*table
) + size
);
967 memcpy(table
->data
, header
, size
);
968 smbios_entries_len
+= sizeof(*table
) + size
;
969 (*(uint16_t *)smbios_entries
) =
970 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries
) + 1);
971 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
976 val
= qemu_opt_get(opts
, "type");
978 unsigned long type
= strtoul(val
, NULL
, 0);
980 if (type
> SMBIOS_MAX_TYPE
) {
981 error_report("out of range!");
985 if (test_bit(type
, have_binfile_bitmap
)) {
986 error_report("can't add fields, binary file already loaded!");
989 set_bit(type
, have_fields_bitmap
);
993 qemu_opts_validate(opts
, qemu_smbios_type0_opts
, &local_err
);
995 error_report("%s", error_get_pretty(local_err
));
998 save_opt(&type0
.vendor
, opts
, "vendor");
999 save_opt(&type0
.version
, opts
, "version");
1000 save_opt(&type0
.date
, opts
, "date");
1001 type0
.uefi
= qemu_opt_get_bool(opts
, "uefi", false);
1003 val
= qemu_opt_get(opts
, "release");
1005 if (sscanf(val
, "%hhu.%hhu", &type0
.major
, &type0
.minor
) != 2) {
1006 error_report("Invalid release");
1009 type0
.have_major_minor
= true;
1013 qemu_opts_validate(opts
, qemu_smbios_type1_opts
, &local_err
);
1015 error_report("%s", error_get_pretty(local_err
));
1018 save_opt(&type1
.manufacturer
, opts
, "manufacturer");
1019 save_opt(&type1
.product
, opts
, "product");
1020 save_opt(&type1
.version
, opts
, "version");
1021 save_opt(&type1
.serial
, opts
, "serial");
1022 save_opt(&type1
.sku
, opts
, "sku");
1023 save_opt(&type1
.family
, opts
, "family");
1025 val
= qemu_opt_get(opts
, "uuid");
1027 if (qemu_uuid_parse(val
, qemu_uuid
) != 0) {
1028 error_report("Invalid UUID");
1031 qemu_uuid_set
= true;
1035 qemu_opts_validate(opts
, qemu_smbios_type2_opts
, &local_err
);
1037 error_report("%s", error_get_pretty(local_err
));
1040 save_opt(&type2
.manufacturer
, opts
, "manufacturer");
1041 save_opt(&type2
.product
, opts
, "product");
1042 save_opt(&type2
.version
, opts
, "version");
1043 save_opt(&type2
.serial
, opts
, "serial");
1044 save_opt(&type2
.asset
, opts
, "asset");
1045 save_opt(&type2
.location
, opts
, "location");
1048 qemu_opts_validate(opts
, qemu_smbios_type3_opts
, &local_err
);
1050 error_report("%s", error_get_pretty(local_err
));
1053 save_opt(&type3
.manufacturer
, opts
, "manufacturer");
1054 save_opt(&type3
.version
, opts
, "version");
1055 save_opt(&type3
.serial
, opts
, "serial");
1056 save_opt(&type3
.asset
, opts
, "asset");
1057 save_opt(&type3
.sku
, opts
, "sku");
1060 qemu_opts_validate(opts
, qemu_smbios_type4_opts
, &local_err
);
1062 error_report("%s", error_get_pretty(local_err
));
1065 save_opt(&type4
.sock_pfx
, opts
, "sock_pfx");
1066 save_opt(&type4
.manufacturer
, opts
, "manufacturer");
1067 save_opt(&type4
.version
, opts
, "version");
1068 save_opt(&type4
.serial
, opts
, "serial");
1069 save_opt(&type4
.asset
, opts
, "asset");
1070 save_opt(&type4
.part
, opts
, "part");
1073 qemu_opts_validate(opts
, qemu_smbios_type17_opts
, &local_err
);
1075 error_report("%s", error_get_pretty(local_err
));
1078 save_opt(&type17
.loc_pfx
, opts
, "loc_pfx");
1079 save_opt(&type17
.bank
, opts
, "bank");
1080 save_opt(&type17
.manufacturer
, opts
, "manufacturer");
1081 save_opt(&type17
.serial
, opts
, "serial");
1082 save_opt(&type17
.asset
, opts
, "asset");
1083 save_opt(&type17
.part
, opts
, "part");
1086 error_report("Don't know how to build fields for SMBIOS type %ld",
1092 error_report("Must specify type= or file=");