1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/*
* This file is part of the coreboot project.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <string.h>
#include <swab.h>
#include <smbios.h>
#include <console/console.h>
#include <arch/io.h>
#include <arch/acpigen.h>
#include "fw_cfg.h"
#include "fw_cfg_if.h"
#define FW_CFG_PORT_CTL 0x0510
#define FW_CFG_PORT_DATA 0x0511
static unsigned char fw_cfg_detected = 0xff;
static FWCfgFiles *fw_files;
static int fw_cfg_present(void)
{
static const char qsig[] = "QEMU";
unsigned char sig[4];
if (fw_cfg_detected == 0xff) {
fw_cfg_get(FW_CFG_SIGNATURE, sig, sizeof(sig));
fw_cfg_detected = (memcmp(sig, qsig, 4) == 0) ? 1 : 0;
printk(BIOS_INFO, "QEMU: firmware config interface %s\n",
fw_cfg_detected ? "detected" : "not found");
}
return fw_cfg_detected;
}
void fw_cfg_get(int entry, void *dst, int dstlen)
{
outw(entry, FW_CFG_PORT_CTL);
insb(FW_CFG_PORT_DATA, dst, dstlen);
}
static void fw_cfg_init_file(void)
{
u32 i, size, count = 0;
if (fw_files != NULL)
return;
fw_cfg_get(FW_CFG_FILE_DIR, &count, sizeof(count));
count = swab32(count);
size = count * sizeof(FWCfgFile) + sizeof(count);
printk(BIOS_DEBUG, "QEMU: %d files in fw_cfg\n", count);
fw_files = malloc(size);
fw_cfg_get(FW_CFG_FILE_DIR, fw_files, size);
fw_files->count = swab32(fw_files->count);
for (i = 0; i < count; i++) {
fw_files->f[i].size = swab32(fw_files->f[i].size);
fw_files->f[i].select = swab16(fw_files->f[i].select);
printk(BIOS_DEBUG, "QEMU: %s [size=%d]\n",
fw_files->f[i].name, fw_files->f[i].size);
}
}
static FWCfgFile *fw_cfg_find_file(const char *name)
{
int i;
fw_cfg_init_file();
for (i = 0; i < fw_files->count; i++)
if (strcmp(fw_files->f[i].name, name) == 0)
return fw_files->f + i;
return NULL;
}
int fw_cfg_check_file(const char *name)
{
FWCfgFile *file;
if (!fw_cfg_present())
return -1;
file = fw_cfg_find_file(name);
if (!file)
return -1;
return file->size;
}
void fw_cfg_load_file(const char *name, void *dst)
{
FWCfgFile *file;
if (!fw_cfg_present())
return;
file = fw_cfg_find_file(name);
if (!file)
return;
fw_cfg_get(file->select, dst, file->size);
}
int fw_cfg_max_cpus(void)
{
unsigned short max_cpus;
if (!fw_cfg_present())
return -1;
fw_cfg_get(FW_CFG_MAX_CPUS, &max_cpus, sizeof(max_cpus));
return max_cpus;
}
/* ---------------------------------------------------------------------- */
/*
* Starting with release 1.7 qemu provides ACPI tables via fw_cfg.
* Main advantage is that new (virtual) hardware which needs acpi
* support JustWorks[tm] without having to patch & update the firmware
* (seabios, coreboot, ...) accordingly.
*
* Qemu provides a etc/table-loader file with instructions for the
* firmware:
* - A "load" instruction to fetch ACPI data from fw_cfg.
* - A "pointer" instruction to patch a pointer. This is needed to
* get table-to-table references right, it is basically a
* primitive dynamic linker for ACPI tables.
* - A "checksum" instruction to generate ACPI table checksums.
*
* If a etc/table-loader file is found we'll go try loading the acpi
* tables from fw_cfg, otherwise we'll fallback to the ACPI tables
* compiled in.
*/
#define BIOS_LINKER_LOADER_FILESZ 56
struct BiosLinkerLoaderEntry {
uint32_t command;
union {
/*
* COMMAND_ALLOCATE - allocate a table from @alloc.file
* subject to @alloc.align alignment (must be power of 2)
* and @alloc.zone (can be HIGH or FSEG) requirements.
*
* Must appear exactly once for each file, and before
* this file is referenced by any other command.
*/
struct {
char file[BIOS_LINKER_LOADER_FILESZ];
uint32_t align;
uint8_t zone;
} alloc;
/*
* COMMAND_ADD_POINTER - patch the table (originating from
* @dest_file) at @pointer.offset, by adding a pointer to the table
* originating from @src_file. 1,2,4 or 8 byte unsigned
* addition is used depending on @pointer.size.
*/
struct {
char dest_file[BIOS_LINKER_LOADER_FILESZ];
char src_file[BIOS_LINKER_LOADER_FILESZ];
uint32_t offset;
uint8_t size;
} pointer;
/*
* COMMAND_ADD_CHECKSUM - calculate checksum of the range specified by
* @cksum_start and @cksum_length fields,
* and then add the value at @cksum.offset.
* Checksum simply sums -X for each byte X in the range
* using 8-bit math.
*/
struct {
char file[BIOS_LINKER_LOADER_FILESZ];
uint32_t offset;
uint32_t start;
uint32_t length;
} cksum;
/* padding */
char pad[124];
};
} __attribute__((packed));
typedef struct BiosLinkerLoaderEntry BiosLinkerLoaderEntry;
enum {
BIOS_LINKER_LOADER_COMMAND_ALLOCATE = 0x1,
BIOS_LINKER_LOADER_COMMAND_ADD_POINTER = 0x2,
BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM = 0x3,
};
enum {
BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1,
BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2,
};
unsigned long fw_cfg_acpi_tables(unsigned long start)
{
BiosLinkerLoaderEntry *s;
unsigned long *addrs, current;
uint32_t *ptr4;
uint64_t *ptr8;
int rc, i, j, src, dst, max;
rc = fw_cfg_check_file("etc/table-loader");
if (rc < 0)
return 0;
printk(BIOS_DEBUG, "QEMU: found ACPI tables in fw_cfg.\n");
max = rc / sizeof(*s);
s = malloc(rc);
addrs = malloc(max * sizeof(*addrs));
fw_cfg_load_file("etc/table-loader", s);
current = start;
for (i = 0; i < max && s[i].command != 0; i++) {
switch (s[i].command) {
case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
current = ALIGN(current, s[i].alloc.align);
rc = fw_cfg_check_file(s[i].alloc.file);
if (rc < 0)
goto err;
printk(BIOS_DEBUG, "QEMU: loading \"%s\" to 0x%lx (len %d)\n",
s[i].alloc.file, current, rc);
fw_cfg_load_file(s[i].alloc.file, (void*)current);
addrs[i] = current;
current += rc;
break;
case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
src = -1; dst = -1;
for (j = 0; j < i; j++) {
if (s[j].command != BIOS_LINKER_LOADER_COMMAND_ALLOCATE)
continue;
if (strcmp(s[j].alloc.file, s[i].pointer.dest_file) == 0)
dst = j;
if (strcmp(s[j].alloc.file, s[i].pointer.src_file) == 0)
src = j;
}
if (src == -1 || dst == -1)
goto err;
switch (s[i].pointer.size) {
case 4:
ptr4 = (uint32_t*)(addrs[dst] + s[i].pointer.offset);
*ptr4 += addrs[src];
break;
case 8:
ptr8 = (uint64_t*)(addrs[dst] + s[i].pointer.offset);
*ptr8 += addrs[src];
break;
default:
/*
* Should not happen. ACPI knows 1 and 2 byte ptrs
* too, but we are operating with 32bit offsets which
* would simply not fit in there ...
*/
printk(BIOS_DEBUG, "QEMU: acpi: unimplemented ptr size %d\n",
s[i].pointer.size);
goto err;
}
break;
case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
dst = -1;
for (j = 0; j < i; j++) {
if (s[j].command != BIOS_LINKER_LOADER_COMMAND_ALLOCATE)
continue;
if (strcmp(s[j].alloc.file, s[i].cksum.file) == 0)
dst = j;
}
if (dst == -1)
goto err;
ptr4 = (uint32_t*)(addrs[dst] + s[i].cksum.offset);
*ptr4 = 0;
*ptr4 = acpi_checksum((void *)(addrs[dst] + s[i].cksum.start),
s[i].cksum.length);
break;
default:
printk(BIOS_DEBUG, "QEMU: acpi: unknown script cmd 0x%x @ %p\n",
s[i].command, s+i);
goto err;
};
}
printk(BIOS_DEBUG, "QEMU: loaded ACPI tables from fw_cfg.\n");
free(s);
free(addrs);
return ALIGN(current, 16);
err:
printk(BIOS_DEBUG, "QEMU: loading ACPI tables from fw_cfg failed.\n");
free(s);
free(addrs);
return 0;
}
/* ---------------------------------------------------------------------- */
/* pick up smbios information from fw_cfg */
static const char *type1_manufacturer;
static const char *type1_product_name;
static const char *type1_version;
static const char *type1_serial_number;
static const char *type1_family;
static u8 type1_uuid[16];
static void fw_cfg_smbios_init(void)
{
static int done = 0;
uint16_t i, count = 0;
FwCfgSmbios entry;
char *buf;
if (done)
return;
done = 1;
fw_cfg_get(FW_CFG_SMBIOS_ENTRIES, &count, sizeof(count));
for (i = 0; i < count; i++) {
insb(FW_CFG_PORT_DATA, &entry, sizeof(entry));
buf = malloc(entry.length - sizeof(entry));
insb(FW_CFG_PORT_DATA, buf, entry.length - sizeof(entry));
if (entry.headertype == SMBIOS_FIELD_ENTRY &&
entry.tabletype == 1) {
switch (entry.fieldoffset) {
case offsetof(struct smbios_type1, manufacturer):
type1_manufacturer = strdup(buf);
break;
case offsetof(struct smbios_type1, product_name):
type1_product_name = strdup(buf);
break;
case offsetof(struct smbios_type1, version):
type1_version = strdup(buf);
break;
case offsetof(struct smbios_type1, serial_number):
type1_serial_number = strdup(buf);
break;
case offsetof(struct smbios_type1, family):
type1_family = strdup(buf);
break;
case offsetof(struct smbios_type1, uuid):
memcpy(type1_uuid, buf, 16);
break;
}
}
free(buf);
}
}
static unsigned long smbios_next(unsigned long current)
{
struct smbios_type0 *t0;
int l, count = 0;
char *s;
t0 = (void*)current;
current += t0->length;
for (;;) {
s = (void*)current;
l = strlen(s);
if (!l)
return current + (count ? 1 : 2);
current += l + 1;
count++;
}
}
/*
* Starting with version 2.1 qemu provides a full set of smbios tables
* for the virtual hardware emulated, except type 0 (bios information).
*
* What we are going to do here is find the type0 table, keep it, and
* override everything else generated by coreboot with the qemu smbios
* tables.
*
* It's a bit hackish, but qemu is a special case (compared to real
* hardware) and this way we don't need special qemu support in the
* generic smbios code.
*/
unsigned long fw_cfg_smbios_tables(int *handle, unsigned long *current)
{
struct smbios_type0 *t0;
unsigned long start, end;
int len, ret, i, count = 1;
char *str;
len = fw_cfg_check_file("etc/smbios/smbios-tables");
if (len < 0)
return 0;
printk(BIOS_DEBUG, "QEMU: found smbios tables in fw_cfg (len %d).\n", len);
/*
* Search backwards for "coreboot" (first string in type0 table,
* see src/arch/x86/boot/smbios.c), then find type0 table.
*/
for (i = 0; i < 16384; i++) {
str = (char*)(*current - i);
if (strcmp(str, "coreboot") == 0)
break;
}
if (i == 16384)
return 0;
i += sizeof(struct smbios_type0) - 2;
t0 = (struct smbios_type0*)(*current - i);
if (t0->type != SMBIOS_BIOS_INFORMATION || t0->handle != 0)
return 0;
printk(BIOS_DEBUG, "QEMU: coreboot type0 table found at 0x%lx.\n",
*current - i);
start = smbios_next(*current - i);
/*
* Fetch smbios tables from qemu, go find the end marker.
* We'll exclude the end marker as coreboot will add one.
*/
printk(BIOS_DEBUG, "QEMU: loading smbios tables to 0x%lx\n", start);
fw_cfg_load_file("etc/smbios/smbios-tables", (void*)start);
end = start;
do {
t0 = (struct smbios_type0*)end;
if (t0->type == SMBIOS_END_OF_TABLE)
break;
end = smbios_next(end);
count++;
} while (end < start + len);
/* final fixups. */
ret = end - *current;
*current = end;
*handle = count;
return ret;
}
const char *smbios_mainboard_manufacturer(void)
{
fw_cfg_smbios_init();
return type1_manufacturer ?: CONFIG_MAINBOARD_SMBIOS_MANUFACTURER;
}
const char *smbios_mainboard_product_name(void)
{
fw_cfg_smbios_init();
return type1_product_name ?: CONFIG_MAINBOARD_SMBIOS_PRODUCT_NAME;
}
const char *smbios_mainboard_version(void)
{
fw_cfg_smbios_init();
return type1_version ?: CONFIG_MAINBOARD_VERSION;
}
const char *smbios_mainboard_serial_number(void)
{
fw_cfg_smbios_init();
return type1_serial_number ?: CONFIG_MAINBOARD_SERIAL_NUMBER;
}
void smbios_mainboard_set_uuid(u8 *uuid)
{
fw_cfg_smbios_init();
memcpy(uuid, type1_uuid, 16);
}
|