rockchip: Remove pulls for gpio_output(), clean up code
[coreboot.git] / util / cbfstool / cbfs_image.h
blob0d7877a7cc9b15380131b88874e0e6e3374ee8d4
1 /*
2 * CBFS Image Manipulation
4 * Copyright (C) 2013 The Chromium OS Authors. All rights reserved.
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.
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.
16 #ifndef __CBFS_IMAGE_H
17 #define __CBFS_IMAGE_H
18 #include "common.h"
19 #include "cbfs.h"
21 /* CBFS image processing */
23 struct cbfs_image {
24 struct buffer buffer;
25 /* An image has a header iff it's a legacy CBFS. */
26 bool has_header;
27 /* Only meaningful if has_header is selected. */
28 struct cbfs_header header;
31 /* Given the string name of a compression algorithm, return the corresponding
32 * enum comp_algo if it's supported, or a number < 0 otherwise. */
33 int cbfs_parse_comp_algo(const char *name);
35 /* Given the string name of a hash algorithm, return the corresponding
36 * id if it's supported, or a number < 0 otherwise. */
37 int cbfs_parse_hash_algo(const char *name);
39 /* Given a pointer, serialize the header from host-native byte format
40 * to cbfs format, i.e. big-endian. */
41 void cbfs_put_header(void *dest, const struct cbfs_header *header);
42 /* Or deserialize into host-native format */
43 void cbfs_get_header(struct cbfs_header *header, void *src);
45 /* Populates a CBFS with a single empty entry filling all available space
46 * (entries_size bytes). If image's header field is already present, its
47 * contents will be used to place an empty entry of the requested length at the
48 * appropriate position in the existing buffer; otherwise, if not has_header,
49 * the first entries_size bytes of buffer will be filled exclusively with the
50 * single empty entry (and no CBFS master header).
51 * Returns 0 on success, otherwise nonzero. */
52 int cbfs_image_create(struct cbfs_image *image, size_t entries_size);
54 /* Creates an empty CBFS image by given size, and description to its content
55 * (bootblock, align, header location, starting offset of CBFS entries).
56 * The output image will contain a valid cbfs_header, with one cbfs_file
57 * entry with type CBFS_COMPONENT_NULL, with max available size.
58 * Only call this if you want a legacy CBFS with a master header.
59 * Returns 0 on success, otherwise nonzero. */
60 int cbfs_legacy_image_create(struct cbfs_image *image,
61 uint32_t arch,
62 uint32_t align,
63 struct buffer *bootblock,
64 uint32_t bootblock_offset,
65 uint32_t header_offset,
66 uint32_t entries_offset);
68 /* Constructs a cbfs_image from a buffer. The resulting image contains a shallow
69 * copy of the buffer; releasing either one is the legal way to clean up after
70 * both of them at once. Always produces a cbfs_image, but...
71 * Returns 0 if it contains a valid CBFS, non-zero if it's unrecognized data. */
72 int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
73 uint32_t offset);
75 /* Create a duplicate CBFS image. Returns 0 on success, otherwise non-zero.
76 * Will not succeed on new-style images without a master header. */
77 int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst);
79 /* Compact a fragmented CBFS image by placing all the non-empty files at the
80 * beginning of the image. Returns 0 on success, otherwise non-zero. */
81 int cbfs_compact_instance(struct cbfs_image *image);
83 /* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */
84 int cbfs_image_delete(struct cbfs_image *image);
86 /* Returns a pointer to entry by name, or NULL if name is not found. */
87 struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name);
89 /* Exports an entry to external file.
90 * Returns 0 on success, otherwise (ex, not found) non-zero. */
91 int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
92 const char *filename, uint32_t arch);
94 /* Adds an entry to CBFS image by given name and type. If content_offset is
95 * non-zero, try to align "content" (CBFS_SUBHEADER(p)) at content_offset.
96 * Never pass this function a top-aligned address: convert it to an offset.
97 * Returns 0 on success, otherwise non-zero. */
98 int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
99 uint32_t content_offset, struct cbfs_file *header);
101 /* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */
102 int cbfs_remove_entry(struct cbfs_image *image, const char *name);
104 /* Create a new cbfs file header structure to work with.
105 Returns newly allocated memory that the caller needs to free after use. */
106 struct cbfs_file *cbfs_create_file_header(int type, size_t len,
107 const char *name);
109 /* Initializes a new empty (type = NULL) entry with size and name in CBFS image.
110 * Returns 0 on success, otherwise (ex, not found) non-zero. */
111 int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
112 size_t len, const char *name);
114 /* Finds a location to put given content by specified criteria:
115 * "page_size" limits the content to fit on same memory page, and
116 * "align" specifies starting address alignment.
117 * Returns a valid offset, or -1 on failure. */
118 int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
119 size_t page_size, size_t align, size_t metadata_size);
121 /* Callback function used by cbfs_walk.
122 * Returns 0 on success, or non-zero to stop further iteration. */
123 typedef int (*cbfs_entry_callback)(struct cbfs_image *image,
124 struct cbfs_file *file,
125 void *arg);
127 /* Iterates through all entries in CBFS image, and invoke with callback.
128 * Stops if callback returns non-zero values.
129 * Returns number of entries invoked. */
130 int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, void *arg);
132 /* Primitive CBFS utilities */
134 /* Returns a pointer to the only valid CBFS header in give buffer, otherwise
135 * NULL (including when multiple headers were found). If there is a X86 ROM
136 * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as
137 * the only header.*/
138 struct cbfs_header *cbfs_find_header(char *data, size_t size,
139 uint32_t forced_offset);
141 /* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if
142 * the entry has valid content or not), otherwise NULL. */
143 struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image);
145 /* Returns next cbfs_file entry (no matter if its content is valid or not), or
146 * NULL on failure. */
147 struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
148 struct cbfs_file *entry);
150 /* Returns ROM address (offset) of entry.
151 * This is different from entry->offset (pointer to content). */
152 uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry);
154 /* Returns 1 if valid new-format CBFS (without a master header), otherwise 0. */
155 int cbfs_is_valid_cbfs(struct cbfs_image *image);
157 /* Returns 1 if valid legacy CBFS (with a master header), otherwise 0. */
158 int cbfs_is_legacy_cbfs(struct cbfs_image *image);
160 /* Returns 1 if entry has valid data (by checking magic number), otherwise 0. */
161 int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry);
163 /* Print CBFS component information. */
164 int cbfs_print_directory(struct cbfs_image *image);
165 int cbfs_print_parseable_directory(struct cbfs_image *image);
166 int cbfs_print_header_info(struct cbfs_image *image);
167 int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
168 void *arg);
170 /* Merge empty entries starting from given entry.
171 * Returns 0 on success, otherwise non-zero. */
172 int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
173 void *arg);
175 /* Returns the size of a cbfs file header with no extensions */
176 size_t cbfs_calculate_file_header_size(const char *name);
178 /* Given a cbfs_file, return the first file attribute, or NULL. */
179 struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file);
181 /* Given a cbfs_file and a cbfs_file_attribute, return the attribute that
182 * follows it, or NULL. */
183 struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
184 struct cbfs_file_attribute *attr);
186 /* Adds to header a new extended attribute tagged 'tag', sized 'size'.
187 * Returns pointer to the new attribute, or NULL on error. */
188 struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
189 uint32_t tag,
190 uint32_t size);
192 /* Adds an extended attribute to header, containing a hash of buffer's data of
193 * the type specified by hash_type.
194 * Returns 0 on success, -1 on error. */
195 int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
196 enum vb2_hash_algorithm hash_type);
197 #endif