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 | * |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 37 | * CBFS_HEADER_ROM_ADDRESS |
| 38 | * ROM address (offset) of CBFS header. Underlying CBFS media may interpret |
| 39 | * it in other way so we call this "address". |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 40 | * |
| 41 | * ERROR(x...) |
| 42 | * print an error message x (in printf format) |
| 43 | * |
| 44 | * LOG(x...) |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 45 | * print a message x (in printf format) |
| 46 | * |
| 47 | * DEBUG(x...) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 48 | * print a debug message x (in printf format) |
| 49 | * |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 50 | */ |
| 51 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 52 | #include <cbfs.h> |
| 53 | #include <string.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 | { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 59 | const struct cbfs_header *header; |
| 60 | struct cbfs_media default_media; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 61 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 62 | if (media == CBFS_DEFAULT_MEDIA) { |
| 63 | media = &default_media; |
| 64 | if (init_default_cbfs_media(media) != 0) { |
Patrick Georgi | 46cb96bb | 2013-02-19 17:59:21 +0100 | [diff] [blame] | 65 | ERROR("Failed to initialize default media.\n"); |
| 66 | return CBFS_HEADER_INVALID_ADDRESS; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 67 | } |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 68 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 69 | |
| 70 | media->open(media); |
| 71 | DEBUG("CBFS_HEADER_ROM_ADDRESS: 0x%x/0x%x\n", CBFS_HEADER_ROM_ADDRESS, |
| 72 | CONFIG_ROM_SIZE); |
| 73 | header = media->map(media, CBFS_HEADER_ROM_ADDRESS, sizeof(*header)); |
| 74 | media->close(media); |
| 75 | |
| 76 | if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) { |
| 77 | ERROR("Failed to load CBFS header from 0x%x\n", |
| 78 | CBFS_HEADER_ROM_ADDRESS); |
| 79 | return CBFS_HEADER_INVALID_ADDRESS; |
| 80 | } |
| 81 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 82 | if (CBFS_HEADER_MAGIC != ntohl(header->magic)) { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 83 | ERROR("Could not find valid CBFS master header at %x: " |
| 84 | "%x vs %x.\n", CBFS_HEADER_ROM_ADDRESS, CBFS_HEADER_MAGIC, |
| 85 | ntohl(header->magic)); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 86 | if (header->magic == 0xffffffff) { |
| 87 | ERROR("Maybe ROM is not mapped properly?\n"); |
| 88 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 89 | return CBFS_HEADER_INVALID_ADDRESS; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 90 | } |
| 91 | return header; |
| 92 | } |
| 93 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 94 | /* public API starts here*/ |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 95 | 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] | 96 | { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 97 | const char *file_name; |
| 98 | uint32_t offset, align, romsize, name_len; |
| 99 | const struct cbfs_header *header; |
| 100 | struct cbfs_file file, *file_ptr; |
| 101 | struct cbfs_media default_media; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 102 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 103 | if (media == CBFS_DEFAULT_MEDIA) { |
| 104 | media = &default_media; |
| 105 | if (init_default_cbfs_media(media) != 0) { |
Patrick Georgi | 46cb96bb | 2013-02-19 17:59:21 +0100 | [diff] [blame] | 106 | ERROR("Failed to initialize default media.\n"); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 110 | |
| 111 | if (CBFS_HEADER_INVALID_ADDRESS == (header = cbfs_get_header(media))) |
| 112 | return NULL; |
| 113 | |
| 114 | // Logical offset (for source media) of first file. |
| 115 | offset = ntohl(header->offset); |
| 116 | align = ntohl(header->align); |
| 117 | romsize = ntohl(header->romsize); |
| 118 | |
Hung-Te Lin | fbf0783 | 2013-02-20 15:50:06 +0800 | [diff] [blame] | 119 | // TODO Add a "size" in CBFS header for a platform independent way to |
| 120 | // determine the end of CBFS data. |
| 121 | #if defined(CONFIG_ARCH_X86) && CONFIG_ARCH_X86 |
| 122 | romsize -= htonl(header->bootblocksize); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 123 | #endif |
Hung-Te Lin | fbf0783 | 2013-02-20 15:50:06 +0800 | [diff] [blame] | 124 | DEBUG("CBFS location: 0x%x~0x%x, align: %d\n", offset, romsize, align); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 125 | |
| 126 | LOG("Looking for '%s' starting from 0x%x.\n", name, offset); |
| 127 | media->open(media); |
| 128 | while (offset < romsize && |
| 129 | media->read(media, &file, offset, sizeof(file)) == sizeof(file)) { |
| 130 | if (memcmp(CBFS_FILE_MAGIC, file.magic, |
| 131 | sizeof(file.magic)) != 0) { |
| 132 | uint32_t new_align = align; |
| 133 | if (offset % align) |
| 134 | new_align += align - (offset % align); |
| 135 | ERROR("ERROR: No file header found at 0x%xx - " |
| 136 | "try next aligned address: 0x%x.\n", offset, |
| 137 | offset + new_align); |
| 138 | offset += new_align; |
| 139 | continue; |
| 140 | } |
| 141 | name_len = ntohl(file.offset) - sizeof(file); |
| 142 | DEBUG(" - load entry 0x%x file name (%d bytes)...\n", offset, |
| 143 | name_len); |
| 144 | |
| 145 | // load file name (arbitrary length). |
| 146 | file_name = (const char*)media->map( |
| 147 | media, offset + sizeof(file), name_len); |
| 148 | if (file_name == CBFS_MEDIA_INVALID_MAP_ADDRESS) { |
| 149 | ERROR("ERROR: Failed to get filename: 0x%x.\n", offset); |
| 150 | } else if (strcmp(file_name, name) == 0) { |
| 151 | int file_offset = ntohl(file.offset), |
| 152 | file_len = ntohl(file.len); |
| 153 | LOG("Found file (offset=0x%x, len=%d).\n", |
| 154 | offset + file_offset, file_len); |
| 155 | media->unmap(media, file_name); |
| 156 | file_ptr = media->map(media, offset, |
| 157 | file_offset + file_len); |
| 158 | media->close(media); |
| 159 | return file_ptr; |
| 160 | } else { |
Aaron Durbin | 3eddcff | 2013-05-01 08:40:13 -0500 | [diff] [blame] | 161 | DEBUG(" (unmatched file @0x%x: %s)\n", offset, |
| 162 | file_name); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 163 | media->unmap(media, file_name); |
| 164 | } |
| 165 | |
| 166 | // Move to next file. |
| 167 | offset += ntohl(file.len) + ntohl(file.offset); |
| 168 | if (offset % align) |
| 169 | offset += align - (offset % align); |
| 170 | } |
| 171 | media->close(media); |
Shawn Nematbakhsh | c322118 | 2013-02-25 12:12:05 -0800 | [diff] [blame] | 172 | LOG("WARNING: Not found.\n"); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 173 | return NULL; |
| 174 | } |
| 175 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 176 | void *cbfs_get_file_content(struct cbfs_media *media, const char *name, int type) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 177 | { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 178 | struct cbfs_file *file = cbfs_get_file(media, name); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 179 | |
| 180 | if (file == NULL) { |
| 181 | ERROR("Could not find file '%s'.\n", name); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | if (ntohl(file->type) != type) { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 186 | ERROR("File '%s' is of type %x, but we requested %x.\n", name, |
| 187 | ntohl(file->type), type); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | return (void*)CBFS_SUBHEADER(file); |
| 192 | } |
| 193 | |
| 194 | int cbfs_decompress(int algo, void *src, void *dst, int len) |
| 195 | { |
| 196 | switch (algo) { |
| 197 | case CBFS_COMPRESS_NONE: |
| 198 | memcpy(dst, src, len); |
| 199 | return 0; |
| 200 | #ifdef CBFS_CORE_WITH_LZMA |
| 201 | case CBFS_COMPRESS_LZMA: |
| 202 | if (ulzma(src, dst) != 0) { |
| 203 | return 0; |
| 204 | } |
| 205 | return -1; |
| 206 | #endif |
| 207 | default: |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 208 | ERROR("tried to decompress %d bytes with algorithm #%x," |
| 209 | "but that algorithm id is unsupported.\n", len, |
| 210 | algo); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 211 | return -1; |
| 212 | } |
| 213 | } |
| 214 | |