blob: 3dc6a7fd775a8505f7aefef58ccfbc20e4b9785c [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
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400354#define CBFS_HEADER_MAGIC 0x4F524243
355#define CBFS_HEADPTR_ADDR 0xFFFFFFFc
356#define CBFS_VERSION1 0x31313131
357
358struct cbfs_header {
359 u32 magic;
360 u32 version;
361 u32 romsize;
362 u32 bootblocksize;
363 u32 align;
364 u32 offset;
365 u32 pad[2];
366} PACKED;
367
368static struct cbfs_header *CBHDR;
369
370static void
371cbfs_setup()
372{
373 if (! CONFIG_COREBOOT_FLASH)
374 return;
375
376 CBHDR = *(void **)CBFS_HEADPTR_ADDR;
377 if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
378 dprintf(1, "Unable to find CBFS (got %x not %x)\n"
379 , CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
380 CBHDR = NULL;
381 return;
382 }
383
384 dprintf(1, "Found CBFS header at %p\n", CBHDR);
385}
386
387#define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
388
389struct cbfs_file {
390 u64 magic;
391 u32 len;
392 u32 type;
393 u32 checksum;
394 u32 offset;
Kevin O'Connor67823442009-04-13 14:14:51 -0400395 char filename[0];
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400396} PACKED;
397
Kevin O'Connor1f836252009-08-16 20:17:35 -0400398// Verify a cbfs entry looks valid.
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400399static struct cbfs_file *
Kevin O'Connor1f836252009-08-16 20:17:35 -0400400cbfs_verify(struct cbfs_file *file)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400401{
Kevin O'Connor1f836252009-08-16 20:17:35 -0400402 if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
403 return NULL;
404 u64 magic = file->magic;
405 if (magic == CBFS_FILE_MAGIC) {
406 dprintf(5, "Found CBFS file %s\n", file->filename);
407 return file;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400408 }
Kevin O'Connor1f836252009-08-16 20:17:35 -0400409 return NULL;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400410}
411
Kevin O'Connor1f836252009-08-16 20:17:35 -0400412// Return the first file in the CBFS archive
Kevin O'Connor67823442009-04-13 14:14:51 -0400413static struct cbfs_file *
414cbfs_getfirst()
415{
416 if (! CBHDR)
417 return NULL;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400418 return cbfs_verify((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
Kevin O'Connor67823442009-04-13 14:14:51 -0400419}
420
Kevin O'Connor1f836252009-08-16 20:17:35 -0400421// Return the file after the given file.
Kevin O'Connor67823442009-04-13 14:14:51 -0400422static struct cbfs_file *
423cbfs_getnext(struct cbfs_file *file)
424{
425 file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
Kevin O'Connor1f836252009-08-16 20:17:35 -0400426 return cbfs_verify(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400427}
428
Kevin O'Connor1f836252009-08-16 20:17:35 -0400429// Find the file with the given filename.
430struct cbfs_file *
Kevin O'Connor67823442009-04-13 14:14:51 -0400431cbfs_findfile(const char *fname)
432{
Kevin O'Connor67823442009-04-13 14:14:51 -0400433 dprintf(3, "Searching CBFS for %s\n", fname);
434 struct cbfs_file *file;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400435 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file))
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400436 if (strcmp(fname, file->filename) == 0)
Kevin O'Connor67823442009-04-13 14:14:51 -0400437 return file;
Kevin O'Connor67823442009-04-13 14:14:51 -0400438 return NULL;
439}
440
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400441// Find next file with the given filename prefix.
Kevin O'Connor1f836252009-08-16 20:17:35 -0400442struct cbfs_file *
443cbfs_findprefix(const char *prefix, struct cbfs_file *last)
Kevin O'Connor67823442009-04-13 14:14:51 -0400444{
445 if (! CONFIG_COREBOOT_FLASH)
446 return NULL;
447
448 dprintf(3, "Searching CBFS for prefix %s\n", prefix);
449 int len = strlen(prefix);
450 struct cbfs_file *file;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400451 if (! last)
452 file = cbfs_getfirst();
453 else
454 file = cbfs_getnext(last);
455 for (; file; file = cbfs_getnext(file))
456 if (memcmp(prefix, file->filename, len) == 0)
457 return file;
458 return NULL;
459}
460
461// Find a file with the given filename (possibly with ".lzma" extension).
462static struct cbfs_file *
Kevin O'Connor00882592009-08-18 22:21:10 -0400463cbfs_finddatafile(const char *fname)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400464{
465 int fnlen = strlen(fname);
466 struct cbfs_file *file = NULL;
467 for (;;) {
468 file = cbfs_findprefix(fname, file);
469 if (!file)
470 return NULL;
Kevin O'Connor00882592009-08-18 22:21:10 -0400471 if (file->filename[fnlen] == '\0'
472 || strcmp(&file->filename[fnlen], ".lzma") == 0)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400473 return file;
Kevin O'Connor67823442009-04-13 14:14:51 -0400474 }
Kevin O'Connor1f836252009-08-16 20:17:35 -0400475}
476
Kevin O'Connor00882592009-08-18 22:21:10 -0400477// Determine whether the file has a ".lzma" extension.
478static int
479cbfs_iscomp(struct cbfs_file *file)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400480{
Kevin O'Connor1f836252009-08-16 20:17:35 -0400481 int fnamelen = strlen(file->filename);
Kevin O'Connor00882592009-08-18 22:21:10 -0400482 return fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400483}
484
485// Return the filename of a given file.
486const char *
487cbfs_filename(struct cbfs_file *file)
488{
489 return file->filename;
490}
491
492// Determine the uncompressed size of a datafile.
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400493u32
Kevin O'Connor00882592009-08-18 22:21:10 -0400494cbfs_datasize(struct cbfs_file *file)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400495{
496 void *src = (void*)file + ntohl(file->offset);
Kevin O'Connor00882592009-08-18 22:21:10 -0400497 if (cbfs_iscomp(file))
Kevin O'Connor1f836252009-08-16 20:17:35 -0400498 return *(u32*)(src + LZMA_PROPERTIES_SIZE);
499 return ntohl(file->len);
500}
501
502// Copy a file to memory (uncompressing if necessary)
503int
Kevin O'Connor00882592009-08-18 22:21:10 -0400504cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400505{
506 if (! CONFIG_COREBOOT_FLASH || !file)
507 return -1;
508
509 u32 size = ntohl(file->len);
510 void *src = (void*)file + ntohl(file->offset);
Kevin O'Connor34036962009-12-05 18:51:53 -0500511 if (cbfs_iscomp(file)) {
512 // Compressed - copy to temp ram and uncompress it.
513 u32 asize = ALIGN(size, 4);
514 void *temp = malloc_tmphigh(asize);
515 if (!temp)
516 return -1;
517 iomemcpy(temp, src, asize);
518 int ret = ulzma(dst, maxlen, temp, size);
519 yield();
520 free(temp);
521 return ret;
522 }
Kevin O'Connor1f836252009-08-16 20:17:35 -0400523
524 // Not compressed.
525 dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
526 if (size > maxlen) {
527 dprintf(1, "File too big to copy\n");
528 return -1;
529 }
Kevin O'Connor34036962009-12-05 18:51:53 -0500530 iomemcpy(dst, src, size);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400531 return size;
Kevin O'Connor67823442009-04-13 14:14:51 -0400532}
533
Kevin O'Connor1f836252009-08-16 20:17:35 -0400534// Find and copy the optionrom for the given vendor/device id.
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400535int
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400536cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400537{
538 if (! CONFIG_COREBOOT_FLASH)
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400539 return -1;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400540
541 char fname[17];
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400542 snprintf(fname, sizeof(fname), "pci%04x,%04x.rom"
543 , (u16)vendev, vendev >> 16);
Kevin O'Connor00882592009-08-18 22:21:10 -0400544 return cbfs_copyfile(cbfs_finddatafile(fname), dst, maxlen);
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400545}
546
Kevin O'Connor67823442009-04-13 14:14:51 -0400547struct cbfs_payload_segment {
548 u32 type;
549 u32 compression;
550 u32 offset;
551 u64 load_addr;
552 u32 len;
553 u32 mem_len;
554} PACKED;
555
556#define PAYLOAD_SEGMENT_BSS 0x20535342
557#define PAYLOAD_SEGMENT_ENTRY 0x52544E45
558
559#define CBFS_COMPRESS_NONE 0
Kevin O'Connor592323f2009-04-26 23:17:49 -0400560#define CBFS_COMPRESS_LZMA 1
Kevin O'Connor67823442009-04-13 14:14:51 -0400561
562struct cbfs_payload {
563 struct cbfs_payload_segment segments[1];
564};
565
566void
Kevin O'Connor1f836252009-08-16 20:17:35 -0400567cbfs_run_payload(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400568{
Kevin O'Connor1f836252009-08-16 20:17:35 -0400569 if (!CONFIG_COREBOOT_FLASH || !file)
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400570 return;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400571 dprintf(1, "Run %s\n", file->filename);
Kevin O'Connor67823442009-04-13 14:14:51 -0400572 struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
573 struct cbfs_payload_segment *seg = pay->segments;
574 for (;;) {
Kevin O'Connor67823442009-04-13 14:14:51 -0400575 void *src = (void*)pay + ntohl(seg->offset);
576 void *dest = (void*)ntohl((u32)seg->load_addr);
577 u32 src_len = ntohl(seg->len);
578 u32 dest_len = ntohl(seg->mem_len);
579 switch (seg->type) {
580 case PAYLOAD_SEGMENT_BSS:
581 dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
582 memset(dest, 0, dest_len);
583 break;
584 case PAYLOAD_SEGMENT_ENTRY: {
585 dprintf(1, "Calling addr %p\n", dest);
586 void (*func)() = dest;
587 func();
588 return;
589 }
590 default:
591 dprintf(3, "Segment %x %d@%p -> %d@%p\n"
592 , seg->type, src_len, src, dest_len, dest);
Kevin O'Connor592323f2009-04-26 23:17:49 -0400593 if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400594 if (src_len > dest_len)
595 src_len = dest_len;
Kevin O'Connor592323f2009-04-26 23:17:49 -0400596 memcpy(dest, src, src_len);
597 } else if (CONFIG_LZMA
598 && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400599 int ret = ulzma(dest, dest_len, src, src_len);
Kevin O'Connor592323f2009-04-26 23:17:49 -0400600 if (ret < 0)
601 return;
602 src_len = ret;
603 } else {
604 dprintf(1, "No support for compression type %x\n"
605 , seg->compression);
606 return;
607 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400608 if (dest_len > src_len)
609 memset(dest + src_len, 0, dest_len - src_len);
610 break;
611 }
612 seg++;
613 }
614}
615
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400616void
617coreboot_setup(void)
618{
619 coreboot_fill_map();
620 cbfs_setup();
621}