blob: 2c1be3da1c098e2b640cafb15f77379faf760d2c [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
Hung-Te Linf56c73f2013-01-29 09:45:12 +080025#define IS_TOP_ALIGNED_ADDRESS(x) ((uint32_t)(x) > 0x80000000)
26
Hung-Te Lineab2c812013-01-29 01:56:17 +080027/* CBFS image processing */
28
29struct cbfs_image {
30 struct buffer buffer;
31 struct cbfs_header *header;
32};
33
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -080034/* Given a pointer, serialize the header from host-native byte format
35 * to cbfs format, i.e. big-endian. */
36void cbfs_put_header(void *dest, const struct cbfs_header *header);
Alexandru Gagniucc1d1fd82014-02-05 01:10:08 -060037/* Or deserialize into host-native format */
38void cbfs_get_header(struct cbfs_header *header, const void *src);
Ronald G. Minnichb5adeee2014-01-06 08:38:15 -080039
Hung-Te Linf56c73f2013-01-29 09:45:12 +080040/* Creates an empty CBFS image by given size, and description to its content
41 * (bootblock, align, header location, starting offset of CBFS entries.
42 * The output image will contain a valid cbfs_header, with one cbfs_file
43 * entry with type CBFS_COMPONENT_NULL, with max available size.
44 * Returns 0 on success, otherwise none-zero. */
45int cbfs_image_create(struct cbfs_image *image,
46 uint32_t arch,
47 size_t size,
48 uint32_t align,
49 struct buffer *bootblock,
50 int32_t bootblock_offset,
51 int32_t header_offset,
52 int32_t entries_offset);
53
Hung-Te Lineab2c812013-01-29 01:56:17 +080054/* Loads a CBFS image from file. Returns 0 on success, otherwise non-zero. */
Vadim Bendebury458a12e2014-12-23 15:10:12 -080055int cbfs_image_from_file(struct cbfs_image *image,
56 const char *filename, uint32_t offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +080057
58/* Writes a CBFS image into file. Returns 0 on success, otherwise non-zero. */
59int cbfs_image_write_file(struct cbfs_image *image, const char *filename);
60
61/* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */
62int cbfs_image_delete(struct cbfs_image *image);
63
Hung-Te Lin0f8af712013-01-29 02:29:49 +080064/* Returns a pointer to entry by name, or NULL if name is not found. */
65struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name);
66
67/* Exports an entry to external file.
68 * Returns 0 on success, otherwise (ex, not found) non-zero. */
69int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
70 const char *filename);
71
Hung-Te Lin5f3eb262013-01-29 10:24:00 +080072/* Adds an entry to CBFS image by given name and type. If content_offset is
73 * non-zero, try to align "content" (CBFS_SUBHEADER(p)) at content_offset.
74 * Returns 0 on success, otherwise non-zero. */
75int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer,
76 const char *name, uint32_t type, uint32_t content_offset);
77
Hung-Te Linc03d9b02013-01-29 02:38:40 +080078/* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */
79int cbfs_remove_entry(struct cbfs_image *image, const char *name);
80
Hung-Te Lin215d1d72013-01-29 03:46:02 +080081/* Initializes a new empty (type = NULL) entry with size and name in CBFS image.
82 * Returns 0 on success, otherwise (ex, not found) non-zero. */
83int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
84 size_t len, const char *name);
85
Hung-Te Line4ea2ca2013-03-19 12:24:43 +080086/* Finds a location to put given content by specified criteria:
87 * "page_size" limits the content to fit on same memory page, and
88 * "align" specifies starting address alignment.
Hung-Te Lin215d1d72013-01-29 03:46:02 +080089 * Returns a valid offset, or -1 on failure. */
90int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
Hung-Te Line4ea2ca2013-03-19 12:24:43 +080091 uint32_t size, uint32_t page_size, uint32_t align);
Hung-Te Lin215d1d72013-01-29 03:46:02 +080092
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080093/* Callback function used by cbfs_walk.
94 * Returns 0 on success, or non-zero to stop further iteration. */
95typedef int (*cbfs_entry_callback)(struct cbfs_image *image,
96 struct cbfs_file *file,
97 void *arg);
98
99/* Iterates through all entries in CBFS image, and invoke with callback.
100 * Stops if callback returns non-zero values.
101 * Returns number of entries invoked. */
102int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, void *arg);
103
Hung-Te Lineab2c812013-01-29 01:56:17 +0800104/* Primitive CBFS utilities */
105
106/* Returns a pointer to the only valid CBFS header in give buffer, otherwise
107 * NULL (including when multiple headers were found). If there is a X86 ROM
108 * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as
109 * the only header.*/
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800110struct cbfs_header *cbfs_find_header(char *data, size_t size,
111 uint32_t forced_offset);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800112
113/* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if
114 * the entry has valid content or not), otherwise NULL. */
115struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image);
116
117/* Returns next cbfs_file entry (no matter if its content is valid or not), or
118 * NULL on failure. */
119struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
120 struct cbfs_file *entry);
121
122/* Returns ROM address (offset) of entry.
123 * This is different from entry->offset (pointer to content). */
124uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry);
125
126/* Returns 1 if entry has valid data (by checking magic number), otherwise 0. */
Hung-Te Lin408aefd2013-02-09 10:38:55 +0800127int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry);
Hung-Te Lineab2c812013-01-29 01:56:17 +0800128
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800129/* Print CBFS component information. */
130int cbfs_print_directory(struct cbfs_image *image);
131int cbfs_print_header_info(struct cbfs_image *image);
132int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
133 void *arg);
134
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800135/* Merge empty entries starting from given entry.
136 * Returns 0 on success, otherwise non-zero. */
137int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
138 void *arg);
139
Hung-Te Lineab2c812013-01-29 01:56:17 +0800140#endif