blob: 2f95d9633f38d2a4074b2a7fc46a880ae7610940 [file] [log] [blame]
Kevin O'Connor0525d292008-07-04 06:18:30 -04001// smbios table generation (on emulators)
Kevin O'Connora4d35762008-03-08 15:43:03 -05002//
Kevin O'Connor2929c352009-07-25 13:48:27 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor0525d292008-07-04 06:18:30 -04004// Copyright (C) 2006 Fabrice Bellard
Kevin O'Connora4d35762008-03-08 15:43:03 -05005//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connora4d35762008-03-08 15:43:03 -05007
Kevin O'Connorac8df8c2008-05-24 23:46:33 -04008#include "util.h" // dprintf
Kevin O'Connorde9e7052013-02-09 19:09:20 -05009#include "config.h" // CONFIG_*
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050010#include "paravirt.h" // RamSize
Kevin O'Connor01a85202009-10-18 09:49:59 -040011#include "smbios.h" // struct smbios_entry_point
Kevin O'Connor41639f82013-09-14 19:37:36 -040012#include "romfile.h" // romfile_findprefix
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040013#include "string.h" // memset
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040014#include "x86.h" // cpuid
Kevin O'Connor0525d292008-07-04 06:18:30 -040015
Kevin O'Connor83012de2011-08-28 12:42:15 -040016struct smbios_entry_point *SMBiosAddr;
17
Kevin O'Connora4d35762008-03-08 15:43:03 -050018static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050019smbios_entry_point_setup(u16 max_structure_size,
20 u16 structure_table_length,
21 void *structure_table_address,
22 u16 number_of_structures)
Kevin O'Connora4d35762008-03-08 15:43:03 -050023{
Kevin O'Connor2929c352009-07-25 13:48:27 -040024 struct smbios_entry_point *ep = malloc_fseg(sizeof(*ep));
Kevin O'Connor32f03872011-08-03 20:45:32 -040025 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);
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -050032 if (!ep || !finaltable) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050033 warn_noalloc();
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -050034 free(ep);
35 free(finaltable);
Kevin O'Connor2929c352009-07-25 13:48:27 -040036 return;
37 }
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -050038 memcpy(finaltable, structure_table_address, structure_table_length);
Kevin O'Connora4d35762008-03-08 15:43:03 -050039
40 memcpy(ep->anchor_string, "_SM_", 4);
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 ep->entry_point_revision = 0;
46 memset(ep->formatted_area, 0, 5);
47 memcpy(ep->intermediate_anchor_string, "_DMI_", 5);
48
49 ep->structure_table_length = structure_table_length;
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -050050 ep->structure_table_address = (u32)finaltable;
Kevin O'Connora4d35762008-03-08 15:43:03 -050051 ep->number_of_structures = number_of_structures;
52 ep->smbios_bcd_revision = 0x24;
53
Kevin O'Connor2929c352009-07-25 13:48:27 -040054 ep->checksum -= checksum(ep, 0x10);
Kevin O'Connora4d35762008-03-08 15:43:03 -050055
Kevin O'Connor2929c352009-07-25 13:48:27 -040056 ep->intermediate_checksum -= checksum((void*)ep + 0x10, ep->length - 0x10);
57
Kevin O'Connor83012de2011-08-28 12:42:15 -040058 SMBiosAddr = ep;
Kevin O'Connor32f03872011-08-03 20:45:32 -040059 dprintf(1, "SMBIOS ptr=%p table=%p size=%d\n"
60 , ep, finaltable, structure_table_length);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040061}
Kevin O'Connora4d35762008-03-08 15:43:03 -050062
Kevin O'Connorde9e7052013-02-09 19:09:20 -050063static int
64get_field(int type, int offset, void *dest)
65{
66 char name[128];
67 snprintf(name, sizeof(name), "smbios/field%d-%d", type, offset);
68 struct romfile_s *file = romfile_find(name);
69 if (!file)
70 return 0;
71 file->copy(file, dest, file->size);
72 return file->size;
73}
74
75static int
76get_external(int type, char **p, unsigned *nr_structs,
77 unsigned *max_struct_size, char *end)
78{
79 static u64 used_bitmap[4] = { 0 };
80 char *start = *p;
81
82 /* Check if we've already reported these tables */
83 if (used_bitmap[(type >> 6) & 0x3] & (1ULL << (type & 0x3f)))
84 return 1;
85
86 /* Don't introduce spurious end markers */
87 if (type == 127)
88 return 0;
89
90 char prefix[128];
91 snprintf(prefix, sizeof(prefix), "smbios/table%d-", type);
92 struct romfile_s *file = NULL;
93 for (;;) {
94 file = romfile_findprefix(prefix, file);
95 if (!file)
96 break;
97
98 if (end - *p < file->size) {
99 warn_noalloc();
100 break;
101 }
102
103 struct smbios_structure_header *header = (void*)*p;
104 file->copy(file, header, file->size);
105 *p += file->size;
106
107 /* Entries end with a double NULL char, if there's a string at
108 * the end (length is greater than formatted length), the string
109 * terminator provides the first NULL. */
110 *((u8*)*p) = 0;
111 (*p)++;
112 if (header->length >= file->size) {
113 *((u8*)*p) = 0;
114 (*p)++;
115 }
116
117 (*nr_structs)++;
118 if (*p - (char*)header > *max_struct_size)
119 *max_struct_size = *p - (char*)header;
120 }
121
122 if (start == *p)
123 return 0;
124
125 /* Mark that we've reported on this type */
126 used_bitmap[(type >> 6) & 0x3] |= (1ULL << (type & 0x3f));
127 return 1;
128}
129
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400130#define load_str_field_with_default(type, field, def) \
131 do { \
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500132 size = get_field(type, offsetof(struct smbios_type_##type, \
133 field), end); \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400134 if (size > 0) { \
135 end += size; \
136 } else { \
137 memcpy(end, def, sizeof(def)); \
138 end += sizeof(def); \
139 } \
140 p->field = ++str_index; \
141 } while (0)
142
Alex Williamson17d3e462010-06-21 09:46:19 -0600143#define load_str_field_or_skip(type, field) \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400144 do { \
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500145 size = get_field(type, offsetof(struct smbios_type_##type, \
146 field), end); \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400147 if (size > 0) { \
148 end += size; \
149 p->field = ++str_index; \
150 } else { \
151 p->field = 0; \
152 } \
153 } while (0)
154
Alex Williamson17d3e462010-06-21 09:46:19 -0600155#define set_field_with_default(type, field, def) \
156 do { \
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500157 if (!get_field(type, offsetof(struct smbios_type_##type, \
158 field), &p->field)) { \
Alex Williamson17d3e462010-06-21 09:46:19 -0600159 p->field = def; \
160 } \
161 } while (0)
162
Kevin O'Connora4d35762008-03-08 15:43:03 -0500163/* Type 0 -- BIOS Information */
Gerd Hoffmann46ee8342012-06-07 10:34:31 +0200164#define RELEASE_DATE_STR "01/01/2011"
Kevin O'Connora4d35762008-03-08 15:43:03 -0500165static void *
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400166smbios_init_type_0(void *start)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500167{
168 struct smbios_type_0 *p = (struct smbios_type_0 *)start;
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400169 char *end = (char *)start + sizeof(struct smbios_type_0);
170 size_t size;
171 int str_index = 0;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500172
173 p->header.type = 0;
174 p->header.length = sizeof(struct smbios_type_0);
175 p->header.handle = 0;
176
Kevin O'Connore52ad392013-02-20 23:48:22 -0500177 load_str_field_with_default(0, vendor_str, BUILD_APPNAME);
178 load_str_field_with_default(0, bios_version_str, BUILD_APPNAME);
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400179
Kevin O'Connora4d35762008-03-08 15:43:03 -0500180 p->bios_starting_address_segment = 0xe800;
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400181
182 load_str_field_with_default(0, bios_release_date_str, RELEASE_DATE_STR);
183
Kevin O'Connora4d35762008-03-08 15:43:03 -0500184 p->bios_rom_size = 0; /* FIXME */
185
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500186 if (!get_field(0, offsetof(struct smbios_type_0, bios_characteristics),
187 &p->bios_characteristics)) {
Alex Williamson17d3e462010-06-21 09:46:19 -0600188 memset(p->bios_characteristics, 0, 8);
189 /* BIOS characteristics not supported */
190 p->bios_characteristics[0] = 0x08;
191 }
Kevin O'Connora4d35762008-03-08 15:43:03 -0500192
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500193 if (!get_field(0, offsetof(struct smbios_type_0,
194 bios_characteristics_extension_bytes),
195 &p->bios_characteristics_extension_bytes)) {
Alex Williamson17d3e462010-06-21 09:46:19 -0600196 p->bios_characteristics_extension_bytes[0] = 0;
197 /* Enable targeted content distribution. Needed for SVVP */
198 p->bios_characteristics_extension_bytes[1] = 4;
199 }
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400200
Alex Williamson17d3e462010-06-21 09:46:19 -0600201 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);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500205
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400206 *end = 0;
207 end++;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500208
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400209 return end;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500210}
211
212/* Type 1 -- System Information */
213static void *
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400214smbios_init_type_1(void *start)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500215{
216 struct smbios_type_1 *p = (struct smbios_type_1 *)start;
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400217 char *end = (char *)start + sizeof(struct smbios_type_1);
218 size_t size;
219 int str_index = 0;
220
Kevin O'Connora4d35762008-03-08 15:43:03 -0500221 p->header.type = 1;
222 p->header.length = sizeof(struct smbios_type_1);
223 p->header.handle = 0x100;
224
Kevin O'Connore52ad392013-02-20 23:48:22 -0500225 load_str_field_with_default(1, manufacturer_str, BUILD_APPNAME);
226 load_str_field_with_default(1, product_name_str, BUILD_APPNAME);
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400227 load_str_field_or_skip(1, version_str);
228 load_str_field_or_skip(1, serial_number_str);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500229
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500230 if (!get_field(1, offsetof(struct smbios_type_1, uuid), &p->uuid))
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400231 memset(p->uuid, 0, 16);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500232
Alex Williamson17d3e462010-06-21 09:46:19 -0600233 set_field_with_default(1, wake_up_type, 0x06); /* power switch */
Kevin O'Connora4d35762008-03-08 15:43:03 -0500234
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400235 load_str_field_or_skip(1, sku_number_str);
236 load_str_field_or_skip(1, family_str);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500237
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400238 *end = 0;
239 end++;
240 if (!str_index) {
241 *end = 0;
242 end++;
243 }
244
245 return end;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500246}
247
248/* Type 3 -- System Enclosure */
249static void *
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400250smbios_init_type_3(void *start)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500251{
252 struct smbios_type_3 *p = (struct smbios_type_3 *)start;
Alex Williamson17d3e462010-06-21 09:46:19 -0600253 char *end = (char *)start + sizeof(struct smbios_type_3);
254 size_t size;
255 int str_index = 0;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500256
257 p->header.type = 3;
258 p->header.length = sizeof(struct smbios_type_3);
259 p->header.handle = 0x300;
260
Kevin O'Connore52ad392013-02-20 23:48:22 -0500261 load_str_field_with_default(3, manufacturer_str, BUILD_APPNAME);
Alex Williamson17d3e462010-06-21 09:46:19 -0600262 set_field_with_default(3, type, 0x01); /* other */
Kevin O'Connora4d35762008-03-08 15:43:03 -0500263
Alex Williamson17d3e462010-06-21 09:46:19 -0600264 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);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500267
Alex Williamson17d3e462010-06-21 09:46:19 -0600268 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;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500286}
287
288/* Type 4 -- Processor Information */
289static void *
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400290smbios_init_type_4(void *start, unsigned int cpu_number)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500291{
292 struct smbios_type_4 *p = (struct smbios_type_4 *)start;
Alex Williamson17d3e462010-06-21 09:46:19 -0600293 char *end = (char *)start + sizeof(struct smbios_type_4);
294 size_t size;
295 int str_index = 0;
296 char name[1024];
Kevin O'Connora4d35762008-03-08 15:43:03 -0500297
298 p->header.type = 4;
299 p->header.length = sizeof(struct smbios_type_4);
300 p->header.handle = 0x400 + cpu_number;
301
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500302 size = get_field(4, offsetof(struct smbios_type_4, socket_designation_str),
303 name);
Alex Williamson17d3e462010-06-21 09:46:19 -0600304 if (size)
305 snprintf(name + size - 1, sizeof(name) - size, "%2x", cpu_number);
306 else
307 snprintf(name, sizeof(name), "CPU%2x", cpu_number);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500308
Alex Williamson17d3e462010-06-21 09:46:19 -0600309 memcpy(end, name, strlen(name) + 1);
310 end += strlen(name) + 1;
311 p->socket_designation_str = ++str_index;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500312
Alex Williamson17d3e462010-06-21 09:46:19 -0600313 set_field_with_default(4, processor_type, 0x03); /* CPU */
314 set_field_with_default(4, processor_family, 0x01); /* other */
Kevin O'Connora4d35762008-03-08 15:43:03 -0500315
Kevin O'Connore52ad392013-02-20 23:48:22 -0500316 load_str_field_with_default(4, processor_manufacturer_str, BUILD_APPNAME);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500317
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500318 if (!get_field(4, offsetof(struct smbios_type_4, processor_id)
319 , p->processor_id)) {
Alex Williamson17d3e462010-06-21 09:46:19 -0600320 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 }
Kevin O'Connora4d35762008-03-08 15:43:03 -0500325
Alex Williamson17d3e462010-06-21 09:46:19 -0600326 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);
Kevin O'Connorf2ce1912008-10-25 15:23:53 -0400329
Alex Williamson17d3e462010-06-21 09:46:19 -0600330 set_field_with_default(4, max_speed, 2000);
331 set_field_with_default(4, current_speed, 2000);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500332
Alex Williamson17d3e462010-06-21 09:46:19 -0600333 set_field_with_default(4, status, 0x41); /* socket populated, CPU enabled */
334 set_field_with_default(4, processor_upgrade, 0x01); /* other */
Kevin O'Connora4d35762008-03-08 15:43:03 -0500335
Alex Williamson17d3e462010-06-21 09:46:19 -0600336 /* 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;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500349}
350
351/* Type 16 -- Physical Memory Array */
352static void *
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400353smbios_init_type_16(void *start, u32 memory_size_mb, int nr_mem_devs)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500354{
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
Alex Williamson17d3e462010-06-21 09:46:19 -0600361 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);
Alex Williamson6d663162010-05-07 13:38:55 -0600365 /* 0x80000000 = unknown, accept sizes < 2TB - TODO multiple arrays */
366 p->maximum_capacity = memory_size_mb < 2 << 20 ?
367 memory_size_mb << 10 : 0x80000000;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500368 p->memory_error_information_handle = 0xfffe; /* none provided */
Kevin O'Connor2a3dfea2009-10-07 19:44:23 -0400369 p->number_of_memory_devices = nr_mem_devs;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500370
371 start += sizeof(struct smbios_type_16);
Kevin O'Connore51313d2008-03-12 21:19:34 -0400372 *((u16 *)start) = 0;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500373
374 return start + 2;
375}
376
377/* Type 17 -- Memory Device */
378static void *
Alex Williamson6d663162010-05-07 13:38:55 -0600379smbios_init_type_17(void *start, u32 size_mb, int instance)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500380{
381 struct smbios_type_17 *p = (struct smbios_type_17 *)start;
Alex Williamson17d3e462010-06-21 09:46:19 -0600382 char *end = (char *)start + sizeof(struct smbios_type_17);
383 size_t size;
384 int str_index = 0;
385 char name[1024];
Kevin O'Connora4d35762008-03-08 15:43:03 -0500386
387 p->header.type = 17;
388 p->header.length = sizeof(struct smbios_type_17);
Kevin O'Connor2a3dfea2009-10-07 19:44:23 -0400389 p->header.handle = 0x1100 + instance;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500390
391 p->physical_memory_array_handle = 0x1000;
Alex Williamson17d3e462010-06-21 09:46:19 -0600392 set_field_with_default(17, total_width, 64);
393 set_field_with_default(17, data_width, 64);
Kevin O'Connor2a3dfea2009-10-07 19:44:23 -0400394/* TODO: should assert in case something is wrong ASSERT((memory_size_mb & ~0x7fff) == 0); */
Alex Williamson6d663162010-05-07 13:38:55 -0600395 p->size = size_mb;
Alex Williamson17d3e462010-06-21 09:46:19 -0600396 set_field_with_default(17, form_factor, 0x09); /* DIMM */
Kevin O'Connora4d35762008-03-08 15:43:03 -0500397 p->device_set = 0;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500398
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500399 size = get_field(17, offsetof(struct smbios_type_17, device_locator_str),
400 name);
Alex Williamson17d3e462010-06-21 09:46:19 -0600401 if (size)
402 snprintf(name + size - 1, sizeof(name) - size, "%d", instance);
403 else
404 snprintf(name, sizeof(name), "DIMM %d", instance);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500405
Alex Williamson17d3e462010-06-21 09:46:19 -0600406 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;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500422}
423
424/* Type 19 -- Memory Array Mapped Address */
425static void *
Alex Williamson6d663162010-05-07 13:38:55 -0600426smbios_init_type_19(void *start, u32 start_mb, u32 size_mb, int instance)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500427{
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);
Kevin O'Connor2a3dfea2009-10-07 19:44:23 -0400432 p->header.handle = 0x1300 + instance;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500433
Alex Williamson6d663162010-05-07 13:38:55 -0600434 p->starting_address = start_mb << 10;
435 p->ending_address = p->starting_address + (size_mb << 10) - 1;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500436 p->memory_array_handle = 0x1000;
437 p->partition_width = 1;
438
439 start += sizeof(struct smbios_type_19);
Kevin O'Connore51313d2008-03-12 21:19:34 -0400440 *((u16 *)start) = 0;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500441
442 return start + 2;
443}
444
445/* Type 20 -- Memory Device Mapped Address */
446static void *
Alex Williamson6d663162010-05-07 13:38:55 -0600447smbios_init_type_20(void *start, u32 start_mb, u32 size_mb, int instance,
448 int dev_handle, int array_handle)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500449{
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);
Kevin O'Connor2a3dfea2009-10-07 19:44:23 -0400454 p->header.handle = 0x1400 + instance;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500455
Alex Williamson6d663162010-05-07 13:38:55 -0600456 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;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500460 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
Kevin O'Connore51313d2008-03-12 21:19:34 -0400466 *((u16 *)start) = 0;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500467 return start+2;
468}
469
470/* Type 32 -- System Boot Information */
471static void *
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400472smbios_init_type_32(void *start)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500473{
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);
Alex Williamson17d3e462010-06-21 09:46:19 -0600480 set_field_with_default(32, boot_status, 0); /* no errors detected */
Kevin O'Connora4d35762008-03-08 15:43:03 -0500481
482 start += sizeof(struct smbios_type_32);
Kevin O'Connore51313d2008-03-12 21:19:34 -0400483 *((u16 *)start) = 0;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500484
485 return start+2;
486}
487
488/* Type 127 -- End of Table */
489static void *
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400490smbios_init_type_127(void *start)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500491{
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);
Kevin O'Connore51313d2008-03-12 21:19:34 -0400499 *((u16 *)start) = 0;
Kevin O'Connora4d35762008-03-08 15:43:03 -0500500
501 return start + 2;
502}
503
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -0500504#define TEMPSMBIOSSIZE (32 * 1024)
505
Kevin O'Connor0525d292008-07-04 06:18:30 -0400506void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500507smbios_setup(void)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500508{
Kevin O'Connor6cb8ba92008-08-17 11:03:24 -0400509 if (! CONFIG_SMBIOS)
510 return;
511
Kevin O'Connor7b49cd92008-11-08 10:35:26 -0500512 dprintf(3, "init SMBIOS tables\n");
513
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -0500514 char *start = malloc_tmphigh(TEMPSMBIOSSIZE);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400515 if (! start) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500516 warn_noalloc();
Kevin O'Connor2929c352009-07-25 13:48:27 -0400517 return;
518 }
Kevin O'Connora4d35762008-03-08 15:43:03 -0500519
Kevin O'Connor2929c352009-07-25 13:48:27 -0400520 u32 nr_structs = 0, max_struct_size = 0;
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -0500521 char *q, *p = start;
522 char *end = start + TEMPSMBIOSSIZE - sizeof(struct smbios_type_127);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500523
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400524#define add_struct(type, args...) \
525 do { \
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500526 if (!get_external(type, &p, &nr_structs, &max_struct_size, end)) { \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400527 q = smbios_init_type_##type(args); \
528 nr_structs++; \
529 if ((q - p) > max_struct_size) \
530 max_struct_size = q - p; \
531 p = q; \
532 } \
533 } while (0)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500534
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400535 add_struct(0, p);
536 add_struct(1, p);
537 add_struct(3, p);
538
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400539 int cpu_num;
540 for (cpu_num = 1; cpu_num <= MaxCountCPUs; cpu_num++)
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400541 add_struct(4, p, cpu_num);
Alex Williamson6d663162010-05-07 13:38:55 -0600542
543 int ram_mb = (RamSize + RamSizeOver4G) >> 20;
544 int nr_mem_devs = (ram_mb + 0x3fff) >> 14;
545 add_struct(16, p, ram_mb, nr_mem_devs);
546
547 int i, j;
Kevin O'Connor2a3dfea2009-10-07 19:44:23 -0400548 for (i = 0; i < nr_mem_devs; i++) {
Alex Williamson6d663162010-05-07 13:38:55 -0600549 u32 dev_mb = ((i == (nr_mem_devs - 1))
550 ? (((ram_mb - 1) & 0x3fff) + 1)
551 : 16384);
552 add_struct(17, p, dev_mb, i);
553 }
Kevin O'Connor7d09d0e2010-05-10 21:51:38 -0400554
Alex Williamson6d663162010-05-07 13:38:55 -0600555 add_struct(19, p, 0, RamSize >> 20, 0);
556 if (RamSizeOver4G)
557 add_struct(19, p, 4096, RamSizeOver4G >> 20, 1);
558
559 add_struct(20, p, 0, RamSize >> 20, 0, 0, 0);
560 if (RamSizeOver4G) {
561 u32 start_mb = 4096;
562 for (j = 1, i = 0; i < nr_mem_devs; i++, j++) {
563 u32 dev_mb = ((i == (nr_mem_devs - 1))
564 ? (((ram_mb - 1) & 0x3fff) + 1)
565 : 16384);
566 if (i == 0)
567 dev_mb -= RamSize >> 20;
568
569 add_struct(20, p, start_mb, dev_mb, j, i, 1);
570 start_mb += dev_mb;
571 }
Kevin O'Connor2a3dfea2009-10-07 19:44:23 -0400572 }
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400573
574 add_struct(32, p);
575 /* Add any remaining provided entries before the end marker */
576 for (i = 0; i < 256; i++)
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500577 get_external(i, &p, &nr_structs, &max_struct_size, end);
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400578 add_struct(127, p);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500579
580#undef add_struct
581
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500582 smbios_entry_point_setup(max_struct_size, p - start, start, nr_structs);
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -0500583 free(start);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500584}
Laszlo Ersek37676f82012-12-14 12:59:39 +0100585
586void
587display_uuid(void)
588{
589 u32 addr, end;
590 u8 *uuid;
591 u8 empty_uuid[16] = { 0 };
592
593 if (SMBiosAddr == NULL)
594 return;
595
596 addr = SMBiosAddr->structure_table_address;
597 end = addr + SMBiosAddr->structure_table_length;
598
599 /* the following takes care of any initial wraparound too */
600 while (addr < end) {
601 const struct smbios_structure_header *hdr;
602
603 /* partial structure header */
604 if (end - addr < sizeof(struct smbios_structure_header))
605 return;
606
607 hdr = (struct smbios_structure_header *)addr;
608
609 /* partial structure */
610 if (end - addr < hdr->length)
611 return;
612
613 /* any Type 1 structure version will do that has the UUID */
614 if (hdr->type == 1 &&
615 hdr->length >= offsetof(struct smbios_type_1, uuid) + 16)
616 break;
617
618 /* done with formatted area, skip string-set */
619 addr += hdr->length;
620
621 while (end - addr >= 2 &&
622 (*(u8 *)addr != '\0' ||
623 *(u8 *)(addr+1) != '\0'))
624 ++addr;
625
626 /* structure terminator not found */
627 if (end - addr < 2)
628 return;
629
630 addr += 2;
631 }
632
Laszlo Ersek9c6fb722012-12-18 05:11:39 +0100633 /* parsing finished or skipped entirely, UUID not found */
634 if (addr >= end)
Laszlo Ersek37676f82012-12-14 12:59:39 +0100635 return;
636
637 uuid = (u8 *)(addr + offsetof(struct smbios_type_1, uuid));
638 if (memcmp(uuid, empty_uuid, sizeof empty_uuid) == 0)
639 return;
640
641 printf("Machine UUID"
642 " %02x%02x%02x%02x"
643 "-%02x%02x"
644 "-%02x%02x"
645 "-%02x%02x"
646 "-%02x%02x%02x%02x%02x%02x\n"
647 , uuid[ 0], uuid[ 1], uuid[ 2], uuid[ 3]
648 , uuid[ 4], uuid[ 5]
649 , uuid[ 6], uuid[ 7]
650 , uuid[ 8], uuid[ 9]
651 , uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
652}