Set smbios cache handles - 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         u16 l1_cache_handle;
143         u16 l2_cache_handle;
144         u16 l3_cache_handle;
145 } PACKED;
146
147 /* SMBIOS type 16 - Physical Memory Array
148  *   Associated with one type 17 (Memory Device).
149  */
150 struct smbios_type_16 {
151         struct smbios_structure_header header;
152         u8 location;
153         u8 use;
154         u8 error_correction;
155         u32 maximum_capacity;
156         u16 memory_error_information_handle;
157         u16 number_of_memory_devices;
158 } PACKED;
159
160 /* SMBIOS type 17 - Memory Device
161  *   Associated with one type 19
162  */
163 struct smbios_type_17 {
164         struct smbios_structure_header header;
165         u16 physical_memory_array_handle;
166         u16 memory_error_information_handle;
167         u16 total_width;
168         u16 data_width;
169         u16 size;
170         u8 form_factor;
171         u8 device_set;
172         u8 device_locator_str;
173         u8 bank_locator_str;
174         u8 memory_type;
175         u16 type_detail;
176 } PACKED;
177
178 /* SMBIOS type 19 - Memory Array Mapped Address */
179 struct smbios_type_19 {
180         struct smbios_structure_header header;
181         u32 starting_address;
182         u32 ending_address;
183         u16 memory_array_handle;
184         u8 partition_width;
185 } PACKED;
186
187 /* SMBIOS type 20 - Memory Device Mapped Address */
188 struct smbios_type_20 {
189         struct smbios_structure_header header;
190         u32 starting_address;
191         u32 ending_address;
192         u16 memory_device_handle;
193         u16 memory_array_mapped_address_handle;
194         u8 partition_row_position;
195         u8 interleave_position;
196         u8 interleaved_data_depth;
197 } PACKED;
198
199 /* SMBIOS type 32 - System Boot Information */
200 struct smbios_type_32 {
201         struct smbios_structure_header header;
202         u8 reserved[6];
203         u8 boot_status;
204 } PACKED;
205
206 /* SMBIOS type 127 -- End-of-table */
207 struct smbios_type_127 {
208         struct smbios_structure_header header;
209 } PACKED;
210
211
212 /****************************************************************
213  * smbios init
214  ****************************************************************/
215
216 static void
217 smbios_entry_point_init(void *start,
218                         u16 max_structure_size,
219                         u16 structure_table_length,
220                         u32 structure_table_address,
221                         u16 number_of_structures)
222 {
223     struct smbios_entry_point *ep = (struct smbios_entry_point *)start;
224
225     memcpy(ep->anchor_string, "_SM_", 4);
226     ep->length = 0x1f;
227     ep->smbios_major_version = 2;
228     ep->smbios_minor_version = 4;
229     ep->max_structure_size = max_structure_size;
230     ep->entry_point_revision = 0;
231     memset(ep->formatted_area, 0, 5);
232     memcpy(ep->intermediate_anchor_string, "_DMI_", 5);
233
234     ep->structure_table_length = structure_table_length;
235     ep->structure_table_address = structure_table_address;
236     ep->number_of_structures = number_of_structures;
237     ep->smbios_bcd_revision = 0x24;
238
239     ep->checksum = 0;
240     ep->intermediate_checksum = 0;
241
242     ep->checksum = -checksum(start, 0x10);
243
244     ep->intermediate_checksum = -checksum(start + 0x10, ep->length - 0x10);
245 }
246
247 /* Type 0 -- BIOS Information */
248 #define RELEASE_DATE_STR "01/01/2007"
249 static void *
250 smbios_type_0_init(void *start)
251 {
252     struct smbios_type_0 *p = (struct smbios_type_0 *)start;
253
254     p->header.type = 0;
255     p->header.length = sizeof(struct smbios_type_0);
256     p->header.handle = 0;
257
258     p->vendor_str = 1;
259     p->bios_version_str = 1;
260     p->bios_starting_address_segment = 0xe800;
261     p->bios_release_date_str = 2;
262     p->bios_rom_size = 0; /* FIXME */
263
264     memset(p->bios_characteristics, 0, sizeof(p->bios_characteristics));
265     p->bios_characteristics[0] = 0x08; /* BIOS characteristics not supported */
266     p->bios_characteristics_extension_bytes[0] = 0;
267     p->bios_characteristics_extension_bytes[1] = 0;
268
269     p->system_bios_major_release = 1;
270     p->system_bios_minor_release = 0;
271     p->embedded_controller_major_release = 0xff;
272     p->embedded_controller_minor_release = 0xff;
273
274     start += sizeof(struct smbios_type_0);
275     memcpy((char *)start, CONFIG_APPNAME, sizeof(CONFIG_APPNAME));
276     start += sizeof(CONFIG_APPNAME);
277     memcpy((char *)start, RELEASE_DATE_STR, sizeof(RELEASE_DATE_STR));
278     start += sizeof(RELEASE_DATE_STR);
279     *((u8 *)start) = 0;
280
281     return start+1;
282 }
283
284 /* Type 1 -- System Information */
285 static void *
286 smbios_type_1_init(void *start)
287 {
288     struct smbios_type_1 *p = (struct smbios_type_1 *)start;
289     p->header.type = 1;
290     p->header.length = sizeof(struct smbios_type_1);
291     p->header.handle = 0x100;
292
293     p->manufacturer_str = 0;
294     p->product_name_str = 0;
295     p->version_str = 0;
296     p->serial_number_str = 0;
297
298     uuid_probe(p->uuid);
299
300     p->wake_up_type = 0x06; /* power switch */
301     p->sku_number_str = 0;
302     p->family_str = 0;
303
304     start += sizeof(struct smbios_type_1);
305     *((u16 *)start) = 0;
306
307     return start+2;
308 }
309
310 /* Type 3 -- System Enclosure */
311 static void *
312 smbios_type_3_init(void *start)
313 {
314     struct smbios_type_3 *p = (struct smbios_type_3 *)start;
315
316     p->header.type = 3;
317     p->header.length = sizeof(struct smbios_type_3);
318     p->header.handle = 0x300;
319
320     p->manufacturer_str = 0;
321     p->type = 0x01; /* other */
322     p->version_str = 0;
323     p->serial_number_str = 0;
324     p->asset_tag_number_str = 0;
325     p->boot_up_state = 0x03; /* safe */
326     p->power_supply_state = 0x03; /* safe */
327     p->thermal_state = 0x03; /* safe */
328     p->security_status = 0x02; /* unknown */
329     p->oem_defined = 0;
330     p->height = 0;
331     p->number_of_power_cords = 0;
332     p->contained_element_count = 0;
333
334     start += sizeof(struct smbios_type_3);
335     *((u16 *)start) = 0;
336
337     return start+2;
338 }
339
340 /* Type 4 -- Processor Information */
341 static void *
342 smbios_type_4_init(void *start, unsigned int cpu_number)
343 {
344     struct smbios_type_4 *p = (struct smbios_type_4 *)start;
345
346     p->header.type = 4;
347     p->header.length = sizeof(struct smbios_type_4);
348     p->header.handle = 0x400 + cpu_number;
349
350     p->socket_designation_str = 1;
351     p->processor_type = 0x03; /* CPU */
352     p->processor_family = 0x01; /* other */
353     p->processor_manufacturer_str = 0;
354
355     u32 cpuid_signature, ebx, ecx, cpuid_features;
356     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
357     p->processor_id[0] = cpuid_signature;
358     p->processor_id[1] = cpuid_features;
359
360     p->processor_version_str = 0;
361     p->voltage = 0;
362     p->external_clock = 0;
363
364     p->max_speed = 0; /* unknown */
365     p->current_speed = 0; /* unknown */
366
367     p->status = 0x41; /* socket populated, CPU enabled */
368     p->processor_upgrade = 0x01; /* other */
369
370     p->l1_cache_handle = 0xffff; /* cache information structure not provided */
371     p->l2_cache_handle = 0xffff;
372     p->l3_cache_handle = 0xffff;
373
374     start += sizeof(struct smbios_type_4);
375
376     memcpy((char *)start, "CPU  " "\0" "" "\0" "", 7);
377         ((char *)start)[4] = cpu_number + '0';
378
379     return start+7;
380 }
381
382 /* Type 16 -- Physical Memory Array */
383 static void *
384 smbios_type_16_init(void *start, u32 memsize)
385 {
386     struct smbios_type_16 *p = (struct smbios_type_16*)start;
387
388     p->header.type = 16;
389     p->header.length = sizeof(struct smbios_type_16);
390     p->header.handle = 0x1000;
391
392     p->location = 0x01; /* other */
393     p->use = 0x03; /* system memory */
394     p->error_correction = 0x01; /* other */
395     p->maximum_capacity = memsize * 1024;
396     p->memory_error_information_handle = 0xfffe; /* none provided */
397     p->number_of_memory_devices = 1;
398
399     start += sizeof(struct smbios_type_16);
400     *((u16 *)start) = 0;
401
402     return start + 2;
403 }
404
405 /* Type 17 -- Memory Device */
406 static void *
407 smbios_type_17_init(void *start, u32 memory_size_mb)
408 {
409     struct smbios_type_17 *p = (struct smbios_type_17 *)start;
410
411     p->header.type = 17;
412     p->header.length = sizeof(struct smbios_type_17);
413     p->header.handle = 0x1100;
414
415     p->physical_memory_array_handle = 0x1000;
416     p->total_width = 64;
417     p->data_width = 64;
418     /* truncate memory_size_mb to 16 bits and clear most significant
419        bit [indicates size in MB] */
420     p->size = (u16) memory_size_mb & 0x7fff;
421     p->form_factor = 0x09; /* DIMM */
422     p->device_set = 0;
423     p->device_locator_str = 1;
424     p->bank_locator_str = 0;
425     p->memory_type = 0x07; /* RAM */
426     p->type_detail = 0;
427
428     start += sizeof(struct smbios_type_17);
429     memcpy((char *)start, "DIMM 1", 7);
430     start += 7;
431     *((u8 *)start) = 0;
432
433     return start+1;
434 }
435
436 /* Type 19 -- Memory Array Mapped Address */
437 static void *
438 smbios_type_19_init(void *start, u32 memory_size_mb)
439 {
440     struct smbios_type_19 *p = (struct smbios_type_19 *)start;
441
442     p->header.type = 19;
443     p->header.length = sizeof(struct smbios_type_19);
444     p->header.handle = 0x1300;
445
446     p->starting_address = 0;
447     p->ending_address = (memory_size_mb-1) * 1024;
448     p->memory_array_handle = 0x1000;
449     p->partition_width = 1;
450
451     start += sizeof(struct smbios_type_19);
452     *((u16 *)start) = 0;
453
454     return start + 2;
455 }
456
457 /* Type 20 -- Memory Device Mapped Address */
458 static void *
459 smbios_type_20_init(void *start, u32 memory_size_mb)
460 {
461     struct smbios_type_20 *p = (struct smbios_type_20 *)start;
462
463     p->header.type = 20;
464     p->header.length = sizeof(struct smbios_type_20);
465     p->header.handle = 0x1400;
466
467     p->starting_address = 0;
468     p->ending_address = (memory_size_mb-1)*1024;
469     p->memory_device_handle = 0x1100;
470     p->memory_array_mapped_address_handle = 0x1300;
471     p->partition_row_position = 1;
472     p->interleave_position = 0;
473     p->interleaved_data_depth = 0;
474
475     start += sizeof(struct smbios_type_20);
476
477     *((u16 *)start) = 0;
478     return start+2;
479 }
480
481 /* Type 32 -- System Boot Information */
482 static void *
483 smbios_type_32_init(void *start)
484 {
485     struct smbios_type_32 *p = (struct smbios_type_32 *)start;
486
487     p->header.type = 32;
488     p->header.length = sizeof(struct smbios_type_32);
489     p->header.handle = 0x2000;
490     memset(p->reserved, 0, 6);
491     p->boot_status = 0; /* no errors detected */
492
493     start += sizeof(struct smbios_type_32);
494     *((u16 *)start) = 0;
495
496     return start+2;
497 }
498
499 /* Type 127 -- End of Table */
500 static void *
501 smbios_type_127_init(void *start)
502 {
503     struct smbios_type_127 *p = (struct smbios_type_127 *)start;
504
505     p->header.type = 127;
506     p->header.length = sizeof(struct smbios_type_127);
507     p->header.handle = 0x7f00;
508
509     start += sizeof(struct smbios_type_127);
510     *((u16 *)start) = 0;
511
512     return start + 2;
513 }
514
515 void
516 smbios_init(void)
517 {
518     if (! CONFIG_SMBIOS)
519         return;
520
521     unsigned cpu_num, nr_structs = 0, max_struct_size = 0;
522     char *start, *p, *q;
523     int memsize = GET_EBDA(ram_size) / (1024 * 1024);
524
525     bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
526     start = (void *)(bios_table_cur_addr);
527
528     p = (char *)start + sizeof(struct smbios_entry_point);
529
530 #define add_struct(fn) { \
531     q = (fn); \
532     nr_structs++; \
533     if ((q - p) > max_struct_size) \
534         max_struct_size = q - p; \
535     p = q; \
536 }
537
538     add_struct(smbios_type_0_init(p));
539     add_struct(smbios_type_1_init(p));
540     add_struct(smbios_type_3_init(p));
541     int smp_cpus = smp_probe();
542     for (cpu_num = 1; cpu_num <= smp_cpus; cpu_num++)
543         add_struct(smbios_type_4_init(p, cpu_num));
544     add_struct(smbios_type_16_init(p, memsize));
545     add_struct(smbios_type_17_init(p, memsize));
546     add_struct(smbios_type_19_init(p, memsize));
547     add_struct(smbios_type_20_init(p, memsize));
548     add_struct(smbios_type_32_init(p));
549     add_struct(smbios_type_127_init(p));
550
551 #undef add_struct
552
553     smbios_entry_point_init(
554         start, max_struct_size,
555         (p - (char *)start) - sizeof(struct smbios_entry_point),
556         (u32)(start + sizeof(struct smbios_entry_point)),
557         nr_structs);
558
559     bios_table_cur_addr += (p - (char *)start);
560
561     dprintf(1, "SMBIOS table addr=0x%08lx\n", (unsigned long)start);
562 }