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