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) {
208 coreboot_copy_biostable(void)
210 struct cb_memory *cbm = CBMemTable;
211 if (! CONFIG_COREBOOT || !cbm)
214 dprintf(3, "Relocating coreboot bios tables\n");
216 // Scan CB_MEM_TABLE areas for bios tables.
217 int i, count = MEM_RANGE_COUNT(cbm);
218 for (i=0; i<count; i++) {
219 struct cb_memory_range *m = &cbm->map[i];
220 if (m->type == CB_MEM_TABLE)
221 scan_tables(m->start, m->size);
224 // XXX - just create dummy smbios table for now - should detect if
225 // smbios/dmi table is found from coreboot and use that instead.
230 /****************************************************************
232 ****************************************************************/
234 // Uncompress data in flash to an area of memory.
236 ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
238 dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
239 CLzmaDecoderState state;
240 int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE);
241 if (ret != LZMA_RESULT_OK) {
242 dprintf(1, "LzmaDecodeProperties error - %d\n", ret);
246 int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
247 if (need > sizeof(scratch)) {
248 dprintf(1, "LzmaDecode need %d have %d\n", need, (unsigned int)sizeof(scratch));
251 state.Probs = (CProb *)scratch;
253 u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
254 if (dstlen > maxlen) {
255 dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
258 u32 inProcessed, outProcessed;
259 ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
260 , &inProcessed, dst, dstlen, &outProcessed);
262 dprintf(1, "LzmaDecode returned %d\n", ret);
269 /****************************************************************
270 * Coreboot flash format
271 ****************************************************************/
273 #define CBFS_HEADER_MAGIC 0x4F524243
274 #define CBFS_HEADPTR_ADDR 0xFFFFFFFc
275 #define CBFS_VERSION1 0x31313131
287 static struct cbfs_header *CBHDR;
292 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
295 CBHDR = *(void **)CBFS_HEADPTR_ADDR;
296 if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
297 dprintf(1, "Unable to find CBFS (ptr=%p; got %x not %x)\n"
298 , CBHDR, CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
303 dprintf(1, "Found CBFS header at %p\n", CBHDR);
306 #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
317 // Verify a cbfs entry looks valid.
318 static struct cbfs_file *
319 cbfs_verify(struct cbfs_file *file)
321 if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
323 u64 magic = file->magic;
324 if (magic == CBFS_FILE_MAGIC) {
325 dprintf(5, "Found CBFS file %s\n", file->filename);
331 // Return the first file in the CBFS archive
332 static struct cbfs_file *
337 return cbfs_verify((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
340 // Return the file after the given file.
341 static struct cbfs_file *
342 cbfs_getnext(struct cbfs_file *file)
344 file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
345 return cbfs_verify(file);
348 // Find the file with the given filename.
350 cbfs_findfile(const char *fname)
352 dprintf(3, "Searching CBFS for %s\n", fname);
353 struct cbfs_file *file;
354 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file))
355 if (strcmp(fname, file->filename) == 0)
360 // Find next file with the given filename prefix.
362 cbfs_findprefix(const char *prefix, struct cbfs_file *last)
364 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
367 dprintf(3, "Searching CBFS for prefix %s\n", prefix);
368 int len = strlen(prefix);
369 struct cbfs_file *file;
371 file = cbfs_getfirst();
373 file = cbfs_getnext(last);
374 for (; file; file = cbfs_getnext(file))
375 if (memcmp(prefix, file->filename, len) == 0)
380 // Find a file with the given filename (possibly with ".lzma" extension).
382 cbfs_finddatafile(const char *fname)
384 int fnlen = strlen(fname);
385 struct cbfs_file *file = NULL;
387 file = cbfs_findprefix(fname, file);
390 if (file->filename[fnlen] == '\0'
391 || strcmp(&file->filename[fnlen], ".lzma") == 0)
396 // Determine whether the file has a ".lzma" extension.
398 cbfs_iscomp(struct cbfs_file *file)
400 int fnamelen = strlen(file->filename);
401 return fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0;
404 // Return the filename of a given file.
406 cbfs_filename(struct cbfs_file *file)
408 return file->filename;
411 // Determine the uncompressed size of a datafile.
413 cbfs_datasize(struct cbfs_file *file)
415 void *src = (void*)file + ntohl(file->offset);
416 if (cbfs_iscomp(file))
417 return *(u32*)(src + LZMA_PROPERTIES_SIZE);
418 return ntohl(file->len);
421 // Copy a file to memory (uncompressing if necessary)
423 cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
425 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
428 u32 size = ntohl(file->len);
429 void *src = (void*)file + ntohl(file->offset);
430 if (cbfs_iscomp(file)) {
431 // Compressed - copy to temp ram and uncompress it.
432 void *temp = malloc_tmphigh(size);
435 iomemcpy(temp, src, size);
436 int ret = ulzma(dst, maxlen, temp, size);
443 dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
448 iomemcpy(dst, src, size);
452 struct cbfs_payload_segment {
461 #define PAYLOAD_SEGMENT_BSS 0x20535342
462 #define PAYLOAD_SEGMENT_ENTRY 0x52544E45
464 #define CBFS_COMPRESS_NONE 0
465 #define CBFS_COMPRESS_LZMA 1
467 struct cbfs_payload {
468 struct cbfs_payload_segment segments[1];
472 cbfs_run_payload(struct cbfs_file *file)
474 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
476 dprintf(1, "Run %s\n", file->filename);
477 struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
478 struct cbfs_payload_segment *seg = pay->segments;
480 void *src = (void*)pay + ntohl(seg->offset);
481 void *dest = (void*)ntohl((u32)seg->load_addr);
482 u32 src_len = ntohl(seg->len);
483 u32 dest_len = ntohl(seg->mem_len);
485 case PAYLOAD_SEGMENT_BSS:
486 dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
487 memset(dest, 0, dest_len);
489 case PAYLOAD_SEGMENT_ENTRY: {
490 dprintf(1, "Calling addr %p\n", dest);
491 void (*func)() = dest;
496 dprintf(3, "Segment %x %d@%p -> %d@%p\n"
497 , seg->type, src_len, src, dest_len, dest);
498 if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
499 if (src_len > dest_len)
501 memcpy(dest, src, src_len);
502 } else if (CONFIG_LZMA
503 && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
504 int ret = ulzma(dest, dest_len, src, src_len);
509 dprintf(1, "No support for compression type %x\n"
513 if (dest_len > src_len)
514 memset(dest + src_len, 0, dest_len - src_len);
521 // Register payloads in "img/" directory with boot system.
523 cbfs_payload_setup(void)
525 struct cbfs_file *file = NULL;
527 file = cbfs_findprefix("img/", file);
530 const char *filename = cbfs_filename(file);
531 char *desc = znprintf(MAXDESCSIZE, "Payload [%s]", &filename[4]);
532 boot_add_cbfs(file, desc, bootprio_find_named_rom(filename, 0));