blob: 80d66ca3eb36b5cb1bcdeae0bebaf7cfe1879656 [file] [log] [blame]
Patrick Georgib8835152011-07-21 15:11:40 +02001/*
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +08002 * This file is part of the coreboot project.
Patrick Georgib8835152011-07-21 15:11:40 +02003 *
4 * Copyright (C) 2011 secunet Security Networks AG
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +08005 * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
Patrick Georgib8835152011-07-21 15:11:40 +02006 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080031/* The CBFS core requires a couple of #defines or functions to adapt it to the
32 * target environment:
Patrick Georgib8835152011-07-21 15:11:40 +020033 *
34 * CBFS_CORE_WITH_LZMA (must be #define)
35 * if defined, ulzma() must exist for decompression of data streams
36 *
Patrick Georgib8835152011-07-21 15:11:40 +020037 * ERROR(x...)
38 * print an error message x (in printf format)
39 *
40 * LOG(x...)
Stefan Reinauerfe422182012-05-02 16:33:18 -070041 * print a message x (in printf format)
42 *
43 * DEBUG(x...)
Patrick Georgib8835152011-07-21 15:11:40 +020044 * print a debug message x (in printf format)
45 *
Patrick Georgib8835152011-07-21 15:11:40 +020046 */
47
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080048#include <cbfs.h>
49#include <string.h>
Patrick Georgib8835152011-07-21 15:11:40 +020050
Edward O'Callaghancdabc882014-11-11 12:22:04 +110051#include "cbfs_core.h"
52
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080053/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS
54 * on failure */
55const struct cbfs_header *cbfs_get_header(struct cbfs_media *media)
Patrick Georgib8835152011-07-21 15:11:40 +020056{
Julius Wernerefcee762014-11-10 13:14:24 -080057 size_t offset;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080058 const struct cbfs_header *header;
59 struct cbfs_media default_media;
Patrick Georgib8835152011-07-21 15:11:40 +020060
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080061 if (media == CBFS_DEFAULT_MEDIA) {
62 media = &default_media;
63 if (init_default_cbfs_media(media) != 0) {
Hung-Te Lin58fd5e12013-02-20 15:43:47 +080064 ERROR("Failed to initialize default media.\n");
65 return CBFS_HEADER_INVALID_ADDRESS;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080066 }
Patrick Georgib8835152011-07-21 15:11:40 +020067 }
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080068 media->open(media);
Julius Wernerefcee762014-11-10 13:14:24 -080069
70 /* TODO: allow negative offsets from the end of the CBFS image at media
71 * layer (like libpayload) so we can combine these two cases. */
72 if (IS_ENABLED(CONFIG_ARCH_X86)) {
73 offset = *(int32_t *)(uintptr_t)0xfffffffc;
74 header = media->map(media, offset, sizeof(*header));
75 } else {
76 int32_t rel_offset;
77 if (!media->read(media, &rel_offset, CONFIG_CBFS_SIZE -
78 sizeof(int32_t), sizeof(int32_t))) {
79 ERROR("Could not read CBFS master header offset!\n");
80 return CBFS_HEADER_INVALID_ADDRESS;
81 }
82 offset = CONFIG_CBFS_SIZE + rel_offset;
83 header = media->map(media, offset, sizeof(*header));
84 }
85 DEBUG("CBFS header offset: 0x%zx/0x%x\n", offset, CONFIG_ROM_SIZE);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080086 media->close(media);
87
88 if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
Julius Wernerefcee762014-11-10 13:14:24 -080089 ERROR("Failed to load CBFS header from 0x%zx\n", offset);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080090 return CBFS_HEADER_INVALID_ADDRESS;
91 }
92
Patrick Georgib8835152011-07-21 15:11:40 +020093 if (CBFS_HEADER_MAGIC != ntohl(header->magic)) {
Julius Wernerefcee762014-11-10 13:14:24 -080094 ERROR("Could not find valid CBFS master header at %#zx: "
95 "magic %#.8x vs %#.8x.\n", offset, CBFS_HEADER_MAGIC,
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080096 ntohl(header->magic));
Patrick Georgib8835152011-07-21 15:11:40 +020097 if (header->magic == 0xffffffff) {
98 ERROR("Maybe ROM is not mapped properly?\n");
99 }
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800100 return CBFS_HEADER_INVALID_ADDRESS;
Patrick Georgib8835152011-07-21 15:11:40 +0200101 }
102 return header;
103}
104
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500105
Aaron Durbin12d45b22015-03-24 15:50:45 -0500106int init_backing_media(struct cbfs_media **media, struct cbfs_media *backing)
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500107{
108 if (*media == CBFS_DEFAULT_MEDIA) {
109 *media = backing;
110 if (init_default_cbfs_media(*media) != 0) {
111 ERROR("Failed to initialize default media.\n");
112 return -1;
113 }
114 }
115 return 0;
116}
117
Patrick Georgib8835152011-07-21 15:11:40 +0200118/* public API starts here*/
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500119ssize_t cbfs_locate_file(struct cbfs_media *media, struct cbfs_file *file,
120 const char *name)
Patrick Georgib8835152011-07-21 15:11:40 +0200121{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800122 const char *file_name;
123 uint32_t offset, align, romsize, name_len;
124 const struct cbfs_header *header;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800125 struct cbfs_media default_media;
Patrick Georgib8835152011-07-21 15:11:40 +0200126
Aaron Durbin12d45b22015-03-24 15:50:45 -0500127 if (init_backing_media(&media, &default_media))
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500128 return -1;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800129
130 if (CBFS_HEADER_INVALID_ADDRESS == (header = cbfs_get_header(media)))
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500131 return -1;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800132
133 // Logical offset (for source media) of first file.
134 offset = ntohl(header->offset);
135 align = ntohl(header->align);
136 romsize = ntohl(header->romsize);
137
Hung-Te Linc720d8d2013-02-06 12:11:57 +0800138 // TODO Add a "size" in CBFS header for a platform independent way to
139 // determine the end of CBFS data.
140#if defined(CONFIG_ARCH_X86) && CONFIG_ARCH_X86
Steven Sherke17843c2013-08-14 14:55:57 -0600141 // resolve actual length of ROM used for CBFS components
142 // the bootblock size was not taken into account
143 romsize -= ntohl(header->bootblocksize);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800144
Steven Sherke17843c2013-08-14 14:55:57 -0600145 // fine tune the length to handle alignment positioning.
146 // using (bootblock size) % align, to derive the
147 // number of bytes the bootblock is off from the alignment size.
148 if ((ntohl(header->bootblocksize) % align))
149 romsize -= (align - (ntohl(header->bootblocksize) % align));
150 else
151 romsize -= 1;
152#endif
153
154 DEBUG("CBFS location: 0x%x~0x%x, align: %d\n", offset, romsize, align);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600155 DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset);
Steven Sherke17843c2013-08-14 14:55:57 -0600156
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800157 media->open(media);
158 while (offset < romsize &&
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500159 media->read(media, file, offset, sizeof(*file)) == sizeof(*file)) {
160 if (memcmp(CBFS_FILE_MAGIC, file->magic,
161 sizeof(file->magic)) != 0) {
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800162 uint32_t new_align = align;
163 if (offset % align)
164 new_align += align - (offset % align);
Kane Chen0a0fc3b2014-09-08 09:04:28 -0700165 LOG("WARNING: No file header found at 0x%x - "
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800166 "try next aligned address: 0x%x.\n", offset,
167 offset + new_align);
168 offset += new_align;
169 continue;
170 }
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500171
172 file->len = ntohl(file->len);
173 file->type= ntohl(file->type);
174 file->offset = ntohl(file->offset);
175
176 name_len = file->offset - sizeof(*file);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800177 DEBUG(" - load entry 0x%x file name (%d bytes)...\n", offset,
178 name_len);
179
180 // load file name (arbitrary length).
181 file_name = (const char *)media->map(
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500182 media, offset + sizeof(*file), name_len);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800183 if (file_name == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
184 ERROR("ERROR: Failed to get filename: 0x%x.\n", offset);
185 } else if (strcmp(file_name, name) == 0) {
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600186 DEBUG("Found file (offset=0x%x, len=%d).\n",
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500187 offset + file->offset, file->len);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800188 media->unmap(media, file_name);
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500189 return offset + file->offset;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800190 } else {
Aaron Durbine690eda2013-04-25 08:42:23 -0500191 DEBUG(" (unmatched file @0x%x: %s)\n", offset,
192 file_name);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800193 media->unmap(media, file_name);
194 }
195
196 // Move to next file.
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500197 offset += file->len + file->offset;
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800198 if (offset % align)
199 offset += align - (offset % align);
200 }
201 media->close(media);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600202 LOG("WARNING: '%s' not found.\n", name);
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500203 return -1;
204}
205
Aaron Durbinb3e02022014-06-27 15:28:39 -0500206size_t cbfs_read(struct cbfs_media *media, void *dest, size_t offset,
207 size_t count)
208{
209 struct cbfs_media default_media;
210 size_t nread;
211
Aaron Durbin12d45b22015-03-24 15:50:45 -0500212 if (init_backing_media(&media, &default_media))
Aaron Durbinb3e02022014-06-27 15:28:39 -0500213 return 0;
214
215 media->open(media);
216 nread = media->read(media, dest, offset, count);
217 media->close(media);
218
219 return nread;
220}
221
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500222struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name)
223{
224 struct cbfs_media default_media;
225 struct cbfs_file file, *file_ptr;
226 ssize_t offset;
227
Aaron Durbin12d45b22015-03-24 15:50:45 -0500228 if (init_backing_media(&media, &default_media))
Aaron Durbinb312b7f2014-06-27 15:06:02 -0500229 return NULL;
230
231 offset = cbfs_locate_file(media, &file, name);
232 if (offset < 0)
233 return NULL;
234
235 /* Map both the metadata and the file contents. */
236 media->open(media);
237 offset -= file.offset;
238 file_ptr = media->map(media, offset, file.offset + file.len);
239 media->close(media);
240
241 if (file_ptr == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
242 ERROR("ERROR: Mapping %s failed.\n", name);
243 return NULL;
244 }
245
246 return file_ptr;
Patrick Georgib8835152011-07-21 15:11:40 +0200247}
248
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100249void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
250 int type, size_t *sz)
Patrick Georgib8835152011-07-21 15:11:40 +0200251{
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800252 struct cbfs_file *file = cbfs_get_file(media, name);
Patrick Georgib8835152011-07-21 15:11:40 +0200253
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100254 if (sz)
255 *sz = 0;
256
Patrick Georgib8835152011-07-21 15:11:40 +0200257 if (file == NULL) {
258 ERROR("Could not find file '%s'.\n", name);
259 return NULL;
260 }
261
262 if (ntohl(file->type) != type) {
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800263 ERROR("File '%s' is of type %x, but we requested %x.\n", name,
264 ntohl(file->type), type);
Patrick Georgib8835152011-07-21 15:11:40 +0200265 return NULL;
266 }
267
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100268 if (sz)
269 *sz = ntohl(file->len);
270
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800271 return (void *)CBFS_SUBHEADER(file);
Patrick Georgib8835152011-07-21 15:11:40 +0200272}
273
274int cbfs_decompress(int algo, void *src, void *dst, int len)
275{
276 switch (algo) {
277 case CBFS_COMPRESS_NONE:
Vladimir Serbinenko3d6ffe72014-02-05 17:00:40 +0100278 /* Reads need to be aligned at 4 bytes to avoid
279 poor flash performance. */
Ronald G. Minnichff178be2014-10-16 10:57:01 +0000280 while (len && ((uintptr_t)src & 3)) {
Vladimir Serbinenko3d6ffe72014-02-05 17:00:40 +0100281 *(u8*)dst++ = *(u8*)src++;
282 len--;
283 }
Gabe Blackc6b44162013-07-01 04:28:23 -0700284 memmove(dst, src, len);
Gabe Black0c605a52013-07-01 04:57:37 -0700285 return len;
Patrick Georgib8835152011-07-21 15:11:40 +0200286#ifdef CBFS_CORE_WITH_LZMA
287 case CBFS_COMPRESS_LZMA:
Gabe Black0c605a52013-07-01 04:57:37 -0700288 return ulzma(src, dst);
Patrick Georgib8835152011-07-21 15:11:40 +0200289#endif
290 default:
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800291 ERROR("tried to decompress %d bytes with algorithm #%x,"
292 "but that algorithm id is unsupported.\n", len,
293 algo);
Gabe Black0c605a52013-07-01 04:57:37 -0700294 return 0;
Patrick Georgib8835152011-07-21 15:11:40 +0200295 }
296}