summaryrefslogtreecommitdiff
path: root/util/cbfstool/partitioned_file.h
blob: 45833161e4ffe4396a0143a637b76943169a17e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * partitioned_file.h, read and write binary file "partitions" described by FMAP
 *
 * Copyright (C) 2015 Google, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc.
 */

#ifndef PARITITONED_FILE_H_
#define PARITITONED_FILE_H_

#include "common.h"
#include "flashmap/fmap.h"

#include <stdbool.h>
#include <stddef.h>

typedef struct partitioned_file partitioned_file_t;

/** @return Whether the specific existing file should be opened in flat mode. */
typedef bool (*partitioned_file_flat_decider_t)(struct buffer *buffer);

/** Pass to partitioned_file_reopen() to force opening as a partitioned file. */
#define partitioned_file_open_as_partitioned NULL

/** Pass to partitioned_file_reopen() to force opening as a flat file. */
extern const partitioned_file_flat_decider_t partitioned_file_open_as_flat;

/**
 * Create a new filesystem-backed flat buffer.
 * This backwards-compatibility function creates a new in-memory buffer and
 * backing binary file of the specified size. Although the file won't actually
 * have multiple regions, it'll still be possible to access and manipulate it
 * using this module; this is accomplished by requesting the special region
 * whose name matches SECTION_NAME_PRIMARY_CBFS, which maps to the whole file.
 * Note that the caller will be responsible for calling partitioned_file_close()
 * on the returned object, and that this function will overwrite any existing
 * file with the given name without warning.
 *
 * @param filename   Name of the backing file
 * @param image_size Size of the image
 * @return           Caller-owned partitioned file, or NULL on error
 */
partitioned_file_t *partitioned_file_create_flat(const char *filename,
							size_t image_size);

/**
 * Create a new filesystem-backed partitioned buffer.
 * This creates a new in-memory buffer and backing binary file. Both are
 * segmented into regions according to the provided flashmap's sections, and the
 * flashmap itself is automatically copied into the region named
 * SECTION_NAME_FMAP: a section with this name must already exist in the FMAP.
 * After calling this function, it is safe for the caller to clean up flashmap
 * at any time. The partitioned_file_t returned from this function is separately
 * owned by the caller, and must later be passed to partitioned_file_close().
 * Note that this function will overwrite any existing file with the given name
 * without warning.
 *
 * @param filename Name of the backing file
 * @param flashmap Buffer containing an FMAP file layout
 * @return         Caller-owned partitioned file, or NULL on error
 */
partitioned_file_t *partitioned_file_create(const char *filename,
						struct buffer *flashmap);

/**
 * Read a file back in from the disk.
 * An in-memory buffer is created and populated with the file's contents. If
 * flat_override is NULL and the image contains an FMAP, it will be opened as a
 * full partitioned file; otherwise, it will be opened as a flat file as if it
 * had been created by partitioned_file_create_flat(). This selection behavior
 * is extensible: if a flat_override function is provided, it is invoked before
 * searching for an FMAP, and has the option of explicitly instructing the
 * module to open the image as a flat file based on its contents.
 * The partitioned_file_t returned from this function is separately owned by the
 * caller, and must later be passed to partitioned_file_close();
 *
 * @param filename      Name of the file to read in
 * @param flat_override Callback that can decide to open it as flat, or NULL
 * @return              Caller-owned partitioned file, or NULL on error
 */
partitioned_file_t *partitioned_file_reopen(const char *filename,
				partitioned_file_flat_decider_t flat_override);

/**
 * Write a buffer's contents to its original region within a segmented file.
 * This function should only be called on buffers originally retrieved by a call
 * to partitioned_file_read_region() on the same partitioned file object. The
 * contents of this buffer are copied back to the same region of the buffer and
 * backing file that the region occupied before.
 *
 * @param file   Partitioned file to which to write the data
 * @param buffer Modified buffer obtained from partitioned_file_read_region()
 * @return       Whether the operation was successful
 */
bool partitioned_file_write_region(partitioned_file_t *file,
						const struct buffer *buffer);

/**
 * Obtain one particular region of a segmented file.
 * The result is owned by the partitioned_file_t and shared among every caller
 * of this function. Thus, it is an error to buffer_delete() it; instead, clean
 * up the entire partitioned_file_t once it's no longer needed with a single
 * call to partitioned_file_close().
 * Note that, if the buffer obtained from this function is modified, the changes
 * will be reflected in any buffers handed out---whether earlier or later---for
 * any region inclusive of the altered location(s). However, the backing file
 * will not be updated until someone calls partitioned_file_write_region() on a
 * buffer that includes the alterations.
 *
 * @param dest   Empty destination buffer for the data
 * @param file   Partitioned file from which to read the data
 * @param region Name of the desired FMAP region
 * @return       Whether the copy was performed successfully
 */
bool partitioned_file_read_region(struct buffer *dest,
			const partitioned_file_t *file, const char *region);

/** @param file Partitioned file to flush and cleanup */
void partitioned_file_close(partitioned_file_t *file);

/** @return Whether the file is partitioned (i.e. not flat). */
bool partitioned_file_is_partitioned(const partitioned_file_t *file);

/** @return The image's overall filesize, regardless of whether it's flat. */
size_t partitioned_file_total_size(const partitioned_file_t *file);

/** @return Whether the specified region begins with the magic bytes. */
bool partitioned_file_region_check_magic(const partitioned_file_t *file,
		const char *region, const char *magic, size_t magic_len);

/** @return Whether the specified region exists and contains nested regions. */
bool partitioned_file_region_contains_nested(const partitioned_file_t *file,
							const char *region);

/** @return An immutable reference to the FMAP, or NULL for flat images. */
const struct fmap *partitioned_file_get_fmap(const partitioned_file_t *file);

/** @return Whether to include area in the running count. */
typedef bool (*partitioned_file_fmap_selector_t)
				(const struct fmap_area *area, const void *arg);

/**
 * Count the number of FMAP entries fulfilling a certain criterion.
 * The result is always 0 if run on a flat (non-partitioned) image.
 *
 * @param file     File on whose FMAP entries the operation should be run
 * @param callback Decider answering whether each individual region should count
 * @param arg      Additional information to furnish to the decider on each call
 * @return         The number of FMAP sections with that property
 */
unsigned partitioned_file_fmap_count(const partitioned_file_t *file,
		partitioned_file_fmap_selector_t callback, const void *arg);

/** Selector that counts every single FMAP section. */
extern const partitioned_file_fmap_selector_t partitioned_file_fmap_select_all;

/** Selector that counts FMAP sections that are descendants of fmap_area arg. */
extern const partitioned_file_fmap_selector_t
				partitioned_file_fmap_select_children_of;

/** Selector that counts FMAP sections that contain the fmap_area arg. */
extern const partitioned_file_fmap_selector_t
					partitioned_file_fmap_select_parents_of;

#endif