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 "block.h" // MAXDESCSIZE
8 #include "byteorder.h" // be32_to_cpu
9 #include "config.h" // CONFIG_*
10 #include "hw/pci.h" // pci_probe_devices
11 #include "lzmadecode.h" // LzmaDecode
12 #include "malloc.h" // free
13 #include "memmap.h" // add_e820
14 #include "output.h" // dprintf
15 #include "paravirt.h" // PlatformRunningOn
16 #include "romfile.h" // romfile_findprefix
17 #include "stacks.h" // yield
18 #include "string.h" // memset
19 #include "util.h" // coreboot_preinit
22 /****************************************************************
24 ****************************************************************/
35 #define CB_SIGNATURE 0x4f49424C // "LBIO"
37 struct cb_memory_range {
43 #define CB_MEM_TABLE 16
48 struct cb_memory_range map[0];
51 #define CB_TAG_MEMORY 0x01
53 #define MEM_RANGE_COUNT(_rec) \
54 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
64 #define CB_TAG_MAINBOARD 0x0003
72 #define CB_TAG_FORWARD 0x11
80 #define CB_TAG_CBMEM_CONSOLE 0x17
82 struct cbmem_console {
87 static struct cbmem_console *cbcon = NULL;
90 ipchksum(char *buf, int count)
95 sum += GET_FARVAR(0, *p);
100 sum += GET_FARVAR(0, *(u8*)p);
101 sum = (sum >> 16) + (sum & 0xffff);
106 // Try to locate the coreboot header in a given address range.
107 static struct cb_header *
108 find_cb_header(u32 addr, int len)
110 u32 end = addr + len;
111 for (; addr < end; addr += 16) {
112 struct cb_header *cbh = (void*)addr;
113 if (GET_FARVAR(0, cbh->signature) != CB_SIGNATURE)
115 u32 tsize = GET_FARVAR(0, cbh->table_bytes);
118 if (ipchksum((void*)addr, sizeof(*cbh)) != 0)
120 if (ipchksum((void*)addr + sizeof(*cbh), tsize)
121 != GET_FARVAR(0, cbh->table_checksum))
128 // Try to find the coreboot memory table in the given coreboot table.
130 find_cb_subtable(struct cb_header *cbh, u32 tag)
132 char *tbl = (char *)cbh + sizeof(*cbh);
133 u32 count = GET_FARVAR(0, cbh->table_entries);
135 for (i=0; i<count; i++) {
136 struct cb_memory *cbm = (void*)tbl;
137 tbl += GET_FARVAR(0, cbm->size);
138 if (GET_FARVAR(0, cbm->tag) == tag)
147 struct cb_header *cbh = find_cb_header(0, 0x1000);
150 struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD);
152 dprintf(3, "Found coreboot table forwarder.\n");
153 cbh = find_cb_header(GET_FARVAR(0, cbf->forward), 0x100);
160 static struct cb_memory *CBMemTable;
161 const char *CBvendor = "", *CBpart = "";
163 // Populate max ram and e820 map info by scanning for a coreboot table.
165 coreboot_preinit(void)
167 if (!CONFIG_COREBOOT)
170 dprintf(3, "Attempting to find coreboot table\n");
172 // Find coreboot table.
173 struct cb_header *cbh = find_cb_table();
176 dprintf(3, "Now attempting to find coreboot memory map\n");
177 struct cb_memory *cbm = CBMemTable = find_cb_subtable(cbh, CB_TAG_MEMORY);
181 int i, count = MEM_RANGE_COUNT(cbm);
182 for (i=0; i<count; i++) {
183 struct cb_memory_range *m = &cbm->map[i];
185 if (type == CB_MEM_TABLE)
186 type = E820_RESERVED;
187 add_e820(m->start, m->size, type);
190 // Ughh - coreboot likes to set a map at 0x0000-0x1000, but this
191 // confuses grub. So, override it.
192 add_e820(0, 16*1024, E820_RAM);
194 struct cb_cbmem_ref *cbref = find_cb_subtable(cbh, CB_TAG_CBMEM_CONSOLE);
196 cbcon = (void*)(u32)cbref->cbmem_addr;
198 dprintf(1, "Found coreboot cbmem console @ %llx\n", cbref->cbmem_addr);
201 struct cb_mainboard *cbmb = find_cb_subtable(cbh, CB_TAG_MAINBOARD);
203 CBvendor = &cbmb->strings[cbmb->vendor_idx];
204 CBpart = &cbmb->strings[cbmb->part_idx];
205 dprintf(1, "Found mainboard %s %s\n", CBvendor, CBpart);
211 // No table found.. Use 16Megs as a dummy value.
212 dprintf(1, "Unable to find coreboot table!\n");
213 add_e820(0, 16*1024*1024, E820_RAM);
217 void coreboot_debug_putc(char c)
219 if (!CONFIG_DEBUG_COREBOOT)
223 u32 cursor = cbcon->buffer_cursor++;
224 if (cursor < cbcon->buffer_size)
225 cbcon->buffer_body[cursor] = c;
228 /****************************************************************
230 ****************************************************************/
232 // Attempt to find (and relocate) any standard bios tables found in a
233 // given address range.
235 scan_tables(u32 start, u32 size)
237 void *p = (void*)ALIGN(start, 16);
238 void *end = (void*)start + size;
239 for (; p<end; p += 16)
244 coreboot_platform_setup(void)
246 if (!CONFIG_COREBOOT)
250 struct cb_memory *cbm = CBMemTable;
254 dprintf(3, "Relocating coreboot bios tables\n");
256 // Scan CB_MEM_TABLE areas for bios tables.
257 int i, count = MEM_RANGE_COUNT(cbm);
258 for (i=0; i<count; i++) {
259 struct cb_memory_range *m = &cbm->map[i];
260 if (m->type == CB_MEM_TABLE)
261 scan_tables(m->start, m->size);
264 find_acpi_features();
268 /****************************************************************
270 ****************************************************************/
272 // Uncompress data in flash to an area of memory.
274 ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
276 dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
277 CLzmaDecoderState state;
278 int ret = LzmaDecodeProperties(&state.Properties, src, LZMA_PROPERTIES_SIZE);
279 if (ret != LZMA_RESULT_OK) {
280 dprintf(1, "LzmaDecodeProperties error - %d\n", ret);
284 int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
285 if (need > sizeof(scratch)) {
286 dprintf(1, "LzmaDecode need %d have %d\n", need, (unsigned int)sizeof(scratch));
289 state.Probs = (CProb *)scratch;
291 u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
292 if (dstlen > maxlen) {
293 dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
296 u32 inProcessed, outProcessed;
297 ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
298 , &inProcessed, dst, dstlen, &outProcessed);
300 dprintf(1, "LzmaDecode returned %d\n", ret);
307 /****************************************************************
308 * Coreboot flash format
309 ****************************************************************/
311 #define CBFS_HEADER_MAGIC 0x4F524243
312 #define CBFS_VERSION1 0x31313131
324 #define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
335 struct cbfs_romfile_s {
336 struct romfile_s file;
337 struct cbfs_file *fhdr;
342 // Copy a file to memory (uncompressing if necessary)
344 cbfs_copyfile(struct romfile_s *file, void *dst, u32 maxlen)
346 if (!CONFIG_COREBOOT_FLASH)
349 struct cbfs_romfile_s *cfile;
350 cfile = container_of(file, struct cbfs_romfile_s, file);
351 u32 size = cfile->rawsize;
352 void *src = cfile->data;
354 // Compressed - copy to temp ram and uncompress it.
355 void *temp = malloc_tmphigh(size);
360 iomemcpy(temp, src, size);
361 int ret = ulzma(dst, maxlen, temp, size);
368 dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
373 iomemcpy(dst, src, size);
377 // Process CBFS links file. The links file is a newline separated
378 // file where each line has a "link name" and a "destination name"
379 // separated by a space character.
381 process_links_file(void)
383 char *links = romfile_loadfile("links", NULL), *next = links;
385 // Parse out linkname and destname
386 char *linkname = next;
387 next = strchr(linkname, '\n');
390 char *comment = strchr(linkname, '#');
393 linkname = nullTrailingSpace(linkname);
394 char *destname = strchr(linkname, ' ');
398 destname = nullTrailingSpace(destname);
399 // Lookup destname and create new romfile entry for linkname
400 struct romfile_s *ufile = romfile_find(destname);
403 struct cbfs_romfile_s *cufile
404 = container_of(ufile, struct cbfs_romfile_s, file);
405 struct cbfs_romfile_s *cfile = malloc_tmp(sizeof(*cfile));
410 memcpy(cfile, cufile, sizeof(*cfile));
411 strtcpy(cfile->file.name, linkname, sizeof(cfile->file.name));
412 romfile_add(&cfile->file);
418 coreboot_cbfs_init(void)
420 if (!CONFIG_COREBOOT_FLASH)
423 struct cbfs_header *hdr = *(void **)(CONFIG_CBFS_LOCATION - 4);
424 if (hdr->magic != cpu_to_be32(CBFS_HEADER_MAGIC)) {
425 dprintf(1, "Unable to find CBFS (ptr=%p; got %x not %x)\n"
426 , hdr, hdr->magic, cpu_to_be32(CBFS_HEADER_MAGIC));
429 dprintf(1, "Found CBFS header at %p\n", hdr);
431 u32 romsize = be32_to_cpu(hdr->romsize);
432 u32 romstart = CONFIG_CBFS_LOCATION - romsize;
433 struct cbfs_file *fhdr = (void*)romstart + be32_to_cpu(hdr->offset);
435 if ((u32)fhdr - romstart > romsize)
437 u64 magic = fhdr->magic;
438 if (magic != CBFS_FILE_MAGIC)
440 struct cbfs_romfile_s *cfile = malloc_tmp(sizeof(*cfile));
445 memset(cfile, 0, sizeof(*cfile));
446 strtcpy(cfile->file.name, fhdr->filename, sizeof(cfile->file.name));
447 cfile->file.size = cfile->rawsize = be32_to_cpu(fhdr->len);
449 cfile->file.copy = cbfs_copyfile;
450 cfile->data = (void*)fhdr + be32_to_cpu(fhdr->offset);
451 int len = strlen(cfile->file.name);
452 if (len > 5 && strcmp(&cfile->file.name[len-5], ".lzma") == 0) {
453 // Using compression.
455 cfile->file.name[len-5] = '\0';
456 cfile->file.size = *(u32*)(cfile->data + LZMA_PROPERTIES_SIZE);
458 romfile_add(&cfile->file);
460 fhdr = (void*)ALIGN((u32)cfile->data + cfile->rawsize
461 , be32_to_cpu(hdr->align));
464 process_links_file();
467 struct cbfs_payload_segment {
476 #define PAYLOAD_SEGMENT_BSS 0x20535342
477 #define PAYLOAD_SEGMENT_ENTRY 0x52544E45
479 #define CBFS_COMPRESS_NONE 0
480 #define CBFS_COMPRESS_LZMA 1
482 struct cbfs_payload {
483 struct cbfs_payload_segment segments[1];
487 cbfs_run_payload(struct cbfs_file *fhdr)
489 if (!CONFIG_COREBOOT_FLASH || !fhdr)
491 dprintf(1, "Run %s\n", fhdr->filename);
492 struct cbfs_payload *pay = (void*)fhdr + be32_to_cpu(fhdr->offset);
493 struct cbfs_payload_segment *seg = pay->segments;
495 void *src = (void*)pay + be32_to_cpu(seg->offset);
496 void *dest = (void*)(u32)be64_to_cpu(seg->load_addr);
497 u32 src_len = be32_to_cpu(seg->len);
498 u32 dest_len = be32_to_cpu(seg->mem_len);
500 case PAYLOAD_SEGMENT_BSS:
501 dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
502 memset(dest, 0, dest_len);
504 case PAYLOAD_SEGMENT_ENTRY: {
505 dprintf(1, "Calling addr %p\n", dest);
506 void (*func)() = dest;
511 dprintf(3, "Segment %x %d@%p -> %d@%p\n"
512 , seg->type, src_len, src, dest_len, dest);
513 if (seg->compression == cpu_to_be32(CBFS_COMPRESS_NONE)) {
514 if (src_len > dest_len)
516 memcpy(dest, src, src_len);
517 } else if (CONFIG_LZMA
518 && seg->compression == cpu_to_be32(CBFS_COMPRESS_LZMA)) {
519 int ret = ulzma(dest, dest_len, src, src_len);
524 dprintf(1, "No support for compression type %x\n"
528 if (dest_len > src_len)
529 memset(dest + src_len, 0, dest_len - src_len);
536 // Register payloads in "img/" directory with boot system.
538 cbfs_payload_setup(void)
540 if (!CONFIG_COREBOOT_FLASH)
542 struct romfile_s *file = NULL;
544 file = romfile_findprefix("img/", file);
547 struct cbfs_romfile_s *cfile;
548 cfile = container_of(file, struct cbfs_romfile_s, file);
549 const char *filename = file->name;
550 char *desc = znprintf(MAXDESCSIZE, "Payload [%s]", &filename[4]);
551 boot_add_cbfs(cfile->fhdr, desc, bootprio_find_named_rom(filename, 0));