]> git.proxmox.com Git - mirror_qemu.git/blame - hw/smbios/smbios.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[mirror_qemu.git] / hw / smbios / smbios.c
CommitLineData
b6f6e3d3
AL
1/*
2 * SMBIOS Support
3 *
4 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
4f953d2f 5 * Copyright (C) 2013 Red Hat, Inc.
b6f6e3d3
AL
6 *
7 * Authors:
8 * Alex Williamson <alex.williamson@hp.com>
4f953d2f 9 * Markus Armbruster <armbru@redhat.com>
b6f6e3d3
AL
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
6b620ca3
PB
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.
b6f6e3d3
AL
16 */
17
0430891c 18#include "qemu/osdep.h"
968dfd05 19#include "qemu/units.h"
da34e65c 20#include "qapi/error.h"
4f953d2f 21#include "qemu/config-file.h"
5bb95e41 22#include "qemu/error-report.h"
922a01a0 23#include "qemu/option.h"
9c17d615 24#include "sysemu/sysemu.h"
cea25275 25#include "qemu/uuid.h"
c97294ec 26#include "sysemu/cpus.h"
60d8f328 27#include "hw/smbios/smbios.h"
83c9f4ca 28#include "hw/loader.h"
60d8f328 29#include "exec/cpu-common.h"
0517cc98 30#include "smbios_build.h"
35658f6e 31#include "hw/smbios/ipmi.h"
e6667f71
GS
32
33/* legacy structures and constants for <= 2.0 machines */
b6f6e3d3
AL
34struct smbios_header {
35 uint16_t length;
36 uint8_t type;
541dc0d4 37} QEMU_PACKED;
b6f6e3d3
AL
38
39struct smbios_field {
40 struct smbios_header header;
41 uint8_t type;
42 uint16_t offset;
43 uint8_t data[];
541dc0d4 44} QEMU_PACKED;
b6f6e3d3
AL
45
46struct smbios_table {
47 struct smbios_header header;
48 uint8_t data[];
541dc0d4 49} QEMU_PACKED;
b6f6e3d3
AL
50
51#define SMBIOS_FIELD_ENTRY 0
52#define SMBIOS_TABLE_ENTRY 1
53
b6f6e3d3
AL
54static uint8_t *smbios_entries;
55static size_t smbios_entries_len;
c97294ec 56static bool smbios_legacy = true;
caad057b 57static bool smbios_uuid_encoded = true;
e6667f71
GS
58/* end: legacy structures & constants for <= 2.0 machines */
59
60
0517cc98
CM
61uint8_t *smbios_tables;
62size_t smbios_tables_len;
63unsigned smbios_table_max;
64unsigned smbios_table_cnt;
86299120
WH
65static SmbiosEntryPointType smbios_ep_type = SMBIOS_ENTRY_POINT_21;
66
67static SmbiosEntryPoint ep;
c97294ec 68
09c0848e 69static int smbios_type4_count = 0;
fc3b3295 70static bool smbios_immutable;
c97294ec
GS
71static bool smbios_have_defaults;
72static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
09c0848e 73
2e6e8d7a
GS
74static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
75static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
ec2df8c1 76
fc3b3295
MA
77static struct {
78 const char *vendor, *version, *date;
84351843 79 bool have_major_minor, uefi;
fc3b3295
MA
80 uint8_t major, minor;
81} type0;
82
83static struct {
84 const char *manufacturer, *product, *version, *serial, *sku, *family;
9c5ce8db 85 /* uuid is in qemu_uuid */
fc3b3295
MA
86} type1;
87
c97294ec
GS
88static struct {
89 const char *manufacturer, *product, *version, *serial, *asset, *location;
90} type2;
91
92static struct {
93 const char *manufacturer, *version, *serial, *asset, *sku;
94} type3;
95
96static struct {
97 const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
98} type4;
99
2d6dcbf9
DB
100static struct {
101 size_t nvalues;
102 const char **values;
103} type11;
104
c97294ec
GS
105static struct {
106 const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
3ebd6cc8 107 uint16_t speed;
c97294ec
GS
108} type17;
109
4f953d2f
MA
110static QemuOptsList qemu_smbios_opts = {
111 .name = "smbios",
112 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
113 .desc = {
114 /*
115 * no elements => accept any params
116 * validation will happen later
117 */
118 { /* end of list */ }
119 }
120};
121
122static const QemuOptDesc qemu_smbios_file_opts[] = {
123 {
124 .name = "file",
125 .type = QEMU_OPT_STRING,
126 .help = "binary file containing an SMBIOS element",
127 },
128 { /* end of list */ }
129};
130
131static const QemuOptDesc qemu_smbios_type0_opts[] = {
132 {
133 .name = "type",
134 .type = QEMU_OPT_NUMBER,
135 .help = "SMBIOS element type",
136 },{
137 .name = "vendor",
138 .type = QEMU_OPT_STRING,
139 .help = "vendor name",
140 },{
141 .name = "version",
142 .type = QEMU_OPT_STRING,
143 .help = "version number",
144 },{
145 .name = "date",
146 .type = QEMU_OPT_STRING,
147 .help = "release date",
148 },{
149 .name = "release",
150 .type = QEMU_OPT_STRING,
151 .help = "revision number",
84351843
GS
152 },{
153 .name = "uefi",
154 .type = QEMU_OPT_BOOL,
155 .help = "uefi support",
4f953d2f
MA
156 },
157 { /* end of list */ }
158};
159
160static const QemuOptDesc qemu_smbios_type1_opts[] = {
161 {
162 .name = "type",
163 .type = QEMU_OPT_NUMBER,
164 .help = "SMBIOS element type",
165 },{
166 .name = "manufacturer",
167 .type = QEMU_OPT_STRING,
168 .help = "manufacturer name",
169 },{
170 .name = "product",
171 .type = QEMU_OPT_STRING,
172 .help = "product name",
173 },{
174 .name = "version",
175 .type = QEMU_OPT_STRING,
176 .help = "version number",
177 },{
178 .name = "serial",
179 .type = QEMU_OPT_STRING,
180 .help = "serial number",
181 },{
182 .name = "uuid",
183 .type = QEMU_OPT_STRING,
184 .help = "UUID",
185 },{
186 .name = "sku",
187 .type = QEMU_OPT_STRING,
188 .help = "SKU number",
189 },{
190 .name = "family",
191 .type = QEMU_OPT_STRING,
192 .help = "family name",
193 },
194 { /* end of list */ }
195};
196
c97294ec
GS
197static const QemuOptDesc qemu_smbios_type2_opts[] = {
198 {
199 .name = "type",
200 .type = QEMU_OPT_NUMBER,
201 .help = "SMBIOS element type",
202 },{
203 .name = "manufacturer",
204 .type = QEMU_OPT_STRING,
205 .help = "manufacturer name",
206 },{
207 .name = "product",
208 .type = QEMU_OPT_STRING,
209 .help = "product name",
210 },{
211 .name = "version",
212 .type = QEMU_OPT_STRING,
213 .help = "version number",
214 },{
215 .name = "serial",
216 .type = QEMU_OPT_STRING,
217 .help = "serial number",
218 },{
219 .name = "asset",
220 .type = QEMU_OPT_STRING,
221 .help = "asset tag number",
222 },{
223 .name = "location",
224 .type = QEMU_OPT_STRING,
225 .help = "location in chassis",
226 },
227 { /* end of list */ }
228};
229
230static const QemuOptDesc qemu_smbios_type3_opts[] = {
231 {
232 .name = "type",
233 .type = QEMU_OPT_NUMBER,
234 .help = "SMBIOS element type",
235 },{
236 .name = "manufacturer",
237 .type = QEMU_OPT_STRING,
238 .help = "manufacturer name",
239 },{
240 .name = "version",
241 .type = QEMU_OPT_STRING,
242 .help = "version number",
243 },{
244 .name = "serial",
245 .type = QEMU_OPT_STRING,
246 .help = "serial number",
247 },{
248 .name = "asset",
249 .type = QEMU_OPT_STRING,
250 .help = "asset tag number",
251 },{
252 .name = "sku",
253 .type = QEMU_OPT_STRING,
254 .help = "SKU number",
255 },
256 { /* end of list */ }
257};
258
259static const QemuOptDesc qemu_smbios_type4_opts[] = {
260 {
261 .name = "type",
262 .type = QEMU_OPT_NUMBER,
263 .help = "SMBIOS element type",
264 },{
265 .name = "sock_pfx",
266 .type = QEMU_OPT_STRING,
267 .help = "socket designation string prefix",
268 },{
269 .name = "manufacturer",
270 .type = QEMU_OPT_STRING,
271 .help = "manufacturer name",
272 },{
273 .name = "version",
274 .type = QEMU_OPT_STRING,
275 .help = "version number",
276 },{
277 .name = "serial",
278 .type = QEMU_OPT_STRING,
279 .help = "serial number",
280 },{
281 .name = "asset",
282 .type = QEMU_OPT_STRING,
283 .help = "asset tag number",
284 },{
285 .name = "part",
286 .type = QEMU_OPT_STRING,
287 .help = "part number",
288 },
289 { /* end of list */ }
290};
291
2d6dcbf9
DB
292static const QemuOptDesc qemu_smbios_type11_opts[] = {
293 {
294 .name = "value",
295 .type = QEMU_OPT_STRING,
296 .help = "OEM string data",
297 },
298};
299
c97294ec
GS
300static const QemuOptDesc qemu_smbios_type17_opts[] = {
301 {
302 .name = "type",
303 .type = QEMU_OPT_NUMBER,
304 .help = "SMBIOS element type",
305 },{
306 .name = "loc_pfx",
307 .type = QEMU_OPT_STRING,
308 .help = "device locator string prefix",
309 },{
310 .name = "bank",
311 .type = QEMU_OPT_STRING,
312 .help = "bank locator string",
313 },{
314 .name = "manufacturer",
315 .type = QEMU_OPT_STRING,
316 .help = "manufacturer name",
317 },{
318 .name = "serial",
319 .type = QEMU_OPT_STRING,
320 .help = "serial number",
321 },{
322 .name = "asset",
323 .type = QEMU_OPT_STRING,
324 .help = "asset tag number",
325 },{
326 .name = "part",
327 .type = QEMU_OPT_STRING,
328 .help = "part number",
3ebd6cc8
GS
329 },{
330 .name = "speed",
331 .type = QEMU_OPT_NUMBER,
332 .help = "maximum capable speed",
c97294ec
GS
333 },
334 { /* end of list */ }
335};
336
4f953d2f
MA
337static void smbios_register_config(void)
338{
339 qemu_add_opts(&qemu_smbios_opts);
340}
341
34294e2f 342opts_init(smbios_register_config);
4f953d2f 343
09c0848e
BK
344static void smbios_validate_table(void)
345{
c97294ec
GS
346 uint32_t expect_t4_count = smbios_legacy ? smp_cpus : smbios_smp_sockets;
347
348 if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
349 error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
350 expect_t4_count, smbios_type4_count);
09c0848e
BK
351 exit(1);
352 }
353}
b6f6e3d3 354
e6667f71
GS
355
356/* legacy setup functions for <= 2.0 machines */
fc3b3295 357static void smbios_add_field(int type, int offset, const void *data, size_t len)
b6f6e3d3
AL
358{
359 struct smbios_field *field;
360
b6f6e3d3
AL
361 if (!smbios_entries) {
362 smbios_entries_len = sizeof(uint16_t);
7267c094 363 smbios_entries = g_malloc0(smbios_entries_len);
b6f6e3d3 364 }
7267c094 365 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
b6f6e3d3
AL
366 sizeof(*field) + len);
367 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
368 field->header.type = SMBIOS_FIELD_ENTRY;
369 field->header.length = cpu_to_le16(sizeof(*field) + len);
370
371 field->type = type;
372 field->offset = cpu_to_le16(offset);
373 memcpy(field->data, data, len);
374
375 smbios_entries_len += sizeof(*field) + len;
376 (*(uint16_t *)smbios_entries) =
377 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
378}
379
e26d3e73 380static void smbios_maybe_add_str(int type, int offset, const char *data)
b6f6e3d3 381{
e26d3e73
MA
382 if (data) {
383 smbios_add_field(type, offset, data, strlen(data) + 1);
4f953d2f 384 }
e26d3e73
MA
385}
386
387static void smbios_build_type_0_fields(void)
388{
389 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
390 type0.vendor);
391 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
392 type0.version);
393 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
b6f6e3d3 394 bios_release_date_str),
e26d3e73 395 type0.date);
fc3b3295 396 if (type0.have_major_minor) {
b6f6e3d3 397 smbios_add_field(0, offsetof(struct smbios_type_0,
ebc85e3f 398 system_bios_major_release),
fc3b3295 399 &type0.major, 1);
b6f6e3d3 400 smbios_add_field(0, offsetof(struct smbios_type_0,
ebc85e3f 401 system_bios_minor_release),
fc3b3295 402 &type0.minor, 1);
b6f6e3d3
AL
403 }
404}
405
fc3b3295 406static void smbios_build_type_1_fields(void)
b6f6e3d3 407{
e26d3e73
MA
408 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
409 type1.manufacturer);
410 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
411 type1.product);
412 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
413 type1.version);
414 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
415 type1.serial);
416 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
417 type1.sku);
418 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
419 type1.family);
fc3b3295 420 if (qemu_uuid_set) {
caad057b
EH
421 /* We don't encode the UUID in the "wire format" here because this
422 * function is for legacy mode and needs to keep the guest ABI, and
423 * because we don't know what's the SMBIOS version advertised by the
424 * BIOS.
425 */
fc3b3295 426 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
9c5ce8db 427 &qemu_uuid, 16);
fc3b3295
MA
428 }
429}
430
e6667f71 431uint8_t *smbios_get_table_legacy(size_t *length)
fc3b3295 432{
c97294ec
GS
433 if (!smbios_legacy) {
434 *length = 0;
435 return NULL;
436 }
437
fc3b3295
MA
438 if (!smbios_immutable) {
439 smbios_build_type_0_fields();
440 smbios_build_type_1_fields();
441 smbios_validate_table();
442 smbios_immutable = true;
443 }
444 *length = smbios_entries_len;
445 return smbios_entries;
446}
e6667f71
GS
447/* end: legacy setup functions for <= 2.0 machines */
448
fc3b3295 449
0517cc98 450bool smbios_skip_table(uint8_t type, bool required_table)
c97294ec
GS
451{
452 if (test_bit(type, have_binfile_bitmap)) {
453 return true; /* user provided their own binary blob(s) */
454 }
455 if (test_bit(type, have_fields_bitmap)) {
456 return false; /* user provided fields via command line */
457 }
458 if (smbios_have_defaults && required_table) {
459 return false; /* we're building tables, and this one's required */
460 }
461 return true;
462}
463
c97294ec
GS
464static void smbios_build_type_0_table(void)
465{
466 SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
467
468 SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
469 SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
470
fb5be2e8 471 t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
c97294ec
GS
472
473 SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
474
475 t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
476
84351843 477 t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
c97294ec 478 t->bios_characteristics_extension_bytes[0] = 0;
84351843
GS
479 t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
480 if (type0.uefi) {
481 t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
482 }
c97294ec
GS
483
484 if (type0.have_major_minor) {
485 t->system_bios_major_release = type0.major;
486 t->system_bios_minor_release = type0.minor;
487 } else {
488 t->system_bios_major_release = 0;
489 t->system_bios_minor_release = 0;
490 }
491
492 /* hardcoded in SeaBIOS */
493 t->embedded_controller_major_release = 0xFF;
494 t->embedded_controller_minor_release = 0xFF;
495
496 SMBIOS_BUILD_TABLE_POST;
497}
498
caad057b
EH
499/* Encode UUID from the big endian encoding described on RFC4122 to the wire
500 * format specified by SMBIOS version 2.6.
501 */
9c5ce8db 502static void smbios_encode_uuid(struct smbios_uuid *uuid, QemuUUID *in)
caad057b 503{
664ee768 504 memcpy(uuid, in, 16);
caad057b
EH
505 if (smbios_uuid_encoded) {
506 uuid->time_low = bswap32(uuid->time_low);
507 uuid->time_mid = bswap16(uuid->time_mid);
508 uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
509 }
510}
511
c97294ec
GS
512static void smbios_build_type_1_table(void)
513{
514 SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
515
516 SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
517 SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
518 SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
519 SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
520 if (qemu_uuid_set) {
9c5ce8db 521 smbios_encode_uuid(&t->uuid, &qemu_uuid);
c97294ec 522 } else {
caad057b 523 memset(&t->uuid, 0, 16);
c97294ec
GS
524 }
525 t->wake_up_type = 0x06; /* power switch */
526 SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
527 SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
528
529 SMBIOS_BUILD_TABLE_POST;
530}
531
532static void smbios_build_type_2_table(void)
533{
534 SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
535
536 SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
537 SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
538 SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
539 SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
540 SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
541 t->feature_flags = 0x01; /* Motherboard */
542 SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
fb5be2e8 543 t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
c97294ec
GS
544 t->board_type = 0x0A; /* Motherboard */
545 t->contained_element_count = 0;
546
547 SMBIOS_BUILD_TABLE_POST;
548}
549
550static void smbios_build_type_3_table(void)
551{
552 SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
553
554 SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
555 t->type = 0x01; /* Other */
556 SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
557 SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
558 SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
559 t->boot_up_state = 0x03; /* Safe */
560 t->power_supply_state = 0x03; /* Safe */
561 t->thermal_state = 0x03; /* Safe */
562 t->security_status = 0x02; /* Unknown */
fb5be2e8 563 t->oem_defined = cpu_to_le32(0);
c97294ec
GS
564 t->height = 0;
565 t->number_of_power_cords = 0;
566 t->contained_element_count = 0;
567 SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
568
569 SMBIOS_BUILD_TABLE_POST;
570}
571
572static void smbios_build_type_4_table(unsigned instance)
573{
574 char sock_str[128];
575
576 SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
577
578 snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
579 SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
580 t->processor_type = 0x03; /* CPU */
fb5be2e8 581 t->processor_family = 0x01; /* Other */
c97294ec 582 SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
fb5be2e8
GS
583 t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
584 t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
c97294ec
GS
585 SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
586 t->voltage = 0;
fb5be2e8 587 t->external_clock = cpu_to_le16(0); /* Unknown */
07d01c9c
EH
588 /* SVVP requires max_speed and current_speed to not be unknown. */
589 t->max_speed = cpu_to_le16(2000); /* 2000 MHz */
590 t->current_speed = cpu_to_le16(2000); /* 2000 MHz */
c97294ec
GS
591 t->status = 0x41; /* Socket populated, CPU enabled */
592 t->processor_upgrade = 0x01; /* Other */
fb5be2e8
GS
593 t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
594 t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
595 t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
c97294ec
GS
596 SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
597 SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
598 SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
599 t->core_count = t->core_enabled = smp_cores;
600 t->thread_count = smp_threads;
fb5be2e8
GS
601 t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
602 t->processor_family2 = cpu_to_le16(0x01); /* Other */
c97294ec
GS
603
604 SMBIOS_BUILD_TABLE_POST;
605 smbios_type4_count++;
606}
607
2d6dcbf9
DB
608static void smbios_build_type_11_table(void)
609{
610 char count_str[128];
611 size_t i;
612
613 if (type11.nvalues == 0) {
614 return;
615 }
616
617 SMBIOS_BUILD_TABLE_PRE(11, 0xe00, true); /* required */
618
619 snprintf(count_str, sizeof(count_str), "%zu", type11.nvalues);
620 t->count = type11.nvalues;
621
622 for (i = 0; i < type11.nvalues; i++) {
623 SMBIOS_TABLE_SET_STR_LIST(11, type11.values[i]);
624 }
625
626 SMBIOS_BUILD_TABLE_POST;
627}
628
c97294ec
GS
629#define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
630
631static void smbios_build_type_16_table(unsigned dimm_cnt)
632{
f4ec5cd2 633 uint64_t size_kb;
c97294ec
GS
634
635 SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
636
637 t->location = 0x01; /* Other */
638 t->use = 0x03; /* System memory */
639 t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
968dfd05 640 size_kb = QEMU_ALIGN_UP(ram_size, KiB) / KiB;
c97294ec 641 if (size_kb < MAX_T16_STD_SZ) {
fb5be2e8
GS
642 t->maximum_capacity = cpu_to_le32(size_kb);
643 t->extended_maximum_capacity = cpu_to_le64(0);
c97294ec 644 } else {
fb5be2e8
GS
645 t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
646 t->extended_maximum_capacity = cpu_to_le64(ram_size);
c97294ec 647 }
fb5be2e8
GS
648 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
649 t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
c97294ec
GS
650
651 SMBIOS_BUILD_TABLE_POST;
652}
653
654#define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
655#define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
656
f4ec5cd2 657static void smbios_build_type_17_table(unsigned instance, uint64_t size)
c97294ec
GS
658{
659 char loc_str[128];
f4ec5cd2 660 uint64_t size_mb;
c97294ec
GS
661
662 SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
663
fb5be2e8
GS
664 t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
665 t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
666 t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
667 t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
968dfd05 668 size_mb = QEMU_ALIGN_UP(size, MiB) / MiB;
c97294ec 669 if (size_mb < MAX_T17_STD_SZ) {
fb5be2e8
GS
670 t->size = cpu_to_le16(size_mb);
671 t->extended_size = cpu_to_le32(0);
c97294ec
GS
672 } else {
673 assert(size_mb < MAX_T17_EXT_SZ);
fb5be2e8
GS
674 t->size = cpu_to_le16(MAX_T17_STD_SZ);
675 t->extended_size = cpu_to_le32(size_mb);
c97294ec
GS
676 }
677 t->form_factor = 0x09; /* DIMM */
678 t->device_set = 0; /* Not in a set */
679 snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
680 SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
681 SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
682 t->memory_type = 0x07; /* RAM */
fb5be2e8 683 t->type_detail = cpu_to_le16(0x02); /* Other */
3ebd6cc8 684 t->speed = cpu_to_le16(type17.speed);
c97294ec
GS
685 SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
686 SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
687 SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
688 SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
689 t->attributes = 0; /* Unknown */
3ebd6cc8 690 t->configured_clock_speed = t->speed; /* reuse value for max speed */
0d73394a
GS
691 t->minimum_voltage = cpu_to_le16(0); /* Unknown */
692 t->maximum_voltage = cpu_to_le16(0); /* Unknown */
693 t->configured_voltage = cpu_to_le16(0); /* Unknown */
c97294ec
GS
694
695 SMBIOS_BUILD_TABLE_POST;
696}
697
698static void smbios_build_type_19_table(unsigned instance,
f4ec5cd2 699 uint64_t start, uint64_t size)
c97294ec 700{
f4ec5cd2 701 uint64_t end, start_kb, end_kb;
c97294ec
GS
702
703 SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
704
705 end = start + size - 1;
706 assert(end > start);
968dfd05
PMD
707 start_kb = start / KiB;
708 end_kb = end / KiB;
c97294ec 709 if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
fb5be2e8
GS
710 t->starting_address = cpu_to_le32(start_kb);
711 t->ending_address = cpu_to_le32(end_kb);
712 t->extended_starting_address =
713 t->extended_ending_address = cpu_to_le64(0);
c97294ec 714 } else {
fb5be2e8
GS
715 t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
716 t->extended_starting_address = cpu_to_le64(start);
717 t->extended_ending_address = cpu_to_le64(end);
c97294ec 718 }
fb5be2e8 719 t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
c97294ec
GS
720 t->partition_width = 1; /* One device per row */
721
722 SMBIOS_BUILD_TABLE_POST;
723}
724
725static void smbios_build_type_32_table(void)
726{
727 SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
728
729 memset(t->reserved, 0, 6);
730 t->boot_status = 0; /* No errors detected */
731
732 SMBIOS_BUILD_TABLE_POST;
733}
734
735static void smbios_build_type_127_table(void)
736{
737 SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
738 SMBIOS_BUILD_TABLE_POST;
739}
740
741void smbios_set_cpuid(uint32_t version, uint32_t features)
742{
743 smbios_cpuid_version = version;
744 smbios_cpuid_features = features;
745}
746
cb36acb6
GS
747#define SMBIOS_SET_DEFAULT(field, value) \
748 if (!field) { \
749 field = value; \
750 }
751
752void smbios_set_defaults(const char *manufacturer, const char *product,
caad057b 753 const char *version, bool legacy_mode,
86299120 754 bool uuid_encoded, SmbiosEntryPointType ep_type)
cb36acb6 755{
c97294ec
GS
756 smbios_have_defaults = true;
757 smbios_legacy = legacy_mode;
caad057b 758 smbios_uuid_encoded = uuid_encoded;
86299120 759 smbios_ep_type = ep_type;
c97294ec
GS
760
761 /* drop unwanted version of command-line file blob(s) */
762 if (smbios_legacy) {
a10678b0 763 g_free(smbios_tables);
c97294ec
GS
764 /* in legacy mode, also complain if fields were given for types > 1 */
765 if (find_next_bit(have_fields_bitmap,
766 SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
767 error_report("can't process fields for smbios "
768 "types > 1 on machine versions < 2.1!");
769 exit(1);
770 }
771 } else {
a10678b0 772 g_free(smbios_entries);
c97294ec
GS
773 }
774
cb36acb6
GS
775 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
776 SMBIOS_SET_DEFAULT(type1.product, product);
777 SMBIOS_SET_DEFAULT(type1.version, version);
c97294ec
GS
778 SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
779 SMBIOS_SET_DEFAULT(type2.product, product);
780 SMBIOS_SET_DEFAULT(type2.version, version);
781 SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
782 SMBIOS_SET_DEFAULT(type3.version, version);
783 SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
784 SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
785 SMBIOS_SET_DEFAULT(type4.version, version);
786 SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
787 SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
788}
789
790static void smbios_entry_point_setup(void)
791{
86299120
WH
792 switch (smbios_ep_type) {
793 case SMBIOS_ENTRY_POINT_21:
794 memcpy(ep.ep21.anchor_string, "_SM_", 4);
795 memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
796 ep.ep21.length = sizeof(struct smbios_21_entry_point);
797 ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
798 memset(ep.ep21.formatted_area, 0, 5);
799
800 /* compliant with smbios spec v2.8 */
801 ep.ep21.smbios_major_version = 2;
802 ep.ep21.smbios_minor_version = 8;
803 ep.ep21.smbios_bcd_revision = 0x28;
804
805 /* set during table construction, but BIOS may override: */
806 ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
807 ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
808 ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
809
810 /* BIOS must recalculate */
811 ep.ep21.checksum = 0;
812 ep.ep21.intermediate_checksum = 0;
813 ep.ep21.structure_table_address = cpu_to_le32(0);
814
815 break;
816 case SMBIOS_ENTRY_POINT_30:
817 memcpy(ep.ep30.anchor_string, "_SM3_", 5);
818 ep.ep30.length = sizeof(struct smbios_30_entry_point);
819 ep.ep30.entry_point_revision = 1;
820 ep.ep30.reserved = 0;
821
822 /* compliant with smbios spec 3.0 */
823 ep.ep30.smbios_major_version = 3;
824 ep.ep30.smbios_minor_version = 0;
825 ep.ep30.smbios_doc_rev = 0;
826
827 /* set during table construct, but BIOS might override */
828 ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
829
830 /* BIOS must recalculate */
831 ep.ep30.checksum = 0;
832 ep.ep30.structure_table_address = cpu_to_le64(0);
833
834 break;
835 default:
836 abort();
837 break;
838 }
c97294ec
GS
839}
840
89cc4a27
WH
841void smbios_get_tables(const struct smbios_phys_mem_area *mem_array,
842 const unsigned int mem_array_size,
843 uint8_t **tables, size_t *tables_len,
c97294ec
GS
844 uint8_t **anchor, size_t *anchor_len)
845{
89cc4a27 846 unsigned i, dimm_cnt;
c97294ec
GS
847
848 if (smbios_legacy) {
849 *tables = *anchor = NULL;
850 *tables_len = *anchor_len = 0;
851 return;
852 }
853
854 if (!smbios_immutable) {
855 smbios_build_type_0_table();
856 smbios_build_type_1_table();
857 smbios_build_type_2_table();
858 smbios_build_type_3_table();
859
7dfddd7f 860 smbios_smp_sockets = DIV_ROUND_UP(smp_cpus, smp_cores * smp_threads);
c97294ec
GS
861 assert(smbios_smp_sockets >= 1);
862
863 for (i = 0; i < smbios_smp_sockets; i++) {
864 smbios_build_type_4_table(i);
865 }
866
2d6dcbf9
DB
867 smbios_build_type_11_table();
868
968dfd05 869#define MAX_DIMM_SZ (16 * GiB)
744c6d47
EH
870#define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
871 : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
c97294ec
GS
872
873 dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
874
875 smbios_build_type_16_table(dimm_cnt);
876
877 for (i = 0; i < dimm_cnt; i++) {
878 smbios_build_type_17_table(i, GET_DIMM_SZ);
879 }
880
89cc4a27
WH
881 for (i = 0; i < mem_array_size; i++) {
882 smbios_build_type_19_table(i, mem_array[i].address,
883 mem_array[i].length);
c97294ec
GS
884 }
885
886 smbios_build_type_32_table();
35658f6e 887 smbios_build_type_38_table();
c97294ec
GS
888 smbios_build_type_127_table();
889
890 smbios_validate_table();
891 smbios_entry_point_setup();
892 smbios_immutable = true;
893 }
894
895 /* return tables blob and entry point (anchor), and their sizes */
896 *tables = smbios_tables;
897 *tables_len = smbios_tables_len;
898 *anchor = (uint8_t *)&ep;
86299120
WH
899
900 /* calculate length based on anchor string */
901 if (!strncmp((char *)&ep, "_SM_", 4)) {
902 *anchor_len = sizeof(struct smbios_21_entry_point);
903 } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
904 *anchor_len = sizeof(struct smbios_30_entry_point);
905 } else {
906 abort();
907 }
cb36acb6
GS
908}
909
fc3b3295
MA
910static void save_opt(const char **dest, QemuOpts *opts, const char *name)
911{
912 const char *val = qemu_opt_get(opts, name);
913
914 if (val) {
915 *dest = val;
4f953d2f 916 }
b6f6e3d3
AL
917}
918
2d6dcbf9
DB
919
920struct opt_list {
921 const char *name;
922 size_t *ndest;
923 const char ***dest;
924};
925
926static int save_opt_one(void *opaque,
927 const char *name, const char *value,
928 Error **errp)
929{
930 struct opt_list *opt = opaque;
931
932 if (!g_str_equal(name, opt->name)) {
933 return 0;
934 }
935
936 *opt->dest = g_renew(const char *, *opt->dest, (*opt->ndest) + 1);
937 (*opt->dest)[*opt->ndest] = value;
938 (*opt->ndest)++;
939 return 0;
940}
941
942static void save_opt_list(size_t *ndest, const char ***dest,
943 QemuOpts *opts, const char *name)
944{
945 struct opt_list opt = {
946 name, ndest, dest,
947 };
948 qemu_opt_foreach(opts, save_opt_one, &opt, NULL);
949}
950
1007a37e 951void smbios_entry_add(QemuOpts *opts, Error **errp)
b6f6e3d3 952{
4f953d2f 953 const char *val;
b6f6e3d3 954
fc3b3295 955 assert(!smbios_immutable);
c97294ec 956
4f953d2f
MA
957 val = qemu_opt_get(opts, "file");
958 if (val) {
b6f6e3d3 959 struct smbios_structure_header *header;
4f953d2f 960 int size;
c97294ec 961 struct smbios_table *table; /* legacy mode only */
4f953d2f 962
007b0657 963 qemu_opts_validate(opts, qemu_smbios_file_opts, &error_fatal);
b6f6e3d3 964
4f953d2f 965 size = get_image_size(val);
09c0848e 966 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
4f953d2f 967 error_report("Cannot read SMBIOS file %s", val);
b6f6e3d3
AL
968 exit(1);
969 }
970
c97294ec
GS
971 /*
972 * NOTE: standard double '\0' terminator expected, per smbios spec.
973 * (except in legacy mode, where the second '\0' is implicit and
974 * will be inserted by the BIOS).
975 */
976 smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
977 header = (struct smbios_structure_header *)(smbios_tables +
978 smbios_tables_len);
b6f6e3d3 979
c97294ec 980 if (load_image(val, (uint8_t *)header) != size) {
4f953d2f 981 error_report("Failed to load SMBIOS file %s", val);
b6f6e3d3
AL
982 exit(1);
983 }
984
2e6e8d7a
GS
985 if (test_bit(header->type, have_fields_bitmap)) {
986 error_report("can't load type %d struct, fields already specified!",
987 header->type);
988 exit(1);
989 }
990 set_bit(header->type, have_binfile_bitmap);
991
09c0848e
BK
992 if (header->type == 4) {
993 smbios_type4_count++;
994 }
b6f6e3d3 995
c97294ec
GS
996 smbios_tables_len += size;
997 if (size > smbios_table_max) {
998 smbios_table_max = size;
999 }
1000 smbios_table_cnt++;
1001
1002 /* add a copy of the newly loaded blob to legacy smbios_entries */
1003 /* NOTE: This code runs before smbios_set_defaults(), so we don't
1004 * yet know which mode (legacy vs. aggregate-table) will be
1005 * required. We therefore add the binary blob to both legacy
1006 * (smbios_entries) and aggregate (smbios_tables) tables, and
1007 * delete the one we don't need from smbios_set_defaults(),
1008 * once we know which machine version has been requested.
1009 */
1010 if (!smbios_entries) {
1011 smbios_entries_len = sizeof(uint16_t);
1012 smbios_entries = g_malloc0(smbios_entries_len);
1013 }
1014 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
1015 size + sizeof(*table));
1016 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
1017 table->header.type = SMBIOS_TABLE_ENTRY;
1018 table->header.length = cpu_to_le16(sizeof(*table) + size);
1019 memcpy(table->data, header, size);
b6f6e3d3
AL
1020 smbios_entries_len += sizeof(*table) + size;
1021 (*(uint16_t *)smbios_entries) =
1022 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
c97294ec
GS
1023 /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1024
351a6a73 1025 return;
b6f6e3d3
AL
1026 }
1027
4f953d2f
MA
1028 val = qemu_opt_get(opts, "type");
1029 if (val) {
1030 unsigned long type = strtoul(val, NULL, 0);
1031
2e6e8d7a
GS
1032 if (type > SMBIOS_MAX_TYPE) {
1033 error_report("out of range!");
1034 exit(1);
1035 }
1036
1037 if (test_bit(type, have_binfile_bitmap)) {
1038 error_report("can't add fields, binary file already loaded!");
1039 exit(1);
1040 }
1041 set_bit(type, have_fields_bitmap);
fc3b3295 1042
b6f6e3d3
AL
1043 switch (type) {
1044 case 0:
007b0657 1045 qemu_opts_validate(opts, qemu_smbios_type0_opts, &error_fatal);
fc3b3295
MA
1046 save_opt(&type0.vendor, opts, "vendor");
1047 save_opt(&type0.version, opts, "version");
1048 save_opt(&type0.date, opts, "date");
84351843 1049 type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
fc3b3295
MA
1050
1051 val = qemu_opt_get(opts, "release");
1052 if (val) {
1053 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1054 error_report("Invalid release");
1055 exit(1);
1056 }
1057 type0.have_major_minor = true;
1058 }
351a6a73 1059 return;
b6f6e3d3 1060 case 1:
007b0657 1061 qemu_opts_validate(opts, qemu_smbios_type1_opts, &error_fatal);
fc3b3295
MA
1062 save_opt(&type1.manufacturer, opts, "manufacturer");
1063 save_opt(&type1.product, opts, "product");
1064 save_opt(&type1.version, opts, "version");
1065 save_opt(&type1.serial, opts, "serial");
1066 save_opt(&type1.sku, opts, "sku");
1067 save_opt(&type1.family, opts, "family");
1068
1069 val = qemu_opt_get(opts, "uuid");
1070 if (val) {
9c5ce8db 1071 if (qemu_uuid_parse(val, &qemu_uuid) != 0) {
fc3b3295
MA
1072 error_report("Invalid UUID");
1073 exit(1);
1074 }
1075 qemu_uuid_set = true;
1076 }
351a6a73 1077 return;
c97294ec 1078 case 2:
007b0657 1079 qemu_opts_validate(opts, qemu_smbios_type2_opts, &error_fatal);
c97294ec
GS
1080 save_opt(&type2.manufacturer, opts, "manufacturer");
1081 save_opt(&type2.product, opts, "product");
1082 save_opt(&type2.version, opts, "version");
1083 save_opt(&type2.serial, opts, "serial");
1084 save_opt(&type2.asset, opts, "asset");
1085 save_opt(&type2.location, opts, "location");
1086 return;
1087 case 3:
007b0657 1088 qemu_opts_validate(opts, qemu_smbios_type3_opts, &error_fatal);
c97294ec
GS
1089 save_opt(&type3.manufacturer, opts, "manufacturer");
1090 save_opt(&type3.version, opts, "version");
1091 save_opt(&type3.serial, opts, "serial");
1092 save_opt(&type3.asset, opts, "asset");
1093 save_opt(&type3.sku, opts, "sku");
1094 return;
1095 case 4:
007b0657 1096 qemu_opts_validate(opts, qemu_smbios_type4_opts, &error_fatal);
c97294ec
GS
1097 save_opt(&type4.sock_pfx, opts, "sock_pfx");
1098 save_opt(&type4.manufacturer, opts, "manufacturer");
1099 save_opt(&type4.version, opts, "version");
1100 save_opt(&type4.serial, opts, "serial");
1101 save_opt(&type4.asset, opts, "asset");
1102 save_opt(&type4.part, opts, "part");
1103 return;
2d6dcbf9
DB
1104 case 11:
1105 qemu_opts_validate(opts, qemu_smbios_type11_opts, &error_fatal);
1106 save_opt_list(&type11.nvalues, &type11.values, opts, "value");
1107 return;
c97294ec 1108 case 17:
007b0657 1109 qemu_opts_validate(opts, qemu_smbios_type17_opts, &error_fatal);
c97294ec
GS
1110 save_opt(&type17.loc_pfx, opts, "loc_pfx");
1111 save_opt(&type17.bank, opts, "bank");
1112 save_opt(&type17.manufacturer, opts, "manufacturer");
1113 save_opt(&type17.serial, opts, "serial");
1114 save_opt(&type17.asset, opts, "asset");
1115 save_opt(&type17.part, opts, "part");
3ebd6cc8 1116 type17.speed = qemu_opt_get_number(opts, "speed", 0);
c97294ec 1117 return;
b6f6e3d3 1118 default:
5bb95e41
MA
1119 error_report("Don't know how to build fields for SMBIOS type %ld",
1120 type);
b6f6e3d3
AL
1121 exit(1);
1122 }
1123 }
1124
5bb95e41 1125 error_report("Must specify type= or file=");
351a6a73 1126 exit(1);
b6f6e3d3 1127}