qerror: Remove assert_no_error()
[qemu.git] / hw / i386 / smbios.c
blobe8f41ad435a76223f52d22e6c80197ef05d9c9d9
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 * Structures shared with the BIOS
27 struct smbios_header {
28 uint16_t length;
29 uint8_t type;
30 } QEMU_PACKED;
32 struct smbios_field {
33 struct smbios_header header;
34 uint8_t type;
35 uint16_t offset;
36 uint8_t data[];
37 } QEMU_PACKED;
39 struct smbios_table {
40 struct smbios_header header;
41 uint8_t data[];
42 } QEMU_PACKED;
44 #define SMBIOS_FIELD_ENTRY 0
45 #define SMBIOS_TABLE_ENTRY 1
47 static uint8_t *smbios_entries;
48 static size_t smbios_entries_len;
49 static int smbios_type4_count = 0;
50 static bool smbios_immutable;
52 static struct {
53 bool seen;
54 int headertype;
55 Location loc;
56 } first_opt[2];
58 static struct {
59 const char *vendor, *version, *date;
60 bool have_major_minor;
61 uint8_t major, minor;
62 } type0;
64 static struct {
65 const char *manufacturer, *product, *version, *serial, *sku, *family;
66 /* uuid is in qemu_uuid[] */
67 } type1;
69 static QemuOptsList qemu_smbios_opts = {
70 .name = "smbios",
71 .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
72 .desc = {
74 * no elements => accept any params
75 * validation will happen later
77 { /* end of list */ }
81 static const QemuOptDesc qemu_smbios_file_opts[] = {
83 .name = "file",
84 .type = QEMU_OPT_STRING,
85 .help = "binary file containing an SMBIOS element",
87 { /* end of list */ }
90 static const QemuOptDesc qemu_smbios_type0_opts[] = {
92 .name = "type",
93 .type = QEMU_OPT_NUMBER,
94 .help = "SMBIOS element type",
95 },{
96 .name = "vendor",
97 .type = QEMU_OPT_STRING,
98 .help = "vendor name",
99 },{
100 .name = "version",
101 .type = QEMU_OPT_STRING,
102 .help = "version number",
104 .name = "date",
105 .type = QEMU_OPT_STRING,
106 .help = "release date",
108 .name = "release",
109 .type = QEMU_OPT_STRING,
110 .help = "revision number",
112 { /* end of list */ }
115 static const QemuOptDesc qemu_smbios_type1_opts[] = {
117 .name = "type",
118 .type = QEMU_OPT_NUMBER,
119 .help = "SMBIOS element type",
121 .name = "manufacturer",
122 .type = QEMU_OPT_STRING,
123 .help = "manufacturer name",
125 .name = "product",
126 .type = QEMU_OPT_STRING,
127 .help = "product name",
129 .name = "version",
130 .type = QEMU_OPT_STRING,
131 .help = "version number",
133 .name = "serial",
134 .type = QEMU_OPT_STRING,
135 .help = "serial number",
137 .name = "uuid",
138 .type = QEMU_OPT_STRING,
139 .help = "UUID",
141 .name = "sku",
142 .type = QEMU_OPT_STRING,
143 .help = "SKU number",
145 .name = "family",
146 .type = QEMU_OPT_STRING,
147 .help = "family name",
149 { /* end of list */ }
152 static void smbios_register_config(void)
154 qemu_add_opts(&qemu_smbios_opts);
157 machine_init(smbios_register_config);
159 static void smbios_validate_table(void)
161 if (smbios_type4_count && smbios_type4_count != smp_cpus) {
162 error_report("Number of SMBIOS Type 4 tables must match cpu count");
163 exit(1);
168 * To avoid unresolvable overlaps in data, don't allow both
169 * tables and fields for the same smbios type.
171 static void smbios_check_collision(int type, int entry)
173 if (type < ARRAY_SIZE(first_opt)) {
174 if (first_opt[type].seen) {
175 if (first_opt[type].headertype != entry) {
176 error_report("Can't mix file= and type= for same type");
177 loc_push_restore(&first_opt[type].loc);
178 error_report("This is the conflicting setting");
179 loc_pop(&first_opt[type].loc);
180 exit(1);
182 } else {
183 first_opt[type].seen = true;
184 first_opt[type].headertype = entry;
185 loc_save(&first_opt[type].loc);
190 static void smbios_add_field(int type, int offset, const void *data, size_t len)
192 struct smbios_field *field;
194 if (!smbios_entries) {
195 smbios_entries_len = sizeof(uint16_t);
196 smbios_entries = g_malloc0(smbios_entries_len);
198 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
199 sizeof(*field) + len);
200 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
201 field->header.type = SMBIOS_FIELD_ENTRY;
202 field->header.length = cpu_to_le16(sizeof(*field) + len);
204 field->type = type;
205 field->offset = cpu_to_le16(offset);
206 memcpy(field->data, data, len);
208 smbios_entries_len += sizeof(*field) + len;
209 (*(uint16_t *)smbios_entries) =
210 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
213 static void smbios_maybe_add_str(int type, int offset, const char *data)
215 if (data) {
216 smbios_add_field(type, offset, data, strlen(data) + 1);
220 static void smbios_build_type_0_fields(void)
222 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, vendor_str),
223 type0.vendor);
224 smbios_maybe_add_str(0, offsetof(struct smbios_type_0, bios_version_str),
225 type0.version);
226 smbios_maybe_add_str(0, offsetof(struct smbios_type_0,
227 bios_release_date_str),
228 type0.date);
229 if (type0.have_major_minor) {
230 smbios_add_field(0, offsetof(struct smbios_type_0,
231 system_bios_major_release),
232 &type0.major, 1);
233 smbios_add_field(0, offsetof(struct smbios_type_0,
234 system_bios_minor_release),
235 &type0.minor, 1);
239 static void smbios_build_type_1_fields(void)
241 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, manufacturer_str),
242 type1.manufacturer);
243 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, product_name_str),
244 type1.product);
245 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, version_str),
246 type1.version);
247 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, serial_number_str),
248 type1.serial);
249 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, sku_number_str),
250 type1.sku);
251 smbios_maybe_add_str(1, offsetof(struct smbios_type_1, family_str),
252 type1.family);
253 if (qemu_uuid_set) {
254 smbios_add_field(1, offsetof(struct smbios_type_1, uuid),
255 qemu_uuid, 16);
259 void smbios_set_type1_defaults(const char *manufacturer,
260 const char *product, const char *version)
262 if (!type1.manufacturer) {
263 type1.manufacturer = manufacturer;
265 if (!type1.product) {
266 type1.product = product;
268 if (!type1.version) {
269 type1.version = version;
273 uint8_t *smbios_get_table(size_t *length)
275 if (!smbios_immutable) {
276 smbios_build_type_0_fields();
277 smbios_build_type_1_fields();
278 smbios_validate_table();
279 smbios_immutable = true;
281 *length = smbios_entries_len;
282 return smbios_entries;
285 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
287 const char *val = qemu_opt_get(opts, name);
289 if (val) {
290 *dest = val;
294 void smbios_entry_add(QemuOpts *opts)
296 Error *local_err = NULL;
297 const char *val;
299 assert(!smbios_immutable);
300 val = qemu_opt_get(opts, "file");
301 if (val) {
302 struct smbios_structure_header *header;
303 struct smbios_table *table;
304 int size;
306 qemu_opts_validate(opts, qemu_smbios_file_opts, &local_err);
307 if (local_err) {
308 error_report("%s", error_get_pretty(local_err));
309 exit(1);
312 size = get_image_size(val);
313 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
314 error_report("Cannot read SMBIOS file %s", val);
315 exit(1);
318 if (!smbios_entries) {
319 smbios_entries_len = sizeof(uint16_t);
320 smbios_entries = g_malloc0(smbios_entries_len);
323 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
324 sizeof(*table) + size);
325 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
326 table->header.type = SMBIOS_TABLE_ENTRY;
327 table->header.length = cpu_to_le16(sizeof(*table) + size);
329 if (load_image(val, table->data) != size) {
330 error_report("Failed to load SMBIOS file %s", val);
331 exit(1);
334 header = (struct smbios_structure_header *)(table->data);
335 smbios_check_collision(header->type, SMBIOS_TABLE_ENTRY);
336 if (header->type == 4) {
337 smbios_type4_count++;
340 smbios_entries_len += sizeof(*table) + size;
341 (*(uint16_t *)smbios_entries) =
342 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
343 return;
346 val = qemu_opt_get(opts, "type");
347 if (val) {
348 unsigned long type = strtoul(val, NULL, 0);
350 smbios_check_collision(type, SMBIOS_FIELD_ENTRY);
352 switch (type) {
353 case 0:
354 qemu_opts_validate(opts, qemu_smbios_type0_opts, &local_err);
355 if (local_err) {
356 error_report("%s", error_get_pretty(local_err));
357 exit(1);
359 save_opt(&type0.vendor, opts, "vendor");
360 save_opt(&type0.version, opts, "version");
361 save_opt(&type0.date, opts, "date");
363 val = qemu_opt_get(opts, "release");
364 if (val) {
365 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
366 error_report("Invalid release");
367 exit(1);
369 type0.have_major_minor = true;
371 return;
372 case 1:
373 qemu_opts_validate(opts, qemu_smbios_type1_opts, &local_err);
374 if (local_err) {
375 error_report("%s", error_get_pretty(local_err));
376 exit(1);
378 save_opt(&type1.manufacturer, opts, "manufacturer");
379 save_opt(&type1.product, opts, "product");
380 save_opt(&type1.version, opts, "version");
381 save_opt(&type1.serial, opts, "serial");
382 save_opt(&type1.sku, opts, "sku");
383 save_opt(&type1.family, opts, "family");
385 val = qemu_opt_get(opts, "uuid");
386 if (val) {
387 if (qemu_uuid_parse(val, qemu_uuid) != 0) {
388 error_report("Invalid UUID");
389 exit(1);
391 qemu_uuid_set = true;
393 return;
394 default:
395 error_report("Don't know how to build fields for SMBIOS type %ld",
396 type);
397 exit(1);
401 error_report("Must specify type= or file=");
402 exit(1);