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