This fixes a bug in romfs code; see comment. If we add the pci rom
[coreboot.git] / src / lib / romfs.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008, Jordan Crouse <jordan@cosmicpenguin.net>
5  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <types.h>
21 #include <string.h>
22 #include <console/console.h>
23 #include <boot/coreboot_tables.h>
24 #include <romfs.h>
25
26 #ifndef CONFIG_BIG_ENDIAN
27 #define ntohl(x) ( ((x&0xff)<<24) | ((x&0xff00)<<8) | \
28                 ((x&0xff0000) >> 8) | ((x&0xff000000) >> 24) )
29 #else
30 #define ntohl(x) (x)
31 #endif
32
33 int run_address(void *f);
34
35 int romfs_decompress(int algo, void *src, void *dst, int len)
36 {
37         switch(algo) {
38         case ROMFS_COMPRESS_NONE:
39                 memcpy(dst, src, len);
40                 return 0;
41
42 #ifdef CONFIG_COMPRESSION_LZMA
43
44         case ROMFS_COMPRESS_LZMA: {
45                 unsigned long ulzma(unsigned char *src, unsigned char *dst);
46                 ulzma(src, dst);
47         }
48                 return 0;
49 #endif
50
51 #ifdef CONFIG_COMPRESSION_NRV2B
52         case ROMFS_COMPRESS_NRV2B: {
53                 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
54                 unsigned long tmp;
55
56                 unrv2b(src, dst, &tmp);
57         }
58                 return 0;
59 #endif
60         default:
61                 printk_info( "ROMFS:  Unknown compression type %d\n",
62                        algo);
63                 return -1;
64         }
65 }
66
67 int romfs_check_magic(struct romfs_file *file)
68 {
69         return !strcmp(file->magic, ROMFS_FILE_MAGIC) ? 1 : 0;
70 }
71
72 struct romfs_header *romfs_master_header(void)
73 {
74         struct romfs_header *header;
75
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;
79
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");
83                 return NULL;
84         }
85
86         printk_debug("Found ROMFS header at %p\n", ptr);
87         return header;
88 }
89
90 struct romfs_file *romfs_find(const char *name)
91 {
92         struct romfs_header *header = romfs_master_header();
93         unsigned long offset;
94
95         if (header == NULL)
96                 return NULL;
97         offset = 0 - ntohl(header->romsize) + ntohl(header->offset);
98
99         while(1) {
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))
104                         return file;
105
106                 offset += ntohl(header->align);
107
108                 if (offset < 0xFFFFFFFF - ntohl(header->romsize))
109                         return NULL;
110         }
111 }
112
113 struct romfs_stage *romfs_find_file(const char *name, int type)
114 {
115         struct romfs_file *file = romfs_find(name);
116
117         if (file == NULL) {
118                 printk_info( "ROMFS:  Could not find file %s\n",
119                        name);
120                 return NULL;
121         }
122
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);
126
127                 return NULL;
128         }
129
130         return (void *) ROMFS_SUBHEADER(file);
131 }
132
133 void *romfs_load_optionrom(u16 vendor, u16 device, void * dest)
134 {
135         char name[17];
136         struct romfs_optionrom *orom;
137         u8 *src;
138
139         sprintf(name,"pci%04x,%04x.rom", vendor, device);
140
141         orom = (struct romfs_optionrom *)
142                 romfs_find_file(name, ROMFS_TYPE_OPTIONROM);
143
144         if (orom == NULL)
145                 return NULL;
146
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. 
152          */
153
154         /* BUG: the romtool is (not yet) including a romfs_optionrom header */
155         src = ((unsigned char *) orom); // + sizeof(struct romfs_optionrom);
156
157         if (! dest)
158                 return src;
159
160         if (romfs_decompress(ntohl(orom->compression),
161                              src,
162                              dest,
163                              ntohl(orom->len)))
164                 return NULL;
165
166         return dest;
167 }
168
169 void * romfs_load_payload(struct lb_memory *lb_mem, const char *name)
170 {
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);
174
175         struct romfs_payload_segment *segment, *first_segment;
176
177         if (payload == NULL)
178                 return (void *) -1;
179         printk_debug("Got a payload\n");
180         first_segment = segment = &payload->segments;
181         selfboot(lb_mem, payload);
182         printk_emerg("SELFBOOT RETURNED!\n");
183
184         return (void *) -1;
185 }
186
187 void * romfs_load_stage(const char *name)
188 {
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. */
193         u32 entry;
194
195         if (stage == NULL)
196                 return (void *) -1;
197
198         printk_info("Stage: load @ %d/%d bytes, enter @ %llx\n", 
199                         ntohl((u32) stage->load), ntohl(stage->memlen), 
200                         stage->entry);
201         memset((void *) ntohl((u32) stage->load), 0, ntohl(stage->memlen));
202
203         if (romfs_decompress(ntohl(stage->compression),
204                              ((unsigned char *) stage) +
205                              sizeof(struct romfs_stage),
206                              (void *) ntohl((u32) stage->load),
207                              ntohl(stage->len)))
208                 return (void *) -1;
209
210         entry = stage->entry;
211 //      return (void *) ntohl((u32) stage->entry);
212         return (void *) entry;
213 }
214
215 void * romfs_get_file(const char *name)
216 {
217         return romfs_find(name);
218 }
219
220 int romfs_execute_stage(const char *name)
221 {
222         struct romfs_stage *stage = (struct romfs_stage *)
223                 romfs_find_file(name, ROMFS_TYPE_STAGE);
224
225         if (stage == NULL)
226                 return 1;
227
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);
231                 return 1;
232         }
233
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));
237 }
238
239 /**
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. 
244  *      */
245
246 int run_address(void *f)
247 {
248         int (*v) (void);
249         v = f;
250         return v();
251 }
252