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