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>
28 static int cbfs_check_magic(struct cbfs_file *file)
30 return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
33 static struct cbfs_header *cbfs_master_header(void)
35 struct cbfs_header *header;
37 void *ptr = (void *)*((unsigned long *) CBFS_HEADPTR_ADDR);
38 printk(BIOS_SPEW, "Check CBFS header at %p\n", ptr);
39 header = (struct cbfs_header *) ptr;
41 printk(BIOS_SPEW, "magic is %08x\n", ntohl(header->magic));
42 if (ntohl(header->magic) != CBFS_HEADER_MAGIC) {
43 printk(BIOS_ERR, "ERROR: No valid CBFS header found!\n");
44 if (header->magic == 0xffffffff) {
45 printk(BIOS_ERR, "Maybe the ROM isn't entirely mapped yet?\n"
46 "See (and report to) http://www.coreboot.org/Infrastructure_Projects#CBFS\n");
51 printk(BIOS_SPEW, "Found CBFS header at %p\n", ptr);
55 struct cbfs_file *cbfs_find(const char *name)
57 struct cbfs_header *header = cbfs_master_header();
62 offset = 0 - ntohl(header->romsize) + ntohl(header->offset);
64 int align= ntohl(header->align);
67 struct cbfs_file *file = (struct cbfs_file *) offset;
68 if (!cbfs_check_magic(file)) return NULL;
69 printk(BIOS_SPEW, "Check %s\n", CBFS_NAME(file));
70 if (!strcmp(CBFS_NAME(file), name))
73 int flen = ntohl(file->len);
74 int foffset = ntohl(file->offset);
75 printk(BIOS_SPEW, "CBFS: follow chain: %p + %x + %x + align -> ", (void *)offset, foffset, flen);
77 unsigned long oldoffset = offset;
78 offset = ALIGN(offset + foffset + flen, align);
79 printk(BIOS_SPEW, "%p\n", (void *)offset);
80 if (offset <= oldoffset) return NULL;
82 if (offset < 0xFFFFFFFF - ntohl(header->romsize))
87 void *cbfs_find_file(const char *name, int type)
89 struct cbfs_file *file = cbfs_find(name);
92 printk(BIOS_INFO, "CBFS: Could not find file %s\n",
97 if (ntohl(file->type) != type) {
98 printk(BIOS_INFO, "CBFS: File %s is of type %x instead of"
99 "type %x\n", name, file->type, type);
104 return (void *) CBFS_SUBHEADER(file);
108 static inline int tohex4(unsigned int c)
110 return (c<=9)?(c+'0'):(c-10+'a');
113 static void tohex16(unsigned int val, char* dest)
115 dest[0]=tohex4(val>>12);
116 dest[1]=tohex4((val>>8) & 0xf);
117 dest[2]=tohex4((val>>4) & 0xf);
118 dest[3]=tohex4(val & 0xf);
122 * Decompression wrapper for CBFS
127 * @return 0 on success, -1 on failure
129 static int cbfs_decompress(int algo, void *src, void *dst, int len)
132 case CBFS_COMPRESS_NONE:
133 memcpy(dst, src, len);
136 case CBFS_COMPRESS_LZMA:
137 if (!ulzma(src, dst)) {
138 printk(BIOS_ERR, "CBFS: LZMA decompression failed!\n");
143 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", algo);
149 void *cbfs_load_optionrom(u16 vendor, u16 device, void * dest)
151 char name[17]="pciXXXX,XXXX.rom";
152 struct cbfs_optionrom *orom;
155 tohex16(vendor, name+3);
156 tohex16(device, name+8);
158 orom = (struct cbfs_optionrom *)
159 cbfs_find_file(name, CBFS_TYPE_OPTIONROM);
164 /* They might have specified a dest address. If so, we can decompress.
165 * If not, there's not much hope of decompressing or relocating the rom.
166 * in the common case, the expansion rom is uncompressed, we
167 * pass 0 in for the dest, and all we have to do is find the rom and
168 * return a pointer to it.
171 /* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
172 src = ((unsigned char *) orom); // + sizeof(struct cbfs_optionrom);
177 if (cbfs_decompress(ntohl(orom->compression),
186 void * cbfs_load_stage(const char *name)
188 struct cbfs_stage *stage = (struct cbfs_stage *)
189 cbfs_find_file(name, CBFS_TYPE_STAGE);
190 /* this is a mess. There is no ntohll. */
191 /* for now, assume compatible byte order until we solve this. */
197 printk(BIOS_INFO, "Stage: loading %s @ 0x%x (%d bytes), entry @ 0x%llx\n",
199 (u32) stage->load, stage->memlen,
201 memset((void *) (u32) stage->load, 0, stage->memlen);
203 if (cbfs_decompress(stage->compression,
204 ((unsigned char *) stage) +
205 sizeof(struct cbfs_stage),
206 (void *) (u32) stage->load,
210 printk(BIOS_DEBUG, "Stage: done loading.\n");
212 entry = stage->entry;
213 // entry = ntohl((u32) stage->entry);
215 return (void *) entry;
218 int cbfs_execute_stage(const char *name)
220 struct cbfs_stage *stage = (struct cbfs_stage *)
221 cbfs_find_file(name, CBFS_TYPE_STAGE);
226 if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
227 printk(BIOS_INFO, "CBFS: Unable to run %s: Compressed file"
228 "Not supported for in-place execution\n", name);
232 /* FIXME: This isn't right */
233 printk(BIOS_INFO, "CBFS: run @ %p\n", (void *) ntohl((u32) stage->entry));
234 return run_address((void *) ntohl((u32) stage->entry));
238 * run_address is passed the address of a function taking no parameters and
239 * jumps to it, returning the result.
240 * @param f the address to call as a function.
241 * @return value returned by the function.
244 int run_address(void *f)