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