Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2011 secunet Security Networks AG |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 5 | * Copyright (C) 2013 Google, Inc. |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 6 | * |
| 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 Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 31 | /* The CBFS core requires a couple of #defines or functions to adapt it to the |
| 32 | * target environment: |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 33 | * |
| 34 | * CBFS_CORE_WITH_LZMA (must be #define) |
| 35 | * if defined, ulzma() must exist for decompression of data streams |
| 36 | * |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 37 | * ERROR(x...) |
| 38 | * print an error message x (in printf format) |
| 39 | * |
| 40 | * LOG(x...) |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 41 | * print a message x (in printf format) |
| 42 | * |
| 43 | * DEBUG(x...) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 44 | * print a debug message x (in printf format) |
| 45 | * |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 46 | */ |
| 47 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 48 | #include <cbfs.h> |
| 49 | #include <string.h> |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 50 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 51 | /* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS |
| 52 | * on failure */ |
| 53 | const struct cbfs_header *cbfs_get_header(struct cbfs_media *media) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 54 | { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 55 | int32_t rel_offset; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 56 | const struct cbfs_header *header; |
| 57 | struct cbfs_media default_media; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 58 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 59 | if (media == CBFS_DEFAULT_MEDIA) { |
| 60 | media = &default_media; |
| 61 | if (init_default_cbfs_media(media) != 0) { |
Patrick Georgi | 46cb96bb | 2013-02-19 17:59:21 +0100 | [diff] [blame] | 62 | ERROR("Failed to initialize default media.\n"); |
| 63 | return CBFS_HEADER_INVALID_ADDRESS; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 64 | } |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 65 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 66 | media->open(media); |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 67 | |
| 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 Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 76 | media->close(media); |
| 77 | |
| 78 | if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 79 | ERROR("Failed to load CBFS header from %#zx(-%#zx)\n", |
| 80 | (size_t)rel_offset, (size_t)-rel_offset); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 81 | return CBFS_HEADER_INVALID_ADDRESS; |
| 82 | } |
| 83 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 84 | if (CBFS_HEADER_MAGIC != ntohl(header->magic)) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 85 | 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 Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 88 | ntohl(header->magic)); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 89 | if (header->magic == 0xffffffff) { |
| 90 | ERROR("Maybe ROM is not mapped properly?\n"); |
| 91 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 92 | return CBFS_HEADER_INVALID_ADDRESS; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 93 | } |
| 94 | return header; |
| 95 | } |
| 96 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 97 | /* public API starts here*/ |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 98 | struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 99 | { |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 100 | const char *vardata; |
| 101 | uint32_t offset, romsize, vardata_len; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 102 | const struct cbfs_header *header; |
| 103 | struct cbfs_file file, *file_ptr; |
| 104 | struct cbfs_media default_media; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 105 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 106 | if (media == CBFS_DEFAULT_MEDIA) { |
| 107 | media = &default_media; |
| 108 | if (init_default_cbfs_media(media) != 0) { |
Patrick Georgi | 46cb96bb | 2013-02-19 17:59:21 +0100 | [diff] [blame] | 109 | ERROR("Failed to initialize default media.\n"); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 113 | |
| 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); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 119 | romsize = ntohl(header->romsize); |
| 120 | |
Hung-Te Lin | fbf0783 | 2013-02-20 15:50:06 +0800 | [diff] [blame] | 121 | // TODO Add a "size" in CBFS header for a platform independent way to |
| 122 | // determine the end of CBFS data. |
Stefan Reinauer | 1b4d394 | 2015-06-29 15:47:34 -0700 | [diff] [blame] | 123 | #if IS_ENABLED(CONFIG_LP_ARCH_X86) |
Steven Sherk | e17843c | 2013-08-14 14:55:57 -0600 | [diff] [blame] | 124 | // resolve actual length of ROM used for CBFS components |
| 125 | // the bootblock size was not taken into account |
| 126 | romsize -= ntohl(header->bootblocksize); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 127 | |
Steven Sherk | e17843c | 2013-08-14 14:55:57 -0600 | [diff] [blame] | 128 | // fine tune the length to handle alignment positioning. |
| 129 | // using (bootblock size) % align, to derive the |
| 130 | // number of bytes the bootblock is off from the alignment size. |
Patrick Georgi | 2272b80 | 2015-07-14 22:29:04 +0200 | [diff] [blame] | 131 | if ((ntohl(header->bootblocksize) % CBFS_ALIGNMENT)) |
| 132 | romsize -= (CBFS_ALIGNMENT - |
| 133 | (ntohl(header->bootblocksize) % CBFS_ALIGNMENT)); |
Steven Sherk | e17843c | 2013-08-14 14:55:57 -0600 | [diff] [blame] | 134 | else |
| 135 | romsize -= 1; |
| 136 | #endif |
| 137 | |
Patrick Georgi | 2272b80 | 2015-07-14 22:29:04 +0200 | [diff] [blame] | 138 | DEBUG("CBFS location: 0x%x~0x%x\n", offset, romsize); |
Dave Frodin | 37f8c3a | 2013-05-06 15:51:39 -0600 | [diff] [blame] | 139 | DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset); |
Steven Sherk | e17843c | 2013-08-14 14:55:57 -0600 | [diff] [blame] | 140 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 141 | 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) { |
Patrick Georgi | 2272b80 | 2015-07-14 22:29:04 +0200 | [diff] [blame] | 146 | uint32_t new_align = CBFS_ALIGNMENT; |
| 147 | if (offset % CBFS_ALIGNMENT) |
| 148 | new_align += CBFS_ALIGNMENT - |
| 149 | (offset % CBFS_ALIGNMENT); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 150 | ERROR("ERROR: No file header found at 0x%xx - " |
| 151 | "try next aligned address: 0x%x.\n", offset, |
| 152 | offset + new_align); |
| 153 | offset += new_align; |
| 154 | continue; |
| 155 | } |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 156 | vardata_len = ntohl(file.offset) - sizeof(file); |
| 157 | DEBUG(" - load entry 0x%x variable data (%d bytes)...\n", |
| 158 | offset, vardata_len); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 159 | |
| 160 | // load file name (arbitrary length). |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 161 | vardata = (const char*)media->map( |
| 162 | media, offset + sizeof(file), vardata_len); |
| 163 | if (vardata == CBFS_MEDIA_INVALID_MAP_ADDRESS) { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 164 | ERROR("ERROR: Failed to get filename: 0x%x.\n", offset); |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 165 | } else if (strcmp(vardata, name) == 0) { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 166 | int file_offset = ntohl(file.offset), |
| 167 | file_len = ntohl(file.len); |
Dave Frodin | 37f8c3a | 2013-05-06 15:51:39 -0600 | [diff] [blame] | 168 | DEBUG("Found file (offset=0x%x, len=%d).\n", |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 169 | offset + file_offset, file_len); |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 170 | media->unmap(media, vardata); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 171 | file_ptr = media->map(media, offset, |
| 172 | file_offset + file_len); |
| 173 | media->close(media); |
| 174 | return file_ptr; |
| 175 | } else { |
Aaron Durbin | 3eddcff | 2013-05-01 08:40:13 -0500 | [diff] [blame] | 176 | DEBUG(" (unmatched file @0x%x: %s)\n", offset, |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 177 | vardata); |
| 178 | media->unmap(media, vardata); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Move to next file. |
| 182 | offset += ntohl(file.len) + ntohl(file.offset); |
Patrick Georgi | 2272b80 | 2015-07-14 22:29:04 +0200 | [diff] [blame] | 183 | if (offset % CBFS_ALIGNMENT) |
| 184 | offset += CBFS_ALIGNMENT - (offset % CBFS_ALIGNMENT); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 185 | } |
| 186 | media->close(media); |
Dave Frodin | 37f8c3a | 2013-05-06 15:51:39 -0600 | [diff] [blame] | 187 | LOG("WARNING: '%s' not found.\n", name); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
| 190 | |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 191 | void *cbfs_get_file_content(struct cbfs_media *media, const char *name, |
| 192 | int type, size_t *sz) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 193 | { |
Nico Huber | bda38ee | 2015-10-05 12:08:24 +0200 | [diff] [blame^] | 194 | struct cbfs_media default_media; |
| 195 | |
| 196 | if (media == CBFS_DEFAULT_MEDIA) { |
| 197 | media = &default_media; |
| 198 | if (init_default_cbfs_media(media) != 0) { |
| 199 | ERROR("Failed to initialize default media.\n"); |
| 200 | return NULL; |
| 201 | } |
| 202 | } |
| 203 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 204 | struct cbfs_file *file = cbfs_get_file(media, name); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 205 | |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 206 | if (sz) |
| 207 | *sz = 0; |
| 208 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 209 | if (file == NULL) { |
| 210 | ERROR("Could not find file '%s'.\n", name); |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | if (ntohl(file->type) != type) { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 215 | ERROR("File '%s' is of type %x, but we requested %x.\n", name, |
| 216 | ntohl(file->type), type); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | |
Daisuke Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 220 | void *file_content = (void *)CBFS_SUBHEADER(file); |
| 221 | |
Patrick Georgi | eb33b3f | 2015-09-17 20:45:52 +0200 | [diff] [blame] | 222 | struct cbfs_file_attribute *attr = |
| 223 | cbfs_file_find_attr(file, CBFS_FILE_ATTR_TAG_COMPRESSION); |
Daisuke Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 224 | |
Nico Huber | ac1f4b8 | 2015-10-02 19:38:24 +0200 | [diff] [blame] | 225 | size_t final_size = ntohl(file->len); |
Daisuke Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 226 | int compression_algo = CBFS_COMPRESS_NONE; |
| 227 | if (attr) { |
| 228 | struct cbfs_file_attr_compression *comp = |
| 229 | (struct cbfs_file_attr_compression *)attr; |
| 230 | compression_algo = ntohl(comp->compression); |
Daisuke Nojiri | 9dcf457 | 2015-09-23 13:01:14 -0700 | [diff] [blame] | 231 | DEBUG("File '%s' is compressed (alg=%d)\n", |
| 232 | name, compression_algo); |
Nico Huber | ac1f4b8 | 2015-10-02 19:38:24 +0200 | [diff] [blame] | 233 | final_size = ntohl(comp->decompressed_size); |
Daisuke Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Nico Huber | ac1f4b8 | 2015-10-02 19:38:24 +0200 | [diff] [blame] | 236 | void *dst = malloc(final_size); |
Daisuke Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 237 | if (dst == NULL) |
| 238 | goto err; |
| 239 | |
Nico Huber | ac1f4b8 | 2015-10-02 19:38:24 +0200 | [diff] [blame] | 240 | if (!cbfs_decompress(compression_algo, file_content, dst, final_size)) |
Daisuke Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 241 | goto err; |
| 242 | |
Nico Huber | ac1f4b8 | 2015-10-02 19:38:24 +0200 | [diff] [blame] | 243 | if (sz) |
| 244 | *sz = final_size; |
| 245 | |
Daisuke Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 246 | media->unmap(media, file); |
| 247 | return dst; |
| 248 | |
| 249 | err: |
| 250 | media->unmap(media, file); |
| 251 | free(dst); |
| 252 | return NULL; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Patrick Georgi | 377d1db | 2015-09-16 18:53:40 +0200 | [diff] [blame] | 255 | struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file) |
| 256 | { |
| 257 | /* attributes_offset should be 0 when there is no attribute, but all |
| 258 | * values that point into the cbfs_file header are invalid, too. */ |
| 259 | if (ntohl(file->attributes_offset) <= sizeof(*file)) |
| 260 | return NULL; |
| 261 | |
| 262 | /* There needs to be enough space for the file header and one |
| 263 | * attribute header for this to make sense. */ |
| 264 | if (ntohl(file->offset) <= |
| 265 | sizeof(*file) + sizeof(struct cbfs_file_attribute)) |
| 266 | return NULL; |
| 267 | |
| 268 | return (struct cbfs_file_attribute *) |
| 269 | (((uint8_t *)file) + ntohl(file->attributes_offset)); |
| 270 | } |
| 271 | |
| 272 | struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file, |
| 273 | struct cbfs_file_attribute *attr) |
| 274 | { |
| 275 | /* ex falso sequitur quodlibet */ |
| 276 | if (attr == NULL) |
| 277 | return NULL; |
| 278 | |
| 279 | /* Is there enough space for another attribute? */ |
| 280 | if ((uint8_t *)attr + ntohl(attr->len) + |
| 281 | sizeof(struct cbfs_file_attribute) >= |
| 282 | (uint8_t *)file + ntohl(file->offset)) |
| 283 | return NULL; |
| 284 | |
| 285 | struct cbfs_file_attribute *next = (struct cbfs_file_attribute *) |
| 286 | (((uint8_t *)attr) + ntohl(attr->len)); |
| 287 | /* If any, "unused" attributes must come last. */ |
| 288 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED) |
| 289 | return NULL; |
| 290 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2) |
| 291 | return NULL; |
| 292 | |
| 293 | return next; |
| 294 | } |
| 295 | |
Patrick Georgi | eb33b3f | 2015-09-17 20:45:52 +0200 | [diff] [blame] | 296 | struct cbfs_file_attribute *cbfs_file_find_attr(struct cbfs_file *file, |
| 297 | uint32_t tag) |
| 298 | { |
| 299 | struct cbfs_file_attribute *attr = cbfs_file_first_attr(file); |
| 300 | while (attr) { |
| 301 | if (ntohl(attr->tag) == tag) |
| 302 | break; |
| 303 | attr = cbfs_file_next_attr(file, attr); |
| 304 | } |
| 305 | return attr; |
| 306 | |
| 307 | } |
| 308 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 309 | int cbfs_decompress(int algo, void *src, void *dst, int len) |
| 310 | { |
| 311 | switch (algo) { |
| 312 | case CBFS_COMPRESS_NONE: |
| 313 | memcpy(dst, src, len); |
Gabe Black | 0c605a5 | 2013-07-01 04:57:37 -0700 | [diff] [blame] | 314 | return len; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 315 | #ifdef CBFS_CORE_WITH_LZMA |
| 316 | case CBFS_COMPRESS_LZMA: |
Gabe Black | 0c605a5 | 2013-07-01 04:57:37 -0700 | [diff] [blame] | 317 | return ulzma(src, dst); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 318 | #endif |
| 319 | default: |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 320 | ERROR("tried to decompress %d bytes with algorithm #%x," |
| 321 | "but that algorithm id is unsupported.\n", len, |
| 322 | algo); |
Gabe Black | 0c605a5 | 2013-07-01 04:57:37 -0700 | [diff] [blame] | 323 | return 0; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 324 | } |
| 325 | } |