]> Git Repo - qemu.git/blame - hw/i386/smbios.c
smbios: Fix -smbios type=0, release=... for big endian hosts
[qemu.git] / hw / i386 / smbios.c
CommitLineData
b6f6e3d3
AL
1/*
2 * SMBIOS Support
3 *
4 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
5 *
6 * Authors:
7 * Alex Williamson <[email protected]>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
b6f6e3d3
AL
14 */
15
5bb95e41 16#include "qemu/error-report.h"
9c17d615 17#include "sysemu/sysemu.h"
0d09e41a 18#include "hw/i386/smbios.h"
83c9f4ca 19#include "hw/loader.h"
b6f6e3d3
AL
20
21/*
22 * Structures shared with the BIOS
23 */
24struct smbios_header {
25 uint16_t length;
26 uint8_t type;
541dc0d4 27} QEMU_PACKED;
b6f6e3d3
AL
28
29struct smbios_field {
30 struct smbios_header header;
31 uint8_t type;
32 uint16_t offset;
33 uint8_t data[];
541dc0d4 34} QEMU_PACKED;
b6f6e3d3
AL
35
36struct smbios_table {
37 struct smbios_header header;
38 uint8_t data[];
541dc0d4 39} QEMU_PACKED;
b6f6e3d3
AL
40
41#define SMBIOS_FIELD_ENTRY 0
42#define SMBIOS_TABLE_ENTRY 1
43
44
45static uint8_t *smbios_entries;
46static size_t smbios_entries_len;
09c0848e
BK
47static int smbios_type4_count = 0;
48
49static void smbios_validate_table(void)
50{
51 if (smbios_type4_count && smbios_type4_count != smp_cpus) {
5bb95e41 52 error_report("Number of SMBIOS Type 4 tables must match cpu count");
09c0848e
BK
53 exit(1);
54 }
55}
b6f6e3d3
AL
56
57uint8_t *smbios_get_table(size_t *length)
58{
09c0848e 59 smbios_validate_table();
b6f6e3d3
AL
60 *length = smbios_entries_len;
61 return smbios_entries;
62}
63
64/*
65 * To avoid unresolvable overlaps in data, don't allow both
66 * tables and fields for the same smbios type.
67 */
68static void smbios_check_collision(int type, int entry)
69{
70 uint16_t *num_entries = (uint16_t *)smbios_entries;
71 struct smbios_header *header;
72 char *p;
73 int i;
74
75 if (!num_entries)
76 return;
77
78 p = (char *)(num_entries + 1);
79
80 for (i = 0; i < *num_entries; i++) {
81 header = (struct smbios_header *)p;
82 if (entry == SMBIOS_TABLE_ENTRY && header->type == SMBIOS_FIELD_ENTRY) {
83 struct smbios_field *field = (void *)header;
84 if (type == field->type) {
5bb95e41
MA
85 error_report("SMBIOS type %d field already defined, "
86 "cannot add table", type);
b6f6e3d3
AL
87 exit(1);
88 }
89 } else if (entry == SMBIOS_FIELD_ENTRY &&
90 header->type == SMBIOS_TABLE_ENTRY) {
91 struct smbios_structure_header *table = (void *)(header + 1);
92 if (type == table->type) {
5bb95e41
MA
93 error_report("SMBIOS type %d table already defined, "
94 "cannot add field", type);
b6f6e3d3
AL
95 exit(1);
96 }
97 }
98 p += le16_to_cpu(header->length);
99 }
100}
101
ebc85e3f 102void smbios_add_field(int type, int offset, const void *data, size_t len)
b6f6e3d3
AL
103{
104 struct smbios_field *field;
105
106 smbios_check_collision(type, SMBIOS_FIELD_ENTRY);
107
108 if (!smbios_entries) {
109 smbios_entries_len = sizeof(uint16_t);
7267c094 110 smbios_entries = g_malloc0(smbios_entries_len);
b6f6e3d3 111 }
7267c094 112 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
b6f6e3d3
AL
113 sizeof(*field) + len);
114 field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
115 field->header.type = SMBIOS_FIELD_ENTRY;
116 field->header.length = cpu_to_le16(sizeof(*field) + len);
117
118 field->type = type;
119 field->offset = cpu_to_le16(offset);
120 memcpy(field->data, data, len);
121
122 smbios_entries_len += sizeof(*field) + len;
123 (*(uint16_t *)smbios_entries) =
124 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
125}
126
127static void smbios_build_type_0_fields(const char *t)
128{
129 char buf[1024];
527cd96f 130 unsigned char major, minor;
b6f6e3d3
AL
131
132 if (get_param_value(buf, sizeof(buf), "vendor", t))
133 smbios_add_field(0, offsetof(struct smbios_type_0, vendor_str),
ebc85e3f 134 buf, strlen(buf) + 1);
b6f6e3d3
AL
135 if (get_param_value(buf, sizeof(buf), "version", t))
136 smbios_add_field(0, offsetof(struct smbios_type_0, bios_version_str),
ebc85e3f 137 buf, strlen(buf) + 1);
b6f6e3d3
AL
138 if (get_param_value(buf, sizeof(buf), "date", t))
139 smbios_add_field(0, offsetof(struct smbios_type_0,
140 bios_release_date_str),
ebc85e3f 141 buf, strlen(buf) + 1);
b6f6e3d3 142 if (get_param_value(buf, sizeof(buf), "release", t)) {
527cd96f 143 sscanf(buf, "%hhu.%hhu", &major, &minor);
b6f6e3d3 144 smbios_add_field(0, offsetof(struct smbios_type_0,
ebc85e3f
MA
145 system_bios_major_release),
146 &major, 1);
b6f6e3d3 147 smbios_add_field(0, offsetof(struct smbios_type_0,
ebc85e3f
MA
148 system_bios_minor_release),
149 &minor, 1);
b6f6e3d3
AL
150 }
151}
152
153static void smbios_build_type_1_fields(const char *t)
154{
155 char buf[1024];
156
157 if (get_param_value(buf, sizeof(buf), "manufacturer", t))
158 smbios_add_field(1, offsetof(struct smbios_type_1, manufacturer_str),
ebc85e3f 159 buf, strlen(buf) + 1);
b6f6e3d3
AL
160 if (get_param_value(buf, sizeof(buf), "product", t))
161 smbios_add_field(1, offsetof(struct smbios_type_1, product_name_str),
ebc85e3f 162 buf, strlen(buf) + 1);
b6f6e3d3
AL
163 if (get_param_value(buf, sizeof(buf), "version", t))
164 smbios_add_field(1, offsetof(struct smbios_type_1, version_str),
ebc85e3f 165 buf, strlen(buf) + 1);
b6f6e3d3
AL
166 if (get_param_value(buf, sizeof(buf), "serial", t))
167 smbios_add_field(1, offsetof(struct smbios_type_1, serial_number_str),
ebc85e3f 168 buf, strlen(buf) + 1);
b6f6e3d3
AL
169 if (get_param_value(buf, sizeof(buf), "uuid", t)) {
170 if (qemu_uuid_parse(buf, qemu_uuid) != 0) {
5bb95e41 171 error_report("Invalid UUID");
b6f6e3d3
AL
172 exit(1);
173 }
174 }
175 if (get_param_value(buf, sizeof(buf), "sku", t))
176 smbios_add_field(1, offsetof(struct smbios_type_1, sku_number_str),
ebc85e3f 177 buf, strlen(buf) + 1);
b6f6e3d3
AL
178 if (get_param_value(buf, sizeof(buf), "family", t))
179 smbios_add_field(1, offsetof(struct smbios_type_1, family_str),
ebc85e3f 180 buf, strlen(buf) + 1);
b6f6e3d3
AL
181}
182
183int smbios_entry_add(const char *t)
184{
185 char buf[1024];
186
187 if (get_param_value(buf, sizeof(buf), "file", t)) {
188 struct smbios_structure_header *header;
189 struct smbios_table *table;
190 int size = get_image_size(buf);
191
09c0848e 192 if (size == -1 || size < sizeof(struct smbios_structure_header)) {
5bb95e41 193 error_report("Cannot read SMBIOS file %s", buf);
b6f6e3d3
AL
194 exit(1);
195 }
196
197 if (!smbios_entries) {
198 smbios_entries_len = sizeof(uint16_t);
7267c094 199 smbios_entries = g_malloc0(smbios_entries_len);
b6f6e3d3
AL
200 }
201
7267c094 202 smbios_entries = g_realloc(smbios_entries, smbios_entries_len +
b6f6e3d3
AL
203 sizeof(*table) + size);
204 table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
205 table->header.type = SMBIOS_TABLE_ENTRY;
206 table->header.length = cpu_to_le16(sizeof(*table) + size);
207
208 if (load_image(buf, table->data) != size) {
5bb95e41 209 error_report("Failed to load SMBIOS file %s", buf);
b6f6e3d3
AL
210 exit(1);
211 }
212
213 header = (struct smbios_structure_header *)(table->data);
214 smbios_check_collision(header->type, SMBIOS_TABLE_ENTRY);
09c0848e
BK
215 if (header->type == 4) {
216 smbios_type4_count++;
217 }
b6f6e3d3
AL
218
219 smbios_entries_len += sizeof(*table) + size;
220 (*(uint16_t *)smbios_entries) =
221 cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
222 return 0;
223 }
224
225 if (get_param_value(buf, sizeof(buf), "type", t)) {
226 unsigned long type = strtoul(buf, NULL, 0);
227 switch (type) {
228 case 0:
229 smbios_build_type_0_fields(t);
230 return 0;
231 case 1:
232 smbios_build_type_1_fields(t);
233 return 0;
234 default:
5bb95e41
MA
235 error_report("Don't know how to build fields for SMBIOS type %ld",
236 type);
b6f6e3d3
AL
237 exit(1);
238 }
239 }
240
5bb95e41 241 error_report("Must specify type= or file=");
b6f6e3d3
AL
242 return -1;
243}
This page took 0.640296 seconds and 4 git commands to generate.