blob: 7fa18e4da0698b6806a11997e970de5efc50522e [file] [log] [blame]
Kevin O'Connor59a23bb2008-06-08 23:09:42 -04001// Coreboot interface support.
2//
Kevin O'Connor2929c352009-07-25 13:48:27 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor59a23bb2008-06-08 23:09:42 -04004//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor59a23bb2008-06-08 23:09:42 -04006
7#include "memmap.h" // add_e820
8#include "util.h" // dprintf
Kevin O'Connor93479e42008-06-12 22:22:43 -04009#include "pci.h" // struct pir_header
10#include "acpi.h" // struct rsdp_descriptor
Kevin O'Connor35746442009-02-27 20:54:51 -050011#include "mptable.h" // MPTABLE_SIGNATURE
Kevin O'Connor9521e262008-07-04 13:04:29 -040012#include "biosvar.h" // GET_EBDA
Kevin O'Connor592323f2009-04-26 23:17:49 -040013#include "lzmadecode.h" // LzmaDecode
Kevin O'Connor01a85202009-10-18 09:49:59 -040014#include "smbios.h" // smbios_init
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040015
16
17/****************************************************************
Kevin O'Connor93479e42008-06-12 22:22:43 -040018 * Memory map
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040019 ****************************************************************/
20
21struct cb_header {
22 u32 signature;
23 u32 header_bytes;
24 u32 header_checksum;
25 u32 table_bytes;
26 u32 table_checksum;
27 u32 table_entries;
28};
29
30#define CB_SIGNATURE 0x4f49424C // "LBIO"
31
32struct cb_memory_range {
33 u64 start;
34 u64 size;
35 u32 type;
36};
37
38#define CB_MEM_TABLE 16
39
40struct cb_memory {
41 u32 tag;
42 u32 size;
43 struct cb_memory_range map[0];
44};
45
46#define CB_TAG_MEMORY 0x01
47
48#define MEM_RANGE_COUNT(_rec) \
49 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
50
Kevin O'Connor22e1b912009-07-19 18:52:46 -040051struct cb_mainboard {
52 u32 tag;
53 u32 size;
54 u8 vendor_idx;
55 u8 part_idx;
56 char strings[0];
57};
58
59#define CB_TAG_MAINBOARD 0x0003
60
Kevin O'Connor071f1542009-03-21 13:17:21 -040061struct cb_forward {
62 u32 tag;
63 u32 size;
64 u64 forward;
65};
66
67#define CB_TAG_FORWARD 0x11
68
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040069static u16
70ipchksum(char *buf, int count)
71{
72 u16 *p = (u16*)buf;
73 u32 sum = 0;
74 while (count > 1) {
75 sum += *p++;
76 count -= 2;
77 }
78 if (count)
79 sum += *(u8*)p;
80 sum = (sum >> 16) + (sum & 0xffff);
81 sum += (sum >> 16);
82 return ~sum;
83}
84
85// Try to locate the coreboot header in a given address range.
86static struct cb_header *
87find_cb_header(char *addr, int len)
88{
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)
93 continue;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040094 if (! cbh->table_bytes)
95 continue;
96 if (ipchksum(addr, sizeof(*cbh)) != 0)
97 continue;
98 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
99 != cbh->table_checksum)
100 continue;
101 return cbh;
102 }
103 return NULL;
104}
105
106// Try to find the coreboot memory table in the given coreboot table.
Kevin O'Connor93479e42008-06-12 22:22:43 -0400107static void *
108find_cb_subtable(struct cb_header *cbh, u32 tag)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400109{
110 char *tbl = (char *)cbh + sizeof(*cbh);
111 int i;
112 for (i=0; i<cbh->table_entries; i++) {
113 struct cb_memory *cbm = (struct cb_memory *)tbl;
114 tbl += cbm->size;
Kevin O'Connor93479e42008-06-12 22:22:43 -0400115 if (cbm->tag == tag)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400116 return cbm;
117 }
118 return NULL;
119}
120
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400121static struct cb_memory *CBMemTable;
122
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400123// Populate max ram and e820 map info by scanning for a coreboot table.
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400124static void
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400125coreboot_fill_map()
126{
127 dprintf(3, "Attempting to find coreboot table\n");
Kevin O'Connoref3d8822009-02-05 19:51:12 -0500128
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400129 CBMemTable = NULL;
Kevin O'Connoref3d8822009-02-05 19:51:12 -0500130
131 // Find coreboot table.
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400132 struct cb_header *cbh = find_cb_header(0, 0x1000);
133 if (!cbh)
134 goto fail;
Kevin O'Connor071f1542009-03-21 13:17:21 -0400135 struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD);
136 if (cbf) {
137 dprintf(3, "Found coreboot table forwarder.\n");
138 cbh = find_cb_header((char *)((u32)cbf->forward), 0x100);
139 if (!cbh)
140 goto fail;
141 }
142 dprintf(3, "Now attempting to find coreboot memory map\n");
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400143 struct cb_memory *cbm = CBMemTable = find_cb_subtable(cbh, CB_TAG_MEMORY);
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400144 if (!cbm)
145 goto fail;
146
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400147 u64 maxram = 0, maxram_over4G = 0;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400148 int i, count = MEM_RANGE_COUNT(cbm);
149 for (i=0; i<count; i++) {
150 struct cb_memory_range *m = &cbm->map[i];
151 u32 type = m->type;
Kevin O'Connor93479e42008-06-12 22:22:43 -0400152 if (type == CB_MEM_TABLE) {
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400153 type = E820_RESERVED;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400154 } 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)
159 maxram_over4G = end;
160 } else if (end > maxram)
161 maxram = end;
Kevin O'Connor93479e42008-06-12 22:22:43 -0400162 }
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400163 add_e820(m->start, m->size, type);
164 }
165
Kevin O'Connore7916362008-12-28 22:03:17 -0500166 RamSize = maxram;
167 RamSizeOver4G = maxram_over4G;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400168
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400169 // 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);
172
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400173 struct cb_mainboard *cbmb = find_cb_subtable(cbh, CB_TAG_MAINBOARD);
174 if (cbmb) {
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);
178
179 vgahook_setup(vendor, part);
180 }
181
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400182 return;
183
184fail:
185 // No table found.. Use 16Megs as a dummy value.
186 dprintf(1, "Unable to find coreboot table!\n");
Kevin O'Connore7916362008-12-28 22:03:17 -0500187 RamSize = 16*1024*1024;
188 RamSizeOver4G = 0;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400189 add_e820(0, 16*1024*1024, E820_RAM);
190 return;
191}
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400192
193
194/****************************************************************
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400195 * BIOS table copying
196 ****************************************************************/
197
198static void
199copy_pir(void *pos)
200{
201 struct pir_header *p = pos;
202 if (p->signature != PIR_SIGNATURE)
203 return;
204 if (PirOffset)
205 return;
206 if (p->size < sizeof(*p))
207 return;
208 if (checksum(pos, p->size) != 0)
209 return;
210 void *newpos = malloc_fseg(p->size);
211 if (!newpos) {
212 dprintf(1, "No room to copy PIR table!\n");
213 return;
214 }
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;
218}
219
220static void
221copy_mptable(void *pos)
222{
223 struct mptable_floating_s *p = pos;
224 if (p->signature != MPTABLE_SIGNATURE)
225 return;
226 if (!p->physaddr)
227 return;
228 if (checksum(pos, sizeof(*p)) != 0)
229 return;
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);
233 if (!newpos) {
234 dprintf(1, "No room to copy MPTABLE!\n");
235 return;
236 }
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);
242}
243
244static void
245copy_acpi_rsdp(void *pos)
246{
247 if (RsdpAddr)
248 return;
249 struct rsdp_descriptor *p = pos;
250 if (p->signature != RSDP_SIGNATURE)
251 return;
252 u32 length = 20;
253 if (checksum(pos, length) != 0)
254 return;
255 if (p->revision > 1) {
256 length = p->length;
257 if (checksum(pos, length) != 0)
258 return;
259 }
260 void *newpos = malloc_fseg(length);
261 if (!newpos) {
262 dprintf(1, "No room to copy ACPI RSDP table!\n");
263 return;
264 }
265 dprintf(1, "Copying ACPI RSDP from %p to %p\n", pos, newpos);
266 memcpy(newpos, pos, length);
267 RsdpAddr = newpos;
268}
269
270// Attempt to find (and relocate) any standard bios tables found in a
271// given address range.
272static void
273scan_tables(u32 start, u32 size)
274{
275 void *p = (void*)ALIGN(start, 16);
276 void *end = (void*)start + size;
277 for (; p<end; p += 16) {
278 copy_pir(p);
279 copy_mptable(p);
280 copy_acpi_rsdp(p);
281 }
282}
283
284void
285coreboot_copy_biostable()
286{
287 struct cb_memory *cbm = CBMemTable;
288 if (! CONFIG_COREBOOT || !cbm)
289 return;
290
291 dprintf(3, "Relocating coreboot bios tables\n");
292
293 // Init variables set in coreboot table memory scan.
294 PirOffset = 0;
295 RsdpAddr = 0;
296
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);
303 }
304
305 // XXX - just create dummy smbios table for now - should detect if
306 // smbios/dmi table is found from coreboot and use that instead.
307 smbios_init();
308}
309
310
311/****************************************************************
Kevin O'Connor592323f2009-04-26 23:17:49 -0400312 * ulzma
313 ****************************************************************/
314
Kevin O'Connor1f836252009-08-16 20:17:35 -0400315// Uncompress data in flash to an area of memory.
Kevin O'Connor592323f2009-04-26 23:17:49 -0400316static int
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400317ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
Kevin O'Connor592323f2009-04-26 23:17:49 -0400318{
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400319 dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
Kevin O'Connor592323f2009-04-26 23:17:49 -0400320 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);
324 return -1;
325 }
326 u8 scratch[15980];
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));
330 return -1;
331 }
332 state.Probs = (CProb *)scratch;
333
334 u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400335 if (dstlen > maxlen) {
336 dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
337 return -1;
338 }
Kevin O'Connor592323f2009-04-26 23:17:49 -0400339 u32 inProcessed, outProcessed;
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400340 ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
Kevin O'Connor592323f2009-04-26 23:17:49 -0400341 , &inProcessed, dst, dstlen, &outProcessed);
342 if (ret) {
343 dprintf(1, "LzmaDecode returned %d\n", ret);
344 return -1;
345 }
346 return dstlen;
347}
348
349
350/****************************************************************
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400351 * Coreboot flash format
352 ****************************************************************/
353
354// XXX - optimize
355#define ntohl(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | \
356 (((x)&0xff0000) >> 8) | (((x)&0xff000000) >> 24))
357#define htonl(x) ntohl(x)
358
359#define CBFS_HEADER_MAGIC 0x4F524243
360#define CBFS_HEADPTR_ADDR 0xFFFFFFFc
361#define CBFS_VERSION1 0x31313131
362
363struct cbfs_header {
364 u32 magic;
365 u32 version;
366 u32 romsize;
367 u32 bootblocksize;
368 u32 align;
369 u32 offset;
370 u32 pad[2];
371} PACKED;
372
373static struct cbfs_header *CBHDR;
374
375static void
376cbfs_setup()
377{
378 if (! CONFIG_COREBOOT_FLASH)
379 return;
380
381 CBHDR = *(void **)CBFS_HEADPTR_ADDR;
382 if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
383 dprintf(1, "Unable to find CBFS (got %x not %x)\n"
384 , CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
385 CBHDR = NULL;
386 return;
387 }
388
389 dprintf(1, "Found CBFS header at %p\n", CBHDR);
390}
391
392#define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
393
394struct cbfs_file {
395 u64 magic;
396 u32 len;
397 u32 type;
398 u32 checksum;
399 u32 offset;
Kevin O'Connor67823442009-04-13 14:14:51 -0400400 char filename[0];
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400401} PACKED;
402
Kevin O'Connor1f836252009-08-16 20:17:35 -0400403// Verify a cbfs entry looks valid.
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400404static struct cbfs_file *
Kevin O'Connor1f836252009-08-16 20:17:35 -0400405cbfs_verify(struct cbfs_file *file)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400406{
Kevin O'Connor1f836252009-08-16 20:17:35 -0400407 if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
408 return NULL;
409 u64 magic = file->magic;
410 if (magic == CBFS_FILE_MAGIC) {
411 dprintf(5, "Found CBFS file %s\n", file->filename);
412 return file;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400413 }
Kevin O'Connor1f836252009-08-16 20:17:35 -0400414 return NULL;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400415}
416
Kevin O'Connor1f836252009-08-16 20:17:35 -0400417// Return the first file in the CBFS archive
Kevin O'Connor67823442009-04-13 14:14:51 -0400418static struct cbfs_file *
419cbfs_getfirst()
420{
421 if (! CBHDR)
422 return NULL;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400423 return cbfs_verify((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
Kevin O'Connor67823442009-04-13 14:14:51 -0400424}
425
Kevin O'Connor1f836252009-08-16 20:17:35 -0400426// Return the file after the given file.
Kevin O'Connor67823442009-04-13 14:14:51 -0400427static struct cbfs_file *
428cbfs_getnext(struct cbfs_file *file)
429{
430 file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
Kevin O'Connor1f836252009-08-16 20:17:35 -0400431 return cbfs_verify(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400432}
433
Kevin O'Connor1f836252009-08-16 20:17:35 -0400434// Find the file with the given filename.
435struct cbfs_file *
Kevin O'Connor67823442009-04-13 14:14:51 -0400436cbfs_findfile(const char *fname)
437{
Kevin O'Connor67823442009-04-13 14:14:51 -0400438 dprintf(3, "Searching CBFS for %s\n", fname);
439 struct cbfs_file *file;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400440 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file))
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400441 if (strcmp(fname, file->filename) == 0)
Kevin O'Connor67823442009-04-13 14:14:51 -0400442 return file;
Kevin O'Connor67823442009-04-13 14:14:51 -0400443 return NULL;
444}
445
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400446// Find next file with the given filename prefix.
Kevin O'Connor1f836252009-08-16 20:17:35 -0400447struct cbfs_file *
448cbfs_findprefix(const char *prefix, struct cbfs_file *last)
Kevin O'Connor67823442009-04-13 14:14:51 -0400449{
450 if (! CONFIG_COREBOOT_FLASH)
451 return NULL;
452
453 dprintf(3, "Searching CBFS for prefix %s\n", prefix);
454 int len = strlen(prefix);
455 struct cbfs_file *file;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400456 if (! last)
457 file = cbfs_getfirst();
458 else
459 file = cbfs_getnext(last);
460 for (; file; file = cbfs_getnext(file))
461 if (memcmp(prefix, file->filename, len) == 0)
462 return file;
463 return NULL;
464}
465
466// Find a file with the given filename (possibly with ".lzma" extension).
467static struct cbfs_file *
Kevin O'Connor00882592009-08-18 22:21:10 -0400468cbfs_finddatafile(const char *fname)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400469{
470 int fnlen = strlen(fname);
471 struct cbfs_file *file = NULL;
472 for (;;) {
473 file = cbfs_findprefix(fname, file);
474 if (!file)
475 return NULL;
Kevin O'Connor00882592009-08-18 22:21:10 -0400476 if (file->filename[fnlen] == '\0'
477 || strcmp(&file->filename[fnlen], ".lzma") == 0)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400478 return file;
Kevin O'Connor67823442009-04-13 14:14:51 -0400479 }
Kevin O'Connor1f836252009-08-16 20:17:35 -0400480}
481
Kevin O'Connor00882592009-08-18 22:21:10 -0400482// Determine whether the file has a ".lzma" extension.
483static int
484cbfs_iscomp(struct cbfs_file *file)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400485{
Kevin O'Connor1f836252009-08-16 20:17:35 -0400486 int fnamelen = strlen(file->filename);
Kevin O'Connor00882592009-08-18 22:21:10 -0400487 return fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400488}
489
490// Return the filename of a given file.
491const char *
492cbfs_filename(struct cbfs_file *file)
493{
494 return file->filename;
495}
496
497// Determine the uncompressed size of a datafile.
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400498u32
Kevin O'Connor00882592009-08-18 22:21:10 -0400499cbfs_datasize(struct cbfs_file *file)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400500{
501 void *src = (void*)file + ntohl(file->offset);
Kevin O'Connor00882592009-08-18 22:21:10 -0400502 if (cbfs_iscomp(file))
Kevin O'Connor1f836252009-08-16 20:17:35 -0400503 return *(u32*)(src + LZMA_PROPERTIES_SIZE);
504 return ntohl(file->len);
505}
506
507// Copy a file to memory (uncompressing if necessary)
508int
Kevin O'Connor00882592009-08-18 22:21:10 -0400509cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400510{
511 if (! CONFIG_COREBOOT_FLASH || !file)
512 return -1;
513
514 u32 size = ntohl(file->len);
515 void *src = (void*)file + ntohl(file->offset);
Kevin O'Connor34036962009-12-05 18:51:53 -0500516 if (cbfs_iscomp(file)) {
517 // Compressed - copy to temp ram and uncompress it.
518 u32 asize = ALIGN(size, 4);
519 void *temp = malloc_tmphigh(asize);
520 if (!temp)
521 return -1;
522 iomemcpy(temp, src, asize);
523 int ret = ulzma(dst, maxlen, temp, size);
524 yield();
525 free(temp);
526 return ret;
527 }
Kevin O'Connor1f836252009-08-16 20:17:35 -0400528
529 // Not compressed.
530 dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
531 if (size > maxlen) {
532 dprintf(1, "File too big to copy\n");
533 return -1;
534 }
Kevin O'Connor34036962009-12-05 18:51:53 -0500535 iomemcpy(dst, src, size);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400536 return size;
Kevin O'Connor67823442009-04-13 14:14:51 -0400537}
538
Kevin O'Connor1f836252009-08-16 20:17:35 -0400539// Find and copy the optionrom for the given vendor/device id.
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400540int
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400541cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400542{
543 if (! CONFIG_COREBOOT_FLASH)
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400544 return -1;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400545
546 char fname[17];
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400547 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
548 , (u16)vendev, vendev >> 16);
Kevin O'Connor00882592009-08-18 22:21:10 -0400549 return cbfs_copyfile(cbfs_finddatafile(fname), dst, maxlen);
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400550}
551
Kevin O'Connor67823442009-04-13 14:14:51 -0400552struct cbfs_payload_segment {
553 u32 type;
554 u32 compression;
555 u32 offset;
556 u64 load_addr;
557 u32 len;
558 u32 mem_len;
559} PACKED;
560
561#define PAYLOAD_SEGMENT_BSS 0x20535342
562#define PAYLOAD_SEGMENT_ENTRY 0x52544E45
563
564#define CBFS_COMPRESS_NONE 0
Kevin O'Connor592323f2009-04-26 23:17:49 -0400565#define CBFS_COMPRESS_LZMA 1
Kevin O'Connor67823442009-04-13 14:14:51 -0400566
567struct cbfs_payload {
568 struct cbfs_payload_segment segments[1];
569};
570
571void
Kevin O'Connor1f836252009-08-16 20:17:35 -0400572cbfs_run_payload(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400573{
Kevin O'Connor1f836252009-08-16 20:17:35 -0400574 if (!CONFIG_COREBOOT_FLASH || !file)
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400575 return;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400576 dprintf(1, "Run %s\n", file->filename);
Kevin O'Connor67823442009-04-13 14:14:51 -0400577 struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
578 struct cbfs_payload_segment *seg = pay->segments;
579 for (;;) {
Kevin O'Connor67823442009-04-13 14:14:51 -0400580 void *src = (void*)pay + ntohl(seg->offset);
581 void *dest = (void*)ntohl((u32)seg->load_addr);
582 u32 src_len = ntohl(seg->len);
583 u32 dest_len = ntohl(seg->mem_len);
584 switch (seg->type) {
585 case PAYLOAD_SEGMENT_BSS:
586 dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
587 memset(dest, 0, dest_len);
588 break;
589 case PAYLOAD_SEGMENT_ENTRY: {
590 dprintf(1, "Calling addr %p\n", dest);
591 void (*func)() = dest;
592 func();
593 return;
594 }
595 default:
596 dprintf(3, "Segment %x %d@%p -> %d@%p\n"
597 , seg->type, src_len, src, dest_len, dest);
Kevin O'Connor592323f2009-04-26 23:17:49 -0400598 if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400599 if (src_len > dest_len)
600 src_len = dest_len;
Kevin O'Connor592323f2009-04-26 23:17:49 -0400601 memcpy(dest, src, src_len);
602 } else if (CONFIG_LZMA
603 && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400604 int ret = ulzma(dest, dest_len, src, src_len);
Kevin O'Connor592323f2009-04-26 23:17:49 -0400605 if (ret < 0)
606 return;
607 src_len = ret;
608 } else {
609 dprintf(1, "No support for compression type %x\n"
610 , seg->compression);
611 return;
612 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400613 if (dest_len > src_len)
614 memset(dest + src_len, 0, dest_len - src_len);
615 break;
616 }
617 seg++;
618 }
619}
620
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400621void
622coreboot_setup(void)
623{
624 coreboot_fill_map();
625 cbfs_setup();
626}