ia64/xen-unstable

view tools/firmware/hvmloader/smbios.c @ 19709:011948e1b5a7

hvmloader: Scan for gpxe-capable NICs until one is found.

Signed-off-by: Akio Takebe <takebe_akio@jp.fujitsu.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Jun 03 16:12:34 2009 +0100 (2009-06-03)
parents 95d8788bf4be
children
line source
1 /*
2 * smbios.c - Generate SMBIOS tables for Xen HVM domU's.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) IBM Corporation, 2006
19 *
20 * Authors: Andrew D. Ball <aball@us.ibm.com>
21 */
23 #include <stdint.h>
24 #include <xen/xen.h>
25 #include <xen/version.h>
26 #include "smbios_types.h"
27 #include "util.h"
28 #include "hypercall.h"
29 #include "e820.h"
31 static int
32 write_smbios_tables(void *start,
33 uint32_t vcpus, uint64_t memsize,
34 uint8_t uuid[16], char *xen_version,
35 uint32_t xen_major_version, uint32_t xen_minor_version);
37 static void
38 get_cpu_manufacturer(char *buf, int len);
39 static void
40 smbios_entry_point_init(void *start,
41 uint16_t max_structure_size,
42 uint16_t structure_table_length,
43 uint32_t structure_table_address,
44 uint16_t number_of_structures);
45 static void *
46 smbios_type_0_init(void *start, const char *xen_version,
47 uint32_t xen_major_version, uint32_t xen_minor_version);
48 static void *
49 smbios_type_1_init(void *start, const char *xen_version,
50 uint8_t uuid[16]);
51 static void *
52 smbios_type_3_init(void *start);
53 static void *
54 smbios_type_4_init(void *start, unsigned int cpu_number,
55 char *cpu_manufacturer);
56 static void *
57 smbios_type_16_init(void *start, uint32_t memory_size_mb, int nr_mem_devs);
58 static void *
59 smbios_type_17_init(void *start, uint32_t memory_size_mb, int instance);
60 static void *
61 smbios_type_19_init(void *start, uint32_t memory_size_mb, int instance);
62 static void *
63 smbios_type_20_init(void *start, uint32_t memory_size_mb, int instance);
64 static void *
65 smbios_type_32_init(void *start);
66 static void *
67 smbios_type_127_init(void *start);
69 static void
70 get_cpu_manufacturer(char *buf, int len)
71 {
72 char id[12];
73 uint32_t eax = 0;
75 cpuid(0, &eax, (uint32_t *)&id[0], (uint32_t *)&id[8],
76 (uint32_t *)&id[4]);
78 if (memcmp(id, "GenuineIntel", 12) == 0)
79 strncpy(buf, "Intel", len);
80 else if (memcmp(id, "AuthenticAMD", 12) == 0)
81 strncpy(buf, "AMD", len);
82 else
83 strncpy(buf, "unknown", len);
84 }
86 static int
87 write_smbios_tables(void *start,
88 uint32_t vcpus, uint64_t memsize,
89 uint8_t uuid[16], char *xen_version,
90 uint32_t xen_major_version, uint32_t xen_minor_version)
91 {
92 unsigned cpu_num, nr_structs = 0, max_struct_size = 0;
93 char *p, *q;
94 char cpu_manufacturer[15];
95 int i, nr_mem_devs;
97 get_cpu_manufacturer(cpu_manufacturer, 15);
99 p = (char *)start + sizeof(struct smbios_entry_point);
101 #define do_struct(fn) do { \
102 q = (fn); \
103 nr_structs++; \
104 if ( (q - p) > max_struct_size ) \
105 max_struct_size = q - p; \
106 p = q; \
107 } while (0)
109 do_struct(smbios_type_0_init(p, xen_version, xen_major_version,
110 xen_minor_version));
111 do_struct(smbios_type_1_init(p, xen_version, uuid));
112 do_struct(smbios_type_3_init(p));
113 for ( cpu_num = 1; cpu_num <= vcpus; cpu_num++ )
114 do_struct(smbios_type_4_init(p, cpu_num, cpu_manufacturer));
116 /* Each 'memory device' covers up to 16GB of address space. */
117 nr_mem_devs = (memsize + 0x3fff) >> 14;
118 do_struct(smbios_type_16_init(p, memsize, nr_mem_devs));
119 for ( i = 0; i < nr_mem_devs; i++ )
120 {
121 uint32_t dev_memsize = 0x4000; /* all but last covers 16GB */
122 if ( (i == (nr_mem_devs - 1)) && ((memsize & 0x3fff) != 0) )
123 dev_memsize = memsize & 0x3fff; /* last dev is <16GB */
124 do_struct(smbios_type_17_init(p, dev_memsize, i));
125 do_struct(smbios_type_19_init(p, dev_memsize, i));
126 do_struct(smbios_type_20_init(p, dev_memsize, i));
127 }
129 do_struct(smbios_type_32_init(p));
130 do_struct(smbios_type_127_init(p));
132 #undef do_struct
134 smbios_entry_point_init(
135 start, max_struct_size,
136 (p - (char *)start) - sizeof(struct smbios_entry_point),
137 SMBIOS_PHYSICAL_ADDRESS + sizeof(struct smbios_entry_point),
138 nr_structs);
140 return ((char *)p - (char *)start);
141 }
143 /* Calculate how much pseudo-physical memory (in MB) is allocated to us. */
144 static uint64_t
145 get_memsize(void)
146 {
147 uint64_t sz;
149 sz = (uint64_t)hvm_info->low_mem_pgend << PAGE_SHIFT;
150 if ( hvm_info->high_mem_pgend )
151 sz += (hvm_info->high_mem_pgend << PAGE_SHIFT) - (1ull << 32);
153 /*
154 * Round up to the nearest MB. The user specifies domU pseudo-physical
155 * memory in megabytes, so not doing this could easily lead to reporting
156 * one less MB than the user specified.
157 */
158 return (sz + (1ul << 20) - 1) >> 20;
159 }
161 int
162 hvm_write_smbios_tables(void)
163 {
164 xen_domain_handle_t uuid;
165 uint16_t xen_major_version, xen_minor_version;
166 uint32_t xen_version;
167 char xen_extra_version[XEN_EXTRAVERSION_LEN];
168 /* guess conservatively on buffer length for Xen version string */
169 char xen_version_str[80];
170 /* temporary variables used to build up Xen version string */
171 char *p = NULL; /* points to next point of insertion */
172 unsigned len = 0; /* length of string already composed */
173 char tmp[16]; /* holds result of itoa() */
174 unsigned tmp_len; /* length of next string to add */
176 hypercall_xen_version(XENVER_guest_handle, uuid);
177 BUILD_BUG_ON(sizeof(xen_domain_handle_t) != 16);
179 /* xen_version major and minor */
180 xen_version = hypercall_xen_version(XENVER_version, NULL);
181 xen_major_version = (uint16_t) (xen_version >> 16);
182 xen_minor_version = (uint16_t) xen_version;
184 hypercall_xen_version(XENVER_extraversion, xen_extra_version);
186 /* build up human-readable Xen version string */
187 p = xen_version_str;
188 len = 0;
190 itoa(tmp, xen_major_version);
191 tmp_len = strlen(tmp);
192 len += tmp_len;
193 if ( len >= sizeof(xen_version_str) )
194 goto error_out;
195 strcpy(p, tmp);
196 p += tmp_len;
198 len++;
199 if ( len >= sizeof(xen_version_str) )
200 goto error_out;
201 *p = '.';
202 p++;
204 itoa(tmp, xen_minor_version);
205 tmp_len = strlen(tmp);
206 len += tmp_len;
207 if ( len >= sizeof(xen_version_str) )
208 goto error_out;
209 strcpy(p, tmp);
210 p += tmp_len;
212 tmp_len = strlen(xen_extra_version);
213 len += tmp_len;
214 if ( len >= sizeof(xen_version_str) )
215 goto error_out;
216 strcpy(p, xen_extra_version);
217 p += tmp_len;
219 xen_version_str[sizeof(xen_version_str)-1] = '\0';
221 /* SCRATCH_PHYSICAL_ADDRESS is a safe large memory area for scratch. */
222 len = write_smbios_tables((void *)SCRATCH_PHYSICAL_ADDRESS,
223 hvm_info->nr_vcpus, get_memsize(),
224 uuid, xen_version_str,
225 xen_major_version, xen_minor_version);
226 if ( len > SMBIOS_MAXIMUM_SIZE )
227 goto error_out;
228 /* Okay, not too large: copy out of scratch to final location. */
229 memcpy((void *)SMBIOS_PHYSICAL_ADDRESS,
230 (void *)SCRATCH_PHYSICAL_ADDRESS, len);
232 return len;
234 error_out:
235 printf("Could not write SMBIOS tables, error in hvmloader.c:"
236 "hvm_write_smbios_tables()\n");
237 return 0;
238 }
241 static void
242 smbios_entry_point_init(void *start,
243 uint16_t max_structure_size,
244 uint16_t structure_table_length,
245 uint32_t structure_table_address,
246 uint16_t number_of_structures)
247 {
248 uint8_t sum;
249 int i;
250 struct smbios_entry_point *ep = (struct smbios_entry_point *)start;
252 memset(ep, 0, sizeof(*ep));
254 strncpy(ep->anchor_string, "_SM_", 4);
255 ep->length = 0x1f;
256 ep->smbios_major_version = 2;
257 ep->smbios_minor_version = 4;
258 ep->max_structure_size = max_structure_size;
259 ep->entry_point_revision = 0;
260 strncpy(ep->intermediate_anchor_string, "_DMI_", 5);
262 ep->structure_table_length = structure_table_length;
263 ep->structure_table_address = structure_table_address;
264 ep->number_of_structures = number_of_structures;
265 ep->smbios_bcd_revision = 0x24;
267 sum = 0;
268 for ( i = 0; i < 0x10; i++ )
269 sum += ((int8_t *)start)[i];
270 ep->checksum = -sum;
272 sum = 0;
273 for ( i = 0x10; i < ep->length; i++ )
274 sum += ((int8_t *)start)[i];
275 ep->intermediate_checksum = -sum;
276 }
278 /* Type 0 -- BIOS Information */
279 static void *
280 smbios_type_0_init(void *start, const char *xen_version,
281 uint32_t xen_major_version, uint32_t xen_minor_version)
282 {
283 struct smbios_type_0 *p = (struct smbios_type_0 *)start;
284 static const char *smbios_release_date = __SMBIOS_DATE__;
286 memset(p, 0, sizeof(*p));
288 p->header.type = 0;
289 p->header.length = sizeof(struct smbios_type_0);
290 p->header.handle = 0;
292 p->vendor_str = 1;
293 p->version_str = 2;
294 p->starting_address_segment = 0xe800;
295 p->release_date_str = 3;
296 p->rom_size = 0;
298 /* BIOS Characteristics. */
299 p->characteristics[0] = 0x80; /* PCI is supported */
300 p->characteristics[2] = 0x08; /* EDD is supported */
302 /* Extended Characteristics: Enable Targeted Content Distribution. */
303 p->characteristics_extension_bytes[1] = 0x04;
305 p->major_release = (uint8_t) xen_major_version;
306 p->minor_release = (uint8_t) xen_minor_version;
307 p->embedded_controller_major = 0xff;
308 p->embedded_controller_minor = 0xff;
310 start += sizeof(struct smbios_type_0);
311 strcpy((char *)start, "Xen");
312 start += strlen("Xen") + 1;
313 strcpy((char *)start, xen_version);
314 start += strlen(xen_version) + 1;
315 strcpy((char *)start, smbios_release_date);
316 start += strlen(smbios_release_date) + 1;
318 *((uint8_t *)start) = 0;
319 return start + 1;
320 }
322 /* Type 1 -- System Information */
323 static void *
324 smbios_type_1_init(void *start, const char *xen_version,
325 uint8_t uuid[16])
326 {
327 char uuid_str[37];
328 struct smbios_type_1 *p = (struct smbios_type_1 *)start;
330 memset(p, 0, sizeof(*p));
332 p->header.type = 1;
333 p->header.length = sizeof(struct smbios_type_1);
334 p->header.handle = 0x100;
336 p->manufacturer_str = 1;
337 p->product_name_str = 2;
338 p->version_str = 3;
339 p->serial_number_str = 4;
341 memcpy(p->uuid, uuid, 16);
343 p->wake_up_type = 0x06; /* power switch */
344 p->sku_str = 0;
345 p->family_str = 0;
347 start += sizeof(struct smbios_type_1);
349 strcpy((char *)start, "Xen");
350 start += strlen("Xen") + 1;
351 strcpy((char *)start, "HVM domU");
352 start += strlen("HVM domU") + 1;
353 strcpy((char *)start, xen_version);
354 start += strlen(xen_version) + 1;
355 uuid_to_string(uuid_str, uuid);
356 strcpy((char *)start, uuid_str);
357 start += strlen(uuid_str) + 1;
358 *((uint8_t *)start) = 0;
360 return start+1;
361 }
363 /* Type 3 -- System Enclosure */
364 static void *
365 smbios_type_3_init(void *start)
366 {
367 struct smbios_type_3 *p = (struct smbios_type_3 *)start;
369 memset(p, 0, sizeof(*p));
371 p->header.type = 3;
372 p->header.length = sizeof(struct smbios_type_3);
373 p->header.handle = 0x300;
375 p->manufacturer_str = 1;
376 p->type = 0x01; /* other */
377 p->version_str = 0;
378 p->serial_number_str = 0;
379 p->asset_tag_str = 0;
380 p->boot_up_state = 0x03; /* safe */
381 p->power_supply_state = 0x03; /* safe */
382 p->thermal_state = 0x03; /* safe */
383 p->security_status = 0x02; /* unknown */
385 start += sizeof(struct smbios_type_3);
387 strcpy((char *)start, "Xen");
388 start += strlen("Xen") + 1;
389 *((uint8_t *)start) = 0;
390 return start+1;
391 }
393 /* Type 4 -- Processor Information */
394 static void *
395 smbios_type_4_init(
396 void *start, unsigned int cpu_number, char *cpu_manufacturer)
397 {
398 char buf[80];
399 struct smbios_type_4 *p = (struct smbios_type_4 *)start;
400 uint32_t eax, ebx, ecx, edx;
402 memset(p, 0, sizeof(*p));
404 p->header.type = 4;
405 p->header.length = sizeof(struct smbios_type_4);
406 p->header.handle = 0x400 + cpu_number;
408 p->socket_designation_str = 1;
409 p->processor_type = 0x03; /* CPU */
410 p->processor_family = 0x01; /* other */
411 p->manufacturer_str = 2;
413 cpuid(1, &eax, &ebx, &ecx, &edx);
415 p->cpuid[0] = eax;
416 p->cpuid[1] = edx;
418 p->version_str = 0;
419 p->voltage = 0;
420 p->external_clock = 0;
422 p->max_speed = p->current_speed = get_cpu_mhz();
424 p->status = 0x41; /* socket populated, CPU enabled */
425 p->upgrade = 0x01; /* other */
427 start += sizeof(struct smbios_type_4);
429 strncpy(buf, "CPU ", sizeof(buf));
430 if ( (sizeof(buf) - strlen("CPU ")) >= 3 )
431 itoa(buf + strlen("CPU "), cpu_number);
433 strcpy((char *)start, buf);
434 start += strlen(buf) + 1;
436 strcpy((char *)start, cpu_manufacturer);
437 start += strlen(cpu_manufacturer) + 1;
439 *((uint8_t *)start) = 0;
440 return start+1;
441 }
443 /* Type 16 -- Physical Memory Array */
444 static void *
445 smbios_type_16_init(void *start, uint32_t memsize, int nr_mem_devs)
446 {
447 struct smbios_type_16 *p = (struct smbios_type_16*)start;
449 memset(p, 0, sizeof(*p));
451 p->header.type = 16;
452 p->header.handle = 0x1000;
453 p->header.length = sizeof(struct smbios_type_16);
455 p->location = 0x01; /* other */
456 p->use = 0x03; /* system memory */
457 p->error_correction = 0x01; /* other */
458 p->maximum_capacity = memsize * 1024;
459 p->memory_error_information_handle = 0xfffe; /* none provided */
460 p->number_of_memory_devices = nr_mem_devs;
462 start += sizeof(struct smbios_type_16);
463 *((uint16_t *)start) = 0;
464 return start + 2;
465 }
467 /* Type 17 -- Memory Device */
468 static void *
469 smbios_type_17_init(void *start, uint32_t memory_size_mb, int instance)
470 {
471 char buf[16];
472 struct smbios_type_17 *p = (struct smbios_type_17 *)start;
474 memset(p, 0, sizeof(*p));
476 p->header.type = 17;
477 p->header.length = sizeof(struct smbios_type_17);
478 p->header.handle = 0x1100 + instance;
480 p->physical_memory_array_handle = 0x1000;
481 p->total_width = 64;
482 p->data_width = 64;
483 ASSERT((memory_size_mb & ~0x7fff) == 0);
484 p->size = memory_size_mb;
485 p->form_factor = 0x09; /* DIMM */
486 p->device_set = 0;
487 p->device_locator_str = 1;
488 p->bank_locator_str = 0;
489 p->memory_type = 0x07; /* RAM */
490 p->type_detail = 0;
492 start += sizeof(struct smbios_type_17);
493 strcpy(start, "DIMM ");
494 start += strlen("DIMM ");
495 itoa(buf, instance);
496 strcpy(start, buf);
497 start += strlen(buf) + 1;
498 *((uint8_t *)start) = 0;
500 return start+1;
501 }
503 /* Type 19 -- Memory Array Mapped Address */
504 static void *
505 smbios_type_19_init(void *start, uint32_t memory_size_mb, int instance)
506 {
507 struct smbios_type_19 *p = (struct smbios_type_19 *)start;
509 memset(p, 0, sizeof(*p));
511 p->header.type = 19;
512 p->header.length = sizeof(struct smbios_type_19);
513 p->header.handle = 0x1300 + instance;
515 p->starting_address = instance << 24;
516 p->ending_address = p->starting_address + (memory_size_mb << 10) - 1;
517 p->memory_array_handle = 0x1000;
518 p->partition_width = 1;
520 start += sizeof(struct smbios_type_19);
521 *((uint16_t *)start) = 0;
522 return start + 2;
523 }
525 /* Type 20 -- Memory Device Mapped Address */
526 static void *
527 smbios_type_20_init(void *start, uint32_t memory_size_mb, int instance)
528 {
529 struct smbios_type_20 *p = (struct smbios_type_20 *)start;
531 memset(p, 0, sizeof(*p));
533 p->header.type = 20;
534 p->header.length = sizeof(struct smbios_type_20);
535 p->header.handle = 0x1400 + instance;
537 p->starting_address = instance << 24;
538 p->ending_address = p->starting_address + (memory_size_mb << 10) - 1;
539 p->memory_device_handle = 0x1100 + instance;
540 p->memory_array_mapped_address_handle = 0x1300 + instance;
541 p->partition_row_position = 1;
542 p->interleave_position = 0;
543 p->interleaved_data_depth = 0;
545 start += sizeof(struct smbios_type_20);
547 *((uint16_t *)start) = 0;
548 return start+2;
549 }
551 /* Type 32 -- System Boot Information */
552 static void *
553 smbios_type_32_init(void *start)
554 {
555 struct smbios_type_32 *p = (struct smbios_type_32 *)start;
557 memset(p, 0, sizeof(*p));
559 p->header.type = 32;
560 p->header.length = sizeof(struct smbios_type_32);
561 p->header.handle = 0x2000;
562 memset(p->reserved, 0, 6);
563 p->boot_status = 0; /* no errors detected */
565 start += sizeof(struct smbios_type_32);
566 *((uint16_t *)start) = 0;
567 return start+2;
568 }
570 /* Type 127 -- End of Table */
571 static void *
572 smbios_type_127_init(void *start)
573 {
574 struct smbios_type_127 *p = (struct smbios_type_127 *)start;
576 memset(p, 0, sizeof(*p));
578 p->header.type = 127;
579 p->header.length = sizeof(struct smbios_type_127);
580 p->header.handle = 0x7f00;
582 start += sizeof(struct smbios_type_127);
583 *((uint16_t *)start) = 0;
584 return start + 2;
585 }
587 /*
588 * Local variables:
589 * mode: C
590 * c-set-style: "BSD"
591 * c-basic-offset: 4
592 * tab-width: 4
593 * indent-tabs-mode: nil
594 * End:
595 */