1 // Coreboot interface support.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "memmap.h" // add_e820
8 #include "util.h" // dprintf
9 #include "pci.h" // struct pir_header
10 #include "acpi.h" // struct rsdp_descriptor
11 #include "mptable.h" // MPTABLE_SIGNATURE
12 #include "biosvar.h" // GET_EBDA
13 #include "lzmadecode.h" // LzmaDecode
16 /****************************************************************
18 ****************************************************************/
23 struct pir_header *p = pos;
24 if (p->signature != PIR_SIGNATURE)
28 if (p->size < sizeof(*p))
30 if (checksum(pos, p->size) != 0)
32 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
33 if (bios_table_cur_addr + p->size > bios_table_end_addr) {
34 dprintf(1, "No room to copy PIR table!\n");
37 dprintf(1, "Copying PIR from %p to %x\n", pos, bios_table_cur_addr);
38 memcpy((void*)bios_table_cur_addr, pos, p->size);
39 PirOffset = bios_table_cur_addr - BUILD_BIOS_ADDR;
40 bios_table_cur_addr += p->size;
44 copy_mptable(void *pos)
46 struct mptable_floating_s *p = pos;
47 if (p->signature != MPTABLE_SIGNATURE)
51 if (checksum(pos, sizeof(*p)) != 0)
53 u32 length = p->length * 16;
54 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
55 u16 mpclength = ((struct mptable_config_s *)p->physaddr)->length;
56 if (bios_table_cur_addr + length + mpclength > bios_table_end_addr) {
57 dprintf(1, "No room to copy MPTABLE!\n");
60 dprintf(1, "Copying MPTABLE from %p/%x to %x\n"
61 , pos, p->physaddr, bios_table_cur_addr);
62 memcpy((void*)bios_table_cur_addr, pos, length);
63 struct mptable_floating_s *newp = (void*)bios_table_cur_addr;
64 newp->physaddr = bios_table_cur_addr + length;
66 newp->checksum = -checksum(newp, sizeof(*newp));
67 memcpy((void*)bios_table_cur_addr + length, (void*)p->physaddr, mpclength);
68 bios_table_cur_addr += length + mpclength;
72 copy_acpi_rsdp(void *pos)
76 struct rsdp_descriptor *p = pos;
77 if (p->signature != RSDP_SIGNATURE)
80 if (checksum(pos, length) != 0)
82 if (p->revision > 1) {
84 if (checksum(pos, length) != 0)
87 bios_table_cur_addr = ALIGN(bios_table_cur_addr, 16);
88 if (bios_table_cur_addr + length > bios_table_end_addr) {
89 dprintf(1, "No room to copy ACPI RSDP table!\n");
92 dprintf(1, "Copying ACPI RSDP from %p to %x\n", pos, bios_table_cur_addr);
93 RsdpAddr = (void*)bios_table_cur_addr;
94 memcpy(RsdpAddr, pos, length);
95 bios_table_cur_addr += length;
98 // Attempt to find (and relocate) any standard bios tables found in a
99 // given address range.
101 scan_tables(u32 start, u32 size)
103 void *p = (void*)ALIGN(start, 16);
104 void *end = (void*)start + size;
105 for (; p<end; p += 16) {
113 /****************************************************************
115 ****************************************************************/
126 #define CB_SIGNATURE 0x4f49424C // "LBIO"
128 struct cb_memory_range {
134 #define CB_MEM_TABLE 16
139 struct cb_memory_range map[0];
142 #define CB_TAG_MEMORY 0x01
144 #define MEM_RANGE_COUNT(_rec) \
145 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
153 #define CB_TAG_FORWARD 0x11
156 ipchksum(char *buf, int count)
166 sum = (sum >> 16) + (sum & 0xffff);
171 // Try to locate the coreboot header in a given address range.
172 static struct cb_header *
173 find_cb_header(char *addr, int len)
175 char *end = addr + len;
176 for (; addr < end; addr += 16) {
177 struct cb_header *cbh = (struct cb_header *)addr;
178 if (cbh->signature != CB_SIGNATURE)
180 if (! cbh->table_bytes)
182 if (ipchksum(addr, sizeof(*cbh)) != 0)
184 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
185 != cbh->table_checksum)
192 // Try to find the coreboot memory table in the given coreboot table.
194 find_cb_subtable(struct cb_header *cbh, u32 tag)
196 char *tbl = (char *)cbh + sizeof(*cbh);
198 for (i=0; i<cbh->table_entries; i++) {
199 struct cb_memory *cbm = (struct cb_memory *)tbl;
207 // Populate max ram and e820 map info by scanning for a coreboot table.
211 dprintf(3, "Attempting to find coreboot table\n");
213 // Init variables set in coreboot table memory scan.
217 // Find coreboot table.
218 struct cb_header *cbh = find_cb_header(0, 0x1000);
221 struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD);
223 dprintf(3, "Found coreboot table forwarder.\n");
224 cbh = find_cb_header((char *)((u32)cbf->forward), 0x100);
228 dprintf(3, "Now attempting to find coreboot memory map\n");
229 struct cb_memory *cbm = find_cb_subtable(cbh, CB_TAG_MEMORY);
233 u64 maxram = 0, maxram_over4G = 0;
234 int i, count = MEM_RANGE_COUNT(cbm);
235 for (i=0; i<count; i++) {
236 struct cb_memory_range *m = &cbm->map[i];
238 if (type == CB_MEM_TABLE) {
239 type = E820_RESERVED;
240 scan_tables(m->start, m->size);
241 } else if (type == E820_ACPI || type == E820_RAM) {
242 u64 end = m->start + m->size;
243 if (end > 0x100000000ull) {
244 end -= 0x100000000ull;
245 if (end > maxram_over4G)
247 } else if (end > maxram)
250 add_e820(m->start, m->size, type);
254 RamSizeOver4G = maxram_over4G;
256 // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
257 // confuses grub. So, override it.
258 add_e820(0, 16*1024, E820_RAM);
260 // XXX - just create dummy smbios table for now - should detect if
261 // smbios/dmi table is found from coreboot and use that instead.
267 // No table found.. Use 16Megs as a dummy value.
268 dprintf(1, "Unable to find coreboot table!\n");
269 RamSize = 16*1024*1024;
271 add_e820(0, 16*1024*1024, E820_RAM);
276 /****************************************************************
278 ****************************************************************/
281 ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
283 CLzmaDecoderState state;
284 int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE);
285 if (ret != LZMA_RESULT_OK) {
286 dprintf(1, "LzmaDecodeProperties error - %d\n", ret);
290 int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
291 if (need > sizeof(scratch)) {
292 dprintf(1, "LzmaDecode need %d have %d\n", need, sizeof(scratch));
295 state.Probs = (CProb *)scratch;
297 u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
298 if (dstlen > maxlen) {
299 dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
302 u32 inProcessed, outProcessed;
303 ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
304 , &inProcessed, dst, dstlen, &outProcessed);
306 dprintf(1, "LzmaDecode returned %d\n", ret);
313 /****************************************************************
314 * Coreboot flash format
315 ****************************************************************/
318 #define ntohl(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | \
319 (((x)&0xff0000) >> 8) | (((x)&0xff000000) >> 24))
320 #define htonl(x) ntohl(x)
322 #define CBFS_HEADER_MAGIC 0x4F524243
323 #define CBFS_HEADPTR_ADDR 0xFFFFFFFc
324 #define CBFS_VERSION1 0x31313131
336 static struct cbfs_header *CBHDR;
341 if (! CONFIG_COREBOOT_FLASH)
344 CBHDR = *(void **)CBFS_HEADPTR_ADDR;
345 if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
346 dprintf(1, "Unable to find CBFS (got %x not %x)\n"
347 , CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
352 dprintf(1, "Found CBFS header at %p\n", CBHDR);
355 #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
366 static struct cbfs_file *
367 cbfs_search(struct cbfs_file *file)
370 if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
372 u64 magic = file->magic;
373 if (magic == CBFS_FILE_MAGIC) {
374 dprintf(5, "Found CBFS file %s\n", file->filename);
379 file = (void*)file + ntohl(CBHDR->align);
383 static struct cbfs_file *
388 return cbfs_search((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
391 static struct cbfs_file *
392 cbfs_getnext(struct cbfs_file *file)
394 file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
395 return cbfs_search(file);
398 static struct cbfs_file *
399 cbfs_findfile(const char *fname)
401 dprintf(3, "Searching CBFS for %s\n", fname);
402 struct cbfs_file *file;
403 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
404 dprintf(3, "Found CBFS file %s\n", file->filename);
405 if (strcmp(fname, file->filename) == 0)
411 // Copy a file to memory (uncompressing if necessary)
413 cbfs_copyfile(void *dst, u32 maxlen, const char *fname)
415 dprintf(3, "Searching CBFS for data file %s\n", fname);
416 int fnlen = strlen(fname);
417 struct cbfs_file *file;
418 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
419 if (memcmp(fname, file->filename, fnlen) != 0)
421 u32 size = ntohl(file->len);
422 void *src = (void*)file + ntohl(file->offset);
423 if (file->filename[fnlen] == '\0') {
426 dprintf(1, "File too big to copy\n");
429 dprintf(3, "Copying data file %s\n", file->filename);
430 memcpy(dst, src, size);
433 if (strcmp(&file->filename[fnlen], ".lzma") == 0) {
434 // lzma compressed file
435 dprintf(3, "Uncompressing data file %s @ %p\n"
436 , file->filename, src);
437 return ulzma(dst, maxlen, src, size);
444 cbfs_findNprefix(const char *prefix, int n)
446 if (! CONFIG_COREBOOT_FLASH)
449 dprintf(3, "Searching CBFS for prefix %s\n", prefix);
450 int len = strlen(prefix);
451 struct cbfs_file *file;
452 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file)) {
453 if (memcmp(prefix, file->filename, len) == 0) {
455 return file->filename;
473 return ((getHex(x&0xf) << 24)
474 | (getHex((x>>4)&0xf) << 16)
475 | (getHex((x>>8)&0xf) << 8)
480 cb_copy_optionrom(void *dst, u32 maxlen, u32 vendev)
482 if (! CONFIG_COREBOOT_FLASH)
486 // Ughh - poor man's sprintf of "pci%04x,%04x.rom"
487 *(u32*)fname = 0x20696370; // "pci "
488 *(u32*)&fname[3] = hexify4(vendev);
490 *(u32*)&fname[8] = hexify4(vendev >> 16);
491 *(u32*)&fname[12] = 0x6d6f722e; // ".rom"
494 return cbfs_copyfile(dst, maxlen, fname);
497 struct cbfs_payload_segment {
506 #define PAYLOAD_SEGMENT_BSS 0x20535342
507 #define PAYLOAD_SEGMENT_ENTRY 0x52544E45
509 #define CBFS_COMPRESS_NONE 0
510 #define CBFS_COMPRESS_LZMA 1
512 struct cbfs_payload {
513 struct cbfs_payload_segment segments[1];
517 cbfs_run_payload(const char *filename)
519 if (! CONFIG_COREBOOT_FLASH)
521 dprintf(1, "Run %s\n", filename);
522 struct cbfs_file *file = cbfs_findfile(filename);
525 struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
526 struct cbfs_payload_segment *seg = pay->segments;
528 void *src = (void*)pay + ntohl(seg->offset);
529 void *dest = (void*)ntohl((u32)seg->load_addr);
530 u32 src_len = ntohl(seg->len);
531 u32 dest_len = ntohl(seg->mem_len);
533 case PAYLOAD_SEGMENT_BSS:
534 dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
535 memset(dest, 0, dest_len);
537 case PAYLOAD_SEGMENT_ENTRY: {
538 dprintf(1, "Calling addr %p\n", dest);
539 void (*func)() = dest;
544 dprintf(3, "Segment %x %d@%p -> %d@%p\n"
545 , seg->type, src_len, src, dest_len, dest);
546 if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
547 if (src_len > dest_len)
549 memcpy(dest, src, src_len);
550 } else if (CONFIG_LZMA
551 && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
552 int ret = ulzma(dest, dest_len, src, src_len);
557 dprintf(1, "No support for compression type %x\n"
561 if (dest_len > src_len)
562 memset(dest + src_len, 0, dest_len - src_len);