Help track down enable_rom issues in CBFS. If the magic
[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 #include <lib.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 cbfs_decompress(int algo, void *src, void *dst, int len)
34 {
35         switch(algo) {
36         case CBFS_COMPRESS_NONE:
37                 memcpy(dst, src, len);
38                 return 0;
39
40         case CBFS_COMPRESS_LZMA:
41                 if (!ulzma(src, dst)) {
42                         printk_err("CBFS: LZMA decompression failed!\n");
43                         return -1;
44                 }
45                 return 0;
46
47         default:
48                 printk_info( "CBFS:  Unknown compression type %d\n", algo);
49                 return -1;
50         }
51 }
52
53 int cbfs_check_magic(struct cbfs_file *file)
54 {
55         return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
56 }
57
58 struct cbfs_header *cbfs_master_header(void)
59 {
60         struct cbfs_header *header;
61
62         void *ptr = (void *)*((unsigned long *) CBFS_HEADPTR_ADDR);
63         printk_spew("Check CBFS header at %p\n", ptr);
64         header = (struct cbfs_header *) ptr;
65
66         printk_spew("magic is %08x\n", ntohl(header->magic));
67         if (ntohl(header->magic) != CBFS_HEADER_MAGIC) {
68                 printk_err("ERROR: No valid CBFS header found!\n");
69                 if (header->magic == 0xffffffff) {
70                         printk_err("Maybe the ROM isn't entirely mapped yet?\n"
71                                 "See (and report to) http://www.coreboot.org/Infrastructure_Projects#CBFS\n");
72                 }
73                 return NULL;
74         }
75
76         printk_spew("Found CBFS header at %p\n", ptr);
77         return header;
78 }
79
80 struct cbfs_file *cbfs_find(const char *name)
81 {
82         struct cbfs_header *header = cbfs_master_header();
83         unsigned long offset;
84
85         if (header == NULL)
86                 return NULL;
87         offset = 0 - ntohl(header->romsize) + ntohl(header->offset);
88
89         int align= ntohl(header->align);
90
91         while(1) {
92                 struct cbfs_file *file = (struct cbfs_file *) offset;
93                 if (!cbfs_check_magic(file)) return NULL;
94                 printk_spew("Check %s\n", CBFS_NAME(file));
95                 if (!strcmp(CBFS_NAME(file), name))
96                         return file;
97
98                 int flen = ntohl(file->len);
99                 int foffset = ntohl(file->offset);
100                 printk_spew("CBFS: follow chain: %p + %x + %x + align -> ", (void *)offset, foffset, flen);
101
102                 unsigned long oldoffset = offset;
103                 offset = ALIGN(offset + foffset + flen, align);
104                 printk_spew("%p\n", (void *)offset);
105                 if (offset <= oldoffset) return NULL;
106
107                 if (offset < 0xFFFFFFFF - ntohl(header->romsize))
108                         return NULL;
109         }
110 }
111
112 struct cbfs_stage *cbfs_find_file(const char *name, int type)
113 {
114         struct cbfs_file *file = cbfs_find(name);
115
116         if (file == NULL) {
117                 printk_info( "CBFS:  Could not find file %s\n",
118                        name);
119                 return NULL;
120         }
121
122         if (ntohl(file->type) != type) {
123                 printk_info( "CBFS:  File %s is of type %x instead of"
124                        "type %x\n", name, file->type, type);
125
126                 return NULL;
127         }
128
129         return (void *) CBFS_SUBHEADER(file);
130 }
131
132 static int tohex4(unsigned int c)
133 {
134         return (c<=9)?(c+'0'):(c-10+'a');
135 }
136
137 static void tohex16(unsigned int val, char* dest)
138 {
139         dest[0]=tohex4(val>>12);
140         dest[1]=tohex4((val>>8) & 0xf);
141         dest[2]=tohex4((val>>4) & 0xf);
142         dest[3]=tohex4(val & 0xf);
143 }
144
145 void *cbfs_load_optionrom(u16 vendor, u16 device, void * dest)
146 {
147         char name[17]="pciXXXX,XXXX.rom";
148         struct cbfs_optionrom *orom;
149         u8 *src;
150
151         tohex16(vendor, name+3);
152         tohex16(device, name+8);
153
154         orom = (struct cbfs_optionrom *)
155                 cbfs_find_file(name, CBFS_TYPE_OPTIONROM);
156
157         if (orom == NULL)
158                 return NULL;
159
160         /* They might have specified a dest address. If so, we can decompress. 
161          * If not, there's not much hope of decompressing or relocating the rom.
162          * in the common case, the expansion rom is uncompressed, we
163          * pass 0 in for the dest, and all we have to do is find the rom and 
164          * return a pointer to it. 
165          */
166
167         /* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
168         src = ((unsigned char *) orom); // + sizeof(struct cbfs_optionrom);
169
170         if (! dest)
171                 return src;
172
173         if (cbfs_decompress(ntohl(orom->compression),
174                              src,
175                              dest,
176                              ntohl(orom->len)))
177                 return NULL;
178
179         return dest;
180 }
181
182 void * cbfs_load_stage(const char *name)
183 {
184         struct cbfs_stage *stage = (struct cbfs_stage *)
185                 cbfs_find_file(name, CBFS_TYPE_STAGE);
186         /* this is a mess. There is no ntohll. */
187         /* for now, assume compatible byte order until we solve this. */
188         u32 entry;
189
190         if (stage == NULL)
191                 return (void *) -1;
192
193         printk_info("Stage: loading %s @ 0x%x (%d bytes), entry @ 0x%llx\n", 
194                         name,
195                         (u32) stage->load, stage->memlen, 
196                         stage->entry);
197         memset((void *) (u32) stage->load, 0, stage->memlen);
198
199         if (cbfs_decompress(stage->compression,
200                              ((unsigned char *) stage) +
201                              sizeof(struct cbfs_stage),
202                              (void *) (u32) stage->load,
203                              stage->len))
204                 return (void *) -1;
205
206         printk_debug("Stage: done loading.\n");
207
208         entry = stage->entry;
209         // entry = ntohl((u32) stage->entry);
210
211         return (void *) entry;
212 }
213
214 void * cbfs_get_file(const char *name)
215 {
216         return cbfs_find(name);
217 }
218
219 int cbfs_execute_stage(const char *name)
220 {
221         struct cbfs_stage *stage = (struct cbfs_stage *)
222                 cbfs_find_file(name, CBFS_TYPE_STAGE);
223
224         if (stage == NULL)
225                 return 1;
226
227         if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
228                 printk_info( "CBFS:  Unable to run %s:  Compressed file"
229                        "Not supported for in-place execution\n", name);
230                 return 1;
231         }
232
233         /* FIXME: This isn't right */
234         printk_info( "CBFS: run @ %p\n", (void *) ntohl((u32) stage->entry));
235         return run_address((void *) ntohl((u32) stage->entry));
236 }
237
238 /**
239  * run_address is passed the address of a function taking no parameters and
240  * jumps to it, returning the result. 
241  * @param f the address to call as a function. 
242  * returns value returned by the function. 
243  */
244
245 int run_address(void *f)
246 {
247         int (*v) (void);
248         v = f;
249         return v();
250 }
251