libpayload: export get_cbfs_header()
[coreboot.git] / payloads / libpayload / libcbfs / cbfs_core.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2011 secunet Security Networks AG
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /* The CBFS core requires a couple of #defines or functions to adapt it to the target environment:
31  *
32  * CBFS_CORE_WITH_LZMA (must be #define)
33  *      if defined, ulzma() must exist for decompression of data streams
34  *
35  * phys_to_virt(x), virt_to_phys(x)
36  *      translate physical addresses to virtual and vice versa
37  *      can be idempotent if no mapping is necessary.
38  *
39  * ERROR(x...)
40  *      print an error message x (in printf format)
41  *
42  * LOG(x...)
43  *      print a debug message x (in printf format)
44  *
45  * romstart()
46  *      returns the start address of the ROM image, or 0xffffffff if ROM is
47  *      top-aligned. This is a physical address.
48  *
49  * romend()
50  *      returns the highest address of the ROM image + 1, for use if
51  *      romstart() == 0xffffffff. This is a physical address.
52  */
53
54 #include <cbfs_core.h>
55
56
57 /* returns pointer to master header or 0xffffffff if not found */
58 struct cbfs_header *get_cbfs_header(void)
59 {
60         struct cbfs_header *header;
61
62         /* find header */
63         if (romstart() == 0xffffffff) {
64                 header = (struct cbfs_header*)phys_to_virt(*(uint32_t*)phys_to_virt(romend() + CBFS_HEADPTR_ADDR));
65         } else {
66                 // FIXME: where's the master header on ARM (our current bottom-aligned platform)?
67                 header = NULL;
68         }
69         if (CBFS_HEADER_MAGIC != ntohl(header->magic)) {
70                 ERROR("Could not find valid CBFS master header at %p: %x vs %x.\n", header, CBFS_HEADER_MAGIC, ntohl(header->magic));
71                 if (header->magic == 0xffffffff) {
72                         ERROR("Maybe ROM is not mapped properly?\n");
73                 }
74                 return (void*)0xffffffff;
75         }
76         return header;
77 }
78
79 // by must be power-of-two
80 #define CBFS_ALIGN(val, by) (typeof(val))((uint32_t)(val + by - 1) & (uint32_t)~(by - 1))
81 #define CBFS_ALIGN_UP(val, by) CBFS_ALIGN(val + 1, by)
82
83 /* public API starts here*/
84 struct cbfs_file *cbfs_find(const char *name)
85 {
86         struct cbfs_header *header = get_cbfs_header();
87         if (header == (void*)0xffffffff) return NULL;
88
89         LOG("Searching for %s\n", name);
90
91         void *data, *dataend, *origdata;
92         /* find first entry */
93         if (romstart() == 0xffffffff) {
94                 data = (void*)phys_to_virt(romend()) - ntohl(header->romsize) + ntohl(header->offset);
95                 dataend = (void*)phys_to_virt(romend());
96         } else {
97                 data = (void*)phys_to_virt(romstart()) + ntohl(header->offset);
98                 dataend = (void*)phys_to_virt(romstart()) + ntohl(header->romsize);
99         }
100
101         int align = ntohl(header->align);
102
103         origdata = data;
104         while ((data < (dataend - 1)) && (data >= origdata)) {
105                 struct cbfs_file *file = data;
106                 if (memcmp(CBFS_FILE_MAGIC, file->magic, strlen(CBFS_FILE_MAGIC)) != 0) {
107                         // no file header found. corruption?
108                         // proceed in aligned steps to resynchronize
109                         LOG("No file header found at %p, searching for header\n", data);
110                         data = phys_to_virt(CBFS_ALIGN_UP(virt_to_phys(data), align));
111                         continue;
112                 }
113                 LOG("Check %s\n", CBFS_NAME(file));
114                 if (strcmp(CBFS_NAME(file), name) == 0) {
115                         return file;
116                 }
117                 void *olddata = data;
118                 data = phys_to_virt(CBFS_ALIGN(virt_to_phys(data) + ntohl(file->len) + ntohl(file->offset), align));
119                 if (olddata > data) {
120                         LOG("Something is wrong here. File chain moved from %p to %p\n", olddata, data);
121                         return NULL;
122                 }
123         }
124         return NULL;
125 }
126
127 void *cbfs_get_file(const char *name)
128 {
129         struct cbfs_file *file = cbfs_find(name);
130
131         if (file == NULL) {
132                 ERROR("Could not find file '%s'.\n", name);
133                 return NULL;
134         }
135
136         return (void*)CBFS_SUBHEADER(file);
137 }
138
139 void *cbfs_find_file(const char *name, int type)
140 {
141         struct cbfs_file *file = cbfs_find(name);
142
143         if (file == NULL) {
144                 ERROR("Could not find file '%s'.\n", name);
145                 return NULL;
146         }
147
148         if (ntohl(file->type) != type) {
149                 ERROR("File '%s' is of type %x, but we requested %x.\n", name, ntohl(file->type), type);
150                 return NULL;
151         }
152
153         return (void*)CBFS_SUBHEADER(file);
154 }
155
156 int cbfs_decompress(int algo, void *src, void *dst, int len)
157 {
158         switch (algo) {
159                 case CBFS_COMPRESS_NONE:
160                         memcpy(dst, src, len);
161                         return 0;
162 #ifdef CBFS_CORE_WITH_LZMA
163                 case CBFS_COMPRESS_LZMA:
164                         if (ulzma(src, dst) != 0) {
165                                 return 0;
166                         }
167                         return -1;
168 #endif
169                 default:
170                         ERROR("tried to decompress %d bytes with algorithm #%x, but that algorithm id is unsupported.\n", len, algo);
171                         return -1;
172         }
173 }
174