1 // Coreboot interface support.
3 // Copyright (C) 2008,2009 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 ****************************************************************/
29 #define CB_SIGNATURE 0x4f49424C // "LBIO"
31 struct cb_memory_range {
37 #define CB_MEM_TABLE 16
42 struct cb_memory_range map[0];
45 #define CB_TAG_MEMORY 0x01
47 #define MEM_RANGE_COUNT(_rec) \
48 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
58 #define CB_TAG_MAINBOARD 0x0003
66 #define CB_TAG_FORWARD 0x11
69 ipchksum(char *buf, int count)
79 sum = (sum >> 16) + (sum & 0xffff);
84 // Try to locate the coreboot header in a given address range.
85 static struct cb_header *
86 find_cb_header(char *addr, int len)
88 char *end = addr + len;
89 for (; addr < end; addr += 16) {
90 struct cb_header *cbh = (struct cb_header *)addr;
91 if (cbh->signature != CB_SIGNATURE)
93 if (! cbh->table_bytes)
95 if (ipchksum(addr, sizeof(*cbh)) != 0)
97 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
98 != cbh->table_checksum)
105 // Try to find the coreboot memory table in the given coreboot table.
107 find_cb_subtable(struct cb_header *cbh, u32 tag)
109 char *tbl = (char *)cbh + sizeof(*cbh);
111 for (i=0; i<cbh->table_entries; i++) {
112 struct cb_memory *cbm = (struct cb_memory *)tbl;
120 static struct cb_memory *CBMemTable;
122 // Populate max ram and e820 map info by scanning for a coreboot table.
126 dprintf(3, "Attempting to find coreboot table\n");
130 // Find coreboot table.
131 struct cb_header *cbh = find_cb_header(0, 0x1000);
134 struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD);
136 dprintf(3, "Found coreboot table forwarder.\n");
137 cbh = find_cb_header((char *)((u32)cbf->forward), 0x100);
141 dprintf(3, "Now attempting to find coreboot memory map\n");
142 struct cb_memory *cbm = CBMemTable = find_cb_subtable(cbh, CB_TAG_MEMORY);
146 u64 maxram = 0, maxram_over4G = 0;
147 int i, count = MEM_RANGE_COUNT(cbm);
148 for (i=0; i<count; i++) {
149 struct cb_memory_range *m = &cbm->map[i];
151 if (type == CB_MEM_TABLE) {
152 type = E820_RESERVED;
153 } else if (type == E820_ACPI || type == E820_RAM) {
154 u64 end = m->start + m->size;
155 if (end > 0x100000000ull) {
156 end -= 0x100000000ull;
157 if (end > maxram_over4G)
159 } else if (end > maxram)
162 add_e820(m->start, m->size, type);
166 RamSizeOver4G = maxram_over4G;
168 // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
169 // confuses grub. So, override it.
170 add_e820(0, 16*1024, E820_RAM);
172 struct cb_mainboard *cbmb = find_cb_subtable(cbh, CB_TAG_MAINBOARD);
174 const char *vendor = &cbmb->strings[cbmb->vendor_idx];
175 const char *part = &cbmb->strings[cbmb->part_idx];
176 dprintf(1, "Found mainboard %s %s\n", vendor, part);
178 vgahook_setup(vendor, part);
184 // No table found.. Use 16Megs as a dummy value.
185 dprintf(1, "Unable to find coreboot table!\n");
186 RamSize = 16*1024*1024;
188 add_e820(0, 16*1024*1024, E820_RAM);
193 /****************************************************************
195 ****************************************************************/
200 struct pir_header *p = pos;
201 if (p->signature != PIR_SIGNATURE)
205 if (p->size < sizeof(*p))
207 if (checksum(pos, p->size) != 0)
209 void *newpos = malloc_fseg(p->size);
211 dprintf(1, "No room to copy PIR table!\n");
214 dprintf(1, "Copying PIR from %p to %p\n", pos, newpos);
215 memcpy(newpos, pos, p->size);
216 PirOffset = (u32)newpos - BUILD_BIOS_ADDR;
220 copy_mptable(void *pos)
222 struct mptable_floating_s *p = pos;
223 if (p->signature != MPTABLE_SIGNATURE)
227 if (checksum(pos, sizeof(*p)) != 0)
229 u32 length = p->length * 16;
230 u16 mpclength = ((struct mptable_config_s *)p->physaddr)->length;
231 struct mptable_floating_s *newpos = malloc_fseg(length + mpclength);
233 dprintf(1, "No room to copy MPTABLE!\n");
236 dprintf(1, "Copying MPTABLE from %p/%x to %p\n", pos, p->physaddr, newpos);
237 memcpy(newpos, pos, length);
238 newpos->physaddr = (u32)newpos + length;
239 newpos->checksum -= checksum(newpos, sizeof(*newpos));
240 memcpy((void*)newpos + length, (void*)p->physaddr, mpclength);
244 copy_acpi_rsdp(void *pos)
248 struct rsdp_descriptor *p = pos;
249 if (p->signature != RSDP_SIGNATURE)
252 if (checksum(pos, length) != 0)
254 if (p->revision > 1) {
256 if (checksum(pos, length) != 0)
259 void *newpos = malloc_fseg(length);
261 dprintf(1, "No room to copy ACPI RSDP table!\n");
264 dprintf(1, "Copying ACPI RSDP from %p to %p\n", pos, newpos);
265 memcpy(newpos, pos, length);
269 // Attempt to find (and relocate) any standard bios tables found in a
270 // given address range.
272 scan_tables(u32 start, u32 size)
274 void *p = (void*)ALIGN(start, 16);
275 void *end = (void*)start + size;
276 for (; p<end; p += 16) {
284 coreboot_copy_biostable()
286 struct cb_memory *cbm = CBMemTable;
287 if (! CONFIG_COREBOOT || !cbm)
290 dprintf(3, "Relocating coreboot bios tables\n");
292 // Init variables set in coreboot table memory scan.
296 // Scan CB_MEM_TABLE areas for bios tables.
297 int i, count = MEM_RANGE_COUNT(cbm);
298 for (i=0; i<count; i++) {
299 struct cb_memory_range *m = &cbm->map[i];
300 if (m->type == CB_MEM_TABLE)
301 scan_tables(m->start, m->size);
304 // XXX - just create dummy smbios table for now - should detect if
305 // smbios/dmi table is found from coreboot and use that instead.
310 /****************************************************************
312 ****************************************************************/
314 // Uncompress data in flash to an area of memory.
316 ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
318 dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
319 CLzmaDecoderState state;
320 int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE);
321 if (ret != LZMA_RESULT_OK) {
322 dprintf(1, "LzmaDecodeProperties error - %d\n", ret);
326 int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
327 if (need > sizeof(scratch)) {
328 dprintf(1, "LzmaDecode need %d have %d\n", need, sizeof(scratch));
331 state.Probs = (CProb *)scratch;
333 u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
334 if (dstlen > maxlen) {
335 dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
338 u32 inProcessed, outProcessed;
339 ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
340 , &inProcessed, dst, dstlen, &outProcessed);
342 dprintf(1, "LzmaDecode returned %d\n", ret);
349 /****************************************************************
350 * Coreboot flash format
351 ****************************************************************/
354 #define ntohl(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | \
355 (((x)&0xff0000) >> 8) | (((x)&0xff000000) >> 24))
356 #define htonl(x) ntohl(x)
358 #define CBFS_HEADER_MAGIC 0x4F524243
359 #define CBFS_HEADPTR_ADDR 0xFFFFFFFc
360 #define CBFS_VERSION1 0x31313131
372 static struct cbfs_header *CBHDR;
377 if (! CONFIG_COREBOOT_FLASH)
380 CBHDR = *(void **)CBFS_HEADPTR_ADDR;
381 if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
382 dprintf(1, "Unable to find CBFS (got %x not %x)\n"
383 , CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
388 dprintf(1, "Found CBFS header at %p\n", CBHDR);
391 #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
402 // Verify a cbfs entry looks valid.
403 static struct cbfs_file *
404 cbfs_verify(struct cbfs_file *file)
406 if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
408 u64 magic = file->magic;
409 if (magic == CBFS_FILE_MAGIC) {
410 dprintf(5, "Found CBFS file %s\n", file->filename);
416 // Return the first file in the CBFS archive
417 static struct cbfs_file *
422 return cbfs_verify((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
425 // Return the file after the given file.
426 static struct cbfs_file *
427 cbfs_getnext(struct cbfs_file *file)
429 file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
430 return cbfs_verify(file);
433 // Find the file with the given filename.
435 cbfs_findfile(const char *fname)
437 dprintf(3, "Searching CBFS for %s\n", fname);
438 struct cbfs_file *file;
439 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file))
440 if (strcmp(fname, file->filename) == 0)
446 cbfs_findprefix(const char *prefix, struct cbfs_file *last)
448 if (! CONFIG_COREBOOT_FLASH)
451 dprintf(3, "Searching CBFS for prefix %s\n", prefix);
452 int len = strlen(prefix);
453 struct cbfs_file *file;
455 file = cbfs_getfirst();
457 file = cbfs_getnext(last);
458 for (; file; file = cbfs_getnext(file))
459 if (memcmp(prefix, file->filename, len) == 0)
464 // Find a file with the given filename (possibly with ".lzma" extension).
465 static struct cbfs_file *
466 cbfs_finddatafile(const char *fname)
468 int fnlen = strlen(fname);
469 struct cbfs_file *file = NULL;
471 file = cbfs_findprefix(fname, file);
474 if (file->filename[fnlen] == '\0'
475 || strcmp(&file->filename[fnlen], ".lzma") == 0)
480 // Determine whether the file has a ".lzma" extension.
482 cbfs_iscomp(struct cbfs_file *file)
484 int fnamelen = strlen(file->filename);
485 return fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0;
488 // Return the filename of a given file.
490 cbfs_filename(struct cbfs_file *file)
492 return file->filename;
495 // Determine the uncompressed size of a datafile.
497 cbfs_datasize(struct cbfs_file *file)
499 void *src = (void*)file + ntohl(file->offset);
500 if (cbfs_iscomp(file))
501 return *(u32*)(src + LZMA_PROPERTIES_SIZE);
502 return ntohl(file->len);
505 // Copy a file to memory (uncompressing if necessary)
507 cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
509 if (! CONFIG_COREBOOT_FLASH || !file)
512 u32 size = ntohl(file->len);
513 void *src = (void*)file + ntohl(file->offset);
514 if (cbfs_iscomp(file))
516 return ulzma(dst, maxlen, src, size);
519 dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
521 dprintf(1, "File too big to copy\n");
524 memcpy(dst, src, size);
539 return ((getHex(x&0xf) << 24)
540 | (getHex((x>>4)&0xf) << 16)
541 | (getHex((x>>8)&0xf) << 8)
545 // Find and copy the optionrom for the given vendor/device id.
547 cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev)
549 if (! CONFIG_COREBOOT_FLASH)
553 // Ughh - poor man's sprintf of "pci%04x,%04x.rom"
554 *(u32*)fname = 0x20696370; // "pci "
555 *(u32*)&fname[3] = hexify4(vendev);
557 *(u32*)&fname[8] = hexify4(vendev >> 16);
558 *(u32*)&fname[12] = 0x6d6f722e; // ".rom"
561 return cbfs_copyfile(cbfs_finddatafile(fname), dst, maxlen);
564 struct cbfs_payload_segment {
573 #define PAYLOAD_SEGMENT_BSS 0x20535342
574 #define PAYLOAD_SEGMENT_ENTRY 0x52544E45
576 #define CBFS_COMPRESS_NONE 0
577 #define CBFS_COMPRESS_LZMA 1
579 struct cbfs_payload {
580 struct cbfs_payload_segment segments[1];
584 cbfs_run_payload(struct cbfs_file *file)
586 if (!CONFIG_COREBOOT_FLASH || !file)
588 dprintf(1, "Run %s\n", file->filename);
589 struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
590 struct cbfs_payload_segment *seg = pay->segments;
592 void *src = (void*)pay + ntohl(seg->offset);
593 void *dest = (void*)ntohl((u32)seg->load_addr);
594 u32 src_len = ntohl(seg->len);
595 u32 dest_len = ntohl(seg->mem_len);
597 case PAYLOAD_SEGMENT_BSS:
598 dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
599 memset(dest, 0, dest_len);
601 case PAYLOAD_SEGMENT_ENTRY: {
602 dprintf(1, "Calling addr %p\n", dest);
603 void (*func)() = dest;
608 dprintf(3, "Segment %x %d@%p -> %d@%p\n"
609 , seg->type, src_len, src, dest_len, dest);
610 if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
611 if (src_len > dest_len)
613 memcpy(dest, src, src_len);
614 } else if (CONFIG_LZMA
615 && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
616 int ret = ulzma(dest, dest_len, src, src_len);
621 dprintf(1, "No support for compression type %x\n"
625 if (dest_len > src_len)
626 memset(dest + src_len, 0, dest_len - src_len);