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