smbios: remove dependency on x86 e820 tables
[qemu/ar7.git] / hw / i386 / smbios.c
blob6f715c641b6e7b54e17344b30178e5b94dd6f2c5
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 uint16_t speed;
95 } type17;
97 static QemuOptsList qemu_smbios_opts = {
98 .name = "smbios",
99 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
100 .desc = {
102 * no elements => accept any params
103 * validation will happen later
105 { /* end of list */ }
109 static const QemuOptDesc qemu_smbios_file_opts[] = {
111 .name = "file",
112 .type = QEMU_OPT_STRING,
113 .help = "binary file containing an SMBIOS element",
115 { /* end of list */ }
118 static const QemuOptDesc qemu_smbios_type0_opts[] = {
120 .name = "type",
121 .type = QEMU_OPT_NUMBER,
122 .help = "SMBIOS element type",
124 .name = "vendor",
125 .type = QEMU_OPT_STRING,
126 .help = "vendor name",
128 .name = "version",
129 .type = QEMU_OPT_STRING,
130 .help = "version number",
132 .name = "date",
133 .type = QEMU_OPT_STRING,
134 .help = "release date",
136 .name = "release",
137 .type = QEMU_OPT_STRING,
138 .help = "revision number",
140 .name = "uefi",
141 .type = QEMU_OPT_BOOL,
142 .help = "uefi support",
144 { /* end of list */ }
147 static const QemuOptDesc qemu_smbios_type1_opts[] = {
149 .name = "type",
150 .type = QEMU_OPT_NUMBER,
151 .help = "SMBIOS element type",
153 .name = "manufacturer",
154 .type = QEMU_OPT_STRING,
155 .help = "manufacturer name",
157 .name = "product",
158 .type = QEMU_OPT_STRING,
159 .help = "product name",
161 .name = "version",
162 .type = QEMU_OPT_STRING,
163 .help = "version number",
165 .name = "serial",
166 .type = QEMU_OPT_STRING,
167 .help = "serial number",
169 .name = "uuid",
170 .type = QEMU_OPT_STRING,
171 .help = "UUID",
173 .name = "sku",
174 .type = QEMU_OPT_STRING,
175 .help = "SKU number",
177 .name = "family",
178 .type = QEMU_OPT_STRING,
179 .help = "family name",
181 { /* end of list */ }
184 static const QemuOptDesc qemu_smbios_type2_opts[] = {
186 .name = "type",
187 .type = QEMU_OPT_NUMBER,
188 .help = "SMBIOS element type",
190 .name = "manufacturer",
191 .type = QEMU_OPT_STRING,
192 .help = "manufacturer name",
194 .name = "product",
195 .type = QEMU_OPT_STRING,
196 .help = "product name",
198 .name = "version",
199 .type = QEMU_OPT_STRING,
200 .help = "version number",
202 .name = "serial",
203 .type = QEMU_OPT_STRING,
204 .help = "serial number",
206 .name = "asset",
207 .type = QEMU_OPT_STRING,
208 .help = "asset tag number",
210 .name = "location",
211 .type = QEMU_OPT_STRING,
212 .help = "location in chassis",
214 { /* end of list */ }
217 static const QemuOptDesc qemu_smbios_type3_opts[] = {
219 .name = "type",
220 .type = QEMU_OPT_NUMBER,
221 .help = "SMBIOS element type",
223 .name = "manufacturer",
224 .type = QEMU_OPT_STRING,
225 .help = "manufacturer name",
227 .name = "version",
228 .type = QEMU_OPT_STRING,
229 .help = "version number",
231 .name = "serial",
232 .type = QEMU_OPT_STRING,
233 .help = "serial number",
235 .name = "asset",
236 .type = QEMU_OPT_STRING,
237 .help = "asset tag number",
239 .name = "sku",
240 .type = QEMU_OPT_STRING,
241 .help = "SKU number",
243 { /* end of list */ }
246 static const QemuOptDesc qemu_smbios_type4_opts[] = {
248 .name = "type",
249 .type = QEMU_OPT_NUMBER,
250 .help = "SMBIOS element type",
252 .name = "sock_pfx",
253 .type = QEMU_OPT_STRING,
254 .help = "socket designation string prefix",
256 .name = "manufacturer",
257 .type = QEMU_OPT_STRING,
258 .help = "manufacturer name",
260 .name = "version",
261 .type = QEMU_OPT_STRING,
262 .help = "version number",
264 .name = "serial",
265 .type = QEMU_OPT_STRING,
266 .help = "serial number",
268 .name = "asset",
269 .type = QEMU_OPT_STRING,
270 .help = "asset tag number",
272 .name = "part",
273 .type = QEMU_OPT_STRING,
274 .help = "part number",
276 { /* end of list */ }
279 static const QemuOptDesc qemu_smbios_type17_opts[] = {
281 .name = "type",
282 .type = QEMU_OPT_NUMBER,
283 .help = "SMBIOS element type",
285 .name = "loc_pfx",
286 .type = QEMU_OPT_STRING,
287 .help = "device locator string prefix",
289 .name = "bank",
290 .type = QEMU_OPT_STRING,
291 .help = "bank locator string",
293 .name = "manufacturer",
294 .type = QEMU_OPT_STRING,
295 .help = "manufacturer name",
297 .name = "serial",
298 .type = QEMU_OPT_STRING,
299 .help = "serial number",
301 .name = "asset",
302 .type = QEMU_OPT_STRING,
303 .help = "asset tag number",
305 .name = "part",
306 .type = QEMU_OPT_STRING,
307 .help = "part number",
309 .name = "speed",
310 .type = QEMU_OPT_NUMBER,
311 .help = "maximum capable speed",
313 { /* end of list */ }
316 static void smbios_register_config(void)
318 qemu_add_opts(&qemu_smbios_opts);
321 machine_init(smbios_register_config);
323 static void smbios_validate_table(void)
325 uint32_t expect_t4_count = smbios_legacy ? smp_cpus : smbios_smp_sockets;
327 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
328 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
329 expect_t4_count, smbios_type4_count);
330 exit(1);
335 /* legacy setup functions for <= 2.0 machines */
336 static void smbios_add_field(int type, int offset, const void *data, size_t len)
338 struct smbios_field *field;
340 if (!smbios_entries) {
341 smbios_entries_len = sizeof(uint16_t);
342 smbios_entries = g_malloc0(smbios_entries_len);
344 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
345 sizeof(*field) + len);
346 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
347 field->header.type = SMBIOS_FIELD_ENTRY;
348 field->header.length = cpu_to_le16(sizeof(*field) + len);
350 field->type = type;
351 field->offset = cpu_to_le16(offset);
352 memcpy(field->data, data, len);
354 smbios_entries_len += sizeof(*field) + len;
355 (*(uint16_t *)smbios_entries) =
356 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
359 static void smbios_maybe_add_str(int type, int offset, const char *data)
361 if (data) {
362 smbios_add_field(type, offset, data, strlen(data) + 1);
366 static void smbios_build_type_0_fields(void)
368 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
369 type0.vendor);
370 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
371 type0.version);
372 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
373 bios_release_date_str),
374 type0.date);
375 if (type0.have_major_minor) {
376 smbios_add_field(0, offsetof(struct smbios_type_0,
377 system_bios_major_release),
378 &type0.major, 1);
379 smbios_add_field(0, offsetof(struct smbios_type_0,
380 system_bios_minor_release),
381 &type0.minor, 1);
385 static void smbios_build_type_1_fields(void)
387 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
388 type1.manufacturer);
389 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
390 type1.product);
391 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
392 type1.version);
393 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
394 type1.serial);
395 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
396 type1.sku);
397 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
398 type1.family);
399 if (qemu_uuid_set) {
400 /* We don't encode the UUID in the "wire format" here because this
401 * function is for legacy mode and needs to keep the guest ABI, and
402 * because we don't know what's the SMBIOS version advertised by the
403 * BIOS.
405 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
406 qemu_uuid, 16);
410 uint8_t *smbios_get_table_legacy(size_t *length)
412 if (!smbios_legacy) {
413 *length = 0;
414 return NULL;
417 if (!smbios_immutable) {
418 smbios_build_type_0_fields();
419 smbios_build_type_1_fields();
420 smbios_validate_table();
421 smbios_immutable = true;
423 *length = smbios_entries_len;
424 return smbios_entries;
426 /* end: legacy setup functions for <= 2.0 machines */
429 static bool smbios_skip_table(uint8_t type, bool required_table)
431 if (test_bit(type, have_binfile_bitmap)) {
432 return true; /* user provided their own binary blob(s) */
434 if (test_bit(type, have_fields_bitmap)) {
435 return false; /* user provided fields via command line */
437 if (smbios_have_defaults && required_table) {
438 return false; /* we're building tables, and this one's required */
440 return true;
443 #define SMBIOS_BUILD_TABLE_PRE(tbl_type, tbl_handle, tbl_required) \
444 struct smbios_type_##tbl_type *t; \
445 size_t t_off; /* table offset into smbios_tables */ \
446 int str_index = 0; \
447 do { \
448 /* should we skip building this table ? */ \
449 if (smbios_skip_table(tbl_type, tbl_required)) { \
450 return; \
453 /* use offset of table t within smbios_tables */ \
454 /* (pointer must be updated after each realloc) */ \
455 t_off = smbios_tables_len; \
456 smbios_tables_len += sizeof(*t); \
457 smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
458 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
460 t->header.type = tbl_type; \
461 t->header.length = sizeof(*t); \
462 t->header.handle = cpu_to_le16(tbl_handle); \
463 } while (0)
465 #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
466 do { \
467 int len = (value != NULL) ? strlen(value) + 1 : 0; \
468 if (len > 1) { \
469 smbios_tables = g_realloc(smbios_tables, \
470 smbios_tables_len + len); \
471 memcpy(smbios_tables + smbios_tables_len, value, len); \
472 smbios_tables_len += len; \
473 /* update pointer post-realloc */ \
474 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
475 t->field = ++str_index; \
476 } else { \
477 t->field = 0; \
479 } while (0)
481 #define SMBIOS_BUILD_TABLE_POST \
482 do { \
483 size_t term_cnt, t_size; \
485 /* add '\0' terminator (add two if no strings defined) */ \
486 term_cnt = (str_index == 0) ? 2 : 1; \
487 smbios_tables = g_realloc(smbios_tables, \
488 smbios_tables_len + term_cnt); \
489 memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
490 smbios_tables_len += term_cnt; \
492 /* update smbios max. element size */ \
493 t_size = smbios_tables_len - t_off; \
494 if (t_size > smbios_table_max) { \
495 smbios_table_max = t_size; \
498 /* update smbios element count */ \
499 smbios_table_cnt++; \
500 } while (0)
502 static void smbios_build_type_0_table(void)
504 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
506 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
507 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
509 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
511 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
513 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
515 t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
516 t->bios_characteristics_extension_bytes[0] = 0;
517 t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
518 if (type0.uefi) {
519 t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
522 if (type0.have_major_minor) {
523 t->system_bios_major_release = type0.major;
524 t->system_bios_minor_release = type0.minor;
525 } else {
526 t->system_bios_major_release = 0;
527 t->system_bios_minor_release = 0;
530 /* hardcoded in SeaBIOS */
531 t->embedded_controller_major_release = 0xFF;
532 t->embedded_controller_minor_release = 0xFF;
534 SMBIOS_BUILD_TABLE_POST;
537 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
538 * format specified by SMBIOS version 2.6.
540 static void smbios_encode_uuid(struct smbios_uuid *uuid, const uint8_t *buf)
542 memcpy(uuid, buf, 16);
543 if (smbios_uuid_encoded) {
544 uuid->time_low = bswap32(uuid->time_low);
545 uuid->time_mid = bswap16(uuid->time_mid);
546 uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
550 static void smbios_build_type_1_table(void)
552 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
554 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
555 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
556 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
557 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
558 if (qemu_uuid_set) {
559 smbios_encode_uuid(&t->uuid, qemu_uuid);
560 } else {
561 memset(&t->uuid, 0, 16);
563 t->wake_up_type = 0x06; /* power switch */
564 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
565 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
567 SMBIOS_BUILD_TABLE_POST;
570 static void smbios_build_type_2_table(void)
572 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
574 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
575 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
576 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
577 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
578 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
579 t->feature_flags = 0x01; /* Motherboard */
580 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
581 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
582 t->board_type = 0x0A; /* Motherboard */
583 t->contained_element_count = 0;
585 SMBIOS_BUILD_TABLE_POST;
588 static void smbios_build_type_3_table(void)
590 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
592 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
593 t->type = 0x01; /* Other */
594 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
595 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
596 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
597 t->boot_up_state = 0x03; /* Safe */
598 t->power_supply_state = 0x03; /* Safe */
599 t->thermal_state = 0x03; /* Safe */
600 t->security_status = 0x02; /* Unknown */
601 t->oem_defined = cpu_to_le32(0);
602 t->height = 0;
603 t->number_of_power_cords = 0;
604 t->contained_element_count = 0;
605 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
607 SMBIOS_BUILD_TABLE_POST;
610 static void smbios_build_type_4_table(unsigned instance)
612 char sock_str[128];
614 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
616 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
617 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
618 t->processor_type = 0x03; /* CPU */
619 t->processor_family = 0x01; /* Other */
620 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
621 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
622 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
623 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
624 t->voltage = 0;
625 t->external_clock = cpu_to_le16(0); /* Unknown */
626 /* SVVP requires max_speed and current_speed to not be unknown. */
627 t->max_speed = cpu_to_le16(2000); /* 2000 MHz */
628 t->current_speed = cpu_to_le16(2000); /* 2000 MHz */
629 t->status = 0x41; /* Socket populated, CPU enabled */
630 t->processor_upgrade = 0x01; /* Other */
631 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
632 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
633 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
634 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
635 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
636 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
637 t->core_count = t->core_enabled = smp_cores;
638 t->thread_count = smp_threads;
639 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
640 t->processor_family2 = cpu_to_le16(0x01); /* Other */
642 SMBIOS_BUILD_TABLE_POST;
643 smbios_type4_count++;
646 #define ONE_KB ((ram_addr_t)1 << 10)
647 #define ONE_MB ((ram_addr_t)1 << 20)
648 #define ONE_GB ((ram_addr_t)1 << 30)
650 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
652 static void smbios_build_type_16_table(unsigned dimm_cnt)
654 uint64_t size_kb;
656 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
658 t->location = 0x01; /* Other */
659 t->use = 0x03; /* System memory */
660 t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
661 size_kb = QEMU_ALIGN_UP(ram_size, ONE_KB) / ONE_KB;
662 if (size_kb < MAX_T16_STD_SZ) {
663 t->maximum_capacity = cpu_to_le32(size_kb);
664 t->extended_maximum_capacity = cpu_to_le64(0);
665 } else {
666 t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
667 t->extended_maximum_capacity = cpu_to_le64(ram_size);
669 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
670 t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
672 SMBIOS_BUILD_TABLE_POST;
675 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
676 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
678 static void smbios_build_type_17_table(unsigned instance, uint64_t size)
680 char loc_str[128];
681 uint64_t size_mb;
683 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
685 t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
686 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
687 t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
688 t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
689 size_mb = QEMU_ALIGN_UP(size, ONE_MB) / ONE_MB;
690 if (size_mb < MAX_T17_STD_SZ) {
691 t->size = cpu_to_le16(size_mb);
692 t->extended_size = cpu_to_le32(0);
693 } else {
694 assert(size_mb < MAX_T17_EXT_SZ);
695 t->size = cpu_to_le16(MAX_T17_STD_SZ);
696 t->extended_size = cpu_to_le32(size_mb);
698 t->form_factor = 0x09; /* DIMM */
699 t->device_set = 0; /* Not in a set */
700 snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
701 SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
702 SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
703 t->memory_type = 0x07; /* RAM */
704 t->type_detail = cpu_to_le16(0x02); /* Other */
705 t->speed = cpu_to_le16(type17.speed);
706 SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
707 SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
708 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
709 SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
710 t->attributes = 0; /* Unknown */
711 t->configured_clock_speed = t->speed; /* reuse value for max speed */
712 t->minimum_voltage = cpu_to_le16(0); /* Unknown */
713 t->maximum_voltage = cpu_to_le16(0); /* Unknown */
714 t->configured_voltage = cpu_to_le16(0); /* Unknown */
716 SMBIOS_BUILD_TABLE_POST;
719 static void smbios_build_type_19_table(unsigned instance,
720 uint64_t start, uint64_t size)
722 uint64_t end, start_kb, end_kb;
724 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
726 end = start + size - 1;
727 assert(end > start);
728 start_kb = start / ONE_KB;
729 end_kb = end / ONE_KB;
730 if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
731 t->starting_address = cpu_to_le32(start_kb);
732 t->ending_address = cpu_to_le32(end_kb);
733 t->extended_starting_address =
734 t->extended_ending_address = cpu_to_le64(0);
735 } else {
736 t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
737 t->extended_starting_address = cpu_to_le64(start);
738 t->extended_ending_address = cpu_to_le64(end);
740 t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
741 t->partition_width = 1; /* One device per row */
743 SMBIOS_BUILD_TABLE_POST;
746 static void smbios_build_type_32_table(void)
748 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
750 memset(t->reserved, 0, 6);
751 t->boot_status = 0; /* No errors detected */
753 SMBIOS_BUILD_TABLE_POST;
756 static void smbios_build_type_127_table(void)
758 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
759 SMBIOS_BUILD_TABLE_POST;
762 void smbios_set_cpuid(uint32_t version, uint32_t features)
764 smbios_cpuid_version = version;
765 smbios_cpuid_features = features;
768 #define SMBIOS_SET_DEFAULT(field, value) \
769 if (!field) { \
770 field = value; \
773 void smbios_set_defaults(const char *manufacturer, const char *product,
774 const char *version, bool legacy_mode,
775 bool uuid_encoded)
777 smbios_have_defaults = true;
778 smbios_legacy = legacy_mode;
779 smbios_uuid_encoded = uuid_encoded;
781 /* drop unwanted version of command-line file blob(s) */
782 if (smbios_legacy) {
783 g_free(smbios_tables);
784 /* in legacy mode, also complain if fields were given for types > 1 */
785 if (find_next_bit(have_fields_bitmap,
786 SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
787 error_report("can't process fields for smbios "
788 "types > 1 on machine versions < 2.1!");
789 exit(1);
791 } else {
792 g_free(smbios_entries);
795 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
796 SMBIOS_SET_DEFAULT(type1.product, product);
797 SMBIOS_SET_DEFAULT(type1.version, version);
798 SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
799 SMBIOS_SET_DEFAULT(type2.product, product);
800 SMBIOS_SET_DEFAULT(type2.version, version);
801 SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
802 SMBIOS_SET_DEFAULT(type3.version, version);
803 SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
804 SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
805 SMBIOS_SET_DEFAULT(type4.version, version);
806 SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
807 SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
810 static void smbios_entry_point_setup(void)
812 memcpy(ep.anchor_string, "_SM_", 4);
813 memcpy(ep.intermediate_anchor_string, "_DMI_", 5);
814 ep.length = sizeof(struct smbios_entry_point);
815 ep.entry_point_revision = 0; /* formatted_area reserved, per spec v2.1+ */
816 memset(ep.formatted_area, 0, 5);
818 /* compliant with smbios spec v2.8 */
819 ep.smbios_major_version = 2;
820 ep.smbios_minor_version = 8;
821 ep.smbios_bcd_revision = 0x28;
823 /* set during table construction, but BIOS may override: */
824 ep.structure_table_length = cpu_to_le16(smbios_tables_len);
825 ep.max_structure_size = cpu_to_le16(smbios_table_max);
826 ep.number_of_structures = cpu_to_le16(smbios_table_cnt);
828 /* BIOS must recalculate: */
829 ep.checksum = 0;
830 ep.intermediate_checksum = 0;
831 ep.structure_table_address = cpu_to_le32(0);
834 void smbios_get_tables(const struct smbios_phys_mem_area *mem_array,
835 const unsigned int mem_array_size,
836 uint8_t **tables, size_t *tables_len,
837 uint8_t **anchor, size_t *anchor_len)
839 unsigned i, dimm_cnt;
841 if (smbios_legacy) {
842 *tables = *anchor = NULL;
843 *tables_len = *anchor_len = 0;
844 return;
847 if (!smbios_immutable) {
848 smbios_build_type_0_table();
849 smbios_build_type_1_table();
850 smbios_build_type_2_table();
851 smbios_build_type_3_table();
853 smbios_smp_sockets = DIV_ROUND_UP(smp_cpus, smp_cores * smp_threads);
854 assert(smbios_smp_sockets >= 1);
856 for (i = 0; i < smbios_smp_sockets; i++) {
857 smbios_build_type_4_table(i);
860 #define MAX_DIMM_SZ (16ll * ONE_GB)
861 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
862 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
864 dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
866 smbios_build_type_16_table(dimm_cnt);
868 for (i = 0; i < dimm_cnt; i++) {
869 smbios_build_type_17_table(i, GET_DIMM_SZ);
872 for (i = 0; i < mem_array_size; i++) {
873 smbios_build_type_19_table(i, mem_array[i].address,
874 mem_array[i].length);
877 smbios_build_type_32_table();
878 smbios_build_type_127_table();
880 smbios_validate_table();
881 smbios_entry_point_setup();
882 smbios_immutable = true;
885 /* return tables blob and entry point (anchor), and their sizes */
886 *tables = smbios_tables;
887 *tables_len = smbios_tables_len;
888 *anchor = (uint8_t *)&ep;
889 *anchor_len = sizeof(struct smbios_entry_point);
892 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
894 const char *val = qemu_opt_get(opts, name);
896 if (val) {
897 *dest = val;
901 void smbios_entry_add(QemuOpts *opts)
903 Error *local_err = NULL;
904 const char *val;
906 assert(!smbios_immutable);
908 val = qemu_opt_get(opts, "file");
909 if (val) {
910 struct smbios_structure_header *header;
911 int size;
912 struct smbios_table *table; /* legacy mode only */
914 qemu_opts_validate(opts, qemu_smbios_file_opts, &local_err);
915 if (local_err) {
916 error_report_err(local_err);
917 exit(1);
920 size = get_image_size(val);
921 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
922 error_report("Cannot read SMBIOS file %s", val);
923 exit(1);
927 * NOTE: standard double '\0' terminator expected, per smbios spec.
928 * (except in legacy mode, where the second '\0' is implicit and
929 * will be inserted by the BIOS).
931 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
932 header = (struct smbios_structure_header *)(smbios_tables +
933 smbios_tables_len);
935 if (load_image(val, (uint8_t *)header) != size) {
936 error_report("Failed to load SMBIOS file %s", val);
937 exit(1);
940 if (test_bit(header->type, have_fields_bitmap)) {
941 error_report("can't load type %d struct, fields already specified!",
942 header->type);
943 exit(1);
945 set_bit(header->type, have_binfile_bitmap);
947 if (header->type == 4) {
948 smbios_type4_count++;
951 smbios_tables_len += size;
952 if (size > smbios_table_max) {
953 smbios_table_max = size;
955 smbios_table_cnt++;
957 /* add a copy of the newly loaded blob to legacy smbios_entries */
958 /* NOTE: This code runs before smbios_set_defaults(), so we don't
959 * yet know which mode (legacy vs. aggregate-table) will be
960 * required. We therefore add the binary blob to both legacy
961 * (smbios_entries) and aggregate (smbios_tables) tables, and
962 * delete the one we don't need from smbios_set_defaults(),
963 * once we know which machine version has been requested.
965 if (!smbios_entries) {
966 smbios_entries_len = sizeof(uint16_t);
967 smbios_entries = g_malloc0(smbios_entries_len);
969 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
970 size + sizeof(*table));
971 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
972 table->header.type = SMBIOS_TABLE_ENTRY;
973 table->header.length = cpu_to_le16(sizeof(*table) + size);
974 memcpy(table->data, header, size);
975 smbios_entries_len += sizeof(*table) + size;
976 (*(uint16_t *)smbios_entries) =
977 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
978 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
980 return;
983 val = qemu_opt_get(opts, "type");
984 if (val) {
985 unsigned long type = strtoul(val, NULL, 0);
987 if (type > SMBIOS_MAX_TYPE) {
988 error_report("out of range!");
989 exit(1);
992 if (test_bit(type, have_binfile_bitmap)) {
993 error_report("can't add fields, binary file already loaded!");
994 exit(1);
996 set_bit(type, have_fields_bitmap);
998 switch (type) {
999 case 0:
1000 qemu_opts_validate(opts, qemu_smbios_type0_opts, &local_err);
1001 if (local_err) {
1002 error_report_err(local_err);
1003 exit(1);
1005 save_opt(&type0.vendor, opts, "vendor");
1006 save_opt(&type0.version, opts, "version");
1007 save_opt(&type0.date, opts, "date");
1008 type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1010 val = qemu_opt_get(opts, "release");
1011 if (val) {
1012 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1013 error_report("Invalid release");
1014 exit(1);
1016 type0.have_major_minor = true;
1018 return;
1019 case 1:
1020 qemu_opts_validate(opts, qemu_smbios_type1_opts, &local_err);
1021 if (local_err) {
1022 error_report_err(local_err);
1023 exit(1);
1025 save_opt(&type1.manufacturer, opts, "manufacturer");
1026 save_opt(&type1.product, opts, "product");
1027 save_opt(&type1.version, opts, "version");
1028 save_opt(&type1.serial, opts, "serial");
1029 save_opt(&type1.sku, opts, "sku");
1030 save_opt(&type1.family, opts, "family");
1032 val = qemu_opt_get(opts, "uuid");
1033 if (val) {
1034 if (qemu_uuid_parse(val, qemu_uuid) != 0) {
1035 error_report("Invalid UUID");
1036 exit(1);
1038 qemu_uuid_set = true;
1040 return;
1041 case 2:
1042 qemu_opts_validate(opts, qemu_smbios_type2_opts, &local_err);
1043 if (local_err) {
1044 error_report_err(local_err);
1045 exit(1);
1047 save_opt(&type2.manufacturer, opts, "manufacturer");
1048 save_opt(&type2.product, opts, "product");
1049 save_opt(&type2.version, opts, "version");
1050 save_opt(&type2.serial, opts, "serial");
1051 save_opt(&type2.asset, opts, "asset");
1052 save_opt(&type2.location, opts, "location");
1053 return;
1054 case 3:
1055 qemu_opts_validate(opts, qemu_smbios_type3_opts, &local_err);
1056 if (local_err) {
1057 error_report_err(local_err);
1058 exit(1);
1060 save_opt(&type3.manufacturer, opts, "manufacturer");
1061 save_opt(&type3.version, opts, "version");
1062 save_opt(&type3.serial, opts, "serial");
1063 save_opt(&type3.asset, opts, "asset");
1064 save_opt(&type3.sku, opts, "sku");
1065 return;
1066 case 4:
1067 qemu_opts_validate(opts, qemu_smbios_type4_opts, &local_err);
1068 if (local_err) {
1069 error_report_err(local_err);
1070 exit(1);
1072 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1073 save_opt(&type4.manufacturer, opts, "manufacturer");
1074 save_opt(&type4.version, opts, "version");
1075 save_opt(&type4.serial, opts, "serial");
1076 save_opt(&type4.asset, opts, "asset");
1077 save_opt(&type4.part, opts, "part");
1078 return;
1079 case 17:
1080 qemu_opts_validate(opts, qemu_smbios_type17_opts, &local_err);
1081 if (local_err) {
1082 error_report_err(local_err);
1083 exit(1);
1085 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1086 save_opt(&type17.bank, opts, "bank");
1087 save_opt(&type17.manufacturer, opts, "manufacturer");
1088 save_opt(&type17.serial, opts, "serial");
1089 save_opt(&type17.asset, opts, "asset");
1090 save_opt(&type17.part, opts, "part");
1091 type17.speed = qemu_opt_get_number(opts, "speed", 0);
1092 return;
1093 default:
1094 error_report("Don't know how to build fields for SMBIOS type %ld",
1095 type);
1096 exit(1);
1100 error_report("Must specify type= or file=");
1101 exit(1);