blob: ee9dd8b4b4d91171440f949673e062574c2f504f [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'Connor9521e262008-07-04 13:04:29 -04009#include "biosvar.h" // GET_EBDA
Kevin O'Connor592323f2009-04-26 23:17:49 -040010#include "lzmadecode.h" // LzmaDecode
Kevin O'Connor01a85202009-10-18 09:49:59 -040011#include "smbios.h" // smbios_init
Kevin O'Connor72eee3e2010-12-27 19:07:49 -050012#include "boot.h" // boot_add_cbfs
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040013
14
15/****************************************************************
Kevin O'Connor93479e42008-06-12 22:22:43 -040016 * Memory map
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040017 ****************************************************************/
18
19struct cb_header {
20 u32 signature;
21 u32 header_bytes;
22 u32 header_checksum;
23 u32 table_bytes;
24 u32 table_checksum;
25 u32 table_entries;
26};
27
28#define CB_SIGNATURE 0x4f49424C // "LBIO"
29
30struct cb_memory_range {
31 u64 start;
32 u64 size;
33 u32 type;
34};
35
36#define CB_MEM_TABLE 16
37
38struct cb_memory {
39 u32 tag;
40 u32 size;
41 struct cb_memory_range map[0];
42};
43
44#define CB_TAG_MEMORY 0x01
45
46#define MEM_RANGE_COUNT(_rec) \
47 (((_rec)->size - sizeof(*(_rec))) / sizeof((_rec)->map[0]))
48
Kevin O'Connor22e1b912009-07-19 18:52:46 -040049struct cb_mainboard {
50 u32 tag;
51 u32 size;
52 u8 vendor_idx;
53 u8 part_idx;
54 char strings[0];
55};
56
57#define CB_TAG_MAINBOARD 0x0003
58
Kevin O'Connor071f1542009-03-21 13:17:21 -040059struct cb_forward {
60 u32 tag;
61 u32 size;
62 u64 forward;
63};
64
65#define CB_TAG_FORWARD 0x11
66
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040067static u16
68ipchksum(char *buf, int count)
69{
70 u16 *p = (u16*)buf;
71 u32 sum = 0;
72 while (count > 1) {
73 sum += *p++;
74 count -= 2;
75 }
76 if (count)
77 sum += *(u8*)p;
78 sum = (sum >> 16) + (sum & 0xffff);
79 sum += (sum >> 16);
80 return ~sum;
81}
82
83// Try to locate the coreboot header in a given address range.
84static struct cb_header *
85find_cb_header(char *addr, int len)
86{
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)
91 continue;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040092 if (! cbh->table_bytes)
93 continue;
94 if (ipchksum(addr, sizeof(*cbh)) != 0)
95 continue;
96 if (ipchksum(addr + sizeof(*cbh), cbh->table_bytes)
97 != cbh->table_checksum)
98 continue;
99 return cbh;
100 }
101 return NULL;
102}
103
104// Try to find the coreboot memory table in the given coreboot table.
Kevin O'Connor93479e42008-06-12 22:22:43 -0400105static void *
106find_cb_subtable(struct cb_header *cbh, u32 tag)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400107{
108 char *tbl = (char *)cbh + sizeof(*cbh);
109 int i;
110 for (i=0; i<cbh->table_entries; i++) {
111 struct cb_memory *cbm = (struct cb_memory *)tbl;
112 tbl += cbm->size;
Kevin O'Connor93479e42008-06-12 22:22:43 -0400113 if (cbm->tag == tag)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400114 return cbm;
115 }
116 return NULL;
117}
118
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400119static struct cb_memory *CBMemTable;
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400120const char *CBvendor, *CBpart;
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400121
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400122// Populate max ram and e820 map info by scanning for a coreboot table.
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400123static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500124coreboot_fill_map(void)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400125{
126 dprintf(3, "Attempting to find coreboot table\n");
Kevin O'Connoref3d8822009-02-05 19:51:12 -0500127
Kevin O'Connoref3d8822009-02-05 19:51:12 -0500128 // Find coreboot table.
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400129 struct cb_header *cbh = find_cb_header(0, 0x1000);
130 if (!cbh)
131 goto fail;
Kevin O'Connor071f1542009-03-21 13:17:21 -0400132 struct cb_forward *cbf = find_cb_subtable(cbh, CB_TAG_FORWARD);
133 if (cbf) {
134 dprintf(3, "Found coreboot table forwarder.\n");
135 cbh = find_cb_header((char *)((u32)cbf->forward), 0x100);
136 if (!cbh)
137 goto fail;
138 }
139 dprintf(3, "Now attempting to find coreboot memory map\n");
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400140 struct cb_memory *cbm = CBMemTable = find_cb_subtable(cbh, CB_TAG_MEMORY);
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400141 if (!cbm)
142 goto fail;
143
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400144 u64 maxram = 0, maxram_over4G = 0;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400145 int i, count = MEM_RANGE_COUNT(cbm);
146 for (i=0; i<count; i++) {
147 struct cb_memory_range *m = &cbm->map[i];
148 u32 type = m->type;
Kevin O'Connor93479e42008-06-12 22:22:43 -0400149 if (type == CB_MEM_TABLE) {
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400150 type = E820_RESERVED;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400151 } 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)
156 maxram_over4G = end;
157 } else if (end > maxram)
158 maxram = end;
Kevin O'Connor93479e42008-06-12 22:22:43 -0400159 }
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400160 add_e820(m->start, m->size, type);
161 }
162
Kevin O'Connore7916362008-12-28 22:03:17 -0500163 RamSize = maxram;
164 RamSizeOver4G = maxram_over4G;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400165
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400166 // 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);
169
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400170 struct cb_mainboard *cbmb = find_cb_subtable(cbh, CB_TAG_MAINBOARD);
171 if (cbmb) {
Kevin O'Connorc1de91b2011-07-02 13:50:21 -0400172 CBvendor = &cbmb->strings[cbmb->vendor_idx];
173 CBpart = &cbmb->strings[cbmb->part_idx];
174 dprintf(1, "Found mainboard %s %s\n", CBvendor, CBpart);
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400175 }
176
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400177 return;
178
179fail:
180 // No table found.. Use 16Megs as a dummy value.
181 dprintf(1, "Unable to find coreboot table!\n");
Kevin O'Connore7916362008-12-28 22:03:17 -0500182 RamSize = 16*1024*1024;
183 RamSizeOver4G = 0;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400184 add_e820(0, 16*1024*1024, E820_RAM);
185 return;
186}
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400187
188
189/****************************************************************
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400190 * BIOS table copying
191 ****************************************************************/
192
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400193// Attempt to find (and relocate) any standard bios tables found in a
194// given address range.
195static void
196scan_tables(u32 start, u32 size)
197{
198 void *p = (void*)ALIGN(start, 16);
199 void *end = (void*)start + size;
200 for (; p<end; p += 16) {
201 copy_pir(p);
202 copy_mptable(p);
203 copy_acpi_rsdp(p);
Kevin O'Connor83012de2011-08-28 12:42:15 -0400204 copy_smbios(p);
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400205 }
206}
207
208void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500209coreboot_copy_biostable(void)
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400210{
211 struct cb_memory *cbm = CBMemTable;
212 if (! CONFIG_COREBOOT || !cbm)
213 return;
214
215 dprintf(3, "Relocating coreboot bios tables\n");
216
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400217 // 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);
223 }
224
Kevin O'Connor83012de2011-08-28 12:42:15 -0400225 // XXX - create a dummy smbios table for now.
226 if (!SMBiosAddr)
227 smbios_init();
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400228}
229
230
231/****************************************************************
Kevin O'Connor592323f2009-04-26 23:17:49 -0400232 * ulzma
233 ****************************************************************/
234
Kevin O'Connor1f836252009-08-16 20:17:35 -0400235// Uncompress data in flash to an area of memory.
Kevin O'Connor592323f2009-04-26 23:17:49 -0400236static int
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400237ulzma(u8 *dst, u32 maxlen, const u8 *src, u32 srclen)
Kevin O'Connor592323f2009-04-26 23:17:49 -0400238{
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400239 dprintf(3, "Uncompressing data %d@%p to %d@%p\n", srclen, src, maxlen, dst);
Kevin O'Connor592323f2009-04-26 23:17:49 -0400240 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);
244 return -1;
245 }
246 u8 scratch[15980];
247 int need = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
248 if (need > sizeof(scratch)) {
Kevin O'Connor311f8872010-12-26 14:16:22 -0500249 dprintf(1, "LzmaDecode need %d have %d\n", need, (unsigned int)sizeof(scratch));
Kevin O'Connor592323f2009-04-26 23:17:49 -0400250 return -1;
251 }
252 state.Probs = (CProb *)scratch;
253
254 u32 dstlen = *(u32*)(src + LZMA_PROPERTIES_SIZE);
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400255 if (dstlen > maxlen) {
256 dprintf(1, "LzmaDecode too large (max %d need %d)\n", maxlen, dstlen);
257 return -1;
258 }
Kevin O'Connor592323f2009-04-26 23:17:49 -0400259 u32 inProcessed, outProcessed;
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400260 ret = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, srclen
Kevin O'Connor592323f2009-04-26 23:17:49 -0400261 , &inProcessed, dst, dstlen, &outProcessed);
262 if (ret) {
263 dprintf(1, "LzmaDecode returned %d\n", ret);
264 return -1;
265 }
266 return dstlen;
267}
268
269
270/****************************************************************
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400271 * Coreboot flash format
272 ****************************************************************/
273
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400274#define CBFS_HEADER_MAGIC 0x4F524243
275#define CBFS_HEADPTR_ADDR 0xFFFFFFFc
276#define CBFS_VERSION1 0x31313131
277
278struct cbfs_header {
279 u32 magic;
280 u32 version;
281 u32 romsize;
282 u32 bootblocksize;
283 u32 align;
284 u32 offset;
285 u32 pad[2];
286} PACKED;
287
288static struct cbfs_header *CBHDR;
289
290static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500291cbfs_setup(void)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400292{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400293 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400294 return;
295
296 CBHDR = *(void **)CBFS_HEADPTR_ADDR;
297 if (CBHDR->magic != htonl(CBFS_HEADER_MAGIC)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500298 dprintf(1, "Unable to find CBFS (ptr=%p; got %x not %x)\n"
299 , CBHDR, CBHDR->magic, htonl(CBFS_HEADER_MAGIC));
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400300 CBHDR = NULL;
301 return;
302 }
303
304 dprintf(1, "Found CBFS header at %p\n", CBHDR);
305}
306
307#define CBFS_FILE_MAGIC 0x455649484352414cLL // LARCHIVE
308
309struct cbfs_file {
310 u64 magic;
311 u32 len;
312 u32 type;
313 u32 checksum;
314 u32 offset;
Kevin O'Connor67823442009-04-13 14:14:51 -0400315 char filename[0];
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400316} PACKED;
317
Kevin O'Connor1f836252009-08-16 20:17:35 -0400318// Verify a cbfs entry looks valid.
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400319static struct cbfs_file *
Kevin O'Connor1f836252009-08-16 20:17:35 -0400320cbfs_verify(struct cbfs_file *file)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400321{
Kevin O'Connor1f836252009-08-16 20:17:35 -0400322 if (file < (struct cbfs_file *)(0xFFFFFFFF - ntohl(CBHDR->romsize)))
323 return NULL;
324 u64 magic = file->magic;
325 if (magic == CBFS_FILE_MAGIC) {
326 dprintf(5, "Found CBFS file %s\n", file->filename);
327 return file;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400328 }
Kevin O'Connor1f836252009-08-16 20:17:35 -0400329 return NULL;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400330}
331
Kevin O'Connor1f836252009-08-16 20:17:35 -0400332// Return the first file in the CBFS archive
Kevin O'Connor67823442009-04-13 14:14:51 -0400333static struct cbfs_file *
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500334cbfs_getfirst(void)
Kevin O'Connor67823442009-04-13 14:14:51 -0400335{
336 if (! CBHDR)
337 return NULL;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400338 return cbfs_verify((void *)(0 - ntohl(CBHDR->romsize) + ntohl(CBHDR->offset)));
Kevin O'Connor67823442009-04-13 14:14:51 -0400339}
340
Kevin O'Connor1f836252009-08-16 20:17:35 -0400341// Return the file after the given file.
Kevin O'Connor67823442009-04-13 14:14:51 -0400342static struct cbfs_file *
343cbfs_getnext(struct cbfs_file *file)
344{
345 file = (void*)file + ALIGN(ntohl(file->len) + ntohl(file->offset), ntohl(CBHDR->align));
Kevin O'Connor1f836252009-08-16 20:17:35 -0400346 return cbfs_verify(file);
Kevin O'Connor67823442009-04-13 14:14:51 -0400347}
348
Kevin O'Connor1f836252009-08-16 20:17:35 -0400349// Find the file with the given filename.
350struct cbfs_file *
Kevin O'Connor67823442009-04-13 14:14:51 -0400351cbfs_findfile(const char *fname)
352{
Kevin O'Connor67823442009-04-13 14:14:51 -0400353 dprintf(3, "Searching CBFS for %s\n", fname);
354 struct cbfs_file *file;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400355 for (file = cbfs_getfirst(); file; file = cbfs_getnext(file))
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400356 if (strcmp(fname, file->filename) == 0)
Kevin O'Connor67823442009-04-13 14:14:51 -0400357 return file;
Kevin O'Connor67823442009-04-13 14:14:51 -0400358 return NULL;
359}
360
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400361// Find next file with the given filename prefix.
Kevin O'Connor1f836252009-08-16 20:17:35 -0400362struct cbfs_file *
363cbfs_findprefix(const char *prefix, struct cbfs_file *last)
Kevin O'Connor67823442009-04-13 14:14:51 -0400364{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400365 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH)
Kevin O'Connor67823442009-04-13 14:14:51 -0400366 return NULL;
367
368 dprintf(3, "Searching CBFS for prefix %s\n", prefix);
369 int len = strlen(prefix);
370 struct cbfs_file *file;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400371 if (! last)
372 file = cbfs_getfirst();
373 else
374 file = cbfs_getnext(last);
375 for (; file; file = cbfs_getnext(file))
376 if (memcmp(prefix, file->filename, len) == 0)
377 return file;
378 return NULL;
379}
380
381// Find a file with the given filename (possibly with ".lzma" extension).
Kevin O'Connore2304262010-06-13 16:05:17 -0400382struct cbfs_file *
Kevin O'Connor00882592009-08-18 22:21:10 -0400383cbfs_finddatafile(const char *fname)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400384{
385 int fnlen = strlen(fname);
386 struct cbfs_file *file = NULL;
387 for (;;) {
388 file = cbfs_findprefix(fname, file);
389 if (!file)
390 return NULL;
Kevin O'Connor00882592009-08-18 22:21:10 -0400391 if (file->filename[fnlen] == '\0'
392 || strcmp(&file->filename[fnlen], ".lzma") == 0)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400393 return file;
Kevin O'Connor67823442009-04-13 14:14:51 -0400394 }
Kevin O'Connor1f836252009-08-16 20:17:35 -0400395}
396
Kevin O'Connor00882592009-08-18 22:21:10 -0400397// Determine whether the file has a ".lzma" extension.
398static int
399cbfs_iscomp(struct cbfs_file *file)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400400{
Kevin O'Connor1f836252009-08-16 20:17:35 -0400401 int fnamelen = strlen(file->filename);
Kevin O'Connor00882592009-08-18 22:21:10 -0400402 return fnamelen > 5 && strcmp(&file->filename[fnamelen-5], ".lzma") == 0;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400403}
404
405// Return the filename of a given file.
406const char *
407cbfs_filename(struct cbfs_file *file)
408{
409 return file->filename;
410}
411
412// Determine the uncompressed size of a datafile.
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400413u32
Kevin O'Connor00882592009-08-18 22:21:10 -0400414cbfs_datasize(struct cbfs_file *file)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400415{
416 void *src = (void*)file + ntohl(file->offset);
Kevin O'Connor00882592009-08-18 22:21:10 -0400417 if (cbfs_iscomp(file))
Kevin O'Connor1f836252009-08-16 20:17:35 -0400418 return *(u32*)(src + LZMA_PROPERTIES_SIZE);
419 return ntohl(file->len);
420}
421
422// Copy a file to memory (uncompressing if necessary)
423int
Kevin O'Connor00882592009-08-18 22:21:10 -0400424cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400425{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400426 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
Kevin O'Connor1f836252009-08-16 20:17:35 -0400427 return -1;
428
429 u32 size = ntohl(file->len);
430 void *src = (void*)file + ntohl(file->offset);
Kevin O'Connor34036962009-12-05 18:51:53 -0500431 if (cbfs_iscomp(file)) {
432 // Compressed - copy to temp ram and uncompress it.
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400433 void *temp = malloc_tmphigh(size);
Kevin O'Connor34036962009-12-05 18:51:53 -0500434 if (!temp)
435 return -1;
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400436 iomemcpy(temp, src, size);
Kevin O'Connor34036962009-12-05 18:51:53 -0500437 int ret = ulzma(dst, maxlen, temp, size);
438 yield();
439 free(temp);
440 return ret;
441 }
Kevin O'Connor1f836252009-08-16 20:17:35 -0400442
443 // Not compressed.
444 dprintf(3, "Copying data %d@%p to %d@%p\n", size, src, maxlen, dst);
445 if (size > maxlen) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500446 warn_noalloc();
Kevin O'Connor1f836252009-08-16 20:17:35 -0400447 return -1;
448 }
Kevin O'Connor34036962009-12-05 18:51:53 -0500449 iomemcpy(dst, src, size);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400450 return size;
Kevin O'Connor67823442009-04-13 14:14:51 -0400451}
452
Kevin O'Connor67823442009-04-13 14:14:51 -0400453struct cbfs_payload_segment {
454 u32 type;
455 u32 compression;
456 u32 offset;
457 u64 load_addr;
458 u32 len;
459 u32 mem_len;
460} PACKED;
461
462#define PAYLOAD_SEGMENT_BSS 0x20535342
463#define PAYLOAD_SEGMENT_ENTRY 0x52544E45
464
465#define CBFS_COMPRESS_NONE 0
Kevin O'Connor592323f2009-04-26 23:17:49 -0400466#define CBFS_COMPRESS_LZMA 1
Kevin O'Connor67823442009-04-13 14:14:51 -0400467
468struct cbfs_payload {
469 struct cbfs_payload_segment segments[1];
470};
471
472void
Kevin O'Connor1f836252009-08-16 20:17:35 -0400473cbfs_run_payload(struct cbfs_file *file)
Kevin O'Connor67823442009-04-13 14:14:51 -0400474{
Kevin O'Connord9c93612010-03-20 11:00:45 -0400475 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !file)
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400476 return;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400477 dprintf(1, "Run %s\n", file->filename);
Kevin O'Connor67823442009-04-13 14:14:51 -0400478 struct cbfs_payload *pay = (void*)file + ntohl(file->offset);
479 struct cbfs_payload_segment *seg = pay->segments;
480 for (;;) {
Kevin O'Connor67823442009-04-13 14:14:51 -0400481 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);
485 switch (seg->type) {
486 case PAYLOAD_SEGMENT_BSS:
487 dprintf(3, "BSS segment %d@%p\n", dest_len, dest);
488 memset(dest, 0, dest_len);
489 break;
490 case PAYLOAD_SEGMENT_ENTRY: {
491 dprintf(1, "Calling addr %p\n", dest);
492 void (*func)() = dest;
493 func();
494 return;
495 }
496 default:
497 dprintf(3, "Segment %x %d@%p -> %d@%p\n"
498 , seg->type, src_len, src, dest_len, dest);
Kevin O'Connor592323f2009-04-26 23:17:49 -0400499 if (seg->compression == htonl(CBFS_COMPRESS_NONE)) {
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400500 if (src_len > dest_len)
501 src_len = dest_len;
Kevin O'Connor592323f2009-04-26 23:17:49 -0400502 memcpy(dest, src, src_len);
503 } else if (CONFIG_LZMA
504 && seg->compression == htonl(CBFS_COMPRESS_LZMA)) {
Kevin O'Connorcbd6ca02009-04-27 21:51:13 -0400505 int ret = ulzma(dest, dest_len, src, src_len);
Kevin O'Connor592323f2009-04-26 23:17:49 -0400506 if (ret < 0)
507 return;
508 src_len = ret;
509 } else {
510 dprintf(1, "No support for compression type %x\n"
511 , seg->compression);
512 return;
513 }
Kevin O'Connor67823442009-04-13 14:14:51 -0400514 if (dest_len > src_len)
515 memset(dest + src_len, 0, dest_len - src_len);
516 break;
517 }
518 seg++;
519 }
520}
521
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500522// Register payloads in "img/" directory with boot system.
Kevin O'Connor89a1efd2011-01-08 12:24:39 -0500523void
524cbfs_payload_setup(void)
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500525{
526 struct cbfs_file *file = NULL;
527 for (;;) {
528 file = cbfs_findprefix("img/", file);
529 if (!file)
530 break;
531 const char *filename = cbfs_filename(file);
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500532 char *desc = znprintf(MAXDESCSIZE, "Payload [%s]", &filename[4]);
Kevin O'Connorc4bd3b92010-12-29 21:59:44 -0500533 boot_add_cbfs(file, desc, bootprio_find_named_rom(filename, 0));
Kevin O'Connor72eee3e2010-12-27 19:07:49 -0500534 }
535}
536
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400537void
538coreboot_setup(void)
539{
540 coreboot_fill_map();
541 cbfs_setup();
542}