blob: 195494c66d8830b2ca9ab9d4ae517866359aa16e [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.
Hung-Te Lineab2c812013-01-29 01:56:17 +080014 */
15
16#ifndef __CBFS_IMAGE_H
17#define __CBFS_IMAGE_H
18#include "common.h"
19#include "cbfs.h"
20
21/* CBFS image processing */
22
23struct cbfs_image {
24 struct buffer buffer;
Sol Boucher67a0a862015-03-18 12:36:27 -070025 /* An image has a header iff it's a legacy CBFS. */
26 bool has_header;
27 /* Only meaningful if has_header is selected. */
Sol Boucher3e060ed2015-05-05 15:40:15 -070028 struct cbfs_header header;
Hung-Te Lineab2c812013-01-29 01:56:17 +080029};
30
Sol Boucherec424862015-05-07 21:00:05 -070031/* Given the string name of a compression algorithm, return the corresponding
32 * enum comp_algo if it's supported, or a number < 0 otherwise. */
33int cbfs_parse_comp_algo(const char *name);
34
Patrick Georgi89f20342015-10-01 15:54:04 +020035/* Given the string name of a hash algorithm, return the corresponding
36 * id if it's supported, or a number < 0 otherwise. */
37int cbfs_parse_hash_algo(const char *name);
38
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -080039/* Given a pointer, serialize the header from host-native byte format
40 * to cbfs format, i.e. big-endian. */
41void cbfs_put_header(void *dest, const struct cbfs_header *header);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060042/* Or deserialize into host-native format */
Sol Boucher0e539312015-03-05 15:38:03 -080043void cbfs_get_header(struct cbfs_header *header, void *src);
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -080044
Sol Boucher67a0a862015-03-18 12:36:27 -070045/* 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. */
52int cbfs_image_create(struct cbfs_image *image, size_t entries_size);
53
Hung-Te Linf56c73f2013-01-29 09:45:12 +080054/* Creates an empty CBFS image by given size, and description to its content
Sol Bouchere3260a02015-03-25 13:40:08 -070055 * (bootblock, align, header location, starting offset of CBFS entries).
Hung-Te Linf56c73f2013-01-29 09:45:12 +080056 * 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 Boucher67a0a862015-03-18 12:36:27 -070058 * Only call this if you want a legacy CBFS with a master header.
59 * Returns 0 on success, otherwise nonzero. */
60int 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 Linf56c73f2013-01-29 09:45:12 +080067
Sol Bouchere3260a02015-03-25 13:40:08 -070068/* 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. */
72int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in,
73 uint32_t offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +080074
Sol Boucher67a0a862015-03-18 12:36:27 -070075/* 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 Bendebury5e273a42014-12-23 19:26:54 -080077int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset,
78 size_t copy_size);
79
Hung-Te Lineab2c812013-01-29 01:56:17 +080080/* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */
81int cbfs_image_delete(struct cbfs_image *image);
82
Hung-Te Lin0f8af712013-01-29 02:29:49 +080083/* Returns a pointer to entry by name, or NULL if name is not found. */
84struct 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. */
88int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
Aaron Durbin17625022015-10-27 13:17:52 -050089 const char *filename, uint32_t arch);
Hung-Te Lin0f8af712013-01-29 02:29:49 +080090
Hung-Te Lin5f3eb262013-01-29 10:24:00 +080091/* 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 Boucher67d59982015-05-07 02:39:22 -070093 * Never pass this function a top-aligned address: convert it to an offset.
Hung-Te Lin5f3eb262013-01-29 10:24:00 +080094 * Returns 0 on success, otherwise non-zero. */
95int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
Patrick Georgif5252f32015-08-25 22:27:57 +020096 uint32_t content_offset, struct cbfs_file *header);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +080097
Hung-Te Linc03d9b02013-01-29 02:38:40 +080098/* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */
99int cbfs_remove_entry(struct cbfs_image *image, const char *name);
100
Patrick Georgi57edf162015-08-12 09:20:11 +0200101/* Create a new cbfs file header structure to work with.
102 Returns newly allocated memory that the caller needs to free after use. */
103struct cbfs_file *cbfs_create_file_header(int type, size_t len,
104 const char *name);
105
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800106/* Initializes a new empty (type = NULL) entry with size and name in CBFS image.
107 * Returns 0 on success, otherwise (ex, not found) non-zero. */
Patrick Georgiedf25d92015-08-12 09:12:06 +0200108int cbfs_create_empty_entry(struct cbfs_file *entry, int type,
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800109 size_t len, const char *name);
110
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800111/* Finds a location to put given content by specified criteria:
112 * "page_size" limits the content to fit on same memory page, and
113 * "align" specifies starting address alignment.
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800114 * Returns a valid offset, or -1 on failure. */
Aaron Durbind7339412015-09-15 12:50:14 -0500115int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size,
116 size_t page_size, size_t align, size_t metadata_size);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800117
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800118/* Callback function used by cbfs_walk.
119 * Returns 0 on success, or non-zero to stop further iteration. */
120typedef int (*cbfs_entry_callback)(struct cbfs_image *image,
121 struct cbfs_file *file,
122 void *arg);
123
124/* Iterates through all entries in CBFS image, and invoke with callback.
125 * Stops if callback returns non-zero values.
126 * Returns number of entries invoked. */
127int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, void *arg);
128
Hung-Te Lineab2c812013-01-29 01:56:17 +0800129/* Primitive CBFS utilities */
130
131/* Returns a pointer to the only valid CBFS header in give buffer, otherwise
132 * NULL (including when multiple headers were found). If there is a X86 ROM
133 * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as
134 * the only header.*/
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800135struct cbfs_header *cbfs_find_header(char *data, size_t size,
136 uint32_t forced_offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800137
138/* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if
139 * the entry has valid content or not), otherwise NULL. */
140struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image);
141
142/* Returns next cbfs_file entry (no matter if its content is valid or not), or
143 * NULL on failure. */
144struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
145 struct cbfs_file *entry);
146
147/* Returns ROM address (offset) of entry.
148 * This is different from entry->offset (pointer to content). */
149uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry);
150
Sol Boucher67a0a862015-03-18 12:36:27 -0700151/* Returns 1 if valid new-format CBFS (without a master header), otherwise 0. */
152int cbfs_is_valid_cbfs(struct cbfs_image *image);
153
154/* Returns 1 if valid legacy CBFS (with a master header), otherwise 0. */
155int cbfs_is_legacy_cbfs(struct cbfs_image *image);
156
Hung-Te Lineab2c812013-01-29 01:56:17 +0800157/* Returns 1 if entry has valid data (by checking magic number), otherwise 0. */
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800158int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800159
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800160/* Print CBFS component information. */
161int cbfs_print_directory(struct cbfs_image *image);
162int cbfs_print_header_info(struct cbfs_image *image);
163int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
164 void *arg);
165
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800166/* Merge empty entries starting from given entry.
167 * Returns 0 on success, otherwise non-zero. */
168int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
169 void *arg);
170
Patrick Georgi11ee08f2015-08-11 15:10:02 +0200171/* Returns the size of a cbfs file header with no extensions */
172size_t cbfs_calculate_file_header_size(const char *name);
Patrick Georgi2c615062015-07-15 20:49:00 +0200173
174/* Given a cbfs_file, return the first file attribute, or NULL. */
175struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file);
176
177/* Given a cbfs_file and a cbfs_file_attribute, return the attribute that
178 * follows it, or NULL. */
179struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file,
180 struct cbfs_file_attribute *attr);
181
182/* Adds to header a new extended attribute tagged 'tag', sized 'size'.
183 * Returns pointer to the new attribute, or NULL on error. */
184struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header,
185 uint32_t tag,
186 uint32_t size);
Patrick Georgi89f20342015-10-01 15:54:04 +0200187
188/* Adds an extended attribute to header, containing a hash of buffer's data of
189 * the type specified by hash_type.
190 * Returns 0 on success, -1 on error. */
191int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer,
192 enum vb2_hash_algorithm hash_type);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800193#endif