blob: 2fc2bfe6b8210dafff93a5c9542683a67cacc767 [file] [log] [blame]
Sol Bouchere3260a02015-03-25 13:40:08 -07001/*
2 * partitioned_file.h, read and write binary file "partitions" described by FMAP
3 *
4 * Copyright (C) 2015 Google, Inc.
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.
Sol Bouchere3260a02015-03-25 13:40:08 -070014 */
15
16#ifndef PARITITONED_FILE_H_
17#define PARITITONED_FILE_H_
18
19#include "common.h"
20#include "flashmap/fmap.h"
21
22#include <stdbool.h>
23#include <stddef.h>
24
25typedef struct partitioned_file partitioned_file_t;
26
Sol Bouchere3260a02015-03-25 13:40:08 -070027/**
28 * Create a new filesystem-backed flat buffer.
29 * This backwards-compatibility function creates a new in-memory buffer and
30 * backing binary file of the specified size. Although the file won't actually
31 * have multiple regions, it'll still be possible to access and manipulate it
32 * using this module; this is accomplished by requesting the special region
33 * whose name matches SECTION_NAME_PRIMARY_CBFS, which maps to the whole file.
34 * Note that the caller will be responsible for calling partitioned_file_close()
35 * on the returned object, and that this function will overwrite any existing
36 * file with the given name without warning.
37 *
38 * @param filename Name of the backing file
39 * @param image_size Size of the image
40 * @return Caller-owned partitioned file, or NULL on error
41 */
42partitioned_file_t *partitioned_file_create_flat(const char *filename,
43 size_t image_size);
44
45/**
46 * Create a new filesystem-backed partitioned buffer.
47 * This creates a new in-memory buffer and backing binary file. Both are
48 * segmented into regions according to the provided flashmap's sections, and the
49 * flashmap itself is automatically copied into the region named
50 * SECTION_NAME_FMAP: a section with this name must already exist in the FMAP.
51 * After calling this function, it is safe for the caller to clean up flashmap
52 * at any time. The partitioned_file_t returned from this function is separately
53 * owned by the caller, and must later be passed to partitioned_file_close().
54 * Note that this function will overwrite any existing file with the given name
55 * without warning.
56 *
57 * @param filename Name of the backing file
58 * @param flashmap Buffer containing an FMAP file layout
59 * @return Caller-owned partitioned file, or NULL on error
60 */
61partitioned_file_t *partitioned_file_create(const char *filename,
62 struct buffer *flashmap);
63
64/**
65 * Read a file back in from the disk.
Patrick Georgi2f953d32015-09-11 18:34:39 +020066 * An in-memory buffer is created and populated with the file's
67 * contents. If the image contains an FMAP, it will be opened as a
68 * full partitioned file; otherwise, it will be opened as a flat file as
69 * if it had been created by partitioned_file_create_flat().
Sol Bouchere3260a02015-03-25 13:40:08 -070070 * The partitioned_file_t returned from this function is separately owned by the
71 * caller, and must later be passed to partitioned_file_close();
72 *
73 * @param filename Name of the file to read in
Sol Bouchere3260a02015-03-25 13:40:08 -070074 * @return Caller-owned partitioned file, or NULL on error
75 */
Patrick Georgi2f953d32015-09-11 18:34:39 +020076partitioned_file_t *partitioned_file_reopen(const char *filename);
Sol Bouchere3260a02015-03-25 13:40:08 -070077
78/**
79 * Write a buffer's contents to its original region within a segmented file.
80 * This function should only be called on buffers originally retrieved by a call
81 * to partitioned_file_read_region() on the same partitioned file object. The
82 * contents of this buffer are copied back to the same region of the buffer and
83 * backing file that the region occupied before.
84 *
85 * @param file Partitioned file to which to write the data
86 * @param buffer Modified buffer obtained from partitioned_file_read_region()
87 * @return Whether the operation was successful
88 */
89bool partitioned_file_write_region(partitioned_file_t *file,
90 const struct buffer *buffer);
91
92/**
93 * Obtain one particular region of a segmented file.
94 * The result is owned by the partitioned_file_t and shared among every caller
95 * of this function. Thus, it is an error to buffer_delete() it; instead, clean
96 * up the entire partitioned_file_t once it's no longer needed with a single
97 * call to partitioned_file_close().
98 * Note that, if the buffer obtained from this function is modified, the changes
99 * will be reflected in any buffers handed out---whether earlier or later---for
100 * any region inclusive of the altered location(s). However, the backing file
101 * will not be updated until someone calls partitioned_file_write_region() on a
102 * buffer that includes the alterations.
103 *
104 * @param dest Empty destination buffer for the data
105 * @param file Partitioned file from which to read the data
106 * @param region Name of the desired FMAP region
107 * @return Whether the copy was performed successfully
108 */
109bool partitioned_file_read_region(struct buffer *dest,
110 const partitioned_file_t *file, const char *region);
111
112/** @param file Partitioned file to flush and cleanup */
113void partitioned_file_close(partitioned_file_t *file);
114
115/** @return Whether the file is partitioned (i.e. not flat). */
116bool partitioned_file_is_partitioned(const partitioned_file_t *file);
117
Sol Boucher67d59982015-05-07 02:39:22 -0700118/** @return The image's overall filesize, regardless of whether it's flat. */
119size_t partitioned_file_total_size(const partitioned_file_t *file);
120
Sol Bouchere3260a02015-03-25 13:40:08 -0700121/** @return Whether the specified region begins with the magic bytes. */
122bool partitioned_file_region_check_magic(const partitioned_file_t *file,
123 const char *region, const char *magic, size_t magic_len);
124
125/** @return Whether the specified region exists and contains nested regions. */
126bool partitioned_file_region_contains_nested(const partitioned_file_t *file,
127 const char *region);
128
129/** @return An immutable reference to the FMAP, or NULL for flat images. */
130const struct fmap *partitioned_file_get_fmap(const partitioned_file_t *file);
131
132/** @return Whether to include area in the running count. */
133typedef bool (*partitioned_file_fmap_selector_t)
134 (const struct fmap_area *area, const void *arg);
135
136/**
137 * Count the number of FMAP entries fulfilling a certain criterion.
138 * The result is always 0 if run on a flat (non-partitioned) image.
139 *
140 * @param file File on whose FMAP entries the operation should be run
141 * @param callback Decider answering whether each individual region should count
142 * @param arg Additional information to furnish to the decider on each call
143 * @return The number of FMAP sections with that property
144 */
145unsigned partitioned_file_fmap_count(const partitioned_file_t *file,
146 partitioned_file_fmap_selector_t callback, const void *arg);
147
148/** Selector that counts every single FMAP section. */
149extern const partitioned_file_fmap_selector_t partitioned_file_fmap_select_all;
150
151/** Selector that counts FMAP sections that are descendants of fmap_area arg. */
152extern const partitioned_file_fmap_selector_t
153 partitioned_file_fmap_select_children_of;
154
155/** Selector that counts FMAP sections that contain the fmap_area arg. */
156extern const partitioned_file_fmap_selector_t
157 partitioned_file_fmap_select_parents_of;
158
159#endif