2 * This file is part of the coreboot project.
4 * Copyright (C) 2008, Jordan Crouse <jordan@cosmicpenguin.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
22 #include <console/console.h>
26 #ifndef CONFIG_BIG_ENDIAN
27 #define ntohl(x) ( ((x&0xff)<<24) | ((x&0xff00)<<8) | \
28 ((x&0xff0000) >> 8) | ((x&0xff000000) >> 24) )
33 int cbfs_decompress(int algo, void *src, void *dst, int len)
36 case CBFS_COMPRESS_NONE:
37 memcpy(dst, src, len);
40 case CBFS_COMPRESS_LZMA:
41 if (!ulzma(src, dst)) {
42 printk_err("CBFS: LZMA decompression failed!\n");
48 printk_info( "CBFS: Unknown compression type %d\n", algo);
53 int cbfs_check_magic(struct cbfs_file *file)
55 return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
58 struct cbfs_header *cbfs_master_header(void)
60 struct cbfs_header *header;
62 void *ptr = (void *)*((unsigned long *) CBFS_HEADPTR_ADDR);
63 printk_spew("Check CBFS header at %p\n", ptr);
64 header = (struct cbfs_header *) ptr;
66 printk_spew("magic is %08x\n", ntohl(header->magic));
67 if (ntohl(header->magic) != CBFS_HEADER_MAGIC) {
68 printk_err("ERROR: No valid CBFS header found!\n");
69 if (header->magic == 0xffffffff) {
70 printk_err("Maybe the ROM isn't entirely mapped yet?\n"
71 "See (and report to) http://www.coreboot.org/Infrastructure_Projects#CBFS\n");
76 printk_spew("Found CBFS header at %p\n", ptr);
80 struct cbfs_file *cbfs_find(const char *name)
82 struct cbfs_header *header = cbfs_master_header();
87 offset = 0 - ntohl(header->romsize) + ntohl(header->offset);
89 int align= ntohl(header->align);
92 struct cbfs_file *file = (struct cbfs_file *) offset;
93 if (!cbfs_check_magic(file)) return NULL;
94 printk_spew("Check %s\n", CBFS_NAME(file));
95 if (!strcmp(CBFS_NAME(file), name))
98 int flen = ntohl(file->len);
99 int foffset = ntohl(file->offset);
100 printk_spew("CBFS: follow chain: %p + %x + %x + align -> ", (void *)offset, foffset, flen);
102 unsigned long oldoffset = offset;
103 offset = ALIGN(offset + foffset + flen, align);
104 printk_spew("%p\n", (void *)offset);
105 if (offset <= oldoffset) return NULL;
107 if (offset < 0xFFFFFFFF - ntohl(header->romsize))
112 struct cbfs_stage *cbfs_find_file(const char *name, int type)
114 struct cbfs_file *file = cbfs_find(name);
117 printk_info( "CBFS: Could not find file %s\n",
122 if (ntohl(file->type) != type) {
123 printk_info( "CBFS: File %s is of type %x instead of"
124 "type %x\n", name, file->type, type);
129 return (void *) CBFS_SUBHEADER(file);
132 static int tohex4(unsigned int c)
134 return (c<=9)?(c+'0'):(c-10+'a');
137 static void tohex16(unsigned int val, char* dest)
139 dest[0]=tohex4(val>>12);
140 dest[1]=tohex4((val>>8) & 0xf);
141 dest[2]=tohex4((val>>4) & 0xf);
142 dest[3]=tohex4(val & 0xf);
145 void *cbfs_load_optionrom(u16 vendor, u16 device, void * dest)
147 char name[17]="pciXXXX,XXXX.rom";
148 struct cbfs_optionrom *orom;
151 tohex16(vendor, name+3);
152 tohex16(device, name+8);
154 orom = (struct cbfs_optionrom *)
155 cbfs_find_file(name, CBFS_TYPE_OPTIONROM);
160 /* They might have specified a dest address. If so, we can decompress.
161 * If not, there's not much hope of decompressing or relocating the rom.
162 * in the common case, the expansion rom is uncompressed, we
163 * pass 0 in for the dest, and all we have to do is find the rom and
164 * return a pointer to it.
167 /* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
168 src = ((unsigned char *) orom); // + sizeof(struct cbfs_optionrom);
173 if (cbfs_decompress(ntohl(orom->compression),
182 void * cbfs_load_stage(const char *name)
184 struct cbfs_stage *stage = (struct cbfs_stage *)
185 cbfs_find_file(name, CBFS_TYPE_STAGE);
186 /* this is a mess. There is no ntohll. */
187 /* for now, assume compatible byte order until we solve this. */
193 printk_info("Stage: loading %s @ 0x%x (%d bytes), entry @ 0x%llx\n",
195 (u32) stage->load, stage->memlen,
197 memset((void *) (u32) stage->load, 0, stage->memlen);
199 if (cbfs_decompress(stage->compression,
200 ((unsigned char *) stage) +
201 sizeof(struct cbfs_stage),
202 (void *) (u32) stage->load,
206 printk_debug("Stage: done loading.\n");
208 entry = stage->entry;
209 // entry = ntohl((u32) stage->entry);
211 return (void *) entry;
214 void * cbfs_get_file(const char *name)
216 return cbfs_find(name);
219 int cbfs_execute_stage(const char *name)
221 struct cbfs_stage *stage = (struct cbfs_stage *)
222 cbfs_find_file(name, CBFS_TYPE_STAGE);
227 if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
228 printk_info( "CBFS: Unable to run %s: Compressed file"
229 "Not supported for in-place execution\n", name);
233 /* FIXME: This isn't right */
234 printk_info( "CBFS: run @ %p\n", (void *) ntohl((u32) stage->entry));
235 return run_address((void *) ntohl((u32) stage->entry));
239 * run_address is passed the address of a function taking no parameters and
240 * jumps to it, returning the result.
241 * @param f the address to call as a function.
242 * returns value returned by the function.
245 int run_address(void *f)