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