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 | */ |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 30 | #define LIBPAYLOAD |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 31 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 32 | #ifdef LIBPAYLOAD |
| 33 | # include <libpayload-config.h> |
Gabe Black | 1ee2c6d | 2013-08-09 04:27:35 -0700 | [diff] [blame] | 34 | # ifdef CONFIG_LP_LZMA |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 35 | # include <lzma.h> |
| 36 | # define CBFS_CORE_WITH_LZMA |
| 37 | # endif |
| 38 | # define CBFS_MINI_BUILD |
| 39 | #elif defined(__SMM__) |
| 40 | # define CBFS_MINI_BUILD |
| 41 | #else |
| 42 | # define CBFS_CORE_WITH_LZMA |
| 43 | # include <lib.h> |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 44 | #endif |
| 45 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 46 | #include <cbfs.h> |
| 47 | #include <string.h> |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 48 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 49 | #ifdef LIBPAYLOAD |
| 50 | # include <stdio.h> |
| 51 | # define DEBUG(x...) |
Patrick Georgi | 9a91ba1 | 2013-03-14 15:11:34 +0100 | [diff] [blame] | 52 | # define LOG(x...) |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 53 | # define ERROR(x...) printf(x) |
| 54 | #else |
| 55 | # include <console/console.h> |
| 56 | # define ERROR(x...) printk(BIOS_ERR, "CBFS: " x) |
| 57 | # define LOG(x...) printk(BIOS_INFO, "CBFS: " x) |
Gabe Black | 1ee2c6d | 2013-08-09 04:27:35 -0700 | [diff] [blame] | 58 | # if CONFIG_LP_DEBUG_CBFS |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 59 | # define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x) |
| 60 | # else |
| 61 | # define DEBUG(x...) |
| 62 | # endif |
| 63 | #endif |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 64 | |
Gabe Black | 1ee2c6d | 2013-08-09 04:27:35 -0700 | [diff] [blame] | 65 | #if defined(CONFIG_LP_CBFS_HEADER_ROM_OFFSET) && (CONFIG_LP_CBFS_HEADER_ROM_OFFSET) |
| 66 | # define CBFS_HEADER_ROM_ADDRESS (CONFIG_LP_CBFS_HEADER_ROM_OFFSET) |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 67 | #else |
Patrick Georgi | db2e3aa | 2013-03-09 10:52:50 +0100 | [diff] [blame] | 68 | /* ugly hack: this assumes that "media" exists |
| 69 | in the scope where the macro is used. */ |
| 70 | static uint32_t fetch_x86_header(struct cbfs_media *media) |
| 71 | { |
| 72 | uint32_t *header_ptr = media->map(media, 0xfffffffc, 4); |
| 73 | return *header_ptr; |
| 74 | } |
| 75 | # define CBFS_HEADER_ROM_ADDRESS fetch_x86_header(media) |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 76 | #endif |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 77 | |
| 78 | #include "cbfs_core.c" |
| 79 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 80 | #ifndef __SMM__ |
| 81 | static inline int tohex4(unsigned int c) |
Stefan Reinauer | 09e16dc | 2013-01-14 10:20:15 -0800 | [diff] [blame] | 82 | { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 83 | return (c <= 9) ? (c + '0') : (c - 10 + 'a'); |
Patrick Georgi | 409d17d | 2012-01-17 15:52:05 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 86 | static void tohex16(unsigned int val, char* dest) |
Stefan Reinauer | 09e16dc | 2013-01-14 10:20:15 -0800 | [diff] [blame] | 87 | { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 88 | dest[0] = tohex4(val>>12); |
| 89 | dest[1] = tohex4((val>>8) & 0xf); |
| 90 | dest[2] = tohex4((val>>4) & 0xf); |
| 91 | dest[3] = tohex4(val & 0xf); |
Patrick Georgi | 409d17d | 2012-01-17 15:52:05 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 94 | void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor, |
| 95 | uint16_t device, void *dest) |
Patrick Georgi | 409d17d | 2012-01-17 15:52:05 +0100 | [diff] [blame] | 96 | { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 97 | char name[17] = "pciXXXX,XXXX.rom"; |
| 98 | struct cbfs_optionrom *orom; |
| 99 | uint8_t *src; |
| 100 | |
| 101 | tohex16(vendor, name+3); |
| 102 | tohex16(device, name+8); |
| 103 | |
| 104 | orom = (struct cbfs_optionrom *) |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 105 | cbfs_get_file_content(media, name, CBFS_TYPE_OPTIONROM, NULL); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 106 | |
| 107 | if (orom == NULL) |
| 108 | return NULL; |
| 109 | |
| 110 | /* They might have specified a dest address. If so, we can decompress. |
| 111 | * If not, there's not much hope of decompressing or relocating the rom. |
| 112 | * in the common case, the expansion rom is uncompressed, we |
| 113 | * pass 0 in for the dest, and all we have to do is find the rom and |
| 114 | * return a pointer to it. |
| 115 | */ |
| 116 | |
| 117 | /* BUG: the cbfstool is (not yet) including a cbfs_optionrom header */ |
| 118 | src = (uint8_t*)orom; // + sizeof(struct cbfs_optionrom); |
| 119 | |
| 120 | if (! dest) |
| 121 | return src; |
| 122 | |
Gabe Black | 0c605a5 | 2013-07-01 04:57:37 -0700 | [diff] [blame] | 123 | if (!cbfs_decompress(ntohl(orom->compression), |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 124 | src, |
| 125 | dest, |
| 126 | ntohl(orom->len))) |
| 127 | return NULL; |
| 128 | |
| 129 | return dest; |
Patrick Georgi | 409d17d | 2012-01-17 15:52:05 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 132 | void * cbfs_load_stage(struct cbfs_media *media, const char *name) |
Patrick Georgi | 409d17d | 2012-01-17 15:52:05 +0100 | [diff] [blame] | 133 | { |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 134 | struct cbfs_stage *stage = (struct cbfs_stage *) |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 135 | cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 136 | /* this is a mess. There is no ntohll. */ |
| 137 | /* for now, assume compatible byte order until we solve this. */ |
| 138 | uint32_t entry; |
Gabe Black | ec3a462 | 2013-07-01 04:34:29 -0700 | [diff] [blame] | 139 | uint32_t final_size; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 140 | |
| 141 | if (stage == NULL) |
| 142 | return (void *) -1; |
| 143 | |
| 144 | LOG("loading stage %s @ 0x%x (%d bytes), entry @ 0x%llx\n", |
| 145 | name, |
| 146 | (uint32_t) stage->load, stage->memlen, |
| 147 | stage->entry); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 148 | |
Gabe Black | ec3a462 | 2013-07-01 04:34:29 -0700 | [diff] [blame] | 149 | final_size = cbfs_decompress(stage->compression, |
| 150 | ((unsigned char *) stage) + |
| 151 | sizeof(struct cbfs_stage), |
| 152 | (void *) (uint32_t) stage->load, |
| 153 | stage->len); |
| 154 | if (!final_size) |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 155 | return (void *) -1; |
| 156 | |
Gabe Black | ec3a462 | 2013-07-01 04:34:29 -0700 | [diff] [blame] | 157 | memset((void *)((uintptr_t)stage->load + final_size), 0, |
| 158 | stage->memlen - final_size); |
| 159 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 160 | DEBUG("stage loaded.\n"); |
| 161 | |
| 162 | entry = stage->entry; |
| 163 | // entry = ntohll(stage->entry); |
| 164 | |
| 165 | return (void *) entry; |
Patrick Georgi | 409d17d | 2012-01-17 15:52:05 +0100 | [diff] [blame] | 166 | } |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 167 | |
| 168 | int cbfs_execute_stage(struct cbfs_media *media, const char *name) |
| 169 | { |
| 170 | struct cbfs_stage *stage = (struct cbfs_stage *) |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 171 | cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 172 | |
| 173 | if (stage == NULL) |
| 174 | return 1; |
| 175 | |
| 176 | if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) { |
| 177 | LOG("Unable to run %s: Compressed file" |
| 178 | "Not supported for in-place execution\n", name); |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | /* FIXME: This isn't right */ |
| 183 | LOG("run @ %p\n", (void *) ntohl((uint32_t) stage->entry)); |
| 184 | return run_address((void *)(uintptr_t)ntohll(stage->entry)); |
| 185 | } |
| 186 | |
| 187 | void *cbfs_load_payload(struct cbfs_media *media, const char *name) |
| 188 | { |
| 189 | return (struct cbfs_payload *)cbfs_get_file_content( |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 190 | media, name, CBFS_TYPE_PAYLOAD, NULL); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | struct cbfs_file *cbfs_find(const char *name) { |
| 194 | return cbfs_get_file(CBFS_DEFAULT_MEDIA, name); |
| 195 | } |
| 196 | |
| 197 | void *cbfs_find_file(const char *name, int type) { |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 198 | return cbfs_get_file_content(CBFS_DEFAULT_MEDIA, name, type, NULL); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | const struct cbfs_header *get_cbfs_header(void) { |
| 202 | return cbfs_get_header(CBFS_DEFAULT_MEDIA); |
| 203 | } |
| 204 | |
| 205 | /* Simple buffer */ |
| 206 | |
| 207 | void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer, |
| 208 | struct cbfs_media *media, |
| 209 | size_t offset, size_t count) { |
| 210 | void *address = buffer->buffer + buffer->allocated;; |
| 211 | DEBUG("simple_buffer_map(offset=%d, count=%d): " |
| 212 | "allocated=%d, size=%d, last_allocate=%d\n", |
| 213 | offset, count, buffer->allocated, buffer->size, |
| 214 | buffer->last_allocate); |
| 215 | if (buffer->allocated + count >= buffer->size) |
| 216 | return CBFS_MEDIA_INVALID_MAP_ADDRESS; |
| 217 | if (media->read(media, address, offset, count) != count) { |
| 218 | ERROR("simple_buffer: fail to read %zd bytes from 0x%zx\n", |
| 219 | count, offset); |
| 220 | return CBFS_MEDIA_INVALID_MAP_ADDRESS; |
| 221 | } |
| 222 | buffer->allocated += count; |
| 223 | buffer->last_allocate = count; |
| 224 | return address; |
| 225 | } |
| 226 | |
| 227 | void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer, |
| 228 | const void *address) { |
| 229 | // TODO Add simple buffer management so we can free more than last |
| 230 | // allocated one. |
| 231 | DEBUG("simple_buffer_unmap(address=0x%p): " |
| 232 | "allocated=%d, size=%d, last_allocate=%d\n", |
| 233 | address, buffer->allocated, buffer->size, |
| 234 | buffer->last_allocate); |
| 235 | if ((buffer->buffer + buffer->allocated - buffer->last_allocate) == |
| 236 | address) { |
| 237 | buffer->allocated -= buffer->last_allocate; |
| 238 | buffer->last_allocate = 0; |
| 239 | } |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * run_address is passed the address of a function taking no parameters and |
| 245 | * jumps to it, returning the result. |
| 246 | * @param f the address to call as a function. |
| 247 | * @return value returned by the function. |
| 248 | */ |
| 249 | |
| 250 | int run_address(void *f) |
| 251 | { |
| 252 | int (*v) (void); |
| 253 | v = f; |
| 254 | return v(); |
| 255 | } |
| 256 | |
| 257 | #endif |