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