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