SMBIOS: Fix endian-ness when populating multi-byte fields
[qemu/ar7.git] / hw / i386 / smbios.c
blobaeb94bc67145cf66a65fcc68b14ba27df4249421
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 /* end: legacy structures & constants for <= 2.0 machines */
54 static uint8_t *smbios_tables;
55 static size_t smbios_tables_len;
56 static unsigned smbios_table_max;
57 static unsigned smbios_table_cnt;
58 static struct smbios_entry_point ep;
60 static int smbios_type4_count = 0;
61 static bool smbios_immutable;
62 static bool smbios_have_defaults;
63 static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
65 static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
66 static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
68 static struct {
69 const char *vendor, *version, *date;
70 bool have_major_minor;
71 uint8_t major, minor;
72 } type0;
74 static struct {
75 const char *manufacturer, *product, *version, *serial, *sku, *family;
76 /* uuid is in qemu_uuid[] */
77 } type1;
79 static struct {
80 const char *manufacturer, *product, *version, *serial, *asset, *location;
81 } type2;
83 static struct {
84 const char *manufacturer, *version, *serial, *asset, *sku;
85 } type3;
87 static struct {
88 const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
89 } type4;
91 static struct {
92 const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
93 } type17;
95 static QemuOptsList qemu_smbios_opts = {
96 .name = "smbios",
97 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
98 .desc = {
100 * no elements => accept any params
101 * validation will happen later
103 { /* end of list */ }
107 static const QemuOptDesc qemu_smbios_file_opts[] = {
109 .name = "file",
110 .type = QEMU_OPT_STRING,
111 .help = "binary file containing an SMBIOS element",
113 { /* end of list */ }
116 static const QemuOptDesc qemu_smbios_type0_opts[] = {
118 .name = "type",
119 .type = QEMU_OPT_NUMBER,
120 .help = "SMBIOS element type",
122 .name = "vendor",
123 .type = QEMU_OPT_STRING,
124 .help = "vendor name",
126 .name = "version",
127 .type = QEMU_OPT_STRING,
128 .help = "version number",
130 .name = "date",
131 .type = QEMU_OPT_STRING,
132 .help = "release date",
134 .name = "release",
135 .type = QEMU_OPT_STRING,
136 .help = "revision number",
138 { /* end of list */ }
141 static const QemuOptDesc qemu_smbios_type1_opts[] = {
143 .name = "type",
144 .type = QEMU_OPT_NUMBER,
145 .help = "SMBIOS element type",
147 .name = "manufacturer",
148 .type = QEMU_OPT_STRING,
149 .help = "manufacturer name",
151 .name = "product",
152 .type = QEMU_OPT_STRING,
153 .help = "product name",
155 .name = "version",
156 .type = QEMU_OPT_STRING,
157 .help = "version number",
159 .name = "serial",
160 .type = QEMU_OPT_STRING,
161 .help = "serial number",
163 .name = "uuid",
164 .type = QEMU_OPT_STRING,
165 .help = "UUID",
167 .name = "sku",
168 .type = QEMU_OPT_STRING,
169 .help = "SKU number",
171 .name = "family",
172 .type = QEMU_OPT_STRING,
173 .help = "family name",
175 { /* end of list */ }
178 static const QemuOptDesc qemu_smbios_type2_opts[] = {
180 .name = "type",
181 .type = QEMU_OPT_NUMBER,
182 .help = "SMBIOS element type",
184 .name = "manufacturer",
185 .type = QEMU_OPT_STRING,
186 .help = "manufacturer name",
188 .name = "product",
189 .type = QEMU_OPT_STRING,
190 .help = "product name",
192 .name = "version",
193 .type = QEMU_OPT_STRING,
194 .help = "version number",
196 .name = "serial",
197 .type = QEMU_OPT_STRING,
198 .help = "serial number",
200 .name = "asset",
201 .type = QEMU_OPT_STRING,
202 .help = "asset tag number",
204 .name = "location",
205 .type = QEMU_OPT_STRING,
206 .help = "location in chassis",
208 { /* end of list */ }
211 static const QemuOptDesc qemu_smbios_type3_opts[] = {
213 .name = "type",
214 .type = QEMU_OPT_NUMBER,
215 .help = "SMBIOS element type",
217 .name = "manufacturer",
218 .type = QEMU_OPT_STRING,
219 .help = "manufacturer name",
221 .name = "version",
222 .type = QEMU_OPT_STRING,
223 .help = "version number",
225 .name = "serial",
226 .type = QEMU_OPT_STRING,
227 .help = "serial number",
229 .name = "asset",
230 .type = QEMU_OPT_STRING,
231 .help = "asset tag number",
233 .name = "sku",
234 .type = QEMU_OPT_STRING,
235 .help = "SKU number",
237 { /* end of list */ }
240 static const QemuOptDesc qemu_smbios_type4_opts[] = {
242 .name = "type",
243 .type = QEMU_OPT_NUMBER,
244 .help = "SMBIOS element type",
246 .name = "sock_pfx",
247 .type = QEMU_OPT_STRING,
248 .help = "socket designation string prefix",
250 .name = "manufacturer",
251 .type = QEMU_OPT_STRING,
252 .help = "manufacturer name",
254 .name = "version",
255 .type = QEMU_OPT_STRING,
256 .help = "version number",
258 .name = "serial",
259 .type = QEMU_OPT_STRING,
260 .help = "serial number",
262 .name = "asset",
263 .type = QEMU_OPT_STRING,
264 .help = "asset tag number",
266 .name = "part",
267 .type = QEMU_OPT_STRING,
268 .help = "part number",
270 { /* end of list */ }
273 static const QemuOptDesc qemu_smbios_type17_opts[] = {
275 .name = "type",
276 .type = QEMU_OPT_NUMBER,
277 .help = "SMBIOS element type",
279 .name = "loc_pfx",
280 .type = QEMU_OPT_STRING,
281 .help = "device locator string prefix",
283 .name = "bank",
284 .type = QEMU_OPT_STRING,
285 .help = "bank locator string",
287 .name = "manufacturer",
288 .type = QEMU_OPT_STRING,
289 .help = "manufacturer name",
291 .name = "serial",
292 .type = QEMU_OPT_STRING,
293 .help = "serial number",
295 .name = "asset",
296 .type = QEMU_OPT_STRING,
297 .help = "asset tag number",
299 .name = "part",
300 .type = QEMU_OPT_STRING,
301 .help = "part number",
303 { /* end of list */ }
306 static void smbios_register_config(void)
308 qemu_add_opts(&qemu_smbios_opts);
311 machine_init(smbios_register_config);
313 static void smbios_validate_table(void)
315 uint32_t expect_t4_count = smbios_legacy ? smp_cpus : smbios_smp_sockets;
317 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
318 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
319 expect_t4_count, smbios_type4_count);
320 exit(1);
325 /* legacy setup functions for <= 2.0 machines */
326 static void smbios_add_field(int type, int offset, const void *data, size_t len)
328 struct smbios_field *field;
330 if (!smbios_entries) {
331 smbios_entries_len = sizeof(uint16_t);
332 smbios_entries = g_malloc0(smbios_entries_len);
334 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
335 sizeof(*field) + len);
336 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
337 field->header.type = SMBIOS_FIELD_ENTRY;
338 field->header.length = cpu_to_le16(sizeof(*field) + len);
340 field->type = type;
341 field->offset = cpu_to_le16(offset);
342 memcpy(field->data, data, len);
344 smbios_entries_len += sizeof(*field) + len;
345 (*(uint16_t *)smbios_entries) =
346 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
349 static void smbios_maybe_add_str(int type, int offset, const char *data)
351 if (data) {
352 smbios_add_field(type, offset, data, strlen(data) + 1);
356 static void smbios_build_type_0_fields(void)
358 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
359 type0.vendor);
360 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
361 type0.version);
362 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
363 bios_release_date_str),
364 type0.date);
365 if (type0.have_major_minor) {
366 smbios_add_field(0, offsetof(struct smbios_type_0,
367 system_bios_major_release),
368 &type0.major, 1);
369 smbios_add_field(0, offsetof(struct smbios_type_0,
370 system_bios_minor_release),
371 &type0.minor, 1);
375 static void smbios_build_type_1_fields(void)
377 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
378 type1.manufacturer);
379 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
380 type1.product);
381 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
382 type1.version);
383 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
384 type1.serial);
385 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
386 type1.sku);
387 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
388 type1.family);
389 if (qemu_uuid_set) {
390 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
391 qemu_uuid, 16);
395 uint8_t *smbios_get_table_legacy(size_t *length)
397 if (!smbios_legacy) {
398 *length = 0;
399 return NULL;
402 if (!smbios_immutable) {
403 smbios_build_type_0_fields();
404 smbios_build_type_1_fields();
405 smbios_validate_table();
406 smbios_immutable = true;
408 *length = smbios_entries_len;
409 return smbios_entries;
411 /* end: legacy setup functions for <= 2.0 machines */
414 static bool smbios_skip_table(uint8_t type, bool required_table)
416 if (test_bit(type, have_binfile_bitmap)) {
417 return true; /* user provided their own binary blob(s) */
419 if (test_bit(type, have_fields_bitmap)) {
420 return false; /* user provided fields via command line */
422 if (smbios_have_defaults && required_table) {
423 return false; /* we're building tables, and this one's required */
425 return true;
428 #define SMBIOS_BUILD_TABLE_PRE(tbl_type, tbl_handle, tbl_required) \
429 struct smbios_type_##tbl_type *t; \
430 size_t t_off; /* table offset into smbios_tables */ \
431 int str_index = 0; \
432 do { \
433 /* should we skip building this table ? */ \
434 if (smbios_skip_table(tbl_type, tbl_required)) { \
435 return; \
438 /* use offset of table t within smbios_tables */ \
439 /* (pointer must be updated after each realloc) */ \
440 t_off = smbios_tables_len; \
441 smbios_tables_len += sizeof(*t); \
442 smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
443 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
445 t->header.type = tbl_type; \
446 t->header.length = sizeof(*t); \
447 t->header.handle = cpu_to_le16(tbl_handle); \
448 } while (0)
450 #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
451 do { \
452 int len = (value != NULL) ? strlen(value) + 1 : 0; \
453 if (len > 1) { \
454 smbios_tables = g_realloc(smbios_tables, \
455 smbios_tables_len + len); \
456 memcpy(smbios_tables + smbios_tables_len, value, len); \
457 smbios_tables_len += len; \
458 /* update pointer post-realloc */ \
459 t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
460 t->field = ++str_index; \
461 } else { \
462 t->field = 0; \
464 } while (0)
466 #define SMBIOS_BUILD_TABLE_POST \
467 do { \
468 size_t term_cnt, t_size; \
470 /* add '\0' terminator (add two if no strings defined) */ \
471 term_cnt = (str_index == 0) ? 2 : 1; \
472 smbios_tables = g_realloc(smbios_tables, \
473 smbios_tables_len + term_cnt); \
474 memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
475 smbios_tables_len += term_cnt; \
477 /* update smbios max. element size */ \
478 t_size = smbios_tables_len - t_off; \
479 if (t_size > smbios_table_max) { \
480 smbios_table_max = t_size; \
483 /* update smbios element count */ \
484 smbios_table_cnt++; \
485 } while (0)
487 static void smbios_build_type_0_table(void)
489 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
491 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
492 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
494 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
496 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
498 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
500 /* BIOS characteristics not supported */
501 memset(t->bios_characteristics, 0, 8);
502 t->bios_characteristics[0] = 0x08;
504 /* Enable targeted content distribution (needed for SVVP, per SeaBIOS) */
505 t->bios_characteristics_extension_bytes[0] = 0;
506 t->bios_characteristics_extension_bytes[1] = 4;
508 if (type0.have_major_minor) {
509 t->system_bios_major_release = type0.major;
510 t->system_bios_minor_release = type0.minor;
511 } else {
512 t->system_bios_major_release = 0;
513 t->system_bios_minor_release = 0;
516 /* hardcoded in SeaBIOS */
517 t->embedded_controller_major_release = 0xFF;
518 t->embedded_controller_minor_release = 0xFF;
520 SMBIOS_BUILD_TABLE_POST;
523 static void smbios_build_type_1_table(void)
525 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
527 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
528 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
529 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
530 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
531 if (qemu_uuid_set) {
532 memcpy(t->uuid, qemu_uuid, 16);
533 } else {
534 memset(t->uuid, 0, 16);
536 t->wake_up_type = 0x06; /* power switch */
537 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
538 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
540 SMBIOS_BUILD_TABLE_POST;
543 static void smbios_build_type_2_table(void)
545 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
547 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
548 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
549 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
550 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
551 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
552 t->feature_flags = 0x01; /* Motherboard */
553 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
554 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
555 t->board_type = 0x0A; /* Motherboard */
556 t->contained_element_count = 0;
558 SMBIOS_BUILD_TABLE_POST;
561 static void smbios_build_type_3_table(void)
563 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
565 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
566 t->type = 0x01; /* Other */
567 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
568 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
569 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
570 t->boot_up_state = 0x03; /* Safe */
571 t->power_supply_state = 0x03; /* Safe */
572 t->thermal_state = 0x03; /* Safe */
573 t->security_status = 0x02; /* Unknown */
574 t->oem_defined = cpu_to_le32(0);
575 t->height = 0;
576 t->number_of_power_cords = 0;
577 t->contained_element_count = 0;
578 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
580 SMBIOS_BUILD_TABLE_POST;
583 static void smbios_build_type_4_table(unsigned instance)
585 char sock_str[128];
587 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
589 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
590 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
591 t->processor_type = 0x03; /* CPU */
592 t->processor_family = 0x01; /* Other */
593 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
594 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
595 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
596 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
597 t->voltage = 0;
598 t->external_clock = cpu_to_le16(0); /* Unknown */
599 t->max_speed = cpu_to_le16(0); /* Unknown */
600 t->current_speed = cpu_to_le16(0); /* Unknown */
601 t->status = 0x41; /* Socket populated, CPU enabled */
602 t->processor_upgrade = 0x01; /* Other */
603 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
604 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
605 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
606 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
607 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
608 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
609 t->core_count = t->core_enabled = smp_cores;
610 t->thread_count = smp_threads;
611 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
612 t->processor_family2 = cpu_to_le16(0x01); /* Other */
614 SMBIOS_BUILD_TABLE_POST;
615 smbios_type4_count++;
618 #define ONE_KB ((ram_addr_t)1 << 10)
619 #define ONE_MB ((ram_addr_t)1 << 20)
620 #define ONE_GB ((ram_addr_t)1 << 30)
622 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
624 static void smbios_build_type_16_table(unsigned dimm_cnt)
626 ram_addr_t size_kb;
628 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
630 t->location = 0x01; /* Other */
631 t->use = 0x03; /* System memory */
632 t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
633 size_kb = QEMU_ALIGN_UP(ram_size, ONE_KB) / ONE_KB;
634 if (size_kb < MAX_T16_STD_SZ) {
635 t->maximum_capacity = cpu_to_le32(size_kb);
636 t->extended_maximum_capacity = cpu_to_le64(0);
637 } else {
638 t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
639 t->extended_maximum_capacity = cpu_to_le64(ram_size);
641 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
642 t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
644 SMBIOS_BUILD_TABLE_POST;
647 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
648 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
650 static void smbios_build_type_17_table(unsigned instance, ram_addr_t size)
652 char loc_str[128];
653 ram_addr_t size_mb;
655 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
657 t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
658 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
659 t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
660 t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
661 size_mb = QEMU_ALIGN_UP(size, ONE_MB) / ONE_MB;
662 if (size_mb < MAX_T17_STD_SZ) {
663 t->size = cpu_to_le16(size_mb);
664 t->extended_size = cpu_to_le32(0);
665 } else {
666 assert(size_mb < MAX_T17_EXT_SZ);
667 t->size = cpu_to_le16(MAX_T17_STD_SZ);
668 t->extended_size = cpu_to_le32(size_mb);
670 t->form_factor = 0x09; /* DIMM */
671 t->device_set = 0; /* Not in a set */
672 snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
673 SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
674 SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
675 t->memory_type = 0x07; /* RAM */
676 t->type_detail = cpu_to_le16(0x02); /* Other */
677 t->speed = cpu_to_le16(0); /* Unknown */
678 SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
679 SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
680 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
681 SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
682 t->attributes = 0; /* Unknown */
683 t->configured_clock_speed = cpu_to_le32(0); /* Unknown */
684 t->minimum_voltage = cpu_to_le32(0); /* Unknown */
685 t->maximum_voltage = cpu_to_le32(0); /* Unknown */
686 t->configured_voltage = cpu_to_le32(0); /* Unknown */
688 SMBIOS_BUILD_TABLE_POST;
691 static void smbios_build_type_19_table(unsigned instance,
692 ram_addr_t start, ram_addr_t size)
694 ram_addr_t end, start_kb, end_kb;
696 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
698 end = start + size - 1;
699 assert(end > start);
700 start_kb = start / ONE_KB;
701 end_kb = end / ONE_KB;
702 if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
703 t->starting_address = cpu_to_le32(start_kb);
704 t->ending_address = cpu_to_le32(end_kb);
705 t->extended_starting_address =
706 t->extended_ending_address = cpu_to_le64(0);
707 } else {
708 t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
709 t->extended_starting_address = cpu_to_le64(start);
710 t->extended_ending_address = cpu_to_le64(end);
712 t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
713 t->partition_width = 1; /* One device per row */
715 SMBIOS_BUILD_TABLE_POST;
718 static void smbios_build_type_32_table(void)
720 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
722 memset(t->reserved, 0, 6);
723 t->boot_status = 0; /* No errors detected */
725 SMBIOS_BUILD_TABLE_POST;
728 static void smbios_build_type_127_table(void)
730 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
731 SMBIOS_BUILD_TABLE_POST;
734 void smbios_set_cpuid(uint32_t version, uint32_t features)
736 smbios_cpuid_version = version;
737 smbios_cpuid_features = features;
740 #define SMBIOS_SET_DEFAULT(field, value) \
741 if (!field) { \
742 field = value; \
745 #define G_FREE_UNLESS_NULL(ptr) \
746 if (ptr != NULL) { \
747 g_free(ptr); \
750 void smbios_set_defaults(const char *manufacturer, const char *product,
751 const char *version, bool legacy_mode)
753 smbios_have_defaults = true;
754 smbios_legacy = legacy_mode;
756 /* drop unwanted version of command-line file blob(s) */
757 if (smbios_legacy) {
758 G_FREE_UNLESS_NULL(smbios_tables);
759 /* in legacy mode, also complain if fields were given for types > 1 */
760 if (find_next_bit(have_fields_bitmap,
761 SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
762 error_report("can't process fields for smbios "
763 "types > 1 on machine versions < 2.1!");
764 exit(1);
766 } else {
767 G_FREE_UNLESS_NULL(smbios_entries);
770 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
771 SMBIOS_SET_DEFAULT(type1.product, product);
772 SMBIOS_SET_DEFAULT(type1.version, version);
773 SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
774 SMBIOS_SET_DEFAULT(type2.product, product);
775 SMBIOS_SET_DEFAULT(type2.version, version);
776 SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
777 SMBIOS_SET_DEFAULT(type3.version, version);
778 SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
779 SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
780 SMBIOS_SET_DEFAULT(type4.version, version);
781 SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
782 SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
785 static void smbios_entry_point_setup(void)
787 memcpy(ep.anchor_string, "_SM_", 4);
788 memcpy(ep.intermediate_anchor_string, "_DMI_", 5);
789 ep.length = sizeof(struct smbios_entry_point);
790 ep.entry_point_revision = 0; /* formatted_area reserved, per spec v2.1+ */
791 memset(ep.formatted_area, 0, 5);
793 /* compliant with smbios spec v2.8 */
794 ep.smbios_major_version = 2;
795 ep.smbios_minor_version = 8;
796 ep.smbios_bcd_revision = 0x28;
798 /* set during table construction, but BIOS may override: */
799 ep.structure_table_length = cpu_to_le16(smbios_tables_len);
800 ep.max_structure_size = cpu_to_le16(smbios_table_max);
801 ep.number_of_structures = cpu_to_le16(smbios_table_cnt);
803 /* BIOS must recalculate: */
804 ep.checksum = 0;
805 ep.intermediate_checksum = 0;
806 ep.structure_table_address = cpu_to_le32(0);
809 void smbios_get_tables(uint8_t **tables, size_t *tables_len,
810 uint8_t **anchor, size_t *anchor_len)
812 unsigned i, dimm_cnt, instance;
814 if (smbios_legacy) {
815 *tables = *anchor = NULL;
816 *tables_len = *anchor_len = 0;
817 return;
820 if (!smbios_immutable) {
821 smbios_build_type_0_table();
822 smbios_build_type_1_table();
823 smbios_build_type_2_table();
824 smbios_build_type_3_table();
826 smbios_smp_sockets = smp_cpus / (smp_cores * smp_threads);
827 assert(smbios_smp_sockets >= 1);
829 for (i = 0; i < smbios_smp_sockets; i++) {
830 smbios_build_type_4_table(i);
833 #define MAX_DIMM_SZ (16ll * ONE_GB)
834 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ : ram_size % MAX_DIMM_SZ)
836 dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
838 smbios_build_type_16_table(dimm_cnt);
840 for (i = 0; i < dimm_cnt; i++) {
841 smbios_build_type_17_table(i, GET_DIMM_SZ);
844 for (i = 0, instance = 0; i < e820_get_num_entries(); i++) {
845 uint64_t address, length;
846 if (e820_get_entry(i, E820_RAM, &address, &length)) {
847 smbios_build_type_19_table(instance++, address, length);
851 smbios_build_type_32_table();
852 smbios_build_type_127_table();
854 smbios_validate_table();
855 smbios_entry_point_setup();
856 smbios_immutable = true;
859 /* return tables blob and entry point (anchor), and their sizes */
860 *tables = smbios_tables;
861 *tables_len = smbios_tables_len;
862 *anchor = (uint8_t *)&ep;
863 *anchor_len = sizeof(struct smbios_entry_point);
866 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
868 const char *val = qemu_opt_get(opts, name);
870 if (val) {
871 *dest = val;
875 void smbios_entry_add(QemuOpts *opts)
877 Error *local_err = NULL;
878 const char *val;
880 assert(!smbios_immutable);
882 val = qemu_opt_get(opts, "file");
883 if (val) {
884 struct smbios_structure_header *header;
885 int size;
886 struct smbios_table *table; /* legacy mode only */
888 qemu_opts_validate(opts, qemu_smbios_file_opts, &local_err);
889 if (local_err) {
890 error_report("%s", error_get_pretty(local_err));
891 exit(1);
894 size = get_image_size(val);
895 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
896 error_report("Cannot read SMBIOS file %s", val);
897 exit(1);
901 * NOTE: standard double '\0' terminator expected, per smbios spec.
902 * (except in legacy mode, where the second '\0' is implicit and
903 * will be inserted by the BIOS).
905 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
906 header = (struct smbios_structure_header *)(smbios_tables +
907 smbios_tables_len);
909 if (load_image(val, (uint8_t *)header) != size) {
910 error_report("Failed to load SMBIOS file %s", val);
911 exit(1);
914 if (test_bit(header->type, have_fields_bitmap)) {
915 error_report("can't load type %d struct, fields already specified!",
916 header->type);
917 exit(1);
919 set_bit(header->type, have_binfile_bitmap);
921 if (header->type == 4) {
922 smbios_type4_count++;
925 smbios_tables_len += size;
926 if (size > smbios_table_max) {
927 smbios_table_max = size;
929 smbios_table_cnt++;
931 /* add a copy of the newly loaded blob to legacy smbios_entries */
932 /* NOTE: This code runs before smbios_set_defaults(), so we don't
933 * yet know which mode (legacy vs. aggregate-table) will be
934 * required. We therefore add the binary blob to both legacy
935 * (smbios_entries) and aggregate (smbios_tables) tables, and
936 * delete the one we don't need from smbios_set_defaults(),
937 * once we know which machine version has been requested.
939 if (!smbios_entries) {
940 smbios_entries_len = sizeof(uint16_t);
941 smbios_entries = g_malloc0(smbios_entries_len);
943 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
944 size + sizeof(*table));
945 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
946 table->header.type = SMBIOS_TABLE_ENTRY;
947 table->header.length = cpu_to_le16(sizeof(*table) + size);
948 memcpy(table->data, header, size);
949 smbios_entries_len += sizeof(*table) + size;
950 (*(uint16_t *)smbios_entries) =
951 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
952 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
954 return;
957 val = qemu_opt_get(opts, "type");
958 if (val) {
959 unsigned long type = strtoul(val, NULL, 0);
961 if (type > SMBIOS_MAX_TYPE) {
962 error_report("out of range!");
963 exit(1);
966 if (test_bit(type, have_binfile_bitmap)) {
967 error_report("can't add fields, binary file already loaded!");
968 exit(1);
970 set_bit(type, have_fields_bitmap);
972 switch (type) {
973 case 0:
974 qemu_opts_validate(opts, qemu_smbios_type0_opts, &local_err);
975 if (local_err) {
976 error_report("%s", error_get_pretty(local_err));
977 exit(1);
979 save_opt(&type0.vendor, opts, "vendor");
980 save_opt(&type0.version, opts, "version");
981 save_opt(&type0.date, opts, "date");
983 val = qemu_opt_get(opts, "release");
984 if (val) {
985 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
986 error_report("Invalid release");
987 exit(1);
989 type0.have_major_minor = true;
991 return;
992 case 1:
993 qemu_opts_validate(opts, qemu_smbios_type1_opts, &local_err);
994 if (local_err) {
995 error_report("%s", error_get_pretty(local_err));
996 exit(1);
998 save_opt(&type1.manufacturer, opts, "manufacturer");
999 save_opt(&type1.product, opts, "product");
1000 save_opt(&type1.version, opts, "version");
1001 save_opt(&type1.serial, opts, "serial");
1002 save_opt(&type1.sku, opts, "sku");
1003 save_opt(&type1.family, opts, "family");
1005 val = qemu_opt_get(opts, "uuid");
1006 if (val) {
1007 if (qemu_uuid_parse(val, qemu_uuid) != 0) {
1008 error_report("Invalid UUID");
1009 exit(1);
1011 qemu_uuid_set = true;
1013 return;
1014 case 2:
1015 qemu_opts_validate(opts, qemu_smbios_type2_opts, &local_err);
1016 if (local_err) {
1017 error_report("%s", error_get_pretty(local_err));
1018 exit(1);
1020 save_opt(&type2.manufacturer, opts, "manufacturer");
1021 save_opt(&type2.product, opts, "product");
1022 save_opt(&type2.version, opts, "version");
1023 save_opt(&type2.serial, opts, "serial");
1024 save_opt(&type2.asset, opts, "asset");
1025 save_opt(&type2.location, opts, "location");
1026 return;
1027 case 3:
1028 qemu_opts_validate(opts, qemu_smbios_type3_opts, &local_err);
1029 if (local_err) {
1030 error_report("%s", error_get_pretty(local_err));
1031 exit(1);
1033 save_opt(&type3.manufacturer, opts, "manufacturer");
1034 save_opt(&type3.version, opts, "version");
1035 save_opt(&type3.serial, opts, "serial");
1036 save_opt(&type3.asset, opts, "asset");
1037 save_opt(&type3.sku, opts, "sku");
1038 return;
1039 case 4:
1040 qemu_opts_validate(opts, qemu_smbios_type4_opts, &local_err);
1041 if (local_err) {
1042 error_report("%s", error_get_pretty(local_err));
1043 exit(1);
1045 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1046 save_opt(&type4.manufacturer, opts, "manufacturer");
1047 save_opt(&type4.version, opts, "version");
1048 save_opt(&type4.serial, opts, "serial");
1049 save_opt(&type4.asset, opts, "asset");
1050 save_opt(&type4.part, opts, "part");
1051 return;
1052 case 17:
1053 qemu_opts_validate(opts, qemu_smbios_type17_opts, &local_err);
1054 if (local_err) {
1055 error_report("%s", error_get_pretty(local_err));
1056 exit(1);
1058 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1059 save_opt(&type17.bank, opts, "bank");
1060 save_opt(&type17.manufacturer, opts, "manufacturer");
1061 save_opt(&type17.serial, opts, "serial");
1062 save_opt(&type17.asset, opts, "asset");
1063 save_opt(&type17.part, opts, "part");
1064 return;
1065 default:
1066 error_report("Don't know how to build fields for SMBIOS type %ld",
1067 type);
1068 exit(1);
1072 error_report("Must specify type= or file=");
1073 exit(1);