Add constants for fast path resume copying
[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 // use CBFS core
28 #ifndef __SMM__
29 #define CBFS_CORE_WITH_LZMA
30 #endif
31 #define phys_to_virt(x) (void*)(x)
32 #define virt_to_phys(x) (uint32_t)(x)
33 #define ERROR(x...) printk(BIOS_ERR, x)
34 #define LOG(x...) printk(BIOS_INFO, x)
35 // FIXME: romstart/romend are fine on x86, but not on ARM
36 #define romstart() 0xffffffff
37 #define romend() 0
38
39 #include "cbfs_core.c"
40
41 #ifndef __SMM__
42 static inline int tohex4(unsigned int c)
43 {
44         return (c<=9)?(c+'0'):(c-10+'a');
45 }
46
47 static void tohex16(unsigned int val, char* dest)
48 {
49         dest[0]=tohex4(val>>12);
50         dest[1]=tohex4((val>>8) & 0xf);
51         dest[2]=tohex4((val>>4) & 0xf);
52         dest[3]=tohex4(val & 0xf);
53 }
54
55 void *cbfs_load_optionrom(u16 vendor, u16 device, void * dest)
56 {
57         char name[17]="pciXXXX,XXXX.rom";
58         struct cbfs_optionrom *orom;
59         u8 *src;
60
61         tohex16(vendor, name+3);
62         tohex16(device, name+8);
63
64         orom = (struct cbfs_optionrom *)
65                 cbfs_find_file(name, CBFS_TYPE_OPTIONROM);
66
67         if (orom == NULL)
68                 return NULL;
69
70         /* They might have specified a dest address. If so, we can decompress.
71          * If not, there's not much hope of decompressing or relocating the rom.
72          * in the common case, the expansion rom is uncompressed, we
73          * pass 0 in for the dest, and all we have to do is find the rom and
74          * return a pointer to it.
75          */
76
77         /* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */
78         src = ((unsigned char *) orom); // + sizeof(struct cbfs_optionrom);
79
80         if (! dest)
81                 return src;
82
83         if (cbfs_decompress(ntohl(orom->compression),
84                              src,
85                              dest,
86                              ntohl(orom->len)))
87                 return NULL;
88
89         return dest;
90 }
91
92 void * cbfs_load_stage(const char *name)
93 {
94         struct cbfs_stage *stage = (struct cbfs_stage *)
95                 cbfs_find_file(name, CBFS_TYPE_STAGE);
96         /* this is a mess. There is no ntohll. */
97         /* for now, assume compatible byte order until we solve this. */
98         u32 entry;
99
100         if (stage == NULL)
101                 return (void *) -1;
102
103         printk(BIOS_INFO, "Stage: loading %s @ 0x%x (%d bytes), entry @ 0x%llx\n",
104                         name,
105                         (u32) stage->load, stage->memlen,
106                         stage->entry);
107         memset((void *) (u32) stage->load, 0, stage->memlen);
108
109         if (cbfs_decompress(stage->compression,
110                              ((unsigned char *) stage) +
111                              sizeof(struct cbfs_stage),
112                              (void *) (u32) stage->load,
113                              stage->len))
114                 return (void *) -1;
115
116         printk(BIOS_DEBUG, "Stage: done loading.\n");
117
118         entry = stage->entry;
119         // entry = ntohll(stage->entry);
120
121         return (void *) entry;
122 }
123
124 int cbfs_execute_stage(const char *name)
125 {
126         struct cbfs_stage *stage = (struct cbfs_stage *)
127                 cbfs_find_file(name, CBFS_TYPE_STAGE);
128
129         if (stage == NULL)
130                 return 1;
131
132         if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
133                 printk(BIOS_INFO,  "CBFS:  Unable to run %s:  Compressed file"
134                        "Not supported for in-place execution\n", name);
135                 return 1;
136         }
137
138         /* FIXME: This isn't right */
139         printk(BIOS_INFO,  "CBFS: run @ %p\n", (void *) ntohl((u32) stage->entry));
140         return run_address((void *) (intptr_t)ntohll(stage->entry));
141 }
142
143 /**
144  * run_address is passed the address of a function taking no parameters and
145  * jumps to it, returning the result.
146  * @param f the address to call as a function.
147  * @return value returned by the function.
148  */
149
150 int run_address(void *f)
151 {
152         int (*v) (void);
153         v = f;
154         return v();
155 }
156 #endif