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