7bcdcb36c65f3db0a4d2190b8706e3e54c9fc1c5
[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" // qemu_cfg_smbios_load_field
11 #include "smbios.h" // struct smbios_entry_point
12
13 static void
14 smbios_entry_point_init(u16 max_structure_size,
15                         u16 structure_table_length,
16                         void *structure_table_address,
17                         u16 number_of_structures)
18 {
19     struct smbios_entry_point *ep = malloc_fseg(sizeof(*ep));
20     void *finaltable = malloc_high(structure_table_length);
21     if (!ep || !finaltable) {
22         warn_noalloc();
23         free(ep);
24         free(finaltable);
25         return;
26     }
27     memcpy(finaltable, structure_table_address, structure_table_length);
28
29     memcpy(ep->anchor_string, "_SM_", 4);
30     ep->length = 0x1f;
31     ep->smbios_major_version = 2;
32     ep->smbios_minor_version = 4;
33     ep->max_structure_size = max_structure_size;
34     ep->entry_point_revision = 0;
35     memset(ep->formatted_area, 0, 5);
36     memcpy(ep->intermediate_anchor_string, "_DMI_", 5);
37
38     ep->structure_table_length = structure_table_length;
39     ep->structure_table_address = (u32)finaltable;
40     ep->number_of_structures = number_of_structures;
41     ep->smbios_bcd_revision = 0x24;
42
43     ep->checksum -= checksum(ep, 0x10);
44
45     ep->intermediate_checksum -= checksum((void*)ep + 0x10, ep->length - 0x10);
46
47     dprintf(1, "SMBIOS ptr=%p table=%p\n", ep, finaltable);
48 }
49
50 #define load_str_field_with_default(type, field, def)                   \
51     do {                                                                \
52         size = qemu_cfg_smbios_load_field(type,                         \
53                                  offsetof(struct smbios_type_##type,    \
54                                           field), end);                 \
55         if (size > 0) {                                                 \
56             end += size;                                                \
57         } else {                                                        \
58             memcpy(end, def, sizeof(def));                              \
59             end += sizeof(def);                                         \
60         }                                                               \
61         p->field = ++str_index;                                         \
62     } while (0)
63
64 #define load_str_field_or_skip(type, field) \
65     do {                                                                \
66         size = qemu_cfg_smbios_load_field(type,                         \
67                                  offsetof(struct smbios_type_##type,    \
68                                           field), end);                 \
69         if (size > 0) {                                                 \
70             end += size;                                                \
71             p->field = ++str_index;                                     \
72         } else {                                                        \
73             p->field = 0;                                               \
74         }                                                               \
75     } while (0)
76
77 /* Type 0 -- BIOS Information */
78 #define RELEASE_DATE_STR "01/01/2007"
79 static void *
80 smbios_init_type_0(void *start)
81 {
82     struct smbios_type_0 *p = (struct smbios_type_0 *)start;
83     char *end = (char *)start + sizeof(struct smbios_type_0);
84     size_t size;
85     int str_index = 0;
86
87     p->header.type = 0;
88     p->header.length = sizeof(struct smbios_type_0);
89     p->header.handle = 0;
90
91     load_str_field_with_default(0, vendor_str, CONFIG_APPNAME);
92     load_str_field_with_default(0, bios_version_str, CONFIG_APPNAME);
93
94     p->bios_starting_address_segment = 0xe800;
95
96     load_str_field_with_default(0, bios_release_date_str, RELEASE_DATE_STR);
97
98     p->bios_rom_size = 0; /* FIXME */
99
100     memset(p->bios_characteristics, 0, 8);
101     p->bios_characteristics[0] = 0x08; /* BIOS characteristics not supported */
102     p->bios_characteristics_extension_bytes[0] = 0;
103     /* Enable targeted content distribution. Needed for SVVP */
104     p->bios_characteristics_extension_bytes[1] = 4;
105
106     if (!qemu_cfg_smbios_load_field(0, offsetof(struct smbios_type_0,
107                                                 system_bios_major_release),
108                                     &p->system_bios_major_release))
109         p->system_bios_major_release = 1;
110
111     if (!qemu_cfg_smbios_load_field(0, offsetof(struct smbios_type_0,
112                                                 system_bios_minor_release),
113                                     &p->system_bios_minor_release))
114         p->system_bios_minor_release = 0;
115
116     p->embedded_controller_major_release = 0xff;
117     p->embedded_controller_minor_release = 0xff;
118
119     *end = 0;
120     end++;
121
122     return end;
123 }
124
125 /* Type 1 -- System Information */
126 static void *
127 smbios_init_type_1(void *start)
128 {
129     struct smbios_type_1 *p = (struct smbios_type_1 *)start;
130     char *end = (char *)start + sizeof(struct smbios_type_1);
131     size_t size;
132     int str_index = 0;
133
134     p->header.type = 1;
135     p->header.length = sizeof(struct smbios_type_1);
136     p->header.handle = 0x100;
137
138     load_str_field_with_default(1, manufacturer_str, CONFIG_APPNAME);
139     load_str_field_with_default(1, product_name_str, CONFIG_APPNAME);
140     load_str_field_or_skip(1, version_str);
141     load_str_field_or_skip(1, serial_number_str);
142
143     size = qemu_cfg_smbios_load_field(1, offsetof(struct smbios_type_1,
144                                                   uuid), &p->uuid);
145     if (size == 0)
146         memset(p->uuid, 0, 16);
147
148     p->wake_up_type = 0x06; /* power switch */
149
150     load_str_field_or_skip(1, sku_number_str);
151     load_str_field_or_skip(1, family_str);
152
153     *end = 0;
154     end++;
155     if (!str_index) {
156         *end = 0;
157         end++;
158     }
159
160     return end;
161 }
162
163 /* Type 3 -- System Enclosure */
164 static void *
165 smbios_init_type_3(void *start)
166 {
167     struct smbios_type_3 *p = (struct smbios_type_3 *)start;
168
169     p->header.type = 3;
170     p->header.length = sizeof(struct smbios_type_3);
171     p->header.handle = 0x300;
172
173     p->manufacturer_str = 1;
174     p->type = 0x01; /* other */
175     p->version_str = 0;
176     p->serial_number_str = 0;
177     p->asset_tag_number_str = 0;
178     p->boot_up_state = 0x03; /* safe */
179     p->power_supply_state = 0x03; /* safe */
180     p->thermal_state = 0x03; /* safe */
181     p->security_status = 0x02; /* unknown */
182     p->oem_defined = 0;
183     p->height = 0;
184     p->number_of_power_cords = 0;
185     p->contained_element_count = 0;
186
187     start += sizeof(struct smbios_type_3);
188     memcpy((char *)start, CONFIG_APPNAME"\0\0", sizeof(CONFIG_APPNAME) + 1);
189
190     return start + sizeof(CONFIG_APPNAME) + 1;
191 }
192
193 /* Type 4 -- Processor Information */
194 static void *
195 smbios_init_type_4(void *start, unsigned int cpu_number)
196 {
197     struct smbios_type_4 *p = (struct smbios_type_4 *)start;
198
199     p->header.type = 4;
200     p->header.length = sizeof(struct smbios_type_4);
201     p->header.handle = 0x400 + cpu_number;
202
203     p->socket_designation_str = 1;
204     p->processor_type = 0x03; /* CPU */
205     p->processor_family = 0x01; /* other */
206     p->processor_manufacturer_str = 2;
207
208     u32 cpuid_signature, ebx, ecx, cpuid_features;
209     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
210     p->processor_id[0] = cpuid_signature;
211     p->processor_id[1] = cpuid_features;
212
213     p->processor_version_str = 0;
214     p->voltage = 0;
215     p->external_clock = 0;
216
217     p->max_speed = 2000;
218     p->current_speed = 2000;
219
220     p->status = 0x41; /* socket populated, CPU enabled */
221     p->processor_upgrade = 0x01; /* other */
222
223     p->l1_cache_handle = 0xffff; /* cache information structure not provided */
224     p->l2_cache_handle = 0xffff;
225     p->l3_cache_handle = 0xffff;
226
227     start += sizeof(struct smbios_type_4);
228
229     snprintf((char*)start, 6, "CPU%2x", cpu_number);
230     start += 6;
231     memcpy((char *)start, CONFIG_APPNAME"\0\0", sizeof(CONFIG_APPNAME) + 1);
232
233     return start + sizeof(CONFIG_APPNAME) + 1;
234 }
235
236 /* Type 16 -- Physical Memory Array */
237 static void *
238 smbios_init_type_16(void *start, u32 memory_size_mb, int nr_mem_devs)
239 {
240     struct smbios_type_16 *p = (struct smbios_type_16*)start;
241
242     p->header.type = 16;
243     p->header.length = sizeof(struct smbios_type_16);
244     p->header.handle = 0x1000;
245
246     p->location = 0x01; /* other */
247     p->use = 0x03; /* system memory */
248     p->error_correction = 0x06; /* Multi-bit ECC to make Microsoft happy */
249     /* 0x80000000 = unknown, accept sizes < 2TB - TODO multiple arrays */
250     p->maximum_capacity = memory_size_mb < 2 << 20 ?
251                           memory_size_mb << 10 : 0x80000000;
252     p->memory_error_information_handle = 0xfffe; /* none provided */
253     p->number_of_memory_devices = nr_mem_devs;
254
255     start += sizeof(struct smbios_type_16);
256     *((u16 *)start) = 0;
257
258     return start + 2;
259 }
260
261 /* Type 17 -- Memory Device */
262 static void *
263 smbios_init_type_17(void *start, u32 size_mb, int instance)
264 {
265     struct smbios_type_17 *p = (struct smbios_type_17 *)start;
266
267     p->header.type = 17;
268     p->header.length = sizeof(struct smbios_type_17);
269     p->header.handle = 0x1100 + instance;
270
271     p->physical_memory_array_handle = 0x1000;
272     p->total_width = 64;
273     p->data_width = 64;
274 /* TODO: should assert in case something is wrong   ASSERT((memory_size_mb & ~0x7fff) == 0); */
275     p->size = size_mb;
276     p->form_factor = 0x09; /* DIMM */
277     p->device_set = 0;
278     p->device_locator_str = 1;
279     p->bank_locator_str = 0;
280     p->memory_type = 0x07; /* RAM */
281     p->type_detail = 0;
282
283     start += sizeof(struct smbios_type_17);
284     memcpy((char *)start, "DIMM 0", 7);
285     ((char*)start)[5] += instance;
286     start += 7;
287     *((u8 *)start) = 0;
288
289     return start+1;
290 }
291
292 /* Type 19 -- Memory Array Mapped Address */
293 static void *
294 smbios_init_type_19(void *start, u32 start_mb, u32 size_mb, int instance)
295 {
296     struct smbios_type_19 *p = (struct smbios_type_19 *)start;
297
298     p->header.type = 19;
299     p->header.length = sizeof(struct smbios_type_19);
300     p->header.handle = 0x1300 + instance;
301
302     p->starting_address = start_mb << 10;
303     p->ending_address = p->starting_address + (size_mb << 10) - 1;
304     p->memory_array_handle = 0x1000;
305     p->partition_width = 1;
306
307     start += sizeof(struct smbios_type_19);
308     *((u16 *)start) = 0;
309
310     return start + 2;
311 }
312
313 /* Type 20 -- Memory Device Mapped Address */
314 static void *
315 smbios_init_type_20(void *start, u32 start_mb, u32 size_mb, int instance,
316                     int dev_handle, int array_handle)
317 {
318     struct smbios_type_20 *p = (struct smbios_type_20 *)start;
319
320     p->header.type = 20;
321     p->header.length = sizeof(struct smbios_type_20);
322     p->header.handle = 0x1400 + instance;
323
324     p->starting_address = start_mb << 10;
325     p->ending_address = p->starting_address + (size_mb << 10) - 1;
326     p->memory_device_handle = 0x1100 + dev_handle;
327     p->memory_array_mapped_address_handle = 0x1300 + array_handle;
328     p->partition_row_position = 1;
329     p->interleave_position = 0;
330     p->interleaved_data_depth = 0;
331
332     start += sizeof(struct smbios_type_20);
333
334     *((u16 *)start) = 0;
335     return start+2;
336 }
337
338 /* Type 32 -- System Boot Information */
339 static void *
340 smbios_init_type_32(void *start)
341 {
342     struct smbios_type_32 *p = (struct smbios_type_32 *)start;
343
344     p->header.type = 32;
345     p->header.length = sizeof(struct smbios_type_32);
346     p->header.handle = 0x2000;
347     memset(p->reserved, 0, 6);
348     p->boot_status = 0; /* no errors detected */
349
350     start += sizeof(struct smbios_type_32);
351     *((u16 *)start) = 0;
352
353     return start+2;
354 }
355
356 /* Type 127 -- End of Table */
357 static void *
358 smbios_init_type_127(void *start)
359 {
360     struct smbios_type_127 *p = (struct smbios_type_127 *)start;
361
362     p->header.type = 127;
363     p->header.length = sizeof(struct smbios_type_127);
364     p->header.handle = 0x7f00;
365
366     start += sizeof(struct smbios_type_127);
367     *((u16 *)start) = 0;
368
369     return start + 2;
370 }
371
372 #define TEMPSMBIOSSIZE (32 * 1024)
373
374 void
375 smbios_init(void)
376 {
377     if (! CONFIG_SMBIOS)
378         return;
379
380     dprintf(3, "init SMBIOS tables\n");
381
382     char *start = malloc_tmphigh(TEMPSMBIOSSIZE);
383     if (! start) {
384         warn_noalloc();
385         return;
386     }
387
388     u32 nr_structs = 0, max_struct_size = 0;
389     char *q, *p = start;
390     char *end = start + TEMPSMBIOSSIZE - sizeof(struct smbios_type_127);
391
392 #define add_struct(type, args...)                                       \
393     do {                                                                \
394         if (!qemu_cfg_smbios_load_external(type, &p, &nr_structs,       \
395                                            &max_struct_size, end)) {    \
396             q = smbios_init_type_##type(args);                          \
397             nr_structs++;                                               \
398             if ((q - p) > max_struct_size)                              \
399                 max_struct_size = q - p;                                \
400             p = q;                                                      \
401         }                                                               \
402     } while (0)
403
404     add_struct(0, p);
405     add_struct(1, p);
406     add_struct(3, p);
407
408     int cpu_num;
409     for (cpu_num = 1; cpu_num <= MaxCountCPUs; cpu_num++)
410         add_struct(4, p, cpu_num);
411
412     int ram_mb = (RamSize + RamSizeOver4G) >> 20;
413     int nr_mem_devs = (ram_mb + 0x3fff) >> 14;
414     add_struct(16, p, ram_mb, nr_mem_devs);
415
416     int i, j;
417     for (i = 0; i < nr_mem_devs; i++) {
418         u32 dev_mb = ((i == (nr_mem_devs - 1))
419                       ? (((ram_mb - 1) & 0x3fff) + 1)
420                       : 16384);
421         add_struct(17, p, dev_mb, i);
422     }
423         
424     add_struct(19, p, 0, RamSize >> 20, 0);
425     if (RamSizeOver4G)
426         add_struct(19, p, 4096, RamSizeOver4G >> 20, 1);
427
428     add_struct(20, p, 0, RamSize >> 20, 0, 0, 0);
429     if (RamSizeOver4G) {
430         u32 start_mb = 4096;
431         for (j = 1, i = 0; i < nr_mem_devs; i++, j++) {
432             u32 dev_mb = ((i == (nr_mem_devs - 1))
433                                ? (((ram_mb - 1) & 0x3fff) + 1)
434                                : 16384);
435             if (i == 0)
436                 dev_mb -= RamSize >> 20;
437
438             add_struct(20, p, start_mb, dev_mb, j, i, 1);
439             start_mb += dev_mb;
440         }
441     }
442
443     add_struct(32, p);
444     /* Add any remaining provided entries before the end marker */
445     for (i = 0; i < 256; i++)
446         qemu_cfg_smbios_load_external(i, &p, &nr_structs, &max_struct_size,
447                                       end);
448     add_struct(127, p);
449
450 #undef add_struct
451
452     smbios_entry_point_init(max_struct_size, p - start, start, nr_structs);
453     free(start);
454 }