Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 1 | /* |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 2 | * This file is part of the libpayload project. |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net> |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 5 | * Copyright (C) 2012 Google, Inc. |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 6 | * |
| 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 Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 20 | * --------------------------------------------------------------------------- |
| 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 Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 49 | #include <endian.h> |
| 50 | #include <stddef.h> |
| 51 | #include <stdint.h> |
Daisuke Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 52 | #include <stdlib.h> |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 53 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 54 | /** 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 Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 78 | #define CBFS_HEADER_MAGIC 0x4F524243 |
Hung-Te Lin | 086842a | 2013-01-04 12:33:03 +0800 | [diff] [blame] | 79 | #define CBFS_HEADER_VERSION1 0x31313131 |
| 80 | #define CBFS_HEADER_VERSION2 0x31313132 |
| 81 | #define CBFS_HEADER_VERSION CBFS_HEADER_VERSION2 |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 82 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 83 | #define CBFS_HEADER_INVALID_ADDRESS ((void*)(0xffffffff)) |
| 84 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 85 | /* 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 Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 88 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 89 | struct cbfs_header { |
| 90 | uint32_t magic; |
| 91 | uint32_t version; |
| 92 | uint32_t romsize; |
| 93 | uint32_t bootblocksize; |
Patrick Georgi | 2272b80 | 2015-07-14 22:29:04 +0200 | [diff] [blame] | 94 | uint32_t align; /* fixed to 64 bytes */ |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 95 | uint32_t offset; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 96 | uint32_t architecture; |
| 97 | uint32_t pad[1]; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 98 | } __attribute__((packed)); |
| 99 | |
Patrick Georgi | 2272b80 | 2015-07-14 22:29:04 +0200 | [diff] [blame] | 100 | /* this used to be flexible, but wasn't ever set to something different. */ |
| 101 | #define CBFS_ALIGNMENT 64 |
| 102 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 103 | /* "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 Black | 51edd54 | 2013-09-30 23:00:33 -0700 | [diff] [blame] | 108 | #define CBFS_ARCHITECTURE_ARM 0x00000010 |
Furquan Shaikh | 8c8c377 | 2014-02-19 11:35:30 -0800 | [diff] [blame] | 109 | #define CBFS_ARCHITECTURE_ARM64 0x00000011 |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 110 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 111 | /** 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 | |
| 128 | struct cbfs_file { |
| 129 | char magic[8]; |
| 130 | uint32_t len; |
| 131 | uint32_t type; |
Patrick Georgi | 377d1db | 2015-09-16 18:53:40 +0200 | [diff] [blame] | 132 | uint32_t attributes_offset; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 133 | uint32_t offset; |
Patrick Georgi | 377d1db | 2015-09-16 18:53:40 +0200 | [diff] [blame] | 134 | char filename[]; |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 135 | } __attribute__((packed)); |
| 136 | |
Patrick Georgi | 377d1db | 2015-09-16 18:53:40 +0200 | [diff] [blame] | 137 | /* 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 Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 141 | #define CBFS_FILE_ATTR_TAG_COMPRESSION 0x42435a4c |
Patrick Georgi | 78517f3 | 2015-10-08 13:02:19 +0200 | [diff] [blame] | 142 | #define CBFS_FILE_ATTR_TAG_HASH 0x68736148 |
Patrick Georgi | 377d1db | 2015-09-16 18:53:40 +0200 | [diff] [blame] | 143 | |
| 144 | /* The common fields of extended cbfs file attributes. |
| 145 | Attributes are expected to start with tag/len, then append their |
| 146 | specific fields. */ |
| 147 | struct 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 Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 154 | struct 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 Georgi | 78517f3 | 2015-10-08 13:02:19 +0200 | [diff] [blame] | 162 | struct 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 Georgi | 377d1db | 2015-09-16 18:53:40 +0200 | [diff] [blame] | 170 | /* Given a cbfs_file, return the first file attribute, or NULL. */ |
| 171 | struct 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. */ |
| 175 | struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file, |
| 176 | struct cbfs_file_attribute *attr); |
| 177 | |
Patrick Georgi | eb33b3f | 2015-09-17 20:45:52 +0200 | [diff] [blame] | 178 | /* Given a cbfs_file and an attribute tag, return the first instance of the |
| 179 | * attribute or NULL if none found. */ |
| 180 | struct cbfs_file_attribute *cbfs_file_find_attr(struct cbfs_file *file, |
| 181 | uint32_t tag); |
| 182 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 183 | /*** 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 | |
| 191 | struct 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 | |
| 202 | struct 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 | |
| 211 | struct 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 | |
| 221 | struct 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 Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 229 | #define CBFS_MEDIA_INVALID_MAP_ADDRESS ((void*)(0xffffffff)) |
| 230 | #define CBFS_DEFAULT_MEDIA ((void*)(0x0)) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 231 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 232 | /* Media for CBFS to load files. */ |
| 233 | struct cbfs_media { |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 234 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 235 | /* 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 Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 260 | /* returns pointer to a file entry inside CBFS or NULL on error */ |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 261 | struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name); |
| 262 | |
Daisuke Nojiri | d66f1da | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 263 | /* |
| 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 Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 268 | void *cbfs_get_file_content(struct cbfs_media *media, const char *name, |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 269 | int type, size_t *sz); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 270 | |
Gabe Black | 0c605a5 | 2013-07-01 04:57:37 -0700 | [diff] [blame] | 271 | /* returns decompressed size on success, 0 on failure */ |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 272 | int cbfs_decompress(int algo, void *src, void *dst, int len); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 273 | |
| 274 | /* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS |
| 275 | * on failure */ |
| 276 | const struct cbfs_header *cbfs_get_header(struct cbfs_media *media); |
| 277 | |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 278 | #endif |