blob: c32d262b3361565550610c352f38ec17bc42df20 [file] [log] [blame]
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +02001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2011 secunet Security Networks AG
Hung-Te Lind01d0362013-01-25 12:42:40 +08005 * Copyright (C) 2013 Google, Inc.
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +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 Lind01d0362013-01-25 12:42:40 +080031/* The CBFS core requires a couple of #defines or functions to adapt it to the
32 * target environment:
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020033 *
34 * CBFS_CORE_WITH_LZMA (must be #define)
35 * if defined, ulzma() must exist for decompression of data streams
36 *
Julius Werner09f29212015-09-29 13:51:35 -070037 * CBFS_CORE_WITH_LZ4 (must be #define)
38 * if defined, ulz4f() must exist for decompression of data streams
39 *
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020040 * ERROR(x...)
41 * print an error message x (in printf format)
42 *
43 * LOG(x...)
Hung-Te Lind01d0362013-01-25 12:42:40 +080044 * print a message x (in printf format)
45 *
46 * DEBUG(x...)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020047 * print a debug message x (in printf format)
48 *
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020049 */
50
Hung-Te Lind01d0362013-01-25 12:42:40 +080051#include <cbfs.h>
52#include <string.h>
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -070053#include <sysinfo.h>
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020054
Hung-Te Lind01d0362013-01-25 12:42:40 +080055/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS
56 * on failure */
57const struct cbfs_header *cbfs_get_header(struct cbfs_media *media)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020058{
Julius Wernerefcee762014-11-10 13:14:24 -080059 int32_t rel_offset;
Hung-Te Lind01d0362013-01-25 12:42:40 +080060 const struct cbfs_header *header;
61 struct cbfs_media default_media;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020062
Hung-Te Lind01d0362013-01-25 12:42:40 +080063 if (media == CBFS_DEFAULT_MEDIA) {
64 media = &default_media;
65 if (init_default_cbfs_media(media) != 0) {
Patrick Georgi46cb96bb2013-02-19 17:59:21 +010066 ERROR("Failed to initialize default media.\n");
67 return CBFS_HEADER_INVALID_ADDRESS;
Hung-Te Lind01d0362013-01-25 12:42:40 +080068 }
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020069 }
Hung-Te Lind01d0362013-01-25 12:42:40 +080070 media->open(media);
Julius Wernerefcee762014-11-10 13:14:24 -080071
72 if (!media->read(media, &rel_offset, (size_t)(0 - sizeof(int32_t)),
73 sizeof(int32_t))) {
74 ERROR("Could not read CBFS master header offset!\n");
75 return CBFS_HEADER_INVALID_ADDRESS;
76 }
77 header = media->map(media, (size_t)rel_offset, sizeof(*header));
78 DEBUG("CBFS header at %#zx (-%#zx from end of image).\n",
79 (size_t)rel_offset, (size_t)-rel_offset);
Hung-Te Lind01d0362013-01-25 12:42:40 +080080 media->close(media);
81
82 if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
Julius Wernerefcee762014-11-10 13:14:24 -080083 ERROR("Failed to load CBFS header from %#zx(-%#zx)\n",
84 (size_t)rel_offset, (size_t)-rel_offset);
Hung-Te Lind01d0362013-01-25 12:42:40 +080085 return CBFS_HEADER_INVALID_ADDRESS;
86 }
87
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020088 if (CBFS_HEADER_MAGIC != ntohl(header->magic)) {
Julius Wernerefcee762014-11-10 13:14:24 -080089 ERROR("Could not find valid CBFS master header at %#zx(-%#zx): "
90 "magic %#.8x vs %#.8x.\n", (size_t)rel_offset,
91 (size_t)-rel_offset, CBFS_HEADER_MAGIC,
Hung-Te Lind01d0362013-01-25 12:42:40 +080092 ntohl(header->magic));
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020093 if (header->magic == 0xffffffff) {
94 ERROR("Maybe ROM is not mapped properly?\n");
95 }
Hung-Te Lind01d0362013-01-25 12:42:40 +080096 return CBFS_HEADER_INVALID_ADDRESS;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020097 }
98 return header;
99}
100
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700101static int get_cbfs_range(uint32_t *offset, uint32_t *cbfs_end,
102 struct cbfs_media *media)
103{
104 const struct cbfs_header *header;
105
Patrick Georgif86d0352015-10-23 20:25:03 +0200106 if (media == CBFS_DEFAULT_MEDIA &&
107 lib_sysinfo.cbfs_offset && lib_sysinfo.cbfs_size) {
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700108 *offset = lib_sysinfo.cbfs_offset;
109 *cbfs_end = *offset + lib_sysinfo.cbfs_size;
110 return 0;
111 }
112
Daisuke Nojiri03688ec2015-11-19 09:10:41 -0800113 /* read offset and size from cbfs master header */
114 DEBUG("Read CBFS offset & size from master header\n");
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700115 header = cbfs_get_header(media);
116 if (header == CBFS_HEADER_INVALID_ADDRESS)
117 return -1;
118 // Logical offset (for source media) of first file.
119 *offset = ntohl(header->offset);
120 *cbfs_end = ntohl(header->romsize);
121#if IS_ENABLED(CONFIG_LP_ARCH_X86)
122 // resolve actual length of ROM used for CBFS components
123 // the bootblock size was not taken into account
124 *cbfs_end -= ntohl(header->bootblocksize);
125
126 // fine tune the length to handle alignment positioning.
127 // using (bootblock size) % align, to derive the
128 // number of bytes the bootblock is off from the alignment size.
129 if ((ntohl(header->bootblocksize) % CBFS_ALIGNMENT))
130 *cbfs_end -= (CBFS_ALIGNMENT -
131 (ntohl(header->bootblocksize) % CBFS_ALIGNMENT));
132 else
133 *cbfs_end -= 1;
134#endif
135 return 0;
136}
137
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200138/* public API starts here*/
Hung-Te Lind01d0362013-01-25 12:42:40 +0800139struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200140{
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700141 const char *vardata;
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700142 uint32_t offset, cbfs_end, vardata_len;
Hung-Te Lind01d0362013-01-25 12:42:40 +0800143 struct cbfs_file file, *file_ptr;
144 struct cbfs_media default_media;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200145
Daisuke Nojiri03688ec2015-11-19 09:10:41 -0800146 if (get_cbfs_range(&offset, &cbfs_end, media)) {
147 ERROR("Failed to find cbfs range\n");
148 return NULL;
149 }
150
Hung-Te Lind01d0362013-01-25 12:42:40 +0800151 if (media == CBFS_DEFAULT_MEDIA) {
152 media = &default_media;
153 if (init_default_cbfs_media(media) != 0) {
Patrick Georgi46cb96bb2013-02-19 17:59:21 +0100154 ERROR("Failed to initialize default media.\n");
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200155 return NULL;
156 }
157 }
Hung-Te Lind01d0362013-01-25 12:42:40 +0800158
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700159 DEBUG("CBFS location: 0x%x~0x%x\n", offset, cbfs_end);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600160 DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset);
Steven Sherke17843c2013-08-14 14:55:57 -0600161
Hung-Te Lind01d0362013-01-25 12:42:40 +0800162 media->open(media);
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700163 while (offset < cbfs_end &&
Hung-Te Lind01d0362013-01-25 12:42:40 +0800164 media->read(media, &file, offset, sizeof(file)) == sizeof(file)) {
165 if (memcmp(CBFS_FILE_MAGIC, file.magic,
166 sizeof(file.magic)) != 0) {
Patrick Georgi2272b802015-07-14 22:29:04 +0200167 uint32_t new_align = CBFS_ALIGNMENT;
168 if (offset % CBFS_ALIGNMENT)
169 new_align += CBFS_ALIGNMENT -
170 (offset % CBFS_ALIGNMENT);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800171 ERROR("ERROR: No file header found at 0x%xx - "
172 "try next aligned address: 0x%x.\n", offset,
173 offset + new_align);
174 offset += new_align;
175 continue;
176 }
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700177 vardata_len = ntohl(file.offset) - sizeof(file);
178 DEBUG(" - load entry 0x%x variable data (%d bytes)...\n",
179 offset, vardata_len);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800180
181 // load file name (arbitrary length).
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700182 vardata = (const char*)media->map(
183 media, offset + sizeof(file), vardata_len);
184 if (vardata == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
Hung-Te Lind01d0362013-01-25 12:42:40 +0800185 ERROR("ERROR: Failed to get filename: 0x%x.\n", offset);
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700186 } else if (strcmp(vardata, name) == 0) {
Hung-Te Lind01d0362013-01-25 12:42:40 +0800187 int file_offset = ntohl(file.offset),
188 file_len = ntohl(file.len);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600189 DEBUG("Found file (offset=0x%x, len=%d).\n",
Hung-Te Lind01d0362013-01-25 12:42:40 +0800190 offset + file_offset, file_len);
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700191 media->unmap(media, vardata);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800192 file_ptr = media->map(media, offset,
193 file_offset + file_len);
194 media->close(media);
195 return file_ptr;
196 } else {
Aaron Durbin3eddcff2013-05-01 08:40:13 -0500197 DEBUG(" (unmatched file @0x%x: %s)\n", offset,
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700198 vardata);
199 media->unmap(media, vardata);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800200 }
201
202 // Move to next file.
203 offset += ntohl(file.len) + ntohl(file.offset);
Patrick Georgi2272b802015-07-14 22:29:04 +0200204 if (offset % CBFS_ALIGNMENT)
205 offset += CBFS_ALIGNMENT - (offset % CBFS_ALIGNMENT);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800206 }
207 media->close(media);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600208 LOG("WARNING: '%s' not found.\n", name);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200209 return NULL;
210}
211
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100212void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
213 int type, size_t *sz)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200214{
Daisuke Nojiri03688ec2015-11-19 09:10:41 -0800215 /*
216 * get file (possibly compressed) data. we pass through 'media' to
217 * cbfs_get_file (don't call init_default_cbfs_media) here so that
218 * cbfs_get_file can see whether the call is for CBFS_DEFAULT_MEDIA
219 * or not. Therefore, we don't (can't) unmap the file but it's caused
220 * by a pre-existing inherent problem with cbfs_get_file (and all
221 * callers are suffering from it).
222 */
Hung-Te Lind01d0362013-01-25 12:42:40 +0800223 struct cbfs_file *file = cbfs_get_file(media, name);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200224
225 if (file == NULL) {
226 ERROR("Could not find file '%s'.\n", name);
227 return NULL;
228 }
229
Daisuke Nojiri03688ec2015-11-19 09:10:41 -0800230 if (sz)
231 *sz = 0;
232
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200233 if (ntohl(file->type) != type) {
Hung-Te Lind01d0362013-01-25 12:42:40 +0800234 ERROR("File '%s' is of type %x, but we requested %x.\n", name,
235 ntohl(file->type), type);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200236 return NULL;
237 }
238
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700239 void *file_content = (void *)CBFS_SUBHEADER(file);
240
Patrick Georgieb33b3f2015-09-17 20:45:52 +0200241 struct cbfs_file_attribute *attr =
242 cbfs_file_find_attr(file, CBFS_FILE_ATTR_TAG_COMPRESSION);
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700243
Nico Huberac1f4b82015-10-02 19:38:24 +0200244 size_t final_size = ntohl(file->len);
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700245 int compression_algo = CBFS_COMPRESS_NONE;
246 if (attr) {
247 struct cbfs_file_attr_compression *comp =
248 (struct cbfs_file_attr_compression *)attr;
249 compression_algo = ntohl(comp->compression);
Daisuke Nojiri9dcf4572015-09-23 13:01:14 -0700250 DEBUG("File '%s' is compressed (alg=%d)\n",
251 name, compression_algo);
Nico Huberac1f4b82015-10-02 19:38:24 +0200252 final_size = ntohl(comp->decompressed_size);
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700253 }
254
Nico Huberac1f4b82015-10-02 19:38:24 +0200255 void *dst = malloc(final_size);
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700256 if (dst == NULL)
257 goto err;
258
Nico Huberac1f4b82015-10-02 19:38:24 +0200259 if (!cbfs_decompress(compression_algo, file_content, dst, final_size))
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700260 goto err;
261
Nico Huberac1f4b82015-10-02 19:38:24 +0200262 if (sz)
263 *sz = final_size;
264
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700265 return dst;
266
267err:
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700268 free(dst);
269 return NULL;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200270}
271
Patrick Georgi377d1db2015-09-16 18:53:40 +0200272struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
273{
274 /* attributes_offset should be 0 when there is no attribute, but all
275 * values that point into the cbfs_file header are invalid, too. */
276 if (ntohl(file->attributes_offset) <= sizeof(*file))
277 return NULL;
278
279 /* There needs to be enough space for the file header and one
280 * attribute header for this to make sense. */
281 if (ntohl(file->offset) <=
282 sizeof(*file) + sizeof(struct cbfs_file_attribute))
283 return NULL;
284
285 return (struct cbfs_file_attribute *)
286 (((uint8_t *)file) + ntohl(file->attributes_offset));
287}
288
289struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
290 struct cbfs_file_attribute *attr)
291{
292 /* ex falso sequitur quodlibet */
293 if (attr == NULL)
294 return NULL;
295
296 /* Is there enough space for another attribute? */
297 if ((uint8_t *)attr + ntohl(attr->len) +
298 sizeof(struct cbfs_file_attribute) >=
299 (uint8_t *)file + ntohl(file->offset))
300 return NULL;
301
302 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
303 (((uint8_t *)attr) + ntohl(attr->len));
304 /* If any, "unused" attributes must come last. */
305 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
306 return NULL;
307 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
308 return NULL;
309
310 return next;
311}
312
Patrick Georgieb33b3f2015-09-17 20:45:52 +0200313struct cbfs_file_attribute *cbfs_file_find_attr(struct cbfs_file *file,
314 uint32_t tag)
315{
316 struct cbfs_file_attribute *attr = cbfs_file_first_attr(file);
317 while (attr) {
318 if (ntohl(attr->tag) == tag)
319 break;
320 attr = cbfs_file_next_attr(file, attr);
321 }
322 return attr;
323
324}
325
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200326int cbfs_decompress(int algo, void *src, void *dst, int len)
327{
328 switch (algo) {
329 case CBFS_COMPRESS_NONE:
330 memcpy(dst, src, len);
Gabe Black0c605a52013-07-01 04:57:37 -0700331 return len;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200332#ifdef CBFS_CORE_WITH_LZMA
333 case CBFS_COMPRESS_LZMA:
Gabe Black0c605a52013-07-01 04:57:37 -0700334 return ulzma(src, dst);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200335#endif
Julius Werner09f29212015-09-29 13:51:35 -0700336#ifdef CBFS_CORE_WITH_LZ4
337 case CBFS_COMPRESS_LZ4:
338 return ulz4f(src, dst);
339#endif
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200340 default:
Hung-Te Lind01d0362013-01-25 12:42:40 +0800341 ERROR("tried to decompress %d bytes with algorithm #%x,"
342 "but that algorithm id is unsupported.\n", len,
343 algo);
Gabe Black0c605a52013-07-01 04:57:37 -0700344 return 0;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200345 }
346}