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