blob: 5e1c87109d6431b7b7e384eecfa30beb0f88874a [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
Hung-Te Linf56c73f2013-01-29 09:45:12 +080034/* Creates an empty CBFS image by given size, and description to its content
35 * (bootblock, align, header location, starting offset of CBFS entries.
36 * The output image will contain a valid cbfs_header, with one cbfs_file
37 * entry with type CBFS_COMPONENT_NULL, with max available size.
38 * Returns 0 on success, otherwise none-zero. */
39int cbfs_image_create(struct cbfs_image *image,
40 uint32_t arch,
41 size_t size,
42 uint32_t align,
43 struct buffer *bootblock,
44 int32_t bootblock_offset,
45 int32_t header_offset,
46 int32_t entries_offset);
47
Hung-Te Lineab2c812013-01-29 01:56:17 +080048/* Loads a CBFS image from file. Returns 0 on success, otherwise non-zero. */
49int cbfs_image_from_file(struct cbfs_image *image, const char *filename);
50
51/* Writes a CBFS image into file. Returns 0 on success, otherwise non-zero. */
52int cbfs_image_write_file(struct cbfs_image *image, const char *filename);
53
54/* Releases the CBFS image. Returns 0 on success, otherwise non-zero. */
55int cbfs_image_delete(struct cbfs_image *image);
56
Hung-Te Lin0f8af712013-01-29 02:29:49 +080057/* Returns a pointer to entry by name, or NULL if name is not found. */
58struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name);
59
60/* Exports an entry to external file.
61 * Returns 0 on success, otherwise (ex, not found) non-zero. */
62int cbfs_export_entry(struct cbfs_image *image, const char *entry_name,
63 const char *filename);
64
Hung-Te Linc03d9b02013-01-29 02:38:40 +080065/* Removes an entry from CBFS image. Returns 0 on success, otherwise non-zero. */
66int cbfs_remove_entry(struct cbfs_image *image, const char *name);
67
Hung-Te Lin215d1d72013-01-29 03:46:02 +080068/* Initializes a new empty (type = NULL) entry with size and name in CBFS image.
69 * Returns 0 on success, otherwise (ex, not found) non-zero. */
70int cbfs_create_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
71 size_t len, const char *name);
72
73/* Finds a location to put given content in same memory page.
74 * Returns a valid offset, or -1 on failure. */
75int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name,
76 uint32_t size, uint32_t page_size);
77
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080078/* Callback function used by cbfs_walk.
79 * Returns 0 on success, or non-zero to stop further iteration. */
80typedef int (*cbfs_entry_callback)(struct cbfs_image *image,
81 struct cbfs_file *file,
82 void *arg);
83
84/* Iterates through all entries in CBFS image, and invoke with callback.
85 * Stops if callback returns non-zero values.
86 * Returns number of entries invoked. */
87int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, void *arg);
88
Hung-Te Lineab2c812013-01-29 01:56:17 +080089/* Primitive CBFS utilities */
90
91/* Returns a pointer to the only valid CBFS header in give buffer, otherwise
92 * NULL (including when multiple headers were found). If there is a X86 ROM
93 * style signature (pointer at 0xfffffffc) found in ROM, it will be selected as
94 * the only header.*/
95struct cbfs_header *cbfs_find_header(char *data, size_t size);
96
97/* Returns the first cbfs_file entry in CBFS image by CBFS header (no matter if
98 * the entry has valid content or not), otherwise NULL. */
99struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image);
100
101/* Returns next cbfs_file entry (no matter if its content is valid or not), or
102 * NULL on failure. */
103struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,
104 struct cbfs_file *entry);
105
106/* Returns ROM address (offset) of entry.
107 * This is different from entry->offset (pointer to content). */
108uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry);
109
110/* Returns 1 if entry has valid data (by checking magic number), otherwise 0. */
111int cbfs_is_valid_entry(struct cbfs_file *entry);
112
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800113/* Print CBFS component information. */
114int cbfs_print_directory(struct cbfs_image *image);
115int cbfs_print_header_info(struct cbfs_image *image);
116int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry,
117 void *arg);
118
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800119/* Merge empty entries starting from given entry.
120 * Returns 0 on success, otherwise non-zero. */
121int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry,
122 void *arg);
123
Hung-Te Lineab2c812013-01-29 01:56:17 +0800124#endif