xref: /qemu/hw/smbios/smbios.c (revision c418f935)
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 "smbios_build.h"
31 
32 /* legacy structures and constants for <= 2.0 machines */
33 struct smbios_header {
34     uint16_t length;
35     uint8_t type;
36 } QEMU_PACKED;
37 
38 struct smbios_field {
39     struct smbios_header header;
40     uint8_t type;
41     uint16_t offset;
42     uint8_t data[];
43 } QEMU_PACKED;
44 
45 struct smbios_table {
46     struct smbios_header header;
47     uint8_t data[];
48 } QEMU_PACKED;
49 
50 #define SMBIOS_FIELD_ENTRY 0
51 #define SMBIOS_TABLE_ENTRY 1
52 
53 static uint8_t *smbios_entries;
54 static size_t smbios_entries_len;
55 static bool smbios_legacy = true;
56 static bool smbios_uuid_encoded = true;
57 /* end: legacy structures & constants for <= 2.0 machines */
58 
59 
60 uint8_t *smbios_tables;
61 size_t smbios_tables_len;
62 unsigned smbios_table_max;
63 unsigned smbios_table_cnt;
64 static SmbiosEntryPointType smbios_ep_type = SMBIOS_ENTRY_POINT_21;
65 
66 static SmbiosEntryPoint ep;
67 
68 static int smbios_type4_count = 0;
69 static bool smbios_immutable;
70 static bool smbios_have_defaults;
71 static uint32_t smbios_cpuid_version, smbios_cpuid_features, smbios_smp_sockets;
72 
73 static DECLARE_BITMAP(have_binfile_bitmap, SMBIOS_MAX_TYPE+1);
74 static DECLARE_BITMAP(have_fields_bitmap, SMBIOS_MAX_TYPE+1);
75 
76 static struct {
77     const char *vendor, *version, *date;
78     bool have_major_minor, uefi;
79     uint8_t major, minor;
80 } type0;
81 
82 static struct {
83     const char *manufacturer, *product, *version, *serial, *sku, *family;
84     /* uuid is in qemu_uuid */
85 } type1;
86 
87 static struct {
88     const char *manufacturer, *product, *version, *serial, *asset, *location;
89 } type2;
90 
91 static struct {
92     const char *manufacturer, *version, *serial, *asset, *sku;
93 } type3;
94 
95 /*
96  * SVVP requires max_speed and current_speed to be set and not being
97  * 0 which counts as unknown (SMBIOS 3.1.0/Table 21). Set the
98  * default value to 2000MHz as we did before.
99  */
100 #define DEFAULT_CPU_SPEED 2000
101 
102 static struct {
103     const char *sock_pfx, *manufacturer, *version, *serial, *asset, *part;
104     uint64_t max_speed;
105     uint64_t current_speed;
106 } type4 = {
107     .max_speed = DEFAULT_CPU_SPEED,
108     .current_speed = DEFAULT_CPU_SPEED
109 };
110 
111 static struct {
112     size_t nvalues;
113     const char **values;
114 } type11;
115 
116 static struct {
117     const char *loc_pfx, *bank, *manufacturer, *serial, *asset, *part;
118     uint16_t speed;
119 } type17;
120 
121 static QemuOptsList qemu_smbios_opts = {
122     .name = "smbios",
123     .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
124     .desc = {
125         /*
126          * no elements => accept any params
127          * validation will happen later
128          */
129         { /* end of list */ }
130     }
131 };
132 
133 static const QemuOptDesc qemu_smbios_file_opts[] = {
134     {
135         .name = "file",
136         .type = QEMU_OPT_STRING,
137         .help = "binary file containing an SMBIOS element",
138     },
139     { /* end of list */ }
140 };
141 
142 static const QemuOptDesc qemu_smbios_type0_opts[] = {
143     {
144         .name = "type",
145         .type = QEMU_OPT_NUMBER,
146         .help = "SMBIOS element type",
147     },{
148         .name = "vendor",
149         .type = QEMU_OPT_STRING,
150         .help = "vendor name",
151     },{
152         .name = "version",
153         .type = QEMU_OPT_STRING,
154         .help = "version number",
155     },{
156         .name = "date",
157         .type = QEMU_OPT_STRING,
158         .help = "release date",
159     },{
160         .name = "release",
161         .type = QEMU_OPT_STRING,
162         .help = "revision number",
163     },{
164         .name = "uefi",
165         .type = QEMU_OPT_BOOL,
166         .help = "uefi support",
167     },
168     { /* end of list */ }
169 };
170 
171 static const QemuOptDesc qemu_smbios_type1_opts[] = {
172     {
173         .name = "type",
174         .type = QEMU_OPT_NUMBER,
175         .help = "SMBIOS element type",
176     },{
177         .name = "manufacturer",
178         .type = QEMU_OPT_STRING,
179         .help = "manufacturer name",
180     },{
181         .name = "product",
182         .type = QEMU_OPT_STRING,
183         .help = "product name",
184     },{
185         .name = "version",
186         .type = QEMU_OPT_STRING,
187         .help = "version number",
188     },{
189         .name = "serial",
190         .type = QEMU_OPT_STRING,
191         .help = "serial number",
192     },{
193         .name = "uuid",
194         .type = QEMU_OPT_STRING,
195         .help = "UUID",
196     },{
197         .name = "sku",
198         .type = QEMU_OPT_STRING,
199         .help = "SKU number",
200     },{
201         .name = "family",
202         .type = QEMU_OPT_STRING,
203         .help = "family name",
204     },
205     { /* end of list */ }
206 };
207 
208 static const QemuOptDesc qemu_smbios_type2_opts[] = {
209     {
210         .name = "type",
211         .type = QEMU_OPT_NUMBER,
212         .help = "SMBIOS element type",
213     },{
214         .name = "manufacturer",
215         .type = QEMU_OPT_STRING,
216         .help = "manufacturer name",
217     },{
218         .name = "product",
219         .type = QEMU_OPT_STRING,
220         .help = "product name",
221     },{
222         .name = "version",
223         .type = QEMU_OPT_STRING,
224         .help = "version number",
225     },{
226         .name = "serial",
227         .type = QEMU_OPT_STRING,
228         .help = "serial number",
229     },{
230         .name = "asset",
231         .type = QEMU_OPT_STRING,
232         .help = "asset tag number",
233     },{
234         .name = "location",
235         .type = QEMU_OPT_STRING,
236         .help = "location in chassis",
237     },
238     { /* end of list */ }
239 };
240 
241 static const QemuOptDesc qemu_smbios_type3_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 = "version",
252         .type = QEMU_OPT_STRING,
253         .help = "version number",
254     },{
255         .name = "serial",
256         .type = QEMU_OPT_STRING,
257         .help = "serial number",
258     },{
259         .name = "asset",
260         .type = QEMU_OPT_STRING,
261         .help = "asset tag number",
262     },{
263         .name = "sku",
264         .type = QEMU_OPT_STRING,
265         .help = "SKU number",
266     },
267     { /* end of list */ }
268 };
269 
270 static const QemuOptDesc qemu_smbios_type4_opts[] = {
271     {
272         .name = "type",
273         .type = QEMU_OPT_NUMBER,
274         .help = "SMBIOS element type",
275     },{
276         .name = "sock_pfx",
277         .type = QEMU_OPT_STRING,
278         .help = "socket designation string prefix",
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 = "max-speed",
289         .type = QEMU_OPT_NUMBER,
290         .help = "max speed in MHz",
291     },{
292         .name = "current-speed",
293         .type = QEMU_OPT_NUMBER,
294         .help = "speed at system boot in MHz",
295     },{
296         .name = "serial",
297         .type = QEMU_OPT_STRING,
298         .help = "serial number",
299     },{
300         .name = "asset",
301         .type = QEMU_OPT_STRING,
302         .help = "asset tag number",
303     },{
304         .name = "part",
305         .type = QEMU_OPT_STRING,
306         .help = "part number",
307     },
308     { /* end of list */ }
309 };
310 
311 static const QemuOptDesc qemu_smbios_type11_opts[] = {
312     {
313         .name = "value",
314         .type = QEMU_OPT_STRING,
315         .help = "OEM string data",
316     },
317 };
318 
319 static const QemuOptDesc qemu_smbios_type17_opts[] = {
320     {
321         .name = "type",
322         .type = QEMU_OPT_NUMBER,
323         .help = "SMBIOS element type",
324     },{
325         .name = "loc_pfx",
326         .type = QEMU_OPT_STRING,
327         .help = "device locator string prefix",
328     },{
329         .name = "bank",
330         .type = QEMU_OPT_STRING,
331         .help = "bank locator string",
332     },{
333         .name = "manufacturer",
334         .type = QEMU_OPT_STRING,
335         .help = "manufacturer name",
336     },{
337         .name = "serial",
338         .type = QEMU_OPT_STRING,
339         .help = "serial number",
340     },{
341         .name = "asset",
342         .type = QEMU_OPT_STRING,
343         .help = "asset tag number",
344     },{
345         .name = "part",
346         .type = QEMU_OPT_STRING,
347         .help = "part number",
348     },{
349         .name = "speed",
350         .type = QEMU_OPT_NUMBER,
351         .help = "maximum capable speed",
352     },
353     { /* end of list */ }
354 };
355 
356 static void smbios_register_config(void)
357 {
358     qemu_add_opts(&qemu_smbios_opts);
359 }
360 
361 opts_init(smbios_register_config);
362 
363 static void smbios_validate_table(MachineState *ms)
364 {
365     uint32_t expect_t4_count = smbios_legacy ?
366                                         ms->smp.cpus : smbios_smp_sockets;
367 
368     if (smbios_type4_count && smbios_type4_count != expect_t4_count) {
369         error_report("Expected %d SMBIOS Type 4 tables, got %d instead",
370                      expect_t4_count, smbios_type4_count);
371         exit(1);
372     }
373 }
374 
375 
376 /* legacy setup functions for <= 2.0 machines */
377 static void smbios_add_field(int type, int offset, const void *data, size_t len)
378 {
379     struct smbios_field *field;
380 
381     if (!smbios_entries) {
382         smbios_entries_len = sizeof(uint16_t);
383         smbios_entries = g_malloc0(smbios_entries_len);
384     }
385     smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
386                                                   sizeof(*field) + len);
387     field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
388     field->header.type = SMBIOS_FIELD_ENTRY;
389     field->header.length = cpu_to_le16(sizeof(*field) + len);
390 
391     field->type = type;
392     field->offset = cpu_to_le16(offset);
393     memcpy(field->data, data, len);
394 
395     smbios_entries_len += sizeof(*field) + len;
396     (*(uint16_t *)smbios_entries) =
397             cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
398 }
399 
400 static void smbios_maybe_add_str(int type, int offset, const char *data)
401 {
402     if (data) {
403         smbios_add_field(type, offset, data, strlen(data) + 1);
404     }
405 }
406 
407 static void smbios_build_type_0_fields(void)
408 {
409     smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
410                          type0.vendor);
411     smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
412                          type0.version);
413     smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
414                                      bios_release_date_str),
415                          type0.date);
416     if (type0.have_major_minor) {
417         smbios_add_field(0, offsetof(struct smbios_type_0,
418                                      system_bios_major_release),
419                          &type0.major, 1);
420         smbios_add_field(0, offsetof(struct smbios_type_0,
421                                      system_bios_minor_release),
422                          &type0.minor, 1);
423     }
424 }
425 
426 static void smbios_build_type_1_fields(void)
427 {
428     smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
429                          type1.manufacturer);
430     smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
431                          type1.product);
432     smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
433                          type1.version);
434     smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
435                          type1.serial);
436     smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
437                          type1.sku);
438     smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
439                          type1.family);
440     if (qemu_uuid_set) {
441         /* We don't encode the UUID in the "wire format" here because this
442          * function is for legacy mode and needs to keep the guest ABI, and
443          * because we don't know what's the SMBIOS version advertised by the
444          * BIOS.
445          */
446         smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
447                          &qemu_uuid, 16);
448     }
449 }
450 
451 uint8_t *smbios_get_table_legacy(MachineState *ms, size_t *length)
452 {
453     if (!smbios_legacy) {
454         *length = 0;
455         return NULL;
456     }
457 
458     if (!smbios_immutable) {
459         smbios_build_type_0_fields();
460         smbios_build_type_1_fields();
461         smbios_validate_table(ms);
462         smbios_immutable = true;
463     }
464     *length = smbios_entries_len;
465     return smbios_entries;
466 }
467 /* end: legacy setup functions for <= 2.0 machines */
468 
469 
470 bool smbios_skip_table(uint8_t type, bool required_table)
471 {
472     if (test_bit(type, have_binfile_bitmap)) {
473         return true; /* user provided their own binary blob(s) */
474     }
475     if (test_bit(type, have_fields_bitmap)) {
476         return false; /* user provided fields via command line */
477     }
478     if (smbios_have_defaults && required_table) {
479         return false; /* we're building tables, and this one's required */
480     }
481     return true;
482 }
483 
484 static void smbios_build_type_0_table(void)
485 {
486     SMBIOS_BUILD_TABLE_PRE(0, 0x000, false); /* optional, leave up to BIOS */
487 
488     SMBIOS_TABLE_SET_STR(0, vendor_str, type0.vendor);
489     SMBIOS_TABLE_SET_STR(0, bios_version_str, type0.version);
490 
491     t->bios_starting_address_segment = cpu_to_le16(0xE800); /* from SeaBIOS */
492 
493     SMBIOS_TABLE_SET_STR(0, bios_release_date_str, type0.date);
494 
495     t->bios_rom_size = 0; /* hardcoded in SeaBIOS with FIXME comment */
496 
497     t->bios_characteristics = cpu_to_le64(0x08); /* Not supported */
498     t->bios_characteristics_extension_bytes[0] = 0;
499     t->bios_characteristics_extension_bytes[1] = 0x14; /* TCD/SVVP | VM */
500     if (type0.uefi) {
501         t->bios_characteristics_extension_bytes[1] |= 0x08; /* |= UEFI */
502     }
503 
504     if (type0.have_major_minor) {
505         t->system_bios_major_release = type0.major;
506         t->system_bios_minor_release = type0.minor;
507     } else {
508         t->system_bios_major_release = 0;
509         t->system_bios_minor_release = 0;
510     }
511 
512     /* hardcoded in SeaBIOS */
513     t->embedded_controller_major_release = 0xFF;
514     t->embedded_controller_minor_release = 0xFF;
515 
516     SMBIOS_BUILD_TABLE_POST;
517 }
518 
519 /* Encode UUID from the big endian encoding described on RFC4122 to the wire
520  * format specified by SMBIOS version 2.6.
521  */
522 static void smbios_encode_uuid(struct smbios_uuid *uuid, QemuUUID *in)
523 {
524     memcpy(uuid, in, 16);
525     if (smbios_uuid_encoded) {
526         uuid->time_low = bswap32(uuid->time_low);
527         uuid->time_mid = bswap16(uuid->time_mid);
528         uuid->time_hi_and_version = bswap16(uuid->time_hi_and_version);
529     }
530 }
531 
532 static void smbios_build_type_1_table(void)
533 {
534     SMBIOS_BUILD_TABLE_PRE(1, 0x100, true); /* required */
535 
536     SMBIOS_TABLE_SET_STR(1, manufacturer_str, type1.manufacturer);
537     SMBIOS_TABLE_SET_STR(1, product_name_str, type1.product);
538     SMBIOS_TABLE_SET_STR(1, version_str, type1.version);
539     SMBIOS_TABLE_SET_STR(1, serial_number_str, type1.serial);
540     if (qemu_uuid_set) {
541         smbios_encode_uuid(&t->uuid, &qemu_uuid);
542     } else {
543         memset(&t->uuid, 0, 16);
544     }
545     t->wake_up_type = 0x06; /* power switch */
546     SMBIOS_TABLE_SET_STR(1, sku_number_str, type1.sku);
547     SMBIOS_TABLE_SET_STR(1, family_str, type1.family);
548 
549     SMBIOS_BUILD_TABLE_POST;
550 }
551 
552 static void smbios_build_type_2_table(void)
553 {
554     SMBIOS_BUILD_TABLE_PRE(2, 0x200, false); /* optional */
555 
556     SMBIOS_TABLE_SET_STR(2, manufacturer_str, type2.manufacturer);
557     SMBIOS_TABLE_SET_STR(2, product_str, type2.product);
558     SMBIOS_TABLE_SET_STR(2, version_str, type2.version);
559     SMBIOS_TABLE_SET_STR(2, serial_number_str, type2.serial);
560     SMBIOS_TABLE_SET_STR(2, asset_tag_number_str, type2.asset);
561     t->feature_flags = 0x01; /* Motherboard */
562     SMBIOS_TABLE_SET_STR(2, location_str, type2.location);
563     t->chassis_handle = cpu_to_le16(0x300); /* Type 3 (System enclosure) */
564     t->board_type = 0x0A; /* Motherboard */
565     t->contained_element_count = 0;
566 
567     SMBIOS_BUILD_TABLE_POST;
568 }
569 
570 static void smbios_build_type_3_table(void)
571 {
572     SMBIOS_BUILD_TABLE_PRE(3, 0x300, true); /* required */
573 
574     SMBIOS_TABLE_SET_STR(3, manufacturer_str, type3.manufacturer);
575     t->type = 0x01; /* Other */
576     SMBIOS_TABLE_SET_STR(3, version_str, type3.version);
577     SMBIOS_TABLE_SET_STR(3, serial_number_str, type3.serial);
578     SMBIOS_TABLE_SET_STR(3, asset_tag_number_str, type3.asset);
579     t->boot_up_state = 0x03; /* Safe */
580     t->power_supply_state = 0x03; /* Safe */
581     t->thermal_state = 0x03; /* Safe */
582     t->security_status = 0x02; /* Unknown */
583     t->oem_defined = cpu_to_le32(0);
584     t->height = 0;
585     t->number_of_power_cords = 0;
586     t->contained_element_count = 0;
587     t->contained_element_record_length = 0;
588     SMBIOS_TABLE_SET_STR(3, sku_number_str, type3.sku);
589 
590     SMBIOS_BUILD_TABLE_POST;
591 }
592 
593 static void smbios_build_type_4_table(MachineState *ms, unsigned instance)
594 {
595     char sock_str[128];
596 
597     SMBIOS_BUILD_TABLE_PRE(4, 0x400 + instance, true); /* required */
598 
599     snprintf(sock_str, sizeof(sock_str), "%s%2x", type4.sock_pfx, instance);
600     SMBIOS_TABLE_SET_STR(4, socket_designation_str, sock_str);
601     t->processor_type = 0x03; /* CPU */
602     t->processor_family = 0x01; /* Other */
603     SMBIOS_TABLE_SET_STR(4, processor_manufacturer_str, type4.manufacturer);
604     t->processor_id[0] = cpu_to_le32(smbios_cpuid_version);
605     t->processor_id[1] = cpu_to_le32(smbios_cpuid_features);
606     SMBIOS_TABLE_SET_STR(4, processor_version_str, type4.version);
607     t->voltage = 0;
608     t->external_clock = cpu_to_le16(0); /* Unknown */
609     t->max_speed = cpu_to_le16(type4.max_speed);
610     t->current_speed = cpu_to_le16(type4.current_speed);
611     t->status = 0x41; /* Socket populated, CPU enabled */
612     t->processor_upgrade = 0x01; /* Other */
613     t->l1_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
614     t->l2_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
615     t->l3_cache_handle = cpu_to_le16(0xFFFF); /* N/A */
616     SMBIOS_TABLE_SET_STR(4, serial_number_str, type4.serial);
617     SMBIOS_TABLE_SET_STR(4, asset_tag_number_str, type4.asset);
618     SMBIOS_TABLE_SET_STR(4, part_number_str, type4.part);
619     t->core_count = t->core_enabled = ms->smp.cores;
620     t->thread_count = ms->smp.threads;
621     t->processor_characteristics = cpu_to_le16(0x02); /* Unknown */
622     t->processor_family2 = cpu_to_le16(0x01); /* Other */
623 
624     SMBIOS_BUILD_TABLE_POST;
625     smbios_type4_count++;
626 }
627 
628 static void smbios_build_type_11_table(void)
629 {
630     char count_str[128];
631     size_t i;
632 
633     if (type11.nvalues == 0) {
634         return;
635     }
636 
637     SMBIOS_BUILD_TABLE_PRE(11, 0xe00, true); /* required */
638 
639     snprintf(count_str, sizeof(count_str), "%zu", type11.nvalues);
640     t->count = type11.nvalues;
641 
642     for (i = 0; i < type11.nvalues; i++) {
643         SMBIOS_TABLE_SET_STR_LIST(11, type11.values[i]);
644     }
645 
646     SMBIOS_BUILD_TABLE_POST;
647 }
648 
649 #define MAX_T16_STD_SZ 0x80000000 /* 2T in Kilobytes */
650 
651 static void smbios_build_type_16_table(unsigned dimm_cnt)
652 {
653     uint64_t size_kb;
654 
655     SMBIOS_BUILD_TABLE_PRE(16, 0x1000, true); /* required */
656 
657     t->location = 0x01; /* Other */
658     t->use = 0x03; /* System memory */
659     t->error_correction = 0x06; /* Multi-bit ECC (for Microsoft, per SeaBIOS) */
660     size_kb = QEMU_ALIGN_UP(ram_size, KiB) / KiB;
661     if (size_kb < MAX_T16_STD_SZ) {
662         t->maximum_capacity = cpu_to_le32(size_kb);
663         t->extended_maximum_capacity = cpu_to_le64(0);
664     } else {
665         t->maximum_capacity = cpu_to_le32(MAX_T16_STD_SZ);
666         t->extended_maximum_capacity = cpu_to_le64(ram_size);
667     }
668     t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
669     t->number_of_memory_devices = cpu_to_le16(dimm_cnt);
670 
671     SMBIOS_BUILD_TABLE_POST;
672 }
673 
674 #define MAX_T17_STD_SZ 0x7FFF /* (32G - 1M), in Megabytes */
675 #define MAX_T17_EXT_SZ 0x80000000 /* 2P, in Megabytes */
676 
677 static void smbios_build_type_17_table(unsigned instance, uint64_t size)
678 {
679     char loc_str[128];
680     uint64_t size_mb;
681 
682     SMBIOS_BUILD_TABLE_PRE(17, 0x1100 + instance, true); /* required */
683 
684     t->physical_memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
685     t->memory_error_information_handle = cpu_to_le16(0xFFFE); /* Not provided */
686     t->total_width = cpu_to_le16(0xFFFF); /* Unknown */
687     t->data_width = cpu_to_le16(0xFFFF); /* Unknown */
688     size_mb = QEMU_ALIGN_UP(size, MiB) / MiB;
689     if (size_mb < MAX_T17_STD_SZ) {
690         t->size = cpu_to_le16(size_mb);
691         t->extended_size = cpu_to_le32(0);
692     } else {
693         assert(size_mb < MAX_T17_EXT_SZ);
694         t->size = cpu_to_le16(MAX_T17_STD_SZ);
695         t->extended_size = cpu_to_le32(size_mb);
696     }
697     t->form_factor = 0x09; /* DIMM */
698     t->device_set = 0; /* Not in a set */
699     snprintf(loc_str, sizeof(loc_str), "%s %d", type17.loc_pfx, instance);
700     SMBIOS_TABLE_SET_STR(17, device_locator_str, loc_str);
701     SMBIOS_TABLE_SET_STR(17, bank_locator_str, type17.bank);
702     t->memory_type = 0x07; /* RAM */
703     t->type_detail = cpu_to_le16(0x02); /* Other */
704     t->speed = cpu_to_le16(type17.speed);
705     SMBIOS_TABLE_SET_STR(17, manufacturer_str, type17.manufacturer);
706     SMBIOS_TABLE_SET_STR(17, serial_number_str, type17.serial);
707     SMBIOS_TABLE_SET_STR(17, asset_tag_number_str, type17.asset);
708     SMBIOS_TABLE_SET_STR(17, part_number_str, type17.part);
709     t->attributes = 0; /* Unknown */
710     t->configured_clock_speed = t->speed; /* reuse value for max speed */
711     t->minimum_voltage = cpu_to_le16(0); /* Unknown */
712     t->maximum_voltage = cpu_to_le16(0); /* Unknown */
713     t->configured_voltage = cpu_to_le16(0); /* Unknown */
714 
715     SMBIOS_BUILD_TABLE_POST;
716 }
717 
718 static void smbios_build_type_19_table(unsigned instance,
719                                        uint64_t start, uint64_t size)
720 {
721     uint64_t end, start_kb, end_kb;
722 
723     SMBIOS_BUILD_TABLE_PRE(19, 0x1300 + instance, true); /* required */
724 
725     end = start + size - 1;
726     assert(end > start);
727     start_kb = start / KiB;
728     end_kb = end / KiB;
729     if (start_kb < UINT32_MAX && end_kb < UINT32_MAX) {
730         t->starting_address = cpu_to_le32(start_kb);
731         t->ending_address = cpu_to_le32(end_kb);
732         t->extended_starting_address =
733             t->extended_ending_address = cpu_to_le64(0);
734     } else {
735         t->starting_address = t->ending_address = cpu_to_le32(UINT32_MAX);
736         t->extended_starting_address = cpu_to_le64(start);
737         t->extended_ending_address = cpu_to_le64(end);
738     }
739     t->memory_array_handle = cpu_to_le16(0x1000); /* Type 16 above */
740     t->partition_width = 1; /* One device per row */
741 
742     SMBIOS_BUILD_TABLE_POST;
743 }
744 
745 static void smbios_build_type_32_table(void)
746 {
747     SMBIOS_BUILD_TABLE_PRE(32, 0x2000, true); /* required */
748 
749     memset(t->reserved, 0, 6);
750     t->boot_status = 0; /* No errors detected */
751 
752     SMBIOS_BUILD_TABLE_POST;
753 }
754 
755 static void smbios_build_type_127_table(void)
756 {
757     SMBIOS_BUILD_TABLE_PRE(127, 0x7F00, true); /* required */
758     SMBIOS_BUILD_TABLE_POST;
759 }
760 
761 void smbios_set_cpuid(uint32_t version, uint32_t features)
762 {
763     smbios_cpuid_version = version;
764     smbios_cpuid_features = features;
765 }
766 
767 #define SMBIOS_SET_DEFAULT(field, value)                                  \
768     if (!field) {                                                         \
769         field = value;                                                    \
770     }
771 
772 void smbios_set_defaults(const char *manufacturer, const char *product,
773                          const char *version, bool legacy_mode,
774                          bool uuid_encoded, SmbiosEntryPointType ep_type)
775 {
776     smbios_have_defaults = true;
777     smbios_legacy = legacy_mode;
778     smbios_uuid_encoded = uuid_encoded;
779     smbios_ep_type = ep_type;
780 
781     /* drop unwanted version of command-line file blob(s) */
782     if (smbios_legacy) {
783         g_free(smbios_tables);
784         /* in legacy mode, also complain if fields were given for types > 1 */
785         if (find_next_bit(have_fields_bitmap,
786                           SMBIOS_MAX_TYPE+1, 2) < SMBIOS_MAX_TYPE+1) {
787             error_report("can't process fields for smbios "
788                          "types > 1 on machine versions < 2.1!");
789             exit(1);
790         }
791     } else {
792         g_free(smbios_entries);
793     }
794 
795     SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
796     SMBIOS_SET_DEFAULT(type1.product, product);
797     SMBIOS_SET_DEFAULT(type1.version, version);
798     SMBIOS_SET_DEFAULT(type2.manufacturer, manufacturer);
799     SMBIOS_SET_DEFAULT(type2.product, product);
800     SMBIOS_SET_DEFAULT(type2.version, version);
801     SMBIOS_SET_DEFAULT(type3.manufacturer, manufacturer);
802     SMBIOS_SET_DEFAULT(type3.version, version);
803     SMBIOS_SET_DEFAULT(type4.sock_pfx, "CPU");
804     SMBIOS_SET_DEFAULT(type4.manufacturer, manufacturer);
805     SMBIOS_SET_DEFAULT(type4.version, version);
806     SMBIOS_SET_DEFAULT(type17.loc_pfx, "DIMM");
807     SMBIOS_SET_DEFAULT(type17.manufacturer, manufacturer);
808 }
809 
810 static void smbios_entry_point_setup(void)
811 {
812     switch (smbios_ep_type) {
813     case SMBIOS_ENTRY_POINT_21:
814         memcpy(ep.ep21.anchor_string, "_SM_", 4);
815         memcpy(ep.ep21.intermediate_anchor_string, "_DMI_", 5);
816         ep.ep21.length = sizeof(struct smbios_21_entry_point);
817         ep.ep21.entry_point_revision = 0; /* formatted_area reserved */
818         memset(ep.ep21.formatted_area, 0, 5);
819 
820         /* compliant with smbios spec v2.8 */
821         ep.ep21.smbios_major_version = 2;
822         ep.ep21.smbios_minor_version = 8;
823         ep.ep21.smbios_bcd_revision = 0x28;
824 
825         /* set during table construction, but BIOS may override: */
826         ep.ep21.structure_table_length = cpu_to_le16(smbios_tables_len);
827         ep.ep21.max_structure_size = cpu_to_le16(smbios_table_max);
828         ep.ep21.number_of_structures = cpu_to_le16(smbios_table_cnt);
829 
830         /* BIOS must recalculate */
831         ep.ep21.checksum = 0;
832         ep.ep21.intermediate_checksum = 0;
833         ep.ep21.structure_table_address = cpu_to_le32(0);
834 
835         break;
836     case SMBIOS_ENTRY_POINT_30:
837         memcpy(ep.ep30.anchor_string, "_SM3_", 5);
838         ep.ep30.length = sizeof(struct smbios_30_entry_point);
839         ep.ep30.entry_point_revision = 1;
840         ep.ep30.reserved = 0;
841 
842         /* compliant with smbios spec 3.0 */
843         ep.ep30.smbios_major_version = 3;
844         ep.ep30.smbios_minor_version = 0;
845         ep.ep30.smbios_doc_rev = 0;
846 
847         /* set during table construct, but BIOS might override */
848         ep.ep30.structure_table_max_size = cpu_to_le32(smbios_tables_len);
849 
850         /* BIOS must recalculate */
851         ep.ep30.checksum = 0;
852         ep.ep30.structure_table_address = cpu_to_le64(0);
853 
854         break;
855     default:
856         abort();
857         break;
858     }
859 }
860 
861 void smbios_get_tables(MachineState *ms,
862                        const struct smbios_phys_mem_area *mem_array,
863                        const unsigned int mem_array_size,
864                        uint8_t **tables, size_t *tables_len,
865                        uint8_t **anchor, size_t *anchor_len)
866 {
867     unsigned i, dimm_cnt;
868 
869     if (smbios_legacy) {
870         *tables = *anchor = NULL;
871         *tables_len = *anchor_len = 0;
872         return;
873     }
874 
875     if (!smbios_immutable) {
876         smbios_build_type_0_table();
877         smbios_build_type_1_table();
878         smbios_build_type_2_table();
879         smbios_build_type_3_table();
880 
881         smbios_smp_sockets = DIV_ROUND_UP(ms->smp.cpus,
882                                           ms->smp.cores * ms->smp.threads);
883         assert(smbios_smp_sockets >= 1);
884 
885         for (i = 0; i < smbios_smp_sockets; i++) {
886             smbios_build_type_4_table(ms, i);
887         }
888 
889         smbios_build_type_11_table();
890 
891 #define MAX_DIMM_SZ (16 * GiB)
892 #define GET_DIMM_SZ ((i < dimm_cnt - 1) ? MAX_DIMM_SZ \
893                                         : ((ram_size - 1) % MAX_DIMM_SZ) + 1)
894 
895         dimm_cnt = QEMU_ALIGN_UP(ram_size, MAX_DIMM_SZ) / MAX_DIMM_SZ;
896 
897         smbios_build_type_16_table(dimm_cnt);
898 
899         for (i = 0; i < dimm_cnt; i++) {
900             smbios_build_type_17_table(i, GET_DIMM_SZ);
901         }
902 
903         for (i = 0; i < mem_array_size; i++) {
904             smbios_build_type_19_table(i, mem_array[i].address,
905                                        mem_array[i].length);
906         }
907 
908         smbios_build_type_32_table();
909         smbios_build_type_38_table();
910         smbios_build_type_127_table();
911 
912         smbios_validate_table(ms);
913         smbios_entry_point_setup();
914         smbios_immutable = true;
915     }
916 
917     /* return tables blob and entry point (anchor), and their sizes */
918     *tables = smbios_tables;
919     *tables_len = smbios_tables_len;
920     *anchor = (uint8_t *)&ep;
921 
922     /* calculate length based on anchor string */
923     if (!strncmp((char *)&ep, "_SM_", 4)) {
924         *anchor_len = sizeof(struct smbios_21_entry_point);
925     } else if (!strncmp((char *)&ep, "_SM3_", 5)) {
926         *anchor_len = sizeof(struct smbios_30_entry_point);
927     } else {
928         abort();
929     }
930 }
931 
932 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
933 {
934     const char *val = qemu_opt_get(opts, name);
935 
936     if (val) {
937         *dest = val;
938     }
939 }
940 
941 
942 struct opt_list {
943     const char *name;
944     size_t *ndest;
945     const char ***dest;
946 };
947 
948 static int save_opt_one(void *opaque,
949                         const char *name, const char *value,
950                         Error **errp)
951 {
952     struct opt_list *opt = opaque;
953 
954     if (!g_str_equal(name, opt->name)) {
955         return 0;
956     }
957 
958     *opt->dest = g_renew(const char *, *opt->dest, (*opt->ndest) + 1);
959     (*opt->dest)[*opt->ndest] = value;
960     (*opt->ndest)++;
961     return 0;
962 }
963 
964 static void save_opt_list(size_t *ndest, const char ***dest,
965                           QemuOpts *opts, const char *name)
966 {
967     struct opt_list opt = {
968         name, ndest, dest,
969     };
970     qemu_opt_foreach(opts, save_opt_one, &opt, NULL);
971 }
972 
973 void smbios_entry_add(QemuOpts *opts, Error **errp)
974 {
975     const char *val;
976 
977     assert(!smbios_immutable);
978 
979     val = qemu_opt_get(opts, "file");
980     if (val) {
981         struct smbios_structure_header *header;
982         int size;
983         struct smbios_table *table; /* legacy mode only */
984 
985         if (!qemu_opts_validate(opts, qemu_smbios_file_opts, errp)) {
986             return;
987         }
988 
989         size = get_image_size(val);
990         if (size == -1 || size < sizeof(struct smbios_structure_header)) {
991             error_setg(errp, "Cannot read SMBIOS file %s", val);
992             return;
993         }
994 
995         /*
996          * NOTE: standard double '\0' terminator expected, per smbios spec.
997          * (except in legacy mode, where the second '\0' is implicit and
998          *  will be inserted by the BIOS).
999          */
1000         smbios_tables = g_realloc(smbios_tables, smbios_tables_len + size);
1001         header = (struct smbios_structure_header *)(smbios_tables +
1002                                                     smbios_tables_len);
1003 
1004         if (load_image_size(val, (uint8_t *)header, size) != size) {
1005             error_setg(errp, "Failed to load SMBIOS file %s", val);
1006             return;
1007         }
1008 
1009         if (test_bit(header->type, have_fields_bitmap)) {
1010             error_setg(errp,
1011                        "can't load type %d struct, fields already specified!",
1012                        header->type);
1013             return;
1014         }
1015         set_bit(header->type, have_binfile_bitmap);
1016 
1017         if (header->type == 4) {
1018             smbios_type4_count++;
1019         }
1020 
1021         smbios_tables_len += size;
1022         if (size > smbios_table_max) {
1023             smbios_table_max = size;
1024         }
1025         smbios_table_cnt++;
1026 
1027         /* add a copy of the newly loaded blob to legacy smbios_entries */
1028         /* NOTE: This code runs before smbios_set_defaults(), so we don't
1029          *       yet know which mode (legacy vs. aggregate-table) will be
1030          *       required. We therefore add the binary blob to both legacy
1031          *       (smbios_entries) and aggregate (smbios_tables) tables, and
1032          *       delete the one we don't need from smbios_set_defaults(),
1033          *       once we know which machine version has been requested.
1034          */
1035         if (!smbios_entries) {
1036             smbios_entries_len = sizeof(uint16_t);
1037             smbios_entries = g_malloc0(smbios_entries_len);
1038         }
1039         smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
1040                                                    size + sizeof(*table));
1041         table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
1042         table->header.type = SMBIOS_TABLE_ENTRY;
1043         table->header.length = cpu_to_le16(sizeof(*table) + size);
1044         memcpy(table->data, header, size);
1045         smbios_entries_len += sizeof(*table) + size;
1046         (*(uint16_t *)smbios_entries) =
1047                 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
1048         /* end: add a copy of the newly loaded blob to legacy smbios_entries */
1049 
1050         return;
1051     }
1052 
1053     val = qemu_opt_get(opts, "type");
1054     if (val) {
1055         unsigned long type = strtoul(val, NULL, 0);
1056 
1057         if (type > SMBIOS_MAX_TYPE) {
1058             error_setg(errp, "out of range!");
1059             return;
1060         }
1061 
1062         if (test_bit(type, have_binfile_bitmap)) {
1063             error_setg(errp, "can't add fields, binary file already loaded!");
1064             return;
1065         }
1066         set_bit(type, have_fields_bitmap);
1067 
1068         switch (type) {
1069         case 0:
1070             if (!qemu_opts_validate(opts, qemu_smbios_type0_opts, errp)) {
1071                 return;
1072             }
1073             save_opt(&type0.vendor, opts, "vendor");
1074             save_opt(&type0.version, opts, "version");
1075             save_opt(&type0.date, opts, "date");
1076             type0.uefi = qemu_opt_get_bool(opts, "uefi", false);
1077 
1078             val = qemu_opt_get(opts, "release");
1079             if (val) {
1080                 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
1081                     error_setg(errp, "Invalid release");
1082                     return;
1083                 }
1084                 type0.have_major_minor = true;
1085             }
1086             return;
1087         case 1:
1088             if (!qemu_opts_validate(opts, qemu_smbios_type1_opts, errp)) {
1089                 return;
1090             }
1091             save_opt(&type1.manufacturer, opts, "manufacturer");
1092             save_opt(&type1.product, opts, "product");
1093             save_opt(&type1.version, opts, "version");
1094             save_opt(&type1.serial, opts, "serial");
1095             save_opt(&type1.sku, opts, "sku");
1096             save_opt(&type1.family, opts, "family");
1097 
1098             val = qemu_opt_get(opts, "uuid");
1099             if (val) {
1100                 if (qemu_uuid_parse(val, &qemu_uuid) != 0) {
1101                     error_setg(errp, "Invalid UUID");
1102                     return;
1103                 }
1104                 qemu_uuid_set = true;
1105             }
1106             return;
1107         case 2:
1108             if (!qemu_opts_validate(opts, qemu_smbios_type2_opts, errp)) {
1109                 return;
1110             }
1111             save_opt(&type2.manufacturer, opts, "manufacturer");
1112             save_opt(&type2.product, opts, "product");
1113             save_opt(&type2.version, opts, "version");
1114             save_opt(&type2.serial, opts, "serial");
1115             save_opt(&type2.asset, opts, "asset");
1116             save_opt(&type2.location, opts, "location");
1117             return;
1118         case 3:
1119             if (!qemu_opts_validate(opts, qemu_smbios_type3_opts, errp)) {
1120                 return;
1121             }
1122             save_opt(&type3.manufacturer, opts, "manufacturer");
1123             save_opt(&type3.version, opts, "version");
1124             save_opt(&type3.serial, opts, "serial");
1125             save_opt(&type3.asset, opts, "asset");
1126             save_opt(&type3.sku, opts, "sku");
1127             return;
1128         case 4:
1129             if (!qemu_opts_validate(opts, qemu_smbios_type4_opts, errp)) {
1130                 return;
1131             }
1132             save_opt(&type4.sock_pfx, opts, "sock_pfx");
1133             save_opt(&type4.manufacturer, opts, "manufacturer");
1134             save_opt(&type4.version, opts, "version");
1135             save_opt(&type4.serial, opts, "serial");
1136             save_opt(&type4.asset, opts, "asset");
1137             save_opt(&type4.part, opts, "part");
1138             type4.max_speed = qemu_opt_get_number(opts, "max-speed",
1139                                                   DEFAULT_CPU_SPEED);
1140             type4.current_speed = qemu_opt_get_number(opts, "current-speed",
1141                                                       DEFAULT_CPU_SPEED);
1142             if (type4.max_speed > UINT16_MAX ||
1143                 type4.current_speed > UINT16_MAX) {
1144                 error_setg(errp, "SMBIOS CPU speed is too large (> %d)",
1145                            UINT16_MAX);
1146             }
1147             return;
1148         case 11:
1149             if (!qemu_opts_validate(opts, qemu_smbios_type11_opts, errp)) {
1150                 return;
1151             }
1152             save_opt_list(&type11.nvalues, &type11.values, opts, "value");
1153             return;
1154         case 17:
1155             if (!qemu_opts_validate(opts, qemu_smbios_type17_opts, errp)) {
1156                 return;
1157             }
1158             save_opt(&type17.loc_pfx, opts, "loc_pfx");
1159             save_opt(&type17.bank, opts, "bank");
1160             save_opt(&type17.manufacturer, opts, "manufacturer");
1161             save_opt(&type17.serial, opts, "serial");
1162             save_opt(&type17.asset, opts, "asset");
1163             save_opt(&type17.part, opts, "part");
1164             type17.speed = qemu_opt_get_number(opts, "speed", 0);
1165             return;
1166         default:
1167             error_setg(errp,
1168                        "Don't know how to build fields for SMBIOS type %ld",
1169                        type);
1170             return;
1171         }
1172     }
1173 
1174     error_setg(errp, "Must specify type= or file=");
1175 }
1176