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