blob: 4c59f4131a73175b623bdd894c15aa97263c93af [file] [log] [blame]
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +02001/*
Hung-Te Lind01d0362013-01-25 12:42:40 +08002 * This file is part of the libpayload project.
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +02003 *
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
David Hendricks90ca3b62012-11-16 14:48:22 -08005 * Copyright (C) 2012 Google, Inc.
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +02006 *
7 * This file is dual-licensed. You can choose between:
8 * - The GNU GPL, version 2, as published by the Free Software Foundation
9 * - The revised BSD license (without advertising clause)
10 *
11 * ---------------------------------------------------------------------------
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2 of the License.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020020 * ---------------------------------------------------------------------------
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. The name of the author may not be used to endorse or promote products
30 * derived from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 * ---------------------------------------------------------------------------
44 */
45
46#ifndef _CBFS_CORE_H_
47#define _CBFS_CORE_H_
48
Hung-Te Lind01d0362013-01-25 12:42:40 +080049#include <endian.h>
50#include <stddef.h>
51#include <stdint.h>
Daisuke Nojirid66f1da2015-07-09 15:07:45 -070052#include <stdlib.h>
Hung-Te Lind01d0362013-01-25 12:42:40 +080053
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020054/** These are standard values for the known compression
55 alogrithms that coreboot knows about for stages and
56 payloads. Of course, other CBFS users can use whatever
57 values they want, as long as they understand them. */
58
59#define CBFS_COMPRESS_NONE 0
60#define CBFS_COMPRESS_LZMA 1
61
62/** These are standard component types for well known
63 components (i.e - those that coreboot needs to consume.
64 Users are welcome to use any other value for their
65 components */
66
67#define CBFS_TYPE_STAGE 0x10
68#define CBFS_TYPE_PAYLOAD 0x20
69#define CBFS_TYPE_OPTIONROM 0x30
70#define CBFS_TYPE_BOOTSPLASH 0x40
71#define CBFS_TYPE_RAW 0x50
72#define CBFS_TYPE_VSA 0x51
73#define CBFS_TYPE_MBI 0x52
74#define CBFS_TYPE_MICROCODE 0x53
75#define CBFS_COMPONENT_CMOS_DEFAULT 0xaa
76#define CBFS_COMPONENT_CMOS_LAYOUT 0x01aa
77
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020078#define CBFS_HEADER_MAGIC 0x4F524243
Hung-Te Lin086842a2013-01-04 12:33:03 +080079#define CBFS_HEADER_VERSION1 0x31313131
80#define CBFS_HEADER_VERSION2 0x31313132
81#define CBFS_HEADER_VERSION CBFS_HEADER_VERSION2
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020082
Hung-Te Lind01d0362013-01-25 12:42:40 +080083#define CBFS_HEADER_INVALID_ADDRESS ((void*)(0xffffffff))
84
Julius Wernerefcee762014-11-10 13:14:24 -080085/* this is the master cbfs header - it must be located somewhere available
86 * to bootblock (to load romstage). The last 4 bytes in the image contain its
87 * relative offset from the end of the image (as a 32-bit signed integer). */
Hung-Te Lind01d0362013-01-25 12:42:40 +080088
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020089struct cbfs_header {
90 uint32_t magic;
91 uint32_t version;
92 uint32_t romsize;
93 uint32_t bootblocksize;
Patrick Georgi2272b802015-07-14 22:29:04 +020094 uint32_t align; /* fixed to 64 bytes */
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020095 uint32_t offset;
David Hendricks90ca3b62012-11-16 14:48:22 -080096 uint32_t architecture;
97 uint32_t pad[1];
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +020098} __attribute__((packed));
99
Patrick Georgi2272b802015-07-14 22:29:04 +0200100/* this used to be flexible, but wasn't ever set to something different. */
101#define CBFS_ALIGNMENT 64
102
David Hendricks90ca3b62012-11-16 14:48:22 -0800103/* "Unknown" refers to CBFS headers version 1,
104 * before the architecture was defined (i.e., x86 only).
105 */
106#define CBFS_ARCHITECTURE_UNKNOWN 0xFFFFFFFF
107#define CBFS_ARCHITECTURE_X86 0x00000001
Gabe Black51edd542013-09-30 23:00:33 -0700108#define CBFS_ARCHITECTURE_ARM 0x00000010
Furquan Shaikh8c8c3772014-02-19 11:35:30 -0800109#define CBFS_ARCHITECTURE_ARM64 0x00000011
David Hendricks90ca3b62012-11-16 14:48:22 -0800110
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200111/** This is a component header - every entry in the CBFS
112 will have this header.
113
114 This is how the component is arranged in the ROM:
115
116 -------------- <- 0
117 component header
118 -------------- <- sizeof(struct component)
119 component name
120 -------------- <- offset
121 data
122 ...
123 -------------- <- offset + len
124*/
125
126#define CBFS_FILE_MAGIC "LARCHIVE"
127
128struct cbfs_file {
129 char magic[8];
130 uint32_t len;
131 uint32_t type;
Patrick Georgi377d1db2015-09-16 18:53:40 +0200132 uint32_t attributes_offset;
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200133 uint32_t offset;
Patrick Georgi377d1db2015-09-16 18:53:40 +0200134 char filename[];
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200135} __attribute__((packed));
136
Patrick Georgi377d1db2015-09-16 18:53:40 +0200137/* Depending on how the header was initialized, it may be backed with 0x00 or
138 * 0xff. Support both. */
139#define CBFS_FILE_ATTR_TAG_UNUSED 0
140#define CBFS_FILE_ATTR_TAG_UNUSED2 0xffffffff
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700141#define CBFS_FILE_ATTR_TAG_COMPRESSION 0x42435a4c
Patrick Georgi78517f32015-10-08 13:02:19 +0200142#define CBFS_FILE_ATTR_TAG_HASH 0x68736148
Patrick Georgi377d1db2015-09-16 18:53:40 +0200143
144/* The common fields of extended cbfs file attributes.
145 Attributes are expected to start with tag/len, then append their
146 specific fields. */
147struct cbfs_file_attribute {
148 uint32_t tag;
149 /* len covers the whole structure, incl. tag and len */
150 uint32_t len;
151 uint8_t data[0];
152} __attribute__((packed));
153
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700154struct cbfs_file_attr_compression {
155 uint32_t tag;
156 uint32_t len;
157 /* whole file compression format. 0 if no compression. */
158 uint32_t compression;
159 uint32_t decompressed_size;
160} __attribute__((packed));
161
Patrick Georgi78517f32015-10-08 13:02:19 +0200162struct cbfs_file_attr_hash {
163 uint32_t tag;
164 uint32_t len;
165 uint32_t hash_type;
166 /* hash_data is len - sizeof(struct) bytes */
167 uint8_t hash_data[];
168} __PACKED;
169
Patrick Georgi377d1db2015-09-16 18:53:40 +0200170/* Given a cbfs_file, return the first file attribute, or NULL. */
171struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file);
172
173/* Given a cbfs_file and a cbfs_file_attribute, return the attribute that
174 * follows it, or NULL. */
175struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
176 struct cbfs_file_attribute *attr);
177
Patrick Georgieb33b3f2015-09-17 20:45:52 +0200178/* Given a cbfs_file and an attribute tag, return the first instance of the
179 * attribute or NULL if none found. */
180struct cbfs_file_attribute *cbfs_file_find_attr(struct cbfs_file *file,
181 uint32_t tag);
182
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200183/*** Component sub-headers ***/
184
185/* Following are component sub-headers for the "standard"
186 component types */
187
188/** This is the sub-header for stage components. Stages are
189 loaded by coreboot during the normal boot process */
190
191struct cbfs_stage {
192 uint32_t compression; /** Compression type */
193 uint64_t entry; /** entry point */
194 uint64_t load; /** Where to load in memory */
195 uint32_t len; /** length of data to load */
196 uint32_t memlen; /** total length of object in memory */
197} __attribute__((packed));
198
199/** this is the sub-header for payload components. Payloads
200 are loaded by coreboot at the end of the boot process */
201
202struct cbfs_payload_segment {
203 uint32_t type;
204 uint32_t compression;
205 uint32_t offset;
206 uint64_t load_addr;
207 uint32_t len;
208 uint32_t mem_len;
209} __attribute__((packed));
210
211struct cbfs_payload {
212 struct cbfs_payload_segment segments;
213};
214
215#define PAYLOAD_SEGMENT_CODE 0x45444F43
216#define PAYLOAD_SEGMENT_DATA 0x41544144
217#define PAYLOAD_SEGMENT_BSS 0x20535342
218#define PAYLOAD_SEGMENT_PARAMS 0x41524150
219#define PAYLOAD_SEGMENT_ENTRY 0x52544E45
220
221struct cbfs_optionrom {
222 uint32_t compression;
223 uint32_t len;
224} __attribute__((packed));
225
226#define CBFS_NAME(_c) (((char *) (_c)) + sizeof(struct cbfs_file))
227#define CBFS_SUBHEADER(_p) ( (void *) ((((uint8_t *) (_p)) + ntohl((_p)->offset))) )
228
Hung-Te Lind01d0362013-01-25 12:42:40 +0800229#define CBFS_MEDIA_INVALID_MAP_ADDRESS ((void*)(0xffffffff))
230#define CBFS_DEFAULT_MEDIA ((void*)(0x0))
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200231
Hung-Te Lind01d0362013-01-25 12:42:40 +0800232/* Media for CBFS to load files. */
233struct cbfs_media {
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200234
Hung-Te Lind01d0362013-01-25 12:42:40 +0800235 /* implementation dependent context, to hold resource references */
236 void *context;
237
238 /* opens media and returns 0 on success, -1 on failure */
239 int (*open)(struct cbfs_media *media);
240
241 /* returns number of bytes read from media into dest, starting from
242 * offset for count of bytes */
243 size_t (*read)(struct cbfs_media *media, void *dest, size_t offset,
244 size_t count);
245
246 /* returns a pointer to memory with count of bytes from media source
247 * starting from offset, or CBFS_MEDIA_INVALID_MAP_ADDRESS on failure.
248 * Note: mapped data can't be free unless unmap is called, even if you
249 * do close first. */
250 void * (*map)(struct cbfs_media *media, size_t offset, size_t count);
251
252 /* returns NULL and releases the memory by address, which was allocated
253 * by map */
254 void * (*unmap)(struct cbfs_media *media, const void *address);
255
256 /* closes media and returns 0 on success, -1 on failure. */
257 int (*close)(struct cbfs_media *media);
258};
259
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700260/* returns pointer to a file entry inside CBFS or NULL on error */
Hung-Te Lind01d0362013-01-25 12:42:40 +0800261struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name);
262
Daisuke Nojirid66f1da2015-07-09 15:07:45 -0700263/*
264 * Returns pointer to a copy of the file content or NULL on error.
265 * If the file is compressed, data will be decompressed.
266 * The caller owns the returned memory.
267 */
Hung-Te Lind01d0362013-01-25 12:42:40 +0800268void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100269 int type, size_t *sz);
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200270
Gabe Black0c605a52013-07-01 04:57:37 -0700271/* returns decompressed size on success, 0 on failure */
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200272int cbfs_decompress(int algo, void *src, void *dst, int len);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800273
274/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS
275 * on failure */
276const struct cbfs_header *cbfs_get_header(struct cbfs_media *media);
277
Patrick Georgi6de1ee4a2011-07-21 15:43:14 +0200278#endif