]> Git Repo - qemu.git/blob - hw/i386/smbios.c
SMBIOS: Rename symbols to better reflect future use
[qemu.git] / hw / i386 / smbios.c
1 /*
2  * SMBIOS Support
3  *
4  * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5  * Copyright (C) 2013 Red Hat, Inc.
6  *
7  * Authors:
8  *  Alex Williamson <[email protected]>
9  *  Markus Armbruster <[email protected]>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.  See
12  * the COPYING file in the top-level directory.
13  *
14  * Contributions after 2012-01-13 are licensed under the terms of the
15  * GNU GPL, version 2 or (at your option) any later version.
16  */
17
18 #include "qemu/config-file.h"
19 #include "qemu/error-report.h"
20 #include "sysemu/sysemu.h"
21 #include "hw/i386/smbios.h"
22 #include "hw/loader.h"
23
24
25 /* legacy structures and constants for <= 2.0 machines */
26 struct smbios_header {
27     uint16_t length;
28     uint8_t type;
29 } QEMU_PACKED;
30
31 struct smbios_field {
32     struct smbios_header header;
33     uint8_t type;
34     uint16_t offset;
35     uint8_t data[];
36 } QEMU_PACKED;
37
38 struct smbios_table {
39     struct smbios_header header;
40     uint8_t data[];
41 } QEMU_PACKED;
42
43 #define SMBIOS_FIELD_ENTRY 0
44 #define SMBIOS_TABLE_ENTRY 1
45
46 static uint8_t *smbios_entries;
47 static size_t smbios_entries_len;
48 /* end: legacy structures & constants for <= 2.0 machines */
49
50
51 static int smbios_type4_count = 0;
52 static bool smbios_immutable;
53
54 static struct {
55     bool seen;
56     int headertype;
57     Location loc;
58 } first_opt[2];
59
60 static struct {
61     const char *vendor, *version, *date;
62     bool have_major_minor;
63     uint8_t major, minor;
64 } type0;
65
66 static struct {
67     const char *manufacturer, *product, *version, *serial, *sku, *family;
68     /* uuid is in qemu_uuid[] */
69 } type1;
70
71 static QemuOptsList qemu_smbios_opts = {
72     .name = "smbios",
73     .head = QTAILQ_HEAD_INITIALIZER(qemu_smbios_opts.head),
74     .desc = {
75         /*
76          * no elements => accept any params
77          * validation will happen later
78          */
79         { /* end of list */ }
80     }
81 };
82
83 static const QemuOptDesc qemu_smbios_file_opts[] = {
84     {
85         .name = "file",
86         .type = QEMU_OPT_STRING,
87         .help = "binary file containing an SMBIOS element",
88     },
89     { /* end of list */ }
90 };
91
92 static const QemuOptDesc qemu_smbios_type0_opts[] = {
93     {
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",
101     },{
102         .name = "version",
103         .type = QEMU_OPT_STRING,
104         .help = "version number",
105     },{
106         .name = "date",
107         .type = QEMU_OPT_STRING,
108         .help = "release date",
109     },{
110         .name = "release",
111         .type = QEMU_OPT_STRING,
112         .help = "revision number",
113     },
114     { /* end of list */ }
115 };
116
117 static const QemuOptDesc qemu_smbios_type1_opts[] = {
118     {
119         .name = "type",
120         .type = QEMU_OPT_NUMBER,
121         .help = "SMBIOS element type",
122     },{
123         .name = "manufacturer",
124         .type = QEMU_OPT_STRING,
125         .help = "manufacturer name",
126     },{
127         .name = "product",
128         .type = QEMU_OPT_STRING,
129         .help = "product name",
130     },{
131         .name = "version",
132         .type = QEMU_OPT_STRING,
133         .help = "version number",
134     },{
135         .name = "serial",
136         .type = QEMU_OPT_STRING,
137         .help = "serial number",
138     },{
139         .name = "uuid",
140         .type = QEMU_OPT_STRING,
141         .help = "UUID",
142     },{
143         .name = "sku",
144         .type = QEMU_OPT_STRING,
145         .help = "SKU number",
146     },{
147         .name = "family",
148         .type = QEMU_OPT_STRING,
149         .help = "family name",
150     },
151     { /* end of list */ }
152 };
153
154 static void smbios_register_config(void)
155 {
156     qemu_add_opts(&qemu_smbios_opts);
157 }
158
159 machine_init(smbios_register_config);
160
161 static void smbios_validate_table(void)
162 {
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);
166     }
167 }
168
169 /*
170  * To avoid unresolvable overlaps in data, don't allow both
171  * tables and fields for the same smbios type.
172  */
173 static void smbios_check_collision(int type, int entry)
174 {
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);
183             }
184         } else {
185             first_opt[type].seen = true;
186             first_opt[type].headertype = entry;
187             loc_save(&first_opt[type].loc);
188         }
189     }
190 }
191
192
193 /* legacy setup functions for <= 2.0 machines */
194 static void smbios_add_field(int type, int offset, const void *data, size_t len)
195 {
196     struct smbios_field *field;
197
198     if (!smbios_entries) {
199         smbios_entries_len = sizeof(uint16_t);
200         smbios_entries = g_malloc0(smbios_entries_len);
201     }
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);
207
208     field->type = type;
209     field->offset = cpu_to_le16(offset);
210     memcpy(field->data, data, len);
211
212     smbios_entries_len += sizeof(*field) + len;
213     (*(uint16_t *)smbios_entries) =
214             cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
215 }
216
217 static void smbios_maybe_add_str(int type, int offset, const char *data)
218 {
219     if (data) {
220         smbios_add_field(type, offset, data, strlen(data) + 1);
221     }
222 }
223
224 static void smbios_build_type_0_fields(void)
225 {
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);
240     }
241 }
242
243 static void smbios_build_type_1_fields(void)
244 {
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);
260     }
261 }
262
263 void smbios_set_defaults(const char *manufacturer, const char *product,
264                          const char *version)
265 {
266     if (!type1.manufacturer) {
267         type1.manufacturer = manufacturer;
268     }
269     if (!type1.product) {
270         type1.product = product;
271     }
272     if (!type1.version) {
273         type1.version = version;
274     }
275 }
276
277 uint8_t *smbios_get_table_legacy(size_t *length)
278 {
279     if (!smbios_immutable) {
280         smbios_build_type_0_fields();
281         smbios_build_type_1_fields();
282         smbios_validate_table();
283         smbios_immutable = true;
284     }
285     *length = smbios_entries_len;
286     return smbios_entries;
287 }
288 /* end: legacy setup functions for <= 2.0 machines */
289
290
291 static void save_opt(const char **dest, QemuOpts *opts, const char *name)
292 {
293     const char *val = qemu_opt_get(opts, name);
294
295     if (val) {
296         *dest = val;
297     }
298 }
299
300 void smbios_entry_add(QemuOpts *opts)
301 {
302     Error *local_err = NULL;
303     const char *val;
304
305     assert(!smbios_immutable);
306     val = qemu_opt_get(opts, "file");
307     if (val) {
308         struct smbios_structure_header *header;
309         struct smbios_table *table;
310         int size;
311
312         qemu_opts_validate(opts, qemu_smbios_file_opts, &local_err);
313         if (local_err) {
314             error_report("%s", error_get_pretty(local_err));
315             exit(1);
316         }
317
318         size = get_image_size(val);
319         if (size == -1 || size < sizeof(struct smbios_structure_header)) {
320             error_report("Cannot read SMBIOS file %s", val);
321             exit(1);
322         }
323
324         if (!smbios_entries) {
325             smbios_entries_len = sizeof(uint16_t);
326             smbios_entries = g_malloc0(smbios_entries_len);
327         }
328
329         smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
330                                                       sizeof(*table) + size);
331         table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
332         table->header.type = SMBIOS_TABLE_ENTRY;
333         table->header.length = cpu_to_le16(sizeof(*table) + size);
334
335         if (load_image(val, table->data) != size) {
336             error_report("Failed to load SMBIOS file %s", val);
337             exit(1);
338         }
339
340         header = (struct smbios_structure_header *)(table->data);
341         smbios_check_collision(header->type, SMBIOS_TABLE_ENTRY);
342         if (header->type == 4) {
343             smbios_type4_count++;
344         }
345
346         smbios_entries_len += sizeof(*table) + size;
347         (*(uint16_t *)smbios_entries) =
348                 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
349         return;
350     }
351
352     val = qemu_opt_get(opts, "type");
353     if (val) {
354         unsigned long type = strtoul(val, NULL, 0);
355
356         smbios_check_collision(type, SMBIOS_FIELD_ENTRY);
357
358         switch (type) {
359         case 0:
360             qemu_opts_validate(opts, qemu_smbios_type0_opts, &local_err);
361             if (local_err) {
362                 error_report("%s", error_get_pretty(local_err));
363                 exit(1);
364             }
365             save_opt(&type0.vendor, opts, "vendor");
366             save_opt(&type0.version, opts, "version");
367             save_opt(&type0.date, opts, "date");
368
369             val = qemu_opt_get(opts, "release");
370             if (val) {
371                 if (sscanf(val, "%hhu.%hhu", &type0.major, &type0.minor) != 2) {
372                     error_report("Invalid release");
373                     exit(1);
374                 }
375                 type0.have_major_minor = true;
376             }
377             return;
378         case 1:
379             qemu_opts_validate(opts, qemu_smbios_type1_opts, &local_err);
380             if (local_err) {
381                 error_report("%s", error_get_pretty(local_err));
382                 exit(1);
383             }
384             save_opt(&type1.manufacturer, opts, "manufacturer");
385             save_opt(&type1.product, opts, "product");
386             save_opt(&type1.version, opts, "version");
387             save_opt(&type1.serial, opts, "serial");
388             save_opt(&type1.sku, opts, "sku");
389             save_opt(&type1.family, opts, "family");
390
391             val = qemu_opt_get(opts, "uuid");
392             if (val) {
393                 if (qemu_uuid_parse(val, qemu_uuid) != 0) {
394                     error_report("Invalid UUID");
395                     exit(1);
396                 }
397                 qemu_uuid_set = true;
398             }
399             return;
400         default:
401             error_report("Don't know how to build fields for SMBIOS type %ld",
402                          type);
403             exit(1);
404         }
405     }
406
407     error_report("Must specify type= or file=");
408     exit(1);
409 }
This page took 0.046287 seconds and 4 git commands to generate.