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