meson: use pkg-config method for libudev
[qemu/ar7.git] / hw / smbios / smbios.c
blobf22c4f5b734e89a390cada7ea422b4e0e34d3562
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 "smbios_build.h"
32 /* legacy structures and constants for <= 2.0 machines */
33 struct smbios_header {
34 uint16_t length;
35 uint8_t type;
36 } QEMU_PACKED;
38 struct smbios_field {
39 struct smbios_header header;
40 uint8_t type;
41 uint16_t offset;
42 uint8_t data[];
43 } QEMU_PACKED;
45 struct smbios_table {
46 struct smbios_header header;
47 uint8_t data[];
48 } QEMU_PACKED;
50 #define SMBIOS_FIELD_ENTRY 0
51 #define SMBIOS_TABLE_ENTRY 1
53 static uint8_t *smbios_entries;
54 static size_t smbios_entries_len;
55 static bool smbios_legacy = true;
56 static bool smbios_uuid_encoded = true;
57 /* end: legacy structures & constants for <= 2.0 machines */
60 uint8_t *smbios_tables;
61 size_t smbios_tables_len;
62 unsigned smbios_table_max;
63 unsigned smbios_table_cnt;
64 static SmbiosEntryPointType smbios_ep_type = SMBIOS_ENTRY_POINT_21;
66 static SmbiosEntryPoint ep;
68 static int smbios_type4_count = 0;
69 static bool smbios_immutable;
70 static bool smbios_have_defaults;
71 static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
73 static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
74 static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
76 static struct {
77 const char *vendor, *version, *date;
78 bool have_major_minor, uefi;
79 uint8_t major, minor;
80 } type0;
82 static struct {
83 const char *manufacturer, *product, *version, *serial, *sku, *family;
84 /* uuid is in qemu_uuid */
85 } type1;
87 static struct {
88 const char *manufacturer, *product, *version, *serial, *asset, *location;
89 } type2;
91 static struct {
92 const char *manufacturer, *version, *serial, *asset, *sku;
93 } type3;
96 * SVVP requires max_speed and current_speed to be set and not being
97 * 0 which counts as unknown (SMBIOS 3.1.0/Table 21). Set the
98 * default value to 2000MHz as we did before.
100 #define DEFAULT_CPU_SPEED 2000
102 static struct {
103 const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
104 uint64_t max_speed;
105 uint64_t current_speed;
106 } type4 = {
107 .max_speed = DEFAULT_CPU_SPEED,
108 .current_speed = DEFAULT_CPU_SPEED
111 static struct {
112 size_t nvalues;
113 char **values;
114 } type11;
116 static struct {
117 const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
118 uint16_t speed;
119 } type17;
121 static QemuOptsList qemu_smbios_opts = {
122 .name = "smbios",
123 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
124 .desc = {
126 * no elements => accept any params
127 * validation will happen later
129 { /* end of list */ }
133 static const QemuOptDesc qemu_smbios_file_opts[] = {
135 .name = "file",
136 .type = QEMU_OPT_STRING,
137 .help = "binary file containing an SMBIOS element",
139 { /* end of list */ }
142 static const QemuOptDesc qemu_smbios_type0_opts[] = {
144 .name = "type",
145 .type = QEMU_OPT_NUMBER,
146 .help = "SMBIOS element type",
148 .name = "vendor",
149 .type = QEMU_OPT_STRING,
150 .help = "vendor name",
152 .name = "version",
153 .type = QEMU_OPT_STRING,
154 .help = "version number",
156 .name = "date",
157 .type = QEMU_OPT_STRING,
158 .help = "release date",
160 .name = "release",
161 .type = QEMU_OPT_STRING,
162 .help = "revision number",
164 .name = "uefi",
165 .type = QEMU_OPT_BOOL,
166 .help = "uefi support",
168 { /* end of list */ }
171 static const QemuOptDesc qemu_smbios_type1_opts[] = {
173 .name = "type",
174 .type = QEMU_OPT_NUMBER,
175 .help = "SMBIOS element type",
177 .name = "manufacturer",
178 .type = QEMU_OPT_STRING,
179 .help = "manufacturer name",
181 .name = "product",
182 .type = QEMU_OPT_STRING,
183 .help = "product name",
185 .name = "version",
186 .type = QEMU_OPT_STRING,
187 .help = "version number",
189 .name = "serial",
190 .type = QEMU_OPT_STRING,
191 .help = "serial number",
193 .name = "uuid",
194 .type = QEMU_OPT_STRING,
195 .help = "UUID",
197 .name = "sku",
198 .type = QEMU_OPT_STRING,
199 .help = "SKU number",
201 .name = "family",
202 .type = QEMU_OPT_STRING,
203 .help = "family name",
205 { /* end of list */ }
208 static const QemuOptDesc qemu_smbios_type2_opts[] = {
210 .name = "type",
211 .type = QEMU_OPT_NUMBER,
212 .help = "SMBIOS element type",
214 .name = "manufacturer",
215 .type = QEMU_OPT_STRING,
216 .help = "manufacturer name",
218 .name = "product",
219 .type = QEMU_OPT_STRING,
220 .help = "product name",
222 .name = "version",
223 .type = QEMU_OPT_STRING,
224 .help = "version number",
226 .name = "serial",
227 .type = QEMU_OPT_STRING,
228 .help = "serial number",
230 .name = "asset",
231 .type = QEMU_OPT_STRING,
232 .help = "asset tag number",
234 .name = "location",
235 .type = QEMU_OPT_STRING,
236 .help = "location in chassis",
238 { /* end of list */ }
241 static const QemuOptDesc qemu_smbios_type3_opts[] = {
243 .name = "type",
244 .type = QEMU_OPT_NUMBER,
245 .help = "SMBIOS element type",
247 .name = "manufacturer",
248 .type = QEMU_OPT_STRING,
249 .help = "manufacturer name",
251 .name = "version",
252 .type = QEMU_OPT_STRING,
253 .help = "version number",
255 .name = "serial",
256 .type = QEMU_OPT_STRING,
257 .help = "serial number",
259 .name = "asset",
260 .type = QEMU_OPT_STRING,
261 .help = "asset tag number",
263 .name = "sku",
264 .type = QEMU_OPT_STRING,
265 .help = "SKU number",
267 { /* end of list */ }
270 static const QemuOptDesc qemu_smbios_type4_opts[] = {
272 .name = "type",
273 .type = QEMU_OPT_NUMBER,
274 .help = "SMBIOS element type",
276 .name = "sock_pfx",
277 .type = QEMU_OPT_STRING,
278 .help = "socket designation string prefix",
280 .name = "manufacturer",
281 .type = QEMU_OPT_STRING,
282 .help = "manufacturer name",
284 .name = "version",
285 .type = QEMU_OPT_STRING,
286 .help = "version number",
288 .name = "max-speed",
289 .type = QEMU_OPT_NUMBER,
290 .help = "max speed in MHz",
292 .name = "current-speed",
293 .type = QEMU_OPT_NUMBER,
294 .help = "speed at system boot in MHz",
296 .name = "serial",
297 .type = QEMU_OPT_STRING,
298 .help = "serial number",
300 .name = "asset",
301 .type = QEMU_OPT_STRING,
302 .help = "asset tag number",
304 .name = "part",
305 .type = QEMU_OPT_STRING,
306 .help = "part number",
308 { /* end of list */ }
311 static const QemuOptDesc qemu_smbios_type11_opts[] = {
313 .name = "value",
314 .type = QEMU_OPT_STRING,
315 .help = "OEM string data",
318 .name = "path",
319 .type = QEMU_OPT_STRING,
320 .help = "OEM string data from file",
324 static const QemuOptDesc qemu_smbios_type17_opts[] = {
326 .name = "type",
327 .type = QEMU_OPT_NUMBER,
328 .help = "SMBIOS element type",
330 .name = "loc_pfx",
331 .type = QEMU_OPT_STRING,
332 .help = "device locator string prefix",
334 .name = "bank",
335 .type = QEMU_OPT_STRING,
336 .help = "bank locator string",
338 .name = "manufacturer",
339 .type = QEMU_OPT_STRING,
340 .help = "manufacturer name",
342 .name = "serial",
343 .type = QEMU_OPT_STRING,
344 .help = "serial number",
346 .name = "asset",
347 .type = QEMU_OPT_STRING,
348 .help = "asset tag number",
350 .name = "part",
351 .type = QEMU_OPT_STRING,
352 .help = "part number",
354 .name = "speed",
355 .type = QEMU_OPT_NUMBER,
356 .help = "maximum capable speed",
358 { /* end of list */ }
361 static void smbios_register_config(void)
363 qemu_add_opts(&qemu_smbios_opts);
366 opts_init(smbios_register_config);
369 * The SMBIOS 2.1 "structure table length" field in the
370 * entry point uses a 16-bit integer, so we're limited
371 * in total table size
373 #define SMBIOS_21_MAX_TABLES_LEN 0xffff
375 static void smbios_validate_table(MachineState *ms)
377 uint32_t expect_t4_count = smbios_legacy ?
378 ms->smp.cpus : smbios_smp_sockets;
380 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
381 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
382 expect_t4_count, smbios_type4_count);
383 exit(1);
386 if (smbios_ep_type == SMBIOS_ENTRY_POINT_21 &&
387 smbios_tables_len > SMBIOS_21_MAX_TABLES_LEN) {
388 error_report("SMBIOS 2.1 table length %zu exceeds %d",
389 smbios_tables_len, SMBIOS_21_MAX_TABLES_LEN);
390 exit(1);
395 /* legacy setup functions for <= 2.0 machines */
396 static void smbios_add_field(int type, int offset, const void *data, size_t len)
398 struct smbios_field *field;
400 if (!smbios_entries) {
401 smbios_entries_len = sizeof(uint16_t);
402 smbios_entries = g_malloc0(smbios_entries_len);
404 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
405 sizeof(*field) + len);
406 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
407 field->header.type = SMBIOS_FIELD_ENTRY;
408 field->header.length = cpu_to_le16(sizeof(*field) + len);
410 field->type = type;
411 field->offset = cpu_to_le16(offset);
412 memcpy(field->data, data, len);
414 smbios_entries_len += sizeof(*field) + len;
415 (*(uint16_t *)smbios_entries) =
416 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
419 static void smbios_maybe_add_str(int type, int offset, const char *data)
421 if (data) {
422 smbios_add_field(type, offset, data, strlen(data) + 1);
426 static void smbios_build_type_0_fields(void)
428 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
429 type0.vendor);
430 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
431 type0.version);
432 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
433 bios_release_date_str),
434 type0.date);
435 if (type0.have_major_minor) {
436 smbios_add_field(0, offsetof(struct smbios_type_0,
437 system_bios_major_release),
438 &type0.major, 1);
439 smbios_add_field(0, offsetof(struct smbios_type_0,
440 system_bios_minor_release),
441 &type0.minor, 1);
445 static void smbios_build_type_1_fields(void)
447 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
448 type1.manufacturer);
449 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
450 type1.product);
451 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
452 type1.version);
453 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
454 type1.serial);
455 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
456 type1.sku);
457 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
458 type1.family);
459 if (qemu_uuid_set) {
460 /* We don't encode the UUID in the "wire format" here because this
461 * function is for legacy mode and needs to keep the guest ABI, and
462 * because we don't know what's the SMBIOS version advertised by the
463 * BIOS.
465 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
466 &qemu_uuid, 16);
470 uint8_t *smbios_get_table_legacy(MachineState *ms, size_t *length)
472 if (!smbios_legacy) {
473 *length = 0;
474 return NULL;
477 if (!smbios_immutable) {
478 smbios_build_type_0_fields();
479 smbios_build_type_1_fields();
480 smbios_validate_table(ms);
481 smbios_immutable = true;
483 *length = smbios_entries_len;
484 return smbios_entries;
486 /* end: legacy setup functions for <= 2.0 machines */
489 bool smbios_skip_table(uint8_t type, bool required_table)
491 if (test_bit(type, have_binfile_bitmap)) {
492 return true; /* user provided their own binary blob(s) */
494 if (test_bit(type, have_fields_bitmap)) {
495 return false; /* user provided fields via command line */
497 if (smbios_have_defaults && required_table) {
498 return false; /* we're building tables, and this one's required */
500 return true;
503 static void smbios_build_type_0_table(void)
505 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
507 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
508 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
510 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
512 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
514 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
516 t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
517 t->bios_characteristics_extension_bytes[0] = 0;
518 t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
519 if (type0.uefi) {
520 t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
523 if (type0.have_major_minor) {
524 t->system_bios_major_release = type0.major;
525 t->system_bios_minor_release = type0.minor;
526 } else {
527 t->system_bios_major_release = 0;
528 t->system_bios_minor_release = 0;
531 /* hardcoded in SeaBIOS */
532 t->embedded_controller_major_release = 0xFF;
533 t->embedded_controller_minor_release = 0xFF;
535 SMBIOS_BUILD_TABLE_POST;
538 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
539 * format specified by SMBIOS version 2.6.
541 static void smbios_encode_uuid(struct smbios_uuid *uuid, QemuUUID *in)
543 memcpy(uuid, in, 16);
544 if (smbios_uuid_encoded) {
545 uuid->time_low = bswap32(uuid->time_low);
546 uuid->time_mid = bswap16(uuid->time_mid);
547 uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
551 static void smbios_build_type_1_table(void)
553 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
555 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
556 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
557 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
558 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
559 if (qemu_uuid_set) {
560 smbios_encode_uuid(&t->uuid, &qemu_uuid);
561 } else {
562 memset(&t->uuid, 0, 16);
564 t->wake_up_type = 0x06; /* power switch */
565 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
566 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
568 SMBIOS_BUILD_TABLE_POST;
571 static void smbios_build_type_2_table(void)
573 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
575 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
576 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
577 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
578 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
579 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
580 t->feature_flags = 0x01; /* Motherboard */
581 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
582 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
583 t->board_type = 0x0A; /* Motherboard */
584 t->contained_element_count = 0;
586 SMBIOS_BUILD_TABLE_POST;
589 static void smbios_build_type_3_table(void)
591 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
593 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
594 t->type = 0x01; /* Other */
595 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
596 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
597 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
598 t->boot_up_state = 0x03; /* Safe */
599 t->power_supply_state = 0x03; /* Safe */
600 t->thermal_state = 0x03; /* Safe */
601 t->security_status = 0x02; /* Unknown */
602 t->oem_defined = cpu_to_le32(0);
603 t->height = 0;
604 t->number_of_power_cords = 0;
605 t->contained_element_count = 0;
606 t->contained_element_record_length = 0;
607 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
609 SMBIOS_BUILD_TABLE_POST;
612 static void smbios_build_type_4_table(MachineState *ms, unsigned instance)
614 char sock_str[128];
616 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
618 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
619 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
620 t->processor_type = 0x03; /* CPU */
621 t->processor_family = 0x01; /* Other */
622 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
623 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
624 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
625 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
626 t->voltage = 0;
627 t->external_clock = cpu_to_le16(0); /* Unknown */
628 t->max_speed = cpu_to_le16(type4.max_speed);
629 t->current_speed = cpu_to_le16(type4.current_speed);
630 t->status = 0x41; /* Socket populated, CPU enabled */
631 t->processor_upgrade = 0x01; /* Other */
632 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
633 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
634 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
635 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
636 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
637 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
638 t->core_count = t->core_enabled = ms->smp.cores;
639 t->thread_count = ms->smp.threads;
640 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
641 t->processor_family2 = cpu_to_le16(0x01); /* Other */
643 SMBIOS_BUILD_TABLE_POST;
644 smbios_type4_count++;
647 static void smbios_build_type_11_table(void)
649 char count_str[128];
650 size_t i;
652 if (type11.nvalues == 0) {
653 return;
656 SMBIOS_BUILD_TABLE_PRE(11, 0xe00, true); /* required */
658 snprintf(count_str, sizeof(count_str), "%zu", type11.nvalues);
659 t->count = type11.nvalues;
661 for (i = 0; i < type11.nvalues; i++) {
662 SMBIOS_TABLE_SET_STR_LIST(11, type11.values[i]);
663 g_free(type11.values[i]);
664 type11.values[i] = NULL;
667 SMBIOS_BUILD_TABLE_POST;
670 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
672 static void smbios_build_type_16_table(unsigned dimm_cnt)
674 uint64_t size_kb;
676 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
678 t->location = 0x01; /* Other */
679 t->use = 0x03; /* System memory */
680 t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
681 size_kb = QEMU_ALIGN_UP(current_machine->ram_size, KiB) / KiB;
682 if (size_kb < MAX_T16_STD_SZ) {
683 t->maximum_capacity = cpu_to_le32(size_kb);
684 t->extended_maximum_capacity = cpu_to_le64(0);
685 } else {
686 t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
687 t->extended_maximum_capacity = cpu_to_le64(current_machine->ram_size);
689 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
690 t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
692 SMBIOS_BUILD_TABLE_POST;
695 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
696 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
698 static void smbios_build_type_17_table(unsigned instance, uint64_t size)
700 char loc_str[128];
701 uint64_t size_mb;
703 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
705 t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
706 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
707 t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
708 t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
709 size_mb = QEMU_ALIGN_UP(size, MiB) / MiB;
710 if (size_mb < MAX_T17_STD_SZ) {
711 t->size = cpu_to_le16(size_mb);
712 t->extended_size = cpu_to_le32(0);
713 } else {
714 assert(size_mb < MAX_T17_EXT_SZ);
715 t->size = cpu_to_le16(MAX_T17_STD_SZ);
716 t->extended_size = cpu_to_le32(size_mb);
718 t->form_factor = 0x09; /* DIMM */
719 t->device_set = 0; /* Not in a set */
720 snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
721 SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
722 SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
723 t->memory_type = 0x07; /* RAM */
724 t->type_detail = cpu_to_le16(0x02); /* Other */
725 t->speed = cpu_to_le16(type17.speed);
726 SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
727 SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
728 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
729 SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
730 t->attributes = 0; /* Unknown */
731 t->configured_clock_speed = t->speed; /* reuse value for max speed */
732 t->minimum_voltage = cpu_to_le16(0); /* Unknown */
733 t->maximum_voltage = cpu_to_le16(0); /* Unknown */
734 t->configured_voltage = cpu_to_le16(0); /* Unknown */
736 SMBIOS_BUILD_TABLE_POST;
739 static void smbios_build_type_19_table(unsigned instance,
740 uint64_t start, uint64_t size)
742 uint64_t end, start_kb, end_kb;
744 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
746 end = start + size - 1;
747 assert(end > start);
748 start_kb = start / KiB;
749 end_kb = end / KiB;
750 if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
751 t->starting_address = cpu_to_le32(start_kb);
752 t->ending_address = cpu_to_le32(end_kb);
753 t->extended_starting_address =
754 t->extended_ending_address = cpu_to_le64(0);
755 } else {
756 t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
757 t->extended_starting_address = cpu_to_le64(start);
758 t->extended_ending_address = cpu_to_le64(end);
760 t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
761 t->partition_width = 1; /* One device per row */
763 SMBIOS_BUILD_TABLE_POST;
766 static void smbios_build_type_32_table(void)
768 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
770 memset(t->reserved, 0, 6);
771 t->boot_status = 0; /* No errors detected */
773 SMBIOS_BUILD_TABLE_POST;
776 static void smbios_build_type_127_table(void)
778 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
779 SMBIOS_BUILD_TABLE_POST;
782 void smbios_set_cpuid(uint32_t version, uint32_t features)
784 smbios_cpuid_version = version;
785 smbios_cpuid_features = features;
788 #define SMBIOS_SET_DEFAULT(field, value) \
789 if (!field) { \
790 field = value; \
793 void smbios_set_defaults(const char *manufacturer, const char *product,
794 const char *version, bool legacy_mode,
795 bool uuid_encoded, SmbiosEntryPointType ep_type)
797 smbios_have_defaults = true;
798 smbios_legacy = legacy_mode;
799 smbios_uuid_encoded = uuid_encoded;
800 smbios_ep_type = ep_type;
802 /* drop unwanted version of command-line file blob(s) */
803 if (smbios_legacy) {
804 g_free(smbios_tables);
805 /* in legacy mode, also complain if fields were given for types > 1 */
806 if (find_next_bit(have_fields_bitmap,
807 SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
808 error_report("can't process fields for smbios "
809 "types > 1 on machine versions < 2.1!");
810 exit(1);
812 } else {
813 g_free(smbios_entries);
816 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
817 SMBIOS_SET_DEFAULT(type1.product, product);
818 SMBIOS_SET_DEFAULT(type1.version, version);
819 SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
820 SMBIOS_SET_DEFAULT(type2.product, product);
821 SMBIOS_SET_DEFAULT(type2.version, version);
822 SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
823 SMBIOS_SET_DEFAULT(type3.version, version);
824 SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
825 SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
826 SMBIOS_SET_DEFAULT(type4.version, version);
827 SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
828 SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
831 static void smbios_entry_point_setup(void)
833 switch (smbios_ep_type) {
834 case SMBIOS_ENTRY_POINT_21:
835 memcpy(ep.ep21.anchor_string, "_SM_", 4);
836 memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
837 ep.ep21.length = sizeof(struct smbios_21_entry_point);
838 ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
839 memset(ep.ep21.formatted_area, 0, 5);
841 /* compliant with smbios spec v2.8 */
842 ep.ep21.smbios_major_version = 2;
843 ep.ep21.smbios_minor_version = 8;
844 ep.ep21.smbios_bcd_revision = 0x28;
846 /* set during table construction, but BIOS may override: */
847 ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
848 ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
849 ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
851 /* BIOS must recalculate */
852 ep.ep21.checksum = 0;
853 ep.ep21.intermediate_checksum = 0;
854 ep.ep21.structure_table_address = cpu_to_le32(0);
856 break;
857 case SMBIOS_ENTRY_POINT_30:
858 memcpy(ep.ep30.anchor_string, "_SM3_", 5);
859 ep.ep30.length = sizeof(struct smbios_30_entry_point);
860 ep.ep30.entry_point_revision = 1;
861 ep.ep30.reserved = 0;
863 /* compliant with smbios spec 3.0 */
864 ep.ep30.smbios_major_version = 3;
865 ep.ep30.smbios_minor_version = 0;
866 ep.ep30.smbios_doc_rev = 0;
868 /* set during table construct, but BIOS might override */
869 ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
871 /* BIOS must recalculate */
872 ep.ep30.checksum = 0;
873 ep.ep30.structure_table_address = cpu_to_le64(0);
875 break;
876 default:
877 abort();
878 break;
882 void smbios_get_tables(MachineState *ms,
883 const struct smbios_phys_mem_area *mem_array,
884 const unsigned int mem_array_size,
885 uint8_t **tables, size_t *tables_len,
886 uint8_t **anchor, size_t *anchor_len)
888 unsigned i, dimm_cnt;
890 if (smbios_legacy) {
891 *tables = *anchor = NULL;
892 *tables_len = *anchor_len = 0;
893 return;
896 if (!smbios_immutable) {
897 smbios_build_type_0_table();
898 smbios_build_type_1_table();
899 smbios_build_type_2_table();
900 smbios_build_type_3_table();
902 smbios_smp_sockets = DIV_ROUND_UP(ms->smp.cpus,
903 ms->smp.cores * ms->smp.threads);
904 assert(smbios_smp_sockets >= 1);
906 for (i = 0; i < smbios_smp_sockets; i++) {
907 smbios_build_type_4_table(ms, i);
910 smbios_build_type_11_table();
912 #define MAX_DIMM_SZ (16 * GiB)
913 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
914 : ((current_machine->ram_size - 1) % MAX_DIMM_SZ) + 1)
916 dimm_cnt = QEMU_ALIGN_UP(current_machine->ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
918 smbios_build_type_16_table(dimm_cnt);
920 for (i = 0; i < dimm_cnt; i++) {
921 smbios_build_type_17_table(i, GET_DIMM_SZ);
924 for (i = 0; i < mem_array_size; i++) {
925 smbios_build_type_19_table(i, mem_array[i].address,
926 mem_array[i].length);
929 smbios_build_type_32_table();
930 smbios_build_type_38_table();
931 smbios_build_type_127_table();
933 smbios_validate_table(ms);
934 smbios_entry_point_setup();
935 smbios_immutable = true;
938 /* return tables blob and entry point (anchor), and their sizes */
939 *tables = smbios_tables;
940 *tables_len = smbios_tables_len;
941 *anchor = (uint8_t *)&ep;
943 /* calculate length based on anchor string */
944 if (!strncmp((char *)&ep, "_SM_", 4)) {
945 *anchor_len = sizeof(struct smbios_21_entry_point);
946 } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
947 *anchor_len = sizeof(struct smbios_30_entry_point);
948 } else {
949 abort();
953 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
955 const char *val = qemu_opt_get(opts, name);
957 if (val) {
958 *dest = val;
963 struct opt_list {
964 size_t *ndest;
965 char ***dest;
968 static int save_opt_one(void *opaque,
969 const char *name, const char *value,
970 Error **errp)
972 struct opt_list *opt = opaque;
974 if (g_str_equal(name, "path")) {
975 g_autoptr(GByteArray) data = g_byte_array_new();
976 g_autofree char *buf = g_new(char, 4096);
977 ssize_t ret;
978 int fd = qemu_open(value, O_RDONLY, errp);
979 if (fd < 0) {
980 return -1;
983 while (1) {
984 ret = read(fd, buf, 4096);
985 if (ret == 0) {
986 break;
988 if (ret < 0) {
989 error_setg(errp, "Unable to read from %s: %s",
990 value, strerror(errno));
991 qemu_close(fd);
992 return -1;
994 if (memchr(buf, '\0', ret)) {
995 error_setg(errp, "NUL in OEM strings value in %s", value);
996 qemu_close(fd);
997 return -1;
999 g_byte_array_append(data, (guint8 *)buf, ret);
1002 qemu_close(fd);
1004 *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1);
1005 (*opt->dest)[*opt->ndest] = (char *)g_byte_array_free(data, FALSE);
1006 (*opt->ndest)++;
1007 data = NULL;
1008 } else if (g_str_equal(name, "value")) {
1009 *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1);
1010 (*opt->dest)[*opt->ndest] = g_strdup(value);
1011 (*opt->ndest)++;
1012 } else if (!g_str_equal(name, "type")) {
1013 error_setg(errp, "Unexpected option %s", name);
1014 return -1;
1017 return 0;
1020 static bool save_opt_list(size_t *ndest, char ***dest, QemuOpts *opts,
1021 Error **errp)
1023 struct opt_list opt = {
1024 ndest, dest,
1026 if (!qemu_opt_foreach(opts, save_opt_one, &opt, errp)) {
1027 return false;
1029 return true;
1032 void smbios_entry_add(QemuOpts *opts, Error **errp)
1034 const char *val;
1036 assert(!smbios_immutable);
1038 val = qemu_opt_get(opts, "file");
1039 if (val) {
1040 struct smbios_structure_header *header;
1041 int size;
1042 struct smbios_table *table; /* legacy mode only */
1044 if (!qemu_opts_validate(opts, qemu_smbios_file_opts, errp)) {
1045 return;
1048 size = get_image_size(val);
1049 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
1050 error_setg(errp, "Cannot read SMBIOS file %s", val);
1051 return;
1055 * NOTE: standard double '\0' terminator expected, per smbios spec.
1056 * (except in legacy mode, where the second '\0' is implicit and
1057 * will be inserted by the BIOS).
1059 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
1060 header = (struct smbios_structure_header *)(smbios_tables +
1061 smbios_tables_len);
1063 if (load_image_size(val, (uint8_t *)header, size) != size) {
1064 error_setg(errp, "Failed to load SMBIOS file %s", val);
1065 return;
1068 if (test_bit(header->type, have_fields_bitmap)) {
1069 error_setg(errp,
1070 "can't load type %d struct, fields already specified!",
1071 header->type);
1072 return;
1074 set_bit(header->type, have_binfile_bitmap);
1076 if (header->type == 4) {
1077 smbios_type4_count++;
1080 smbios_tables_len += size;
1081 if (size > smbios_table_max) {
1082 smbios_table_max = size;
1084 smbios_table_cnt++;
1086 /* add a copy of the newly loaded blob to legacy smbios_entries */
1087 /* NOTE: This code runs before smbios_set_defaults(), so we don't
1088 * yet know which mode (legacy vs. aggregate-table) will be
1089 * required. We therefore add the binary blob to both legacy
1090 * (smbios_entries) and aggregate (smbios_tables) tables, and
1091 * delete the one we don't need from smbios_set_defaults(),
1092 * once we know which machine version has been requested.
1094 if (!smbios_entries) {
1095 smbios_entries_len = sizeof(uint16_t);
1096 smbios_entries = g_malloc0(smbios_entries_len);
1098 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
1099 size + sizeof(*table));
1100 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
1101 table->header.type = SMBIOS_TABLE_ENTRY;
1102 table->header.length = cpu_to_le16(sizeof(*table) + size);
1103 memcpy(table->data, header, size);
1104 smbios_entries_len += sizeof(*table) + size;
1105 (*(uint16_t *)smbios_entries) =
1106 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
1107 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1109 return;
1112 val = qemu_opt_get(opts, "type");
1113 if (val) {
1114 unsigned long type = strtoul(val, NULL, 0);
1116 if (type > SMBIOS_MAX_TYPE) {
1117 error_setg(errp, "out of range!");
1118 return;
1121 if (test_bit(type, have_binfile_bitmap)) {
1122 error_setg(errp, "can't add fields, binary file already loaded!");
1123 return;
1125 set_bit(type, have_fields_bitmap);
1127 switch (type) {
1128 case 0:
1129 if (!qemu_opts_validate(opts, qemu_smbios_type0_opts, errp)) {
1130 return;
1132 save_opt(&type0.vendor, opts, "vendor");
1133 save_opt(&type0.version, opts, "version");
1134 save_opt(&type0.date, opts, "date");
1135 type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1137 val = qemu_opt_get(opts, "release");
1138 if (val) {
1139 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1140 error_setg(errp, "Invalid release");
1141 return;
1143 type0.have_major_minor = true;
1145 return;
1146 case 1:
1147 if (!qemu_opts_validate(opts, qemu_smbios_type1_opts, errp)) {
1148 return;
1150 save_opt(&type1.manufacturer, opts, "manufacturer");
1151 save_opt(&type1.product, opts, "product");
1152 save_opt(&type1.version, opts, "version");
1153 save_opt(&type1.serial, opts, "serial");
1154 save_opt(&type1.sku, opts, "sku");
1155 save_opt(&type1.family, opts, "family");
1157 val = qemu_opt_get(opts, "uuid");
1158 if (val) {
1159 if (qemu_uuid_parse(val, &qemu_uuid) != 0) {
1160 error_setg(errp, "Invalid UUID");
1161 return;
1163 qemu_uuid_set = true;
1165 return;
1166 case 2:
1167 if (!qemu_opts_validate(opts, qemu_smbios_type2_opts, errp)) {
1168 return;
1170 save_opt(&type2.manufacturer, opts, "manufacturer");
1171 save_opt(&type2.product, opts, "product");
1172 save_opt(&type2.version, opts, "version");
1173 save_opt(&type2.serial, opts, "serial");
1174 save_opt(&type2.asset, opts, "asset");
1175 save_opt(&type2.location, opts, "location");
1176 return;
1177 case 3:
1178 if (!qemu_opts_validate(opts, qemu_smbios_type3_opts, errp)) {
1179 return;
1181 save_opt(&type3.manufacturer, opts, "manufacturer");
1182 save_opt(&type3.version, opts, "version");
1183 save_opt(&type3.serial, opts, "serial");
1184 save_opt(&type3.asset, opts, "asset");
1185 save_opt(&type3.sku, opts, "sku");
1186 return;
1187 case 4:
1188 if (!qemu_opts_validate(opts, qemu_smbios_type4_opts, errp)) {
1189 return;
1191 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1192 save_opt(&type4.manufacturer, opts, "manufacturer");
1193 save_opt(&type4.version, opts, "version");
1194 save_opt(&type4.serial, opts, "serial");
1195 save_opt(&type4.asset, opts, "asset");
1196 save_opt(&type4.part, opts, "part");
1197 type4.max_speed = qemu_opt_get_number(opts, "max-speed",
1198 DEFAULT_CPU_SPEED);
1199 type4.current_speed = qemu_opt_get_number(opts, "current-speed",
1200 DEFAULT_CPU_SPEED);
1201 if (type4.max_speed > UINT16_MAX ||
1202 type4.current_speed > UINT16_MAX) {
1203 error_setg(errp, "SMBIOS CPU speed is too large (> %d)",
1204 UINT16_MAX);
1206 return;
1207 case 11:
1208 if (!qemu_opts_validate(opts, qemu_smbios_type11_opts, errp)) {
1209 return;
1211 if (!save_opt_list(&type11.nvalues, &type11.values, opts, errp)) {
1212 return;
1214 return;
1215 case 17:
1216 if (!qemu_opts_validate(opts, qemu_smbios_type17_opts, errp)) {
1217 return;
1219 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1220 save_opt(&type17.bank, opts, "bank");
1221 save_opt(&type17.manufacturer, opts, "manufacturer");
1222 save_opt(&type17.serial, opts, "serial");
1223 save_opt(&type17.asset, opts, "asset");
1224 save_opt(&type17.part, opts, "part");
1225 type17.speed = qemu_opt_get_number(opts, "speed", 0);
1226 return;
1227 default:
1228 error_setg(errp,
1229 "Don't know how to build fields for SMBIOS type %ld",
1230 type);
1231 return;
1235 error_setg(errp, "Must specify type= or file=");