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