pc: init pcms->apic_id_limit once and use it throughout pc.c
[qemu/ar7.git] / hw / smbios / smbios.c
blob3b5f9bd534e7658e3284c052ad37fc01699c45c5
1 /*
2 * SMBIOS Support
4 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5 * Copyright (C) 2013 Red Hat, Inc.
7 * Authors:
8 * Alex Williamson <alex.williamson@hp.com>
9 * Markus Armbruster <armbru@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
18 #include "qemu/osdep.h"
19 #include "qemu/config-file.h"
20 #include "qemu/error-report.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/cpus.h"
23 #include "hw/smbios/smbios.h"
24 #include "hw/loader.h"
25 #include "exec/cpu-common.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 SmbiosEntryPointType smbios_ep_type = SMBIOS_ENTRY_POINT_21;
61 static SmbiosEntryPoint ep;
63 static int smbios_type4_count = 0;
64 static bool smbios_immutable;
65 static bool smbios_have_defaults;
66 static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
68 static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
69 static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
71 static struct {
72 const char *vendor, *version, *date;
73 bool have_major_minor, uefi;
74 uint8_t major, minor;
75 } type0;
77 static struct {
78 const char *manufacturer, *product, *version, *serial, *sku, *family;
79 /* uuid is in qemu_uuid[] */
80 } type1;
82 static struct {
83 const char *manufacturer, *product, *version, *serial, *asset, *location;
84 } type2;
86 static struct {
87 const char *manufacturer, *version, *serial, *asset, *sku;
88 } type3;
90 static struct {
91 const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
92 } type4;
94 static struct {
95 const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
96 uint16_t speed;
97 } type17;
99 static QemuOptsList qemu_smbios_opts = {
100 .name = "smbios",
101 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
102 .desc = {
104 * no elements => accept any params
105 * validation will happen later
107 { /* end of list */ }
111 static const QemuOptDesc qemu_smbios_file_opts[] = {
113 .name = "file",
114 .type = QEMU_OPT_STRING,
115 .help = "binary file containing an SMBIOS element",
117 { /* end of list */ }
120 static const QemuOptDesc qemu_smbios_type0_opts[] = {
122 .name = "type",
123 .type = QEMU_OPT_NUMBER,
124 .help = "SMBIOS element type",
126 .name = "vendor",
127 .type = QEMU_OPT_STRING,
128 .help = "vendor name",
130 .name = "version",
131 .type = QEMU_OPT_STRING,
132 .help = "version number",
134 .name = "date",
135 .type = QEMU_OPT_STRING,
136 .help = "release date",
138 .name = "release",
139 .type = QEMU_OPT_STRING,
140 .help = "revision number",
142 .name = "uefi",
143 .type = QEMU_OPT_BOOL,
144 .help = "uefi support",
146 { /* end of list */ }
149 static const QemuOptDesc qemu_smbios_type1_opts[] = {
151 .name = "type",
152 .type = QEMU_OPT_NUMBER,
153 .help = "SMBIOS element type",
155 .name = "manufacturer",
156 .type = QEMU_OPT_STRING,
157 .help = "manufacturer name",
159 .name = "product",
160 .type = QEMU_OPT_STRING,
161 .help = "product name",
163 .name = "version",
164 .type = QEMU_OPT_STRING,
165 .help = "version number",
167 .name = "serial",
168 .type = QEMU_OPT_STRING,
169 .help = "serial number",
171 .name = "uuid",
172 .type = QEMU_OPT_STRING,
173 .help = "UUID",
175 .name = "sku",
176 .type = QEMU_OPT_STRING,
177 .help = "SKU number",
179 .name = "family",
180 .type = QEMU_OPT_STRING,
181 .help = "family name",
183 { /* end of list */ }
186 static const QemuOptDesc qemu_smbios_type2_opts[] = {
188 .name = "type",
189 .type = QEMU_OPT_NUMBER,
190 .help = "SMBIOS element type",
192 .name = "manufacturer",
193 .type = QEMU_OPT_STRING,
194 .help = "manufacturer name",
196 .name = "product",
197 .type = QEMU_OPT_STRING,
198 .help = "product name",
200 .name = "version",
201 .type = QEMU_OPT_STRING,
202 .help = "version number",
204 .name = "serial",
205 .type = QEMU_OPT_STRING,
206 .help = "serial number",
208 .name = "asset",
209 .type = QEMU_OPT_STRING,
210 .help = "asset tag number",
212 .name = "location",
213 .type = QEMU_OPT_STRING,
214 .help = "location in chassis",
216 { /* end of list */ }
219 static const QemuOptDesc qemu_smbios_type3_opts[] = {
221 .name = "type",
222 .type = QEMU_OPT_NUMBER,
223 .help = "SMBIOS element type",
225 .name = "manufacturer",
226 .type = QEMU_OPT_STRING,
227 .help = "manufacturer name",
229 .name = "version",
230 .type = QEMU_OPT_STRING,
231 .help = "version number",
233 .name = "serial",
234 .type = QEMU_OPT_STRING,
235 .help = "serial number",
237 .name = "asset",
238 .type = QEMU_OPT_STRING,
239 .help = "asset tag number",
241 .name = "sku",
242 .type = QEMU_OPT_STRING,
243 .help = "SKU number",
245 { /* end of list */ }
248 static const QemuOptDesc qemu_smbios_type4_opts[] = {
250 .name = "type",
251 .type = QEMU_OPT_NUMBER,
252 .help = "SMBIOS element type",
254 .name = "sock_pfx",
255 .type = QEMU_OPT_STRING,
256 .help = "socket designation string prefix",
258 .name = "manufacturer",
259 .type = QEMU_OPT_STRING,
260 .help = "manufacturer name",
262 .name = "version",
263 .type = QEMU_OPT_STRING,
264 .help = "version number",
266 .name = "serial",
267 .type = QEMU_OPT_STRING,
268 .help = "serial number",
270 .name = "asset",
271 .type = QEMU_OPT_STRING,
272 .help = "asset tag number",
274 .name = "part",
275 .type = QEMU_OPT_STRING,
276 .help = "part number",
278 { /* end of list */ }
281 static const QemuOptDesc qemu_smbios_type17_opts[] = {
283 .name = "type",
284 .type = QEMU_OPT_NUMBER,
285 .help = "SMBIOS element type",
287 .name = "loc_pfx",
288 .type = QEMU_OPT_STRING,
289 .help = "device locator string prefix",
291 .name = "bank",
292 .type = QEMU_OPT_STRING,
293 .help = "bank locator string",
295 .name = "manufacturer",
296 .type = QEMU_OPT_STRING,
297 .help = "manufacturer name",
299 .name = "serial",
300 .type = QEMU_OPT_STRING,
301 .help = "serial number",
303 .name = "asset",
304 .type = QEMU_OPT_STRING,
305 .help = "asset tag number",
307 .name = "part",
308 .type = QEMU_OPT_STRING,
309 .help = "part number",
311 .name = "speed",
312 .type = QEMU_OPT_NUMBER,
313 .help = "maximum capable speed",
315 { /* end of list */ }
318 static void smbios_register_config(void)
320 qemu_add_opts(&qemu_smbios_opts);
323 machine_init(smbios_register_config);
325 static void smbios_validate_table(void)
327 uint32_t expect_t4_count = smbios_legacy ? smp_cpus : smbios_smp_sockets;
329 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
330 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
331 expect_t4_count, smbios_type4_count);
332 exit(1);
337 /* legacy setup functions for <= 2.0 machines */
338 static void smbios_add_field(int type, int offset, const void *data, size_t len)
340 struct smbios_field *field;
342 if (!smbios_entries) {
343 smbios_entries_len = sizeof(uint16_t);
344 smbios_entries = g_malloc0(smbios_entries_len);
346 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
347 sizeof(*field) + len);
348 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
349 field->header.type = SMBIOS_FIELD_ENTRY;
350 field->header.length = cpu_to_le16(sizeof(*field) + len);
352 field->type = type;
353 field->offset = cpu_to_le16(offset);
354 memcpy(field->data, data, len);
356 smbios_entries_len += sizeof(*field) + len;
357 (*(uint16_t *)smbios_entries) =
358 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
361 static void smbios_maybe_add_str(int type, int offset, const char *data)
363 if (data) {
364 smbios_add_field(type, offset, data, strlen(data) + 1);
368 static void smbios_build_type_0_fields(void)
370 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
371 type0.vendor);
372 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
373 type0.version);
374 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
375 bios_release_date_str),
376 type0.date);
377 if (type0.have_major_minor) {
378 smbios_add_field(0, offsetof(struct smbios_type_0,
379 system_bios_major_release),
380 &type0.major, 1);
381 smbios_add_field(0, offsetof(struct smbios_type_0,
382 system_bios_minor_release),
383 &type0.minor, 1);
387 static void smbios_build_type_1_fields(void)
389 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
390 type1.manufacturer);
391 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
392 type1.product);
393 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
394 type1.version);
395 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
396 type1.serial);
397 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
398 type1.sku);
399 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
400 type1.family);
401 if (qemu_uuid_set) {
402 /* We don't encode the UUID in the "wire format" here because this
403 * function is for legacy mode and needs to keep the guest ABI, and
404 * because we don't know what's the SMBIOS version advertised by the
405 * BIOS.
407 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
408 qemu_uuid, 16);
412 uint8_t *smbios_get_table_legacy(size_t *length)
414 if (!smbios_legacy) {
415 *length = 0;
416 return NULL;
419 if (!smbios_immutable) {
420 smbios_build_type_0_fields();
421 smbios_build_type_1_fields();
422 smbios_validate_table();
423 smbios_immutable = true;
425 *length = smbios_entries_len;
426 return smbios_entries;
428 /* end: legacy setup functions for <= 2.0 machines */
431 static bool smbios_skip_table(uint8_t type, bool required_table)
433 if (test_bit(type, have_binfile_bitmap)) {
434 return true; /* user provided their own binary blob(s) */
436 if (test_bit(type, have_fields_bitmap)) {
437 return false; /* user provided fields via command line */
439 if (smbios_have_defaults && required_table) {
440 return false; /* we're building tables, and this one's required */
442 return true;
445 #define SMBIOS_BUILD_TABLE_PRE(tbl_type, tbl_handle, tbl_required) \
446 struct smbios_type_##tbl_type *t; \
447 size_t t_off; /* table offset into smbios_tables */ \
448 int str_index = 0; \
449 do { \
450 /* should we skip building this table ? */ \
451 if (smbios_skip_table(tbl_type, tbl_required)) { \
452 return; \
455 /* use offset of table t within smbios_tables */ \
456 /* (pointer must be updated after each realloc) */ \
457 t_off = smbios_tables_len; \
458 smbios_tables_len += sizeof(*t); \
459 smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
460 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
462 t->header.type = tbl_type; \
463 t->header.length = sizeof(*t); \
464 t->header.handle = cpu_to_le16(tbl_handle); \
465 } while (0)
467 #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
468 do { \
469 int len = (value != NULL) ? strlen(value) + 1 : 0; \
470 if (len > 1) { \
471 smbios_tables = g_realloc(smbios_tables, \
472 smbios_tables_len + len); \
473 memcpy(smbios_tables + smbios_tables_len, value, len); \
474 smbios_tables_len += len; \
475 /* update pointer post-realloc */ \
476 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
477 t->field = ++str_index; \
478 } else { \
479 t->field = 0; \
481 } while (0)
483 #define SMBIOS_BUILD_TABLE_POST \
484 do { \
485 size_t term_cnt, t_size; \
487 /* add '\0' terminator (add two if no strings defined) */ \
488 term_cnt = (str_index == 0) ? 2 : 1; \
489 smbios_tables = g_realloc(smbios_tables, \
490 smbios_tables_len + term_cnt); \
491 memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
492 smbios_tables_len += term_cnt; \
494 /* update smbios max. element size */ \
495 t_size = smbios_tables_len - t_off; \
496 if (t_size > smbios_table_max) { \
497 smbios_table_max = t_size; \
500 /* update smbios element count */ \
501 smbios_table_cnt++; \
502 } while (0)
504 static void smbios_build_type_0_table(void)
506 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
508 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
509 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
511 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
513 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
515 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
517 t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
518 t->bios_characteristics_extension_bytes[0] = 0;
519 t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
520 if (type0.uefi) {
521 t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
524 if (type0.have_major_minor) {
525 t->system_bios_major_release = type0.major;
526 t->system_bios_minor_release = type0.minor;
527 } else {
528 t->system_bios_major_release = 0;
529 t->system_bios_minor_release = 0;
532 /* hardcoded in SeaBIOS */
533 t->embedded_controller_major_release = 0xFF;
534 t->embedded_controller_minor_release = 0xFF;
536 SMBIOS_BUILD_TABLE_POST;
539 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
540 * format specified by SMBIOS version 2.6.
542 static void smbios_encode_uuid(struct smbios_uuid *uuid, const uint8_t *buf)
544 memcpy(uuid, buf, 16);
545 if (smbios_uuid_encoded) {
546 uuid->time_low = bswap32(uuid->time_low);
547 uuid->time_mid = bswap16(uuid->time_mid);
548 uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
552 static void smbios_build_type_1_table(void)
554 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
556 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
557 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
558 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
559 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
560 if (qemu_uuid_set) {
561 smbios_encode_uuid(&t->uuid, qemu_uuid);
562 } else {
563 memset(&t->uuid, 0, 16);
565 t->wake_up_type = 0x06; /* power switch */
566 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
567 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
569 SMBIOS_BUILD_TABLE_POST;
572 static void smbios_build_type_2_table(void)
574 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
576 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
577 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
578 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
579 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
580 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
581 t->feature_flags = 0x01; /* Motherboard */
582 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
583 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
584 t->board_type = 0x0A; /* Motherboard */
585 t->contained_element_count = 0;
587 SMBIOS_BUILD_TABLE_POST;
590 static void smbios_build_type_3_table(void)
592 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
594 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
595 t->type = 0x01; /* Other */
596 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
597 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
598 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
599 t->boot_up_state = 0x03; /* Safe */
600 t->power_supply_state = 0x03; /* Safe */
601 t->thermal_state = 0x03; /* Safe */
602 t->security_status = 0x02; /* Unknown */
603 t->oem_defined = cpu_to_le32(0);
604 t->height = 0;
605 t->number_of_power_cords = 0;
606 t->contained_element_count = 0;
607 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
609 SMBIOS_BUILD_TABLE_POST;
612 static void smbios_build_type_4_table(unsigned instance)
614 char sock_str[128];
616 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
618 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
619 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
620 t->processor_type = 0x03; /* CPU */
621 t->processor_family = 0x01; /* Other */
622 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
623 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
624 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
625 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
626 t->voltage = 0;
627 t->external_clock = cpu_to_le16(0); /* Unknown */
628 /* SVVP requires max_speed and current_speed to not be unknown. */
629 t->max_speed = cpu_to_le16(2000); /* 2000 MHz */
630 t->current_speed = cpu_to_le16(2000); /* 2000 MHz */
631 t->status = 0x41; /* Socket populated, CPU enabled */
632 t->processor_upgrade = 0x01; /* Other */
633 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
634 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
635 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
636 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
637 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
638 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
639 t->core_count = t->core_enabled = smp_cores;
640 t->thread_count = smp_threads;
641 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
642 t->processor_family2 = cpu_to_le16(0x01); /* Other */
644 SMBIOS_BUILD_TABLE_POST;
645 smbios_type4_count++;
648 #define ONE_KB ((ram_addr_t)1 << 10)
649 #define ONE_MB ((ram_addr_t)1 << 20)
650 #define ONE_GB ((ram_addr_t)1 << 30)
652 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
654 static void smbios_build_type_16_table(unsigned dimm_cnt)
656 uint64_t size_kb;
658 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
660 t->location = 0x01; /* Other */
661 t->use = 0x03; /* System memory */
662 t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
663 size_kb = QEMU_ALIGN_UP(ram_size, ONE_KB) / ONE_KB;
664 if (size_kb < MAX_T16_STD_SZ) {
665 t->maximum_capacity = cpu_to_le32(size_kb);
666 t->extended_maximum_capacity = cpu_to_le64(0);
667 } else {
668 t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
669 t->extended_maximum_capacity = cpu_to_le64(ram_size);
671 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
672 t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
674 SMBIOS_BUILD_TABLE_POST;
677 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
678 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
680 static void smbios_build_type_17_table(unsigned instance, uint64_t size)
682 char loc_str[128];
683 uint64_t size_mb;
685 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
687 t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
688 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
689 t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
690 t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
691 size_mb = QEMU_ALIGN_UP(size, ONE_MB) / ONE_MB;
692 if (size_mb < MAX_T17_STD_SZ) {
693 t->size = cpu_to_le16(size_mb);
694 t->extended_size = cpu_to_le32(0);
695 } else {
696 assert(size_mb < MAX_T17_EXT_SZ);
697 t->size = cpu_to_le16(MAX_T17_STD_SZ);
698 t->extended_size = cpu_to_le32(size_mb);
700 t->form_factor = 0x09; /* DIMM */
701 t->device_set = 0; /* Not in a set */
702 snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
703 SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
704 SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
705 t->memory_type = 0x07; /* RAM */
706 t->type_detail = cpu_to_le16(0x02); /* Other */
707 t->speed = cpu_to_le16(type17.speed);
708 SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
709 SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
710 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
711 SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
712 t->attributes = 0; /* Unknown */
713 t->configured_clock_speed = t->speed; /* reuse value for max speed */
714 t->minimum_voltage = cpu_to_le16(0); /* Unknown */
715 t->maximum_voltage = cpu_to_le16(0); /* Unknown */
716 t->configured_voltage = cpu_to_le16(0); /* Unknown */
718 SMBIOS_BUILD_TABLE_POST;
721 static void smbios_build_type_19_table(unsigned instance,
722 uint64_t start, uint64_t size)
724 uint64_t end, start_kb, end_kb;
726 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
728 end = start + size - 1;
729 assert(end > start);
730 start_kb = start / ONE_KB;
731 end_kb = end / ONE_KB;
732 if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
733 t->starting_address = cpu_to_le32(start_kb);
734 t->ending_address = cpu_to_le32(end_kb);
735 t->extended_starting_address =
736 t->extended_ending_address = cpu_to_le64(0);
737 } else {
738 t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
739 t->extended_starting_address = cpu_to_le64(start);
740 t->extended_ending_address = cpu_to_le64(end);
742 t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
743 t->partition_width = 1; /* One device per row */
745 SMBIOS_BUILD_TABLE_POST;
748 static void smbios_build_type_32_table(void)
750 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
752 memset(t->reserved, 0, 6);
753 t->boot_status = 0; /* No errors detected */
755 SMBIOS_BUILD_TABLE_POST;
758 static void smbios_build_type_127_table(void)
760 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
761 SMBIOS_BUILD_TABLE_POST;
764 void smbios_set_cpuid(uint32_t version, uint32_t features)
766 smbios_cpuid_version = version;
767 smbios_cpuid_features = features;
770 #define SMBIOS_SET_DEFAULT(field, value) \
771 if (!field) { \
772 field = value; \
775 void smbios_set_defaults(const char *manufacturer, const char *product,
776 const char *version, bool legacy_mode,
777 bool uuid_encoded, SmbiosEntryPointType ep_type)
779 smbios_have_defaults = true;
780 smbios_legacy = legacy_mode;
781 smbios_uuid_encoded = uuid_encoded;
782 smbios_ep_type = ep_type;
784 /* drop unwanted version of command-line file blob(s) */
785 if (smbios_legacy) {
786 g_free(smbios_tables);
787 /* in legacy mode, also complain if fields were given for types > 1 */
788 if (find_next_bit(have_fields_bitmap,
789 SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
790 error_report("can't process fields for smbios "
791 "types > 1 on machine versions < 2.1!");
792 exit(1);
794 } else {
795 g_free(smbios_entries);
798 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
799 SMBIOS_SET_DEFAULT(type1.product, product);
800 SMBIOS_SET_DEFAULT(type1.version, version);
801 SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
802 SMBIOS_SET_DEFAULT(type2.product, product);
803 SMBIOS_SET_DEFAULT(type2.version, version);
804 SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
805 SMBIOS_SET_DEFAULT(type3.version, version);
806 SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
807 SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
808 SMBIOS_SET_DEFAULT(type4.version, version);
809 SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
810 SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
813 static void smbios_entry_point_setup(void)
815 switch (smbios_ep_type) {
816 case SMBIOS_ENTRY_POINT_21:
817 memcpy(ep.ep21.anchor_string, "_SM_", 4);
818 memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
819 ep.ep21.length = sizeof(struct smbios_21_entry_point);
820 ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
821 memset(ep.ep21.formatted_area, 0, 5);
823 /* compliant with smbios spec v2.8 */
824 ep.ep21.smbios_major_version = 2;
825 ep.ep21.smbios_minor_version = 8;
826 ep.ep21.smbios_bcd_revision = 0x28;
828 /* set during table construction, but BIOS may override: */
829 ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
830 ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
831 ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
833 /* BIOS must recalculate */
834 ep.ep21.checksum = 0;
835 ep.ep21.intermediate_checksum = 0;
836 ep.ep21.structure_table_address = cpu_to_le32(0);
838 break;
839 case SMBIOS_ENTRY_POINT_30:
840 memcpy(ep.ep30.anchor_string, "_SM3_", 5);
841 ep.ep30.length = sizeof(struct smbios_30_entry_point);
842 ep.ep30.entry_point_revision = 1;
843 ep.ep30.reserved = 0;
845 /* compliant with smbios spec 3.0 */
846 ep.ep30.smbios_major_version = 3;
847 ep.ep30.smbios_minor_version = 0;
848 ep.ep30.smbios_doc_rev = 0;
850 /* set during table construct, but BIOS might override */
851 ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
853 /* BIOS must recalculate */
854 ep.ep30.checksum = 0;
855 ep.ep30.structure_table_address = cpu_to_le64(0);
857 break;
858 default:
859 abort();
860 break;
864 void smbios_get_tables(const struct smbios_phys_mem_area *mem_array,
865 const unsigned int mem_array_size,
866 uint8_t **tables, size_t *tables_len,
867 uint8_t **anchor, size_t *anchor_len)
869 unsigned i, dimm_cnt;
871 if (smbios_legacy) {
872 *tables = *anchor = NULL;
873 *tables_len = *anchor_len = 0;
874 return;
877 if (!smbios_immutable) {
878 smbios_build_type_0_table();
879 smbios_build_type_1_table();
880 smbios_build_type_2_table();
881 smbios_build_type_3_table();
883 smbios_smp_sockets = DIV_ROUND_UP(smp_cpus, smp_cores * smp_threads);
884 assert(smbios_smp_sockets >= 1);
886 for (i = 0; i < smbios_smp_sockets; i++) {
887 smbios_build_type_4_table(i);
890 #define MAX_DIMM_SZ (16ll * ONE_GB)
891 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
892 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
894 dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
896 smbios_build_type_16_table(dimm_cnt);
898 for (i = 0; i < dimm_cnt; i++) {
899 smbios_build_type_17_table(i, GET_DIMM_SZ);
902 for (i = 0; i < mem_array_size; i++) {
903 smbios_build_type_19_table(i, mem_array[i].address,
904 mem_array[i].length);
907 smbios_build_type_32_table();
908 smbios_build_type_127_table();
910 smbios_validate_table();
911 smbios_entry_point_setup();
912 smbios_immutable = true;
915 /* return tables blob and entry point (anchor), and their sizes */
916 *tables = smbios_tables;
917 *tables_len = smbios_tables_len;
918 *anchor = (uint8_t *)&ep;
920 /* calculate length based on anchor string */
921 if (!strncmp((char *)&ep, "_SM_", 4)) {
922 *anchor_len = sizeof(struct smbios_21_entry_point);
923 } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
924 *anchor_len = sizeof(struct smbios_30_entry_point);
925 } else {
926 abort();
930 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
932 const char *val = qemu_opt_get(opts, name);
934 if (val) {
935 *dest = val;
939 void smbios_entry_add(QemuOpts *opts)
941 const char *val;
943 assert(!smbios_immutable);
945 val = qemu_opt_get(opts, "file");
946 if (val) {
947 struct smbios_structure_header *header;
948 int size;
949 struct smbios_table *table; /* legacy mode only */
951 qemu_opts_validate(opts, qemu_smbios_file_opts, &error_fatal);
953 size = get_image_size(val);
954 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
955 error_report("Cannot read SMBIOS file %s", val);
956 exit(1);
960 * NOTE: standard double '\0' terminator expected, per smbios spec.
961 * (except in legacy mode, where the second '\0' is implicit and
962 * will be inserted by the BIOS).
964 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
965 header = (struct smbios_structure_header *)(smbios_tables +
966 smbios_tables_len);
968 if (load_image(val, (uint8_t *)header) != size) {
969 error_report("Failed to load SMBIOS file %s", val);
970 exit(1);
973 if (test_bit(header->type, have_fields_bitmap)) {
974 error_report("can't load type %d struct, fields already specified!",
975 header->type);
976 exit(1);
978 set_bit(header->type, have_binfile_bitmap);
980 if (header->type == 4) {
981 smbios_type4_count++;
984 smbios_tables_len += size;
985 if (size > smbios_table_max) {
986 smbios_table_max = size;
988 smbios_table_cnt++;
990 /* add a copy of the newly loaded blob to legacy smbios_entries */
991 /* NOTE: This code runs before smbios_set_defaults(), so we don't
992 * yet know which mode (legacy vs. aggregate-table) will be
993 * required. We therefore add the binary blob to both legacy
994 * (smbios_entries) and aggregate (smbios_tables) tables, and
995 * delete the one we don't need from smbios_set_defaults(),
996 * once we know which machine version has been requested.
998 if (!smbios_entries) {
999 smbios_entries_len = sizeof(uint16_t);
1000 smbios_entries = g_malloc0(smbios_entries_len);
1002 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
1003 size + sizeof(*table));
1004 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
1005 table->header.type = SMBIOS_TABLE_ENTRY;
1006 table->header.length = cpu_to_le16(sizeof(*table) + size);
1007 memcpy(table->data, header, size);
1008 smbios_entries_len += sizeof(*table) + size;
1009 (*(uint16_t *)smbios_entries) =
1010 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
1011 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1013 return;
1016 val = qemu_opt_get(opts, "type");
1017 if (val) {
1018 unsigned long type = strtoul(val, NULL, 0);
1020 if (type > SMBIOS_MAX_TYPE) {
1021 error_report("out of range!");
1022 exit(1);
1025 if (test_bit(type, have_binfile_bitmap)) {
1026 error_report("can't add fields, binary file already loaded!");
1027 exit(1);
1029 set_bit(type, have_fields_bitmap);
1031 switch (type) {
1032 case 0:
1033 qemu_opts_validate(opts, qemu_smbios_type0_opts, &error_fatal);
1034 save_opt(&type0.vendor, opts, "vendor");
1035 save_opt(&type0.version, opts, "version");
1036 save_opt(&type0.date, opts, "date");
1037 type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1039 val = qemu_opt_get(opts, "release");
1040 if (val) {
1041 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1042 error_report("Invalid release");
1043 exit(1);
1045 type0.have_major_minor = true;
1047 return;
1048 case 1:
1049 qemu_opts_validate(opts, qemu_smbios_type1_opts, &error_fatal);
1050 save_opt(&type1.manufacturer, opts, "manufacturer");
1051 save_opt(&type1.product, opts, "product");
1052 save_opt(&type1.version, opts, "version");
1053 save_opt(&type1.serial, opts, "serial");
1054 save_opt(&type1.sku, opts, "sku");
1055 save_opt(&type1.family, opts, "family");
1057 val = qemu_opt_get(opts, "uuid");
1058 if (val) {
1059 if (qemu_uuid_parse(val, qemu_uuid) != 0) {
1060 error_report("Invalid UUID");
1061 exit(1);
1063 qemu_uuid_set = true;
1065 return;
1066 case 2:
1067 qemu_opts_validate(opts, qemu_smbios_type2_opts, &error_fatal);
1068 save_opt(&type2.manufacturer, opts, "manufacturer");
1069 save_opt(&type2.product, opts, "product");
1070 save_opt(&type2.version, opts, "version");
1071 save_opt(&type2.serial, opts, "serial");
1072 save_opt(&type2.asset, opts, "asset");
1073 save_opt(&type2.location, opts, "location");
1074 return;
1075 case 3:
1076 qemu_opts_validate(opts, qemu_smbios_type3_opts, &error_fatal);
1077 save_opt(&type3.manufacturer, opts, "manufacturer");
1078 save_opt(&type3.version, opts, "version");
1079 save_opt(&type3.serial, opts, "serial");
1080 save_opt(&type3.asset, opts, "asset");
1081 save_opt(&type3.sku, opts, "sku");
1082 return;
1083 case 4:
1084 qemu_opts_validate(opts, qemu_smbios_type4_opts, &error_fatal);
1085 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1086 save_opt(&type4.manufacturer, opts, "manufacturer");
1087 save_opt(&type4.version, opts, "version");
1088 save_opt(&type4.serial, opts, "serial");
1089 save_opt(&type4.asset, opts, "asset");
1090 save_opt(&type4.part, opts, "part");
1091 return;
1092 case 17:
1093 qemu_opts_validate(opts, qemu_smbios_type17_opts, &error_fatal);
1094 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1095 save_opt(&type17.bank, opts, "bank");
1096 save_opt(&type17.manufacturer, opts, "manufacturer");
1097 save_opt(&type17.serial, opts, "serial");
1098 save_opt(&type17.asset, opts, "asset");
1099 save_opt(&type17.part, opts, "part");
1100 type17.speed = qemu_opt_get_number(opts, "speed", 0);
1101 return;
1102 default:
1103 error_report("Don't know how to build fields for SMBIOS type %ld",
1104 type);
1105 exit(1);
1109 error_report("Must specify type= or file=");
1110 exit(1);