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