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