xref: /openbmc/qemu/hw/smbios/smbios.c (revision 243975c0)
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     unsigned threads_per_socket;
717     unsigned cores_per_socket;
718 
719     if (smbios_ep_type == SMBIOS_ENTRY_POINT_TYPE_64) {
720         tbl_len = SMBIOS_TYPE_4_LEN_V30;
721     }
722 
723     SMBIOS_BUILD_TABLE_PRE_SIZE(4, T4_BASE + instance,
724                                 true, tbl_len); /* required */
725 
726     snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
727     SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
728     t->processor_type = 0x03; /* CPU */
729     t->processor_family = 0x01; /* Other */
730     SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
731     if (type4.processor_id == 0) {
732         t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
733         t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
734     } else {
735         t->processor_id[0] = cpu_to_le32((uint32_t)type4.processor_id);
736         t->processor_id[1] = cpu_to_le32(type4.processor_id >> 32);
737     }
738     SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
739     t->voltage = 0;
740     t->external_clock = cpu_to_le16(0); /* Unknown */
741     t->max_speed = cpu_to_le16(type4.max_speed);
742     t->current_speed = cpu_to_le16(type4.current_speed);
743     t->status = 0x41; /* Socket populated, CPU enabled */
744     t->processor_upgrade = 0x01; /* Other */
745     t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
746     t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
747     t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
748     SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
749     SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
750     SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
751 
752     threads_per_socket = machine_topo_get_threads_per_socket(ms);
753     cores_per_socket = machine_topo_get_cores_per_socket(ms);
754 
755     t->core_count = (cores_per_socket > 255) ? 0xFF : cores_per_socket;
756     t->core_enabled = t->core_count;
757 
758     t->thread_count = (threads_per_socket > 255) ? 0xFF : threads_per_socket;
759 
760     t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
761     t->processor_family2 = cpu_to_le16(0x01); /* Other */
762 
763     if (tbl_len == SMBIOS_TYPE_4_LEN_V30) {
764         t->core_count2 = t->core_enabled2 = cpu_to_le16(cores_per_socket);
765         t->thread_count2 = cpu_to_le16(threads_per_socket);
766     }
767 
768     SMBIOS_BUILD_TABLE_POST;
769     smbios_type4_count++;
770 }
771 
772 static void smbios_build_type_8_table(void)
773 {
774     unsigned instance = 0;
775     struct type8_instance *t8;
776 
777     QTAILQ_FOREACH(t8, &type8, next) {
778         SMBIOS_BUILD_TABLE_PRE(8, T0_BASE + instance, true);
779 
780         SMBIOS_TABLE_SET_STR(8, internal_reference_str, t8->internal_reference);
781         SMBIOS_TABLE_SET_STR(8, external_reference_str, t8->external_reference);
782         /* most vendors seem to set this to None */
783         t->internal_connector_type = 0x0;
784         t->external_connector_type = t8->connector_type;
785         t->port_type = t8->port_type;
786 
787         SMBIOS_BUILD_TABLE_POST;
788         instance++;
789     }
790 }
791 
792 static void smbios_build_type_11_table(void)
793 {
794     char count_str[128];
795     size_t i;
796 
797     if (type11.nvalues == 0) {
798         return;
799     }
800 
801     SMBIOS_BUILD_TABLE_PRE(11, T11_BASE, true); /* required */
802 
803     snprintf(count_str, sizeof(count_str), "%zu", type11.nvalues);
804     t->count = type11.nvalues;
805 
806     for (i = 0; i < type11.nvalues; i++) {
807         SMBIOS_TABLE_SET_STR_LIST(11, type11.values[i]);
808         g_free(type11.values[i]);
809         type11.values[i] = NULL;
810     }
811 
812     SMBIOS_BUILD_TABLE_POST;
813 }
814 
815 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
816 
817 static void smbios_build_type_16_table(unsigned dimm_cnt)
818 {
819     uint64_t size_kb;
820 
821     SMBIOS_BUILD_TABLE_PRE(16, T16_BASE, true); /* required */
822 
823     t->location = 0x01; /* Other */
824     t->use = 0x03; /* System memory */
825     t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
826     size_kb = QEMU_ALIGN_UP(current_machine->ram_size, KiB) / KiB;
827     if (size_kb < MAX_T16_STD_SZ) {
828         t->maximum_capacity = cpu_to_le32(size_kb);
829         t->extended_maximum_capacity = cpu_to_le64(0);
830     } else {
831         t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
832         t->extended_maximum_capacity = cpu_to_le64(current_machine->ram_size);
833     }
834     t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
835     t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
836 
837     SMBIOS_BUILD_TABLE_POST;
838 }
839 
840 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
841 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
842 
843 static void smbios_build_type_17_table(unsigned instance, uint64_t size)
844 {
845     char loc_str[128];
846     uint64_t size_mb;
847 
848     SMBIOS_BUILD_TABLE_PRE(17, T17_BASE + instance, true); /* required */
849 
850     t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
851     t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
852     t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
853     t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
854     size_mb = QEMU_ALIGN_UP(size, MiB) / MiB;
855     if (size_mb < MAX_T17_STD_SZ) {
856         t->size = cpu_to_le16(size_mb);
857         t->extended_size = cpu_to_le32(0);
858     } else {
859         assert(size_mb < MAX_T17_EXT_SZ);
860         t->size = cpu_to_le16(MAX_T17_STD_SZ);
861         t->extended_size = cpu_to_le32(size_mb);
862     }
863     t->form_factor = 0x09; /* DIMM */
864     t->device_set = 0; /* Not in a set */
865     snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
866     SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
867     SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
868     t->memory_type = 0x07; /* RAM */
869     t->type_detail = cpu_to_le16(0x02); /* Other */
870     t->speed = cpu_to_le16(type17.speed);
871     SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
872     SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
873     SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
874     SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
875     t->attributes = 0; /* Unknown */
876     t->configured_clock_speed = t->speed; /* reuse value for max speed */
877     t->minimum_voltage = cpu_to_le16(0); /* Unknown */
878     t->maximum_voltage = cpu_to_le16(0); /* Unknown */
879     t->configured_voltage = cpu_to_le16(0); /* Unknown */
880 
881     SMBIOS_BUILD_TABLE_POST;
882 }
883 
884 static void smbios_build_type_19_table(unsigned instance, unsigned offset,
885                                        uint64_t start, uint64_t size)
886 {
887     uint64_t end, start_kb, end_kb;
888 
889     SMBIOS_BUILD_TABLE_PRE(19, T19_BASE + offset + instance,
890                            true); /* required */
891 
892     end = start + size - 1;
893     assert(end > start);
894     start_kb = start / KiB;
895     end_kb = end / KiB;
896     if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
897         t->starting_address = cpu_to_le32(start_kb);
898         t->ending_address = cpu_to_le32(end_kb);
899         t->extended_starting_address =
900             t->extended_ending_address = cpu_to_le64(0);
901     } else {
902         t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
903         t->extended_starting_address = cpu_to_le64(start);
904         t->extended_ending_address = cpu_to_le64(end);
905     }
906     t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
907     t->partition_width = 1; /* One device per row */
908 
909     SMBIOS_BUILD_TABLE_POST;
910 }
911 
912 static void smbios_build_type_32_table(void)
913 {
914     SMBIOS_BUILD_TABLE_PRE(32, T32_BASE, true); /* required */
915 
916     memset(t->reserved, 0, 6);
917     t->boot_status = 0; /* No errors detected */
918 
919     SMBIOS_BUILD_TABLE_POST;
920 }
921 
922 static void smbios_build_type_41_table(Error **errp)
923 {
924     unsigned instance = 0;
925     struct type41_instance *t41;
926 
927     QTAILQ_FOREACH(t41, &type41, next) {
928         SMBIOS_BUILD_TABLE_PRE(41, T41_BASE + instance, true);
929 
930         SMBIOS_TABLE_SET_STR(41, reference_designation_str, t41->designation);
931         t->device_type = t41->kind;
932         t->device_type_instance = t41->instance;
933         t->segment_group_number = cpu_to_le16(0);
934         t->bus_number = 0;
935         t->device_number = 0;
936 
937         if (t41->pcidev) {
938             PCIDevice *pdev = NULL;
939             int rc = pci_qdev_find_device(t41->pcidev, &pdev);
940             if (rc != 0) {
941                 error_setg(errp,
942                            "No PCI device %s for SMBIOS type 41 entry %s",
943                            t41->pcidev, t41->designation);
944                 return;
945             }
946             /*
947              * We only handle the case were the device is attached to
948              * the PCI root bus. The general case is more complex as
949              * bridges are enumerated later and the table would need
950              * to be updated at this moment.
951              */
952             if (!pci_bus_is_root(pci_get_bus(pdev))) {
953                 error_setg(errp,
954                            "Cannot create type 41 entry for PCI device %s: "
955                            "not attached to the root bus",
956                            t41->pcidev);
957                 return;
958             }
959             t->segment_group_number = cpu_to_le16(0);
960             t->bus_number = pci_dev_bus_num(pdev);
961             t->device_number = pdev->devfn;
962         }
963 
964         SMBIOS_BUILD_TABLE_POST;
965         instance++;
966     }
967 }
968 
969 static void smbios_build_type_127_table(void)
970 {
971     SMBIOS_BUILD_TABLE_PRE(127, T127_BASE, true); /* required */
972     SMBIOS_BUILD_TABLE_POST;
973 }
974 
975 void smbios_set_cpuid(uint32_t version, uint32_t features)
976 {
977     smbios_cpuid_version = version;
978     smbios_cpuid_features = features;
979 }
980 
981 #define SMBIOS_SET_DEFAULT(field, value)                                  \
982     if (!field) {                                                         \
983         field = value;                                                    \
984     }
985 
986 void smbios_set_defaults(const char *manufacturer, const char *product,
987                          const char *version, bool legacy_mode,
988                          bool uuid_encoded, SmbiosEntryPointType ep_type)
989 {
990     smbios_have_defaults = true;
991     smbios_legacy = legacy_mode;
992     smbios_uuid_encoded = uuid_encoded;
993     smbios_ep_type = ep_type;
994 
995     /* drop unwanted version of command-line file blob(s) */
996     if (smbios_legacy) {
997         g_free(smbios_tables);
998         /* in legacy mode, also complain if fields were given for types > 1 */
999         if (find_next_bit(have_fields_bitmap,
1000                           SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
1001             error_report("can't process fields for smbios "
1002                          "types > 1 on machine versions < 2.1!");
1003             exit(1);
1004         }
1005     } else {
1006         g_free(smbios_entries);
1007     }
1008 
1009     SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
1010     SMBIOS_SET_DEFAULT(type1.product, product);
1011     SMBIOS_SET_DEFAULT(type1.version, version);
1012     SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
1013     SMBIOS_SET_DEFAULT(type2.product, product);
1014     SMBIOS_SET_DEFAULT(type2.version, version);
1015     SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
1016     SMBIOS_SET_DEFAULT(type3.version, version);
1017     SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
1018     SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
1019     SMBIOS_SET_DEFAULT(type4.version, version);
1020     SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
1021     SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
1022 }
1023 
1024 static void smbios_entry_point_setup(void)
1025 {
1026     switch (smbios_ep_type) {
1027     case SMBIOS_ENTRY_POINT_TYPE_32:
1028         memcpy(ep.ep21.anchor_string, "_SM_", 4);
1029         memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
1030         ep.ep21.length = sizeof(struct smbios_21_entry_point);
1031         ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
1032         memset(ep.ep21.formatted_area, 0, 5);
1033 
1034         /* compliant with smbios spec v2.8 */
1035         ep.ep21.smbios_major_version = 2;
1036         ep.ep21.smbios_minor_version = 8;
1037         ep.ep21.smbios_bcd_revision = 0x28;
1038 
1039         /* set during table construction, but BIOS may override: */
1040         ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
1041         ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
1042         ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
1043 
1044         /* BIOS must recalculate */
1045         ep.ep21.checksum = 0;
1046         ep.ep21.intermediate_checksum = 0;
1047         ep.ep21.structure_table_address = cpu_to_le32(0);
1048 
1049         break;
1050     case SMBIOS_ENTRY_POINT_TYPE_64:
1051         memcpy(ep.ep30.anchor_string, "_SM3_", 5);
1052         ep.ep30.length = sizeof(struct smbios_30_entry_point);
1053         ep.ep30.entry_point_revision = 1;
1054         ep.ep30.reserved = 0;
1055 
1056         /* compliant with smbios spec 3.0 */
1057         ep.ep30.smbios_major_version = 3;
1058         ep.ep30.smbios_minor_version = 0;
1059         ep.ep30.smbios_doc_rev = 0;
1060 
1061         /* set during table construct, but BIOS might override */
1062         ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
1063 
1064         /* BIOS must recalculate */
1065         ep.ep30.checksum = 0;
1066         ep.ep30.structure_table_address = cpu_to_le64(0);
1067 
1068         break;
1069     default:
1070         abort();
1071         break;
1072     }
1073 }
1074 
1075 void smbios_get_tables(MachineState *ms,
1076                        const struct smbios_phys_mem_area *mem_array,
1077                        const unsigned int mem_array_size,
1078                        uint8_t **tables, size_t *tables_len,
1079                        uint8_t **anchor, size_t *anchor_len,
1080                        Error **errp)
1081 {
1082     unsigned i, dimm_cnt, offset;
1083 
1084     if (smbios_legacy) {
1085         *tables = *anchor = NULL;
1086         *tables_len = *anchor_len = 0;
1087         return;
1088     }
1089 
1090     if (!smbios_immutable) {
1091         smbios_build_type_0_table();
1092         smbios_build_type_1_table();
1093         smbios_build_type_2_table();
1094         smbios_build_type_3_table();
1095 
1096         smbios_smp_sockets = ms->smp.sockets;
1097         assert(smbios_smp_sockets >= 1);
1098 
1099         for (i = 0; i < smbios_smp_sockets; i++) {
1100             smbios_build_type_4_table(ms, i);
1101         }
1102 
1103         smbios_build_type_8_table();
1104         smbios_build_type_11_table();
1105 
1106 #define MAX_DIMM_SZ (16 * GiB)
1107 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
1108                                         : ((current_machine->ram_size - 1) % MAX_DIMM_SZ) + 1)
1109 
1110         dimm_cnt = QEMU_ALIGN_UP(current_machine->ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
1111 
1112         /*
1113          * The offset determines if we need to keep additional space betweeen
1114          * table 17 and table 19 header handle numbers so that they do
1115          * not overlap. For example, for a VM with larger than 8 TB guest
1116          * memory and DIMM like chunks of 16 GiB, the default space between
1117          * the two tables (T19_BASE - T17_BASE = 512) is not enough.
1118          */
1119         offset = (dimm_cnt > (T19_BASE - T17_BASE)) ? \
1120                  dimm_cnt - (T19_BASE - T17_BASE) : 0;
1121 
1122         smbios_build_type_16_table(dimm_cnt);
1123 
1124         for (i = 0; i < dimm_cnt; i++) {
1125             smbios_build_type_17_table(i, GET_DIMM_SZ);
1126         }
1127 
1128         for (i = 0; i < mem_array_size; i++) {
1129             smbios_build_type_19_table(i, offset, mem_array[i].address,
1130                                        mem_array[i].length);
1131         }
1132 
1133         /*
1134          * make sure 16 bit handle numbers in the headers of tables 19
1135          * and 32 do not overlap.
1136          */
1137         assert((mem_array_size + offset) < (T32_BASE - T19_BASE));
1138 
1139         smbios_build_type_32_table();
1140         smbios_build_type_38_table();
1141         smbios_build_type_41_table(errp);
1142         smbios_build_type_127_table();
1143 
1144         smbios_validate_table(ms);
1145         smbios_entry_point_setup();
1146         smbios_immutable = true;
1147     }
1148 
1149     /* return tables blob and entry point (anchor), and their sizes */
1150     *tables = smbios_tables;
1151     *tables_len = smbios_tables_len;
1152     *anchor = (uint8_t *)&ep;
1153 
1154     /* calculate length based on anchor string */
1155     if (!strncmp((char *)&ep, "_SM_", 4)) {
1156         *anchor_len = sizeof(struct smbios_21_entry_point);
1157     } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
1158         *anchor_len = sizeof(struct smbios_30_entry_point);
1159     } else {
1160         abort();
1161     }
1162 }
1163 
1164 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
1165 {
1166     const char *val = qemu_opt_get(opts, name);
1167 
1168     if (val) {
1169         *dest = val;
1170     }
1171 }
1172 
1173 
1174 struct opt_list {
1175     size_t *ndest;
1176     char ***dest;
1177 };
1178 
1179 static int save_opt_one(void *opaque,
1180                         const char *name, const char *value,
1181                         Error **errp)
1182 {
1183     struct opt_list *opt = opaque;
1184 
1185     if (g_str_equal(name, "path")) {
1186         g_autoptr(GByteArray) data = g_byte_array_new();
1187         g_autofree char *buf = g_new(char, 4096);
1188         ssize_t ret;
1189         int fd = qemu_open(value, O_RDONLY, errp);
1190         if (fd < 0) {
1191             return -1;
1192         }
1193 
1194         while (1) {
1195             ret = read(fd, buf, 4096);
1196             if (ret == 0) {
1197                 break;
1198             }
1199             if (ret < 0) {
1200                 error_setg(errp, "Unable to read from %s: %s",
1201                            value, strerror(errno));
1202                 qemu_close(fd);
1203                 return -1;
1204             }
1205             if (memchr(buf, '\0', ret)) {
1206                 error_setg(errp, "NUL in OEM strings value in %s", value);
1207                 qemu_close(fd);
1208                 return -1;
1209             }
1210             g_byte_array_append(data, (guint8 *)buf, ret);
1211         }
1212 
1213         qemu_close(fd);
1214 
1215         *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1);
1216         (*opt->dest)[*opt->ndest] = (char *)g_byte_array_free(data,  FALSE);
1217         (*opt->ndest)++;
1218         data = NULL;
1219    } else if (g_str_equal(name, "value")) {
1220         *opt->dest = g_renew(char *, *opt->dest, (*opt->ndest) + 1);
1221         (*opt->dest)[*opt->ndest] = g_strdup(value);
1222         (*opt->ndest)++;
1223     } else if (!g_str_equal(name, "type")) {
1224         error_setg(errp, "Unexpected option %s", name);
1225         return -1;
1226     }
1227 
1228     return 0;
1229 }
1230 
1231 static bool save_opt_list(size_t *ndest, char ***dest, QemuOpts *opts,
1232                           Error **errp)
1233 {
1234     struct opt_list opt = {
1235         ndest, dest,
1236     };
1237     if (!qemu_opt_foreach(opts, save_opt_one, &opt, errp)) {
1238         return false;
1239     }
1240     return true;
1241 }
1242 
1243 void smbios_entry_add(QemuOpts *opts, Error **errp)
1244 {
1245     const char *val;
1246 
1247     assert(!smbios_immutable);
1248 
1249     val = qemu_opt_get(opts, "file");
1250     if (val) {
1251         struct smbios_structure_header *header;
1252         int size;
1253         struct smbios_table *table; /* legacy mode only */
1254 
1255         if (!qemu_opts_validate(opts, qemu_smbios_file_opts, errp)) {
1256             return;
1257         }
1258 
1259         size = get_image_size(val);
1260         if (size == -1 || size < sizeof(struct smbios_structure_header)) {
1261             error_setg(errp, "Cannot read SMBIOS file %s", val);
1262             return;
1263         }
1264 
1265         /*
1266          * NOTE: standard double '\0' terminator expected, per smbios spec.
1267          * (except in legacy mode, where the second '\0' is implicit and
1268          *  will be inserted by the BIOS).
1269          */
1270         smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
1271         header = (struct smbios_structure_header *)(smbios_tables +
1272                                                     smbios_tables_len);
1273 
1274         if (load_image_size(val, (uint8_t *)header, size) != size) {
1275             error_setg(errp, "Failed to load SMBIOS file %s", val);
1276             return;
1277         }
1278 
1279         if (header->type <= SMBIOS_MAX_TYPE) {
1280             if (test_bit(header->type, have_fields_bitmap)) {
1281                 error_setg(errp,
1282                            "can't load type %d struct, fields already specified!",
1283                            header->type);
1284                 return;
1285             }
1286             set_bit(header->type, have_binfile_bitmap);
1287         }
1288 
1289         if (header->type == 4) {
1290             smbios_type4_count++;
1291         }
1292 
1293         smbios_tables_len += size;
1294         if (size > smbios_table_max) {
1295             smbios_table_max = size;
1296         }
1297         smbios_table_cnt++;
1298 
1299         /* add a copy of the newly loaded blob to legacy smbios_entries */
1300         /* NOTE: This code runs before smbios_set_defaults(), so we don't
1301          *       yet know which mode (legacy vs. aggregate-table) will be
1302          *       required. We therefore add the binary blob to both legacy
1303          *       (smbios_entries) and aggregate (smbios_tables) tables, and
1304          *       delete the one we don't need from smbios_set_defaults(),
1305          *       once we know which machine version has been requested.
1306          */
1307         if (!smbios_entries) {
1308             smbios_entries_len = sizeof(uint16_t);
1309             smbios_entries = g_malloc0(smbios_entries_len);
1310         }
1311         smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
1312                                                    size + sizeof(*table));
1313         table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
1314         table->header.type = SMBIOS_TABLE_ENTRY;
1315         table->header.length = cpu_to_le16(sizeof(*table) + size);
1316         memcpy(table->data, header, size);
1317         smbios_entries_len += sizeof(*table) + size;
1318         (*(uint16_t *)smbios_entries) =
1319                 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
1320         /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1321 
1322         return;
1323     }
1324 
1325     val = qemu_opt_get(opts, "type");
1326     if (val) {
1327         unsigned long type = strtoul(val, NULL, 0);
1328 
1329         if (type > SMBIOS_MAX_TYPE) {
1330             error_setg(errp, "out of range!");
1331             return;
1332         }
1333 
1334         if (test_bit(type, have_binfile_bitmap)) {
1335             error_setg(errp, "can't add fields, binary file already loaded!");
1336             return;
1337         }
1338         set_bit(type, have_fields_bitmap);
1339 
1340         switch (type) {
1341         case 0:
1342             if (!qemu_opts_validate(opts, qemu_smbios_type0_opts, errp)) {
1343                 return;
1344             }
1345             save_opt(&type0.vendor, opts, "vendor");
1346             save_opt(&type0.version, opts, "version");
1347             save_opt(&type0.date, opts, "date");
1348             type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1349 
1350             val = qemu_opt_get(opts, "release");
1351             if (val) {
1352                 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1353                     error_setg(errp, "Invalid release");
1354                     return;
1355                 }
1356                 type0.have_major_minor = true;
1357             }
1358             return;
1359         case 1:
1360             if (!qemu_opts_validate(opts, qemu_smbios_type1_opts, errp)) {
1361                 return;
1362             }
1363             save_opt(&type1.manufacturer, opts, "manufacturer");
1364             save_opt(&type1.product, opts, "product");
1365             save_opt(&type1.version, opts, "version");
1366             save_opt(&type1.serial, opts, "serial");
1367             save_opt(&type1.sku, opts, "sku");
1368             save_opt(&type1.family, opts, "family");
1369 
1370             val = qemu_opt_get(opts, "uuid");
1371             if (val) {
1372                 if (qemu_uuid_parse(val, &qemu_uuid) != 0) {
1373                     error_setg(errp, "Invalid UUID");
1374                     return;
1375                 }
1376                 qemu_uuid_set = true;
1377             }
1378             return;
1379         case 2:
1380             if (!qemu_opts_validate(opts, qemu_smbios_type2_opts, errp)) {
1381                 return;
1382             }
1383             save_opt(&type2.manufacturer, opts, "manufacturer");
1384             save_opt(&type2.product, opts, "product");
1385             save_opt(&type2.version, opts, "version");
1386             save_opt(&type2.serial, opts, "serial");
1387             save_opt(&type2.asset, opts, "asset");
1388             save_opt(&type2.location, opts, "location");
1389             return;
1390         case 3:
1391             if (!qemu_opts_validate(opts, qemu_smbios_type3_opts, errp)) {
1392                 return;
1393             }
1394             save_opt(&type3.manufacturer, opts, "manufacturer");
1395             save_opt(&type3.version, opts, "version");
1396             save_opt(&type3.serial, opts, "serial");
1397             save_opt(&type3.asset, opts, "asset");
1398             save_opt(&type3.sku, opts, "sku");
1399             return;
1400         case 4:
1401             if (!qemu_opts_validate(opts, qemu_smbios_type4_opts, errp)) {
1402                 return;
1403             }
1404             save_opt(&type4.sock_pfx, opts, "sock_pfx");
1405             save_opt(&type4.manufacturer, opts, "manufacturer");
1406             save_opt(&type4.version, opts, "version");
1407             save_opt(&type4.serial, opts, "serial");
1408             save_opt(&type4.asset, opts, "asset");
1409             save_opt(&type4.part, opts, "part");
1410             /* If the value is 0, it will take the value from the CPU model. */
1411             type4.processor_id = qemu_opt_get_number(opts, "processor-id", 0);
1412             type4.max_speed = qemu_opt_get_number(opts, "max-speed",
1413                                                   DEFAULT_CPU_SPEED);
1414             type4.current_speed = qemu_opt_get_number(opts, "current-speed",
1415                                                       DEFAULT_CPU_SPEED);
1416             if (type4.max_speed > UINT16_MAX ||
1417                 type4.current_speed > UINT16_MAX) {
1418                 error_setg(errp, "SMBIOS CPU speed is too large (> %d)",
1419                            UINT16_MAX);
1420             }
1421             return;
1422         case 8:
1423             if (!qemu_opts_validate(opts, qemu_smbios_type8_opts, errp)) {
1424                 return;
1425             }
1426             struct type8_instance *t;
1427             t = g_new0(struct type8_instance, 1);
1428             save_opt(&t->internal_reference, opts, "internal_reference");
1429             save_opt(&t->external_reference, opts, "external_reference");
1430             t->connector_type = qemu_opt_get_number(opts, "connector_type", 0);
1431             t->port_type = qemu_opt_get_number(opts, "port_type", 0);
1432             QTAILQ_INSERT_TAIL(&type8, t, next);
1433             return;
1434         case 11:
1435             if (!qemu_opts_validate(opts, qemu_smbios_type11_opts, errp)) {
1436                 return;
1437             }
1438             if (!save_opt_list(&type11.nvalues, &type11.values, opts, errp)) {
1439                 return;
1440             }
1441             return;
1442         case 17:
1443             if (!qemu_opts_validate(opts, qemu_smbios_type17_opts, errp)) {
1444                 return;
1445             }
1446             save_opt(&type17.loc_pfx, opts, "loc_pfx");
1447             save_opt(&type17.bank, opts, "bank");
1448             save_opt(&type17.manufacturer, opts, "manufacturer");
1449             save_opt(&type17.serial, opts, "serial");
1450             save_opt(&type17.asset, opts, "asset");
1451             save_opt(&type17.part, opts, "part");
1452             type17.speed = qemu_opt_get_number(opts, "speed", 0);
1453             return;
1454         case 41: {
1455             struct type41_instance *t;
1456             Error *local_err = NULL;
1457 
1458             if (!qemu_opts_validate(opts, qemu_smbios_type41_opts, errp)) {
1459                 return;
1460             }
1461             t = g_new0(struct type41_instance, 1);
1462             save_opt(&t->designation, opts, "designation");
1463             t->kind = qapi_enum_parse(&type41_kind_lookup,
1464                                       qemu_opt_get(opts, "kind"),
1465                                       0, &local_err) + 1;
1466             t->kind |= 0x80;     /* enabled */
1467             if (local_err != NULL) {
1468                 error_propagate(errp, local_err);
1469                 g_free(t);
1470                 return;
1471             }
1472             t->instance = qemu_opt_get_number(opts, "instance", 1);
1473             save_opt(&t->pcidev, opts, "pcidev");
1474 
1475             QTAILQ_INSERT_TAIL(&type41, t, next);
1476             return;
1477         }
1478         default:
1479             error_setg(errp,
1480                        "Don't know how to build fields for SMBIOS type %ld",
1481                        type);
1482             return;
1483         }
1484     }
1485 
1486     error_setg(errp, "Must specify type= or file=");
1487 }
1488