SMBIOS: Use macro to set smbios defaults
[qemu/ar7.git] / hw / i386 / smbios.c
blob9f83bfba58af2b211d46bc72b03cbb021f494d6f
1 /*
2 * SMBIOS Support
4 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5 * Copyright (C) 2013 Red Hat, Inc.
7 * Authors:
8 * Alex Williamson <alex.williamson@hp.com>
9 * Markus Armbruster <armbru@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
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.
18 #include "qemu/config-file.h"
19 #include "qemu/error-report.h"
20 #include "sysemu/sysemu.h"
21 #include "hw/i386/smbios.h"
22 #include "hw/loader.h"
25 /* legacy structures and constants for <= 2.0 machines */
26 struct smbios_header {
27 uint16_t length;
28 uint8_t type;
29 } QEMU_PACKED;
31 struct smbios_field {
32 struct smbios_header header;
33 uint8_t type;
34 uint16_t offset;
35 uint8_t data[];
36 } QEMU_PACKED;
38 struct smbios_table {
39 struct smbios_header header;
40 uint8_t data[];
41 } QEMU_PACKED;
43 #define SMBIOS_FIELD_ENTRY 0
44 #define SMBIOS_TABLE_ENTRY 1
46 static uint8_t *smbios_entries;
47 static size_t smbios_entries_len;
48 /* end: legacy structures & constants for <= 2.0 machines */
51 static int smbios_type4_count = 0;
52 static bool smbios_immutable;
54 static struct {
55 bool seen;
56 int headertype;
57 Location loc;
58 } first_opt[2];
60 static struct {
61 const char *vendor, *version, *date;
62 bool have_major_minor;
63 uint8_t major, minor;
64 } type0;
66 static struct {
67 const char *manufacturer, *product, *version, *serial, *sku, *family;
68 /* uuid is in qemu_uuid[] */
69 } type1;
71 static QemuOptsList qemu_smbios_opts = {
72 .name = "smbios",
73 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
74 .desc = {
76 * no elements => accept any params
77 * validation will happen later
79 { /* end of list */ }
83 static const QemuOptDesc qemu_smbios_file_opts[] = {
85 .name = "file",
86 .type = QEMU_OPT_STRING,
87 .help = "binary file containing an SMBIOS element",
89 { /* end of list */ }
92 static const QemuOptDesc qemu_smbios_type0_opts[] = {
94 .name = "type",
95 .type = QEMU_OPT_NUMBER,
96 .help = "SMBIOS element type",
97 },{
98 .name = "vendor",
99 .type = QEMU_OPT_STRING,
100 .help = "vendor name",
102 .name = "version",
103 .type = QEMU_OPT_STRING,
104 .help = "version number",
106 .name = "date",
107 .type = QEMU_OPT_STRING,
108 .help = "release date",
110 .name = "release",
111 .type = QEMU_OPT_STRING,
112 .help = "revision number",
114 { /* end of list */ }
117 static const QemuOptDesc qemu_smbios_type1_opts[] = {
119 .name = "type",
120 .type = QEMU_OPT_NUMBER,
121 .help = "SMBIOS element type",
123 .name = "manufacturer",
124 .type = QEMU_OPT_STRING,
125 .help = "manufacturer name",
127 .name = "product",
128 .type = QEMU_OPT_STRING,
129 .help = "product name",
131 .name = "version",
132 .type = QEMU_OPT_STRING,
133 .help = "version number",
135 .name = "serial",
136 .type = QEMU_OPT_STRING,
137 .help = "serial number",
139 .name = "uuid",
140 .type = QEMU_OPT_STRING,
141 .help = "UUID",
143 .name = "sku",
144 .type = QEMU_OPT_STRING,
145 .help = "SKU number",
147 .name = "family",
148 .type = QEMU_OPT_STRING,
149 .help = "family name",
151 { /* end of list */ }
154 static void smbios_register_config(void)
156 qemu_add_opts(&qemu_smbios_opts);
159 machine_init(smbios_register_config);
161 static void smbios_validate_table(void)
163 if (smbios_type4_count && smbios_type4_count != smp_cpus) {
164 error_report("Number of SMBIOS Type 4 tables must match cpu count");
165 exit(1);
170 * To avoid unresolvable overlaps in data, don't allow both
171 * tables and fields for the same smbios type.
173 static void smbios_check_collision(int type, int entry)
175 if (type < ARRAY_SIZE(first_opt)) {
176 if (first_opt[type].seen) {
177 if (first_opt[type].headertype != entry) {
178 error_report("Can't mix file= and type= for same type");
179 loc_push_restore(&first_opt[type].loc);
180 error_report("This is the conflicting setting");
181 loc_pop(&first_opt[type].loc);
182 exit(1);
184 } else {
185 first_opt[type].seen = true;
186 first_opt[type].headertype = entry;
187 loc_save(&first_opt[type].loc);
193 /* legacy setup functions for <= 2.0 machines */
194 static void smbios_add_field(int type, int offset, const void *data, size_t len)
196 struct smbios_field *field;
198 if (!smbios_entries) {
199 smbios_entries_len = sizeof(uint16_t);
200 smbios_entries = g_malloc0(smbios_entries_len);
202 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
203 sizeof(*field) + len);
204 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
205 field->header.type = SMBIOS_FIELD_ENTRY;
206 field->header.length = cpu_to_le16(sizeof(*field) + len);
208 field->type = type;
209 field->offset = cpu_to_le16(offset);
210 memcpy(field->data, data, len);
212 smbios_entries_len += sizeof(*field) + len;
213 (*(uint16_t *)smbios_entries) =
214 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
217 static void smbios_maybe_add_str(int type, int offset, const char *data)
219 if (data) {
220 smbios_add_field(type, offset, data, strlen(data) + 1);
224 static void smbios_build_type_0_fields(void)
226 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
227 type0.vendor);
228 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
229 type0.version);
230 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
231 bios_release_date_str),
232 type0.date);
233 if (type0.have_major_minor) {
234 smbios_add_field(0, offsetof(struct smbios_type_0,
235 system_bios_major_release),
236 &type0.major, 1);
237 smbios_add_field(0, offsetof(struct smbios_type_0,
238 system_bios_minor_release),
239 &type0.minor, 1);
243 static void smbios_build_type_1_fields(void)
245 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
246 type1.manufacturer);
247 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
248 type1.product);
249 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
250 type1.version);
251 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
252 type1.serial);
253 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
254 type1.sku);
255 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
256 type1.family);
257 if (qemu_uuid_set) {
258 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
259 qemu_uuid, 16);
263 uint8_t *smbios_get_table_legacy(size_t *length)
265 if (!smbios_immutable) {
266 smbios_build_type_0_fields();
267 smbios_build_type_1_fields();
268 smbios_validate_table();
269 smbios_immutable = true;
271 *length = smbios_entries_len;
272 return smbios_entries;
274 /* end: legacy setup functions for <= 2.0 machines */
277 #define SMBIOS_SET_DEFAULT(field, value) \
278 if (!field) { \
279 field = value; \
282 void smbios_set_defaults(const char *manufacturer, const char *product,
283 const char *version)
285 SMBIOS_SET_DEFAULT(type1.manufacturer, manufacturer);
286 SMBIOS_SET_DEFAULT(type1.product, product);
287 SMBIOS_SET_DEFAULT(type1.version, version);
290 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
292 const char *val = qemu_opt_get(opts, name);
294 if (val) {
295 *dest = val;
299 void smbios_entry_add(QemuOpts *opts)
301 Error *local_err = NULL;
302 const char *val;
304 assert(!smbios_immutable);
305 val = qemu_opt_get(opts, "file");
306 if (val) {
307 struct smbios_structure_header *header;
308 struct smbios_table *table;
309 int size;
311 qemu_opts_validate(opts, qemu_smbios_file_opts, &local_err);
312 if (local_err) {
313 error_report("%s", error_get_pretty(local_err));
314 exit(1);
317 size = get_image_size(val);
318 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
319 error_report("Cannot read SMBIOS file %s", val);
320 exit(1);
323 if (!smbios_entries) {
324 smbios_entries_len = sizeof(uint16_t);
325 smbios_entries = g_malloc0(smbios_entries_len);
328 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
329 sizeof(*table) + size);
330 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
331 table->header.type = SMBIOS_TABLE_ENTRY;
332 table->header.length = cpu_to_le16(sizeof(*table) + size);
334 if (load_image(val, table->data) != size) {
335 error_report("Failed to load SMBIOS file %s", val);
336 exit(1);
339 header = (struct smbios_structure_header *)(table->data);
340 smbios_check_collision(header->type, SMBIOS_TABLE_ENTRY);
341 if (header->type == 4) {
342 smbios_type4_count++;
345 smbios_entries_len += sizeof(*table) + size;
346 (*(uint16_t *)smbios_entries) =
347 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
348 return;
351 val = qemu_opt_get(opts, "type");
352 if (val) {
353 unsigned long type = strtoul(val, NULL, 0);
355 smbios_check_collision(type, SMBIOS_FIELD_ENTRY);
357 switch (type) {
358 case 0:
359 qemu_opts_validate(opts, qemu_smbios_type0_opts, &local_err);
360 if (local_err) {
361 error_report("%s", error_get_pretty(local_err));
362 exit(1);
364 save_opt(&type0.vendor, opts, "vendor");
365 save_opt(&type0.version, opts, "version");
366 save_opt(&type0.date, opts, "date");
368 val = qemu_opt_get(opts, "release");
369 if (val) {
370 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
371 error_report("Invalid release");
372 exit(1);
374 type0.have_major_minor = true;
376 return;
377 case 1:
378 qemu_opts_validate(opts, qemu_smbios_type1_opts, &local_err);
379 if (local_err) {
380 error_report("%s", error_get_pretty(local_err));
381 exit(1);
383 save_opt(&type1.manufacturer, opts, "manufacturer");
384 save_opt(&type1.product, opts, "product");
385 save_opt(&type1.version, opts, "version");
386 save_opt(&type1.serial, opts, "serial");
387 save_opt(&type1.sku, opts, "sku");
388 save_opt(&type1.family, opts, "family");
390 val = qemu_opt_get(opts, "uuid");
391 if (val) {
392 if (qemu_uuid_parse(val, qemu_uuid) != 0) {
393 error_report("Invalid UUID");
394 exit(1);
396 qemu_uuid_set = true;
398 return;
399 default:
400 error_report("Don't know how to build fields for SMBIOS type %ld",
401 type);
402 exit(1);
406 error_report("Must specify type= or file=");
407 exit(1);