Fix 32-bit overflow in parallels image support
[qemu-kvm/fedora.git] / pc-bios / bios-pq / 0012-load-smbios-entries-and-files-from-qemu.patch
blobe7a1204116142096687b3cf1c3d7619d0617c317
1 qemu:bios: Load SMBIOS entries and files from qemu (Alex Williamson)
3 Allow SMBIOS fields to be overridden and entries replaced by those
4 read from qemu.
6 Signed-off-by: Alex Williamson <alex.williamson@hp.com>
7 Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
9 diff --git a/bios/rombios32.c b/bios/rombios32.c
10 index 7be4216..1a1ed64 100644
11 --- a/bios/rombios32.c
12 +++ b/bios/rombios32.c
13 @@ -441,7 +441,6 @@ uint32_t cpuid_features;
14 uint32_t cpuid_ext_features;
15 unsigned long ram_size;
16 uint64_t ram_end;
17 -uint8_t bios_uuid[16];
18 #ifdef BX_USE_EBDA_TABLES
19 unsigned long ebda_cur_addr;
20 #endif
21 @@ -471,6 +470,7 @@ void wrmsr_smp(uint32_t index, uint64_t val)
22 #define QEMU_CFG_UUID 0x02
23 #define QEMU_CFG_ARCH_LOCAL 0x8000
24 #define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
25 +#define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
27 int qemu_cfg_port;
29 @@ -519,19 +519,17 @@ static int acpi_load_table(int i, uint32_t addr, uint16_t *len)
30 qemu_cfg_read((uint8_t*)addr, *len);
31 return 0;
33 -#endif
35 -void uuid_probe(void)
36 +static uint16_t smbios_entries(void)
38 -#ifdef BX_QEMU
39 - if(qemu_cfg_port) {
40 - qemu_cfg_select(QEMU_CFG_UUID);
41 - qemu_cfg_read(bios_uuid, 16);
42 - return;
43 - }
44 -#endif
45 - memset(bios_uuid, 0, 16);
46 + uint16_t cnt;
48 + qemu_cfg_select(QEMU_CFG_SMBIOS_ENTRIES);
49 + qemu_cfg_read((uint8_t*)&cnt, sizeof(cnt));
51 + return cnt;
53 +#endif
55 void cpu_probe(void)
57 @@ -1963,21 +1961,105 @@ smbios_entry_point_init(void *start,
58 ep->intermediate_checksum = -sum;
61 +struct smbios_header {
62 + uint16_t length;
63 + uint8_t type;
64 +} __attribute__((__packed__));
66 +struct smbios_field {
67 + struct smbios_header header;
68 + uint8_t type;
69 + uint16_t offset;
70 + uint8_t data[];
71 +} __attribute__((__packed__));
73 +struct smbios_table {
74 + struct smbios_header header;
75 + uint8_t data[];
76 +} __attribute__((__packed__));
78 +#define SMBIOS_FIELD_ENTRY 0
79 +#define SMBIOS_TABLE_ENTRY 1
81 +static size_t
82 +smbios_load_field(int type, size_t offset, void *addr)
84 +#ifdef BX_QEMU
85 + int i;
87 + for (i = smbios_entries(); i > 0; i--) {
88 + struct smbios_field field;
90 + qemu_cfg_read((uint8_t *)&field, sizeof(struct smbios_header));
91 + field.header.length -= sizeof(struct smbios_header);
93 + if (field.header.type != SMBIOS_FIELD_ENTRY) {
94 + while (field.header.length--)
95 + inb(QEMU_CFG_DATA_PORT);
96 + continue;
97 + }
99 + qemu_cfg_read((uint8_t *)&field.type,
100 + sizeof(field) - sizeof(struct smbios_header));
101 + field.header.length -= sizeof(field) - sizeof(struct smbios_header);
103 + if (field.type != type || field.offset != offset) {
104 + while (field.header.length--)
105 + inb(QEMU_CFG_DATA_PORT);
106 + continue;
109 + qemu_cfg_read(addr, field.header.length);
110 + return (size_t)field.header.length;
112 +#endif
113 + return 0;
116 +#define load_str_field_with_default(type, field, def) do { \
117 + size = smbios_load_field(type, offsetof(struct smbios_type_##type, \
118 + field), end); \
119 + if (size > 0) { \
120 + end += size; \
121 + } else { \
122 + memcpy(end, def, sizeof(def)); \
123 + end += sizeof(def); \
124 + } \
125 + p->field = ++str_index; \
126 +} while (0)
128 +#define load_str_field_or_skip(type, field) do { \
129 + size = smbios_load_field(type, offsetof(struct smbios_type_##type, \
130 + field), end); \
131 + if (size > 0) { \
132 + end += size; \
133 + p->field = ++str_index; \
134 + } else { \
135 + p->field = 0; \
136 + } \
137 +} while (0)
139 /* Type 0 -- BIOS Information */
140 #define RELEASE_DATE_STR "01/01/2007"
141 static void *
142 -smbios_type_0_init(void *start)
143 +smbios_init_type_0(void *start)
145 struct smbios_type_0 *p = (struct smbios_type_0 *)start;
146 + char *end = (char *)start + sizeof(struct smbios_type_0);
147 + size_t size;
148 + int str_index = 0;
150 p->header.type = 0;
151 p->header.length = sizeof(struct smbios_type_0);
152 p->header.handle = 0;
154 - p->vendor_str = 1;
155 - p->bios_version_str = 1;
156 + load_str_field_with_default(0, vendor_str, BX_APPNAME);
157 + load_str_field_with_default(0, bios_version_str, BX_APPNAME);
159 p->bios_starting_address_segment = 0xe800;
160 - p->bios_release_date_str = 2;
162 + load_str_field_with_default(0, bios_release_date_str, RELEASE_DATE_STR);
164 p->bios_rom_size = 0; /* FIXME */
166 memset(p->bios_characteristics, 0, 8);
167 @@ -1985,50 +2067,66 @@ smbios_type_0_init(void *start)
168 p->bios_characteristics_extension_bytes[0] = 0;
169 p->bios_characteristics_extension_bytes[1] = 0;
171 - p->system_bios_major_release = 1;
172 - p->system_bios_minor_release = 0;
173 + if (!smbios_load_field(0, offsetof(struct smbios_type_0,
174 + system_bios_major_release),
175 + &p->system_bios_major_release))
176 + p->system_bios_major_release = 1;
178 + if (!smbios_load_field(0, offsetof(struct smbios_type_0,
179 + system_bios_minor_release),
180 + &p->system_bios_minor_release))
181 + p->system_bios_minor_release = 0;
183 p->embedded_controller_major_release = 0xff;
184 p->embedded_controller_minor_release = 0xff;
186 - start += sizeof(struct smbios_type_0);
187 - memcpy((char *)start, BX_APPNAME, sizeof(BX_APPNAME));
188 - start += sizeof(BX_APPNAME);
189 - memcpy((char *)start, RELEASE_DATE_STR, sizeof(RELEASE_DATE_STR));
190 - start += sizeof(RELEASE_DATE_STR);
191 - *((uint8_t *)start) = 0;
192 + *end = 0;
193 + end++;
195 - return start+1;
196 + return end;
199 /* Type 1 -- System Information */
200 static void *
201 -smbios_type_1_init(void *start)
202 +smbios_init_type_1(void *start)
204 struct smbios_type_1 *p = (struct smbios_type_1 *)start;
205 + char *end = (char *)start + sizeof(struct smbios_type_1);
206 + size_t size;
207 + int str_index = 0;
209 p->header.type = 1;
210 p->header.length = sizeof(struct smbios_type_1);
211 p->header.handle = 0x100;
213 - p->manufacturer_str = 0;
214 - p->product_name_str = 0;
215 - p->version_str = 0;
216 - p->serial_number_str = 0;
217 + load_str_field_or_skip(1, manufacturer_str);
218 + load_str_field_or_skip(1, product_name_str);
219 + load_str_field_or_skip(1, version_str);
220 + load_str_field_or_skip(1, serial_number_str);
222 - memcpy(p->uuid, bios_uuid, 16);
223 + size = smbios_load_field(1, offsetof(struct smbios_type_1,
224 + uuid), &p->uuid);
225 + if (size == 0)
226 + memset(p->uuid, 0, 16);
228 p->wake_up_type = 0x06; /* power switch */
229 - p->sku_number_str = 0;
230 - p->family_str = 0;
232 - start += sizeof(struct smbios_type_1);
233 - *((uint16_t *)start) = 0;
234 + load_str_field_or_skip(1, sku_number_str);
235 + load_str_field_or_skip(1, family_str);
237 - return start+2;
238 + *end = 0;
239 + end++;
240 + if (!str_index) {
241 + *end = 0;
242 + end++;
245 + return end;
248 /* Type 3 -- System Enclosure */
249 static void *
250 -smbios_type_3_init(void *start)
251 +smbios_init_type_3(void *start)
253 struct smbios_type_3 *p = (struct smbios_type_3 *)start;
255 @@ -2058,7 +2156,7 @@ smbios_type_3_init(void *start)
257 /* Type 4 -- Processor Information */
258 static void *
259 -smbios_type_4_init(void *start, unsigned int cpu_number)
260 +smbios_init_type_4(void *start, unsigned int cpu_number)
262 struct smbios_type_4 *p = (struct smbios_type_4 *)start;
264 @@ -2098,7 +2196,7 @@ smbios_type_4_init(void *start, unsigned int cpu_number)
266 /* Type 16 -- Physical Memory Array */
267 static void *
268 -smbios_type_16_init(void *start, uint32_t memsize, int nr_mem_devs)
269 +smbios_init_type_16(void *start, uint32_t memsize, int nr_mem_devs)
271 struct smbios_type_16 *p = (struct smbios_type_16*)start;
273 @@ -2121,7 +2219,7 @@ smbios_type_16_init(void *start, uint32_t memsize, int nr_mem_devs)
275 /* Type 17 -- Memory Device */
276 static void *
277 -smbios_type_17_init(void *start, uint32_t memory_size_mb, int instance)
278 +smbios_init_type_17(void *start, uint32_t memory_size_mb, int instance)
280 struct smbios_type_17 *p = (struct smbios_type_17 *)start;
282 @@ -2151,7 +2249,7 @@ smbios_type_17_init(void *start, uint32_t memory_size_mb, int instance)
284 /* Type 19 -- Memory Array Mapped Address */
285 static void *
286 -smbios_type_19_init(void *start, uint32_t memory_size_mb, int instance)
287 +smbios_init_type_19(void *start, uint32_t memory_size_mb, int instance)
289 struct smbios_type_19 *p = (struct smbios_type_19 *)start;
291 @@ -2172,7 +2270,7 @@ smbios_type_19_init(void *start, uint32_t memory_size_mb, int instance)
293 /* Type 20 -- Memory Device Mapped Address */
294 static void *
295 -smbios_type_20_init(void *start, uint32_t memory_size_mb, int instance)
296 +smbios_init_type_20(void *start, uint32_t memory_size_mb, int instance)
298 struct smbios_type_20 *p = (struct smbios_type_20 *)start;
300 @@ -2196,7 +2294,7 @@ smbios_type_20_init(void *start, uint32_t memory_size_mb, int instance)
302 /* Type 32 -- System Boot Information */
303 static void *
304 -smbios_type_32_init(void *start)
305 +smbios_init_type_32(void *start)
307 struct smbios_type_32 *p = (struct smbios_type_32 *)start;
309 @@ -2214,7 +2312,7 @@ smbios_type_32_init(void *start)
311 /* Type 127 -- End of Table */
312 static void *
313 -smbios_type_127_init(void *start)
314 +smbios_init_type_127(void *start)
316 struct smbios_type_127 *p = (struct smbios_type_127 *)start;
318 @@ -2228,6 +2326,78 @@ smbios_type_127_init(void *start)
319 return start + 2;
322 +static int
323 +smbios_load_external(int type, char **p, unsigned *nr_structs,
324 + unsigned *max_struct_size)
326 +#ifdef BX_QEMU
327 + static uint64_t used_bitmap[4] = { 0 };
328 + char *start = *p;
329 + int i;
331 + /* Check if we've already reported these tables */
332 + if (used_bitmap[(type >> 6) & 0x3] & (1ULL << (type & 0x3f)))
333 + return 1;
335 + /* Don't introduce spurious end markers */
336 + if (type == 127)
337 + return 0;
339 + for (i = smbios_entries(); i > 0; i--) {
340 + struct smbios_table table;
341 + struct smbios_structure_header *header = (void *)*p;
342 + int string;
344 + qemu_cfg_read((uint8_t *)&table, sizeof(struct smbios_header));
345 + table.header.length -= sizeof(struct smbios_header);
347 + if (table.header.type != SMBIOS_TABLE_ENTRY) {
348 + while (table.header.length--)
349 + inb(QEMU_CFG_DATA_PORT);
350 + continue;
353 + qemu_cfg_read((uint8_t *)*p, sizeof(struct smbios_structure_header));
354 + table.header.length -= sizeof(struct smbios_structure_header);
356 + if (header->type != type) {
357 + while (table.header.length--)
358 + inb(QEMU_CFG_DATA_PORT);
359 + continue;
362 + *p += sizeof(struct smbios_structure_header);
364 + /* Entries end with a double NULL char, if there's a string at
365 + * the end (length is greater than formatted length), the string
366 + * terminator provides the first NULL. */
367 + string = header->length < table.header.length +
368 + sizeof(struct smbios_structure_header);
370 + /* Read the rest and terminate the entry */
371 + qemu_cfg_read((uint8_t *)*p, table.header.length);
372 + *p += table.header.length;
373 + *((uint8_t*)*p) = 0;
374 + (*p)++;
375 + if (!string) {
376 + *((uint8_t*)*p) = 0;
377 + (*p)++;
380 + (*nr_structs)++;
381 + if (*p - (char *)header > *max_struct_size)
382 + *max_struct_size = *p - (char *)header;
385 + /* Mark that we've reported on this type */
386 + used_bitmap[(type >> 6) & 0x3] |= (1ULL << (type & 0x3f));
388 + return (start != *p);
389 +#else /* !BX_QEMU */
390 + return 0;
391 +#endif
394 void smbios_init(void)
396 unsigned cpu_num, nr_structs = 0, max_struct_size = 0;
397 @@ -2246,34 +2416,39 @@ void smbios_init(void)
399 p = (char *)start + sizeof(struct smbios_entry_point);
401 -#define add_struct(fn) do{ \
402 - q = (fn); \
403 - nr_structs++; \
404 - if ((q - p) > max_struct_size) \
405 - max_struct_size = q - p; \
406 - p = q; \
407 -}while (0)
409 - add_struct(smbios_type_0_init(p));
410 - add_struct(smbios_type_1_init(p));
411 - add_struct(smbios_type_3_init(p));
412 +#define add_struct(type, args...) do { \
413 + if (!smbios_load_external(type, &p, &nr_structs, &max_struct_size)) { \
414 + q = smbios_init_type_##type(args); \
415 + nr_structs++; \
416 + if ((q - p) > max_struct_size) \
417 + max_struct_size = q - p; \
418 + p = q; \
419 + } \
420 +} while (0)
422 + add_struct(0, p);
423 + add_struct(1, p);
424 + add_struct(3, p);
425 for (cpu_num = 1; cpu_num <= smp_cpus; cpu_num++)
426 - add_struct(smbios_type_4_init(p, cpu_num));
427 + add_struct(4, p, cpu_num);
429 /* Each 'memory device' covers up to 16GB of address space. */
430 nr_mem_devs = (memsize + 0x3fff) >> 14;
431 - add_struct(smbios_type_16_init(p, memsize, nr_mem_devs));
432 + add_struct(16, p, memsize, nr_mem_devs);
433 for ( i = 0; i < nr_mem_devs; i++ )
435 uint32_t dev_memsize = ((i == (nr_mem_devs - 1))
436 ? (((memsize-1) & 0x3fff)+1) : 0x4000);
437 - add_struct(smbios_type_17_init(p, dev_memsize, i));
438 - add_struct(smbios_type_19_init(p, dev_memsize, i));
439 - add_struct(smbios_type_20_init(p, dev_memsize, i));
440 + add_struct(17, p, dev_memsize, i);
441 + add_struct(19, p, dev_memsize, i);
442 + add_struct(20, p, dev_memsize, i);
445 - add_struct(smbios_type_32_init(p));
446 - add_struct(smbios_type_127_init(p));
447 + add_struct(32, p);
448 + /* Add any remaining provided entries before the end marker */
449 + for (i = 0; i < 256; i++)
450 + smbios_load_external(i, &p, &nr_structs, &max_struct_size);
451 + add_struct(127, p);
453 #undef add_struct
455 @@ -2380,8 +2555,6 @@ void rombios32_init(uint32_t *s3_resume_vector, uint8_t *shutdown_flag)
457 mptable_init();
459 - uuid_probe();
461 smbios_init();
463 if (acpi_enabled)
467 To unsubscribe from this list: send the line "unsubscribe kvm" in
468 the body of a message to majordomo@vger.kernel.org
469 More majordomo info at http://vger.kernel.org/majordomo-info.html