blob: d99bee851e0cbc152a550eaf1eb494c953224da2 [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
25/* CBFS image processing */
26
27struct cbfs_image {
28 struct buffer buffer;
29 struct cbfs_header *header;
30};
31
32/* Loads a CBFS image from file. Returns 0 on success, otherwise non-zero. */
33int cbfs_image_from_file(struct cbfs_image *image, const char *filename);
34
35/* Writes a CBFS image into file. Returns 0 on success, otherwise non-zero. */
36int cbfs_image_write_file(struct cbfs_image *image, const char *filename);
37
38/* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */
39int cbfs_image_delete(struct cbfs_image *image);
40
Hung-Te Lin0f8af712013-01-29 02:29:49 +080041/* Returns a pointer to entry by name, or NULL if name is not found. */
42struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name);
43
44/* Exports an entry to external file.
45 * Returns 0 on success, otherwise (ex, not found) non-zero. */
46int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
47 const char *filename);
48
Hung-Te Linc03d9b02013-01-29 02:38:40 +080049/* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */
50int cbfs_remove_entry(struct cbfs_image *image, const char *name);
51
Hung-Te Lin215d1d72013-01-29 03:46:02 +080052/* Initializes a new empty (type = NULL) entry with size and name in CBFS image.
53 * Returns 0 on success, otherwise (ex, not found) non-zero. */
54int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
55 size_t len, const char *name);
56
57/* Finds a location to put given content in same memory page.
58 * Returns a valid offset, or -1 on failure. */
59int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
60 uint32_t size, uint32_t page_size);
61
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080062/* Callback function used by cbfs_walk.
63 * Returns 0 on success, or non-zero to stop further iteration. */
64typedef int (*cbfs_entry_callback)(struct cbfs_image *image,
65 struct cbfs_file *file,
66 void *arg);
67
68/* Iterates through all entries in CBFS image, and invoke with callback.
69 * Stops if callback returns non-zero values.
70 * Returns number of entries invoked. */
71int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, void *arg);
72
Hung-Te Lineab2c812013-01-29 01:56:17 +080073/* Primitive CBFS utilities */
74
75/* Returns a pointer to the only valid CBFS header in give buffer, otherwise
76 * NULL (including when multiple headers were found). If there is a X86 ROM
77 * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as
78 * the only header.*/
79struct cbfs_header *cbfs_find_header(char *data, size_t size);
80
81/* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if
82 * the entry has valid content or not), otherwise NULL. */
83struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image);
84
85/* Returns next cbfs_file entry (no matter if its content is valid or not), or
86 * NULL on failure. */
87struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
88 struct cbfs_file *entry);
89
90/* Returns ROM address (offset) of entry.
91 * This is different from entry->offset (pointer to content). */
92uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry);
93
94/* Returns 1 if entry has valid data (by checking magic number), otherwise 0. */
95int cbfs_is_valid_entry(struct cbfs_file *entry);
96
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080097/* Print CBFS component information. */
98int cbfs_print_directory(struct cbfs_image *image);
99int cbfs_print_header_info(struct cbfs_image *image);
100int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
101 void *arg);
102
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800103/* Merge empty entries starting from given entry.
104 * Returns 0 on success, otherwise non-zero. */
105int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
106 void *arg);
107
Hung-Te Lineab2c812013-01-29 01:56:17 +0800108#endif