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 if (!str_index) {
209 *end = 0;
210 end++;
211 }
212
213 return end;
214 }
215
216 /* Type 1 -- System Information */
217 static void *
smbios_init_type_1(void * start)218 smbios_init_type_1(void *start)
219 {
220 struct smbios_type_1 *p = (struct smbios_type_1 *)start;
221 char *end = (char *)start + sizeof(struct smbios_type_1);
222 size_t size;
223 int str_index = 0;
224
225 p->header.type = 1;
226 p->header.length = sizeof(struct smbios_type_1);
227 p->header.handle = 0x100;
228
229 load_str_field_with_default(1, manufacturer_str, BUILD_APPNAME);
230 load_str_field_with_default(1, product_name_str, BUILD_APPNAME);
231 load_str_field_or_skip(1, version_str);
232 load_str_field_or_skip(1, serial_number_str);
233
234 if (!get_field(1, offsetof(struct smbios_type_1, uuid), &p->uuid))
235 memset(p->uuid, 0, 16);
236
237 set_field_with_default(1, wake_up_type, 0x06); /* power switch */
238
239 load_str_field_or_skip(1, sku_number_str);
240 load_str_field_or_skip(1, family_str);
241
242 *end = 0;
243 end++;
244 if (!str_index) {
245 *end = 0;
246 end++;
247 }
248
249 return end;
250 }
251
252 /* Type 3 -- System Enclosure */
253 static void *
smbios_init_type_3(void * start)254 smbios_init_type_3(void *start)
255 {
256 struct smbios_type_3 *p = (struct smbios_type_3 *)start;
257 char *end = (char *)start + sizeof(struct smbios_type_3);
258 size_t size;
259 int str_index = 0;
260
261 p->header.type = 3;
262 p->header.length = sizeof(struct smbios_type_3);
263 p->header.handle = 0x300;
264
265 load_str_field_with_default(3, manufacturer_str, BUILD_APPNAME);
266 set_field_with_default(3, type, 0x01); /* other */
267
268 load_str_field_or_skip(3, version_str);
269 load_str_field_or_skip(3, serial_number_str);
270 load_str_field_or_skip(3, asset_tag_number_str);
271
272 set_field_with_default(3, boot_up_state, 0x03); /* safe */
273 set_field_with_default(3, power_supply_state, 0x03); /* safe */
274 set_field_with_default(3, thermal_state, 0x03); /* safe */
275 set_field_with_default(3, security_status, 0x02); /* unknown */
276
277 set_field_with_default(3, oem_defined, 0);
278 set_field_with_default(3, height, 0);
279 set_field_with_default(3, number_of_power_cords, 0);
280 set_field_with_default(3, contained_element_count, 0);
281
282 *end = 0;
283 end++;
284 if (!str_index) {
285 *end = 0;
286 end++;
287 }
288
289 return end;
290 }
291
292 /* Type 4 -- Processor Information */
293 static void *
smbios_init_type_4(void * start,unsigned int cpu_number)294 smbios_init_type_4(void *start, unsigned int cpu_number)
295 {
296 struct smbios_type_4 *p = (struct smbios_type_4 *)start;
297 char *end = (char *)start + sizeof(struct smbios_type_4);
298 size_t size;
299 int str_index = 0;
300 char name[1024];
301
302 p->header.type = 4;
303 p->header.length = sizeof(struct smbios_type_4);
304 p->header.handle = 0x400 + cpu_number;
305
306 size = get_field(4, offsetof(struct smbios_type_4, socket_designation_str),
307 name);
308 if (size)
309 snprintf(name + size - 1, sizeof(name) - size, "%2x", cpu_number);
310 else
311 snprintf(name, sizeof(name), "CPU%2x", cpu_number);
312
313 memcpy(end, name, strlen(name) + 1);
314 end += strlen(name) + 1;
315 p->socket_designation_str = ++str_index;
316
317 set_field_with_default(4, processor_type, 0x03); /* CPU */
318 set_field_with_default(4, processor_family, 0x01); /* other */
319
320 load_str_field_with_default(4, processor_manufacturer_str, BUILD_APPNAME);
321
322 if (!get_field(4, offsetof(struct smbios_type_4, processor_id)
323 , p->processor_id)) {
324 u32 cpuid_signature, ebx, ecx, cpuid_features;
325 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
326 p->processor_id[0] = cpuid_signature;
327 p->processor_id[1] = cpuid_features;
328 }
329
330 load_str_field_or_skip(4, processor_version_str);
331 set_field_with_default(4, voltage, 0);
332 set_field_with_default(4, external_clock, 0);
333
334 set_field_with_default(4, max_speed, 2000);
335 set_field_with_default(4, current_speed, 2000);
336
337 set_field_with_default(4, status, 0x41); /* socket populated, CPU enabled */
338 set_field_with_default(4, processor_upgrade, 0x01); /* other */
339
340 /* cache information structure not provided */
341 p->l1_cache_handle = 0xffff;
342 p->l2_cache_handle = 0xffff;
343 p->l3_cache_handle = 0xffff;
344
345 *end = 0;
346 end++;
347 if (!str_index) {
348 *end = 0;
349 end++;
350 }
351
352 return end;
353 }
354
355 /* Type 16 -- Physical Memory Array */
356 static void *
smbios_init_type_16(void * start,u32 memory_size_mb,int nr_mem_devs)357 smbios_init_type_16(void *start, u32 memory_size_mb, int nr_mem_devs)
358 {
359 struct smbios_type_16 *p = (struct smbios_type_16*)start;
360
361 p->header.type = 16;
362 p->header.length = sizeof(struct smbios_type_16);
363 p->header.handle = 0x1000;
364
365 set_field_with_default(16, location, 0x01); /* other */
366 set_field_with_default(16, use, 0x03); /* system memory */
367 /* Multi-bit ECC to make Microsoft happy */
368 set_field_with_default(16, error_correction, 0x06);
369 /* 0x80000000 = unknown, accept sizes < 2TB - TODO multiple arrays */
370 p->maximum_capacity = memory_size_mb < 2 << 20 ?
371 memory_size_mb << 10 : 0x80000000;
372 p->memory_error_information_handle = 0xfffe; /* none provided */
373 p->number_of_memory_devices = nr_mem_devs;
374
375 start += sizeof(struct smbios_type_16);
376 *((u16 *)start) = 0;
377
378 return start + 2;
379 }
380
381 /* Type 17 -- Memory Device */
382 static void *
smbios_init_type_17(void * start,u32 size_mb,int instance)383 smbios_init_type_17(void *start, u32 size_mb, int instance)
384 {
385 struct smbios_type_17 *p = (struct smbios_type_17 *)start;
386 char *end = (char *)start + sizeof(struct smbios_type_17);
387 size_t size;
388 int str_index = 0;
389 char name[1024];
390
391 p->header.type = 17;
392 p->header.length = sizeof(struct smbios_type_17);
393 p->header.handle = 0x1100 + instance;
394
395 p->physical_memory_array_handle = 0x1000;
396 set_field_with_default(17, total_width, 64);
397 set_field_with_default(17, data_width, 64);
398 /* TODO: should assert in case something is wrong ASSERT((memory_size_mb & ~0x7fff) == 0); */
399 p->size = size_mb;
400 set_field_with_default(17, form_factor, 0x09); /* DIMM */
401 p->device_set = 0;
402
403 size = get_field(17, offsetof(struct smbios_type_17, device_locator_str),
404 name);
405 if (size)
406 snprintf(name + size - 1, sizeof(name) - size, "%d", instance);
407 else
408 snprintf(name, sizeof(name), "DIMM %d", instance);
409
410 memcpy(end, name, strlen(name) + 1);
411 end += strlen(name) + 1;
412 p->device_locator_str = ++str_index;
413
414 load_str_field_or_skip(17, bank_locator_str);
415 set_field_with_default(17, memory_type, 0x07); /* RAM */
416 set_field_with_default(17, type_detail, 0);
417
418 *end = 0;
419 end++;
420 if (!str_index) {
421 *end = 0;
422 end++;
423 }
424
425 return end;
426 }
427
428 /* Type 19 -- Memory Array Mapped Address */
429 static void *
smbios_init_type_19(void * start,u32 start_mb,u32 size_mb,int instance)430 smbios_init_type_19(void *start, u32 start_mb, u32 size_mb, int instance)
431 {
432 struct smbios_type_19 *p = (struct smbios_type_19 *)start;
433
434 p->header.type = 19;
435 p->header.length = sizeof(struct smbios_type_19);
436 p->header.handle = 0x1300 + instance;
437
438 p->starting_address = start_mb << 10;
439 p->ending_address = p->starting_address + (size_mb << 10) - 1;
440 p->memory_array_handle = 0x1000;
441 p->partition_width = 1;
442
443 start += sizeof(struct smbios_type_19);
444 *((u16 *)start) = 0;
445
446 return start + 2;
447 }
448
449 /* Type 20 -- Memory Device Mapped Address */
450 static void *
smbios_init_type_20(void * start,u32 start_mb,u32 size_mb,int instance,int dev_handle,int array_handle)451 smbios_init_type_20(void *start, u32 start_mb, u32 size_mb, int instance,
452 int dev_handle, int array_handle)
453 {
454 struct smbios_type_20 *p = (struct smbios_type_20 *)start;
455
456 p->header.type = 20;
457 p->header.length = sizeof(struct smbios_type_20);
458 p->header.handle = 0x1400 + instance;
459
460 p->starting_address = start_mb << 10;
461 p->ending_address = p->starting_address + (size_mb << 10) - 1;
462 p->memory_device_handle = 0x1100 + dev_handle;
463 p->memory_array_mapped_address_handle = 0x1300 + array_handle;
464 p->partition_row_position = 1;
465 p->interleave_position = 0;
466 p->interleaved_data_depth = 0;
467
468 start += sizeof(struct smbios_type_20);
469
470 *((u16 *)start) = 0;
471 return start+2;
472 }
473
474 /* Type 32 -- System Boot Information */
475 static void *
smbios_init_type_32(void * start)476 smbios_init_type_32(void *start)
477 {
478 struct smbios_type_32 *p = (struct smbios_type_32 *)start;
479
480 p->header.type = 32;
481 p->header.length = sizeof(struct smbios_type_32);
482 p->header.handle = 0x2000;
483 memset(p->reserved, 0, 6);
484 set_field_with_default(32, boot_status, 0); /* no errors detected */
485
486 start += sizeof(struct smbios_type_32);
487 *((u16 *)start) = 0;
488
489 return start+2;
490 }
491
492 /* Type 127 -- End of Table */
493 static void *
smbios_init_type_127(void * start)494 smbios_init_type_127(void *start)
495 {
496 struct smbios_type_127 *p = (struct smbios_type_127 *)start;
497
498 p->header.type = 127;
499 p->header.length = sizeof(struct smbios_type_127);
500 p->header.handle = 0x7f00;
501
502 start += sizeof(struct smbios_type_127);
503 *((u16 *)start) = 0;
504
505 return start + 2;
506 }
507
508 #define TEMPSMBIOSSIZE (32 * 1024)
509
510 void
smbios_legacy_setup(void)511 smbios_legacy_setup(void)
512 {
513 if (! CONFIG_SMBIOS)
514 return;
515
516 dprintf(3, "init SMBIOS tables\n");
517
518 char *start = malloc_tmphigh(TEMPSMBIOSSIZE);
519 if (! start) {
520 warn_noalloc();
521 return;
522 }
523 memset(start, 0, TEMPSMBIOSSIZE);
524
525 u32 nr_structs = 0, max_struct_size = 0;
526 char *q, *p = start;
527 char *end = start + TEMPSMBIOSSIZE - sizeof(struct smbios_type_127);
528
529 #define add_struct(type, args...) \
530 do { \
531 if (!get_external(type, &p, &nr_structs, &max_struct_size, end)) { \
532 q = smbios_init_type_##type(args); \
533 nr_structs++; \
534 if ((q - p) > max_struct_size) \
535 max_struct_size = q - p; \
536 p = q; \
537 } \
538 } while (0)
539
540 add_struct(0, p);
541 add_struct(1, p);
542 add_struct(3, p);
543
544 int cpu_num;
545 for (cpu_num = 1; cpu_num <= MaxCountCPUs; cpu_num++)
546 add_struct(4, p, cpu_num);
547
548 int ram_mb = (RamSize + RamSizeOver4G) >> 20;
549 int nr_mem_devs = (ram_mb + 0x3fff) >> 14;
550 add_struct(16, p, ram_mb, nr_mem_devs);
551
552 int i, j;
553 for (i = 0; i < nr_mem_devs; i++) {
554 u32 dev_mb = ((i == (nr_mem_devs - 1))
555 ? (((ram_mb - 1) & 0x3fff) + 1)
556 : 16384);
557 add_struct(17, p, dev_mb, i);
558 }
559
560 add_struct(19, p, 0, RamSize >> 20, 0);
561 if (RamSizeOver4G)
562 add_struct(19, p, 4096, RamSizeOver4G >> 20, 1);
563
564 add_struct(20, p, 0, RamSize >> 20, 0, 0, 0);
565 if (RamSizeOver4G) {
566 u32 start_mb = 4096;
567 for (j = 1, i = 0; i < nr_mem_devs; i++, j++) {
568 u32 dev_mb = ((i == (nr_mem_devs - 1))
569 ? (((ram_mb - 1) & 0x3fff) + 1)
570 : 16384);
571 if (i == 0)
572 dev_mb -= RamSize >> 20;
573
574 add_struct(20, p, start_mb, dev_mb, j, i, 1);
575 start_mb += dev_mb;
576 }
577 }
578
579 add_struct(32, p);
580 /* Add any remaining provided entries before the end marker */
581 for (i = 0; i < 256; i++)
582 get_external(i, &p, &nr_structs, &max_struct_size, end);
583 add_struct(127, p);
584
585 #undef add_struct
586
587 smbios_entry_point_setup(max_struct_size, p - start, start, nr_structs);
588 free(start);
589 }
590