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