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
14 #include "smbios.h" // smbios_init
17 /****************************************************************
19 ****************************************************************/
30 #define CB_SIGNATURE 0x4f49424C // "LBIO"
32 struct cb_memory_range {
38 #define CB_MEM_TABLE 16
43 struct cb_memory_range map[0];
46 #define CB_TAG_MEMORY 0x01
48 #define MEM_RANGE_COUNT(_rec) \
49 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
59 #define CB_TAG_MAINBOARD 0x0003
67 #define CB_TAG_FORWARD 0x11
70 ipchksum(char *buf, int count)
80 sum = (sum >> 16) + (sum & 0xffff);
85 // Try to locate the coreboot header in a given address range.
86 static struct cb_header *
87 find_cb_header(char *addr, int len)
89 char *end = addr + len;
90 for (; addr < end; addr += 16) {
91 struct cb_header *cbh = (struct cb_header *)addr;
92 if (cbh->signature != CB_SIGNATURE)
94 if (! cbh->table_bytes)
96 if (ipchksum(addr, sizeof(*cbh)) != 0)
98 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
99 != cbh->table_checksum)
106 // Try to find the coreboot memory table in the given coreboot table.
108 find_cb_subtable(struct cb_header *cbh, u32 tag)
110 char *tbl = (char *)cbh + sizeof(*cbh);
112 for (i=0; i<cbh->table_entries; i++) {
113 struct cb_memory *cbm = (struct cb_memory *)tbl;
121 static struct cb_memory *CBMemTable;
123 // Populate max ram and e820 map info by scanning for a coreboot table.
125 coreboot_fill_map(void)
127 dprintf(3, "Attempting to find coreboot table\n");
131 // Find coreboot table.
132 struct cb_header *cbh = find_cb_header(0, 0x1000);
135 struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD);
137 dprintf(3, "Found coreboot table forwarder.\n");
138 cbh = find_cb_header((char *)((u32)cbf->forward), 0x100);
142 dprintf(3, "Now attempting to find coreboot memory map\n");
143 struct cb_memory *cbm = CBMemTable = find_cb_subtable(cbh, CB_TAG_MEMORY);
147 u64 maxram = 0, maxram_over4G = 0;
148 int i, count = MEM_RANGE_COUNT(cbm);
149 for (i=0; i<count; i++) {
150 struct cb_memory_range *m = &cbm->map[i];
152 if (type == CB_MEM_TABLE) {
153 type = E820_RESERVED;
154 } else if (type == E820_ACPI || type == E820_RAM) {
155 u64 end = m->start + m->size;
156 if (end > 0x100000000ull) {
157 end -= 0x100000000ull;
158 if (end > maxram_over4G)
160 } else if (end > maxram)
163 add_e820(m->start, m->size, type);
167 RamSizeOver4G = maxram_over4G;
169 // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
170 // confuses grub. So, override it.
171 add_e820(0, 16*1024, E820_RAM);
173 struct cb_mainboard *cbmb = find_cb_subtable(cbh, CB_TAG_MAINBOARD);
175 const char *vendor = &cbmb->strings[cbmb->vendor_idx];
176 const char *part = &cbmb->strings[cbmb->part_idx];
177 dprintf(1, "Found mainboard %s %s\n", vendor, part);
179 vgahook_setup(vendor, part);
185 // No table found.. Use 16Megs as a dummy value.
186 dprintf(1, "Unable to find coreboot table!\n");
187 RamSize = 16*1024*1024;
189 add_e820(0, 16*1024*1024, E820_RAM);
194 /****************************************************************
196 ****************************************************************/
201 struct pir_header *p = pos;
202 if (p->signature != PIR_SIGNATURE)
206 if (p->size < sizeof(*p))
208 if (checksum(pos, p->size) != 0)
210 void *newpos = malloc_fseg(p->size);
215 dprintf(1, "Copying PIR from %p to %p\n", pos, newpos);
216 memcpy(newpos, pos, p->size);
217 PirOffset = (u32)newpos - BUILD_BIOS_ADDR;
221 copy_mptable(void *pos)
223 struct mptable_floating_s *p = pos;
224 if (p->signature != MPTABLE_SIGNATURE)
228 if (checksum(pos, sizeof(*p)) != 0)
230 u32 length = p->length * 16;
231 u16 mpclength = ((struct mptable_config_s *)p->physaddr)->length;
232 struct mptable_floating_s *newpos = malloc_fseg(length + mpclength);
237 dprintf(1, "Copying MPTABLE from %p/%x to %p\n", pos, p->physaddr, newpos);
238 memcpy(newpos, pos, length);
239 newpos->physaddr = (u32)newpos + length;
240 newpos->checksum -= checksum(newpos, sizeof(*newpos));
241 memcpy((void*)newpos + length, (void*)p->physaddr, mpclength);
245 copy_acpi_rsdp(void *pos)
249 struct rsdp_descriptor *p = pos;
250 if (p->signature != RSDP_SIGNATURE)
253 if (checksum(pos, length) != 0)
255 if (p->revision > 1) {
257 if (checksum(pos, length) != 0)
260 void *newpos = malloc_fseg(length);
265 dprintf(1, "Copying ACPI RSDP from %p to %p\n", pos, newpos);
266 memcpy(newpos, pos, length);
270 // Attempt to find (and relocate) any standard bios tables found in a
271 // given address range.
273 scan_tables(u32 start, u32 size)
275 void *p = (void*)ALIGN(start, 16);
276 void *end = (void*)start + size;
277 for (; p<end; p += 16) {
285 coreboot_copy_biostable(void)
287 struct cb_memory *cbm = CBMemTable;
288 if (! CONFIG_COREBOOT || !cbm)
291 dprintf(3, "Relocating coreboot bios tables\n");
293 // Init variables set in coreboot table memory scan.
297 // Scan CB_MEM_TABLE areas for bios tables.
298 int i, count = MEM_RANGE_COUNT(cbm);
299 for (i=0; i<count; i++) {
300 struct cb_memory_range *m = &cbm->map[i];
301 if (m->type == CB_MEM_TABLE)
302 scan_tables(m->start, m->size);
305 // XXX - just create dummy smbios table for now - should detect if
306 // smbios/dmi table is found from coreboot and use that instead.
311 /****************************************************************
313 ****************************************************************/
315 // Uncompress data in flash to an area of memory.
317 ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
319 dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
320 CLzmaDecoderState state;
321 int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE);
322 if (ret != LZMA_RESULT_OK) {
323 dprintf(1, "LzmaDecodeProperties error - %d\n", ret);
327 int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
328 if (need > sizeof(scratch)) {
329 dprintf(1, "LzmaDecode need %d have %d\n", need, sizeof(scratch));
332 state.Probs = (CProb *)scratch;
334 u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
335 if (dstlen > maxlen) {
336 dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
339 u32 inProcessed, outProcessed;
340 ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
341 , &inProcessed, dst, dstlen, &outProcessed);
343 dprintf(1, "LzmaDecode returned %d\n", ret);
350 /****************************************************************
351 * Coreboot flash format
352 ****************************************************************/
354 #define CBFS_HEADER_MAGIC 0x4F524243
355 #define CBFS_HEADPTR_ADDR 0xFFFFFFFc
356 #define CBFS_VERSION1 0x31313131
368 static struct cbfs_header *CBHDR;
373 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
376 CBHDR = *(void **)CBFS_HEADPTR_ADDR;
377 if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
378 dprintf(1, "Unable to find CBFS (ptr=%p; got %x not %x)\n"
379 , CBHDR, CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
384 dprintf(1, "Found CBFS header at %p\n", CBHDR);
387 #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
398 // Verify a cbfs entry looks valid.
399 static struct cbfs_file *
400 cbfs_verify(struct cbfs_file *file)
402 if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
404 u64 magic = file->magic;
405 if (magic == CBFS_FILE_MAGIC) {
406 dprintf(5, "Found CBFS file %s\n", file->filename);
412 // Return the first file in the CBFS archive
413 static struct cbfs_file *
418 return cbfs_verify((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
421 // Return the file after the given file.
422 static struct cbfs_file *
423 cbfs_getnext(struct cbfs_file *file)
425 file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
426 return cbfs_verify(file);
429 // Find the file with the given filename.
431 cbfs_findfile(const char *fname)
433 dprintf(3, "Searching CBFS for %s\n", fname);
434 struct cbfs_file *file;
435 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file))
436 if (strcmp(fname, file->filename) == 0)
441 // Find next file with the given filename prefix.
443 cbfs_findprefix(const char *prefix, struct cbfs_file *last)
445 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
448 dprintf(3, "Searching CBFS for prefix %s\n", prefix);
449 int len = strlen(prefix);
450 struct cbfs_file *file;
452 file = cbfs_getfirst();
454 file = cbfs_getnext(last);
455 for (; file; file = cbfs_getnext(file))
456 if (memcmp(prefix, file->filename, len) == 0)
461 // Find a file with the given filename (possibly with ".lzma" extension).
463 cbfs_finddatafile(const char *fname)
465 int fnlen = strlen(fname);
466 struct cbfs_file *file = NULL;
468 file = cbfs_findprefix(fname, file);
471 if (file->filename[fnlen] == '\0'
472 || strcmp(&file->filename[fnlen], ".lzma") == 0)
477 // Determine whether the file has a ".lzma" extension.
479 cbfs_iscomp(struct cbfs_file *file)
481 int fnamelen = strlen(file->filename);
482 return fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0;
485 // Return the filename of a given file.
487 cbfs_filename(struct cbfs_file *file)
489 return file->filename;
492 // Determine the uncompressed size of a datafile.
494 cbfs_datasize(struct cbfs_file *file)
496 void *src = (void*)file + ntohl(file->offset);
497 if (cbfs_iscomp(file))
498 return *(u32*)(src + LZMA_PROPERTIES_SIZE);
499 return ntohl(file->len);
502 // Copy a file to memory (uncompressing if necessary)
504 cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
506 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
509 u32 size = ntohl(file->len);
510 void *src = (void*)file + ntohl(file->offset);
511 if (cbfs_iscomp(file)) {
512 // Compressed - copy to temp ram and uncompress it.
513 void *temp = malloc_tmphigh(size);
516 iomemcpy(temp, src, size);
517 int ret = ulzma(dst, maxlen, temp, size);
524 dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
529 iomemcpy(dst, src, size);
533 struct cbfs_payload_segment {
542 #define PAYLOAD_SEGMENT_BSS 0x20535342
543 #define PAYLOAD_SEGMENT_ENTRY 0x52544E45
545 #define CBFS_COMPRESS_NONE 0
546 #define CBFS_COMPRESS_LZMA 1
548 struct cbfs_payload {
549 struct cbfs_payload_segment segments[1];
553 cbfs_run_payload(struct cbfs_file *file)
555 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
557 dprintf(1, "Run %s\n", file->filename);
558 struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
559 struct cbfs_payload_segment *seg = pay->segments;
561 void *src = (void*)pay + ntohl(seg->offset);
562 void *dest = (void*)ntohl((u32)seg->load_addr);
563 u32 src_len = ntohl(seg->len);
564 u32 dest_len = ntohl(seg->mem_len);
566 case PAYLOAD_SEGMENT_BSS:
567 dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
568 memset(dest, 0, dest_len);
570 case PAYLOAD_SEGMENT_ENTRY: {
571 dprintf(1, "Calling addr %p\n", dest);
572 void (*func)() = dest;
577 dprintf(3, "Segment %x %d@%p -> %d@%p\n"
578 , seg->type, src_len, src, dest_len, dest);
579 if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
580 if (src_len > dest_len)
582 memcpy(dest, src, src_len);
583 } else if (CONFIG_LZMA
584 && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
585 int ret = ulzma(dest, dest_len, src, src_len);
590 dprintf(1, "No support for compression type %x\n"
594 if (dest_len > src_len)
595 memset(dest + src_len, 0, dest_len - src_len);