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>
23 #include <boot/coreboot_tables.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 run_address(void *f);
35 int romfs_decompress(int algo, void *src, void *dst, int len)
38 case ROMFS_COMPRESS_NONE:
39 memcpy(dst, src, len);
42 #ifdef CONFIG_COMPRESSION_LZMA
44 case ROMFS_COMPRESS_LZMA: {
45 unsigned long ulzma(unsigned char *src, unsigned char *dst);
51 #ifdef CONFIG_COMPRESSION_NRV2B
52 case ROMFS_COMPRESS_NRV2B: {
53 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
56 unrv2b(src, dst, &tmp);
61 printk_info( "ROMFS: Unknown compression type %d\n",
67 int romfs_check_magic(struct romfs_file *file)
69 return !strcmp(file->magic, ROMFS_FILE_MAGIC) ? 1 : 0;
72 struct romfs_header *romfs_master_header(void)
74 struct romfs_header *header;
76 void *ptr = (void *)*((unsigned long *) ROMFS_HEADPTR_ADDR);
77 printk_debug("Check ROMFS header at %p\n", ptr);
78 header = (struct romfs_header *) ptr;
80 printk_debug("magic is %08x\n", ntohl(header->magic));
81 if (ntohl(header->magic) != ROMFS_HEADER_MAGIC) {
82 printk_err("NO ROMFS HEADER\n");
86 printk_debug("Found ROMFS header at %p\n", ptr);
90 struct romfs_file *romfs_find(const char *name)
92 struct romfs_header *header = romfs_master_header();
97 offset = 0 - ntohl(header->romsize) + ntohl(header->offset);
100 struct romfs_file *file = (struct romfs_file *) offset;
101 if (romfs_check_magic(file)) printk_info("Check %s\n", ROMFS_NAME(file));
102 if (romfs_check_magic(file) &&
103 !strcmp(ROMFS_NAME(file), name))
106 offset += ntohl(header->align);
108 if (offset < 0xFFFFFFFF - ntohl(header->romsize))
113 struct romfs_stage *romfs_find_file(const char *name, int type)
115 struct romfs_file *file = romfs_find(name);
118 printk_info( "ROMFS: Could not find file %s\n",
123 if (ntohl(file->type) != type) {
124 printk_info( "ROMFS: File %s is of type %x instead of"
125 "type %x\n", name, file->type, type);
130 return (void *) ROMFS_SUBHEADER(file);
133 void *romfs_load_optionrom(u16 vendor, u16 device, void * dest)
136 struct romfs_optionrom *orom;
139 sprintf(name,"pci%04x,%04x.rom", vendor, device);
141 orom = (struct romfs_optionrom *)
142 romfs_find_file(name, ROMFS_TYPE_OPTIONROM);
147 /* They might have specified a dest address. If so, we can decompress.
148 * If not, there's not much hope of decompressing or relocating the rom.
149 * in the common case, the expansion rom is uncompressed, we
150 * pass 0 in for the dest, and all we have to do is find the rom and
151 * return a pointer to it.
154 /* BUG: the romtool is (not yet) including a romfs_optionrom header */
155 src = ((unsigned char *) orom); // + sizeof(struct romfs_optionrom);
160 if (romfs_decompress(ntohl(orom->compression),
169 void * romfs_load_payload(struct lb_memory *lb_mem, const char *name)
171 int selfboot(struct lb_memory *mem, struct romfs_payload *payload);
172 struct romfs_payload *payload = (struct romfs_payload *)
173 romfs_find_file(name, ROMFS_TYPE_PAYLOAD);
175 struct romfs_payload_segment *segment, *first_segment;
179 printk_debug("Got a payload\n");
180 first_segment = segment = &payload->segments;
181 selfboot(lb_mem, payload);
182 printk_emerg("SELFBOOT RETURNED!\n");
187 void * romfs_load_stage(const char *name)
189 struct romfs_stage *stage = (struct romfs_stage *)
190 romfs_find_file(name, ROMFS_TYPE_STAGE);
191 /* this is a mess. There is no ntohll. */
192 /* for now, assume compatible byte order until we solve this. */
198 printk_info("Stage: load @ %d/%d bytes, enter @ %llx\n",
199 ntohl((u32) stage->load), ntohl(stage->memlen),
201 memset((void *) ntohl((u32) stage->load), 0, ntohl(stage->memlen));
203 if (romfs_decompress(ntohl(stage->compression),
204 ((unsigned char *) stage) +
205 sizeof(struct romfs_stage),
206 (void *) ntohl((u32) stage->load),
210 entry = stage->entry;
211 // return (void *) ntohl((u32) stage->entry);
212 return (void *) entry;
215 void * romfs_get_file(const char *name)
217 return romfs_find(name);
220 int romfs_execute_stage(const char *name)
222 struct romfs_stage *stage = (struct romfs_stage *)
223 romfs_find_file(name, ROMFS_TYPE_STAGE);
228 if (ntohl(stage->compression) != ROMFS_COMPRESS_NONE) {
229 printk_info( "ROMFS: Unable to run %s: Compressed file"
230 "Not supported for in-place execution\n", name);
234 /* FIXME: This isn't right */
235 printk_info( "ROMFS: run @ %p\n", (void *) ntohl((u32) stage->entry));
236 return run_address((void *) ntohl((u32) stage->entry));
240 * * run_address is passed the address of a function taking no parameters and
241 * * jumps to it, returning the result.
242 * * @param f the address to call as a function.
243 * * returns value returned by the function.
246 int run_address(void *f)