| 1 |
|
| 2 |
|
| 3 |
#include <endian.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
#include <smbios.h> |
| 7 |
#include <console/console.h> |
| 8 |
#include <arch/io.h> |
| 9 |
#include <acpi/acpi.h> |
| 10 |
#include <commonlib/endian.h> |
| 11 |
#include <device/fw_cfg.h> |
| 12 |
#include <device/fw_cfg_if.h> |
| 13 |
|
| 14 |
#define FW_CFG_PORT_CTL 0x0510 |
| 15 |
#define FW_CFG_PORT_DATA 0x0511 |
| 16 |
#define FW_CFG_DMA_ADDR_HIGH 0x0514 |
| 17 |
#define FW_CFG_DMA_ADDR_LOW 0x0518 |
| 18 |
|
| 19 |
static int fw_cfg_detected; |
| 20 |
static uint8_t fw_ver; |
| 21 |
|
| 22 |
static void fw_cfg_dma(int control, void *buf, int len); |
| 23 |
|
| 24 |
static int fw_cfg_present(void) |
| 25 |
{ |
| 26 |
static const char qsig[] = "QEMU"; |
| 27 |
unsigned char sig[FW_CFG_SIG_SIZE]; |
| 28 |
int detected = 0; |
| 29 |
|
| 30 |
if (fw_cfg_detected == 0) { |
| 31 |
fw_cfg_get(FW_CFG_SIGNATURE, sig, sizeof(sig)); |
| 32 |
detected = memcmp(sig, qsig, FW_CFG_SIG_SIZE) == 0; |
| 33 |
printk(BIOS_INFO, "QEMU: firmware config interface %s\n", |
| 34 |
detected ? "detected" : "not found"); |
| 35 |
if (detected) { |
| 36 |
fw_cfg_get(FW_CFG_ID, &fw_ver, sizeof(fw_ver)); |
| 37 |
printk(BIOS_INFO, "Firmware config version id: %d\n", fw_ver); |
| 38 |
} |
| 39 |
fw_cfg_detected = detected + 1; |
| 40 |
} |
| 41 |
return fw_cfg_detected - 1; |
| 42 |
} |
| 43 |
|
| 44 |
static void fw_cfg_select(uint16_t entry) |
| 45 |
{ |
| 46 |
if (fw_ver & FW_CFG_VERSION_DMA) |
| 47 |
fw_cfg_dma(FW_CFG_DMA_CTL_SELECT | entry << 16, NULL, 0); |
| 48 |
else |
| 49 |
outw(entry, FW_CFG_PORT_CTL); |
| 50 |
} |
| 51 |
|
| 52 |
static void fw_cfg_read(void *dst, int dstlen) |
| 53 |
{ |
| 54 |
if (fw_ver & FW_CFG_VERSION_DMA) |
| 55 |
fw_cfg_dma(FW_CFG_DMA_CTL_READ, dst, dstlen); |
| 56 |
else |
| 57 |
insb(FW_CFG_PORT_DATA, dst, dstlen); |
| 58 |
} |
| 59 |
|
| 60 |
void fw_cfg_get(uint16_t entry, void *dst, int dstlen) |
| 61 |
{ |
| 62 |
fw_cfg_select(entry); |
| 63 |
fw_cfg_read(dst, dstlen); |
| 64 |
} |
| 65 |
|
| 66 |
static int fw_cfg_find_file(FWCfgFile *file, const char *name) |
| 67 |
{ |
| 68 |
uint32_t count = 0; |
| 69 |
|
| 70 |
fw_cfg_select(FW_CFG_FILE_DIR); |
| 71 |
fw_cfg_read(&count, sizeof(count)); |
| 72 |
count = be32_to_cpu(count); |
| 73 |
|
| 74 |
for (int i = 0; i < count; i++) { |
| 75 |
fw_cfg_read(file, sizeof(*file)); |
| 76 |
if (strcmp(file->name, name) == 0) { |
| 77 |
file->size = be32_to_cpu(file->size); |
| 78 |
file->select = be16_to_cpu(file->select); |
| 79 |
printk(BIOS_INFO, "QEMU: firmware config: Found '%s'\n", name); |
| 80 |
return 0; |
| 81 |
} |
| 82 |
} |
| 83 |
printk(BIOS_INFO, "QEMU: firmware config: Couldn't find '%s'\n", name); |
| 84 |
return -1; |
| 85 |
} |
| 86 |
|
| 87 |
int fw_cfg_check_file(FWCfgFile *file, const char *name) |
| 88 |
{ |
| 89 |
if (!fw_cfg_present()) |
| 90 |
return -1; |
| 91 |
return fw_cfg_find_file(file, name); |
| 92 |
} |
| 93 |
|
| 94 |
static int fw_cfg_e820_select(uint32_t *size) |
| 95 |
{ |
| 96 |
FWCfgFile file; |
| 97 |
|
| 98 |
if (!fw_cfg_present() || fw_cfg_find_file(&file, "etc/e820")) |
| 99 |
return -1; |
| 100 |
fw_cfg_select(file.select); |
| 101 |
*size = file.size; |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
static int fw_cfg_e820_read(FwCfgE820Entry *entry, uint32_t *size, |
| 106 |
uint32_t *pos) |
| 107 |
{ |
| 108 |
if (*pos + sizeof(*entry) > *size) |
| 109 |
return -1; |
| 110 |
|
| 111 |
fw_cfg_read(entry, sizeof(*entry)); |
| 112 |
*pos += sizeof(*entry); |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
uintptr_t fw_cfg_tolud(void) |
| 118 |
{ |
| 119 |
FwCfgE820Entry e; |
| 120 |
uint64_t top = 0; |
| 121 |
uint32_t size = 0, pos = 0; |
| 122 |
|
| 123 |
if (fw_cfg_e820_select(&size) == 0) { |
| 124 |
while (!fw_cfg_e820_read(&e, &size, &pos)) { |
| 125 |
uint64_t limit = e.address + e.length; |
| 126 |
if (e.type == 1 && limit < 4ULL * GiB && limit > top) |
| 127 |
top = limit; |
| 128 |
} |
| 129 |
} |
| 130 |
return (uintptr_t)top; |
| 131 |
} |
| 132 |
|
| 133 |
int fw_cfg_max_cpus(void) |
| 134 |
{ |
| 135 |
unsigned short max_cpus; |
| 136 |
|
| 137 |
if (!fw_cfg_present()) |
| 138 |
return 0; |
| 139 |
|
| 140 |
fw_cfg_get(FW_CFG_MAX_CPUS, &max_cpus, sizeof(max_cpus)); |
| 141 |
return max_cpus; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
#define BIOS_LINKER_LOADER_FILESZ 56 |
| 166 |
|
| 167 |
struct BiosLinkerLoaderEntry { |
| 168 |
uint32_t command; |
| 169 |
union { |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
struct { |
| 179 |
char file[BIOS_LINKER_LOADER_FILESZ]; |
| 180 |
uint32_t align; |
| 181 |
uint8_t zone; |
| 182 |
} alloc; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
struct { |
| 191 |
char dest_file[BIOS_LINKER_LOADER_FILESZ]; |
| 192 |
char src_file[BIOS_LINKER_LOADER_FILESZ]; |
| 193 |
uint32_t offset; |
| 194 |
uint8_t size; |
| 195 |
} pointer; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
struct { |
| 205 |
char file[BIOS_LINKER_LOADER_FILESZ]; |
| 206 |
uint32_t offset; |
| 207 |
uint32_t start; |
| 208 |
uint32_t length; |
| 209 |
} cksum; |
| 210 |
|
| 211 |
|
| 212 |
char pad[124]; |
| 213 |
}; |
| 214 |
} __packed; |
| 215 |
typedef struct BiosLinkerLoaderEntry BiosLinkerLoaderEntry; |
| 216 |
|
| 217 |
enum { |
| 218 |
BIOS_LINKER_LOADER_COMMAND_ALLOCATE = 0x1, |
| 219 |
BIOS_LINKER_LOADER_COMMAND_ADD_POINTER = 0x2, |
| 220 |
BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM = 0x3, |
| 221 |
}; |
| 222 |
|
| 223 |
enum { |
| 224 |
BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1, |
| 225 |
BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2, |
| 226 |
}; |
| 227 |
|
| 228 |
unsigned long fw_cfg_acpi_tables(unsigned long start) |
| 229 |
{ |
| 230 |
FWCfgFile f; |
| 231 |
BiosLinkerLoaderEntry *s; |
| 232 |
unsigned long *addrs, current; |
| 233 |
uint8_t *ptr; |
| 234 |
int i, j, src, dst, max; |
| 235 |
|
| 236 |
if (fw_cfg_check_file(&f, "etc/table-loader")) |
| 237 |
return 0; |
| 238 |
|
| 239 |
printk(BIOS_DEBUG, "QEMU: found ACPI tables in fw_cfg.\n"); |
| 240 |
|
| 241 |
max = f.size / sizeof(*s); |
| 242 |
s = malloc(f.size); |
| 243 |
addrs = malloc(max * sizeof(*addrs)); |
| 244 |
fw_cfg_get(f.select, s, f.size); |
| 245 |
|
| 246 |
current = start; |
| 247 |
for (i = 0; i < max && s[i].command != 0; i++) { |
| 248 |
void *cksum_data; |
| 249 |
uint32_t cksum; |
| 250 |
uint32_t addr4; |
| 251 |
uint64_t addr8; |
| 252 |
switch (s[i].command) { |
| 253 |
case BIOS_LINKER_LOADER_COMMAND_ALLOCATE: |
| 254 |
current = ALIGN_UP(current, s[i].alloc.align); |
| 255 |
if (fw_cfg_check_file(&f, s[i].alloc.file)) |
| 256 |
goto err; |
| 257 |
|
| 258 |
printk(BIOS_DEBUG, "QEMU: loading \"%s\" to 0x%lx (len %d)\n", |
| 259 |
s[i].alloc.file, current, f.size); |
| 260 |
fw_cfg_get(f.select, (void *)current, f.size); |
| 261 |
addrs[i] = current; |
| 262 |
current += f.size; |
| 263 |
break; |
| 264 |
|
| 265 |
case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER: |
| 266 |
src = -1; dst = -1; |
| 267 |
for (j = 0; j < i; j++) { |
| 268 |
if (s[j].command != BIOS_LINKER_LOADER_COMMAND_ALLOCATE) |
| 269 |
continue; |
| 270 |
if (strcmp(s[j].alloc.file, s[i].pointer.dest_file) == 0) |
| 271 |
dst = j; |
| 272 |
if (strcmp(s[j].alloc.file, s[i].pointer.src_file) == 0) |
| 273 |
src = j; |
| 274 |
} |
| 275 |
if (src == -1 || dst == -1) |
| 276 |
goto err; |
| 277 |
|
| 278 |
switch (s[i].pointer.size) { |
| 279 |
case 4: |
| 280 |
ptr = (uint8_t *)addrs[dst]; |
| 281 |
ptr += s[i].pointer.offset; |
| 282 |
addr4 = read_le32(ptr); |
| 283 |
addr4 += addrs[src]; |
| 284 |
write_le32(ptr, addr4); |
| 285 |
break; |
| 286 |
|
| 287 |
case 8: |
| 288 |
ptr = (uint8_t *)addrs[dst]; |
| 289 |
ptr += s[i].pointer.offset; |
| 290 |
addr8 = read_le64(ptr); |
| 291 |
addr8 += addrs[src]; |
| 292 |
write_le64(ptr, addr8); |
| 293 |
break; |
| 294 |
|
| 295 |
default: |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
printk(BIOS_DEBUG, "QEMU: acpi: unimplemented ptr size %d\n", |
| 302 |
s[i].pointer.size); |
| 303 |
goto err; |
| 304 |
} |
| 305 |
break; |
| 306 |
|
| 307 |
case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM: |
| 308 |
dst = -1; |
| 309 |
for (j = 0; j < i; j++) { |
| 310 |
if (s[j].command != BIOS_LINKER_LOADER_COMMAND_ALLOCATE) |
| 311 |
continue; |
| 312 |
if (strcmp(s[j].alloc.file, s[i].cksum.file) == 0) |
| 313 |
dst = j; |
| 314 |
} |
| 315 |
if (dst == -1) |
| 316 |
goto err; |
| 317 |
|
| 318 |
ptr = (uint8_t *)(addrs[dst] + s[i].cksum.offset); |
| 319 |
cksum_data = (void *)(addrs[dst] + s[i].cksum.start); |
| 320 |
cksum = acpi_checksum(cksum_data, s[i].cksum.length); |
| 321 |
write_le8(ptr, cksum); |
| 322 |
break; |
| 323 |
|
| 324 |
default: |
| 325 |
printk(BIOS_DEBUG, "QEMU: acpi: unknown script cmd 0x%x @ %p\n", |
| 326 |
s[i].command, s+i); |
| 327 |
goto err; |
| 328 |
}; |
| 329 |
} |
| 330 |
|
| 331 |
printk(BIOS_DEBUG, "QEMU: loaded ACPI tables from fw_cfg.\n"); |
| 332 |
free(s); |
| 333 |
free(addrs); |
| 334 |
return ALIGN_UP(current, 16); |
| 335 |
|
| 336 |
err: |
| 337 |
printk(BIOS_DEBUG, "QEMU: loading ACPI tables from fw_cfg failed.\n"); |
| 338 |
free(s); |
| 339 |
free(addrs); |
| 340 |
return 0; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
#if CONFIG(GENERATE_SMBIOS_TABLES) |
| 347 |
static const char *type1_manufacturer; |
| 348 |
static const char *type1_product_name; |
| 349 |
static const char *type1_version; |
| 350 |
static const char *type1_serial_number; |
| 351 |
static const char *type1_family; |
| 352 |
static u8 type1_uuid[16]; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
static void fw_cfg_smbios_init(void) |
| 361 |
{ |
| 362 |
static int done = 0; |
| 363 |
uint16_t i, count = 0; |
| 364 |
FwCfgSmbios entry; |
| 365 |
char *buf; |
| 366 |
|
| 367 |
if (done) |
| 368 |
return; |
| 369 |
done = 1; |
| 370 |
|
| 371 |
fw_cfg_get(FW_CFG_SMBIOS_ENTRIES, &count, sizeof(count)); |
| 372 |
for (i = 0; i < count; i++) { |
| 373 |
fw_cfg_read(&entry, sizeof(entry)); |
| 374 |
buf = malloc(entry.length - sizeof(entry)); |
| 375 |
fw_cfg_read(buf, entry.length - sizeof(entry)); |
| 376 |
if (entry.headertype == SMBIOS_FIELD_ENTRY && |
| 377 |
entry.tabletype == 1) { |
| 378 |
switch (entry.fieldoffset) { |
| 379 |
case offsetof(struct smbios_type1, manufacturer): |
| 380 |
type1_manufacturer = strdup(buf); |
| 381 |
break; |
| 382 |
case offsetof(struct smbios_type1, product_name): |
| 383 |
type1_product_name = strdup(buf); |
| 384 |
break; |
| 385 |
case offsetof(struct smbios_type1, version): |
| 386 |
type1_version = strdup(buf); |
| 387 |
break; |
| 388 |
case offsetof(struct smbios_type1, serial_number): |
| 389 |
type1_serial_number = strdup(buf); |
| 390 |
break; |
| 391 |
case offsetof(struct smbios_type1, family): |
| 392 |
type1_family = strdup(buf); |
| 393 |
break; |
| 394 |
case offsetof(struct smbios_type1, uuid): |
| 395 |
memcpy(type1_uuid, buf, 16); |
| 396 |
break; |
| 397 |
} |
| 398 |
} |
| 399 |
free(buf); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
static unsigned long smbios_next(unsigned long current) |
| 404 |
{ |
| 405 |
struct smbios_header *header; |
| 406 |
int l, count = 0; |
| 407 |
char *s; |
| 408 |
|
| 409 |
header = (void *)current; |
| 410 |
current += header->length; |
| 411 |
for (;;) { |
| 412 |
s = (void *)current; |
| 413 |
l = strlen(s); |
| 414 |
if (!l) |
| 415 |
return current + (count ? 1 : 2); |
| 416 |
current += l + 1; |
| 417 |
count++; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
unsigned long fw_cfg_smbios_tables(int *handle, unsigned long *current) |
| 434 |
{ |
| 435 |
FWCfgFile f; |
| 436 |
struct smbios_header *header; |
| 437 |
unsigned long start, end; |
| 438 |
int ret, i, count = 1; |
| 439 |
char *str; |
| 440 |
|
| 441 |
if (fw_cfg_check_file(&f, "etc/smbios/smbios-tables")) |
| 442 |
return 0; |
| 443 |
|
| 444 |
printk(BIOS_DEBUG, "QEMU: found smbios tables in fw_cfg (len %d).\n", f.size); |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
for (i = 0; i < 16384; i++) { |
| 451 |
str = (char*)(*current - i); |
| 452 |
if (strcmp(str, "coreboot") == 0) |
| 453 |
break; |
| 454 |
} |
| 455 |
if (i == 16384) |
| 456 |
return 0; |
| 457 |
i += sizeof(struct smbios_type0) - 2; |
| 458 |
header = (struct smbios_header *)(*current - i); |
| 459 |
if (header->type != SMBIOS_BIOS_INFORMATION || header->handle != 0) |
| 460 |
return 0; |
| 461 |
printk(BIOS_DEBUG, "QEMU: coreboot type0 table found at 0x%lx.\n", |
| 462 |
*current - i); |
| 463 |
start = smbios_next(*current - i); |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
printk(BIOS_DEBUG, "QEMU: loading smbios tables to 0x%lx\n", start); |
| 470 |
fw_cfg_get(f.select, (void *)start, f.size); |
| 471 |
end = start; |
| 472 |
do { |
| 473 |
header = (struct smbios_header *)end; |
| 474 |
if (header->type == SMBIOS_END_OF_TABLE) |
| 475 |
break; |
| 476 |
end = smbios_next(end); |
| 477 |
count++; |
| 478 |
} while (end < start + f.size); |
| 479 |
|
| 480 |
|
| 481 |
ret = end - *current; |
| 482 |
*current = end; |
| 483 |
*handle = count; |
| 484 |
return ret; |
| 485 |
} |
| 486 |
|
| 487 |
const char *smbios_mainboard_manufacturer(void) |
| 488 |
{ |
| 489 |
fw_cfg_smbios_init(); |
| 490 |
return type1_manufacturer ?: CONFIG_MAINBOARD_SMBIOS_MANUFACTURER; |
| 491 |
} |
| 492 |
|
| 493 |
const char *smbios_mainboard_product_name(void) |
| 494 |
{ |
| 495 |
fw_cfg_smbios_init(); |
| 496 |
return type1_product_name ?: CONFIG_MAINBOARD_SMBIOS_PRODUCT_NAME; |
| 497 |
} |
| 498 |
|
| 499 |
const char *smbios_mainboard_version(void) |
| 500 |
{ |
| 501 |
fw_cfg_smbios_init(); |
| 502 |
return type1_version ?: CONFIG_MAINBOARD_VERSION; |
| 503 |
} |
| 504 |
|
| 505 |
const char *smbios_mainboard_serial_number(void) |
| 506 |
{ |
| 507 |
fw_cfg_smbios_init(); |
| 508 |
return type1_serial_number ?: CONFIG_MAINBOARD_SERIAL_NUMBER; |
| 509 |
} |
| 510 |
|
| 511 |
void smbios_system_set_uuid(u8 *uuid) |
| 512 |
{ |
| 513 |
fw_cfg_smbios_init(); |
| 514 |
memcpy(uuid, type1_uuid, 16); |
| 515 |
} |
| 516 |
#endif |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
static void fw_cfg_dma(int control, void *buf, int len) |
| 523 |
{ |
| 524 |
volatile FwCfgDmaAccess dma; |
| 525 |
uintptr_t dma_desc_addr; |
| 526 |
uint32_t dma_desc_addr_hi, dma_desc_addr_lo; |
| 527 |
|
| 528 |
dma.control = be32_to_cpu(control); |
| 529 |
dma.length = be32_to_cpu(len); |
| 530 |
dma.address = be64_to_cpu((uintptr_t)buf); |
| 531 |
|
| 532 |
dma_desc_addr = (uintptr_t)&dma; |
| 533 |
dma_desc_addr_lo = (uint32_t)(dma_desc_addr & 0xFFFFFFFFU); |
| 534 |
dma_desc_addr_hi = sizeof(uintptr_t) > sizeof(uint32_t) |
| 535 |
? (uint32_t)(dma_desc_addr >> 32) : 0; |
| 536 |
|
| 537 |
|
| 538 |
if (dma_desc_addr_hi) |
| 539 |
outl(be32_to_cpu(dma_desc_addr_hi), FW_CFG_DMA_ADDR_HIGH); |
| 540 |
outl(be32_to_cpu(dma_desc_addr_lo), FW_CFG_DMA_ADDR_LOW); |
| 541 |
|
| 542 |
while (be32_to_cpu(dma.control) & ~FW_CFG_DMA_CTL_ERROR) |
| 543 |
; |
| 544 |
} |