blob: f5842d9047eedf73eaeef679673a7aced1e64e63 [file] [log] [blame]
Aaron Durbin295d58b2015-12-15 13:33:51 -06001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbin295d58b2015-12-15 13:33:51 -06004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#ifndef _COMMONLIB_CBFS_H_
15#define _COMMONLIB_CBFS_H_
16
Julius Werner98eeb962019-12-11 15:47:42 -080017#include <commonlib/bsd/cbfs_serialized.h>
Aaron Durbin295d58b2015-12-15 13:33:51 -060018#include <commonlib/region.h>
Aaron Durbincbb6c752015-12-15 15:57:11 -060019#include <vb2_api.h>
Aaron Durbin295d58b2015-12-15 13:33:51 -060020
21/* Object representing cbfs files. */
22struct cbfsf {
23 struct region_device metadata;
24 struct region_device data;
25};
26
Marshall Dawsonbd3c1c72020-02-03 11:44:18 -070027/* Locate file by name and optional type. Returns 0 on success else < 0 on
Aaron Durbin295d58b2015-12-15 13:33:51 -060028 * error.*/
29int cbfs_locate(struct cbfsf *fh, const struct region_device *cbfs,
30 const char *name, uint32_t *type);
31
32static inline void cbfs_file_data(struct region_device *data,
33 const struct cbfsf *file)
34{
Aaron Durbinb1ea53d2019-11-08 09:51:15 -070035 rdev_chain_full(data, &file->data);
Aaron Durbin295d58b2015-12-15 13:33:51 -060036}
37
38static inline void cbfs_file_metadata(struct region_device *metadata,
39 const struct cbfsf *file)
40{
Aaron Durbinb1ea53d2019-11-08 09:51:15 -070041 rdev_chain_full(metadata, &file->metadata);
Aaron Durbin295d58b2015-12-15 13:33:51 -060042}
43
44/*
45 * Provide a handle to each cbfs file within a cbfs. The prev pointer represents
46 * the previous file (NULL on first invocation). The next object gets filled
47 * out with the next file. This returns < 0 on error, 0 on finding the next
48 * file, and > 0 at end of cbfs.
49 */
50int cbfs_for_each_file(const struct region_device *cbfs,
51 const struct cbfsf *prev, struct cbfsf *fh);
52
Aaron Durbincbb6c752015-12-15 15:57:11 -060053/*
Julius Werner71885a42016-08-19 15:24:54 -070054 * Return the offset for each CBFS attribute in a CBFS file metadata region.
55 * The metadata must already be fully mapped by the caller. Will return the
56 * offset (relative to the start of the metadata) or 0 when there are no
57 * further attributes. Should be called with 0 to begin, then always with
58 * the previously returned value until it returns 0.
59 */
60size_t cbfs_for_each_attr(void *metadata, size_t metadata_size,
61 size_t last_offset);
62
63/*
64 * Find out the decompression algorithm and decompressed size of a non-stage
65 * CBFS file (by parsing its metadata attributes), and return them with
66 * out-parameters. Returns 0 on success and < 0 on error.
67 */
68int cbfsf_decompression_info(struct cbfsf *fh, uint32_t *algo, size_t *size);
69
70/*
Patrick Rudolpheeb4e202018-04-26 10:26:13 +020071 * Return the CBFS file type as out-parameter.
72 * Returns 0 on success and < 0 on error.
73 */
74int cbfsf_file_type(struct cbfsf *fh, uint32_t *ftype);
75
76/*
Aaron Durbincbb6c752015-12-15 15:57:11 -060077 * Perform the vb2 hash over the CBFS region skipping empty file contents.
78 * Caller is responsible for providing the hash algorithm as well as storage
79 * for the final digest. Return 0 on success or non-zero on error.
80 */
81int cbfs_vb2_hash_contents(const struct region_device *cbfs,
82 enum vb2_hash_algorithm hash_alg, void *digest,
83 size_t digest_sz);
84
Aaron Durbin295d58b2015-12-15 13:33:51 -060085#endif