root / hw / smbios.c @ 636aa70a
History | View | Annotate | Download (7.3 kB)
1 | b6f6e3d3 | aliguori | /*
|
---|---|---|---|
2 | b6f6e3d3 | aliguori | * SMBIOS Support
|
3 | b6f6e3d3 | aliguori | *
|
4 | b6f6e3d3 | aliguori | * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
|
5 | b6f6e3d3 | aliguori | *
|
6 | b6f6e3d3 | aliguori | * Authors:
|
7 | b6f6e3d3 | aliguori | * Alex Williamson <alex.williamson@hp.com>
|
8 | b6f6e3d3 | aliguori | *
|
9 | b6f6e3d3 | aliguori | * This work is licensed under the terms of the GNU GPL, version 2. See
|
10 | b6f6e3d3 | aliguori | * the COPYING file in the top-level directory.
|
11 | b6f6e3d3 | aliguori | *
|
12 | b6f6e3d3 | aliguori | */
|
13 | b6f6e3d3 | aliguori | |
14 | b6f6e3d3 | aliguori | #include "sysemu.h" |
15 | b6f6e3d3 | aliguori | #include "smbios.h" |
16 | b6f6e3d3 | aliguori | |
17 | b6f6e3d3 | aliguori | /*
|
18 | b6f6e3d3 | aliguori | * Structures shared with the BIOS
|
19 | b6f6e3d3 | aliguori | */
|
20 | b6f6e3d3 | aliguori | struct smbios_header {
|
21 | b6f6e3d3 | aliguori | uint16_t length; |
22 | b6f6e3d3 | aliguori | uint8_t type; |
23 | b6f6e3d3 | aliguori | } __attribute__((__packed__)); |
24 | b6f6e3d3 | aliguori | |
25 | b6f6e3d3 | aliguori | struct smbios_field {
|
26 | b6f6e3d3 | aliguori | struct smbios_header header;
|
27 | b6f6e3d3 | aliguori | uint8_t type; |
28 | b6f6e3d3 | aliguori | uint16_t offset; |
29 | b6f6e3d3 | aliguori | uint8_t data[]; |
30 | b6f6e3d3 | aliguori | } __attribute__((__packed__)); |
31 | b6f6e3d3 | aliguori | |
32 | b6f6e3d3 | aliguori | struct smbios_table {
|
33 | b6f6e3d3 | aliguori | struct smbios_header header;
|
34 | b6f6e3d3 | aliguori | uint8_t data[]; |
35 | b6f6e3d3 | aliguori | } __attribute__((__packed__)); |
36 | b6f6e3d3 | aliguori | |
37 | b6f6e3d3 | aliguori | #define SMBIOS_FIELD_ENTRY 0 |
38 | b6f6e3d3 | aliguori | #define SMBIOS_TABLE_ENTRY 1 |
39 | b6f6e3d3 | aliguori | |
40 | b6f6e3d3 | aliguori | |
41 | b6f6e3d3 | aliguori | static uint8_t *smbios_entries;
|
42 | b6f6e3d3 | aliguori | static size_t smbios_entries_len;
|
43 | b6f6e3d3 | aliguori | |
44 | b6f6e3d3 | aliguori | uint8_t *smbios_get_table(size_t *length) |
45 | b6f6e3d3 | aliguori | { |
46 | b6f6e3d3 | aliguori | *length = smbios_entries_len; |
47 | b6f6e3d3 | aliguori | return smbios_entries;
|
48 | b6f6e3d3 | aliguori | } |
49 | b6f6e3d3 | aliguori | |
50 | b6f6e3d3 | aliguori | /*
|
51 | b6f6e3d3 | aliguori | * To avoid unresolvable overlaps in data, don't allow both
|
52 | b6f6e3d3 | aliguori | * tables and fields for the same smbios type.
|
53 | b6f6e3d3 | aliguori | */
|
54 | b6f6e3d3 | aliguori | static void smbios_check_collision(int type, int entry) |
55 | b6f6e3d3 | aliguori | { |
56 | b6f6e3d3 | aliguori | uint16_t *num_entries = (uint16_t *)smbios_entries; |
57 | b6f6e3d3 | aliguori | struct smbios_header *header;
|
58 | b6f6e3d3 | aliguori | char *p;
|
59 | b6f6e3d3 | aliguori | int i;
|
60 | b6f6e3d3 | aliguori | |
61 | b6f6e3d3 | aliguori | if (!num_entries)
|
62 | b6f6e3d3 | aliguori | return;
|
63 | b6f6e3d3 | aliguori | |
64 | b6f6e3d3 | aliguori | p = (char *)(num_entries + 1); |
65 | b6f6e3d3 | aliguori | |
66 | b6f6e3d3 | aliguori | for (i = 0; i < *num_entries; i++) { |
67 | b6f6e3d3 | aliguori | header = (struct smbios_header *)p;
|
68 | b6f6e3d3 | aliguori | if (entry == SMBIOS_TABLE_ENTRY && header->type == SMBIOS_FIELD_ENTRY) {
|
69 | b6f6e3d3 | aliguori | struct smbios_field *field = (void *)header; |
70 | b6f6e3d3 | aliguori | if (type == field->type) {
|
71 | b6f6e3d3 | aliguori | fprintf(stderr, "SMBIOS type %d field already defined, "
|
72 | b6f6e3d3 | aliguori | "cannot add table\n", type);
|
73 | b6f6e3d3 | aliguori | exit(1);
|
74 | b6f6e3d3 | aliguori | } |
75 | b6f6e3d3 | aliguori | } else if (entry == SMBIOS_FIELD_ENTRY && |
76 | b6f6e3d3 | aliguori | header->type == SMBIOS_TABLE_ENTRY) { |
77 | b6f6e3d3 | aliguori | struct smbios_structure_header *table = (void *)(header + 1); |
78 | b6f6e3d3 | aliguori | if (type == table->type) {
|
79 | b6f6e3d3 | aliguori | fprintf(stderr, "SMBIOS type %d table already defined, "
|
80 | b6f6e3d3 | aliguori | "cannot add field\n", type);
|
81 | b6f6e3d3 | aliguori | exit(1);
|
82 | b6f6e3d3 | aliguori | } |
83 | b6f6e3d3 | aliguori | } |
84 | b6f6e3d3 | aliguori | p += le16_to_cpu(header->length); |
85 | b6f6e3d3 | aliguori | } |
86 | b6f6e3d3 | aliguori | } |
87 | b6f6e3d3 | aliguori | |
88 | b6f6e3d3 | aliguori | void smbios_add_field(int type, int offset, int len, void *data) |
89 | b6f6e3d3 | aliguori | { |
90 | b6f6e3d3 | aliguori | struct smbios_field *field;
|
91 | b6f6e3d3 | aliguori | |
92 | b6f6e3d3 | aliguori | smbios_check_collision(type, SMBIOS_FIELD_ENTRY); |
93 | b6f6e3d3 | aliguori | |
94 | b6f6e3d3 | aliguori | if (!smbios_entries) {
|
95 | b6f6e3d3 | aliguori | smbios_entries_len = sizeof(uint16_t);
|
96 | b6f6e3d3 | aliguori | smbios_entries = qemu_mallocz(smbios_entries_len); |
97 | b6f6e3d3 | aliguori | } |
98 | b6f6e3d3 | aliguori | smbios_entries = qemu_realloc(smbios_entries, smbios_entries_len + |
99 | b6f6e3d3 | aliguori | sizeof(*field) + len);
|
100 | b6f6e3d3 | aliguori | field = (struct smbios_field *)(smbios_entries + smbios_entries_len);
|
101 | b6f6e3d3 | aliguori | field->header.type = SMBIOS_FIELD_ENTRY; |
102 | b6f6e3d3 | aliguori | field->header.length = cpu_to_le16(sizeof(*field) + len);
|
103 | b6f6e3d3 | aliguori | |
104 | b6f6e3d3 | aliguori | field->type = type; |
105 | b6f6e3d3 | aliguori | field->offset = cpu_to_le16(offset); |
106 | b6f6e3d3 | aliguori | memcpy(field->data, data, len); |
107 | b6f6e3d3 | aliguori | |
108 | b6f6e3d3 | aliguori | smbios_entries_len += sizeof(*field) + len;
|
109 | b6f6e3d3 | aliguori | (*(uint16_t *)smbios_entries) = |
110 | b6f6e3d3 | aliguori | cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
|
111 | b6f6e3d3 | aliguori | } |
112 | b6f6e3d3 | aliguori | |
113 | b6f6e3d3 | aliguori | static void smbios_build_type_0_fields(const char *t) |
114 | b6f6e3d3 | aliguori | { |
115 | b6f6e3d3 | aliguori | char buf[1024]; |
116 | b6f6e3d3 | aliguori | |
117 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "vendor", t)) |
118 | b6f6e3d3 | aliguori | smbios_add_field(0, offsetof(struct smbios_type_0, vendor_str), |
119 | b6f6e3d3 | aliguori | strlen(buf) + 1, buf);
|
120 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "version", t)) |
121 | b6f6e3d3 | aliguori | smbios_add_field(0, offsetof(struct smbios_type_0, bios_version_str), |
122 | b6f6e3d3 | aliguori | strlen(buf) + 1, buf);
|
123 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "date", t)) |
124 | b6f6e3d3 | aliguori | smbios_add_field(0, offsetof(struct smbios_type_0, |
125 | b6f6e3d3 | aliguori | bios_release_date_str), |
126 | b6f6e3d3 | aliguori | strlen(buf) + 1, buf);
|
127 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "release", t)) { |
128 | b6f6e3d3 | aliguori | int major, minor;
|
129 | b6f6e3d3 | aliguori | sscanf(buf, "%d.%d", &major, &minor);
|
130 | b6f6e3d3 | aliguori | smbios_add_field(0, offsetof(struct smbios_type_0, |
131 | b6f6e3d3 | aliguori | system_bios_major_release), 1, &major);
|
132 | b6f6e3d3 | aliguori | smbios_add_field(0, offsetof(struct smbios_type_0, |
133 | b6f6e3d3 | aliguori | system_bios_minor_release), 1, &minor);
|
134 | b6f6e3d3 | aliguori | } |
135 | b6f6e3d3 | aliguori | } |
136 | b6f6e3d3 | aliguori | |
137 | b6f6e3d3 | aliguori | static void smbios_build_type_1_fields(const char *t) |
138 | b6f6e3d3 | aliguori | { |
139 | b6f6e3d3 | aliguori | char buf[1024]; |
140 | b6f6e3d3 | aliguori | |
141 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "manufacturer", t)) |
142 | b6f6e3d3 | aliguori | smbios_add_field(1, offsetof(struct smbios_type_1, manufacturer_str), |
143 | b6f6e3d3 | aliguori | strlen(buf) + 1, buf);
|
144 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "product", t)) |
145 | b6f6e3d3 | aliguori | smbios_add_field(1, offsetof(struct smbios_type_1, product_name_str), |
146 | b6f6e3d3 | aliguori | strlen(buf) + 1, buf);
|
147 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "version", t)) |
148 | b6f6e3d3 | aliguori | smbios_add_field(1, offsetof(struct smbios_type_1, version_str), |
149 | b6f6e3d3 | aliguori | strlen(buf) + 1, buf);
|
150 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "serial", t)) |
151 | b6f6e3d3 | aliguori | smbios_add_field(1, offsetof(struct smbios_type_1, serial_number_str), |
152 | b6f6e3d3 | aliguori | strlen(buf) + 1, buf);
|
153 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "uuid", t)) { |
154 | b6f6e3d3 | aliguori | if (qemu_uuid_parse(buf, qemu_uuid) != 0) { |
155 | b6f6e3d3 | aliguori | fprintf(stderr, "Invalid SMBIOS UUID string\n");
|
156 | b6f6e3d3 | aliguori | exit(1);
|
157 | b6f6e3d3 | aliguori | } |
158 | b6f6e3d3 | aliguori | } |
159 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "sku", t)) |
160 | b6f6e3d3 | aliguori | smbios_add_field(1, offsetof(struct smbios_type_1, sku_number_str), |
161 | b6f6e3d3 | aliguori | strlen(buf) + 1, buf);
|
162 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "family", t)) |
163 | b6f6e3d3 | aliguori | smbios_add_field(1, offsetof(struct smbios_type_1, family_str), |
164 | b6f6e3d3 | aliguori | strlen(buf) + 1, buf);
|
165 | b6f6e3d3 | aliguori | } |
166 | b6f6e3d3 | aliguori | |
167 | b6f6e3d3 | aliguori | int smbios_entry_add(const char *t) |
168 | b6f6e3d3 | aliguori | { |
169 | b6f6e3d3 | aliguori | char buf[1024]; |
170 | b6f6e3d3 | aliguori | |
171 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "file", t)) { |
172 | b6f6e3d3 | aliguori | struct smbios_structure_header *header;
|
173 | b6f6e3d3 | aliguori | struct smbios_table *table;
|
174 | b6f6e3d3 | aliguori | int size = get_image_size(buf);
|
175 | b6f6e3d3 | aliguori | |
176 | b6f6e3d3 | aliguori | if (size < sizeof(struct smbios_structure_header)) { |
177 | b6f6e3d3 | aliguori | fprintf(stderr, "Cannot read smbios file %s", buf);
|
178 | b6f6e3d3 | aliguori | exit(1);
|
179 | b6f6e3d3 | aliguori | } |
180 | b6f6e3d3 | aliguori | |
181 | b6f6e3d3 | aliguori | if (!smbios_entries) {
|
182 | b6f6e3d3 | aliguori | smbios_entries_len = sizeof(uint16_t);
|
183 | b6f6e3d3 | aliguori | smbios_entries = qemu_mallocz(smbios_entries_len); |
184 | b6f6e3d3 | aliguori | } |
185 | b6f6e3d3 | aliguori | |
186 | b6f6e3d3 | aliguori | smbios_entries = qemu_realloc(smbios_entries, smbios_entries_len + |
187 | b6f6e3d3 | aliguori | sizeof(*table) + size);
|
188 | b6f6e3d3 | aliguori | table = (struct smbios_table *)(smbios_entries + smbios_entries_len);
|
189 | b6f6e3d3 | aliguori | table->header.type = SMBIOS_TABLE_ENTRY; |
190 | b6f6e3d3 | aliguori | table->header.length = cpu_to_le16(sizeof(*table) + size);
|
191 | b6f6e3d3 | aliguori | |
192 | b6f6e3d3 | aliguori | if (load_image(buf, table->data) != size) {
|
193 | b6f6e3d3 | aliguori | fprintf(stderr, "Failed to load smbios file %s", buf);
|
194 | b6f6e3d3 | aliguori | exit(1);
|
195 | b6f6e3d3 | aliguori | } |
196 | b6f6e3d3 | aliguori | |
197 | b6f6e3d3 | aliguori | header = (struct smbios_structure_header *)(table->data);
|
198 | b6f6e3d3 | aliguori | smbios_check_collision(header->type, SMBIOS_TABLE_ENTRY); |
199 | b6f6e3d3 | aliguori | |
200 | b6f6e3d3 | aliguori | smbios_entries_len += sizeof(*table) + size;
|
201 | b6f6e3d3 | aliguori | (*(uint16_t *)smbios_entries) = |
202 | b6f6e3d3 | aliguori | cpu_to_le16(le16_to_cpu(*(uint16_t *)smbios_entries) + 1);
|
203 | b6f6e3d3 | aliguori | return 0; |
204 | b6f6e3d3 | aliguori | } |
205 | b6f6e3d3 | aliguori | |
206 | b6f6e3d3 | aliguori | if (get_param_value(buf, sizeof(buf), "type", t)) { |
207 | b6f6e3d3 | aliguori | unsigned long type = strtoul(buf, NULL, 0); |
208 | b6f6e3d3 | aliguori | switch (type) {
|
209 | b6f6e3d3 | aliguori | case 0: |
210 | b6f6e3d3 | aliguori | smbios_build_type_0_fields(t); |
211 | b6f6e3d3 | aliguori | return 0; |
212 | b6f6e3d3 | aliguori | case 1: |
213 | b6f6e3d3 | aliguori | smbios_build_type_1_fields(t); |
214 | b6f6e3d3 | aliguori | return 0; |
215 | b6f6e3d3 | aliguori | default:
|
216 | b6f6e3d3 | aliguori | fprintf(stderr, "Don't know how to build fields for SMBIOS type "
|
217 | b6f6e3d3 | aliguori | "%ld\n", type);
|
218 | b6f6e3d3 | aliguori | exit(1);
|
219 | b6f6e3d3 | aliguori | } |
220 | b6f6e3d3 | aliguori | } |
221 | b6f6e3d3 | aliguori | |
222 | b6f6e3d3 | aliguori | fprintf(stderr, "smbios: must specify type= or file=\n");
|
223 | b6f6e3d3 | aliguori | return -1; |
224 | b6f6e3d3 | aliguori | } |