blob: f3b5ad9dd9a02d8c21e86276246d07d8d66c4f39 [file] [log] [blame]
Kevin O'Connor0525d292008-07-04 06:18:30 -04001// smbios table generation (on emulators)
Kevin O'Connor8996ad22014-04-07 16:02:22 -04002// DO NOT ADD NEW FEATURES HERE. (See paravirt.c / biostables.c instead.)
Kevin O'Connora4d35762008-03-08 15:43:03 -05003//
Kevin O'Connor2929c352009-07-25 13:48:27 -04004// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor0525d292008-07-04 06:18:30 -04005// Copyright (C) 2006 Fabrice Bellard
Kevin O'Connora4d35762008-03-08 15:43:03 -05006//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05007// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connora4d35762008-03-08 15:43:03 -05008
Kevin O'Connorde9e7052013-02-09 19:09:20 -05009#include "config.h" // CONFIG_*
Kevin O'Connor9dea5902013-09-14 20:23:54 -040010#include "malloc.h" // free
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040011#include "output.h" // dprintf
12#include "paravirt.h" // RamSize
Kevin O'Connor41639f82013-09-14 19:37:36 -040013#include "romfile.h" // romfile_findprefix
Kevin O'Connorb37a5282013-09-14 22:45:05 -040014#include "std/smbios.h" // struct smbios_entry_point
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040015#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "util.h" // MaxCountCPUs
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040017#include "x86.h" // cpuid
Kevin O'Connor0525d292008-07-04 06:18:30 -040018
Kevin O'Connora4d35762008-03-08 15:43:03 -050019static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050020smbios_entry_point_setup(u16 max_structure_size,
21 u16 structure_table_length,
22 void *structure_table_address,
23 u16 number_of_structures)
Kevin O'Connora4d35762008-03-08 15:43:03 -050024{
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'Connor3bd2dc62014-04-07 16:35:18 -040032 if (!finaltable) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050033 warn_noalloc();
Kevin O'Connor2929c352009-07-25 13:48:27 -040034 return;
35 }
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -050036 memcpy(finaltable, structure_table_address, structure_table_length);
Kevin O'Connora4d35762008-03-08 15:43:03 -050037
Kevin O'Connor3bd2dc62014-04-07 16:35:18 -040038 struct smbios_entry_point ep;
39 memset(&ep, 0, sizeof(ep));
Kevin O'Connor9d3c0632015-04-09 11:57:44 -040040 ep.signature = SMBIOS_SIGNATURE;
Kevin O'Connor3bd2dc62014-04-07 16:35:18 -040041 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);
Kevin O'Connora4d35762008-03-08 15:43:03 -050046
Kevin O'Connor3bd2dc62014-04-07 16:35:18 -040047 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;
Kevin O'Connora4d35762008-03-08 15:43:03 -050051
Kevin O'Connor3bd2dc62014-04-07 16:35:18 -040052 ep.checksum -= checksum(&ep, 0x10);
Kevin O'Connora4d35762008-03-08 15:43:03 -050053
Kevin O'Connor3bd2dc62014-04-07 16:35:18 -040054 ep.intermediate_checksum -= checksum((void*)&ep + 0x10, ep.length - 0x10);
Kevin O'Connor2929c352009-07-25 13:48:27 -040055
Kevin O'Connor3bd2dc62014-04-07 16:35:18 -040056 copy_smbios(&ep);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040057}
Kevin O'Connora4d35762008-03-08 15:43:03 -050058
Kevin O'Connorde9e7052013-02-09 19:09:20 -050059static int
60get_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
71static int
72get_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
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400126#define load_str_field_with_default(type, field, def) \
127 do { \
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500128 size = get_field(type, offsetof(struct smbios_type_##type, \
129 field), end); \
Gerd Hoffmann344496f2014-01-20 14:32:54 +0100130 if (size == 1) { \
131 /* zero-length string, skip to avoid bogus end marker */ \
132 p->field = 0; \
133 } else if (size > 1) { \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400134 end += size; \
Gerd Hoffmann344496f2014-01-20 14:32:54 +0100135 p->field = ++str_index; \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400136 } else { \
137 memcpy(end, def, sizeof(def)); \
138 end += sizeof(def); \
Gerd Hoffmann344496f2014-01-20 14:32:54 +0100139 p->field = ++str_index; \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400140 } \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400141 } 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); \
Gerd Hoffmann344496f2014-01-20 14:32:54 +0100147 if (size > 1) { \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400148 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
Gabriel L. Somlod85c22e2014-04-23 15:04:49 -0400507smbios_legacy_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'Connor9b9c2402014-03-11 11:46:42 -0400519 memset(start, 0, TEMPSMBIOSSIZE);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500520
Kevin O'Connor2929c352009-07-25 13:48:27 -0400521 u32 nr_structs = 0, max_struct_size = 0;
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -0500522 char *q, *p = start;
523 char *end = start + TEMPSMBIOSSIZE - sizeof(struct smbios_type_127);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500524
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400525#define add_struct(type, args...) \
526 do { \
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500527 if (!get_external(type, &p, &nr_structs, &max_struct_size, end)) { \
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400528 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)
Kevin O'Connora4d35762008-03-08 15:43:03 -0500535
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400536 add_struct(0, p);
537 add_struct(1, p);
538 add_struct(3, p);
539
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400540 int cpu_num;
541 for (cpu_num = 1; cpu_num <= MaxCountCPUs; cpu_num++)
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400542 add_struct(4, p, cpu_num);
Alex Williamson6d663162010-05-07 13:38:55 -0600543
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;
Kevin O'Connor2a3dfea2009-10-07 19:44:23 -0400549 for (i = 0; i < nr_mem_devs; i++) {
Alex Williamson6d663162010-05-07 13:38:55 -0600550 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 }
Kevin O'Connor7d09d0e2010-05-10 21:51:38 -0400555
Alex Williamson6d663162010-05-07 13:38:55 -0600556 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 }
Kevin O'Connor2a3dfea2009-10-07 19:44:23 -0400573 }
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400574
575 add_struct(32, p);
576 /* Add any remaining provided entries before the end marker */
577 for (i = 0; i < 256; i++)
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500578 get_external(i, &p, &nr_structs, &max_struct_size, end);
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400579 add_struct(127, p);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500580
581#undef add_struct
582
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500583 smbios_entry_point_setup(max_struct_size, p - start, start, nr_structs);
Kevin O'Connorfe2c3ee2009-12-26 23:26:44 -0500584 free(start);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500585}