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