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>
25 #include <arch/byteorder.h>
27 int cbfs_decompress(int algo, void *src, void *dst, int len)
30 case CBFS_COMPRESS_NONE:
31 memcpy(dst, src, len);
34 case CBFS_COMPRESS_LZMA:
35 if (!ulzma(src, dst)) {
36 printk_err("CBFS: LZMA decompression failed!\n");
42 printk_info( "CBFS: Unknown compression type %d\n", algo);
47 int cbfs_check_magic(struct cbfs_file *file)
49 return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
52 struct cbfs_header *cbfs_master_header(void)
54 struct cbfs_header *header;
56 void *ptr = (void *)*((unsigned long *) CBFS_HEADPTR_ADDR);
57 printk_spew("Check CBFS header at %p\n", ptr);
58 header = (struct cbfs_header *) ptr;
60 printk_spew("magic is %08x\n", ntohl(header->magic));
61 if (ntohl(header->magic) != CBFS_HEADER_MAGIC) {
62 printk_err("ERROR: No valid CBFS header found!\n");
63 if (header->magic == 0xffffffff) {
64 printk_err("Maybe the ROM isn't entirely mapped yet?\n"
65 "See (and report to) http://www.coreboot.org/Infrastructure_Projects#CBFS\n");
70 printk_spew("Found CBFS header at %p\n", ptr);
74 struct cbfs_file *cbfs_find(const char *name)
76 struct cbfs_header *header = cbfs_master_header();
81 offset = 0 - ntohl(header->romsize) + ntohl(header->offset);
83 int align= ntohl(header->align);
86 struct cbfs_file *file = (struct cbfs_file *) offset;
87 if (!cbfs_check_magic(file)) return NULL;
88 printk_spew("Check %s\n", CBFS_NAME(file));
89 if (!strcmp(CBFS_NAME(file), name))
92 int flen = ntohl(file->len);
93 int foffset = ntohl(file->offset);
94 printk_spew("CBFS: follow chain: %p + %x + %x + align -> ", (void *)offset, foffset, flen);
96 unsigned long oldoffset = offset;
97 offset = ALIGN(offset + foffset + flen, align);
98 printk_spew("%p\n", (void *)offset);
99 if (offset <= oldoffset) return NULL;
101 if (offset < 0xFFFFFFFF - ntohl(header->romsize))
106 struct cbfs_stage *cbfs_find_file(const char *name, int type)
108 struct cbfs_file *file = cbfs_find(name);
111 printk_info( "CBFS: Could not find file %s\n",
116 if (ntohl(file->type) != type) {
117 printk_info( "CBFS: File %s is of type %x instead of"
118 "type %x\n", name, file->type, type);
123 return (void *) CBFS_SUBHEADER(file);
126 static int tohex4(unsigned int c)
128 return (c<=9)?(c+'0'):(c-10+'a');
131 static void tohex16(unsigned int val, char* dest)
133 dest[0]=tohex4(val>>12);
134 dest[1]=tohex4((val>>8) & 0xf);
135 dest[2]=tohex4((val>>4) & 0xf);
136 dest[3]=tohex4(val & 0xf);
139 void *cbfs_load_optionrom(u16 vendor, u16 device, void * dest)
141 char name[17]="pciXXXX,XXXX.rom";
142 struct cbfs_optionrom *orom;
145 tohex16(vendor, name+3);
146 tohex16(device, name+8);
148 orom = (struct cbfs_optionrom *)
149 cbfs_find_file(name, CBFS_TYPE_OPTIONROM);
154 /* They might have specified a dest address. If so, we can decompress.
155 * If not, there's not much hope of decompressing or relocating the rom.
156 * in the common case, the expansion rom is uncompressed, we
157 * pass 0 in for the dest, and all we have to do is find the rom and
158 * return a pointer to it.
161 /* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
162 src = ((unsigned char *) orom); // + sizeof(struct cbfs_optionrom);
167 if (cbfs_decompress(ntohl(orom->compression),
176 void * cbfs_load_stage(const char *name)
178 struct cbfs_stage *stage = (struct cbfs_stage *)
179 cbfs_find_file(name, CBFS_TYPE_STAGE);
180 /* this is a mess. There is no ntohll. */
181 /* for now, assume compatible byte order until we solve this. */
187 printk_info("Stage: loading %s @ 0x%x (%d bytes), entry @ 0x%llx\n",
189 (u32) stage->load, stage->memlen,
191 memset((void *) (u32) stage->load, 0, stage->memlen);
193 if (cbfs_decompress(stage->compression,
194 ((unsigned char *) stage) +
195 sizeof(struct cbfs_stage),
196 (void *) (u32) stage->load,
200 printk_debug("Stage: done loading.\n");
202 entry = stage->entry;
203 // entry = ntohl((u32) stage->entry);
205 return (void *) entry;
208 void * cbfs_get_file(const char *name)
210 return (void *) cbfs_find(name);
213 int cbfs_execute_stage(const char *name)
215 struct cbfs_stage *stage = (struct cbfs_stage *)
216 cbfs_find_file(name, CBFS_TYPE_STAGE);
221 if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
222 printk_info( "CBFS: Unable to run %s: Compressed file"
223 "Not supported for in-place execution\n", name);
227 /* FIXME: This isn't right */
228 printk_info( "CBFS: run @ %p\n", (void *) ntohl((u32) stage->entry));
229 return run_address((void *) ntohl((u32) stage->entry));
233 * run_address is passed the address of a function taking no parameters and
234 * jumps to it, returning the result.
235 * @param f the address to call as a function.
236 * returns value returned by the function.
239 int run_address(void *f)