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