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