blob: ddf0da5f42189e9a4e882a4e5a9c6ed19ebdd705 [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 *
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020037 * ERROR(x...)
38 * print an error message x (in printf format)
39 *
40 * LOG(x...)
Hung-Te Lind01d0362013-01-25 12:42:40 +080041 * print a message x (in printf format)
42 *
43 * DEBUG(x...)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020044 * print a debug message x (in printf format)
45 *
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020046 */
47
Hung-Te Lind01d0362013-01-25 12:42:40 +080048#include <cbfs.h>
49#include <string.h>
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -070050#include <sysinfo.h>
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020051
Hung-Te Lind01d0362013-01-25 12:42:40 +080052/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS
53 * on failure */
54const struct cbfs_header *cbfs_get_header(struct cbfs_media *media)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020055{
Julius Wernerefcee762014-11-10 13:14:24 -080056 int32_t rel_offset;
Hung-Te Lind01d0362013-01-25 12:42:40 +080057 const struct cbfs_header *header;
58 struct cbfs_media default_media;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020059
Hung-Te Lind01d0362013-01-25 12:42:40 +080060 if (media == CBFS_DEFAULT_MEDIA) {
61 media = &default_media;
62 if (init_default_cbfs_media(media) != 0) {
Patrick Georgi46cb96bb2013-02-19 17:59:21 +010063 ERROR("Failed to initialize default media.\n");
64 return CBFS_HEADER_INVALID_ADDRESS;
Hung-Te Lind01d0362013-01-25 12:42:40 +080065 }
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020066 }
Hung-Te Lind01d0362013-01-25 12:42:40 +080067 media->open(media);
Julius Wernerefcee762014-11-10 13:14:24 -080068
69 if (!media->read(media, &rel_offset, (size_t)(0 - sizeof(int32_t)),
70 sizeof(int32_t))) {
71 ERROR("Could not read CBFS master header offset!\n");
72 return CBFS_HEADER_INVALID_ADDRESS;
73 }
74 header = media->map(media, (size_t)rel_offset, sizeof(*header));
75 DEBUG("CBFS header at %#zx (-%#zx from end of image).\n",
76 (size_t)rel_offset, (size_t)-rel_offset);
Hung-Te Lind01d0362013-01-25 12:42:40 +080077 media->close(media);
78
79 if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
Julius Wernerefcee762014-11-10 13:14:24 -080080 ERROR("Failed to load CBFS header from %#zx(-%#zx)\n",
81 (size_t)rel_offset, (size_t)-rel_offset);
Hung-Te Lind01d0362013-01-25 12:42:40 +080082 return CBFS_HEADER_INVALID_ADDRESS;
83 }
84
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020085 if (CBFS_HEADER_MAGIC != ntohl(header->magic)) {
Julius Wernerefcee762014-11-10 13:14:24 -080086 ERROR("Could not find valid CBFS master header at %#zx(-%#zx): "
87 "magic %#.8x vs %#.8x.\n", (size_t)rel_offset,
88 (size_t)-rel_offset, CBFS_HEADER_MAGIC,
Hung-Te Lind01d0362013-01-25 12:42:40 +080089 ntohl(header->magic));
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020090 if (header->magic == 0xffffffff) {
91 ERROR("Maybe ROM is not mapped properly?\n");
92 }
Hung-Te Lind01d0362013-01-25 12:42:40 +080093 return CBFS_HEADER_INVALID_ADDRESS;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020094 }
95 return header;
96}
97
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -070098static int get_cbfs_range(uint32_t *offset, uint32_t *cbfs_end,
99 struct cbfs_media *media)
100{
101 const struct cbfs_header *header;
102
Patrick Georgif86d0352015-10-23 20:25:03 +0200103 if (media == CBFS_DEFAULT_MEDIA &&
104 lib_sysinfo.cbfs_offset && lib_sysinfo.cbfs_size) {
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700105 *offset = lib_sysinfo.cbfs_offset;
106 *cbfs_end = *offset + lib_sysinfo.cbfs_size;
107 return 0;
108 }
109
Daisuke Nojiri03688ec2015-11-19 09:10:41 -0800110 /* read offset and size from cbfs master header */
111 DEBUG("Read CBFS offset & size from master header\n");
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700112 header = cbfs_get_header(media);
113 if (header == CBFS_HEADER_INVALID_ADDRESS)
114 return -1;
115 // Logical offset (for source media) of first file.
116 *offset = ntohl(header->offset);
117 *cbfs_end = ntohl(header->romsize);
118#if IS_ENABLED(CONFIG_LP_ARCH_X86)
119 // resolve actual length of ROM used for CBFS components
120 // the bootblock size was not taken into account
121 *cbfs_end -= ntohl(header->bootblocksize);
122
123 // fine tune the length to handle alignment positioning.
124 // using (bootblock size) % align, to derive the
125 // number of bytes the bootblock is off from the alignment size.
126 if ((ntohl(header->bootblocksize) % CBFS_ALIGNMENT))
127 *cbfs_end -= (CBFS_ALIGNMENT -
128 (ntohl(header->bootblocksize) % CBFS_ALIGNMENT));
129 else
130 *cbfs_end -= 1;
131#endif
132 return 0;
133}
134
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200135/* public API starts here*/
Hung-Te Lind01d0362013-01-25 12:42:40 +0800136struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200137{
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700138 const char *vardata;
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700139 uint32_t offset, cbfs_end, vardata_len;
Hung-Te Lind01d0362013-01-25 12:42:40 +0800140 struct cbfs_file file, *file_ptr;
141 struct cbfs_media default_media;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200142
Daisuke Nojiri03688ec2015-11-19 09:10:41 -0800143 if (get_cbfs_range(&offset, &cbfs_end, media)) {
144 ERROR("Failed to find cbfs range\n");
145 return NULL;
146 }
147
Hung-Te Lind01d0362013-01-25 12:42:40 +0800148 if (media == CBFS_DEFAULT_MEDIA) {
149 media = &default_media;
150 if (init_default_cbfs_media(media) != 0) {
Patrick Georgi46cb96bb2013-02-19 17:59:21 +0100151 ERROR("Failed to initialize default media.\n");
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200152 return NULL;
153 }
154 }
Hung-Te Lind01d0362013-01-25 12:42:40 +0800155
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700156 DEBUG("CBFS location: 0x%x~0x%x\n", offset, cbfs_end);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600157 DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset);
Steven Sherke17843c2013-08-14 14:55:57 -0600158
Hung-Te Lind01d0362013-01-25 12:42:40 +0800159 media->open(media);
Daisuke Nojiri2ccb2802015-09-02 10:53:13 -0700160 while (offset < cbfs_end &&
Hung-Te Lind01d0362013-01-25 12:42:40 +0800161 media->read(media, &file, offset, sizeof(file)) == sizeof(file)) {
162 if (memcmp(CBFS_FILE_MAGIC, file.magic,
163 sizeof(file.magic)) != 0) {
Patrick Georgi2272b802015-07-14 22:29:04 +0200164 uint32_t new_align = CBFS_ALIGNMENT;
165 if (offset % CBFS_ALIGNMENT)
166 new_align += CBFS_ALIGNMENT -
167 (offset % CBFS_ALIGNMENT);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800168 ERROR("ERROR: No file header found at 0x%xx - "
169 "try next aligned address: 0x%x.\n", offset,
170 offset + new_align);
171 offset += new_align;
172 continue;
173 }
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700174 vardata_len = ntohl(file.offset) - sizeof(file);
175 DEBUG(" - load entry 0x%x variable data (%d bytes)...\n",
176 offset, vardata_len);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800177
178 // load file name (arbitrary length).
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700179 vardata = (const char*)media->map(
180 media, offset + sizeof(file), vardata_len);
181 if (vardata == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
Hung-Te Lind01d0362013-01-25 12:42:40 +0800182 ERROR("ERROR: Failed to get filename: 0x%x.\n", offset);
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700183 } else if (strcmp(vardata, name) == 0) {
Hung-Te Lind01d0362013-01-25 12:42:40 +0800184 int file_offset = ntohl(file.offset),
185 file_len = ntohl(file.len);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600186 DEBUG("Found file (offset=0x%x, len=%d).\n",
Hung-Te Lind01d0362013-01-25 12:42:40 +0800187 offset + file_offset, file_len);
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700188 media->unmap(media, vardata);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800189 file_ptr = media->map(media, offset,
190 file_offset + file_len);
191 media->close(media);
192 return file_ptr;
193 } else {
Aaron Durbin3eddcff2013-05-01 08:40:13 -0500194 DEBUG(" (unmatched file @0x%x: %s)\n", offset,
Daisuke Nojiri5c6dc722015-07-09 15:07:45 -0700195 vardata);
196 media->unmap(media, vardata);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800197 }
198
199 // Move to next file.
200 offset += ntohl(file.len) + ntohl(file.offset);
Patrick Georgi2272b802015-07-14 22:29:04 +0200201 if (offset % CBFS_ALIGNMENT)
202 offset += CBFS_ALIGNMENT - (offset % CBFS_ALIGNMENT);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800203 }
204 media->close(media);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600205 LOG("WARNING: '%s' not found.\n", name);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200206 return NULL;
207}
208
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100209void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
210 int type, size_t *sz)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200211{
Daisuke Nojiri03688ec2015-11-19 09:10:41 -0800212 /*
213 * get file (possibly compressed) data. we pass through 'media' to
214 * cbfs_get_file (don't call init_default_cbfs_media) here so that
215 * cbfs_get_file can see whether the call is for CBFS_DEFAULT_MEDIA
216 * or not. Therefore, we don't (can't) unmap the file but it's caused
217 * by a pre-existing inherent problem with cbfs_get_file (and all
218 * callers are suffering from it).
219 */
Hung-Te Lind01d0362013-01-25 12:42:40 +0800220 struct cbfs_file *file = cbfs_get_file(media, name);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200221
222 if (file == NULL) {
223 ERROR("Could not find file '%s'.\n", name);
224 return NULL;
225 }
226
Daisuke Nojiri03688ec2015-11-19 09:10:41 -0800227 if (sz)
228 *sz = 0;
229
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200230 if (ntohl(file->type) != type) {
Hung-Te Lind01d0362013-01-25 12:42:40 +0800231 ERROR("File '%s' is of type %x, but we requested %x.\n", name,
232 ntohl(file->type), type);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200233 return NULL;
234 }
235
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700236 void *file_content = (void *)CBFS_SUBHEADER(file);
237
Patrick Georgieb33b3f2015-09-17 20:45:52 +0200238 struct cbfs_file_attribute *attr =
239 cbfs_file_find_attr(file, CBFS_FILE_ATTR_TAG_COMPRESSION);
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700240
Nico Huberac1f4b82015-10-02 19:38:24 +0200241 size_t final_size = ntohl(file->len);
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700242 int compression_algo = CBFS_COMPRESS_NONE;
243 if (attr) {
244 struct cbfs_file_attr_compression *comp =
245 (struct cbfs_file_attr_compression *)attr;
246 compression_algo = ntohl(comp->compression);
Daisuke Nojiri9dcf4572015-09-23 13:01:14 -0700247 DEBUG("File '%s' is compressed (alg=%d)\n",
248 name, compression_algo);
Nico Huberac1f4b82015-10-02 19:38:24 +0200249 final_size = ntohl(comp->decompressed_size);
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700250 }
251
Nico Huberac1f4b82015-10-02 19:38:24 +0200252 void *dst = malloc(final_size);
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700253 if (dst == NULL)
254 goto err;
255
Nico Huberac1f4b82015-10-02 19:38:24 +0200256 if (!cbfs_decompress(compression_algo, file_content, dst, final_size))
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700257 goto err;
258
Nico Huberac1f4b82015-10-02 19:38:24 +0200259 if (sz)
260 *sz = final_size;
261
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700262 return dst;
263
264err:
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700265 free(dst);
266 return NULL;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200267}
268
Patrick Georgi377d1db2015-09-16 18:53:40 +0200269struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file)
270{
271 /* attributes_offset should be 0 when there is no attribute, but all
272 * values that point into the cbfs_file header are invalid, too. */
273 if (ntohl(file->attributes_offset) <= sizeof(*file))
274 return NULL;
275
276 /* There needs to be enough space for the file header and one
277 * attribute header for this to make sense. */
278 if (ntohl(file->offset) <=
279 sizeof(*file) + sizeof(struct cbfs_file_attribute))
280 return NULL;
281
282 return (struct cbfs_file_attribute *)
283 (((uint8_t *)file) + ntohl(file->attributes_offset));
284}
285
286struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
287 struct cbfs_file_attribute *attr)
288{
289 /* ex falso sequitur quodlibet */
290 if (attr == NULL)
291 return NULL;
292
293 /* Is there enough space for another attribute? */
294 if ((uint8_t *)attr + ntohl(attr->len) +
295 sizeof(struct cbfs_file_attribute) >=
296 (uint8_t *)file + ntohl(file->offset))
297 return NULL;
298
299 struct cbfs_file_attribute *next = (struct cbfs_file_attribute *)
300 (((uint8_t *)attr) + ntohl(attr->len));
301 /* If any, "unused" attributes must come last. */
302 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED)
303 return NULL;
304 if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2)
305 return NULL;
306
307 return next;
308}
309
Patrick Georgieb33b3f2015-09-17 20:45:52 +0200310struct cbfs_file_attribute *cbfs_file_find_attr(struct cbfs_file *file,
311 uint32_t tag)
312{
313 struct cbfs_file_attribute *attr = cbfs_file_first_attr(file);
314 while (attr) {
315 if (ntohl(attr->tag) == tag)
316 break;
317 attr = cbfs_file_next_attr(file, attr);
318 }
319 return attr;
320
321}
322
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200323int cbfs_decompress(int algo, void *src, void *dst, int len)
324{
325 switch (algo) {
326 case CBFS_COMPRESS_NONE:
327 memcpy(dst, src, len);
Gabe Black0c605a52013-07-01 04:57:37 -0700328 return len;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200329#ifdef CBFS_CORE_WITH_LZMA
330 case CBFS_COMPRESS_LZMA:
Gabe Black0c605a52013-07-01 04:57:37 -0700331 return ulzma(src, dst);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200332#endif
333 default:
Hung-Te Lind01d0362013-01-25 12:42:40 +0800334 ERROR("tried to decompress %d bytes with algorithm #%x,"
335 "but that algorithm id is unsupported.\n", len,
336 algo);
Gabe Black0c605a52013-07-01 04:57:37 -0700337 return 0;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200338 }
339}