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