Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1 | /* |
| 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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 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 | |
| 27 | struct cbfs_image { |
| 28 | struct buffer buffer; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 29 | /* An image has a header iff it's a legacy CBFS. */ |
| 30 | bool has_header; |
| 31 | /* Only meaningful if has_header is selected. */ |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 32 | struct cbfs_header header; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 33 | }; |
| 34 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 35 | /* Given the string name of a compression algorithm, return the corresponding |
| 36 | * enum comp_algo if it's supported, or a number < 0 otherwise. */ |
| 37 | int cbfs_parse_comp_algo(const char *name); |
| 38 | |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 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); |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 42 | /* Or deserialize into host-native format */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 43 | void cbfs_get_header(struct cbfs_header *header, void *src); |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 44 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 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); |
| 53 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 54 | /* 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] | 55 | * (bootblock, align, header location, starting offset of CBFS entries). |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 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. |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 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); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 67 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 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); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 74 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 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. */ |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 77 | int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset, |
| 78 | size_t copy_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 | |
| 86 | /* Exports an entry to external file. |
| 87 | * Returns 0 on success, otherwise (ex, not found) non-zero. */ |
| 88 | int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, |
| 89 | const char *filename); |
| 90 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 91 | /* Adds an entry to CBFS image by given name and type. If content_offset is |
| 92 | * non-zero, try to align "content" (CBFS_SUBHEADER(p)) at content_offset. |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 93 | * 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] | 94 | * Returns 0 on success, otherwise non-zero. */ |
| 95 | int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, |
Patrick Georgi | e590358 | 2015-08-25 13:53:42 +0200 | [diff] [blame] | 96 | uint32_t content_offset, |
Patrick Georgi | a60e7b6 | 2015-08-25 22:26:02 +0200 | [diff] [blame] | 97 | struct cbfs_file *header, uint32_t header_size); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 98 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 99 | /* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */ |
| 100 | int cbfs_remove_entry(struct cbfs_image *image, const char *name); |
| 101 | |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 102 | /* Create a new cbfs file header structure to work with. |
| 103 | Returns newly allocated memory that the caller needs to free after use. */ |
| 104 | struct cbfs_file *cbfs_create_file_header(int type, size_t len, |
| 105 | const char *name); |
| 106 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 107 | /* Initializes a new empty (type = NULL) entry with size and name in CBFS image. |
| 108 | * Returns 0 on success, otherwise (ex, not found) non-zero. */ |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 109 | int cbfs_create_empty_entry(struct cbfs_file *entry, int type, |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 110 | size_t len, const char *name); |
| 111 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 112 | /* Finds a location to put given content by specified criteria: |
| 113 | * "page_size" limits the content to fit on same memory page, and |
| 114 | * "align" specifies starting address alignment. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 115 | * Returns a valid offset, or -1 on failure. */ |
| 116 | int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name, |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 117 | uint32_t size, uint32_t page_size, uint32_t align); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 118 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 119 | /* Callback function used by cbfs_walk. |
| 120 | * Returns 0 on success, or non-zero to stop further iteration. */ |
| 121 | typedef int (*cbfs_entry_callback)(struct cbfs_image *image, |
| 122 | struct cbfs_file *file, |
| 123 | void *arg); |
| 124 | |
| 125 | /* Iterates through all entries in CBFS image, and invoke with callback. |
| 126 | * Stops if callback returns non-zero values. |
| 127 | * Returns number of entries invoked. */ |
| 128 | int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, void *arg); |
| 129 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 130 | /* Primitive CBFS utilities */ |
| 131 | |
| 132 | /* Returns a pointer to the only valid CBFS header in give buffer, otherwise |
| 133 | * NULL (including when multiple headers were found). If there is a X86 ROM |
| 134 | * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as |
| 135 | * the only header.*/ |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 136 | struct cbfs_header *cbfs_find_header(char *data, size_t size, |
| 137 | uint32_t forced_offset); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 138 | |
| 139 | /* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if |
| 140 | * the entry has valid content or not), otherwise NULL. */ |
| 141 | struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image); |
| 142 | |
| 143 | /* Returns next cbfs_file entry (no matter if its content is valid or not), or |
| 144 | * NULL on failure. */ |
| 145 | struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, |
| 146 | struct cbfs_file *entry); |
| 147 | |
| 148 | /* Returns ROM address (offset) of entry. |
| 149 | * This is different from entry->offset (pointer to content). */ |
| 150 | uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry); |
| 151 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 152 | /* Returns 1 if valid new-format CBFS (without a master header), otherwise 0. */ |
| 153 | int cbfs_is_valid_cbfs(struct cbfs_image *image); |
| 154 | |
| 155 | /* Returns 1 if valid legacy CBFS (with a master header), otherwise 0. */ |
| 156 | int cbfs_is_legacy_cbfs(struct cbfs_image *image); |
| 157 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 158 | /* 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] | 159 | 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] | 160 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 161 | /* Print CBFS component information. */ |
| 162 | int cbfs_print_directory(struct cbfs_image *image); |
| 163 | int cbfs_print_header_info(struct cbfs_image *image); |
| 164 | int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, |
| 165 | void *arg); |
| 166 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 167 | /* Merge empty entries starting from given entry. |
| 168 | * Returns 0 on success, otherwise non-zero. */ |
| 169 | int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, |
| 170 | void *arg); |
| 171 | |
Patrick Georgi | 11ee08f | 2015-08-11 15:10:02 +0200 | [diff] [blame] | 172 | /* Returns the size of a cbfs file header with no extensions */ |
| 173 | size_t cbfs_calculate_file_header_size(const char *name); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 174 | #endif |