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 "biosvar.h" // GET_EBDA
10 #include "lzmadecode.h" // LzmaDecode
11 #include "smbios.h" // smbios_init
12 #include "boot.h" // boot_add_cbfs
15 /****************************************************************
17 ****************************************************************/
28 #define CB_SIGNATURE 0x4f49424C // "LBIO"
30 struct cb_memory_range {
36 #define CB_MEM_TABLE 16
41 struct cb_memory_range map[0];
44 #define CB_TAG_MEMORY 0x01
46 #define MEM_RANGE_COUNT(_rec) \
47 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
57 #define CB_TAG_MAINBOARD 0x0003
65 #define CB_TAG_FORWARD 0x11
68 ipchksum(char *buf, int count)
78 sum = (sum >> 16) + (sum & 0xffff);
83 // Try to locate the coreboot header in a given address range.
84 static struct cb_header *
85 find_cb_header(char *addr, int len)
87 char *end = addr + len;
88 for (; addr < end; addr += 16) {
89 struct cb_header *cbh = (struct cb_header *)addr;
90 if (cbh->signature != CB_SIGNATURE)
92 if (! cbh->table_bytes)
94 if (ipchksum(addr, sizeof(*cbh)) != 0)
96 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
97 != cbh->table_checksum)
104 // Try to find the coreboot memory table in the given coreboot table.
106 find_cb_subtable(struct cb_header *cbh, u32 tag)
108 char *tbl = (char *)cbh + sizeof(*cbh);
110 for (i=0; i<cbh->table_entries; i++) {
111 struct cb_memory *cbm = (struct cb_memory *)tbl;
119 static struct cb_memory *CBMemTable;
120 const char *CBvendor, *CBpart;
122 // Populate max ram and e820 map info by scanning for a coreboot table.
124 coreboot_fill_map(void)
126 dprintf(3, "Attempting to find coreboot table\n");
128 // Find coreboot table.
129 struct cb_header *cbh = find_cb_header(0, 0x1000);
132 struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD);
134 dprintf(3, "Found coreboot table forwarder.\n");
135 cbh = find_cb_header((char *)((u32)cbf->forward), 0x100);
139 dprintf(3, "Now attempting to find coreboot memory map\n");
140 struct cb_memory *cbm = CBMemTable = find_cb_subtable(cbh, CB_TAG_MEMORY);
144 u64 maxram = 0, maxram_over4G = 0;
145 int i, count = MEM_RANGE_COUNT(cbm);
146 for (i=0; i<count; i++) {
147 struct cb_memory_range *m = &cbm->map[i];
149 if (type == CB_MEM_TABLE) {
150 type = E820_RESERVED;
151 } else if (type == E820_ACPI || type == E820_RAM) {
152 u64 end = m->start + m->size;
153 if (end > 0x100000000ull) {
154 end -= 0x100000000ull;
155 if (end > maxram_over4G)
157 } else if (end > maxram)
160 add_e820(m->start, m->size, type);
164 RamSizeOver4G = maxram_over4G;
166 // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
167 // confuses grub. So, override it.
168 add_e820(0, 16*1024, E820_RAM);
170 struct cb_mainboard *cbmb = find_cb_subtable(cbh, CB_TAG_MAINBOARD);
172 CBvendor = &cbmb->strings[cbmb->vendor_idx];
173 CBpart = &cbmb->strings[cbmb->part_idx];
174 dprintf(1, "Found mainboard %s %s\n", CBvendor, CBpart);
180 // No table found.. Use 16Megs as a dummy value.
181 dprintf(1, "Unable to find coreboot table!\n");
182 RamSize = 16*1024*1024;
184 add_e820(0, 16*1024*1024, E820_RAM);
189 /****************************************************************
191 ****************************************************************/
193 // Attempt to find (and relocate) any standard bios tables found in a
194 // given address range.
196 scan_tables(u32 start, u32 size)
198 void *p = (void*)ALIGN(start, 16);
199 void *end = (void*)start + size;
200 for (; p<end; p += 16) {
209 coreboot_copy_biostable(void)
211 struct cb_memory *cbm = CBMemTable;
212 if (! CONFIG_COREBOOT || !cbm)
215 dprintf(3, "Relocating coreboot bios tables\n");
217 // Scan CB_MEM_TABLE areas for bios tables.
218 int i, count = MEM_RANGE_COUNT(cbm);
219 for (i=0; i<count; i++) {
220 struct cb_memory_range *m = &cbm->map[i];
221 if (m->type == CB_MEM_TABLE)
222 scan_tables(m->start, m->size);
225 // XXX - create a dummy smbios table for now.
231 /****************************************************************
233 ****************************************************************/
235 // Uncompress data in flash to an area of memory.
237 ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
239 dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
240 CLzmaDecoderState state;
241 int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE);
242 if (ret != LZMA_RESULT_OK) {
243 dprintf(1, "LzmaDecodeProperties error - %d\n", ret);
247 int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
248 if (need > sizeof(scratch)) {
249 dprintf(1, "LzmaDecode need %d have %d\n", need, (unsigned int)sizeof(scratch));
252 state.Probs = (CProb *)scratch;
254 u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
255 if (dstlen > maxlen) {
256 dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
259 u32 inProcessed, outProcessed;
260 ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
261 , &inProcessed, dst, dstlen, &outProcessed);
263 dprintf(1, "LzmaDecode returned %d\n", ret);
270 /****************************************************************
271 * Coreboot flash format
272 ****************************************************************/
274 #define CBFS_HEADER_MAGIC 0x4F524243
275 #define CBFS_HEADPTR_ADDR 0xFFFFFFFc
276 #define CBFS_VERSION1 0x31313131
288 static struct cbfs_header *CBHDR;
293 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
296 CBHDR = *(void **)CBFS_HEADPTR_ADDR;
297 if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
298 dprintf(1, "Unable to find CBFS (ptr=%p; got %x not %x)\n"
299 , CBHDR, CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
304 dprintf(1, "Found CBFS header at %p\n", CBHDR);
307 #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
318 // Verify a cbfs entry looks valid.
319 static struct cbfs_file *
320 cbfs_verify(struct cbfs_file *file)
322 if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
324 u64 magic = file->magic;
325 if (magic == CBFS_FILE_MAGIC) {
326 dprintf(5, "Found CBFS file %s\n", file->filename);
332 // Return the first file in the CBFS archive
333 static struct cbfs_file *
338 return cbfs_verify((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
341 // Return the file after the given file.
342 static struct cbfs_file *
343 cbfs_getnext(struct cbfs_file *file)
345 file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
346 return cbfs_verify(file);
349 // Find the file with the given filename.
351 cbfs_findfile(const char *fname)
353 dprintf(3, "Searching CBFS for %s\n", fname);
354 struct cbfs_file *file;
355 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file))
356 if (strcmp(fname, file->filename) == 0)
361 // Find next file with the given filename prefix.
363 cbfs_findprefix(const char *prefix, struct cbfs_file *last)
365 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
368 dprintf(3, "Searching CBFS for prefix %s\n", prefix);
369 int len = strlen(prefix);
370 struct cbfs_file *file;
372 file = cbfs_getfirst();
374 file = cbfs_getnext(last);
375 for (; file; file = cbfs_getnext(file))
376 if (memcmp(prefix, file->filename, len) == 0)
381 // Find a file with the given filename (possibly with ".lzma" extension).
383 cbfs_finddatafile(const char *fname)
385 int fnlen = strlen(fname);
386 struct cbfs_file *file = NULL;
388 file = cbfs_findprefix(fname, file);
391 if (file->filename[fnlen] == '\0'
392 || strcmp(&file->filename[fnlen], ".lzma") == 0)
397 // Determine whether the file has a ".lzma" extension.
399 cbfs_iscomp(struct cbfs_file *file)
401 int fnamelen = strlen(file->filename);
402 return fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0;
405 // Return the filename of a given file.
407 cbfs_filename(struct cbfs_file *file)
409 return file->filename;
412 // Determine the uncompressed size of a datafile.
414 cbfs_datasize(struct cbfs_file *file)
416 void *src = (void*)file + ntohl(file->offset);
417 if (cbfs_iscomp(file))
418 return *(u32*)(src + LZMA_PROPERTIES_SIZE);
419 return ntohl(file->len);
422 // Copy a file to memory (uncompressing if necessary)
424 cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
426 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
429 u32 size = ntohl(file->len);
430 void *src = (void*)file + ntohl(file->offset);
431 if (cbfs_iscomp(file)) {
432 // Compressed - copy to temp ram and uncompress it.
433 void *temp = malloc_tmphigh(size);
436 iomemcpy(temp, src, size);
437 int ret = ulzma(dst, maxlen, temp, size);
444 dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
449 iomemcpy(dst, src, size);
453 struct cbfs_payload_segment {
462 #define PAYLOAD_SEGMENT_BSS 0x20535342
463 #define PAYLOAD_SEGMENT_ENTRY 0x52544E45
465 #define CBFS_COMPRESS_NONE 0
466 #define CBFS_COMPRESS_LZMA 1
468 struct cbfs_payload {
469 struct cbfs_payload_segment segments[1];
473 cbfs_run_payload(struct cbfs_file *file)
475 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
477 dprintf(1, "Run %s\n", file->filename);
478 struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
479 struct cbfs_payload_segment *seg = pay->segments;
481 void *src = (void*)pay + ntohl(seg->offset);
482 void *dest = (void*)ntohl((u32)seg->load_addr);
483 u32 src_len = ntohl(seg->len);
484 u32 dest_len = ntohl(seg->mem_len);
486 case PAYLOAD_SEGMENT_BSS:
487 dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
488 memset(dest, 0, dest_len);
490 case PAYLOAD_SEGMENT_ENTRY: {
491 dprintf(1, "Calling addr %p\n", dest);
492 void (*func)() = dest;
497 dprintf(3, "Segment %x %d@%p -> %d@%p\n"
498 , seg->type, src_len, src, dest_len, dest);
499 if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
500 if (src_len > dest_len)
502 memcpy(dest, src, src_len);
503 } else if (CONFIG_LZMA
504 && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
505 int ret = ulzma(dest, dest_len, src, src_len);
510 dprintf(1, "No support for compression type %x\n"
514 if (dest_len > src_len)
515 memset(dest + src_len, 0, dest_len - src_len);
522 // Register payloads in "img/" directory with boot system.
524 cbfs_payload_setup(void)
526 struct cbfs_file *file = NULL;
528 file = cbfs_findprefix("img/", file);
531 const char *filename = cbfs_filename(file);
532 char *desc = znprintf(MAXDESCSIZE, "Payload [%s]", &filename[4]);
533 boot_add_cbfs(file, desc, bootprio_find_named_rom(filename, 0));