resolve memory device roll over reporting issues with >32G guests
[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, u32 memory_size_mb, int nr_mem_devs)
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     p->maximum_capacity = memory_size_mb * 1024;
381     p->memory_error_information_handle = 0xfffe; /* none provided */
382     p->number_of_memory_devices = nr_mem_devs;
383
384     start += sizeof(struct smbios_type_16);
385     *((u16 *)start) = 0;
386
387     return start + 2;
388 }
389
390 /* Type 17 -- Memory Device */
391 static void *
392 smbios_type_17_init(void *start, u32 memory_size_mb, int instance)
393 {
394     struct smbios_type_17 *p = (struct smbios_type_17 *)start;
395
396     p->header.type = 17;
397     p->header.length = sizeof(struct smbios_type_17);
398     p->header.handle = 0x1100 + instance;
399
400     p->physical_memory_array_handle = 0x1000;
401     p->total_width = 64;
402     p->data_width = 64;
403 /* TODO: should assert in case something is wrong   ASSERT((memory_size_mb & ~0x7fff) == 0); */
404     p->size = memory_size_mb;
405     p->form_factor = 0x09; /* DIMM */
406     p->device_set = 0;
407     p->device_locator_str = 1;
408     p->bank_locator_str = 0;
409     p->memory_type = 0x07; /* RAM */
410     p->type_detail = 0;
411
412     start += sizeof(struct smbios_type_17);
413     memcpy((char *)start, "DIMM 0", 7);
414     ((char*)start)[5] += instance;
415     start += 7;
416     *((u8 *)start) = 0;
417
418     return start+1;
419 }
420
421 /* Type 19 -- Memory Array Mapped Address */
422 static void *
423 smbios_type_19_init(void *start, u32 memory_size_mb, int instance)
424 {
425     struct smbios_type_19 *p = (struct smbios_type_19 *)start;
426
427     p->header.type = 19;
428     p->header.length = sizeof(struct smbios_type_19);
429     p->header.handle = 0x1300 + instance;
430
431     p->starting_address = instance << 24;
432     p->ending_address = p->starting_address + (memory_size_mb << 10) - 1;
433     p->memory_array_handle = 0x1000;
434     p->partition_width = 1;
435
436     start += sizeof(struct smbios_type_19);
437     *((u16 *)start) = 0;
438
439     return start + 2;
440 }
441
442 /* Type 20 -- Memory Device Mapped Address */
443 static void *
444 smbios_type_20_init(void *start, u32 memory_size_mb, int instance)
445 {
446     struct smbios_type_20 *p = (struct smbios_type_20 *)start;
447
448     p->header.type = 20;
449     p->header.length = sizeof(struct smbios_type_20);
450     p->header.handle = 0x1400 + instance;
451
452     p->starting_address = instance << 24;
453     p->ending_address = p->starting_address + (memory_size_mb << 10) - 1;
454     p->memory_device_handle = 0x1100 + instance;
455     p->memory_array_mapped_address_handle = 0x1300 + instance;
456     p->partition_row_position = 1;
457     p->interleave_position = 0;
458     p->interleaved_data_depth = 0;
459
460     start += sizeof(struct smbios_type_20);
461
462     *((u16 *)start) = 0;
463     return start+2;
464 }
465
466 /* Type 32 -- System Boot Information */
467 static void *
468 smbios_type_32_init(void *start)
469 {
470     struct smbios_type_32 *p = (struct smbios_type_32 *)start;
471
472     p->header.type = 32;
473     p->header.length = sizeof(struct smbios_type_32);
474     p->header.handle = 0x2000;
475     memset(p->reserved, 0, 6);
476     p->boot_status = 0; /* no errors detected */
477
478     start += sizeof(struct smbios_type_32);
479     *((u16 *)start) = 0;
480
481     return start+2;
482 }
483
484 /* Type 127 -- End of Table */
485 static void *
486 smbios_type_127_init(void *start)
487 {
488     struct smbios_type_127 *p = (struct smbios_type_127 *)start;
489
490     p->header.type = 127;
491     p->header.length = sizeof(struct smbios_type_127);
492     p->header.handle = 0x7f00;
493
494     start += sizeof(struct smbios_type_127);
495     *((u16 *)start) = 0;
496
497     return start + 2;
498 }
499
500 void
501 smbios_init(void)
502 {
503     if (! CONFIG_SMBIOS)
504         return;
505
506     dprintf(3, "init SMBIOS tables\n");
507
508     char *start = malloc_high(2048); // XXX - determine real size
509     if (! start) {
510         dprintf(1, "No memory for smbios tables\n");
511         return;
512     }
513
514     u32 nr_structs = 0, max_struct_size = 0;
515     char *q, *p = start;
516
517 #define add_struct(fn) { \
518     q = (fn); \
519     nr_structs++; \
520     if ((q - p) > max_struct_size) \
521         max_struct_size = q - p; \
522     p = q; \
523 }
524
525     add_struct(smbios_type_0_init(p));
526     add_struct(smbios_type_1_init(p));
527     add_struct(smbios_type_3_init(p));
528     int cpu_num, smp_cpus = CountCPUs;
529     for (cpu_num = 1; cpu_num <= smp_cpus; cpu_num++)
530         add_struct(smbios_type_4_init(p, cpu_num));
531     u64 memsize = RamSizeOver4G;
532     if (memsize)
533         memsize += 0x100000000ull;
534     else
535         memsize = RamSize;
536     memsize = memsize / (1024 * 1024);
537     int nr_mem_devs = (memsize + 0x3fff) >> 14;
538     add_struct(smbios_type_16_init(p, memsize, nr_mem_devs));
539     int i;
540     for (i = 0; i < nr_mem_devs; i++) {
541         u32 dev_memsize = ((i == (nr_mem_devs - 1))
542                            ? (((memsize-1) & 0x3fff)+1) : 0x4000);
543         add_struct(smbios_type_17_init(p, dev_memsize, i));
544         add_struct(smbios_type_19_init(p, dev_memsize, i));
545         add_struct(smbios_type_20_init(p, dev_memsize, i));
546     }
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 }