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