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