blob: 44205f666fe1435ac5f34506bdde496a2e5ee60c [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>
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020050
Hung-Te Lind01d0362013-01-25 12:42:40 +080051/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS
52 * on failure */
53const struct cbfs_header *cbfs_get_header(struct cbfs_media *media)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020054{
Julius Wernerefcee762014-11-10 13:14:24 -080055 int32_t rel_offset;
Hung-Te Lind01d0362013-01-25 12:42:40 +080056 const struct cbfs_header *header;
57 struct cbfs_media default_media;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020058
Hung-Te Lind01d0362013-01-25 12:42:40 +080059 if (media == CBFS_DEFAULT_MEDIA) {
60 media = &default_media;
61 if (init_default_cbfs_media(media) != 0) {
Patrick Georgi46cb96bb2013-02-19 17:59:21 +010062 ERROR("Failed to initialize default media.\n");
63 return CBFS_HEADER_INVALID_ADDRESS;
Hung-Te Lind01d0362013-01-25 12:42:40 +080064 }
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020065 }
Hung-Te Lind01d0362013-01-25 12:42:40 +080066 media->open(media);
Julius Wernerefcee762014-11-10 13:14:24 -080067
68 if (!media->read(media, &rel_offset, (size_t)(0 - sizeof(int32_t)),
69 sizeof(int32_t))) {
70 ERROR("Could not read CBFS master header offset!\n");
71 return CBFS_HEADER_INVALID_ADDRESS;
72 }
73 header = media->map(media, (size_t)rel_offset, sizeof(*header));
74 DEBUG("CBFS header at %#zx (-%#zx from end of image).\n",
75 (size_t)rel_offset, (size_t)-rel_offset);
Hung-Te Lind01d0362013-01-25 12:42:40 +080076 media->close(media);
77
78 if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
Julius Wernerefcee762014-11-10 13:14:24 -080079 ERROR("Failed to load CBFS header from %#zx(-%#zx)\n",
80 (size_t)rel_offset, (size_t)-rel_offset);
Hung-Te Lind01d0362013-01-25 12:42:40 +080081 return CBFS_HEADER_INVALID_ADDRESS;
82 }
83
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020084 if (CBFS_HEADER_MAGIC != ntohl(header->magic)) {
Julius Wernerefcee762014-11-10 13:14:24 -080085 ERROR("Could not find valid CBFS master header at %#zx(-%#zx): "
86 "magic %#.8x vs %#.8x.\n", (size_t)rel_offset,
87 (size_t)-rel_offset, CBFS_HEADER_MAGIC,
Hung-Te Lind01d0362013-01-25 12:42:40 +080088 ntohl(header->magic));
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020089 if (header->magic == 0xffffffff) {
90 ERROR("Maybe ROM is not mapped properly?\n");
91 }
Hung-Te Lind01d0362013-01-25 12:42:40 +080092 return CBFS_HEADER_INVALID_ADDRESS;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020093 }
94 return header;
95}
96
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020097/* public API starts here*/
Hung-Te Lind01d0362013-01-25 12:42:40 +080098struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020099{
Hung-Te Lind01d0362013-01-25 12:42:40 +0800100 const char *file_name;
101 uint32_t offset, align, romsize, name_len;
102 const struct cbfs_header *header;
103 struct cbfs_file file, *file_ptr;
104 struct cbfs_media default_media;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200105
Hung-Te Lind01d0362013-01-25 12:42:40 +0800106 if (media == CBFS_DEFAULT_MEDIA) {
107 media = &default_media;
108 if (init_default_cbfs_media(media) != 0) {
Patrick Georgi46cb96bb2013-02-19 17:59:21 +0100109 ERROR("Failed to initialize default media.\n");
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200110 return NULL;
111 }
112 }
Hung-Te Lind01d0362013-01-25 12:42:40 +0800113
114 if (CBFS_HEADER_INVALID_ADDRESS == (header = cbfs_get_header(media)))
115 return NULL;
116
117 // Logical offset (for source media) of first file.
118 offset = ntohl(header->offset);
119 align = ntohl(header->align);
120 romsize = ntohl(header->romsize);
121
Hung-Te Linfbf07832013-02-20 15:50:06 +0800122 // TODO Add a "size" in CBFS header for a platform independent way to
123 // determine the end of CBFS data.
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700124#if IS_ENABLED(CONFIG_LP_ARCH_X86)
Steven Sherke17843c2013-08-14 14:55:57 -0600125 // resolve actual length of ROM used for CBFS components
126 // the bootblock size was not taken into account
127 romsize -= ntohl(header->bootblocksize);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800128
Steven Sherke17843c2013-08-14 14:55:57 -0600129 // fine tune the length to handle alignment positioning.
130 // using (bootblock size) % align, to derive the
131 // number of bytes the bootblock is off from the alignment size.
132 if ((ntohl(header->bootblocksize) % align))
133 romsize -= (align - (ntohl(header->bootblocksize) % align));
134 else
135 romsize -= 1;
136#endif
137
138 DEBUG("CBFS location: 0x%x~0x%x, align: %d\n", offset, romsize, align);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600139 DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset);
Steven Sherke17843c2013-08-14 14:55:57 -0600140
Hung-Te Lind01d0362013-01-25 12:42:40 +0800141 media->open(media);
142 while (offset < romsize &&
143 media->read(media, &file, offset, sizeof(file)) == sizeof(file)) {
144 if (memcmp(CBFS_FILE_MAGIC, file.magic,
145 sizeof(file.magic)) != 0) {
146 uint32_t new_align = align;
147 if (offset % align)
148 new_align += align - (offset % align);
149 ERROR("ERROR: No file header found at 0x%xx - "
150 "try next aligned address: 0x%x.\n", offset,
151 offset + new_align);
152 offset += new_align;
153 continue;
154 }
155 name_len = ntohl(file.offset) - sizeof(file);
156 DEBUG(" - load entry 0x%x file name (%d bytes)...\n", offset,
157 name_len);
158
159 // load file name (arbitrary length).
160 file_name = (const char*)media->map(
161 media, offset + sizeof(file), name_len);
162 if (file_name == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
163 ERROR("ERROR: Failed to get filename: 0x%x.\n", offset);
164 } else if (strcmp(file_name, name) == 0) {
165 int file_offset = ntohl(file.offset),
166 file_len = ntohl(file.len);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600167 DEBUG("Found file (offset=0x%x, len=%d).\n",
Hung-Te Lind01d0362013-01-25 12:42:40 +0800168 offset + file_offset, file_len);
169 media->unmap(media, file_name);
170 file_ptr = media->map(media, offset,
171 file_offset + file_len);
172 media->close(media);
173 return file_ptr;
174 } else {
Aaron Durbin3eddcff2013-05-01 08:40:13 -0500175 DEBUG(" (unmatched file @0x%x: %s)\n", offset,
176 file_name);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800177 media->unmap(media, file_name);
178 }
179
180 // Move to next file.
181 offset += ntohl(file.len) + ntohl(file.offset);
182 if (offset % align)
183 offset += align - (offset % align);
184 }
185 media->close(media);
Dave Frodin37f8c3a2013-05-06 15:51:39 -0600186 LOG("WARNING: '%s' not found.\n", name);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200187 return NULL;
188}
189
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100190void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
191 int type, size_t *sz)
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200192{
Hung-Te Lind01d0362013-01-25 12:42:40 +0800193 struct cbfs_file *file = cbfs_get_file(media, name);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200194
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100195 if (sz)
196 *sz = 0;
197
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200198 if (file == NULL) {
199 ERROR("Could not find file '%s'.\n", name);
200 return NULL;
201 }
202
203 if (ntohl(file->type) != type) {
Hung-Te Lind01d0362013-01-25 12:42:40 +0800204 ERROR("File '%s' is of type %x, but we requested %x.\n", name,
205 ntohl(file->type), type);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200206 return NULL;
207 }
208
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100209 if (sz)
210 *sz = ntohl(file->len);
211
212 return (void *)CBFS_SUBHEADER(file);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200213}
214
215int cbfs_decompress(int algo, void *src, void *dst, int len)
216{
217 switch (algo) {
218 case CBFS_COMPRESS_NONE:
219 memcpy(dst, src, len);
Gabe Black0c605a52013-07-01 04:57:37 -0700220 return len;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200221#ifdef CBFS_CORE_WITH_LZMA
222 case CBFS_COMPRESS_LZMA:
Gabe Black0c605a52013-07-01 04:57:37 -0700223 return ulzma(src, dst);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200224#endif
225 default:
Hung-Te Lind01d0362013-01-25 12:42:40 +0800226 ERROR("tried to decompress %d bytes with algorithm #%x,"
227 "but that algorithm id is unsupported.\n", len,
228 algo);
Gabe Black0c605a52013-07-01 04:57:37 -0700229 return 0;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200230 }
231}