tcg: Search includes in the parent source directory
[qemu/ar7.git] / hw / smbios / smbios.c
blob11d476c4a2cbdabc546c02b4f076c81dba72c120
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 "sysemu/cpus.h"
28 #include "hw/firmware/smbios.h"
29 #include "hw/loader.h"
30 #include "hw/boards.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_21;
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;
96 static struct {
97 const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
98 } type4;
100 static struct {
101 size_t nvalues;
102 const char **values;
103 } type11;
105 static struct {
106 const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
107 uint16_t speed;
108 } type17;
110 static QemuOptsList qemu_smbios_opts = {
111 .name = "smbios",
112 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
113 .desc = {
115 * no elements => accept any params
116 * validation will happen later
118 { /* end of list */ }
122 static const QemuOptDesc qemu_smbios_file_opts[] = {
124 .name = "file",
125 .type = QEMU_OPT_STRING,
126 .help = "binary file containing an SMBIOS element",
128 { /* end of list */ }
131 static const QemuOptDesc qemu_smbios_type0_opts[] = {
133 .name = "type",
134 .type = QEMU_OPT_NUMBER,
135 .help = "SMBIOS element type",
137 .name = "vendor",
138 .type = QEMU_OPT_STRING,
139 .help = "vendor name",
141 .name = "version",
142 .type = QEMU_OPT_STRING,
143 .help = "version number",
145 .name = "date",
146 .type = QEMU_OPT_STRING,
147 .help = "release date",
149 .name = "release",
150 .type = QEMU_OPT_STRING,
151 .help = "revision number",
153 .name = "uefi",
154 .type = QEMU_OPT_BOOL,
155 .help = "uefi support",
157 { /* end of list */ }
160 static const QemuOptDesc qemu_smbios_type1_opts[] = {
162 .name = "type",
163 .type = QEMU_OPT_NUMBER,
164 .help = "SMBIOS element type",
166 .name = "manufacturer",
167 .type = QEMU_OPT_STRING,
168 .help = "manufacturer name",
170 .name = "product",
171 .type = QEMU_OPT_STRING,
172 .help = "product name",
174 .name = "version",
175 .type = QEMU_OPT_STRING,
176 .help = "version number",
178 .name = "serial",
179 .type = QEMU_OPT_STRING,
180 .help = "serial number",
182 .name = "uuid",
183 .type = QEMU_OPT_STRING,
184 .help = "UUID",
186 .name = "sku",
187 .type = QEMU_OPT_STRING,
188 .help = "SKU number",
190 .name = "family",
191 .type = QEMU_OPT_STRING,
192 .help = "family name",
194 { /* end of list */ }
197 static const QemuOptDesc qemu_smbios_type2_opts[] = {
199 .name = "type",
200 .type = QEMU_OPT_NUMBER,
201 .help = "SMBIOS element type",
203 .name = "manufacturer",
204 .type = QEMU_OPT_STRING,
205 .help = "manufacturer name",
207 .name = "product",
208 .type = QEMU_OPT_STRING,
209 .help = "product name",
211 .name = "version",
212 .type = QEMU_OPT_STRING,
213 .help = "version number",
215 .name = "serial",
216 .type = QEMU_OPT_STRING,
217 .help = "serial number",
219 .name = "asset",
220 .type = QEMU_OPT_STRING,
221 .help = "asset tag number",
223 .name = "location",
224 .type = QEMU_OPT_STRING,
225 .help = "location in chassis",
227 { /* end of list */ }
230 static const QemuOptDesc qemu_smbios_type3_opts[] = {
232 .name = "type",
233 .type = QEMU_OPT_NUMBER,
234 .help = "SMBIOS element type",
236 .name = "manufacturer",
237 .type = QEMU_OPT_STRING,
238 .help = "manufacturer name",
240 .name = "version",
241 .type = QEMU_OPT_STRING,
242 .help = "version number",
244 .name = "serial",
245 .type = QEMU_OPT_STRING,
246 .help = "serial number",
248 .name = "asset",
249 .type = QEMU_OPT_STRING,
250 .help = "asset tag number",
252 .name = "sku",
253 .type = QEMU_OPT_STRING,
254 .help = "SKU number",
256 { /* end of list */ }
259 static const QemuOptDesc qemu_smbios_type4_opts[] = {
261 .name = "type",
262 .type = QEMU_OPT_NUMBER,
263 .help = "SMBIOS element type",
265 .name = "sock_pfx",
266 .type = QEMU_OPT_STRING,
267 .help = "socket designation string prefix",
269 .name = "manufacturer",
270 .type = QEMU_OPT_STRING,
271 .help = "manufacturer name",
273 .name = "version",
274 .type = QEMU_OPT_STRING,
275 .help = "version number",
277 .name = "serial",
278 .type = QEMU_OPT_STRING,
279 .help = "serial number",
281 .name = "asset",
282 .type = QEMU_OPT_STRING,
283 .help = "asset tag number",
285 .name = "part",
286 .type = QEMU_OPT_STRING,
287 .help = "part number",
289 { /* end of list */ }
292 static const QemuOptDesc qemu_smbios_type11_opts[] = {
294 .name = "value",
295 .type = QEMU_OPT_STRING,
296 .help = "OEM string data",
300 static const QemuOptDesc qemu_smbios_type17_opts[] = {
302 .name = "type",
303 .type = QEMU_OPT_NUMBER,
304 .help = "SMBIOS element type",
306 .name = "loc_pfx",
307 .type = QEMU_OPT_STRING,
308 .help = "device locator string prefix",
310 .name = "bank",
311 .type = QEMU_OPT_STRING,
312 .help = "bank locator string",
314 .name = "manufacturer",
315 .type = QEMU_OPT_STRING,
316 .help = "manufacturer name",
318 .name = "serial",
319 .type = QEMU_OPT_STRING,
320 .help = "serial number",
322 .name = "asset",
323 .type = QEMU_OPT_STRING,
324 .help = "asset tag number",
326 .name = "part",
327 .type = QEMU_OPT_STRING,
328 .help = "part number",
330 .name = "speed",
331 .type = QEMU_OPT_NUMBER,
332 .help = "maximum capable speed",
334 { /* end of list */ }
337 static void smbios_register_config(void)
339 qemu_add_opts(&qemu_smbios_opts);
342 opts_init(smbios_register_config);
344 static void smbios_validate_table(MachineState *ms)
346 uint32_t expect_t4_count = smbios_legacy ?
347 ms->smp.cpus : smbios_smp_sockets;
349 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
350 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
351 expect_t4_count, smbios_type4_count);
352 exit(1);
357 /* legacy setup functions for <= 2.0 machines */
358 static void smbios_add_field(int type, int offset, const void *data, size_t len)
360 struct smbios_field *field;
362 if (!smbios_entries) {
363 smbios_entries_len = sizeof(uint16_t);
364 smbios_entries = g_malloc0(smbios_entries_len);
366 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
367 sizeof(*field) + len);
368 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
369 field->header.type = SMBIOS_FIELD_ENTRY;
370 field->header.length = cpu_to_le16(sizeof(*field) + len);
372 field->type = type;
373 field->offset = cpu_to_le16(offset);
374 memcpy(field->data, data, len);
376 smbios_entries_len += sizeof(*field) + len;
377 (*(uint16_t *)smbios_entries) =
378 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
381 static void smbios_maybe_add_str(int type, int offset, const char *data)
383 if (data) {
384 smbios_add_field(type, offset, data, strlen(data) + 1);
388 static void smbios_build_type_0_fields(void)
390 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
391 type0.vendor);
392 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
393 type0.version);
394 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
395 bios_release_date_str),
396 type0.date);
397 if (type0.have_major_minor) {
398 smbios_add_field(0, offsetof(struct smbios_type_0,
399 system_bios_major_release),
400 &type0.major, 1);
401 smbios_add_field(0, offsetof(struct smbios_type_0,
402 system_bios_minor_release),
403 &type0.minor, 1);
407 static void smbios_build_type_1_fields(void)
409 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
410 type1.manufacturer);
411 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
412 type1.product);
413 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
414 type1.version);
415 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
416 type1.serial);
417 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
418 type1.sku);
419 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
420 type1.family);
421 if (qemu_uuid_set) {
422 /* We don't encode the UUID in the "wire format" here because this
423 * function is for legacy mode and needs to keep the guest ABI, and
424 * because we don't know what's the SMBIOS version advertised by the
425 * BIOS.
427 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
428 &qemu_uuid, 16);
432 uint8_t *smbios_get_table_legacy(MachineState *ms, size_t *length)
434 if (!smbios_legacy) {
435 *length = 0;
436 return NULL;
439 if (!smbios_immutable) {
440 smbios_build_type_0_fields();
441 smbios_build_type_1_fields();
442 smbios_validate_table(ms);
443 smbios_immutable = true;
445 *length = smbios_entries_len;
446 return smbios_entries;
448 /* end: legacy setup functions for <= 2.0 machines */
451 bool smbios_skip_table(uint8_t type, bool required_table)
453 if (test_bit(type, have_binfile_bitmap)) {
454 return true; /* user provided their own binary blob(s) */
456 if (test_bit(type, have_fields_bitmap)) {
457 return false; /* user provided fields via command line */
459 if (smbios_have_defaults && required_table) {
460 return false; /* we're building tables, and this one's required */
462 return true;
465 static void smbios_build_type_0_table(void)
467 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
469 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
470 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
472 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
474 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
476 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
478 t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
479 t->bios_characteristics_extension_bytes[0] = 0;
480 t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
481 if (type0.uefi) {
482 t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
485 if (type0.have_major_minor) {
486 t->system_bios_major_release = type0.major;
487 t->system_bios_minor_release = type0.minor;
488 } else {
489 t->system_bios_major_release = 0;
490 t->system_bios_minor_release = 0;
493 /* hardcoded in SeaBIOS */
494 t->embedded_controller_major_release = 0xFF;
495 t->embedded_controller_minor_release = 0xFF;
497 SMBIOS_BUILD_TABLE_POST;
500 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
501 * format specified by SMBIOS version 2.6.
503 static void smbios_encode_uuid(struct smbios_uuid *uuid, QemuUUID *in)
505 memcpy(uuid, in, 16);
506 if (smbios_uuid_encoded) {
507 uuid->time_low = bswap32(uuid->time_low);
508 uuid->time_mid = bswap16(uuid->time_mid);
509 uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
513 static void smbios_build_type_1_table(void)
515 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
517 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
518 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
519 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
520 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
521 if (qemu_uuid_set) {
522 smbios_encode_uuid(&t->uuid, &qemu_uuid);
523 } else {
524 memset(&t->uuid, 0, 16);
526 t->wake_up_type = 0x06; /* power switch */
527 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
528 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
530 SMBIOS_BUILD_TABLE_POST;
533 static void smbios_build_type_2_table(void)
535 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
537 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
538 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
539 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
540 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
541 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
542 t->feature_flags = 0x01; /* Motherboard */
543 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
544 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
545 t->board_type = 0x0A; /* Motherboard */
546 t->contained_element_count = 0;
548 SMBIOS_BUILD_TABLE_POST;
551 static void smbios_build_type_3_table(void)
553 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
555 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
556 t->type = 0x01; /* Other */
557 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
558 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
559 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
560 t->boot_up_state = 0x03; /* Safe */
561 t->power_supply_state = 0x03; /* Safe */
562 t->thermal_state = 0x03; /* Safe */
563 t->security_status = 0x02; /* Unknown */
564 t->oem_defined = cpu_to_le32(0);
565 t->height = 0;
566 t->number_of_power_cords = 0;
567 t->contained_element_count = 0;
568 t->contained_element_record_length = 0;
569 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
571 SMBIOS_BUILD_TABLE_POST;
574 static void smbios_build_type_4_table(MachineState *ms, unsigned instance)
576 char sock_str[128];
578 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
580 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
581 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
582 t->processor_type = 0x03; /* CPU */
583 t->processor_family = 0x01; /* Other */
584 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
585 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
586 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
587 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
588 t->voltage = 0;
589 t->external_clock = cpu_to_le16(0); /* Unknown */
590 /* SVVP requires max_speed and current_speed to not be unknown. */
591 t->max_speed = cpu_to_le16(2000); /* 2000 MHz */
592 t->current_speed = cpu_to_le16(2000); /* 2000 MHz */
593 t->status = 0x41; /* Socket populated, CPU enabled */
594 t->processor_upgrade = 0x01; /* Other */
595 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
596 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
597 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
598 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
599 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
600 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
601 t->core_count = t->core_enabled = ms->smp.cores;
602 t->thread_count = ms->smp.threads;
603 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
604 t->processor_family2 = cpu_to_le16(0x01); /* Other */
606 SMBIOS_BUILD_TABLE_POST;
607 smbios_type4_count++;
610 static void smbios_build_type_11_table(void)
612 char count_str[128];
613 size_t i;
615 if (type11.nvalues == 0) {
616 return;
619 SMBIOS_BUILD_TABLE_PRE(11, 0xe00, true); /* required */
621 snprintf(count_str, sizeof(count_str), "%zu", type11.nvalues);
622 t->count = type11.nvalues;
624 for (i = 0; i < type11.nvalues; i++) {
625 SMBIOS_TABLE_SET_STR_LIST(11, type11.values[i]);
628 SMBIOS_BUILD_TABLE_POST;
631 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
633 static void smbios_build_type_16_table(unsigned dimm_cnt)
635 uint64_t size_kb;
637 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
639 t->location = 0x01; /* Other */
640 t->use = 0x03; /* System memory */
641 t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
642 size_kb = QEMU_ALIGN_UP(ram_size, KiB) / KiB;
643 if (size_kb < MAX_T16_STD_SZ) {
644 t->maximum_capacity = cpu_to_le32(size_kb);
645 t->extended_maximum_capacity = cpu_to_le64(0);
646 } else {
647 t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
648 t->extended_maximum_capacity = cpu_to_le64(ram_size);
650 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
651 t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
653 SMBIOS_BUILD_TABLE_POST;
656 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
657 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
659 static void smbios_build_type_17_table(unsigned instance, uint64_t size)
661 char loc_str[128];
662 uint64_t size_mb;
664 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
666 t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
667 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
668 t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
669 t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
670 size_mb = QEMU_ALIGN_UP(size, MiB) / MiB;
671 if (size_mb < MAX_T17_STD_SZ) {
672 t->size = cpu_to_le16(size_mb);
673 t->extended_size = cpu_to_le32(0);
674 } else {
675 assert(size_mb < MAX_T17_EXT_SZ);
676 t->size = cpu_to_le16(MAX_T17_STD_SZ);
677 t->extended_size = cpu_to_le32(size_mb);
679 t->form_factor = 0x09; /* DIMM */
680 t->device_set = 0; /* Not in a set */
681 snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
682 SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
683 SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
684 t->memory_type = 0x07; /* RAM */
685 t->type_detail = cpu_to_le16(0x02); /* Other */
686 t->speed = cpu_to_le16(type17.speed);
687 SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
688 SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
689 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
690 SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
691 t->attributes = 0; /* Unknown */
692 t->configured_clock_speed = t->speed; /* reuse value for max speed */
693 t->minimum_voltage = cpu_to_le16(0); /* Unknown */
694 t->maximum_voltage = cpu_to_le16(0); /* Unknown */
695 t->configured_voltage = cpu_to_le16(0); /* Unknown */
697 SMBIOS_BUILD_TABLE_POST;
700 static void smbios_build_type_19_table(unsigned instance,
701 uint64_t start, uint64_t size)
703 uint64_t end, start_kb, end_kb;
705 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
707 end = start + size - 1;
708 assert(end > start);
709 start_kb = start / KiB;
710 end_kb = end / KiB;
711 if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
712 t->starting_address = cpu_to_le32(start_kb);
713 t->ending_address = cpu_to_le32(end_kb);
714 t->extended_starting_address =
715 t->extended_ending_address = cpu_to_le64(0);
716 } else {
717 t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
718 t->extended_starting_address = cpu_to_le64(start);
719 t->extended_ending_address = cpu_to_le64(end);
721 t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
722 t->partition_width = 1; /* One device per row */
724 SMBIOS_BUILD_TABLE_POST;
727 static void smbios_build_type_32_table(void)
729 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
731 memset(t->reserved, 0, 6);
732 t->boot_status = 0; /* No errors detected */
734 SMBIOS_BUILD_TABLE_POST;
737 static void smbios_build_type_127_table(void)
739 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
740 SMBIOS_BUILD_TABLE_POST;
743 void smbios_set_cpuid(uint32_t version, uint32_t features)
745 smbios_cpuid_version = version;
746 smbios_cpuid_features = features;
749 #define SMBIOS_SET_DEFAULT(field, value) \
750 if (!field) { \
751 field = value; \
754 void smbios_set_defaults(const char *manufacturer, const char *product,
755 const char *version, bool legacy_mode,
756 bool uuid_encoded, SmbiosEntryPointType ep_type)
758 smbios_have_defaults = true;
759 smbios_legacy = legacy_mode;
760 smbios_uuid_encoded = uuid_encoded;
761 smbios_ep_type = ep_type;
763 /* drop unwanted version of command-line file blob(s) */
764 if (smbios_legacy) {
765 g_free(smbios_tables);
766 /* in legacy mode, also complain if fields were given for types > 1 */
767 if (find_next_bit(have_fields_bitmap,
768 SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
769 error_report("can't process fields for smbios "
770 "types > 1 on machine versions < 2.1!");
771 exit(1);
773 } else {
774 g_free(smbios_entries);
777 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
778 SMBIOS_SET_DEFAULT(type1.product, product);
779 SMBIOS_SET_DEFAULT(type1.version, version);
780 SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
781 SMBIOS_SET_DEFAULT(type2.product, product);
782 SMBIOS_SET_DEFAULT(type2.version, version);
783 SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
784 SMBIOS_SET_DEFAULT(type3.version, version);
785 SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
786 SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
787 SMBIOS_SET_DEFAULT(type4.version, version);
788 SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
789 SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
792 static void smbios_entry_point_setup(void)
794 switch (smbios_ep_type) {
795 case SMBIOS_ENTRY_POINT_21:
796 memcpy(ep.ep21.anchor_string, "_SM_", 4);
797 memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
798 ep.ep21.length = sizeof(struct smbios_21_entry_point);
799 ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
800 memset(ep.ep21.formatted_area, 0, 5);
802 /* compliant with smbios spec v2.8 */
803 ep.ep21.smbios_major_version = 2;
804 ep.ep21.smbios_minor_version = 8;
805 ep.ep21.smbios_bcd_revision = 0x28;
807 /* set during table construction, but BIOS may override: */
808 ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
809 ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
810 ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
812 /* BIOS must recalculate */
813 ep.ep21.checksum = 0;
814 ep.ep21.intermediate_checksum = 0;
815 ep.ep21.structure_table_address = cpu_to_le32(0);
817 break;
818 case SMBIOS_ENTRY_POINT_30:
819 memcpy(ep.ep30.anchor_string, "_SM3_", 5);
820 ep.ep30.length = sizeof(struct smbios_30_entry_point);
821 ep.ep30.entry_point_revision = 1;
822 ep.ep30.reserved = 0;
824 /* compliant with smbios spec 3.0 */
825 ep.ep30.smbios_major_version = 3;
826 ep.ep30.smbios_minor_version = 0;
827 ep.ep30.smbios_doc_rev = 0;
829 /* set during table construct, but BIOS might override */
830 ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
832 /* BIOS must recalculate */
833 ep.ep30.checksum = 0;
834 ep.ep30.structure_table_address = cpu_to_le64(0);
836 break;
837 default:
838 abort();
839 break;
843 void smbios_get_tables(MachineState *ms,
844 const struct smbios_phys_mem_area *mem_array,
845 const unsigned int mem_array_size,
846 uint8_t **tables, size_t *tables_len,
847 uint8_t **anchor, size_t *anchor_len)
849 unsigned i, dimm_cnt;
851 if (smbios_legacy) {
852 *tables = *anchor = NULL;
853 *tables_len = *anchor_len = 0;
854 return;
857 if (!smbios_immutable) {
858 smbios_build_type_0_table();
859 smbios_build_type_1_table();
860 smbios_build_type_2_table();
861 smbios_build_type_3_table();
863 smbios_smp_sockets = DIV_ROUND_UP(ms->smp.cpus,
864 ms->smp.cores * ms->smp.threads);
865 assert(smbios_smp_sockets >= 1);
867 for (i = 0; i < smbios_smp_sockets; i++) {
868 smbios_build_type_4_table(ms, i);
871 smbios_build_type_11_table();
873 #define MAX_DIMM_SZ (16 * GiB)
874 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
875 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
877 dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
879 smbios_build_type_16_table(dimm_cnt);
881 for (i = 0; i < dimm_cnt; i++) {
882 smbios_build_type_17_table(i, GET_DIMM_SZ);
885 for (i = 0; i < mem_array_size; i++) {
886 smbios_build_type_19_table(i, mem_array[i].address,
887 mem_array[i].length);
890 smbios_build_type_32_table();
891 smbios_build_type_38_table();
892 smbios_build_type_127_table();
894 smbios_validate_table(ms);
895 smbios_entry_point_setup();
896 smbios_immutable = true;
899 /* return tables blob and entry point (anchor), and their sizes */
900 *tables = smbios_tables;
901 *tables_len = smbios_tables_len;
902 *anchor = (uint8_t *)&ep;
904 /* calculate length based on anchor string */
905 if (!strncmp((char *)&ep, "_SM_", 4)) {
906 *anchor_len = sizeof(struct smbios_21_entry_point);
907 } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
908 *anchor_len = sizeof(struct smbios_30_entry_point);
909 } else {
910 abort();
914 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
916 const char *val = qemu_opt_get(opts, name);
918 if (val) {
919 *dest = val;
924 struct opt_list {
925 const char *name;
926 size_t *ndest;
927 const char ***dest;
930 static int save_opt_one(void *opaque,
931 const char *name, const char *value,
932 Error **errp)
934 struct opt_list *opt = opaque;
936 if (!g_str_equal(name, opt->name)) {
937 return 0;
940 *opt->dest = g_renew(const char *, *opt->dest, (*opt->ndest) + 1);
941 (*opt->dest)[*opt->ndest] = value;
942 (*opt->ndest)++;
943 return 0;
946 static void save_opt_list(size_t *ndest, const char ***dest,
947 QemuOpts *opts, const char *name)
949 struct opt_list opt = {
950 name, ndest, dest,
952 qemu_opt_foreach(opts, save_opt_one, &opt, NULL);
955 void smbios_entry_add(QemuOpts *opts, Error **errp)
957 Error *err = NULL;
958 const char *val;
960 assert(!smbios_immutable);
962 val = qemu_opt_get(opts, "file");
963 if (val) {
964 struct smbios_structure_header *header;
965 int size;
966 struct smbios_table *table; /* legacy mode only */
968 qemu_opts_validate(opts, qemu_smbios_file_opts, &err);
969 if (err) {
970 error_propagate(errp, err);
971 return;
974 size = get_image_size(val);
975 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
976 error_setg(errp, "Cannot read SMBIOS file %s", val);
977 return;
981 * NOTE: standard double '\0' terminator expected, per smbios spec.
982 * (except in legacy mode, where the second '\0' is implicit and
983 * will be inserted by the BIOS).
985 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
986 header = (struct smbios_structure_header *)(smbios_tables +
987 smbios_tables_len);
989 if (load_image_size(val, (uint8_t *)header, size) != size) {
990 error_setg(errp, "Failed to load SMBIOS file %s", val);
991 return;
994 if (test_bit(header->type, have_fields_bitmap)) {
995 error_setg(errp,
996 "can't load type %d struct, fields already specified!",
997 header->type);
998 return;
1000 set_bit(header->type, have_binfile_bitmap);
1002 if (header->type == 4) {
1003 smbios_type4_count++;
1006 smbios_tables_len += size;
1007 if (size > smbios_table_max) {
1008 smbios_table_max = size;
1010 smbios_table_cnt++;
1012 /* add a copy of the newly loaded blob to legacy smbios_entries */
1013 /* NOTE: This code runs before smbios_set_defaults(), so we don't
1014 * yet know which mode (legacy vs. aggregate-table) will be
1015 * required. We therefore add the binary blob to both legacy
1016 * (smbios_entries) and aggregate (smbios_tables) tables, and
1017 * delete the one we don't need from smbios_set_defaults(),
1018 * once we know which machine version has been requested.
1020 if (!smbios_entries) {
1021 smbios_entries_len = sizeof(uint16_t);
1022 smbios_entries = g_malloc0(smbios_entries_len);
1024 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
1025 size + sizeof(*table));
1026 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
1027 table->header.type = SMBIOS_TABLE_ENTRY;
1028 table->header.length = cpu_to_le16(sizeof(*table) + size);
1029 memcpy(table->data, header, size);
1030 smbios_entries_len += sizeof(*table) + size;
1031 (*(uint16_t *)smbios_entries) =
1032 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
1033 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1035 return;
1038 val = qemu_opt_get(opts, "type");
1039 if (val) {
1040 unsigned long type = strtoul(val, NULL, 0);
1042 if (type > SMBIOS_MAX_TYPE) {
1043 error_setg(errp, "out of range!");
1044 return;
1047 if (test_bit(type, have_binfile_bitmap)) {
1048 error_setg(errp, "can't add fields, binary file already loaded!");
1049 return;
1051 set_bit(type, have_fields_bitmap);
1053 switch (type) {
1054 case 0:
1055 qemu_opts_validate(opts, qemu_smbios_type0_opts, &err);
1056 if (err) {
1057 error_propagate(errp, err);
1058 return;
1060 save_opt(&type0.vendor, opts, "vendor");
1061 save_opt(&type0.version, opts, "version");
1062 save_opt(&type0.date, opts, "date");
1063 type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1065 val = qemu_opt_get(opts, "release");
1066 if (val) {
1067 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1068 error_setg(errp, "Invalid release");
1069 return;
1071 type0.have_major_minor = true;
1073 return;
1074 case 1:
1075 qemu_opts_validate(opts, qemu_smbios_type1_opts, &err);
1076 if (err) {
1077 error_propagate(errp, err);
1078 return;
1080 save_opt(&type1.manufacturer, opts, "manufacturer");
1081 save_opt(&type1.product, opts, "product");
1082 save_opt(&type1.version, opts, "version");
1083 save_opt(&type1.serial, opts, "serial");
1084 save_opt(&type1.sku, opts, "sku");
1085 save_opt(&type1.family, opts, "family");
1087 val = qemu_opt_get(opts, "uuid");
1088 if (val) {
1089 if (qemu_uuid_parse(val, &qemu_uuid) != 0) {
1090 error_setg(errp, "Invalid UUID");
1091 return;
1093 qemu_uuid_set = true;
1095 return;
1096 case 2:
1097 qemu_opts_validate(opts, qemu_smbios_type2_opts, &err);
1098 if (err) {
1099 error_propagate(errp, err);
1100 return;
1102 save_opt(&type2.manufacturer, opts, "manufacturer");
1103 save_opt(&type2.product, opts, "product");
1104 save_opt(&type2.version, opts, "version");
1105 save_opt(&type2.serial, opts, "serial");
1106 save_opt(&type2.asset, opts, "asset");
1107 save_opt(&type2.location, opts, "location");
1108 return;
1109 case 3:
1110 qemu_opts_validate(opts, qemu_smbios_type3_opts, &err);
1111 if (err) {
1112 error_propagate(errp, err);
1113 return;
1115 save_opt(&type3.manufacturer, opts, "manufacturer");
1116 save_opt(&type3.version, opts, "version");
1117 save_opt(&type3.serial, opts, "serial");
1118 save_opt(&type3.asset, opts, "asset");
1119 save_opt(&type3.sku, opts, "sku");
1120 return;
1121 case 4:
1122 qemu_opts_validate(opts, qemu_smbios_type4_opts, &err);
1123 if (err) {
1124 error_propagate(errp, err);
1125 return;
1127 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1128 save_opt(&type4.manufacturer, opts, "manufacturer");
1129 save_opt(&type4.version, opts, "version");
1130 save_opt(&type4.serial, opts, "serial");
1131 save_opt(&type4.asset, opts, "asset");
1132 save_opt(&type4.part, opts, "part");
1133 return;
1134 case 11:
1135 qemu_opts_validate(opts, qemu_smbios_type11_opts, &err);
1136 if (err) {
1137 error_propagate(errp, err);
1138 return;
1140 save_opt_list(&type11.nvalues, &type11.values, opts, "value");
1141 return;
1142 case 17:
1143 qemu_opts_validate(opts, qemu_smbios_type17_opts, &err);
1144 if (err) {
1145 error_propagate(errp, err);
1146 return;
1148 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1149 save_opt(&type17.bank, opts, "bank");
1150 save_opt(&type17.manufacturer, opts, "manufacturer");
1151 save_opt(&type17.serial, opts, "serial");
1152 save_opt(&type17.asset, opts, "asset");
1153 save_opt(&type17.part, opts, "part");
1154 type17.speed = qemu_opt_get_number(opts, "speed", 0);
1155 return;
1156 default:
1157 error_setg(errp,
1158 "Don't know how to build fields for SMBIOS type %ld",
1159 type);
1160 return;
1164 error_setg(errp, "Must specify type= or file=");