Add new ec subdir for Embedded Controllers and common ACPI EC support
[coreboot.git] / src / lib / cbfs.c
blob690033e6b037e1dd43079a6f082c05ef290bfdb9
1 /*
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
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>
28 /**
29 * Decompression wrapper for CBFS
30 * @param algo
31 * @param src
32 * @param dst
33 * @param len
34 * @return 0 on success, -1 on failure
36 static int cbfs_decompress(int algo, void *src, void *dst, int len)
38 switch(algo) {
39 case CBFS_COMPRESS_NONE:
40 memcpy(dst, src, len);
41 return 0;
43 case CBFS_COMPRESS_LZMA:
44 if (!ulzma(src, dst)) {
45 printk(BIOS_ERR, "CBFS: LZMA decompression failed!\n");
46 return -1;
48 return 0;
50 default:
51 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", algo);
52 return -1;
56 static int cbfs_check_magic(struct cbfs_file *file)
58 return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
61 static struct cbfs_header *cbfs_master_header(void)
63 struct cbfs_header *header;
65 void *ptr = (void *)*((unsigned long *) CBFS_HEADPTR_ADDR);
66 printk(BIOS_SPEW, "Check CBFS header at %p\n", ptr);
67 header = (struct cbfs_header *) ptr;
69 printk(BIOS_SPEW, "magic is %08x\n", ntohl(header->magic));
70 if (ntohl(header->magic) != CBFS_HEADER_MAGIC) {
71 printk(BIOS_ERR, "ERROR: No valid CBFS header found!\n");
72 if (header->magic == 0xffffffff) {
73 printk(BIOS_ERR, "Maybe the ROM isn't entirely mapped yet?\n"
74 "See (and report to) http://www.coreboot.org/Infrastructure_Projects#CBFS\n");
76 return NULL;
79 printk(BIOS_SPEW, "Found CBFS header at %p\n", ptr);
80 return header;
83 struct cbfs_file *cbfs_find(const char *name)
85 struct cbfs_header *header = cbfs_master_header();
86 unsigned long offset;
88 if (header == NULL)
89 return NULL;
90 offset = 0 - ntohl(header->romsize) + ntohl(header->offset);
92 int align= ntohl(header->align);
94 while(1) {
95 struct cbfs_file *file = (struct cbfs_file *) offset;
96 if (!cbfs_check_magic(file)) return NULL;
97 printk(BIOS_SPEW, "Check %s\n", CBFS_NAME(file));
98 if (!strcmp(CBFS_NAME(file), name))
99 return file;
101 int flen = ntohl(file->len);
102 int foffset = ntohl(file->offset);
103 printk(BIOS_SPEW, "CBFS: follow chain: %p + %x + %x + align -> ", (void *)offset, foffset, flen);
105 unsigned long oldoffset = offset;
106 offset = ALIGN(offset + foffset + flen, align);
107 printk(BIOS_SPEW, "%p\n", (void *)offset);
108 if (offset <= oldoffset) return NULL;
110 if (offset < 0xFFFFFFFF - ntohl(header->romsize))
111 return NULL;
115 void *cbfs_find_file(const char *name, int type)
117 struct cbfs_file *file = cbfs_find(name);
119 if (file == NULL) {
120 printk(BIOS_INFO, "CBFS: Could not find file %s\n",
121 name);
122 return NULL;
125 if (ntohl(file->type) != type) {
126 printk(BIOS_INFO, "CBFS: File %s is of type %x instead of"
127 "type %x\n", name, file->type, type);
129 return NULL;
132 return (void *) CBFS_SUBHEADER(file);
135 static inline int tohex4(unsigned int c)
137 return (c<=9)?(c+'0'):(c-10+'a');
140 static void tohex16(unsigned int val, char* dest)
142 dest[0]=tohex4(val>>12);
143 dest[1]=tohex4((val>>8) & 0xf);
144 dest[2]=tohex4((val>>4) & 0xf);
145 dest[3]=tohex4(val & 0xf);
148 void *cbfs_load_optionrom(u16 vendor, u16 device, void * dest)
150 char name[17]="pciXXXX,XXXX.rom";
151 struct cbfs_optionrom *orom;
152 u8 *src;
154 tohex16(vendor, name+3);
155 tohex16(device, name+8);
157 orom = (struct cbfs_optionrom *)
158 cbfs_find_file(name, CBFS_TYPE_OPTIONROM);
160 if (orom == NULL)
161 return NULL;
163 /* They might have specified a dest address. If so, we can decompress.
164 * If not, there's not much hope of decompressing or relocating the rom.
165 * in the common case, the expansion rom is uncompressed, we
166 * pass 0 in for the dest, and all we have to do is find the rom and
167 * return a pointer to it.
170 /* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
171 src = ((unsigned char *) orom); // + sizeof(struct cbfs_optionrom);
173 if (! dest)
174 return src;
176 if (cbfs_decompress(ntohl(orom->compression),
177 src,
178 dest,
179 ntohl(orom->len)))
180 return NULL;
182 return dest;
185 void * cbfs_load_stage(const char *name)
187 struct cbfs_stage *stage = (struct cbfs_stage *)
188 cbfs_find_file(name, CBFS_TYPE_STAGE);
189 /* this is a mess. There is no ntohll. */
190 /* for now, assume compatible byte order until we solve this. */
191 u32 entry;
193 if (stage == NULL)
194 return (void *) -1;
196 printk(BIOS_INFO, "Stage: loading %s @ 0x%x (%d bytes), entry @ 0x%llx\n",
197 name,
198 (u32) stage->load, stage->memlen,
199 stage->entry);
200 memset((void *) (u32) stage->load, 0, stage->memlen);
202 if (cbfs_decompress(stage->compression,
203 ((unsigned char *) stage) +
204 sizeof(struct cbfs_stage),
205 (void *) (u32) stage->load,
206 stage->len))
207 return (void *) -1;
209 printk(BIOS_DEBUG, "Stage: done loading.\n");
211 entry = stage->entry;
212 // entry = ntohl((u32) stage->entry);
214 return (void *) entry;
217 int cbfs_execute_stage(const char *name)
219 struct cbfs_stage *stage = (struct cbfs_stage *)
220 cbfs_find_file(name, CBFS_TYPE_STAGE);
222 if (stage == NULL)
223 return 1;
225 if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
226 printk(BIOS_INFO, "CBFS: Unable to run %s: Compressed file"
227 "Not supported for in-place execution\n", name);
228 return 1;
231 /* FIXME: This isn't right */
232 printk(BIOS_INFO, "CBFS: run @ %p\n", (void *) ntohl((u32) stage->entry));
233 return run_address((void *) ntohl((u32) stage->entry));
237 * run_address is passed the address of a function taking no parameters and
238 * jumps to it, returning the result.
239 * @param f the address to call as a function.
240 * @return value returned by the function.
243 int run_address(void *f)
245 int (*v) (void);
246 v = f;
247 return v();