target/ppc: add support for POWER9 HILE
[qemu/ar7.git] / hw / smbios / smbios.c
blob5d11f01874dd407f1216733a894324d885dfab0a
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 "qapi/error.h"
20 #include "qemu/config-file.h"
21 #include "qemu/error-report.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/uuid.h"
24 #include "sysemu/cpus.h"
25 #include "hw/smbios/smbios.h"
26 #include "hw/loader.h"
27 #include "exec/cpu-common.h"
28 #include "smbios_build.h"
29 #include "hw/smbios/ipmi.h"
31 /* legacy structures and constants for <= 2.0 machines */
32 struct smbios_header {
33 uint16_t length;
34 uint8_t type;
35 } QEMU_PACKED;
37 struct smbios_field {
38 struct smbios_header header;
39 uint8_t type;
40 uint16_t offset;
41 uint8_t data[];
42 } QEMU_PACKED;
44 struct smbios_table {
45 struct smbios_header header;
46 uint8_t data[];
47 } QEMU_PACKED;
49 #define SMBIOS_FIELD_ENTRY 0
50 #define SMBIOS_TABLE_ENTRY 1
52 static uint8_t *smbios_entries;
53 static size_t smbios_entries_len;
54 static bool smbios_legacy = true;
55 static bool smbios_uuid_encoded = true;
56 /* end: legacy structures & constants for <= 2.0 machines */
59 uint8_t *smbios_tables;
60 size_t smbios_tables_len;
61 unsigned smbios_table_max;
62 unsigned smbios_table_cnt;
63 static SmbiosEntryPointType smbios_ep_type = SMBIOS_ENTRY_POINT_21;
65 static SmbiosEntryPoint ep;
67 static int smbios_type4_count = 0;
68 static bool smbios_immutable;
69 static bool smbios_have_defaults;
70 static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
72 static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
73 static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
75 static struct {
76 const char *vendor, *version, *date;
77 bool have_major_minor, uefi;
78 uint8_t major, minor;
79 } type0;
81 static struct {
82 const char *manufacturer, *product, *version, *serial, *sku, *family;
83 /* uuid is in qemu_uuid */
84 } type1;
86 static struct {
87 const char *manufacturer, *product, *version, *serial, *asset, *location;
88 } type2;
90 static struct {
91 const char *manufacturer, *version, *serial, *asset, *sku;
92 } type3;
94 static struct {
95 const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
96 } type4;
98 static struct {
99 size_t nvalues;
100 const char **values;
101 } type11;
103 static struct {
104 const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
105 uint16_t speed;
106 } type17;
108 static QemuOptsList qemu_smbios_opts = {
109 .name = "smbios",
110 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
111 .desc = {
113 * no elements => accept any params
114 * validation will happen later
116 { /* end of list */ }
120 static const QemuOptDesc qemu_smbios_file_opts[] = {
122 .name = "file",
123 .type = QEMU_OPT_STRING,
124 .help = "binary file containing an SMBIOS element",
126 { /* end of list */ }
129 static const QemuOptDesc qemu_smbios_type0_opts[] = {
131 .name = "type",
132 .type = QEMU_OPT_NUMBER,
133 .help = "SMBIOS element type",
135 .name = "vendor",
136 .type = QEMU_OPT_STRING,
137 .help = "vendor name",
139 .name = "version",
140 .type = QEMU_OPT_STRING,
141 .help = "version number",
143 .name = "date",
144 .type = QEMU_OPT_STRING,
145 .help = "release date",
147 .name = "release",
148 .type = QEMU_OPT_STRING,
149 .help = "revision number",
151 .name = "uefi",
152 .type = QEMU_OPT_BOOL,
153 .help = "uefi support",
155 { /* end of list */ }
158 static const QemuOptDesc qemu_smbios_type1_opts[] = {
160 .name = "type",
161 .type = QEMU_OPT_NUMBER,
162 .help = "SMBIOS element type",
164 .name = "manufacturer",
165 .type = QEMU_OPT_STRING,
166 .help = "manufacturer name",
168 .name = "product",
169 .type = QEMU_OPT_STRING,
170 .help = "product name",
172 .name = "version",
173 .type = QEMU_OPT_STRING,
174 .help = "version number",
176 .name = "serial",
177 .type = QEMU_OPT_STRING,
178 .help = "serial number",
180 .name = "uuid",
181 .type = QEMU_OPT_STRING,
182 .help = "UUID",
184 .name = "sku",
185 .type = QEMU_OPT_STRING,
186 .help = "SKU number",
188 .name = "family",
189 .type = QEMU_OPT_STRING,
190 .help = "family name",
192 { /* end of list */ }
195 static const QemuOptDesc qemu_smbios_type2_opts[] = {
197 .name = "type",
198 .type = QEMU_OPT_NUMBER,
199 .help = "SMBIOS element type",
201 .name = "manufacturer",
202 .type = QEMU_OPT_STRING,
203 .help = "manufacturer name",
205 .name = "product",
206 .type = QEMU_OPT_STRING,
207 .help = "product name",
209 .name = "version",
210 .type = QEMU_OPT_STRING,
211 .help = "version number",
213 .name = "serial",
214 .type = QEMU_OPT_STRING,
215 .help = "serial number",
217 .name = "asset",
218 .type = QEMU_OPT_STRING,
219 .help = "asset tag number",
221 .name = "location",
222 .type = QEMU_OPT_STRING,
223 .help = "location in chassis",
225 { /* end of list */ }
228 static const QemuOptDesc qemu_smbios_type3_opts[] = {
230 .name = "type",
231 .type = QEMU_OPT_NUMBER,
232 .help = "SMBIOS element type",
234 .name = "manufacturer",
235 .type = QEMU_OPT_STRING,
236 .help = "manufacturer name",
238 .name = "version",
239 .type = QEMU_OPT_STRING,
240 .help = "version number",
242 .name = "serial",
243 .type = QEMU_OPT_STRING,
244 .help = "serial number",
246 .name = "asset",
247 .type = QEMU_OPT_STRING,
248 .help = "asset tag number",
250 .name = "sku",
251 .type = QEMU_OPT_STRING,
252 .help = "SKU number",
254 { /* end of list */ }
257 static const QemuOptDesc qemu_smbios_type4_opts[] = {
259 .name = "type",
260 .type = QEMU_OPT_NUMBER,
261 .help = "SMBIOS element type",
263 .name = "sock_pfx",
264 .type = QEMU_OPT_STRING,
265 .help = "socket designation string prefix",
267 .name = "manufacturer",
268 .type = QEMU_OPT_STRING,
269 .help = "manufacturer name",
271 .name = "version",
272 .type = QEMU_OPT_STRING,
273 .help = "version number",
275 .name = "serial",
276 .type = QEMU_OPT_STRING,
277 .help = "serial number",
279 .name = "asset",
280 .type = QEMU_OPT_STRING,
281 .help = "asset tag number",
283 .name = "part",
284 .type = QEMU_OPT_STRING,
285 .help = "part number",
287 { /* end of list */ }
290 static const QemuOptDesc qemu_smbios_type11_opts[] = {
292 .name = "value",
293 .type = QEMU_OPT_STRING,
294 .help = "OEM string data",
298 static const QemuOptDesc qemu_smbios_type17_opts[] = {
300 .name = "type",
301 .type = QEMU_OPT_NUMBER,
302 .help = "SMBIOS element type",
304 .name = "loc_pfx",
305 .type = QEMU_OPT_STRING,
306 .help = "device locator string prefix",
308 .name = "bank",
309 .type = QEMU_OPT_STRING,
310 .help = "bank locator string",
312 .name = "manufacturer",
313 .type = QEMU_OPT_STRING,
314 .help = "manufacturer name",
316 .name = "serial",
317 .type = QEMU_OPT_STRING,
318 .help = "serial number",
320 .name = "asset",
321 .type = QEMU_OPT_STRING,
322 .help = "asset tag number",
324 .name = "part",
325 .type = QEMU_OPT_STRING,
326 .help = "part number",
328 .name = "speed",
329 .type = QEMU_OPT_NUMBER,
330 .help = "maximum capable speed",
332 { /* end of list */ }
335 static void smbios_register_config(void)
337 qemu_add_opts(&qemu_smbios_opts);
340 opts_init(smbios_register_config);
342 static void smbios_validate_table(void)
344 uint32_t expect_t4_count = smbios_legacy ? smp_cpus : smbios_smp_sockets;
346 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
347 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
348 expect_t4_count, smbios_type4_count);
349 exit(1);
354 /* legacy setup functions for <= 2.0 machines */
355 static void smbios_add_field(int type, int offset, const void *data, size_t len)
357 struct smbios_field *field;
359 if (!smbios_entries) {
360 smbios_entries_len = sizeof(uint16_t);
361 smbios_entries = g_malloc0(smbios_entries_len);
363 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
364 sizeof(*field) + len);
365 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
366 field->header.type = SMBIOS_FIELD_ENTRY;
367 field->header.length = cpu_to_le16(sizeof(*field) + len);
369 field->type = type;
370 field->offset = cpu_to_le16(offset);
371 memcpy(field->data, data, len);
373 smbios_entries_len += sizeof(*field) + len;
374 (*(uint16_t *)smbios_entries) =
375 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
378 static void smbios_maybe_add_str(int type, int offset, const char *data)
380 if (data) {
381 smbios_add_field(type, offset, data, strlen(data) + 1);
385 static void smbios_build_type_0_fields(void)
387 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
388 type0.vendor);
389 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
390 type0.version);
391 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
392 bios_release_date_str),
393 type0.date);
394 if (type0.have_major_minor) {
395 smbios_add_field(0, offsetof(struct smbios_type_0,
396 system_bios_major_release),
397 &type0.major, 1);
398 smbios_add_field(0, offsetof(struct smbios_type_0,
399 system_bios_minor_release),
400 &type0.minor, 1);
404 static void smbios_build_type_1_fields(void)
406 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
407 type1.manufacturer);
408 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
409 type1.product);
410 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
411 type1.version);
412 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
413 type1.serial);
414 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
415 type1.sku);
416 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
417 type1.family);
418 if (qemu_uuid_set) {
419 /* We don't encode the UUID in the "wire format" here because this
420 * function is for legacy mode and needs to keep the guest ABI, and
421 * because we don't know what's the SMBIOS version advertised by the
422 * BIOS.
424 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
425 &qemu_uuid, 16);
429 uint8_t *smbios_get_table_legacy(size_t *length)
431 if (!smbios_legacy) {
432 *length = 0;
433 return NULL;
436 if (!smbios_immutable) {
437 smbios_build_type_0_fields();
438 smbios_build_type_1_fields();
439 smbios_validate_table();
440 smbios_immutable = true;
442 *length = smbios_entries_len;
443 return smbios_entries;
445 /* end: legacy setup functions for <= 2.0 machines */
448 bool smbios_skip_table(uint8_t type, bool required_table)
450 if (test_bit(type, have_binfile_bitmap)) {
451 return true; /* user provided their own binary blob(s) */
453 if (test_bit(type, have_fields_bitmap)) {
454 return false; /* user provided fields via command line */
456 if (smbios_have_defaults && required_table) {
457 return false; /* we're building tables, and this one's required */
459 return true;
462 static void smbios_build_type_0_table(void)
464 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
466 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
467 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
469 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
471 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
473 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
475 t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
476 t->bios_characteristics_extension_bytes[0] = 0;
477 t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
478 if (type0.uefi) {
479 t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
482 if (type0.have_major_minor) {
483 t->system_bios_major_release = type0.major;
484 t->system_bios_minor_release = type0.minor;
485 } else {
486 t->system_bios_major_release = 0;
487 t->system_bios_minor_release = 0;
490 /* hardcoded in SeaBIOS */
491 t->embedded_controller_major_release = 0xFF;
492 t->embedded_controller_minor_release = 0xFF;
494 SMBIOS_BUILD_TABLE_POST;
497 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
498 * format specified by SMBIOS version 2.6.
500 static void smbios_encode_uuid(struct smbios_uuid *uuid, QemuUUID *in)
502 memcpy(uuid, in, 16);
503 if (smbios_uuid_encoded) {
504 uuid->time_low = bswap32(uuid->time_low);
505 uuid->time_mid = bswap16(uuid->time_mid);
506 uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
510 static void smbios_build_type_1_table(void)
512 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
514 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
515 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
516 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
517 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
518 if (qemu_uuid_set) {
519 smbios_encode_uuid(&t->uuid, &qemu_uuid);
520 } else {
521 memset(&t->uuid, 0, 16);
523 t->wake_up_type = 0x06; /* power switch */
524 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
525 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
527 SMBIOS_BUILD_TABLE_POST;
530 static void smbios_build_type_2_table(void)
532 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
534 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
535 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
536 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
537 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
538 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
539 t->feature_flags = 0x01; /* Motherboard */
540 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
541 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
542 t->board_type = 0x0A; /* Motherboard */
543 t->contained_element_count = 0;
545 SMBIOS_BUILD_TABLE_POST;
548 static void smbios_build_type_3_table(void)
550 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
552 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
553 t->type = 0x01; /* Other */
554 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
555 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
556 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
557 t->boot_up_state = 0x03; /* Safe */
558 t->power_supply_state = 0x03; /* Safe */
559 t->thermal_state = 0x03; /* Safe */
560 t->security_status = 0x02; /* Unknown */
561 t->oem_defined = cpu_to_le32(0);
562 t->height = 0;
563 t->number_of_power_cords = 0;
564 t->contained_element_count = 0;
565 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
567 SMBIOS_BUILD_TABLE_POST;
570 static void smbios_build_type_4_table(unsigned instance)
572 char sock_str[128];
574 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
576 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
577 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
578 t->processor_type = 0x03; /* CPU */
579 t->processor_family = 0x01; /* Other */
580 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
581 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
582 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
583 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
584 t->voltage = 0;
585 t->external_clock = cpu_to_le16(0); /* Unknown */
586 /* SVVP requires max_speed and current_speed to not be unknown. */
587 t->max_speed = cpu_to_le16(2000); /* 2000 MHz */
588 t->current_speed = cpu_to_le16(2000); /* 2000 MHz */
589 t->status = 0x41; /* Socket populated, CPU enabled */
590 t->processor_upgrade = 0x01; /* Other */
591 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
592 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
593 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
594 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
595 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
596 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
597 t->core_count = t->core_enabled = smp_cores;
598 t->thread_count = smp_threads;
599 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
600 t->processor_family2 = cpu_to_le16(0x01); /* Other */
602 SMBIOS_BUILD_TABLE_POST;
603 smbios_type4_count++;
606 static void smbios_build_type_11_table(void)
608 char count_str[128];
609 size_t i;
611 if (type11.nvalues == 0) {
612 return;
615 SMBIOS_BUILD_TABLE_PRE(11, 0xe00, true); /* required */
617 snprintf(count_str, sizeof(count_str), "%zu", type11.nvalues);
618 t->count = type11.nvalues;
620 for (i = 0; i < type11.nvalues; i++) {
621 SMBIOS_TABLE_SET_STR_LIST(11, type11.values[i]);
624 SMBIOS_BUILD_TABLE_POST;
627 #define ONE_KB ((ram_addr_t)1 << 10)
628 #define ONE_MB ((ram_addr_t)1 << 20)
629 #define ONE_GB ((ram_addr_t)1 << 30)
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, ONE_KB) / ONE_KB;
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, ONE_MB) / ONE_MB;
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 / ONE_KB;
710 end_kb = end / ONE_KB;
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(const struct smbios_phys_mem_area *mem_array,
844 const unsigned int mem_array_size,
845 uint8_t **tables, size_t *tables_len,
846 uint8_t **anchor, size_t *anchor_len)
848 unsigned i, dimm_cnt;
850 if (smbios_legacy) {
851 *tables = *anchor = NULL;
852 *tables_len = *anchor_len = 0;
853 return;
856 if (!smbios_immutable) {
857 smbios_build_type_0_table();
858 smbios_build_type_1_table();
859 smbios_build_type_2_table();
860 smbios_build_type_3_table();
862 smbios_smp_sockets = DIV_ROUND_UP(smp_cpus, smp_cores * smp_threads);
863 assert(smbios_smp_sockets >= 1);
865 for (i = 0; i < smbios_smp_sockets; i++) {
866 smbios_build_type_4_table(i);
869 smbios_build_type_11_table();
871 #define MAX_DIMM_SZ (16ll * ONE_GB)
872 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
873 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
875 dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
877 smbios_build_type_16_table(dimm_cnt);
879 for (i = 0; i < dimm_cnt; i++) {
880 smbios_build_type_17_table(i, GET_DIMM_SZ);
883 for (i = 0; i < mem_array_size; i++) {
884 smbios_build_type_19_table(i, mem_array[i].address,
885 mem_array[i].length);
888 smbios_build_type_32_table();
889 smbios_build_type_38_table();
890 smbios_build_type_127_table();
892 smbios_validate_table();
893 smbios_entry_point_setup();
894 smbios_immutable = true;
897 /* return tables blob and entry point (anchor), and their sizes */
898 *tables = smbios_tables;
899 *tables_len = smbios_tables_len;
900 *anchor = (uint8_t *)&ep;
902 /* calculate length based on anchor string */
903 if (!strncmp((char *)&ep, "_SM_", 4)) {
904 *anchor_len = sizeof(struct smbios_21_entry_point);
905 } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
906 *anchor_len = sizeof(struct smbios_30_entry_point);
907 } else {
908 abort();
912 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
914 const char *val = qemu_opt_get(opts, name);
916 if (val) {
917 *dest = val;
922 struct opt_list {
923 const char *name;
924 size_t *ndest;
925 const char ***dest;
928 static int save_opt_one(void *opaque,
929 const char *name, const char *value,
930 Error **errp)
932 struct opt_list *opt = opaque;
934 if (!g_str_equal(name, opt->name)) {
935 return 0;
938 *opt->dest = g_renew(const char *, *opt->dest, (*opt->ndest) + 1);
939 (*opt->dest)[*opt->ndest] = value;
940 (*opt->ndest)++;
941 return 0;
944 static void save_opt_list(size_t *ndest, const char ***dest,
945 QemuOpts *opts, const char *name)
947 struct opt_list opt = {
948 name, ndest, dest,
950 qemu_opt_foreach(opts, save_opt_one, &opt, NULL);
953 void smbios_entry_add(QemuOpts *opts, Error **errp)
955 const char *val;
957 assert(!smbios_immutable);
959 val = qemu_opt_get(opts, "file");
960 if (val) {
961 struct smbios_structure_header *header;
962 int size;
963 struct smbios_table *table; /* legacy mode only */
965 qemu_opts_validate(opts, qemu_smbios_file_opts, &error_fatal);
967 size = get_image_size(val);
968 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
969 error_report("Cannot read SMBIOS file %s", val);
970 exit(1);
974 * NOTE: standard double '\0' terminator expected, per smbios spec.
975 * (except in legacy mode, where the second '\0' is implicit and
976 * will be inserted by the BIOS).
978 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
979 header = (struct smbios_structure_header *)(smbios_tables +
980 smbios_tables_len);
982 if (load_image(val, (uint8_t *)header) != size) {
983 error_report("Failed to load SMBIOS file %s", val);
984 exit(1);
987 if (test_bit(header->type, have_fields_bitmap)) {
988 error_report("can't load type %d struct, fields already specified!",
989 header->type);
990 exit(1);
992 set_bit(header->type, have_binfile_bitmap);
994 if (header->type == 4) {
995 smbios_type4_count++;
998 smbios_tables_len += size;
999 if (size > smbios_table_max) {
1000 smbios_table_max = size;
1002 smbios_table_cnt++;
1004 /* add a copy of the newly loaded blob to legacy smbios_entries */
1005 /* NOTE: This code runs before smbios_set_defaults(), so we don't
1006 * yet know which mode (legacy vs. aggregate-table) will be
1007 * required. We therefore add the binary blob to both legacy
1008 * (smbios_entries) and aggregate (smbios_tables) tables, and
1009 * delete the one we don't need from smbios_set_defaults(),
1010 * once we know which machine version has been requested.
1012 if (!smbios_entries) {
1013 smbios_entries_len = sizeof(uint16_t);
1014 smbios_entries = g_malloc0(smbios_entries_len);
1016 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
1017 size + sizeof(*table));
1018 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
1019 table->header.type = SMBIOS_TABLE_ENTRY;
1020 table->header.length = cpu_to_le16(sizeof(*table) + size);
1021 memcpy(table->data, header, size);
1022 smbios_entries_len += sizeof(*table) + size;
1023 (*(uint16_t *)smbios_entries) =
1024 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
1025 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1027 return;
1030 val = qemu_opt_get(opts, "type");
1031 if (val) {
1032 unsigned long type = strtoul(val, NULL, 0);
1034 if (type > SMBIOS_MAX_TYPE) {
1035 error_report("out of range!");
1036 exit(1);
1039 if (test_bit(type, have_binfile_bitmap)) {
1040 error_report("can't add fields, binary file already loaded!");
1041 exit(1);
1043 set_bit(type, have_fields_bitmap);
1045 switch (type) {
1046 case 0:
1047 qemu_opts_validate(opts, qemu_smbios_type0_opts, &error_fatal);
1048 save_opt(&type0.vendor, opts, "vendor");
1049 save_opt(&type0.version, opts, "version");
1050 save_opt(&type0.date, opts, "date");
1051 type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1053 val = qemu_opt_get(opts, "release");
1054 if (val) {
1055 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1056 error_report("Invalid release");
1057 exit(1);
1059 type0.have_major_minor = true;
1061 return;
1062 case 1:
1063 qemu_opts_validate(opts, qemu_smbios_type1_opts, &error_fatal);
1064 save_opt(&type1.manufacturer, opts, "manufacturer");
1065 save_opt(&type1.product, opts, "product");
1066 save_opt(&type1.version, opts, "version");
1067 save_opt(&type1.serial, opts, "serial");
1068 save_opt(&type1.sku, opts, "sku");
1069 save_opt(&type1.family, opts, "family");
1071 val = qemu_opt_get(opts, "uuid");
1072 if (val) {
1073 if (qemu_uuid_parse(val, &qemu_uuid) != 0) {
1074 error_report("Invalid UUID");
1075 exit(1);
1077 qemu_uuid_set = true;
1079 return;
1080 case 2:
1081 qemu_opts_validate(opts, qemu_smbios_type2_opts, &error_fatal);
1082 save_opt(&type2.manufacturer, opts, "manufacturer");
1083 save_opt(&type2.product, opts, "product");
1084 save_opt(&type2.version, opts, "version");
1085 save_opt(&type2.serial, opts, "serial");
1086 save_opt(&type2.asset, opts, "asset");
1087 save_opt(&type2.location, opts, "location");
1088 return;
1089 case 3:
1090 qemu_opts_validate(opts, qemu_smbios_type3_opts, &error_fatal);
1091 save_opt(&type3.manufacturer, opts, "manufacturer");
1092 save_opt(&type3.version, opts, "version");
1093 save_opt(&type3.serial, opts, "serial");
1094 save_opt(&type3.asset, opts, "asset");
1095 save_opt(&type3.sku, opts, "sku");
1096 return;
1097 case 4:
1098 qemu_opts_validate(opts, qemu_smbios_type4_opts, &error_fatal);
1099 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1100 save_opt(&type4.manufacturer, opts, "manufacturer");
1101 save_opt(&type4.version, opts, "version");
1102 save_opt(&type4.serial, opts, "serial");
1103 save_opt(&type4.asset, opts, "asset");
1104 save_opt(&type4.part, opts, "part");
1105 return;
1106 case 11:
1107 qemu_opts_validate(opts, qemu_smbios_type11_opts, &error_fatal);
1108 save_opt_list(&type11.nvalues, &type11.values, opts, "value");
1109 return;
1110 case 17:
1111 qemu_opts_validate(opts, qemu_smbios_type17_opts, &error_fatal);
1112 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1113 save_opt(&type17.bank, opts, "bank");
1114 save_opt(&type17.manufacturer, opts, "manufacturer");
1115 save_opt(&type17.serial, opts, "serial");
1116 save_opt(&type17.asset, opts, "asset");
1117 save_opt(&type17.part, opts, "part");
1118 type17.speed = qemu_opt_get_number(opts, "speed", 0);
1119 return;
1120 default:
1121 error_report("Don't know how to build fields for SMBIOS type %ld",
1122 type);
1123 exit(1);
1127 error_report("Must specify type= or file=");
1128 exit(1);