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