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