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