blob: c71cc26e7857ec46cf262eca11a249fa6f77e520 [file] [log] [blame]
Patrick Georgib8835152011-07-21 15:11:40 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
David Hendricks90ca3b62012-11-16 14:48:22 -08005 * Copyright (C) 2012 Google, Inc.
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +08006 * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
Patrick Georgib8835152011-07-21 15:11:40 +02007 *
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 Lin6fe0cab2013-01-22 18:57:56 +080054#include <endian.h>
55#include <stddef.h>
56#include <stdint.h>
Aaron Durbinf7866522015-03-26 11:05:26 -050057#include <cbfs_serialized.h>
Patrick Georgib8835152011-07-21 15:11:40 +020058
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080059#define CBFS_HEADER_INVALID_ADDRESS ((void*)(0xffffffff))
60
Alexandru Gagniuc299c2652013-12-08 01:13:43 -060061#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 Lin6fe0cab2013-01-22 18:57:56 +080070#define CBFS_MEDIA_INVALID_MAP_ADDRESS ((void*)(0xffffffff))
71#define CBFS_DEFAULT_MEDIA ((void*)(0x0))
Patrick Georgib8835152011-07-21 15:11:40 +020072
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080073/* Media for CBFS to load files. */
74struct cbfs_media {
Patrick Georgib8835152011-07-21 15:11:40 +020075
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +080076 /* 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 Durbinb312b7f2014-06-27 15:06:02 -0500101/*
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 */
105ssize_t cbfs_locate_file(struct cbfs_media *media, struct cbfs_file *file,
106 const char *name);
107
Aaron Durbinb3e02022014-06-27 15:28:39 -0500108/* Read count bytes at offset into dest. Return number of bytes read. */
109size_t cbfs_read(struct cbfs_media *media, void *dest, size_t offset,
110 size_t count);
111
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800112/* returns pointer to a file entry inside CBFS or NULL */
113struct 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 */
116void *cbfs_get_file_content(struct cbfs_media *media, const char *name,
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100117 int type, size_t *sz);
Patrick Georgib8835152011-07-21 15:11:40 +0200118
Gabe Black0c605a52013-07-01 04:57:37 -0700119/* returns decompressed size on success, 0 on failure */
Patrick Georgib8835152011-07-21 15:11:40 +0200120int cbfs_decompress(int algo, void *src, void *dst, int len);
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800121
122/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS
123 * on failure */
124const struct cbfs_header *cbfs_get_header(struct cbfs_media *media);
125
Alexandru Gagniuc299c2652013-12-08 01:13:43 -0600126#endif /* __ROMCC__ */
127
Patrick Georgib8835152011-07-21 15:11:40 +0200128#endif