blob: 5ff8a9abef433e3ad7b87c3b955fcce0de212e95 [file] [log] [blame]
Hung-Te Lineab2c812013-01-29 01:56:17 +08001/*
2 * CBFS Image Manipulation
3 *
4 * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20#ifndef __CBFS_IMAGE_H
21#define __CBFS_IMAGE_H
22#include "common.h"
23#include "cbfs.h"
24
25/* CBFS image processing */
26
27struct cbfs_image {
28 struct buffer buffer;
Sol Boucher67a0a862015-03-18 12:36:27 -070029 /* An image has a header iff it's a legacy CBFS. */
30 bool has_header;
31 /* Only meaningful if has_header is selected. */
Sol Boucher3e060ed2015-05-05 15:40:15 -070032 struct cbfs_header header;
Hung-Te Lineab2c812013-01-29 01:56:17 +080033};
34
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -080035/* Given a pointer, serialize the header from host-native byte format
36 * to cbfs format, i.e. big-endian. */
37void cbfs_put_header(void *dest, const struct cbfs_header *header);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060038/* Or deserialize into host-native format */
Sol Boucher0e539312015-03-05 15:38:03 -080039void cbfs_get_header(struct cbfs_header *header, void *src);
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -080040
Sol Boucher67a0a862015-03-18 12:36:27 -070041/* Populates a CBFS with a single empty entry filling all available space
42 * (entries_size bytes). If image's header field is already present, its
43 * contents will be used to place an empty entry of the requested length at the
44 * appropriate position in the existing buffer; otherwise, if not has_header,
45 * the first entries_size bytes of buffer will be filled exclusively with the
46 * single empty entry (and no CBFS master header).
47 * Returns 0 on success, otherwise nonzero. */
48int cbfs_image_create(struct cbfs_image *image, size_t entries_size);
49
Hung-Te Linf56c73f2013-01-29 09:45:12 +080050/* Creates an empty CBFS image by given size, and description to its content
Sol Bouchere3260a02015-03-25 13:40:08 -070051 * (bootblock, align, header location, starting offset of CBFS entries).
Hung-Te Linf56c73f2013-01-29 09:45:12 +080052 * The output image will contain a valid cbfs_header, with one cbfs_file
53 * entry with type CBFS_COMPONENT_NULL, with max available size.
Sol Boucher67a0a862015-03-18 12:36:27 -070054 * Only call this if you want a legacy CBFS with a master header.
55 * Returns 0 on success, otherwise nonzero. */
56int cbfs_legacy_image_create(struct cbfs_image *image,
57 uint32_t arch,
58 uint32_t align,
59 struct buffer *bootblock,
60 uint32_t bootblock_offset,
61 uint32_t header_offset,
62 uint32_t entries_offset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +080063
Sol Bouchere3260a02015-03-25 13:40:08 -070064/* Constructs a cbfs_image from a buffer. The resulting image contains a shallow
65 * copy of the buffer; releasing either one is the legal way to clean up after
66 * both of them at once. Always produces a cbfs_image, but...
67 * Returns 0 if it contains a valid CBFS, non-zero if it's unrecognized data. */
68int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
69 uint32_t offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +080070
Sol Boucher67a0a862015-03-18 12:36:27 -070071/* Create a duplicate CBFS image. Returns 0 on success, otherwise non-zero.
72 * Will not succeed on new-style images without a master header. */
Vadim Bendebury5e273a42014-12-23 19:26:54 -080073int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
74 size_t copy_size);
75
Hung-Te Lineab2c812013-01-29 01:56:17 +080076/* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */
77int cbfs_image_delete(struct cbfs_image *image);
78
Hung-Te Lin0f8af712013-01-29 02:29:49 +080079/* Returns a pointer to entry by name, or NULL if name is not found. */
80struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name);
81
82/* Exports an entry to external file.
83 * Returns 0 on success, otherwise (ex, not found) non-zero. */
84int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
85 const char *filename);
86
Hung-Te Lin5f3eb262013-01-29 10:24:00 +080087/* Adds an entry to CBFS image by given name and type. If content_offset is
88 * non-zero, try to align "content" (CBFS_SUBHEADER(p)) at content_offset.
Sol Boucher67d59982015-05-07 02:39:22 -070089 * Never pass this function a top-aligned address: convert it to an offset.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +080090 * Returns 0 on success, otherwise non-zero. */
91int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
92 const char *name, uint32_t type, uint32_t content_offset);
93
Hung-Te Linc03d9b02013-01-29 02:38:40 +080094/* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */
95int cbfs_remove_entry(struct cbfs_image *image, const char *name);
96
Hung-Te Lin215d1d72013-01-29 03:46:02 +080097/* Initializes a new empty (type = NULL) entry with size and name in CBFS image.
98 * Returns 0 on success, otherwise (ex, not found) non-zero. */
Vadim Bendebury45e59972014-12-23 15:59:57 -080099int cbfs_create_empty_entry(struct cbfs_file *entry,
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800100 size_t len, const char *name);
101
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800102/* Finds a location to put given content by specified criteria:
103 * "page_size" limits the content to fit on same memory page, and
104 * "align" specifies starting address alignment.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800105 * Returns a valid offset, or -1 on failure. */
106int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800107 uint32_t size, uint32_t page_size, uint32_t align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800108
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800109/* Callback function used by cbfs_walk.
110 * Returns 0 on success, or non-zero to stop further iteration. */
111typedef int (*cbfs_entry_callback)(struct cbfs_image *image,
112 struct cbfs_file *file,
113 void *arg);
114
115/* Iterates through all entries in CBFS image, and invoke with callback.
116 * Stops if callback returns non-zero values.
117 * Returns number of entries invoked. */
118int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, void *arg);
119
Hung-Te Lineab2c812013-01-29 01:56:17 +0800120/* Primitive CBFS utilities */
121
122/* Returns a pointer to the only valid CBFS header in give buffer, otherwise
123 * NULL (including when multiple headers were found). If there is a X86 ROM
124 * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as
125 * the only header.*/
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800126struct cbfs_header *cbfs_find_header(char *data, size_t size,
127 uint32_t forced_offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800128
129/* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if
130 * the entry has valid content or not), otherwise NULL. */
131struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image);
132
133/* Returns next cbfs_file entry (no matter if its content is valid or not), or
134 * NULL on failure. */
135struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
136 struct cbfs_file *entry);
137
138/* Returns ROM address (offset) of entry.
139 * This is different from entry->offset (pointer to content). */
140uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry);
141
Sol Boucher67a0a862015-03-18 12:36:27 -0700142/* Returns 1 if valid new-format CBFS (without a master header), otherwise 0. */
143int cbfs_is_valid_cbfs(struct cbfs_image *image);
144
145/* Returns 1 if valid legacy CBFS (with a master header), otherwise 0. */
146int cbfs_is_legacy_cbfs(struct cbfs_image *image);
147
Hung-Te Lineab2c812013-01-29 01:56:17 +0800148/* Returns 1 if entry has valid data (by checking magic number), otherwise 0. */
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800149int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800150
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800151/* Print CBFS component information. */
152int cbfs_print_directory(struct cbfs_image *image);
153int cbfs_print_header_info(struct cbfs_image *image);
154int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
155 void *arg);
156
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800157/* Merge empty entries starting from given entry.
158 * Returns 0 on success, otherwise non-zero. */
159int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
160 void *arg);
161
Hung-Te Lineab2c812013-01-29 01:56:17 +0800162#endif