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