blob: f019d71fe4fa595f6c7f2ea784da3f73670c8842 [file] [log] [blame]
Sol Bouchere3260a02015-03-25 13:40:08 -07001/*
2 * partitioned_file.c, 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#include "partitioned_file.h"
17
18#include "cbfs_sections.h"
19
20#include <assert.h>
21#include <stdlib.h>
22#include <string.h>
23
24struct partitioned_file {
25 struct fmap *fmap;
26 struct buffer buffer;
27 FILE *stream;
28};
29
30static bool fill_ones_through(struct partitioned_file *file)
31{
32 assert(file);
33
34 memset(file->buffer.data, 0xff, file->buffer.size);
35 return partitioned_file_write_region(file, &file->buffer);
36}
37
38static unsigned count_selected_fmap_entries(const struct fmap *fmap,
39 partitioned_file_fmap_selector_t callback, const void *arg)
40{
41 assert(fmap);
42 assert(callback);
43
44 unsigned count = 0;
Kyösti Mälkki2e042592015-05-15 09:14:09 +030045 for (unsigned i = 0; i < fmap->nareas; ++i) {
46 if (callback(fmap->areas + i, arg))
Sol Bouchere3260a02015-03-25 13:40:08 -070047 ++count;
48 }
49 return count;
50}
51
52static partitioned_file_t *reopen_flat_file(const char *filename)
53{
54 assert(filename);
55
56 struct partitioned_file *file = calloc(1, sizeof(*file));
57 if (!file) {
58 ERROR("Failed to allocate partitioned file structure\n");
59 return NULL;
60 }
61
62 if (buffer_from_file(&file->buffer, filename)) {
63 free(file);
64 return NULL;
65 }
66
67 file->stream = fopen(filename, "rb+");
68 if (!file->stream) {
69 perror(filename);
70 partitioned_file_close(file);
71 return NULL;
72 }
73
74 return file;
75}
76
77partitioned_file_t *partitioned_file_create_flat(const char *filename,
78 size_t image_size)
79{
80 assert(filename);
81
82 struct partitioned_file *file = calloc(1, sizeof(*file));
83 if (!file) {
84 ERROR("Failed to allocate partitioned file structure\n");
85 return NULL;
86 }
87
88 file->stream = fopen(filename, "wb");
89 if (!file->stream) {
90 perror(filename);
91 free(file);
92 return NULL;
93 }
94
95 if (buffer_create(&file->buffer, image_size, filename)) {
96 partitioned_file_close(file);
97 return NULL;
98 }
99
100 if (!fill_ones_through(file)) {
101 partitioned_file_close(file);
102 return NULL;
103 }
104
105 return file;
106}
107
108partitioned_file_t *partitioned_file_create(const char *filename,
109 struct buffer *flashmap)
110{
111 assert(filename);
112 assert(flashmap);
113 assert(flashmap->data);
114
115 if (fmap_find((const uint8_t *)flashmap->data, flashmap->size) != 0) {
116 ERROR("Attempted to create a partitioned image out of something that isn't an FMAP\n");
117 return NULL;
118 }
119 struct fmap *bootstrap_fmap = (struct fmap *)flashmap->data;
120
121 const struct fmap_area *fmap_area =
122 fmap_find_area(bootstrap_fmap, SECTION_NAME_FMAP);
123 if (!fmap_area) {
124 ERROR("Provided FMAP missing '%s' region\n", SECTION_NAME_FMAP);
125 return NULL;
126 }
127
128 if (count_selected_fmap_entries(bootstrap_fmap,
129 partitioned_file_fmap_select_children_of, fmap_area)) {
130 ERROR("Provided FMAP's '%s' region contains other regions\n",
131 SECTION_NAME_FMAP);
132 return NULL;
133 }
134
135 int fmap_len = fmap_size(bootstrap_fmap);
136 if (fmap_len < 0) {
137 ERROR("Unable to determine size of provided FMAP\n");
138 return NULL;
139 }
140 assert((size_t)fmap_len <= flashmap->size);
141 if ((uint32_t)fmap_len > fmap_area->size) {
142 ERROR("Provided FMAP's '%s' region needs to be at least %d bytes\n",
143 SECTION_NAME_FMAP, fmap_len);
144 return NULL;
145 }
146
147 partitioned_file_t *file = partitioned_file_create_flat(filename,
148 bootstrap_fmap->size);
149 if (!file)
150 return NULL;
151
152 struct buffer fmap_region;
153 buffer_splice(&fmap_region, &file->buffer, fmap_area->offset, fmap_area->size);
154 memcpy(fmap_region.data, bootstrap_fmap, fmap_len);
155 if (!partitioned_file_write_region(file, &fmap_region)) {
156 partitioned_file_close(file);
157 return NULL;
158 }
159 file->fmap = (struct fmap *)(file->buffer.data + fmap_area->offset);
160
161 return file;
162}
163
Patrick Georgi2f953d32015-09-11 18:34:39 +0200164partitioned_file_t *partitioned_file_reopen(const char *filename)
Sol Bouchere3260a02015-03-25 13:40:08 -0700165{
166 assert(filename);
167
168 partitioned_file_t *file = reopen_flat_file(filename);
169 if (!file)
170 return NULL;
171
Sol Bouchere3260a02015-03-25 13:40:08 -0700172 long fmap_region_offset = fmap_find((const uint8_t *)file->buffer.data,
173 file->buffer.size);
174 if (fmap_region_offset < 0) {
175 INFO("Opening image as a flat file because it doesn't contain any FMAP\n");
176 return file;
177 }
178 file->fmap = (struct fmap *)(file->buffer.data + fmap_region_offset);
179
180 if (file->fmap->size > file->buffer.size) {
181 int fmap_region_size = fmap_size(file->fmap);
182 ERROR("FMAP records image size as %u, but file is only %zu bytes%s\n",
183 file->fmap->size, file->buffer.size,
184 fmap_region_offset == 0 &&
185 (signed)file->buffer.size == fmap_region_size ?
186 " (is it really an image, or *just* an FMAP?)" :
187 " (did something truncate this file?)");
188 partitioned_file_close(file);
189 return NULL;
190 }
191
192 const struct fmap_area *fmap_fmap_entry =
193 fmap_find_area(file->fmap, SECTION_NAME_FMAP);
Patrick Georgic7a8c382015-05-09 08:36:28 +0200194 if ((long)fmap_fmap_entry->offset != fmap_region_offset) {
Sol Bouchere3260a02015-03-25 13:40:08 -0700195 ERROR("FMAP's '%s' section doesn't point back to FMAP start (did something corrupt this file?)\n",
196 SECTION_NAME_FMAP);
197 partitioned_file_close(file);
198 return NULL;
199 }
200
201 return file;
202}
203
204bool partitioned_file_write_region(partitioned_file_t *file,
205 const struct buffer *buffer)
206{
207 assert(file);
208 assert(file->stream);
209 assert(buffer);
210 assert(buffer->data);
211
212 if (buffer->data - buffer->offset != file->buffer.data) {
213 ERROR("Attempted to write a partition buffer back to a different file than it came from\n");
214 return false;
215 }
216 if (buffer->offset + buffer->size > file->buffer.size) {
217 ERROR("Attempted to write data off the end of image file\n");
218 return false;
219 }
220
221 if (fseek(file->stream, buffer->offset, SEEK_SET)) {
222 ERROR("Failed to seek within image file\n");
223 return false;
224 }
225 if (!fwrite(buffer->data, buffer->size, 1, file->stream)) {
226 ERROR("Failed to write to image file\n");
227 return false;
228 }
229 return true;
230}
231
232bool partitioned_file_read_region(struct buffer *dest,
233 const partitioned_file_t *file, const char *region)
234{
235 assert(dest);
236 assert(file);
237 assert(file->buffer.data);
238 assert(region);
239
240 if (file->fmap) {
241 const struct fmap_area *area = fmap_find_area(file->fmap,
242 region);
243 if (!area) {
244 ERROR("Image is missing '%s' region\n", region);
245 return false;
246 }
247 if (area->offset + area->size > file->buffer.size) {
248 ERROR("Region '%s' runs off the end of the image file\n",
249 region);
250 return false;
251 }
252 buffer_splice(dest, &file->buffer, area->offset, area->size);
253 } else {
254 if (strcmp(region, SECTION_NAME_PRIMARY_CBFS) != 0) {
255 ERROR("This is a legacy image that contains only a CBFS\n");
256 return false;
257 }
258 buffer_clone(dest, &file->buffer);
259 }
260
261 return true;
262}
263
264void partitioned_file_close(partitioned_file_t *file)
265{
266 if (!file)
267 return;
268
269 file->fmap = NULL;
270 buffer_delete(&file->buffer);
271 if (file->stream) {
272 fclose(file->stream);
273 file->stream = NULL;
274 }
275 free(file);
276}
277
278bool partitioned_file_is_partitioned(const partitioned_file_t *file)
279{
280 return partitioned_file_get_fmap(file) != NULL;
281}
282
Sol Boucher67d59982015-05-07 02:39:22 -0700283size_t partitioned_file_total_size(const partitioned_file_t *file)
284{
285 assert(file);
286
287 return file->buffer.size;
288}
289
Sol Bouchere3260a02015-03-25 13:40:08 -0700290bool partitioned_file_region_check_magic(const partitioned_file_t *file,
291 const char *region, const char *magic, size_t magic_len)
292{
293 struct buffer area;
294 return partitioned_file_read_region(&area, file, region) &&
295 buffer_check_magic(&area, magic, magic_len);
296}
297
298bool partitioned_file_region_contains_nested(const partitioned_file_t *file,
299 const char *region)
300{
301 assert(file);
302 assert(region);
303
304 if (!file->fmap)
305 return false;
306 const struct fmap_area *area = fmap_find_area(file->fmap, region);
307 return area && partitioned_file_fmap_count(file,
308 partitioned_file_fmap_select_children_of, area);
309}
310
311const struct fmap *partitioned_file_get_fmap(const partitioned_file_t *file)
312{
313 assert(file);
314
315 return file->fmap;
316}
317
318unsigned partitioned_file_fmap_count(const partitioned_file_t *file,
319 partitioned_file_fmap_selector_t callback, const void *arg)
320{
321 assert(file);
322 assert(callback);
323
324 if (!file->fmap)
325 return 0;
326 return count_selected_fmap_entries(file->fmap, callback, arg);
327}
328
329static bool select_all(unused const struct fmap_area *area,
330 unused const void *arg)
331{
332 return true;
333}
334const partitioned_file_fmap_selector_t partitioned_file_fmap_select_all =
335 select_all;
336
337static bool select_children_of(const struct fmap_area *child, const void *arg)
338{
339 assert(child);
340 assert(arg);
341
342 const struct fmap_area *parent = (const struct fmap_area *)arg;
343 if (child == arg || (child->offset == parent->offset &&
344 child->size == parent->size))
345 return false;
346 return child->offset >= parent->offset &&
347 child->offset + child->size <= parent->offset + parent->size;
348}
349const partitioned_file_fmap_selector_t
350 partitioned_file_fmap_select_children_of = select_children_of;
351
352static bool select_parents_of(const struct fmap_area *parent, const void *arg)
353{
354 return select_children_of((const struct fmap_area *)arg, parent);
355}
356const partitioned_file_fmap_selector_t partitioned_file_fmap_select_parents_of =
357 select_parents_of;