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