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 | * |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 37 | * CBFS_CORE_WITH_LZ4 (must be #define) |
| 38 | * if defined, ulz4f() must exist for decompression of data streams |
| 39 | * |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 40 | * ERROR(x...) |
| 41 | * print an error message x (in printf format) |
| 42 | * |
| 43 | * LOG(x...) |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 44 | * print a message x (in printf format) |
| 45 | * |
| 46 | * DEBUG(x...) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 47 | * print a debug message x (in printf format) |
| 48 | * |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 49 | */ |
| 50 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 51 | #include <cbfs.h> |
| 52 | #include <string.h> |
Daisuke Nojiri | 2ccb280 | 2015-09-02 10:53:13 -0700 | [diff] [blame] | 53 | #include <sysinfo.h> |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 54 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 55 | /* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS |
| 56 | * on failure */ |
| 57 | const struct cbfs_header *cbfs_get_header(struct cbfs_media *media) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 58 | { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 59 | int32_t rel_offset; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 60 | const struct cbfs_header *header; |
| 61 | struct cbfs_media default_media; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 62 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 63 | if (media == CBFS_DEFAULT_MEDIA) { |
| 64 | media = &default_media; |
| 65 | if (init_default_cbfs_media(media) != 0) { |
Patrick Georgi | 46cb96bb | 2013-02-19 17:59:21 +0100 | [diff] [blame] | 66 | ERROR("Failed to initialize default media.\n"); |
| 67 | return CBFS_HEADER_INVALID_ADDRESS; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 68 | } |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 69 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 70 | media->open(media); |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 71 | |
| 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 Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 80 | media->close(media); |
| 81 | |
| 82 | if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 83 | ERROR("Failed to load CBFS header from %#zx(-%#zx)\n", |
| 84 | (size_t)rel_offset, (size_t)-rel_offset); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 85 | return CBFS_HEADER_INVALID_ADDRESS; |
| 86 | } |
| 87 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 88 | if (CBFS_HEADER_MAGIC != ntohl(header->magic)) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 89 | 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 Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 92 | ntohl(header->magic)); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 93 | if (header->magic == 0xffffffff) { |
| 94 | ERROR("Maybe ROM is not mapped properly?\n"); |
| 95 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 96 | return CBFS_HEADER_INVALID_ADDRESS; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 97 | } |
| 98 | return header; |
| 99 | } |
| 100 | |
Daisuke Nojiri | 2ccb280 | 2015-09-02 10:53:13 -0700 | [diff] [blame] | 101 | static 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 Georgi | f86d035 | 2015-10-23 20:25:03 +0200 | [diff] [blame] | 106 | if (media == CBFS_DEFAULT_MEDIA && |
| 107 | lib_sysinfo.cbfs_offset && lib_sysinfo.cbfs_size) { |
Daisuke Nojiri | 2ccb280 | 2015-09-02 10:53:13 -0700 | [diff] [blame] | 108 | *offset = lib_sysinfo.cbfs_offset; |
| 109 | *cbfs_end = *offset + lib_sysinfo.cbfs_size; |
| 110 | return 0; |
| 111 | } |
| 112 | |
Daisuke Nojiri | 03688ec | 2015-11-19 09:10:41 -0800 | [diff] [blame] | 113 | /* read offset and size from cbfs master header */ |
| 114 | DEBUG("Read CBFS offset & size from master header\n"); |
Daisuke Nojiri | 2ccb280 | 2015-09-02 10:53:13 -0700 | [diff] [blame] | 115 | 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 Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 138 | /* public API starts here*/ |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 139 | struct cbfs_handle *cbfs_get_handle(struct cbfs_media *media, const char *name) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 140 | { |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 141 | const char *vardata; |
Daisuke Nojiri | 2ccb280 | 2015-09-02 10:53:13 -0700 | [diff] [blame] | 142 | uint32_t offset, cbfs_end, vardata_len; |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 143 | struct cbfs_file file; |
| 144 | struct cbfs_handle *handle = malloc(sizeof(*handle)); |
| 145 | |
| 146 | if (!handle) |
| 147 | return NULL; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 148 | |
Daisuke Nojiri | 03688ec | 2015-11-19 09:10:41 -0800 | [diff] [blame] | 149 | if (get_cbfs_range(&offset, &cbfs_end, media)) { |
| 150 | ERROR("Failed to find cbfs range\n"); |
Julius Werner | 55ffccf | 2016-08-12 12:50:42 -0700 | [diff] [blame] | 151 | free(handle); |
Daisuke Nojiri | 03688ec | 2015-11-19 09:10:41 -0800 | [diff] [blame] | 152 | return NULL; |
| 153 | } |
| 154 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 155 | if (media == CBFS_DEFAULT_MEDIA) { |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 156 | media = &handle->media; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 157 | if (init_default_cbfs_media(media) != 0) { |
Patrick Georgi | 46cb96bb | 2013-02-19 17:59:21 +0100 | [diff] [blame] | 158 | ERROR("Failed to initialize default media.\n"); |
Julius Werner | 55ffccf | 2016-08-12 12:50:42 -0700 | [diff] [blame] | 159 | free(handle); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 160 | return NULL; |
| 161 | } |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 162 | } else { |
| 163 | memcpy(&handle->media, media, sizeof(*media)); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 164 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 165 | |
Daisuke Nojiri | 2ccb280 | 2015-09-02 10:53:13 -0700 | [diff] [blame] | 166 | DEBUG("CBFS location: 0x%x~0x%x\n", offset, cbfs_end); |
Dave Frodin | 37f8c3a | 2013-05-06 15:51:39 -0600 | [diff] [blame] | 167 | DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset); |
Steven Sherk | e17843c | 2013-08-14 14:55:57 -0600 | [diff] [blame] | 168 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 169 | media->open(media); |
Daisuke Nojiri | 2ccb280 | 2015-09-02 10:53:13 -0700 | [diff] [blame] | 170 | while (offset < cbfs_end && |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 171 | media->read(media, &file, offset, sizeof(file)) == sizeof(file)) { |
| 172 | if (memcmp(CBFS_FILE_MAGIC, file.magic, |
| 173 | sizeof(file.magic)) != 0) { |
Patrick Georgi | 2272b80 | 2015-07-14 22:29:04 +0200 | [diff] [blame] | 174 | uint32_t new_align = CBFS_ALIGNMENT; |
| 175 | if (offset % CBFS_ALIGNMENT) |
| 176 | new_align += CBFS_ALIGNMENT - |
| 177 | (offset % CBFS_ALIGNMENT); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 178 | ERROR("ERROR: No file header found at 0x%xx - " |
| 179 | "try next aligned address: 0x%x.\n", offset, |
| 180 | offset + new_align); |
| 181 | offset += new_align; |
| 182 | continue; |
| 183 | } |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 184 | vardata_len = ntohl(file.offset) - sizeof(file); |
| 185 | DEBUG(" - load entry 0x%x variable data (%d bytes)...\n", |
| 186 | offset, vardata_len); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 187 | |
| 188 | // load file name (arbitrary length). |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 189 | vardata = (const char*)media->map( |
| 190 | media, offset + sizeof(file), vardata_len); |
| 191 | if (vardata == CBFS_MEDIA_INVALID_MAP_ADDRESS) { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 192 | ERROR("ERROR: Failed to get filename: 0x%x.\n", offset); |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 193 | } else if (strcmp(vardata, name) == 0) { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 194 | int file_offset = ntohl(file.offset), |
| 195 | file_len = ntohl(file.len); |
Dave Frodin | 37f8c3a | 2013-05-06 15:51:39 -0600 | [diff] [blame] | 196 | DEBUG("Found file (offset=0x%x, len=%d).\n", |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 197 | offset + file_offset, file_len); |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 198 | media->unmap(media, vardata); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 199 | media->close(media); |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 200 | handle->type = ntohl(file.type); |
| 201 | handle->media_offset = offset; |
| 202 | handle->content_offset = file_offset; |
| 203 | handle->content_size = file_len; |
| 204 | handle->attribute_offset = |
| 205 | ntohl(file.attributes_offset); |
| 206 | return handle; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 207 | } else { |
Aaron Durbin | 3eddcff | 2013-05-01 08:40:13 -0500 | [diff] [blame] | 208 | DEBUG(" (unmatched file @0x%x: %s)\n", offset, |
Daisuke Nojiri | 5c6dc72 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 209 | vardata); |
| 210 | media->unmap(media, vardata); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Move to next file. |
| 214 | offset += ntohl(file.len) + ntohl(file.offset); |
Patrick Georgi | 2272b80 | 2015-07-14 22:29:04 +0200 | [diff] [blame] | 215 | if (offset % CBFS_ALIGNMENT) |
| 216 | offset += CBFS_ALIGNMENT - (offset % CBFS_ALIGNMENT); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 217 | } |
| 218 | media->close(media); |
Dave Frodin | 37f8c3a | 2013-05-06 15:51:39 -0600 | [diff] [blame] | 219 | LOG("WARNING: '%s' not found.\n", name); |
Julius Werner | 55ffccf | 2016-08-12 12:50:42 -0700 | [diff] [blame] | 220 | free(handle); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 224 | void *cbfs_get_contents(struct cbfs_handle *handle, size_t *size, size_t limit) |
| 225 | { |
| 226 | struct cbfs_media *m = &handle->media; |
| 227 | size_t on_media_size = handle->content_size; |
| 228 | int algo = CBFS_COMPRESS_NONE; |
| 229 | void *ret = NULL; |
| 230 | size_t dummy_size; |
| 231 | |
| 232 | if (!size) |
| 233 | size = &dummy_size; |
| 234 | |
| 235 | struct cbfs_file_attr_compression *comp = |
| 236 | cbfs_get_attr(handle, CBFS_FILE_ATTR_TAG_COMPRESSION); |
| 237 | if (comp) { |
| 238 | algo = ntohl(comp->compression); |
| 239 | DEBUG("File '%s' is compressed (alg=%d)\n", name, algo); |
| 240 | *size = ntohl(comp->decompressed_size); |
| 241 | /* TODO: Implement partial decompression with |limit| */ |
| 242 | } |
| 243 | |
| 244 | if (algo == CBFS_COMPRESS_NONE) { |
| 245 | if (limit != 0 && limit < on_media_size) { |
| 246 | *size = limit; |
| 247 | on_media_size = limit; |
| 248 | } else { |
| 249 | *size = on_media_size; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void *data = m->map(m, handle->media_offset + handle->content_offset, |
| 254 | on_media_size); |
| 255 | if (data == CBFS_MEDIA_INVALID_MAP_ADDRESS) |
| 256 | return NULL; |
| 257 | |
| 258 | ret = malloc(*size); |
| 259 | if (ret != NULL && !cbfs_decompress(algo, data, ret, *size)) { |
| 260 | free(ret); |
| 261 | ret = NULL; |
| 262 | } |
| 263 | |
| 264 | m->unmap(m, data); |
| 265 | return ret; |
| 266 | } |
| 267 | |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 268 | void *cbfs_get_file_content(struct cbfs_media *media, const char *name, |
| 269 | int type, size_t *sz) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 270 | { |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 271 | void *ret = NULL; |
| 272 | struct cbfs_handle *handle = cbfs_get_handle(media, name); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 273 | |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 274 | if (!handle) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 275 | return NULL; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 276 | |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 277 | if (handle->type == type) |
| 278 | ret = cbfs_get_contents(handle, sz, 0); |
| 279 | else |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 280 | ERROR("File '%s' is of type %x, but we requested %x.\n", name, |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 281 | handle->type, type); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 282 | |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 283 | free(handle); |
| 284 | return ret; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 287 | void *cbfs_get_attr(struct cbfs_handle *handle, uint32_t tag) |
Patrick Georgi | 377d1db | 2015-09-16 18:53:40 +0200 | [diff] [blame] | 288 | { |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 289 | struct cbfs_media *m = &handle->media; |
| 290 | uint32_t offset = handle->media_offset + handle->attribute_offset; |
| 291 | uint32_t end = handle->media_offset + handle->content_offset; |
| 292 | struct cbfs_file_attribute attr; |
| 293 | void *ret; |
| 294 | |
| 295 | /* attribute_offset should be 0 when there is no attribute, but all |
Patrick Georgi | 377d1db | 2015-09-16 18:53:40 +0200 | [diff] [blame] | 296 | * values that point into the cbfs_file header are invalid, too. */ |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 297 | if (handle->attribute_offset <= sizeof(struct cbfs_file)) |
Patrick Georgi | 377d1db | 2015-09-16 18:53:40 +0200 | [diff] [blame] | 298 | return NULL; |
| 299 | |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 300 | m->open(m); |
| 301 | while (offset + sizeof(attr) <= end) { |
| 302 | if (m->read(m, &attr, offset, sizeof(attr)) != sizeof(attr)) { |
| 303 | ERROR("Failed to read attribute header %#x\n", offset); |
| 304 | m->close(m); |
| 305 | return NULL; |
| 306 | } |
| 307 | if (ntohl(attr.tag) != tag) { |
| 308 | offset += ntohl(attr.len); |
| 309 | continue; |
| 310 | } |
| 311 | ret = m->map(m, offset, ntohl(attr.len)); |
| 312 | if (ret == CBFS_MEDIA_INVALID_MAP_ADDRESS) { |
| 313 | ERROR("Failed to map attribute at %#x\n", offset); |
| 314 | m->close(m); |
| 315 | return NULL; |
| 316 | } |
| 317 | return ret; |
Patrick Georgi | eb33b3f | 2015-09-17 20:45:52 +0200 | [diff] [blame] | 318 | } |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 319 | m->close(m); |
Patrick Georgi | eb33b3f | 2015-09-17 20:45:52 +0200 | [diff] [blame] | 320 | |
Julius Werner | 2296479 | 2016-05-12 14:40:57 -0700 | [diff] [blame] | 321 | return NULL; |
Patrick Georgi | eb33b3f | 2015-09-17 20:45:52 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 324 | int cbfs_decompress(int algo, void *src, void *dst, int len) |
| 325 | { |
| 326 | switch (algo) { |
| 327 | case CBFS_COMPRESS_NONE: |
| 328 | memcpy(dst, src, len); |
Gabe Black | 0c605a5 | 2013-07-01 04:57:37 -0700 | [diff] [blame] | 329 | return len; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 330 | #ifdef CBFS_CORE_WITH_LZMA |
| 331 | case CBFS_COMPRESS_LZMA: |
Gabe Black | 0c605a5 | 2013-07-01 04:57:37 -0700 | [diff] [blame] | 332 | return ulzma(src, dst); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 333 | #endif |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 334 | #ifdef CBFS_CORE_WITH_LZ4 |
| 335 | case CBFS_COMPRESS_LZ4: |
| 336 | return ulz4f(src, dst); |
| 337 | #endif |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 338 | default: |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 339 | ERROR("tried to decompress %d bytes with algorithm #%x," |
| 340 | "but that algorithm id is unsupported.\n", len, |
| 341 | algo); |
Gabe Black | 0c605a5 | 2013-07-01 04:57:37 -0700 | [diff] [blame] | 342 | return 0; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 343 | } |
| 344 | } |