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