Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* CBFS Image Manipulation */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 3 | |
| 4 | #ifndef __CBFS_IMAGE_H |
| 5 | #define __CBFS_IMAGE_H |
| 6 | #include "common.h" |
| 7 | #include "cbfs.h" |
| 8 | |
| 9 | /* CBFS image processing */ |
| 10 | |
| 11 | struct cbfs_image { |
| 12 | struct buffer buffer; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 13 | /* An image has a header iff it's a legacy CBFS. */ |
| 14 | bool has_header; |
| 15 | /* Only meaningful if has_header is selected. */ |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 16 | struct cbfs_header header; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 17 | }; |
| 18 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 19 | /* Given the string name of a compression algorithm, return the corresponding |
| 20 | * enum comp_algo if it's supported, or a number < 0 otherwise. */ |
| 21 | int cbfs_parse_comp_algo(const char *name); |
| 22 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 23 | /* Given the string name of a hash algorithm, return the corresponding |
| 24 | * id if it's supported, or a number < 0 otherwise. */ |
| 25 | int cbfs_parse_hash_algo(const char *name); |
| 26 | |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 27 | /* Given a pointer, serialize the header from host-native byte format |
| 28 | * to cbfs format, i.e. big-endian. */ |
| 29 | void cbfs_put_header(void *dest, const struct cbfs_header *header); |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 30 | /* Or deserialize into host-native format */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 31 | void cbfs_get_header(struct cbfs_header *header, void *src); |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 32 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 33 | /* Populates a CBFS with a single empty entry filling all available space |
| 34 | * (entries_size bytes). If image's header field is already present, its |
| 35 | * contents will be used to place an empty entry of the requested length at the |
| 36 | * appropriate position in the existing buffer; otherwise, if not has_header, |
| 37 | * the first entries_size bytes of buffer will be filled exclusively with the |
| 38 | * single empty entry (and no CBFS master header). |
| 39 | * Returns 0 on success, otherwise nonzero. */ |
| 40 | int cbfs_image_create(struct cbfs_image *image, size_t entries_size); |
| 41 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 42 | /* Creates an empty CBFS image by given size, and description to its content |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 43 | * (bootblock, align, header location, starting offset of CBFS entries). |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 44 | * The output image will contain a valid cbfs_header, with one cbfs_file |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 45 | * entry with type CBFS_TYPE_NULL, with max available size. |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 46 | * Only call this if you want a legacy CBFS with a master header. |
| 47 | * Returns 0 on success, otherwise nonzero. */ |
| 48 | int cbfs_legacy_image_create(struct cbfs_image *image, |
| 49 | uint32_t arch, |
| 50 | uint32_t align, |
| 51 | struct buffer *bootblock, |
| 52 | uint32_t bootblock_offset, |
| 53 | uint32_t header_offset, |
| 54 | uint32_t entries_offset); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 55 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 56 | /* Constructs a cbfs_image from a buffer. The resulting image contains a shallow |
| 57 | * copy of the buffer; releasing either one is the legal way to clean up after |
| 58 | * both of them at once. Always produces a cbfs_image, but... |
| 59 | * Returns 0 if it contains a valid CBFS, non-zero if it's unrecognized data. */ |
| 60 | int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in, |
| 61 | uint32_t offset); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 62 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 63 | /* Create a duplicate CBFS image. Returns 0 on success, otherwise non-zero. |
| 64 | * Will not succeed on new-style images without a master header. */ |
Patrick Georgi | 214e4af | 2015-11-20 19:22:50 +0100 | [diff] [blame] | 65 | int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 66 | |
Aaron Durbin | 71c60ca | 2016-01-26 17:08:56 -0600 | [diff] [blame] | 67 | /* Compact a fragmented CBFS image by placing all the non-empty files at the |
| 68 | * beginning of the image. Returns 0 on success, otherwise non-zero. */ |
| 69 | int cbfs_compact_instance(struct cbfs_image *image); |
| 70 | |
Patrick Georgi | 5d982d7 | 2017-09-19 14:39:58 +0200 | [diff] [blame] | 71 | /* Expand a CBFS image inside an fmap region to the entire region's space. |
| 72 | Returns 0 on success, otherwise non-zero. */ |
| 73 | int cbfs_expand_to_region(struct buffer *region); |
| 74 | |
Patrick Georgi | 12631a4 | 2017-09-20 11:59:18 +0200 | [diff] [blame] | 75 | /* Truncate a CBFS by removing a trailing "empty" file if it exists. |
| 76 | Returns 0 on success, otherwise non-zero and passes the CBFS' remaining |
| 77 | size in the size argument. */ |
| 78 | int cbfs_truncate_space(struct buffer *region, uint32_t *size); |
| 79 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 80 | /* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */ |
| 81 | int cbfs_image_delete(struct cbfs_image *image); |
| 82 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 83 | /* Returns a pointer to entry by name, or NULL if name is not found. */ |
| 84 | struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name); |
| 85 | |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 86 | /* Exports an entry to external file. If do_processing is true, file contents |
| 87 | * will be decompressed, and also turned into an ELF if appropriate. |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 88 | * Returns 0 on success, otherwise (ex, not found) non-zero. */ |
| 89 | int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 90 | const char *filename, uint32_t arch, bool do_processing); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 91 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 92 | /* Adds an entry to CBFS image by given name and type. If content_offset is |
| 93 | * non-zero, try to align "content" (CBFS_SUBHEADER(p)) at content_offset. |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 94 | * Never pass this function a top-aligned address: convert it to an offset. |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 95 | * Returns 0 on success, otherwise non-zero. */ |
| 96 | int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 97 | uint32_t content_offset, struct cbfs_file *header, |
| 98 | const size_t len_align); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 99 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 100 | /* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */ |
| 101 | int cbfs_remove_entry(struct cbfs_image *image, const char *name); |
| 102 | |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 103 | /* Create a new cbfs file header structure to work with. |
| 104 | Returns newly allocated memory that the caller needs to free after use. */ |
| 105 | struct cbfs_file *cbfs_create_file_header(int type, size_t len, |
| 106 | const char *name); |
| 107 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 108 | /* Initializes a new empty (type = NULL) entry with size and name in CBFS image. |
| 109 | * Returns 0 on success, otherwise (ex, not found) non-zero. */ |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 110 | int cbfs_create_empty_entry(struct cbfs_file *entry, int type, |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 111 | size_t len, const char *name); |
| 112 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 113 | /* Finds a location to put given content by specified criteria: |
| 114 | * "page_size" limits the content to fit on same memory page, and |
| 115 | * "align" specifies starting address alignment. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 116 | * Returns a valid offset, or -1 on failure. */ |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 117 | int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size, |
| 118 | size_t page_size, size_t align, size_t metadata_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 119 | |
Julius Werner | 7066a1e | 2020-04-02 15:49:34 -0700 | [diff] [blame] | 120 | /* Callback function used by cbfs_legacy_walk. |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 121 | * Returns 0 on success, or non-zero to stop further iteration. */ |
| 122 | typedef int (*cbfs_entry_callback)(struct cbfs_image *image, |
| 123 | struct cbfs_file *file, |
| 124 | void *arg); |
| 125 | |
| 126 | /* Iterates through all entries in CBFS image, and invoke with callback. |
Julius Werner | 7066a1e | 2020-04-02 15:49:34 -0700 | [diff] [blame] | 127 | * Stops if callback returns non-zero values. Unlike the commonlib cbfs_walk(), |
| 128 | * this can deal with different alignments in legacy CBFS (with master header). |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 129 | * Returns number of entries invoked. */ |
Julius Werner | 7066a1e | 2020-04-02 15:49:34 -0700 | [diff] [blame] | 130 | int cbfs_legacy_walk(struct cbfs_image *image, cbfs_entry_callback callback, |
| 131 | void *arg); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 132 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 133 | /* Primitive CBFS utilities */ |
| 134 | |
| 135 | /* Returns a pointer to the only valid CBFS header in give buffer, otherwise |
| 136 | * NULL (including when multiple headers were found). If there is a X86 ROM |
| 137 | * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as |
| 138 | * the only header.*/ |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 139 | struct cbfs_header *cbfs_find_header(char *data, size_t size, |
| 140 | uint32_t forced_offset); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 141 | |
| 142 | /* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if |
| 143 | * the entry has valid content or not), otherwise NULL. */ |
| 144 | struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image); |
| 145 | |
| 146 | /* Returns next cbfs_file entry (no matter if its content is valid or not), or |
| 147 | * NULL on failure. */ |
| 148 | struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, |
| 149 | struct cbfs_file *entry); |
| 150 | |
| 151 | /* Returns ROM address (offset) of entry. |
| 152 | * This is different from entry->offset (pointer to content). */ |
| 153 | uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry); |
| 154 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 155 | /* Returns 1 if valid new-format CBFS (without a master header), otherwise 0. */ |
| 156 | int cbfs_is_valid_cbfs(struct cbfs_image *image); |
| 157 | |
| 158 | /* Returns 1 if valid legacy CBFS (with a master header), otherwise 0. */ |
| 159 | int cbfs_is_legacy_cbfs(struct cbfs_image *image); |
| 160 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 161 | /* Returns 1 if entry has valid data (by checking magic number), otherwise 0. */ |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 162 | int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 163 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 164 | /* Print CBFS component information. */ |
Julius Werner | c4ee28c | 2020-04-27 19:31:03 -0700 | [diff] [blame] | 165 | void cbfs_print_directory(struct cbfs_image *image); |
| 166 | void cbfs_print_parseable_directory(struct cbfs_image *image); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 167 | int cbfs_print_header_info(struct cbfs_image *image); |
| 168 | int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, |
| 169 | void *arg); |
| 170 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 171 | /* Merge empty entries starting from given entry. |
| 172 | * Returns 0 on success, otherwise non-zero. */ |
| 173 | int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, |
| 174 | void *arg); |
| 175 | |
Patrick Georgi | 11ee08f | 2015-08-11 15:10:02 +0200 | [diff] [blame] | 176 | /* Returns the size of a cbfs file header with no extensions */ |
| 177 | size_t cbfs_calculate_file_header_size(const char *name); |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 178 | |
| 179 | /* Given a cbfs_file, return the first file attribute, or NULL. */ |
| 180 | struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file); |
| 181 | |
| 182 | /* Given a cbfs_file and a cbfs_file_attribute, return the attribute that |
| 183 | * follows it, or NULL. */ |
| 184 | struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file, |
| 185 | struct cbfs_file_attribute *attr); |
| 186 | |
| 187 | /* Adds to header a new extended attribute tagged 'tag', sized 'size'. |
| 188 | * Returns pointer to the new attribute, or NULL on error. */ |
| 189 | struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header, |
| 190 | uint32_t tag, |
| 191 | uint32_t size); |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 192 | |
| 193 | /* Adds an extended attribute to header, containing a hash of buffer's data of |
| 194 | * the type specified by hash_type. |
| 195 | * Returns 0 on success, -1 on error. */ |
| 196 | int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer, |
| 197 | enum vb2_hash_algorithm hash_type); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 198 | #endif |