4c025d14729b19d949f3628664e1e675c897eead
[coreboot.git] / src / lib / cbfs.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 <cbfs.h>
24
25 #ifndef CONFIG_BIG_ENDIAN
26 #define ntohl(x) ( ((x&0xff)<<24) | ((x&0xff00)<<8) | \
27                 ((x&0xff0000) >> 8) | ((x&0xff000000) >> 24) )
28 #else
29 #define ntohl(x) (x)
30 #endif
31
32 int run_address(void *f);
33
34 int cbfs_decompress(int algo, void *src, void *dst, int len)
35 {
36         switch(algo) {
37         case CBFS_COMPRESS_NONE:
38                 memcpy(dst, src, len);
39                 return 0;
40
41 #if CONFIG_COMPRESSED_PAYLOAD_LZMA==1
42
43         case CBFS_COMPRESS_LZMA: {
44                 unsigned long ulzma(unsigned char *src, unsigned char *dst);
45                 ulzma(src, dst);
46         }
47                 return 0;
48 #endif
49
50 #if CONFIG_COMPRESSED_PAYLOAD_NRV2B==1
51         case CBFS_COMPRESS_NRV2B: {
52                 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
53                 unsigned long tmp;
54
55                 unrv2b(src, dst, &tmp);
56         }
57                 return 0;
58 #endif
59         default:
60                 printk_info( "CBFS:  Unknown compression type %d\n",
61                        algo);
62                 return -1;
63         }
64 }
65
66 int cbfs_check_magic(struct cbfs_file *file)
67 {
68         return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
69 }
70
71 struct cbfs_header *cbfs_master_header(void)
72 {
73         struct cbfs_header *header;
74
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;
78
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");
82                 return NULL;
83         }
84
85         printk_debug("Found CBFS header at %p\n", ptr);
86         return header;
87 }
88
89 struct cbfs_file *cbfs_find(const char *name)
90 {
91         struct cbfs_header *header = cbfs_master_header();
92         unsigned long offset;
93
94         if (header == NULL)
95                 return NULL;
96         offset = 0 - ntohl(header->romsize) + ntohl(header->offset);
97
98         int align= ntohl(header->align);
99
100         while(1) {
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))
105                         return file;
106
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);
110
111                 unsigned long oldoffset = offset;
112                 offset = ALIGN(offset + foffset + flen, align);
113                 printk_spew("%p\n", offset);
114                 if (offset <= oldoffset) return NULL;
115
116                 if (offset < 0xFFFFFFFF - ntohl(header->romsize))
117                         return NULL;
118         }
119 }
120
121 struct cbfs_stage *cbfs_find_file(const char *name, int type)
122 {
123         struct cbfs_file *file = cbfs_find(name);
124
125         if (file == NULL) {
126                 printk_info( "CBFS:  Could not find file %s\n",
127                        name);
128                 return NULL;
129         }
130
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);
134
135                 return NULL;
136         }
137
138         return (void *) CBFS_SUBHEADER(file);
139 }
140
141 void *cbfs_load_optionrom(u16 vendor, u16 device, void * dest)
142 {
143         char name[17];
144         struct cbfs_optionrom *orom;
145         u8 *src;
146
147         sprintf(name,"pci%04x,%04x.rom", vendor, device);
148
149         orom = (struct cbfs_optionrom *)
150                 cbfs_find_file(name, CBFS_TYPE_OPTIONROM);
151
152         if (orom == NULL)
153                 return NULL;
154
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. 
160          */
161
162         /* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
163         src = ((unsigned char *) orom); // + sizeof(struct cbfs_optionrom);
164
165         if (! dest)
166                 return src;
167
168         if (cbfs_decompress(ntohl(orom->compression),
169                              src,
170                              dest,
171                              ntohl(orom->len)))
172                 return NULL;
173
174         return dest;
175 }
176
177 void * cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
178 {
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);
182
183         struct cbfs_payload_segment *segment, *first_segment;
184
185         if (payload == NULL)
186                 return (void *) -1;
187         printk_debug("Got a payload\n");
188         first_segment = segment = &payload->segments;
189         selfboot(lb_mem, payload);
190         printk_emerg("SELFBOOT RETURNED!\n");
191
192         return (void *) -1;
193 }
194
195 void * cbfs_load_stage(const char *name)
196 {
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. */
201         u32 entry;
202
203         if (stage == NULL)
204                 return (void *) -1;
205
206         printk_info("Stage: load @ %d/%d bytes, enter @ %llx\n", 
207                         ntohl((u32) stage->load), ntohl(stage->memlen), 
208                         stage->entry);
209         memset((void *) ntohl((u32) stage->load), 0, ntohl(stage->memlen));
210
211         if (cbfs_decompress(ntohl(stage->compression),
212                              ((unsigned char *) stage) +
213                              sizeof(struct cbfs_stage),
214                              (void *) ntohl((u32) stage->load),
215                              ntohl(stage->len)))
216                 return (void *) -1;
217
218         entry = stage->entry;
219 //      return (void *) ntohl((u32) stage->entry);
220         return (void *) entry;
221 }
222
223 void * cbfs_get_file(const char *name)
224 {
225         return cbfs_find(name);
226 }
227
228 int cbfs_execute_stage(const char *name)
229 {
230         struct cbfs_stage *stage = (struct cbfs_stage *)
231                 cbfs_find_file(name, CBFS_TYPE_STAGE);
232
233         if (stage == NULL)
234                 return 1;
235
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);
239                 return 1;
240         }
241
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));
245 }
246
247 /**
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. 
252  */
253
254 int run_address(void *f)
255 {
256         int (*v) (void);
257         v = f;
258         return v();
259 }
260