Patrick Georgi | b883515 | 2011-07-21 15:11:40 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 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. |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 6 | * Copyright (C) 2013 The Chromium OS Authors. All rights reserved. |
Patrick Georgi | b883515 | 2011-07-21 15:11:40 +0200 | [diff] [blame] | 7 | * |
| 8 | * This file is dual-licensed. You can choose between: |
| 9 | * - The GNU GPL, version 2, as published by the Free Software Foundation |
| 10 | * - The revised BSD license (without advertising clause) |
| 11 | * |
| 12 | * --------------------------------------------------------------------------- |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; version 2 of the License. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 25 | * --------------------------------------------------------------------------- |
| 26 | * Redistribution and use in source and binary forms, with or without |
| 27 | * modification, are permitted provided that the following conditions |
| 28 | * are met: |
| 29 | * 1. Redistributions of source code must retain the above copyright |
| 30 | * notice, this list of conditions and the following disclaimer. |
| 31 | * 2. Redistributions in binary form must reproduce the above copyright |
| 32 | * notice, this list of conditions and the following disclaimer in the |
| 33 | * documentation and/or other materials provided with the distribution. |
| 34 | * 3. The name of the author may not be used to endorse or promote products |
| 35 | * derived from this software without specific prior written permission. |
| 36 | * |
| 37 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 38 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 39 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 40 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 41 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 42 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 43 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 45 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 46 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 47 | * SUCH DAMAGE. |
| 48 | * --------------------------------------------------------------------------- |
| 49 | */ |
| 50 | |
| 51 | #ifndef _CBFS_CORE_H_ |
| 52 | #define _CBFS_CORE_H_ |
| 53 | |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 54 | #include <endian.h> |
| 55 | #include <stddef.h> |
| 56 | #include <stdint.h> |
Aaron Durbin | f786652 | 2015-03-26 11:05:26 -0500 | [diff] [blame] | 57 | #include <cbfs_serialized.h> |
Patrick Georgi | b883515 | 2011-07-21 15:11:40 +0200 | [diff] [blame] | 58 | |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 59 | #define CBFS_HEADER_INVALID_ADDRESS ((void*)(0xffffffff)) |
| 60 | |
Alexandru Gagniuc | 299c265 | 2013-12-08 01:13:43 -0600 | [diff] [blame] | 61 | #define CBFS_NAME(_c) (((char *) (_c)) + sizeof(struct cbfs_file)) |
| 62 | #define CBFS_SUBHEADER(_p) ( (void *) ((((uint8_t *) (_p)) + ntohl((_p)->offset))) ) |
| 63 | |
| 64 | /* |
| 65 | * ROMCC does not understand uint64_t, so we hide future definitions as they are |
| 66 | * unlikely to be ever needed from ROMCC |
| 67 | */ |
| 68 | #ifndef __ROMCC__ |
| 69 | |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 70 | #define CBFS_MEDIA_INVALID_MAP_ADDRESS ((void*)(0xffffffff)) |
| 71 | #define CBFS_DEFAULT_MEDIA ((void*)(0x0)) |
Patrick Georgi | b883515 | 2011-07-21 15:11:40 +0200 | [diff] [blame] | 72 | |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 73 | /* Media for CBFS to load files. */ |
| 74 | struct cbfs_media { |
Patrick Georgi | b883515 | 2011-07-21 15:11:40 +0200 | [diff] [blame] | 75 | |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 76 | /* implementation dependent context, to hold resource references */ |
| 77 | void *context; |
| 78 | |
| 79 | /* opens media and returns 0 on success, -1 on failure */ |
| 80 | int (*open)(struct cbfs_media *media); |
| 81 | |
| 82 | /* returns number of bytes read from media into dest, starting from |
| 83 | * offset for count of bytes */ |
| 84 | size_t (*read)(struct cbfs_media *media, void *dest, size_t offset, |
| 85 | size_t count); |
| 86 | |
| 87 | /* returns a pointer to memory with count of bytes from media source |
| 88 | * starting from offset, or CBFS_MEDIA_INVALID_MAP_ADDRESS on failure. |
| 89 | * Note: mapped data can't be free unless unmap is called, even if you |
| 90 | * do close first. */ |
| 91 | void * (*map)(struct cbfs_media *media, size_t offset, size_t count); |
| 92 | |
| 93 | /* returns NULL and releases the memory by address, which was allocated |
| 94 | * by map */ |
| 95 | void * (*unmap)(struct cbfs_media *media, const void *address); |
| 96 | |
| 97 | /* closes media and returns 0 on success, -1 on failure. */ |
| 98 | int (*close)(struct cbfs_media *media); |
| 99 | }; |
| 100 | |
Aaron Durbin | b312b7f | 2014-06-27 15:06:02 -0500 | [diff] [blame] | 101 | /* |
| 102 | * Locate file by name and fill in cbfs_file in host byte order. Returns |
| 103 | * < 0 on error, else the offset of the file data. |
| 104 | */ |
| 105 | ssize_t cbfs_locate_file(struct cbfs_media *media, struct cbfs_file *file, |
| 106 | const char *name); |
| 107 | |
Aaron Durbin | b3e0202 | 2014-06-27 15:28:39 -0500 | [diff] [blame] | 108 | /* Read count bytes at offset into dest. Return number of bytes read. */ |
| 109 | size_t cbfs_read(struct cbfs_media *media, void *dest, size_t offset, |
| 110 | size_t count); |
| 111 | |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 112 | /* returns pointer to a file entry inside CBFS or NULL */ |
| 113 | struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name); |
| 114 | |
| 115 | /* returns pointer to file content inside CBFS after if type is correct */ |
| 116 | void *cbfs_get_file_content(struct cbfs_media *media, const char *name, |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 117 | int type, size_t *sz); |
Patrick Georgi | b883515 | 2011-07-21 15:11:40 +0200 | [diff] [blame] | 118 | |
Gabe Black | 0c605a5 | 2013-07-01 04:57:37 -0700 | [diff] [blame] | 119 | /* returns decompressed size on success, 0 on failure */ |
Patrick Georgi | b883515 | 2011-07-21 15:11:40 +0200 | [diff] [blame] | 120 | int cbfs_decompress(int algo, void *src, void *dst, int len); |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 121 | |
| 122 | /* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS |
| 123 | * on failure */ |
| 124 | const struct cbfs_header *cbfs_get_header(struct cbfs_media *media); |
| 125 | |
Alexandru Gagniuc | 299c265 | 2013-12-08 01:13:43 -0600 | [diff] [blame] | 126 | #endif /* __ROMCC__ */ |
| 127 | |
Patrick Georgi | b883515 | 2011-07-21 15:11:40 +0200 | [diff] [blame] | 128 | #endif |