blob: 38b1ff8c71052d7c90ea2cbcfb4319bf440a2dab [file] [log] [blame]
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +02001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2011 secunet Security Networks AG
Hung-Te Lind01d0362013-01-25 12:42:40 +08005 * Copyright (C) 2013 Google, Inc.
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +02006 *
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 Lind01d0362013-01-25 12:42:40 +080030#define LIBPAYLOAD
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020031
Hung-Te Lind01d0362013-01-25 12:42:40 +080032#ifdef LIBPAYLOAD
33# include <libpayload-config.h>
Stefan Reinauer1b4d3942015-06-29 15:47:34 -070034# if IS_ENABLED(CONFIG_LP_LZMA)
Hung-Te Lind01d0362013-01-25 12:42:40 +080035# include <lzma.h>
36# define CBFS_CORE_WITH_LZMA
37# endif
Julius Werner09f29212015-09-29 13:51:35 -070038# if IS_ENABLED(CONFIG_LP_LZ4)
39# include <lz4.h>
40# define CBFS_CORE_WITH_LZ4
41# endif
Hung-Te Lind01d0362013-01-25 12:42:40 +080042# define CBFS_MINI_BUILD
43#elif defined(__SMM__)
44# define CBFS_MINI_BUILD
45#else
46# define CBFS_CORE_WITH_LZMA
Julius Werner09f29212015-09-29 13:51:35 -070047# define CBFS_CORE_WITH_LZ4
Hung-Te Lind01d0362013-01-25 12:42:40 +080048# include <lib.h>
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020049#endif
50
Hung-Te Lind01d0362013-01-25 12:42:40 +080051#include <cbfs.h>
52#include <string.h>
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020053
Hung-Te Lind01d0362013-01-25 12:42:40 +080054#ifdef LIBPAYLOAD
55# include <stdio.h>
56# define DEBUG(x...)
Patrick Georgi9a91ba12013-03-14 15:11:34 +010057# define LOG(x...)
Hung-Te Lind01d0362013-01-25 12:42:40 +080058# 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 Black1ee2c6d2013-08-09 04:27:35 -070063# if CONFIG_LP_DEBUG_CBFS
Hung-Te Lind01d0362013-01-25 12:42:40 +080064# define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
65# else
66# define DEBUG(x...)
67# endif
68#endif
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020069
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020070#include "cbfs_core.c"
71
Hung-Te Lind01d0362013-01-25 12:42:40 +080072#ifndef __SMM__
73static inline int tohex4(unsigned int c)
Stefan Reinauer09e16dc2013-01-14 10:20:15 -080074{
Hung-Te Lind01d0362013-01-25 12:42:40 +080075 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
Patrick Georgi409d17d2012-01-17 15:52:05 +010076}
77
Hung-Te Lind01d0362013-01-25 12:42:40 +080078static void tohex16(unsigned int val, char* dest)
Stefan Reinauer09e16dc2013-01-14 10:20:15 -080079{
Hung-Te Lind01d0362013-01-25 12:42:40 +080080 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 Georgi409d17d2012-01-17 15:52:05 +010084}
85
Hung-Te Lind01d0362013-01-25 12:42:40 +080086void *cbfs_load_optionrom(struct cbfs_media *media, uint16_t vendor,
Daisuke Nojirid66f1da2015-07-09 15:07:45 -070087 uint16_t device)
Patrick Georgi409d17d2012-01-17 15:52:05 +010088{
Hung-Te Lind01d0362013-01-25 12:42:40 +080089 char name[17] = "pciXXXX,XXXX.rom";
Hung-Te Lind01d0362013-01-25 12:42:40 +080090
91 tohex16(vendor, name+3);
92 tohex16(device, name+8);
93
Daisuke Nojirid66f1da2015-07-09 15:07:45 -070094 return cbfs_get_file_content(media, name, CBFS_TYPE_OPTIONROM, NULL);
Patrick Georgi409d17d2012-01-17 15:52:05 +010095}
96
Hung-Te Lind01d0362013-01-25 12:42:40 +080097void * cbfs_load_stage(struct cbfs_media *media, const char *name)
Patrick Georgi409d17d2012-01-17 15:52:05 +010098{
Hung-Te Lind01d0362013-01-25 12:42:40 +080099 struct cbfs_stage *stage = (struct cbfs_stage *)
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100100 cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800101 /* this is a mess. There is no ntohll. */
102 /* for now, assume compatible byte order until we solve this. */
Furquan Shaikh79a591f2014-05-13 13:47:32 -0700103 uintptr_t entry;
Gabe Blackec3a4622013-07-01 04:34:29 -0700104 uint32_t final_size;
Hung-Te Lind01d0362013-01-25 12:42:40 +0800105
106 if (stage == NULL)
107 return (void *) -1;
108
Furquan Shaikh79a591f2014-05-13 13:47:32 -0700109 LOG("loading stage %s @ 0x%p (%d bytes), entry @ 0x%llx\n",
Hung-Te Lind01d0362013-01-25 12:42:40 +0800110 name,
Furquan Shaikh79a591f2014-05-13 13:47:32 -0700111 (void*)(uintptr_t) stage->load, stage->memlen,
Hung-Te Lind01d0362013-01-25 12:42:40 +0800112 stage->entry);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800113
Gabe Blackec3a4622013-07-01 04:34:29 -0700114 final_size = cbfs_decompress(stage->compression,
115 ((unsigned char *) stage) +
116 sizeof(struct cbfs_stage),
Furquan Shaikh79a591f2014-05-13 13:47:32 -0700117 (void *) (uintptr_t) stage->load,
Gabe Blackec3a4622013-07-01 04:34:29 -0700118 stage->len);
119 if (!final_size)
Hung-Te Lind01d0362013-01-25 12:42:40 +0800120 return (void *) -1;
121
Gabe Blackec3a4622013-07-01 04:34:29 -0700122 memset((void *)((uintptr_t)stage->load + final_size), 0,
123 stage->memlen - final_size);
124
Hung-Te Lind01d0362013-01-25 12:42:40 +0800125 DEBUG("stage loaded.\n");
126
127 entry = stage->entry;
128 // entry = ntohll(stage->entry);
129
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700130 free(stage);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800131 return (void *) entry;
Patrick Georgi409d17d2012-01-17 15:52:05 +0100132}
Hung-Te Lind01d0362013-01-25 12:42:40 +0800133
134int cbfs_execute_stage(struct cbfs_media *media, const char *name)
135{
136 struct cbfs_stage *stage = (struct cbfs_stage *)
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100137 cbfs_get_file_content(media, name, CBFS_TYPE_STAGE, NULL);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800138
139 if (stage == NULL)
140 return 1;
141
142 if (ntohl(stage->compression) != CBFS_COMPRESS_NONE) {
143 LOG("Unable to run %s: Compressed file"
144 "Not supported for in-place execution\n", name);
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700145 free(stage);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800146 return 1;
147 }
148
Furquan Shaikh79a591f2014-05-13 13:47:32 -0700149 LOG("run @ %p\n", (void *) (uintptr_t)ntohll(stage->entry));
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700150 int result = run_address((void *)(uintptr_t)ntohll(stage->entry));
151 free(stage);
152 return result;
Hung-Te Lind01d0362013-01-25 12:42:40 +0800153}
154
155void *cbfs_load_payload(struct cbfs_media *media, const char *name)
156{
157 return (struct cbfs_payload *)cbfs_get_file_content(
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100158 media, name, CBFS_TYPE_PAYLOAD, NULL);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800159}
160
161struct cbfs_file *cbfs_find(const char *name) {
Julius Werner22964792016-05-12 14:40:57 -0700162 struct cbfs_handle *handle = cbfs_get_handle(CBFS_DEFAULT_MEDIA, name);
163 struct cbfs_media *m = &handle->media;
164 void *ret;
165
166 if (!handle)
167 return NULL;
168
169 ret = m->map(m, handle->media_offset,
170 handle->content_offset + handle->content_size);
171 if (ret == CBFS_MEDIA_INVALID_MAP_ADDRESS) {
172 free(handle);
173 return NULL;
174 }
175
176 free(handle);
177 return ret;
Hung-Te Lind01d0362013-01-25 12:42:40 +0800178}
179
180void *cbfs_find_file(const char *name, int type) {
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100181 return cbfs_get_file_content(CBFS_DEFAULT_MEDIA, name, type, NULL);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800182}
183
184const struct cbfs_header *get_cbfs_header(void) {
185 return cbfs_get_header(CBFS_DEFAULT_MEDIA);
186}
187
188/* Simple buffer */
189
190void *cbfs_simple_buffer_map(struct cbfs_simple_buffer *buffer,
191 struct cbfs_media *media,
192 size_t offset, size_t count) {
193 void *address = buffer->buffer + buffer->allocated;;
Daisuke Nojiri03e81882015-07-28 09:21:08 -0700194 DEBUG("simple_buffer_map(offset=%zu, count=%zu): "
195 "allocated=%zu, size=%zu, last_allocate=%zu\n",
Hung-Te Lind01d0362013-01-25 12:42:40 +0800196 offset, count, buffer->allocated, buffer->size,
197 buffer->last_allocate);
198 if (buffer->allocated + count >= buffer->size)
199 return CBFS_MEDIA_INVALID_MAP_ADDRESS;
200 if (media->read(media, address, offset, count) != count) {
201 ERROR("simple_buffer: fail to read %zd bytes from 0x%zx\n",
202 count, offset);
203 return CBFS_MEDIA_INVALID_MAP_ADDRESS;
204 }
205 buffer->allocated += count;
206 buffer->last_allocate = count;
207 return address;
208}
209
210void *cbfs_simple_buffer_unmap(struct cbfs_simple_buffer *buffer,
211 const void *address) {
212 // TODO Add simple buffer management so we can free more than last
213 // allocated one.
214 DEBUG("simple_buffer_unmap(address=0x%p): "
Daisuke Nojiri03e81882015-07-28 09:21:08 -0700215 "allocated=%zu, size=%zu, last_allocate=%zu\n",
Hung-Te Lind01d0362013-01-25 12:42:40 +0800216 address, buffer->allocated, buffer->size,
217 buffer->last_allocate);
218 if ((buffer->buffer + buffer->allocated - buffer->last_allocate) ==
219 address) {
220 buffer->allocated -= buffer->last_allocate;
221 buffer->last_allocate = 0;
222 }
223 return NULL;
224}
225
226/**
227 * run_address is passed the address of a function taking no parameters and
228 * jumps to it, returning the result.
229 * @param f the address to call as a function.
230 * @return value returned by the function.
231 */
232
233int run_address(void *f)
234{
235 int (*v) (void);
236 v = f;
237 return v();
238}
239
240#endif