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 #ifndef CONFIG_BIG_ENDIAN
26 #define ntohl(x) ( ((x&0xff)<<24) | ((x&0xff00)<<8) | \
27 ((x&0xff0000) >> 8) | ((x&0xff000000) >> 24) )
32 int run_address(void *f);
34 int cbfs_decompress(int algo, void *src, void *dst, int len)
37 case CBFS_COMPRESS_NONE:
38 memcpy(dst, src, len);
41 #if CONFIG_COMPRESSED_PAYLOAD_LZMA==1
43 case CBFS_COMPRESS_LZMA: {
44 unsigned long ulzma(unsigned char *src, unsigned char *dst);
50 #if CONFIG_COMPRESSED_PAYLOAD_NRV2B==1
51 case CBFS_COMPRESS_NRV2B: {
52 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
55 unrv2b(src, dst, &tmp);
60 printk_info( "CBFS: Unknown compression type %d\n",
66 int cbfs_check_magic(struct cbfs_file *file)
68 return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
71 struct cbfs_header *cbfs_master_header(void)
73 struct cbfs_header *header;
75 void *ptr = (void *)*((unsigned long *) CBFS_HEADPTR_ADDR);
76 printk_debug("Check CBFS header at %p\n", ptr);
77 header = (struct cbfs_header *) ptr;
79 printk_debug("magic is %08x\n", ntohl(header->magic));
80 if (ntohl(header->magic) != CBFS_HEADER_MAGIC) {
81 printk_err("NO CBFS HEADER\n");
85 printk_debug("Found CBFS header at %p\n", ptr);
89 struct cbfs_file *cbfs_find(const char *name)
91 struct cbfs_header *header = cbfs_master_header();
96 offset = 0 - ntohl(header->romsize) + ntohl(header->offset);
98 int align= ntohl(header->align);
101 struct cbfs_file *file = (struct cbfs_file *) offset;
102 if (!cbfs_check_magic(file)) return NULL;
103 printk_info("Check %s\n", CBFS_NAME(file));
104 if (!strcmp(CBFS_NAME(file), name))
107 int flen = ntohl(file->len);
108 int foffset = ntohl(file->offset);
109 printk_spew("CBFS: follow chain: %p + %x + %x + align -> ", offset, foffset, flen);
111 unsigned long oldoffset = offset;
112 offset = ALIGN(offset + foffset + flen, align);
113 printk_spew("%p\n", offset);
114 if (offset <= oldoffset) return NULL;
116 if (offset < 0xFFFFFFFF - ntohl(header->romsize))
121 struct cbfs_stage *cbfs_find_file(const char *name, int type)
123 struct cbfs_file *file = cbfs_find(name);
126 printk_info( "CBFS: Could not find file %s\n",
131 if (ntohl(file->type) != type) {
132 printk_info( "CBFS: File %s is of type %x instead of"
133 "type %x\n", name, file->type, type);
138 return (void *) CBFS_SUBHEADER(file);
141 void *cbfs_load_optionrom(u16 vendor, u16 device, void * dest)
144 struct cbfs_optionrom *orom;
147 sprintf(name,"pci%04x,%04x.rom", vendor, device);
149 orom = (struct cbfs_optionrom *)
150 cbfs_find_file(name, CBFS_TYPE_OPTIONROM);
155 /* They might have specified a dest address. If so, we can decompress.
156 * If not, there's not much hope of decompressing or relocating the rom.
157 * in the common case, the expansion rom is uncompressed, we
158 * pass 0 in for the dest, and all we have to do is find the rom and
159 * return a pointer to it.
162 /* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
163 src = ((unsigned char *) orom); // + sizeof(struct cbfs_optionrom);
168 if (cbfs_decompress(ntohl(orom->compression),
177 void * cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
179 int selfboot(struct lb_memory *mem, struct cbfs_payload *payload);
180 struct cbfs_payload *payload = (struct cbfs_payload *)
181 cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
183 struct cbfs_payload_segment *segment, *first_segment;
187 printk_debug("Got a payload\n");
188 first_segment = segment = &payload->segments;
189 selfboot(lb_mem, payload);
190 printk_emerg("SELFBOOT RETURNED!\n");
195 void * cbfs_load_stage(const char *name)
197 struct cbfs_stage *stage = (struct cbfs_stage *)
198 cbfs_find_file(name, CBFS_TYPE_STAGE);
199 /* this is a mess. There is no ntohll. */
200 /* for now, assume compatible byte order until we solve this. */
206 printk_info("Stage: load @ %d/%d bytes, enter @ %llx\n",
207 ntohl((u32) stage->load), ntohl(stage->memlen),
209 memset((void *) ntohl((u32) stage->load), 0, ntohl(stage->memlen));
211 if (cbfs_decompress(ntohl(stage->compression),
212 ((unsigned char *) stage) +
213 sizeof(struct cbfs_stage),
214 (void *) ntohl((u32) stage->load),
218 entry = stage->entry;
219 // return (void *) ntohl((u32) stage->entry);
220 return (void *) entry;
223 void * cbfs_get_file(const char *name)
225 return cbfs_find(name);
228 int cbfs_execute_stage(const char *name)
230 struct cbfs_stage *stage = (struct cbfs_stage *)
231 cbfs_find_file(name, CBFS_TYPE_STAGE);
236 if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
237 printk_info( "CBFS: Unable to run %s: Compressed file"
238 "Not supported for in-place execution\n", name);
242 /* FIXME: This isn't right */
243 printk_info( "CBFS: run @ %p\n", (void *) ntohl((u32) stage->entry));
244 return run_address((void *) ntohl((u32) stage->entry));
248 * run_address is passed the address of a function taking no parameters and
249 * jumps to it, returning the result.
250 * @param f the address to call as a function.
251 * returns value returned by the function.
254 int run_address(void *f)