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 | |
| 119 | // TODO header->romsize seems broken now on ARM. Remove this when it's |
| 120 | // fixed. |
| 121 | #if defined(CONFIG_ARCH_ARMV7) && CONFIG_ARCH_ARMV7 |
| 122 | romsize = CONFIG_ROM_SIZE; |
| 123 | #endif |
| 124 | DEBUG("offset: 0x%x, align: %d, romsize: %d\n", offset, align, romsize); |
| 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 { |
| 161 | LOG(" (unmatched file @0x%x: %s)\n", offset, file_name); |
| 162 | media->unmap(media, file_name); |
| 163 | } |
| 164 | |
| 165 | // Move to next file. |
| 166 | offset += ntohl(file.len) + ntohl(file.offset); |
| 167 | if (offset % align) |
| 168 | offset += align - (offset % align); |
| 169 | } |
| 170 | media->close(media); |
| 171 | ERROR("ERROR: Not found.\n"); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 172 | return NULL; |
| 173 | } |
| 174 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 175 | 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] | 176 | { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 177 | struct cbfs_file *file = cbfs_get_file(media, name); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 178 | |
| 179 | if (file == NULL) { |
| 180 | ERROR("Could not find file '%s'.\n", name); |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | if (ntohl(file->type) != type) { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 185 | ERROR("File '%s' is of type %x, but we requested %x.\n", name, |
| 186 | ntohl(file->type), type); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | return (void*)CBFS_SUBHEADER(file); |
| 191 | } |
| 192 | |
| 193 | int cbfs_decompress(int algo, void *src, void *dst, int len) |
| 194 | { |
| 195 | switch (algo) { |
| 196 | case CBFS_COMPRESS_NONE: |
| 197 | memcpy(dst, src, len); |
| 198 | return 0; |
| 199 | #ifdef CBFS_CORE_WITH_LZMA |
| 200 | case CBFS_COMPRESS_LZMA: |
| 201 | if (ulzma(src, dst) != 0) { |
| 202 | return 0; |
| 203 | } |
| 204 | return -1; |
| 205 | #endif |
| 206 | default: |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 207 | ERROR("tried to decompress %d bytes with algorithm #%x," |
| 208 | "but that algorithm id is unsupported.\n", len, |
| 209 | algo); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 210 | return -1; |
| 211 | } |
| 212 | } |
| 213 | |