MAINTAINERS: Add jcody as blockjobs, block devices maintainer
[qemu.git] / hw / i386 / smbios.c
blobf2e9ab62cef671a74cd8791bad88959dc5ce7f9f
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/config-file.h"
19 #include "qemu/error-report.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/cpus.h"
22 #include "hw/i386/pc.h"
23 #include "hw/i386/smbios.h"
24 #include "hw/loader.h"
27 /* legacy structures and constants for <= 2.0 machines */
28 struct smbios_header {
29 uint16_t length;
30 uint8_t type;
31 } QEMU_PACKED;
33 struct smbios_field {
34 struct smbios_header header;
35 uint8_t type;
36 uint16_t offset;
37 uint8_t data[];
38 } QEMU_PACKED;
40 struct smbios_table {
41 struct smbios_header header;
42 uint8_t data[];
43 } QEMU_PACKED;
45 #define SMBIOS_FIELD_ENTRY 0
46 #define SMBIOS_TABLE_ENTRY 1
48 static uint8_t *smbios_entries;
49 static size_t smbios_entries_len;
50 static bool smbios_legacy = true;
51 static bool smbios_uuid_encoded = true;
52 /* end: legacy structures & constants for <= 2.0 machines */
55 static uint8_t *smbios_tables;
56 static size_t smbios_tables_len;
57 static unsigned smbios_table_max;
58 static unsigned smbios_table_cnt;
59 static struct smbios_entry_point ep;
61 static int smbios_type4_count = 0;
62 static bool smbios_immutable;
63 static bool smbios_have_defaults;
64 static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
66 static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
67 static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
69 static struct {
70 const char *vendor, *version, *date;
71 bool have_major_minor, uefi;
72 uint8_t major, minor;
73 } type0;
75 static struct {
76 const char *manufacturer, *product, *version, *serial, *sku, *family;
77 /* uuid is in qemu_uuid[] */
78 } type1;
80 static struct {
81 const char *manufacturer, *product, *version, *serial, *asset, *location;
82 } type2;
84 static struct {
85 const char *manufacturer, *version, *serial, *asset, *sku;
86 } type3;
88 static struct {
89 const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
90 } type4;
92 static struct {
93 const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
94 } type17;
96 static QemuOptsList qemu_smbios_opts = {
97 .name = "smbios",
98 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
99 .desc = {
101 * no elements => accept any params
102 * validation will happen later
104 { /* end of list */ }
108 static const QemuOptDesc qemu_smbios_file_opts[] = {
110 .name = "file",
111 .type = QEMU_OPT_STRING,
112 .help = "binary file containing an SMBIOS element",
114 { /* end of list */ }
117 static const QemuOptDesc qemu_smbios_type0_opts[] = {
119 .name = "type",
120 .type = QEMU_OPT_NUMBER,
121 .help = "SMBIOS element type",
123 .name = "vendor",
124 .type = QEMU_OPT_STRING,
125 .help = "vendor name",
127 .name = "version",
128 .type = QEMU_OPT_STRING,
129 .help = "version number",
131 .name = "date",
132 .type = QEMU_OPT_STRING,
133 .help = "release date",
135 .name = "release",
136 .type = QEMU_OPT_STRING,
137 .help = "revision number",
139 .name = "uefi",
140 .type = QEMU_OPT_BOOL,
141 .help = "uefi support",
143 { /* end of list */ }
146 static const QemuOptDesc qemu_smbios_type1_opts[] = {
148 .name = "type",
149 .type = QEMU_OPT_NUMBER,
150 .help = "SMBIOS element type",
152 .name = "manufacturer",
153 .type = QEMU_OPT_STRING,
154 .help = "manufacturer name",
156 .name = "product",
157 .type = QEMU_OPT_STRING,
158 .help = "product name",
160 .name = "version",
161 .type = QEMU_OPT_STRING,
162 .help = "version number",
164 .name = "serial",
165 .type = QEMU_OPT_STRING,
166 .help = "serial number",
168 .name = "uuid",
169 .type = QEMU_OPT_STRING,
170 .help = "UUID",
172 .name = "sku",
173 .type = QEMU_OPT_STRING,
174 .help = "SKU number",
176 .name = "family",
177 .type = QEMU_OPT_STRING,
178 .help = "family name",
180 { /* end of list */ }
183 static const QemuOptDesc qemu_smbios_type2_opts[] = {
185 .name = "type",
186 .type = QEMU_OPT_NUMBER,
187 .help = "SMBIOS element type",
189 .name = "manufacturer",
190 .type = QEMU_OPT_STRING,
191 .help = "manufacturer name",
193 .name = "product",
194 .type = QEMU_OPT_STRING,
195 .help = "product name",
197 .name = "version",
198 .type = QEMU_OPT_STRING,
199 .help = "version number",
201 .name = "serial",
202 .type = QEMU_OPT_STRING,
203 .help = "serial number",
205 .name = "asset",
206 .type = QEMU_OPT_STRING,
207 .help = "asset tag number",
209 .name = "location",
210 .type = QEMU_OPT_STRING,
211 .help = "location in chassis",
213 { /* end of list */ }
216 static const QemuOptDesc qemu_smbios_type3_opts[] = {
218 .name = "type",
219 .type = QEMU_OPT_NUMBER,
220 .help = "SMBIOS element type",
222 .name = "manufacturer",
223 .type = QEMU_OPT_STRING,
224 .help = "manufacturer name",
226 .name = "version",
227 .type = QEMU_OPT_STRING,
228 .help = "version number",
230 .name = "serial",
231 .type = QEMU_OPT_STRING,
232 .help = "serial number",
234 .name = "asset",
235 .type = QEMU_OPT_STRING,
236 .help = "asset tag number",
238 .name = "sku",
239 .type = QEMU_OPT_STRING,
240 .help = "SKU number",
242 { /* end of list */ }
245 static const QemuOptDesc qemu_smbios_type4_opts[] = {
247 .name = "type",
248 .type = QEMU_OPT_NUMBER,
249 .help = "SMBIOS element type",
251 .name = "sock_pfx",
252 .type = QEMU_OPT_STRING,
253 .help = "socket designation string prefix",
255 .name = "manufacturer",
256 .type = QEMU_OPT_STRING,
257 .help = "manufacturer name",
259 .name = "version",
260 .type = QEMU_OPT_STRING,
261 .help = "version number",
263 .name = "serial",
264 .type = QEMU_OPT_STRING,
265 .help = "serial number",
267 .name = "asset",
268 .type = QEMU_OPT_STRING,
269 .help = "asset tag number",
271 .name = "part",
272 .type = QEMU_OPT_STRING,
273 .help = "part number",
275 { /* end of list */ }
278 static const QemuOptDesc qemu_smbios_type17_opts[] = {
280 .name = "type",
281 .type = QEMU_OPT_NUMBER,
282 .help = "SMBIOS element type",
284 .name = "loc_pfx",
285 .type = QEMU_OPT_STRING,
286 .help = "device locator string prefix",
288 .name = "bank",
289 .type = QEMU_OPT_STRING,
290 .help = "bank locator string",
292 .name = "manufacturer",
293 .type = QEMU_OPT_STRING,
294 .help = "manufacturer name",
296 .name = "serial",
297 .type = QEMU_OPT_STRING,
298 .help = "serial number",
300 .name = "asset",
301 .type = QEMU_OPT_STRING,
302 .help = "asset tag number",
304 .name = "part",
305 .type = QEMU_OPT_STRING,
306 .help = "part number",
308 { /* end of list */ }
311 static void smbios_register_config(void)
313 qemu_add_opts(&qemu_smbios_opts);
316 machine_init(smbios_register_config);
318 static void smbios_validate_table(void)
320 uint32_t expect_t4_count = smbios_legacy ? smp_cpus : smbios_smp_sockets;
322 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
323 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
324 expect_t4_count, smbios_type4_count);
325 exit(1);
330 /* legacy setup functions for <= 2.0 machines */
331 static void smbios_add_field(int type, int offset, const void *data, size_t len)
333 struct smbios_field *field;
335 if (!smbios_entries) {
336 smbios_entries_len = sizeof(uint16_t);
337 smbios_entries = g_malloc0(smbios_entries_len);
339 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
340 sizeof(*field) + len);
341 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
342 field->header.type = SMBIOS_FIELD_ENTRY;
343 field->header.length = cpu_to_le16(sizeof(*field) + len);
345 field->type = type;
346 field->offset = cpu_to_le16(offset);
347 memcpy(field->data, data, len);
349 smbios_entries_len += sizeof(*field) + len;
350 (*(uint16_t *)smbios_entries) =
351 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
354 static void smbios_maybe_add_str(int type, int offset, const char *data)
356 if (data) {
357 smbios_add_field(type, offset, data, strlen(data) + 1);
361 static void smbios_build_type_0_fields(void)
363 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
364 type0.vendor);
365 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
366 type0.version);
367 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
368 bios_release_date_str),
369 type0.date);
370 if (type0.have_major_minor) {
371 smbios_add_field(0, offsetof(struct smbios_type_0,
372 system_bios_major_release),
373 &type0.major, 1);
374 smbios_add_field(0, offsetof(struct smbios_type_0,
375 system_bios_minor_release),
376 &type0.minor, 1);
380 static void smbios_build_type_1_fields(void)
382 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
383 type1.manufacturer);
384 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
385 type1.product);
386 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
387 type1.version);
388 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
389 type1.serial);
390 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
391 type1.sku);
392 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
393 type1.family);
394 if (qemu_uuid_set) {
395 /* We don't encode the UUID in the "wire format" here because this
396 * function is for legacy mode and needs to keep the guest ABI, and
397 * because we don't know what's the SMBIOS version advertised by the
398 * BIOS.
400 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
401 qemu_uuid, 16);
405 uint8_t *smbios_get_table_legacy(size_t *length)
407 if (!smbios_legacy) {
408 *length = 0;
409 return NULL;
412 if (!smbios_immutable) {
413 smbios_build_type_0_fields();
414 smbios_build_type_1_fields();
415 smbios_validate_table();
416 smbios_immutable = true;
418 *length = smbios_entries_len;
419 return smbios_entries;
421 /* end: legacy setup functions for <= 2.0 machines */
424 static bool smbios_skip_table(uint8_t type, bool required_table)
426 if (test_bit(type, have_binfile_bitmap)) {
427 return true; /* user provided their own binary blob(s) */
429 if (test_bit(type, have_fields_bitmap)) {
430 return false; /* user provided fields via command line */
432 if (smbios_have_defaults && required_table) {
433 return false; /* we're building tables, and this one's required */
435 return true;
438 #define SMBIOS_BUILD_TABLE_PRE(tbl_type, tbl_handle, tbl_required) \
439 struct smbios_type_##tbl_type *t; \
440 size_t t_off; /* table offset into smbios_tables */ \
441 int str_index = 0; \
442 do { \
443 /* should we skip building this table ? */ \
444 if (smbios_skip_table(tbl_type, tbl_required)) { \
445 return; \
448 /* use offset of table t within smbios_tables */ \
449 /* (pointer must be updated after each realloc) */ \
450 t_off = smbios_tables_len; \
451 smbios_tables_len += sizeof(*t); \
452 smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
453 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
455 t->header.type = tbl_type; \
456 t->header.length = sizeof(*t); \
457 t->header.handle = cpu_to_le16(tbl_handle); \
458 } while (0)
460 #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
461 do { \
462 int len = (value != NULL) ? strlen(value) + 1 : 0; \
463 if (len > 1) { \
464 smbios_tables = g_realloc(smbios_tables, \
465 smbios_tables_len + len); \
466 memcpy(smbios_tables + smbios_tables_len, value, len); \
467 smbios_tables_len += len; \
468 /* update pointer post-realloc */ \
469 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
470 t->field = ++str_index; \
471 } else { \
472 t->field = 0; \
474 } while (0)
476 #define SMBIOS_BUILD_TABLE_POST \
477 do { \
478 size_t term_cnt, t_size; \
480 /* add '\0' terminator (add two if no strings defined) */ \
481 term_cnt = (str_index == 0) ? 2 : 1; \
482 smbios_tables = g_realloc(smbios_tables, \
483 smbios_tables_len + term_cnt); \
484 memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
485 smbios_tables_len += term_cnt; \
487 /* update smbios max. element size */ \
488 t_size = smbios_tables_len - t_off; \
489 if (t_size > smbios_table_max) { \
490 smbios_table_max = t_size; \
493 /* update smbios element count */ \
494 smbios_table_cnt++; \
495 } while (0)
497 static void smbios_build_type_0_table(void)
499 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
501 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
502 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
504 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
506 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
508 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
510 t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
511 t->bios_characteristics_extension_bytes[0] = 0;
512 t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
513 if (type0.uefi) {
514 t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
517 if (type0.have_major_minor) {
518 t->system_bios_major_release = type0.major;
519 t->system_bios_minor_release = type0.minor;
520 } else {
521 t->system_bios_major_release = 0;
522 t->system_bios_minor_release = 0;
525 /* hardcoded in SeaBIOS */
526 t->embedded_controller_major_release = 0xFF;
527 t->embedded_controller_minor_release = 0xFF;
529 SMBIOS_BUILD_TABLE_POST;
532 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
533 * format specified by SMBIOS version 2.6.
535 static void smbios_encode_uuid(struct smbios_uuid *uuid, const uint8_t *buf)
537 memcpy(uuid, buf, 16);
538 if (smbios_uuid_encoded) {
539 uuid->time_low = bswap32(uuid->time_low);
540 uuid->time_mid = bswap16(uuid->time_mid);
541 uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
545 static void smbios_build_type_1_table(void)
547 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
549 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
550 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
551 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
552 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
553 if (qemu_uuid_set) {
554 smbios_encode_uuid(&t->uuid, qemu_uuid);
555 } else {
556 memset(&t->uuid, 0, 16);
558 t->wake_up_type = 0x06; /* power switch */
559 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
560 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
562 SMBIOS_BUILD_TABLE_POST;
565 static void smbios_build_type_2_table(void)
567 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
569 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
570 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
571 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
572 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
573 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
574 t->feature_flags = 0x01; /* Motherboard */
575 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
576 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
577 t->board_type = 0x0A; /* Motherboard */
578 t->contained_element_count = 0;
580 SMBIOS_BUILD_TABLE_POST;
583 static void smbios_build_type_3_table(void)
585 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
587 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
588 t->type = 0x01; /* Other */
589 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
590 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
591 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
592 t->boot_up_state = 0x03; /* Safe */
593 t->power_supply_state = 0x03; /* Safe */
594 t->thermal_state = 0x03; /* Safe */
595 t->security_status = 0x02; /* Unknown */
596 t->oem_defined = cpu_to_le32(0);
597 t->height = 0;
598 t->number_of_power_cords = 0;
599 t->contained_element_count = 0;
600 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
602 SMBIOS_BUILD_TABLE_POST;
605 static void smbios_build_type_4_table(unsigned instance)
607 char sock_str[128];
609 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
611 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
612 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
613 t->processor_type = 0x03; /* CPU */
614 t->processor_family = 0x01; /* Other */
615 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
616 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
617 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
618 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
619 t->voltage = 0;
620 t->external_clock = cpu_to_le16(0); /* Unknown */
621 /* SVVP requires max_speed and current_speed to not be unknown. */
622 t->max_speed = cpu_to_le16(2000); /* 2000 MHz */
623 t->current_speed = cpu_to_le16(2000); /* 2000 MHz */
624 t->status = 0x41; /* Socket populated, CPU enabled */
625 t->processor_upgrade = 0x01; /* Other */
626 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
627 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
628 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
629 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
630 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
631 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
632 t->core_count = t->core_enabled = smp_cores;
633 t->thread_count = smp_threads;
634 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
635 t->processor_family2 = cpu_to_le16(0x01); /* Other */
637 SMBIOS_BUILD_TABLE_POST;
638 smbios_type4_count++;
641 #define ONE_KB ((ram_addr_t)1 << 10)
642 #define ONE_MB ((ram_addr_t)1 << 20)
643 #define ONE_GB ((ram_addr_t)1 << 30)
645 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
647 static void smbios_build_type_16_table(unsigned dimm_cnt)
649 uint64_t size_kb;
651 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
653 t->location = 0x01; /* Other */
654 t->use = 0x03; /* System memory */
655 t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
656 size_kb = QEMU_ALIGN_UP(ram_size, ONE_KB) / ONE_KB;
657 if (size_kb < MAX_T16_STD_SZ) {
658 t->maximum_capacity = cpu_to_le32(size_kb);
659 t->extended_maximum_capacity = cpu_to_le64(0);
660 } else {
661 t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
662 t->extended_maximum_capacity = cpu_to_le64(ram_size);
664 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
665 t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
667 SMBIOS_BUILD_TABLE_POST;
670 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
671 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
673 static void smbios_build_type_17_table(unsigned instance, uint64_t size)
675 char loc_str[128];
676 uint64_t size_mb;
678 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
680 t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
681 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
682 t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
683 t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
684 size_mb = QEMU_ALIGN_UP(size, ONE_MB) / ONE_MB;
685 if (size_mb < MAX_T17_STD_SZ) {
686 t->size = cpu_to_le16(size_mb);
687 t->extended_size = cpu_to_le32(0);
688 } else {
689 assert(size_mb < MAX_T17_EXT_SZ);
690 t->size = cpu_to_le16(MAX_T17_STD_SZ);
691 t->extended_size = cpu_to_le32(size_mb);
693 t->form_factor = 0x09; /* DIMM */
694 t->device_set = 0; /* Not in a set */
695 snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
696 SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
697 SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
698 t->memory_type = 0x07; /* RAM */
699 t->type_detail = cpu_to_le16(0x02); /* Other */
700 t->speed = cpu_to_le16(0); /* Unknown */
701 SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
702 SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
703 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
704 SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
705 t->attributes = 0; /* Unknown */
706 t->configured_clock_speed = cpu_to_le16(0); /* Unknown */
707 t->minimum_voltage = cpu_to_le16(0); /* Unknown */
708 t->maximum_voltage = cpu_to_le16(0); /* Unknown */
709 t->configured_voltage = cpu_to_le16(0); /* Unknown */
711 SMBIOS_BUILD_TABLE_POST;
714 static void smbios_build_type_19_table(unsigned instance,
715 uint64_t start, uint64_t size)
717 uint64_t end, start_kb, end_kb;
719 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
721 end = start + size - 1;
722 assert(end > start);
723 start_kb = start / ONE_KB;
724 end_kb = end / ONE_KB;
725 if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
726 t->starting_address = cpu_to_le32(start_kb);
727 t->ending_address = cpu_to_le32(end_kb);
728 t->extended_starting_address =
729 t->extended_ending_address = cpu_to_le64(0);
730 } else {
731 t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
732 t->extended_starting_address = cpu_to_le64(start);
733 t->extended_ending_address = cpu_to_le64(end);
735 t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
736 t->partition_width = 1; /* One device per row */
738 SMBIOS_BUILD_TABLE_POST;
741 static void smbios_build_type_32_table(void)
743 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
745 memset(t->reserved, 0, 6);
746 t->boot_status = 0; /* No errors detected */
748 SMBIOS_BUILD_TABLE_POST;
751 static void smbios_build_type_127_table(void)
753 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
754 SMBIOS_BUILD_TABLE_POST;
757 void smbios_set_cpuid(uint32_t version, uint32_t features)
759 smbios_cpuid_version = version;
760 smbios_cpuid_features = features;
763 #define SMBIOS_SET_DEFAULT(field, value) \
764 if (!field) { \
765 field = value; \
768 void smbios_set_defaults(const char *manufacturer, const char *product,
769 const char *version, bool legacy_mode,
770 bool uuid_encoded)
772 smbios_have_defaults = true;
773 smbios_legacy = legacy_mode;
774 smbios_uuid_encoded = uuid_encoded;
776 /* drop unwanted version of command-line file blob(s) */
777 if (smbios_legacy) {
778 g_free(smbios_tables);
779 /* in legacy mode, also complain if fields were given for types > 1 */
780 if (find_next_bit(have_fields_bitmap,
781 SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
782 error_report("can't process fields for smbios "
783 "types > 1 on machine versions < 2.1!");
784 exit(1);
786 } else {
787 g_free(smbios_entries);
790 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
791 SMBIOS_SET_DEFAULT(type1.product, product);
792 SMBIOS_SET_DEFAULT(type1.version, version);
793 SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
794 SMBIOS_SET_DEFAULT(type2.product, product);
795 SMBIOS_SET_DEFAULT(type2.version, version);
796 SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
797 SMBIOS_SET_DEFAULT(type3.version, version);
798 SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
799 SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
800 SMBIOS_SET_DEFAULT(type4.version, version);
801 SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
802 SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
805 static void smbios_entry_point_setup(void)
807 memcpy(ep.anchor_string, "_SM_", 4);
808 memcpy(ep.intermediate_anchor_string, "_DMI_", 5);
809 ep.length = sizeof(struct smbios_entry_point);
810 ep.entry_point_revision = 0; /* formatted_area reserved, per spec v2.1+ */
811 memset(ep.formatted_area, 0, 5);
813 /* compliant with smbios spec v2.8 */
814 ep.smbios_major_version = 2;
815 ep.smbios_minor_version = 8;
816 ep.smbios_bcd_revision = 0x28;
818 /* set during table construction, but BIOS may override: */
819 ep.structure_table_length = cpu_to_le16(smbios_tables_len);
820 ep.max_structure_size = cpu_to_le16(smbios_table_max);
821 ep.number_of_structures = cpu_to_le16(smbios_table_cnt);
823 /* BIOS must recalculate: */
824 ep.checksum = 0;
825 ep.intermediate_checksum = 0;
826 ep.structure_table_address = cpu_to_le32(0);
829 void smbios_get_tables(uint8_t **tables, size_t *tables_len,
830 uint8_t **anchor, size_t *anchor_len)
832 unsigned i, dimm_cnt, instance;
834 if (smbios_legacy) {
835 *tables = *anchor = NULL;
836 *tables_len = *anchor_len = 0;
837 return;
840 if (!smbios_immutable) {
841 smbios_build_type_0_table();
842 smbios_build_type_1_table();
843 smbios_build_type_2_table();
844 smbios_build_type_3_table();
846 smbios_smp_sockets = DIV_ROUND_UP(smp_cpus, smp_cores * smp_threads);
847 assert(smbios_smp_sockets >= 1);
849 for (i = 0; i < smbios_smp_sockets; i++) {
850 smbios_build_type_4_table(i);
853 #define MAX_DIMM_SZ (16ll * ONE_GB)
854 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
855 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
857 dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
859 smbios_build_type_16_table(dimm_cnt);
861 for (i = 0; i < dimm_cnt; i++) {
862 smbios_build_type_17_table(i, GET_DIMM_SZ);
865 for (i = 0, instance = 0; i < e820_get_num_entries(); i++) {
866 uint64_t address, length;
867 if (e820_get_entry(i, E820_RAM, &address, &length)) {
868 smbios_build_type_19_table(instance++, address, length);
872 smbios_build_type_32_table();
873 smbios_build_type_127_table();
875 smbios_validate_table();
876 smbios_entry_point_setup();
877 smbios_immutable = true;
880 /* return tables blob and entry point (anchor), and their sizes */
881 *tables = smbios_tables;
882 *tables_len = smbios_tables_len;
883 *anchor = (uint8_t *)&ep;
884 *anchor_len = sizeof(struct smbios_entry_point);
887 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
889 const char *val = qemu_opt_get(opts, name);
891 if (val) {
892 *dest = val;
896 void smbios_entry_add(QemuOpts *opts)
898 Error *local_err = NULL;
899 const char *val;
901 assert(!smbios_immutable);
903 val = qemu_opt_get(opts, "file");
904 if (val) {
905 struct smbios_structure_header *header;
906 int size;
907 struct smbios_table *table; /* legacy mode only */
909 qemu_opts_validate(opts, qemu_smbios_file_opts, &local_err);
910 if (local_err) {
911 error_report_err(local_err);
912 exit(1);
915 size = get_image_size(val);
916 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
917 error_report("Cannot read SMBIOS file %s", val);
918 exit(1);
922 * NOTE: standard double '\0' terminator expected, per smbios spec.
923 * (except in legacy mode, where the second '\0' is implicit and
924 * will be inserted by the BIOS).
926 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
927 header = (struct smbios_structure_header *)(smbios_tables +
928 smbios_tables_len);
930 if (load_image(val, (uint8_t *)header) != size) {
931 error_report("Failed to load SMBIOS file %s", val);
932 exit(1);
935 if (test_bit(header->type, have_fields_bitmap)) {
936 error_report("can't load type %d struct, fields already specified!",
937 header->type);
938 exit(1);
940 set_bit(header->type, have_binfile_bitmap);
942 if (header->type == 4) {
943 smbios_type4_count++;
946 smbios_tables_len += size;
947 if (size > smbios_table_max) {
948 smbios_table_max = size;
950 smbios_table_cnt++;
952 /* add a copy of the newly loaded blob to legacy smbios_entries */
953 /* NOTE: This code runs before smbios_set_defaults(), so we don't
954 * yet know which mode (legacy vs. aggregate-table) will be
955 * required. We therefore add the binary blob to both legacy
956 * (smbios_entries) and aggregate (smbios_tables) tables, and
957 * delete the one we don't need from smbios_set_defaults(),
958 * once we know which machine version has been requested.
960 if (!smbios_entries) {
961 smbios_entries_len = sizeof(uint16_t);
962 smbios_entries = g_malloc0(smbios_entries_len);
964 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
965 size + sizeof(*table));
966 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
967 table->header.type = SMBIOS_TABLE_ENTRY;
968 table->header.length = cpu_to_le16(sizeof(*table) + size);
969 memcpy(table->data, header, size);
970 smbios_entries_len += sizeof(*table) + size;
971 (*(uint16_t *)smbios_entries) =
972 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
973 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
975 return;
978 val = qemu_opt_get(opts, "type");
979 if (val) {
980 unsigned long type = strtoul(val, NULL, 0);
982 if (type > SMBIOS_MAX_TYPE) {
983 error_report("out of range!");
984 exit(1);
987 if (test_bit(type, have_binfile_bitmap)) {
988 error_report("can't add fields, binary file already loaded!");
989 exit(1);
991 set_bit(type, have_fields_bitmap);
993 switch (type) {
994 case 0:
995 qemu_opts_validate(opts, qemu_smbios_type0_opts, &local_err);
996 if (local_err) {
997 error_report_err(local_err);
998 exit(1);
1000 save_opt(&type0.vendor, opts, "vendor");
1001 save_opt(&type0.version, opts, "version");
1002 save_opt(&type0.date, opts, "date");
1003 type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1005 val = qemu_opt_get(opts, "release");
1006 if (val) {
1007 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1008 error_report("Invalid release");
1009 exit(1);
1011 type0.have_major_minor = true;
1013 return;
1014 case 1:
1015 qemu_opts_validate(opts, qemu_smbios_type1_opts, &local_err);
1016 if (local_err) {
1017 error_report_err(local_err);
1018 exit(1);
1020 save_opt(&type1.manufacturer, opts, "manufacturer");
1021 save_opt(&type1.product, opts, "product");
1022 save_opt(&type1.version, opts, "version");
1023 save_opt(&type1.serial, opts, "serial");
1024 save_opt(&type1.sku, opts, "sku");
1025 save_opt(&type1.family, opts, "family");
1027 val = qemu_opt_get(opts, "uuid");
1028 if (val) {
1029 if (qemu_uuid_parse(val, qemu_uuid) != 0) {
1030 error_report("Invalid UUID");
1031 exit(1);
1033 qemu_uuid_set = true;
1035 return;
1036 case 2:
1037 qemu_opts_validate(opts, qemu_smbios_type2_opts, &local_err);
1038 if (local_err) {
1039 error_report_err(local_err);
1040 exit(1);
1042 save_opt(&type2.manufacturer, opts, "manufacturer");
1043 save_opt(&type2.product, opts, "product");
1044 save_opt(&type2.version, opts, "version");
1045 save_opt(&type2.serial, opts, "serial");
1046 save_opt(&type2.asset, opts, "asset");
1047 save_opt(&type2.location, opts, "location");
1048 return;
1049 case 3:
1050 qemu_opts_validate(opts, qemu_smbios_type3_opts, &local_err);
1051 if (local_err) {
1052 error_report_err(local_err);
1053 exit(1);
1055 save_opt(&type3.manufacturer, opts, "manufacturer");
1056 save_opt(&type3.version, opts, "version");
1057 save_opt(&type3.serial, opts, "serial");
1058 save_opt(&type3.asset, opts, "asset");
1059 save_opt(&type3.sku, opts, "sku");
1060 return;
1061 case 4:
1062 qemu_opts_validate(opts, qemu_smbios_type4_opts, &local_err);
1063 if (local_err) {
1064 error_report_err(local_err);
1065 exit(1);
1067 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1068 save_opt(&type4.manufacturer, opts, "manufacturer");
1069 save_opt(&type4.version, opts, "version");
1070 save_opt(&type4.serial, opts, "serial");
1071 save_opt(&type4.asset, opts, "asset");
1072 save_opt(&type4.part, opts, "part");
1073 return;
1074 case 17:
1075 qemu_opts_validate(opts, qemu_smbios_type17_opts, &local_err);
1076 if (local_err) {
1077 error_report_err(local_err);
1078 exit(1);
1080 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1081 save_opt(&type17.bank, opts, "bank");
1082 save_opt(&type17.manufacturer, opts, "manufacturer");
1083 save_opt(&type17.serial, opts, "serial");
1084 save_opt(&type17.asset, opts, "asset");
1085 save_opt(&type17.part, opts, "part");
1086 return;
1087 default:
1088 error_report("Don't know how to build fields for SMBIOS type %ld",
1089 type);
1090 exit(1);
1094 error_report("Must specify type= or file=");
1095 exit(1);