Fix VMware backdoor command 0Ah - patch from bochs bios.
[seabios.git] / src / smbios.c
1 // smbios table generation (on emulators)
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "memmap.h" // bios_table_cur_addr
10 #include "biosvar.h" // GET_EBDA
11
12
13 /****************************************************************
14  * UUID probe
15  ****************************************************************/
16
17 static void
18 uuid_probe(u8 *bios_uuid)
19 {
20     // Default to UUID not set
21     memset(bios_uuid, 0, 16);
22
23     if (! CONFIG_UUID_BACKDOOR)
24         return;
25     if (CONFIG_COREBOOT)
26         return;
27
28     // check if backdoor port exists
29     u32 eax, ebx, ecx, edx;
30     asm volatile ("outl %%eax, %%dx"
31                   : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
32                   : "a" (0x564d5868), "b" (0), "c" (0xa), "d" (0x5658));
33     if (ebx != 0x564d5868)
34         return;
35
36     u32 *uuid_ptr = (u32 *)bios_uuid;
37     // get uuid
38     asm volatile ("outl %%eax, %%dx"
39                   : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx)
40                   : "a" (0x564d5868), "c" (0x13), "d" (0x5658));
41     uuid_ptr[0] = eax;
42     uuid_ptr[1] = ebx;
43     uuid_ptr[2] = ecx;
44     uuid_ptr[3] = edx;
45 }
46
47
48 /****************************************************************
49  * smbios tables
50  ****************************************************************/
51
52 /* SMBIOS entry point -- must be written to a 16-bit aligned address
53    between 0xf0000 and 0xfffff.
54  */
55 struct smbios_entry_point {
56         char anchor_string[4];
57         u8 checksum;
58         u8 length;
59         u8 smbios_major_version;
60         u8 smbios_minor_version;
61         u16 max_structure_size;
62         u8 entry_point_revision;
63         u8 formatted_area[5];
64         char intermediate_anchor_string[5];
65         u8 intermediate_checksum;
66         u16 structure_table_length;
67         u32 structure_table_address;
68         u16 number_of_structures;
69         u8 smbios_bcd_revision;
70 } PACKED;
71
72 /* This goes at the beginning of every SMBIOS structure. */
73 struct smbios_structure_header {
74         u8 type;
75         u8 length;
76         u16 handle;
77 } PACKED;
78
79 /* SMBIOS type 0 - BIOS Information */
80 struct smbios_type_0 {
81         struct smbios_structure_header header;
82         u8 vendor_str;
83         u8 bios_version_str;
84         u16 bios_starting_address_segment;
85         u8 bios_release_date_str;
86         u8 bios_rom_size;
87         u8 bios_characteristics[8];
88         u8 bios_characteristics_extension_bytes[2];
89         u8 system_bios_major_release;
90         u8 system_bios_minor_release;
91         u8 embedded_controller_major_release;
92         u8 embedded_controller_minor_release;
93 } PACKED;
94
95 /* SMBIOS type 1 - System Information */
96 struct smbios_type_1 {
97         struct smbios_structure_header header;
98         u8 manufacturer_str;
99         u8 product_name_str;
100         u8 version_str;
101         u8 serial_number_str;
102         u8 uuid[16];
103         u8 wake_up_type;
104         u8 sku_number_str;
105         u8 family_str;
106 } PACKED;
107
108 /* SMBIOS type 3 - System Enclosure (v2.3) */
109 struct smbios_type_3 {
110         struct smbios_structure_header header;
111         u8 manufacturer_str;
112         u8 type;
113         u8 version_str;
114         u8 serial_number_str;
115         u8 asset_tag_number_str;
116         u8 boot_up_state;
117         u8 power_supply_state;
118         u8 thermal_state;
119         u8 security_status;
120     u32 oem_defined;
121     u8 height;
122     u8 number_of_power_cords;
123     u8 contained_element_count;
124     // contained elements follow
125 } PACKED;
126
127 /* SMBIOS type 4 - Processor Information (v2.0) */
128 struct smbios_type_4 {
129         struct smbios_structure_header header;
130         u8 socket_designation_str;
131         u8 processor_type;
132         u8 processor_family;
133         u8 processor_manufacturer_str;
134         u32 processor_id[2];
135         u8 processor_version_str;
136         u8 voltage;
137         u16 external_clock;
138         u16 max_speed;
139         u16 current_speed;
140         u8 status;
141         u8 processor_upgrade;
142 } PACKED;
143
144 /* SMBIOS type 16 - Physical Memory Array
145  *   Associated with one type 17 (Memory Device).
146  */
147 struct smbios_type_16 {
148         struct smbios_structure_header header;
149         u8 location;
150         u8 use;
151         u8 error_correction;
152         u32 maximum_capacity;
153         u16 memory_error_information_handle;
154         u16 number_of_memory_devices;
155 } PACKED;
156
157 /* SMBIOS type 17 - Memory Device
158  *   Associated with one type 19
159  */
160 struct smbios_type_17 {
161         struct smbios_structure_header header;
162         u16 physical_memory_array_handle;
163         u16 memory_error_information_handle;
164         u16 total_width;
165         u16 data_width;
166         u16 size;
167         u8 form_factor;
168         u8 device_set;
169         u8 device_locator_str;
170         u8 bank_locator_str;
171         u8 memory_type;
172         u16 type_detail;
173 } PACKED;
174
175 /* SMBIOS type 19 - Memory Array Mapped Address */
176 struct smbios_type_19 {
177         struct smbios_structure_header header;
178         u32 starting_address;
179         u32 ending_address;
180         u16 memory_array_handle;
181         u8 partition_width;
182 } PACKED;
183
184 /* SMBIOS type 20 - Memory Device Mapped Address */
185 struct smbios_type_20 {
186         struct smbios_structure_header header;
187         u32 starting_address;
188         u32 ending_address;
189         u16 memory_device_handle;
190         u16 memory_array_mapped_address_handle;
191         u8 partition_row_position;
192         u8 interleave_position;
193         u8 interleaved_data_depth;
194 } PACKED;
195
196 /* SMBIOS type 32 - System Boot Information */
197 struct smbios_type_32 {
198         struct smbios_structure_header header;
199         u8 reserved[6];
200         u8 boot_status;
201 } PACKED;
202
203 /* SMBIOS type 127 -- End-of-table */
204 struct smbios_type_127 {
205         struct smbios_structure_header header;
206 } PACKED;
207
208
209 /****************************************************************
210  * smbios init
211  ****************************************************************/
212
213 static void
214 smbios_entry_point_init(void *start,
215                         u16 max_structure_size,
216                         u16 structure_table_length,
217                         u32 structure_table_address,
218                         u16 number_of_structures)
219 {
220     struct smbios_entry_point *ep = (struct smbios_entry_point *)start;
221
222     memcpy(ep->anchor_string, "_SM_", 4);
223     ep->length = 0x1f;
224     ep->smbios_major_version = 2;
225     ep->smbios_minor_version = 4;
226     ep->max_structure_size = max_structure_size;
227     ep->entry_point_revision = 0;
228     memset(ep->formatted_area, 0, 5);
229     memcpy(ep->intermediate_anchor_string, "_DMI_", 5);
230
231     ep->structure_table_length = structure_table_length;
232     ep->structure_table_address = structure_table_address;
233     ep->number_of_structures = number_of_structures;
234     ep->smbios_bcd_revision = 0x24;
235
236     ep->checksum = 0;
237     ep->intermediate_checksum = 0;
238
239     ep->checksum = -checksum(start, 0x10);
240
241     ep->intermediate_checksum = -checksum(start + 0x10, ep->length - 0x10);
242 }
243
244 /* Type 0 -- BIOS Information */
245 #define RELEASE_DATE_STR "01/01/2007"
246 static void *
247 smbios_type_0_init(void *start)
248 {
249     struct smbios_type_0 *p = (struct smbios_type_0 *)start;
250
251     p->header.type = 0;
252     p->header.length = sizeof(struct smbios_type_0);
253     p->header.handle = 0;
254
255     p->vendor_str = 1;
256     p->bios_version_str = 1;
257     p->bios_starting_address_segment = 0xe800;
258     p->bios_release_date_str = 2;
259     p->bios_rom_size = 0; /* FIXME */
260
261     memset(p->bios_characteristics, 0, 7);
262     p->bios_characteristics[7] = 0x08; /* BIOS characteristics not supported */
263     p->bios_characteristics_extension_bytes[0] = 0;
264     p->bios_characteristics_extension_bytes[1] = 0;
265
266     p->system_bios_major_release = 1;
267     p->system_bios_minor_release = 0;
268     p->embedded_controller_major_release = 0xff;
269     p->embedded_controller_minor_release = 0xff;
270
271     start += sizeof(struct smbios_type_0);
272     memcpy((char *)start, CONFIG_APPNAME, sizeof(CONFIG_APPNAME));
273     start += sizeof(CONFIG_APPNAME);
274     memcpy((char *)start, RELEASE_DATE_STR, sizeof(RELEASE_DATE_STR));
275     start += sizeof(RELEASE_DATE_STR);
276     *((u8 *)start) = 0;
277
278     return start+1;
279 }
280
281 /* Type 1 -- System Information */
282 static void *
283 smbios_type_1_init(void *start)
284 {
285     struct smbios_type_1 *p = (struct smbios_type_1 *)start;
286     p->header.type = 1;
287     p->header.length = sizeof(struct smbios_type_1);
288     p->header.handle = 0x100;
289
290     p->manufacturer_str = 0;
291     p->product_name_str = 0;
292     p->version_str = 0;
293     p->serial_number_str = 0;
294
295     uuid_probe(p->uuid);
296
297     p->wake_up_type = 0x06; /* power switch */
298     p->sku_number_str = 0;
299     p->family_str = 0;
300
301     start += sizeof(struct smbios_type_1);
302     *((u16 *)start) = 0;
303
304     return start+2;
305 }
306
307 /* Type 3 -- System Enclosure */
308 static void *
309 smbios_type_3_init(void *start)
310 {
311     struct smbios_type_3 *p = (struct smbios_type_3 *)start;
312
313     p->header.type = 3;
314     p->header.length = sizeof(struct smbios_type_3);
315     p->header.handle = 0x300;
316
317     p->manufacturer_str = 0;
318     p->type = 0x01; /* other */
319     p->version_str = 0;
320     p->serial_number_str = 0;
321     p->asset_tag_number_str = 0;
322     p->boot_up_state = 0x03; /* safe */
323     p->power_supply_state = 0x03; /* safe */
324     p->thermal_state = 0x03; /* safe */
325     p->security_status = 0x02; /* unknown */
326     p->oem_defined = 0;
327     p->height = 0;
328     p->number_of_power_cords = 0;
329     p->contained_element_count = 0;
330
331     start += sizeof(struct smbios_type_3);
332     *((u16 *)start) = 0;
333
334     return start+2;
335 }
336
337 /* Type 4 -- Processor Information */
338 static void *
339 smbios_type_4_init(void *start, unsigned int cpu_number)
340 {
341     struct smbios_type_4 *p = (struct smbios_type_4 *)start;
342
343     p->header.type = 4;
344     p->header.length = sizeof(struct smbios_type_4);
345     p->header.handle = 0x400 + cpu_number;
346
347     p->socket_designation_str = 1;
348     p->processor_type = 0x03; /* CPU */
349     p->processor_family = 0x01; /* other */
350     p->processor_manufacturer_str = 0;
351
352     u32 cpuid_signature, ebx, ecx, cpuid_features;
353     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
354     p->processor_id[0] = cpuid_signature;
355     p->processor_id[1] = cpuid_features;
356
357     p->processor_version_str = 0;
358     p->voltage = 0;
359     p->external_clock = 0;
360
361     p->max_speed = 0; /* unknown */
362     p->current_speed = 0; /* unknown */
363
364     p->status = 0x41; /* socket populated, CPU enabled */
365     p->processor_upgrade = 0x01; /* other */
366
367     start += sizeof(struct smbios_type_4);
368
369     memcpy((char *)start, "CPU  " "\0" "" "\0" "", 7);
370         ((char *)start)[4] = cpu_number + '0';
371
372     return start+7;
373 }
374
375 /* Type 16 -- Physical Memory Array */
376 static void *
377 smbios_type_16_init(void *start, u32 memsize)
378 {
379     struct smbios_type_16 *p = (struct smbios_type_16*)start;
380
381     p->header.type = 16;
382     p->header.length = sizeof(struct smbios_type_16);
383     p->header.handle = 0x1000;
384
385     p->location = 0x01; /* other */
386     p->use = 0x03; /* system memory */
387     p->error_correction = 0x01; /* other */
388     p->maximum_capacity = memsize * 1024;
389     p->memory_error_information_handle = 0xfffe; /* none provided */
390     p->number_of_memory_devices = 1;
391
392     start += sizeof(struct smbios_type_16);
393     *((u16 *)start) = 0;
394
395     return start + 2;
396 }
397
398 /* Type 17 -- Memory Device */
399 static void *
400 smbios_type_17_init(void *start, u32 memory_size_mb)
401 {
402     struct smbios_type_17 *p = (struct smbios_type_17 *)start;
403
404     p->header.type = 17;
405     p->header.length = sizeof(struct smbios_type_17);
406     p->header.handle = 0x1100;
407
408     p->physical_memory_array_handle = 0x1000;
409     p->total_width = 64;
410     p->data_width = 64;
411     /* truncate memory_size_mb to 16 bits and clear most significant
412        bit [indicates size in MB] */
413     p->size = (u16) memory_size_mb & 0x7fff;
414     p->form_factor = 0x09; /* DIMM */
415     p->device_set = 0;
416     p->device_locator_str = 1;
417     p->bank_locator_str = 0;
418     p->memory_type = 0x07; /* RAM */
419     p->type_detail = 0;
420
421     start += sizeof(struct smbios_type_17);
422     memcpy((char *)start, "DIMM 1", 7);
423     start += 7;
424     *((u8 *)start) = 0;
425
426     return start+1;
427 }
428
429 /* Type 19 -- Memory Array Mapped Address */
430 static void *
431 smbios_type_19_init(void *start, u32 memory_size_mb)
432 {
433     struct smbios_type_19 *p = (struct smbios_type_19 *)start;
434
435     p->header.type = 19;
436     p->header.length = sizeof(struct smbios_type_19);
437     p->header.handle = 0x1300;
438
439     p->starting_address = 0;
440     p->ending_address = (memory_size_mb-1) * 1024;
441     p->memory_array_handle = 0x1000;
442     p->partition_width = 1;
443
444     start += sizeof(struct smbios_type_19);
445     *((u16 *)start) = 0;
446
447     return start + 2;
448 }
449
450 /* Type 20 -- Memory Device Mapped Address */
451 static void *
452 smbios_type_20_init(void *start, u32 memory_size_mb)
453 {
454     struct smbios_type_20 *p = (struct smbios_type_20 *)start;
455
456     p->header.type = 20;
457     p->header.length = sizeof(struct smbios_type_20);
458     p->header.handle = 0x1400;
459
460     p->starting_address = 0;
461     p->ending_address = (memory_size_mb-1)*1024;
462     p->memory_device_handle = 0x1100;
463     p->memory_array_mapped_address_handle = 0x1300;
464     p->partition_row_position = 1;
465     p->interleave_position = 0;
466     p->interleaved_data_depth = 0;
467
468     start += sizeof(struct smbios_type_20);
469
470     *((u16 *)start) = 0;
471     return start+2;
472 }
473
474 /* Type 32 -- System Boot Information */
475 static void *
476 smbios_type_32_init(void *start)
477 {
478     struct smbios_type_32 *p = (struct smbios_type_32 *)start;
479
480     p->header.type = 32;
481     p->header.length = sizeof(struct smbios_type_32);
482     p->header.handle = 0x2000;
483     memset(p->reserved, 0, 6);
484     p->boot_status = 0; /* no errors detected */
485
486     start += sizeof(struct smbios_type_32);
487     *((u16 *)start) = 0;
488
489     return start+2;
490 }
491
492 /* Type 127 -- End of Table */
493 static void *
494 smbios_type_127_init(void *start)
495 {
496     struct smbios_type_127 *p = (struct smbios_type_127 *)start;
497
498     p->header.type = 127;
499     p->header.length = sizeof(struct smbios_type_127);
500     p->header.handle = 0x7f00;
501
502     start += sizeof(struct smbios_type_127);
503     *((u16 *)start) = 0;
504
505     return start + 2;
506 }
507
508 void
509 smbios_init(void)
510 {
511     if (! CONFIG_SMBIOS)
512         return;
513
514     unsigned cpu_num, nr_structs = 0, max_struct_size = 0;
515     char *start, *p, *q;
516     int memsize = GET_EBDA(ram_size) / (1024 * 1024);
517
518     bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
519     start = (void *)(bios_table_cur_addr);
520
521     p = (char *)start + sizeof(struct smbios_entry_point);
522
523 #define add_struct(fn) { \
524     q = (fn); \
525     nr_structs++; \
526     if ((q - p) > max_struct_size) \
527         max_struct_size = q - p; \
528     p = q; \
529 }
530
531     add_struct(smbios_type_0_init(p));
532     add_struct(smbios_type_1_init(p));
533     add_struct(smbios_type_3_init(p));
534     int smp_cpus = smp_probe();
535     for (cpu_num = 1; cpu_num <= smp_cpus; cpu_num++)
536         add_struct(smbios_type_4_init(p, cpu_num));
537     add_struct(smbios_type_16_init(p, memsize));
538     add_struct(smbios_type_17_init(p, memsize));
539     add_struct(smbios_type_19_init(p, memsize));
540     add_struct(smbios_type_20_init(p, memsize));
541     add_struct(smbios_type_32_init(p));
542     add_struct(smbios_type_127_init(p));
543
544 #undef add_struct
545
546     smbios_entry_point_init(
547         start, max_struct_size,
548         (p - (char *)start) - sizeof(struct smbios_entry_point),
549         (u32)(start + sizeof(struct smbios_entry_point)),
550         nr_structs);
551
552     bios_table_cur_addr += (p - (char *)start);
553
554     dprintf(1, "SMBIOS table addr=0x%08lx\n", (unsigned long)start);
555 }