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