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