ppc: Fix POWER7 and POWER8 exception definitions
[qemu.git] / hw / smbios / smbios.c
blobcb8a1111029cfc140f4cc3b944ba28aac8fbe72e
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 "sysemu/cpus.h"
24 #include "hw/smbios/smbios.h"
25 #include "hw/loader.h"
26 #include "exec/cpu-common.h"
28 /* legacy structures and constants for <= 2.0 machines */
29 struct smbios_header {
30 uint16_t length;
31 uint8_t type;
32 } QEMU_PACKED;
34 struct smbios_field {
35 struct smbios_header header;
36 uint8_t type;
37 uint16_t offset;
38 uint8_t data[];
39 } QEMU_PACKED;
41 struct smbios_table {
42 struct smbios_header header;
43 uint8_t data[];
44 } QEMU_PACKED;
46 #define SMBIOS_FIELD_ENTRY 0
47 #define SMBIOS_TABLE_ENTRY 1
49 static uint8_t *smbios_entries;
50 static size_t smbios_entries_len;
51 static bool smbios_legacy = true;
52 static bool smbios_uuid_encoded = true;
53 /* end: legacy structures & constants for <= 2.0 machines */
56 static uint8_t *smbios_tables;
57 static size_t smbios_tables_len;
58 static unsigned smbios_table_max;
59 static unsigned smbios_table_cnt;
60 static SmbiosEntryPointType smbios_ep_type = SMBIOS_ENTRY_POINT_21;
62 static SmbiosEntryPoint ep;
64 static int smbios_type4_count = 0;
65 static bool smbios_immutable;
66 static bool smbios_have_defaults;
67 static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
69 static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
70 static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
72 static struct {
73 const char *vendor, *version, *date;
74 bool have_major_minor, uefi;
75 uint8_t major, minor;
76 } type0;
78 static struct {
79 const char *manufacturer, *product, *version, *serial, *sku, *family;
80 /* uuid is in qemu_uuid[] */
81 } type1;
83 static struct {
84 const char *manufacturer, *product, *version, *serial, *asset, *location;
85 } type2;
87 static struct {
88 const char *manufacturer, *version, *serial, *asset, *sku;
89 } type3;
91 static struct {
92 const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
93 } type4;
95 static struct {
96 const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
97 uint16_t speed;
98 } type17;
100 static QemuOptsList qemu_smbios_opts = {
101 .name = "smbios",
102 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
103 .desc = {
105 * no elements => accept any params
106 * validation will happen later
108 { /* end of list */ }
112 static const QemuOptDesc qemu_smbios_file_opts[] = {
114 .name = "file",
115 .type = QEMU_OPT_STRING,
116 .help = "binary file containing an SMBIOS element",
118 { /* end of list */ }
121 static const QemuOptDesc qemu_smbios_type0_opts[] = {
123 .name = "type",
124 .type = QEMU_OPT_NUMBER,
125 .help = "SMBIOS element type",
127 .name = "vendor",
128 .type = QEMU_OPT_STRING,
129 .help = "vendor name",
131 .name = "version",
132 .type = QEMU_OPT_STRING,
133 .help = "version number",
135 .name = "date",
136 .type = QEMU_OPT_STRING,
137 .help = "release date",
139 .name = "release",
140 .type = QEMU_OPT_STRING,
141 .help = "revision number",
143 .name = "uefi",
144 .type = QEMU_OPT_BOOL,
145 .help = "uefi support",
147 { /* end of list */ }
150 static const QemuOptDesc qemu_smbios_type1_opts[] = {
152 .name = "type",
153 .type = QEMU_OPT_NUMBER,
154 .help = "SMBIOS element type",
156 .name = "manufacturer",
157 .type = QEMU_OPT_STRING,
158 .help = "manufacturer name",
160 .name = "product",
161 .type = QEMU_OPT_STRING,
162 .help = "product name",
164 .name = "version",
165 .type = QEMU_OPT_STRING,
166 .help = "version number",
168 .name = "serial",
169 .type = QEMU_OPT_STRING,
170 .help = "serial number",
172 .name = "uuid",
173 .type = QEMU_OPT_STRING,
174 .help = "UUID",
176 .name = "sku",
177 .type = QEMU_OPT_STRING,
178 .help = "SKU number",
180 .name = "family",
181 .type = QEMU_OPT_STRING,
182 .help = "family name",
184 { /* end of list */ }
187 static const QemuOptDesc qemu_smbios_type2_opts[] = {
189 .name = "type",
190 .type = QEMU_OPT_NUMBER,
191 .help = "SMBIOS element type",
193 .name = "manufacturer",
194 .type = QEMU_OPT_STRING,
195 .help = "manufacturer name",
197 .name = "product",
198 .type = QEMU_OPT_STRING,
199 .help = "product name",
201 .name = "version",
202 .type = QEMU_OPT_STRING,
203 .help = "version number",
205 .name = "serial",
206 .type = QEMU_OPT_STRING,
207 .help = "serial number",
209 .name = "asset",
210 .type = QEMU_OPT_STRING,
211 .help = "asset tag number",
213 .name = "location",
214 .type = QEMU_OPT_STRING,
215 .help = "location in chassis",
217 { /* end of list */ }
220 static const QemuOptDesc qemu_smbios_type3_opts[] = {
222 .name = "type",
223 .type = QEMU_OPT_NUMBER,
224 .help = "SMBIOS element type",
226 .name = "manufacturer",
227 .type = QEMU_OPT_STRING,
228 .help = "manufacturer name",
230 .name = "version",
231 .type = QEMU_OPT_STRING,
232 .help = "version number",
234 .name = "serial",
235 .type = QEMU_OPT_STRING,
236 .help = "serial number",
238 .name = "asset",
239 .type = QEMU_OPT_STRING,
240 .help = "asset tag number",
242 .name = "sku",
243 .type = QEMU_OPT_STRING,
244 .help = "SKU number",
246 { /* end of list */ }
249 static const QemuOptDesc qemu_smbios_type4_opts[] = {
251 .name = "type",
252 .type = QEMU_OPT_NUMBER,
253 .help = "SMBIOS element type",
255 .name = "sock_pfx",
256 .type = QEMU_OPT_STRING,
257 .help = "socket designation string prefix",
259 .name = "manufacturer",
260 .type = QEMU_OPT_STRING,
261 .help = "manufacturer name",
263 .name = "version",
264 .type = QEMU_OPT_STRING,
265 .help = "version number",
267 .name = "serial",
268 .type = QEMU_OPT_STRING,
269 .help = "serial number",
271 .name = "asset",
272 .type = QEMU_OPT_STRING,
273 .help = "asset tag number",
275 .name = "part",
276 .type = QEMU_OPT_STRING,
277 .help = "part number",
279 { /* end of list */ }
282 static const QemuOptDesc qemu_smbios_type17_opts[] = {
284 .name = "type",
285 .type = QEMU_OPT_NUMBER,
286 .help = "SMBIOS element type",
288 .name = "loc_pfx",
289 .type = QEMU_OPT_STRING,
290 .help = "device locator string prefix",
292 .name = "bank",
293 .type = QEMU_OPT_STRING,
294 .help = "bank locator string",
296 .name = "manufacturer",
297 .type = QEMU_OPT_STRING,
298 .help = "manufacturer name",
300 .name = "serial",
301 .type = QEMU_OPT_STRING,
302 .help = "serial number",
304 .name = "asset",
305 .type = QEMU_OPT_STRING,
306 .help = "asset tag number",
308 .name = "part",
309 .type = QEMU_OPT_STRING,
310 .help = "part number",
312 .name = "speed",
313 .type = QEMU_OPT_NUMBER,
314 .help = "maximum capable speed",
316 { /* end of list */ }
319 static void smbios_register_config(void)
321 qemu_add_opts(&qemu_smbios_opts);
324 opts_init(smbios_register_config);
326 static void smbios_validate_table(void)
328 uint32_t expect_t4_count = smbios_legacy ? smp_cpus : smbios_smp_sockets;
330 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
331 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
332 expect_t4_count, smbios_type4_count);
333 exit(1);
338 /* legacy setup functions for <= 2.0 machines */
339 static void smbios_add_field(int type, int offset, const void *data, size_t len)
341 struct smbios_field *field;
343 if (!smbios_entries) {
344 smbios_entries_len = sizeof(uint16_t);
345 smbios_entries = g_malloc0(smbios_entries_len);
347 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
348 sizeof(*field) + len);
349 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
350 field->header.type = SMBIOS_FIELD_ENTRY;
351 field->header.length = cpu_to_le16(sizeof(*field) + len);
353 field->type = type;
354 field->offset = cpu_to_le16(offset);
355 memcpy(field->data, data, len);
357 smbios_entries_len += sizeof(*field) + len;
358 (*(uint16_t *)smbios_entries) =
359 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
362 static void smbios_maybe_add_str(int type, int offset, const char *data)
364 if (data) {
365 smbios_add_field(type, offset, data, strlen(data) + 1);
369 static void smbios_build_type_0_fields(void)
371 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
372 type0.vendor);
373 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
374 type0.version);
375 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
376 bios_release_date_str),
377 type0.date);
378 if (type0.have_major_minor) {
379 smbios_add_field(0, offsetof(struct smbios_type_0,
380 system_bios_major_release),
381 &type0.major, 1);
382 smbios_add_field(0, offsetof(struct smbios_type_0,
383 system_bios_minor_release),
384 &type0.minor, 1);
388 static void smbios_build_type_1_fields(void)
390 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
391 type1.manufacturer);
392 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
393 type1.product);
394 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
395 type1.version);
396 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
397 type1.serial);
398 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
399 type1.sku);
400 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
401 type1.family);
402 if (qemu_uuid_set) {
403 /* We don't encode the UUID in the "wire format" here because this
404 * function is for legacy mode and needs to keep the guest ABI, and
405 * because we don't know what's the SMBIOS version advertised by the
406 * BIOS.
408 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
409 qemu_uuid, 16);
413 uint8_t *smbios_get_table_legacy(size_t *length)
415 if (!smbios_legacy) {
416 *length = 0;
417 return NULL;
420 if (!smbios_immutable) {
421 smbios_build_type_0_fields();
422 smbios_build_type_1_fields();
423 smbios_validate_table();
424 smbios_immutable = true;
426 *length = smbios_entries_len;
427 return smbios_entries;
429 /* end: legacy setup functions for <= 2.0 machines */
432 static bool smbios_skip_table(uint8_t type, bool required_table)
434 if (test_bit(type, have_binfile_bitmap)) {
435 return true; /* user provided their own binary blob(s) */
437 if (test_bit(type, have_fields_bitmap)) {
438 return false; /* user provided fields via command line */
440 if (smbios_have_defaults && required_table) {
441 return false; /* we're building tables, and this one's required */
443 return true;
446 #define SMBIOS_BUILD_TABLE_PRE(tbl_type, tbl_handle, tbl_required) \
447 struct smbios_type_##tbl_type *t; \
448 size_t t_off; /* table offset into smbios_tables */ \
449 int str_index = 0; \
450 do { \
451 /* should we skip building this table ? */ \
452 if (smbios_skip_table(tbl_type, tbl_required)) { \
453 return; \
456 /* use offset of table t within smbios_tables */ \
457 /* (pointer must be updated after each realloc) */ \
458 t_off = smbios_tables_len; \
459 smbios_tables_len += sizeof(*t); \
460 smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
461 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
463 t->header.type = tbl_type; \
464 t->header.length = sizeof(*t); \
465 t->header.handle = cpu_to_le16(tbl_handle); \
466 } while (0)
468 #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
469 do { \
470 int len = (value != NULL) ? strlen(value) + 1 : 0; \
471 if (len > 1) { \
472 smbios_tables = g_realloc(smbios_tables, \
473 smbios_tables_len + len); \
474 memcpy(smbios_tables + smbios_tables_len, value, len); \
475 smbios_tables_len += len; \
476 /* update pointer post-realloc */ \
477 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
478 t->field = ++str_index; \
479 } else { \
480 t->field = 0; \
482 } while (0)
484 #define SMBIOS_BUILD_TABLE_POST \
485 do { \
486 size_t term_cnt, t_size; \
488 /* add '\0' terminator (add two if no strings defined) */ \
489 term_cnt = (str_index == 0) ? 2 : 1; \
490 smbios_tables = g_realloc(smbios_tables, \
491 smbios_tables_len + term_cnt); \
492 memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
493 smbios_tables_len += term_cnt; \
495 /* update smbios max. element size */ \
496 t_size = smbios_tables_len - t_off; \
497 if (t_size > smbios_table_max) { \
498 smbios_table_max = t_size; \
501 /* update smbios element count */ \
502 smbios_table_cnt++; \
503 } while (0)
505 static void smbios_build_type_0_table(void)
507 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
509 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
510 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
512 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
514 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
516 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
518 t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
519 t->bios_characteristics_extension_bytes[0] = 0;
520 t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
521 if (type0.uefi) {
522 t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
525 if (type0.have_major_minor) {
526 t->system_bios_major_release = type0.major;
527 t->system_bios_minor_release = type0.minor;
528 } else {
529 t->system_bios_major_release = 0;
530 t->system_bios_minor_release = 0;
533 /* hardcoded in SeaBIOS */
534 t->embedded_controller_major_release = 0xFF;
535 t->embedded_controller_minor_release = 0xFF;
537 SMBIOS_BUILD_TABLE_POST;
540 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
541 * format specified by SMBIOS version 2.6.
543 static void smbios_encode_uuid(struct smbios_uuid *uuid, const uint8_t *buf)
545 memcpy(uuid, buf, 16);
546 if (smbios_uuid_encoded) {
547 uuid->time_low = bswap32(uuid->time_low);
548 uuid->time_mid = bswap16(uuid->time_mid);
549 uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
553 static void smbios_build_type_1_table(void)
555 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
557 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
558 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
559 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
560 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
561 if (qemu_uuid_set) {
562 smbios_encode_uuid(&t->uuid, qemu_uuid);
563 } else {
564 memset(&t->uuid, 0, 16);
566 t->wake_up_type = 0x06; /* power switch */
567 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
568 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
570 SMBIOS_BUILD_TABLE_POST;
573 static void smbios_build_type_2_table(void)
575 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
577 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
578 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
579 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
580 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
581 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
582 t->feature_flags = 0x01; /* Motherboard */
583 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
584 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
585 t->board_type = 0x0A; /* Motherboard */
586 t->contained_element_count = 0;
588 SMBIOS_BUILD_TABLE_POST;
591 static void smbios_build_type_3_table(void)
593 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
595 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
596 t->type = 0x01; /* Other */
597 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
598 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
599 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
600 t->boot_up_state = 0x03; /* Safe */
601 t->power_supply_state = 0x03; /* Safe */
602 t->thermal_state = 0x03; /* Safe */
603 t->security_status = 0x02; /* Unknown */
604 t->oem_defined = cpu_to_le32(0);
605 t->height = 0;
606 t->number_of_power_cords = 0;
607 t->contained_element_count = 0;
608 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
610 SMBIOS_BUILD_TABLE_POST;
613 static void smbios_build_type_4_table(unsigned instance)
615 char sock_str[128];
617 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
619 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
620 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
621 t->processor_type = 0x03; /* CPU */
622 t->processor_family = 0x01; /* Other */
623 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
624 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
625 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
626 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
627 t->voltage = 0;
628 t->external_clock = cpu_to_le16(0); /* Unknown */
629 /* SVVP requires max_speed and current_speed to not be unknown. */
630 t->max_speed = cpu_to_le16(2000); /* 2000 MHz */
631 t->current_speed = cpu_to_le16(2000); /* 2000 MHz */
632 t->status = 0x41; /* Socket populated, CPU enabled */
633 t->processor_upgrade = 0x01; /* Other */
634 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
635 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
636 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
637 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
638 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
639 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
640 t->core_count = t->core_enabled = smp_cores;
641 t->thread_count = smp_threads;
642 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
643 t->processor_family2 = cpu_to_le16(0x01); /* Other */
645 SMBIOS_BUILD_TABLE_POST;
646 smbios_type4_count++;
649 #define ONE_KB ((ram_addr_t)1 << 10)
650 #define ONE_MB ((ram_addr_t)1 << 20)
651 #define ONE_GB ((ram_addr_t)1 << 30)
653 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
655 static void smbios_build_type_16_table(unsigned dimm_cnt)
657 uint64_t size_kb;
659 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
661 t->location = 0x01; /* Other */
662 t->use = 0x03; /* System memory */
663 t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
664 size_kb = QEMU_ALIGN_UP(ram_size, ONE_KB) / ONE_KB;
665 if (size_kb < MAX_T16_STD_SZ) {
666 t->maximum_capacity = cpu_to_le32(size_kb);
667 t->extended_maximum_capacity = cpu_to_le64(0);
668 } else {
669 t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
670 t->extended_maximum_capacity = cpu_to_le64(ram_size);
672 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
673 t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
675 SMBIOS_BUILD_TABLE_POST;
678 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
679 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
681 static void smbios_build_type_17_table(unsigned instance, uint64_t size)
683 char loc_str[128];
684 uint64_t size_mb;
686 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
688 t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
689 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
690 t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
691 t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
692 size_mb = QEMU_ALIGN_UP(size, ONE_MB) / ONE_MB;
693 if (size_mb < MAX_T17_STD_SZ) {
694 t->size = cpu_to_le16(size_mb);
695 t->extended_size = cpu_to_le32(0);
696 } else {
697 assert(size_mb < MAX_T17_EXT_SZ);
698 t->size = cpu_to_le16(MAX_T17_STD_SZ);
699 t->extended_size = cpu_to_le32(size_mb);
701 t->form_factor = 0x09; /* DIMM */
702 t->device_set = 0; /* Not in a set */
703 snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
704 SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
705 SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
706 t->memory_type = 0x07; /* RAM */
707 t->type_detail = cpu_to_le16(0x02); /* Other */
708 t->speed = cpu_to_le16(type17.speed);
709 SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
710 SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
711 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
712 SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
713 t->attributes = 0; /* Unknown */
714 t->configured_clock_speed = t->speed; /* reuse value for max speed */
715 t->minimum_voltage = cpu_to_le16(0); /* Unknown */
716 t->maximum_voltage = cpu_to_le16(0); /* Unknown */
717 t->configured_voltage = cpu_to_le16(0); /* Unknown */
719 SMBIOS_BUILD_TABLE_POST;
722 static void smbios_build_type_19_table(unsigned instance,
723 uint64_t start, uint64_t size)
725 uint64_t end, start_kb, end_kb;
727 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
729 end = start + size - 1;
730 assert(end > start);
731 start_kb = start / ONE_KB;
732 end_kb = end / ONE_KB;
733 if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
734 t->starting_address = cpu_to_le32(start_kb);
735 t->ending_address = cpu_to_le32(end_kb);
736 t->extended_starting_address =
737 t->extended_ending_address = cpu_to_le64(0);
738 } else {
739 t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
740 t->extended_starting_address = cpu_to_le64(start);
741 t->extended_ending_address = cpu_to_le64(end);
743 t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
744 t->partition_width = 1; /* One device per row */
746 SMBIOS_BUILD_TABLE_POST;
749 static void smbios_build_type_32_table(void)
751 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
753 memset(t->reserved, 0, 6);
754 t->boot_status = 0; /* No errors detected */
756 SMBIOS_BUILD_TABLE_POST;
759 static void smbios_build_type_127_table(void)
761 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
762 SMBIOS_BUILD_TABLE_POST;
765 void smbios_set_cpuid(uint32_t version, uint32_t features)
767 smbios_cpuid_version = version;
768 smbios_cpuid_features = features;
771 #define SMBIOS_SET_DEFAULT(field, value) \
772 if (!field) { \
773 field = value; \
776 void smbios_set_defaults(const char *manufacturer, const char *product,
777 const char *version, bool legacy_mode,
778 bool uuid_encoded, SmbiosEntryPointType ep_type)
780 smbios_have_defaults = true;
781 smbios_legacy = legacy_mode;
782 smbios_uuid_encoded = uuid_encoded;
783 smbios_ep_type = ep_type;
785 /* drop unwanted version of command-line file blob(s) */
786 if (smbios_legacy) {
787 g_free(smbios_tables);
788 /* in legacy mode, also complain if fields were given for types > 1 */
789 if (find_next_bit(have_fields_bitmap,
790 SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
791 error_report("can't process fields for smbios "
792 "types > 1 on machine versions < 2.1!");
793 exit(1);
795 } else {
796 g_free(smbios_entries);
799 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
800 SMBIOS_SET_DEFAULT(type1.product, product);
801 SMBIOS_SET_DEFAULT(type1.version, version);
802 SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
803 SMBIOS_SET_DEFAULT(type2.product, product);
804 SMBIOS_SET_DEFAULT(type2.version, version);
805 SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
806 SMBIOS_SET_DEFAULT(type3.version, version);
807 SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
808 SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
809 SMBIOS_SET_DEFAULT(type4.version, version);
810 SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
811 SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
814 static void smbios_entry_point_setup(void)
816 switch (smbios_ep_type) {
817 case SMBIOS_ENTRY_POINT_21:
818 memcpy(ep.ep21.anchor_string, "_SM_", 4);
819 memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
820 ep.ep21.length = sizeof(struct smbios_21_entry_point);
821 ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
822 memset(ep.ep21.formatted_area, 0, 5);
824 /* compliant with smbios spec v2.8 */
825 ep.ep21.smbios_major_version = 2;
826 ep.ep21.smbios_minor_version = 8;
827 ep.ep21.smbios_bcd_revision = 0x28;
829 /* set during table construction, but BIOS may override: */
830 ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
831 ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
832 ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
834 /* BIOS must recalculate */
835 ep.ep21.checksum = 0;
836 ep.ep21.intermediate_checksum = 0;
837 ep.ep21.structure_table_address = cpu_to_le32(0);
839 break;
840 case SMBIOS_ENTRY_POINT_30:
841 memcpy(ep.ep30.anchor_string, "_SM3_", 5);
842 ep.ep30.length = sizeof(struct smbios_30_entry_point);
843 ep.ep30.entry_point_revision = 1;
844 ep.ep30.reserved = 0;
846 /* compliant with smbios spec 3.0 */
847 ep.ep30.smbios_major_version = 3;
848 ep.ep30.smbios_minor_version = 0;
849 ep.ep30.smbios_doc_rev = 0;
851 /* set during table construct, but BIOS might override */
852 ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
854 /* BIOS must recalculate */
855 ep.ep30.checksum = 0;
856 ep.ep30.structure_table_address = cpu_to_le64(0);
858 break;
859 default:
860 abort();
861 break;
865 void smbios_get_tables(const struct smbios_phys_mem_area *mem_array,
866 const unsigned int mem_array_size,
867 uint8_t **tables, size_t *tables_len,
868 uint8_t **anchor, size_t *anchor_len)
870 unsigned i, dimm_cnt;
872 if (smbios_legacy) {
873 *tables = *anchor = NULL;
874 *tables_len = *anchor_len = 0;
875 return;
878 if (!smbios_immutable) {
879 smbios_build_type_0_table();
880 smbios_build_type_1_table();
881 smbios_build_type_2_table();
882 smbios_build_type_3_table();
884 smbios_smp_sockets = DIV_ROUND_UP(smp_cpus, smp_cores * smp_threads);
885 assert(smbios_smp_sockets >= 1);
887 for (i = 0; i < smbios_smp_sockets; i++) {
888 smbios_build_type_4_table(i);
891 #define MAX_DIMM_SZ (16ll * ONE_GB)
892 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
893 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
895 dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
897 smbios_build_type_16_table(dimm_cnt);
899 for (i = 0; i < dimm_cnt; i++) {
900 smbios_build_type_17_table(i, GET_DIMM_SZ);
903 for (i = 0; i < mem_array_size; i++) {
904 smbios_build_type_19_table(i, mem_array[i].address,
905 mem_array[i].length);
908 smbios_build_type_32_table();
909 smbios_build_type_127_table();
911 smbios_validate_table();
912 smbios_entry_point_setup();
913 smbios_immutable = true;
916 /* return tables blob and entry point (anchor), and their sizes */
917 *tables = smbios_tables;
918 *tables_len = smbios_tables_len;
919 *anchor = (uint8_t *)&ep;
921 /* calculate length based on anchor string */
922 if (!strncmp((char *)&ep, "_SM_", 4)) {
923 *anchor_len = sizeof(struct smbios_21_entry_point);
924 } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
925 *anchor_len = sizeof(struct smbios_30_entry_point);
926 } else {
927 abort();
931 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
933 const char *val = qemu_opt_get(opts, name);
935 if (val) {
936 *dest = val;
940 void smbios_entry_add(QemuOpts *opts)
942 const char *val;
944 assert(!smbios_immutable);
946 val = qemu_opt_get(opts, "file");
947 if (val) {
948 struct smbios_structure_header *header;
949 int size;
950 struct smbios_table *table; /* legacy mode only */
952 qemu_opts_validate(opts, qemu_smbios_file_opts, &error_fatal);
954 size = get_image_size(val);
955 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
956 error_report("Cannot read SMBIOS file %s", val);
957 exit(1);
961 * NOTE: standard double '\0' terminator expected, per smbios spec.
962 * (except in legacy mode, where the second '\0' is implicit and
963 * will be inserted by the BIOS).
965 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
966 header = (struct smbios_structure_header *)(smbios_tables +
967 smbios_tables_len);
969 if (load_image(val, (uint8_t *)header) != size) {
970 error_report("Failed to load SMBIOS file %s", val);
971 exit(1);
974 if (test_bit(header->type, have_fields_bitmap)) {
975 error_report("can't load type %d struct, fields already specified!",
976 header->type);
977 exit(1);
979 set_bit(header->type, have_binfile_bitmap);
981 if (header->type == 4) {
982 smbios_type4_count++;
985 smbios_tables_len += size;
986 if (size > smbios_table_max) {
987 smbios_table_max = size;
989 smbios_table_cnt++;
991 /* add a copy of the newly loaded blob to legacy smbios_entries */
992 /* NOTE: This code runs before smbios_set_defaults(), so we don't
993 * yet know which mode (legacy vs. aggregate-table) will be
994 * required. We therefore add the binary blob to both legacy
995 * (smbios_entries) and aggregate (smbios_tables) tables, and
996 * delete the one we don't need from smbios_set_defaults(),
997 * once we know which machine version has been requested.
999 if (!smbios_entries) {
1000 smbios_entries_len = sizeof(uint16_t);
1001 smbios_entries = g_malloc0(smbios_entries_len);
1003 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
1004 size + sizeof(*table));
1005 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
1006 table->header.type = SMBIOS_TABLE_ENTRY;
1007 table->header.length = cpu_to_le16(sizeof(*table) + size);
1008 memcpy(table->data, header, size);
1009 smbios_entries_len += sizeof(*table) + size;
1010 (*(uint16_t *)smbios_entries) =
1011 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
1012 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1014 return;
1017 val = qemu_opt_get(opts, "type");
1018 if (val) {
1019 unsigned long type = strtoul(val, NULL, 0);
1021 if (type > SMBIOS_MAX_TYPE) {
1022 error_report("out of range!");
1023 exit(1);
1026 if (test_bit(type, have_binfile_bitmap)) {
1027 error_report("can't add fields, binary file already loaded!");
1028 exit(1);
1030 set_bit(type, have_fields_bitmap);
1032 switch (type) {
1033 case 0:
1034 qemu_opts_validate(opts, qemu_smbios_type0_opts, &error_fatal);
1035 save_opt(&type0.vendor, opts, "vendor");
1036 save_opt(&type0.version, opts, "version");
1037 save_opt(&type0.date, opts, "date");
1038 type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1040 val = qemu_opt_get(opts, "release");
1041 if (val) {
1042 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1043 error_report("Invalid release");
1044 exit(1);
1046 type0.have_major_minor = true;
1048 return;
1049 case 1:
1050 qemu_opts_validate(opts, qemu_smbios_type1_opts, &error_fatal);
1051 save_opt(&type1.manufacturer, opts, "manufacturer");
1052 save_opt(&type1.product, opts, "product");
1053 save_opt(&type1.version, opts, "version");
1054 save_opt(&type1.serial, opts, "serial");
1055 save_opt(&type1.sku, opts, "sku");
1056 save_opt(&type1.family, opts, "family");
1058 val = qemu_opt_get(opts, "uuid");
1059 if (val) {
1060 if (qemu_uuid_parse(val, qemu_uuid) != 0) {
1061 error_report("Invalid UUID");
1062 exit(1);
1064 qemu_uuid_set = true;
1066 return;
1067 case 2:
1068 qemu_opts_validate(opts, qemu_smbios_type2_opts, &error_fatal);
1069 save_opt(&type2.manufacturer, opts, "manufacturer");
1070 save_opt(&type2.product, opts, "product");
1071 save_opt(&type2.version, opts, "version");
1072 save_opt(&type2.serial, opts, "serial");
1073 save_opt(&type2.asset, opts, "asset");
1074 save_opt(&type2.location, opts, "location");
1075 return;
1076 case 3:
1077 qemu_opts_validate(opts, qemu_smbios_type3_opts, &error_fatal);
1078 save_opt(&type3.manufacturer, opts, "manufacturer");
1079 save_opt(&type3.version, opts, "version");
1080 save_opt(&type3.serial, opts, "serial");
1081 save_opt(&type3.asset, opts, "asset");
1082 save_opt(&type3.sku, opts, "sku");
1083 return;
1084 case 4:
1085 qemu_opts_validate(opts, qemu_smbios_type4_opts, &error_fatal);
1086 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1087 save_opt(&type4.manufacturer, opts, "manufacturer");
1088 save_opt(&type4.version, opts, "version");
1089 save_opt(&type4.serial, opts, "serial");
1090 save_opt(&type4.asset, opts, "asset");
1091 save_opt(&type4.part, opts, "part");
1092 return;
1093 case 17:
1094 qemu_opts_validate(opts, qemu_smbios_type17_opts, &error_fatal);
1095 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1096 save_opt(&type17.bank, opts, "bank");
1097 save_opt(&type17.manufacturer, opts, "manufacturer");
1098 save_opt(&type17.serial, opts, "serial");
1099 save_opt(&type17.asset, opts, "asset");
1100 save_opt(&type17.part, opts, "part");
1101 type17.speed = qemu_opt_get_number(opts, "speed", 0);
1102 return;
1103 default:
1104 error_report("Don't know how to build fields for SMBIOS type %ld",
1105 type);
1106 exit(1);
1110 error_report("Must specify type= or file=");
1111 exit(1);