Use "p->sum -= checksum()" style for setting checksums.
[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 -= checksum(start, 0x10);
248
249     ep->intermediate_checksum -= checksum(start + 0x10, ep->length - 0x10);
250 }
251
252 /* Type 0 -- BIOS Information */
253 #define RELEASE_DATE_STR "01/01/2007"
254 static void *
255 smbios_type_0_init(void *start)
256 {
257     struct smbios_type_0 *p = (struct smbios_type_0 *)start;
258
259     p->header.type = 0;
260     p->header.length = sizeof(struct smbios_type_0);
261     p->header.handle = 0;
262
263     p->vendor_str = 1;
264     p->bios_version_str = 1;
265     p->bios_starting_address_segment = 0xe800;
266     p->bios_release_date_str = 2;
267     p->bios_rom_size = 0; /* FIXME */
268
269     memset(p->bios_characteristics, 0, sizeof(p->bios_characteristics));
270     p->bios_characteristics[0] = 0x08; /* BIOS characteristics not supported */
271     p->bios_characteristics_extension_bytes[0] = 0;
272     p->bios_characteristics_extension_bytes[1] = 0;
273
274     p->system_bios_major_release = 1;
275     p->system_bios_minor_release = 0;
276     p->embedded_controller_major_release = 0xff;
277     p->embedded_controller_minor_release = 0xff;
278
279     start += sizeof(struct smbios_type_0);
280     memcpy((char *)start, CONFIG_APPNAME, sizeof(CONFIG_APPNAME));
281     start += sizeof(CONFIG_APPNAME);
282     memcpy((char *)start, RELEASE_DATE_STR, sizeof(RELEASE_DATE_STR));
283     start += sizeof(RELEASE_DATE_STR);
284     *((u8 *)start) = 0;
285
286     return start+1;
287 }
288
289 /* Type 1 -- System Information */
290 static void *
291 smbios_type_1_init(void *start)
292 {
293     struct smbios_type_1 *p = (struct smbios_type_1 *)start;
294     p->header.type = 1;
295     p->header.length = sizeof(struct smbios_type_1);
296     p->header.handle = 0x100;
297
298     p->manufacturer_str = 0;
299     p->product_name_str = 0;
300     p->version_str = 0;
301     p->serial_number_str = 0;
302
303     uuid_probe(p->uuid);
304
305     p->wake_up_type = 0x06; /* power switch */
306     p->sku_number_str = 0;
307     p->family_str = 0;
308
309     start += sizeof(struct smbios_type_1);
310     *((u16 *)start) = 0;
311
312     return start+2;
313 }
314
315 /* Type 3 -- System Enclosure */
316 static void *
317 smbios_type_3_init(void *start)
318 {
319     struct smbios_type_3 *p = (struct smbios_type_3 *)start;
320
321     p->header.type = 3;
322     p->header.length = sizeof(struct smbios_type_3);
323     p->header.handle = 0x300;
324
325     p->manufacturer_str = 0;
326     p->type = 0x01; /* other */
327     p->version_str = 0;
328     p->serial_number_str = 0;
329     p->asset_tag_number_str = 0;
330     p->boot_up_state = 0x03; /* safe */
331     p->power_supply_state = 0x03; /* safe */
332     p->thermal_state = 0x03; /* safe */
333     p->security_status = 0x02; /* unknown */
334     p->oem_defined = 0;
335     p->height = 0;
336     p->number_of_power_cords = 0;
337     p->contained_element_count = 0;
338
339     start += sizeof(struct smbios_type_3);
340     *((u16 *)start) = 0;
341
342     return start+2;
343 }
344
345 /* Type 4 -- Processor Information */
346 static void *
347 smbios_type_4_init(void *start, unsigned int cpu_number)
348 {
349     struct smbios_type_4 *p = (struct smbios_type_4 *)start;
350
351     p->header.type = 4;
352     p->header.length = sizeof(struct smbios_type_4);
353     p->header.handle = 0x400 + cpu_number;
354
355     p->socket_designation_str = 1;
356     p->processor_type = 0x03; /* CPU */
357     p->processor_family = 0x01; /* other */
358     p->processor_manufacturer_str = 0;
359
360     u32 cpuid_signature, ebx, ecx, cpuid_features;
361     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
362     p->processor_id[0] = cpuid_signature;
363     p->processor_id[1] = cpuid_features;
364
365     p->processor_version_str = 0;
366     p->voltage = 0;
367     p->external_clock = 0;
368
369     p->max_speed = 0; /* unknown */
370     p->current_speed = 0; /* unknown */
371
372     p->status = 0x41; /* socket populated, CPU enabled */
373     p->processor_upgrade = 0x01; /* other */
374
375     p->l1_cache_handle = 0xffff; /* cache information structure not provided */
376     p->l2_cache_handle = 0xffff;
377     p->l3_cache_handle = 0xffff;
378
379     start += sizeof(struct smbios_type_4);
380
381     memcpy((char *)start, "CPU  " "\0" "" "\0" "", 7);
382         ((char *)start)[4] = cpu_number + '0';
383
384     return start+7;
385 }
386
387 /* Type 16 -- Physical Memory Array */
388 static void *
389 smbios_type_16_init(void *start)
390 {
391     struct smbios_type_16 *p = (struct smbios_type_16*)start;
392
393     p->header.type = 16;
394     p->header.length = sizeof(struct smbios_type_16);
395     p->header.handle = 0x1000;
396
397     p->location = 0x01; /* other */
398     p->use = 0x03; /* system memory */
399     p->error_correction = 0x01; /* other */
400     u64 memsize = RamSize + RamSizeOver4G;
401     p->maximum_capacity = memsize / 1024;
402     p->memory_error_information_handle = 0xfffe; /* none provided */
403     p->number_of_memory_devices = 1;
404
405     start += sizeof(struct smbios_type_16);
406     *((u16 *)start) = 0;
407
408     return start + 2;
409 }
410
411 /* Type 17 -- Memory Device */
412 static void *
413 smbios_type_17_init(void *start)
414 {
415     struct smbios_type_17 *p = (struct smbios_type_17 *)start;
416
417     p->header.type = 17;
418     p->header.length = sizeof(struct smbios_type_17);
419     p->header.handle = 0x1100;
420
421     p->physical_memory_array_handle = 0x1000;
422     p->total_width = 64;
423     p->data_width = 64;
424     /* truncate memory_size_mb to 16 bits and clear most significant
425        bit [indicates size in MB] */
426     u64 memsize = RamSize + RamSizeOver4G;
427     p->size = (u16) (memsize / (1024*1024)) & 0x7fff;
428     p->form_factor = 0x09; /* DIMM */
429     p->device_set = 0;
430     p->device_locator_str = 1;
431     p->bank_locator_str = 0;
432     p->memory_type = 0x07; /* RAM */
433     p->type_detail = 0;
434
435     start += sizeof(struct smbios_type_17);
436     memcpy((char *)start, "DIMM 1", 7);
437     start += 7;
438     *((u8 *)start) = 0;
439
440     return start+1;
441 }
442
443 /* Type 19 -- Memory Array Mapped Address */
444 static void *
445 smbios_type_19_init(void *start)
446 {
447     struct smbios_type_19 *p = (struct smbios_type_19 *)start;
448
449     p->header.type = 19;
450     p->header.length = sizeof(struct smbios_type_19);
451     p->header.handle = 0x1300;
452
453     p->starting_address = 0;
454     u64 memsize = RamSizeOver4G;
455     if (memsize)
456         memsize += 0x100000000ull;
457     else
458         memsize = RamSize;
459     p->ending_address = memsize / 1024 - 1;
460     p->memory_array_handle = 0x1000;
461     p->partition_width = 1;
462
463     start += sizeof(struct smbios_type_19);
464     *((u16 *)start) = 0;
465
466     return start + 2;
467 }
468
469 /* Type 20 -- Memory Device Mapped Address */
470 static void *
471 smbios_type_20_init(void *start)
472 {
473     struct smbios_type_20 *p = (struct smbios_type_20 *)start;
474
475     p->header.type = 20;
476     p->header.length = sizeof(struct smbios_type_20);
477     p->header.handle = 0x1400;
478
479     p->starting_address = 0;
480     u64 memsize = RamSizeOver4G;
481     if (memsize)
482         memsize += 0x100000000ull;
483     else
484         memsize = RamSize;
485     p->ending_address = memsize / 1024 - 1;
486     p->memory_device_handle = 0x1100;
487     p->memory_array_mapped_address_handle = 0x1300;
488     p->partition_row_position = 1;
489     p->interleave_position = 0;
490     p->interleaved_data_depth = 0;
491
492     start += sizeof(struct smbios_type_20);
493
494     *((u16 *)start) = 0;
495     return start+2;
496 }
497
498 /* Type 32 -- System Boot Information */
499 static void *
500 smbios_type_32_init(void *start)
501 {
502     struct smbios_type_32 *p = (struct smbios_type_32 *)start;
503
504     p->header.type = 32;
505     p->header.length = sizeof(struct smbios_type_32);
506     p->header.handle = 0x2000;
507     memset(p->reserved, 0, 6);
508     p->boot_status = 0; /* no errors detected */
509
510     start += sizeof(struct smbios_type_32);
511     *((u16 *)start) = 0;
512
513     return start+2;
514 }
515
516 /* Type 127 -- End of Table */
517 static void *
518 smbios_type_127_init(void *start)
519 {
520     struct smbios_type_127 *p = (struct smbios_type_127 *)start;
521
522     p->header.type = 127;
523     p->header.length = sizeof(struct smbios_type_127);
524     p->header.handle = 0x7f00;
525
526     start += sizeof(struct smbios_type_127);
527     *((u16 *)start) = 0;
528
529     return start + 2;
530 }
531
532 void
533 smbios_init(void)
534 {
535     if (! CONFIG_SMBIOS)
536         return;
537
538     dprintf(3, "init SMBIOS tables\n");
539
540     unsigned cpu_num, nr_structs = 0, max_struct_size = 0;
541     char *start, *p, *q;
542
543     bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
544     start = (void *)(bios_table_cur_addr);
545
546     p = (char *)start + sizeof(struct smbios_entry_point);
547
548 #define add_struct(fn) { \
549     q = (fn); \
550     nr_structs++; \
551     if ((q - p) > max_struct_size) \
552         max_struct_size = q - p; \
553     p = q; \
554 }
555
556     add_struct(smbios_type_0_init(p));
557     add_struct(smbios_type_1_init(p));
558     add_struct(smbios_type_3_init(p));
559     int smp_cpus = CountCPUs;
560     for (cpu_num = 1; cpu_num <= smp_cpus; cpu_num++)
561         add_struct(smbios_type_4_init(p, cpu_num));
562     add_struct(smbios_type_16_init(p));
563     add_struct(smbios_type_17_init(p));
564     add_struct(smbios_type_19_init(p));
565     add_struct(smbios_type_20_init(p));
566     add_struct(smbios_type_32_init(p));
567     add_struct(smbios_type_127_init(p));
568
569 #undef add_struct
570
571     smbios_entry_point_init(
572         start, max_struct_size,
573         (p - (char *)start) - sizeof(struct smbios_entry_point),
574         (u32)(start + sizeof(struct smbios_entry_point)),
575         nr_structs);
576
577     bios_table_cur_addr += (p - (char *)start);
578
579     dprintf(1, "SMBIOS table addr=0x%08lx\n", (unsigned long)start);
580 }