blob: a2d9ddfee23029ec95547a6cef4d89dd91a9e6ae [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
Vadim Bendebury75599462015-12-09 09:39:31 -080052static partitioned_file_t *reopen_flat_file(const char *filename,
53 bool write_access)
Sol Bouchere3260a02015-03-25 13:40:08 -070054{
55 assert(filename);
Sol Bouchere3260a02015-03-25 13:40:08 -070056 struct partitioned_file *file = calloc(1, sizeof(*file));
Vadim Bendebury75599462015-12-09 09:39:31 -080057 const char *access_mode;
58
Sol Bouchere3260a02015-03-25 13:40:08 -070059 if (!file) {
60 ERROR("Failed to allocate partitioned file structure\n");
61 return NULL;
62 }
63
64 if (buffer_from_file(&file->buffer, filename)) {
65 free(file);
66 return NULL;
67 }
68
Vadim Bendebury75599462015-12-09 09:39:31 -080069 access_mode = write_access ? "rb+" : "rb";
70 file->stream = fopen(filename, access_mode);
71
Sol Bouchere3260a02015-03-25 13:40:08 -070072 if (!file->stream) {
73 perror(filename);
74 partitioned_file_close(file);
75 return NULL;
76 }
77
78 return file;
79}
80
81partitioned_file_t *partitioned_file_create_flat(const char *filename,
82 size_t image_size)
83{
84 assert(filename);
85
86 struct partitioned_file *file = calloc(1, sizeof(*file));
87 if (!file) {
88 ERROR("Failed to allocate partitioned file structure\n");
89 return NULL;
90 }
91
92 file->stream = fopen(filename, "wb");
93 if (!file->stream) {
94 perror(filename);
95 free(file);
96 return NULL;
97 }
98
99 if (buffer_create(&file->buffer, image_size, filename)) {
100 partitioned_file_close(file);
101 return NULL;
102 }
103
104 if (!fill_ones_through(file)) {
105 partitioned_file_close(file);
106 return NULL;
107 }
108
109 return file;
110}
111
112partitioned_file_t *partitioned_file_create(const char *filename,
113 struct buffer *flashmap)
114{
115 assert(filename);
116 assert(flashmap);
117 assert(flashmap->data);
118
119 if (fmap_find((const uint8_t *)flashmap->data, flashmap->size) != 0) {
120 ERROR("Attempted to create a partitioned image out of something that isn't an FMAP\n");
121 return NULL;
122 }
123 struct fmap *bootstrap_fmap = (struct fmap *)flashmap->data;
124
125 const struct fmap_area *fmap_area =
126 fmap_find_area(bootstrap_fmap, SECTION_NAME_FMAP);
127 if (!fmap_area) {
128 ERROR("Provided FMAP missing '%s' region\n", SECTION_NAME_FMAP);
129 return NULL;
130 }
131
132 if (count_selected_fmap_entries(bootstrap_fmap,
133 partitioned_file_fmap_select_children_of, fmap_area)) {
134 ERROR("Provided FMAP's '%s' region contains other regions\n",
135 SECTION_NAME_FMAP);
136 return NULL;
137 }
138
139 int fmap_len = fmap_size(bootstrap_fmap);
140 if (fmap_len < 0) {
141 ERROR("Unable to determine size of provided FMAP\n");
142 return NULL;
143 }
144 assert((size_t)fmap_len <= flashmap->size);
145 if ((uint32_t)fmap_len > fmap_area->size) {
146 ERROR("Provided FMAP's '%s' region needs to be at least %d bytes\n",
147 SECTION_NAME_FMAP, fmap_len);
148 return NULL;
149 }
150
151 partitioned_file_t *file = partitioned_file_create_flat(filename,
152 bootstrap_fmap->size);
153 if (!file)
154 return NULL;
155
156 struct buffer fmap_region;
157 buffer_splice(&fmap_region, &file->buffer, fmap_area->offset, fmap_area->size);
158 memcpy(fmap_region.data, bootstrap_fmap, fmap_len);
159 if (!partitioned_file_write_region(file, &fmap_region)) {
160 partitioned_file_close(file);
161 return NULL;
162 }
163 file->fmap = (struct fmap *)(file->buffer.data + fmap_area->offset);
164
165 return file;
166}
167
Vadim Bendebury75599462015-12-09 09:39:31 -0800168partitioned_file_t *partitioned_file_reopen(const char *filename,
169 bool write_access)
Sol Bouchere3260a02015-03-25 13:40:08 -0700170{
171 assert(filename);
172
Vadim Bendebury75599462015-12-09 09:39:31 -0800173 partitioned_file_t *file = reopen_flat_file(filename, write_access);
Sol Bouchere3260a02015-03-25 13:40:08 -0700174 if (!file)
175 return NULL;
176
Sol Bouchere3260a02015-03-25 13:40:08 -0700177 long fmap_region_offset = fmap_find((const uint8_t *)file->buffer.data,
178 file->buffer.size);
179 if (fmap_region_offset < 0) {
180 INFO("Opening image as a flat file because it doesn't contain any FMAP\n");
181 return file;
182 }
183 file->fmap = (struct fmap *)(file->buffer.data + fmap_region_offset);
184
185 if (file->fmap->size > file->buffer.size) {
186 int fmap_region_size = fmap_size(file->fmap);
187 ERROR("FMAP records image size as %u, but file is only %zu bytes%s\n",
188 file->fmap->size, file->buffer.size,
189 fmap_region_offset == 0 &&
190 (signed)file->buffer.size == fmap_region_size ?
191 " (is it really an image, or *just* an FMAP?)" :
192 " (did something truncate this file?)");
193 partitioned_file_close(file);
194 return NULL;
195 }
196
197 const struct fmap_area *fmap_fmap_entry =
198 fmap_find_area(file->fmap, SECTION_NAME_FMAP);
Martin Rothf183a852017-12-15 10:18:57 -0700199
Jacob Garber2e31ea02019-07-02 17:50:59 -0600200 if (!fmap_fmap_entry) {
201 partitioned_file_close(file);
Martin Rothf183a852017-12-15 10:18:57 -0700202 return NULL;
Jacob Garber2e31ea02019-07-02 17:50:59 -0600203 }
Martin Rothf183a852017-12-15 10:18:57 -0700204
Patrick Georgic7a8c382015-05-09 08:36:28 +0200205 if ((long)fmap_fmap_entry->offset != fmap_region_offset) {
Sol Bouchere3260a02015-03-25 13:40:08 -0700206 ERROR("FMAP's '%s' section doesn't point back to FMAP start (did something corrupt this file?)\n",
207 SECTION_NAME_FMAP);
208 partitioned_file_close(file);
209 return NULL;
210 }
211
212 return file;
213}
214
215bool partitioned_file_write_region(partitioned_file_t *file,
216 const struct buffer *buffer)
217{
218 assert(file);
219 assert(file->stream);
220 assert(buffer);
221 assert(buffer->data);
222
223 if (buffer->data - buffer->offset != file->buffer.data) {
224 ERROR("Attempted to write a partition buffer back to a different file than it came from\n");
225 return false;
226 }
227 if (buffer->offset + buffer->size > file->buffer.size) {
228 ERROR("Attempted to write data off the end of image file\n");
229 return false;
230 }
231
232 if (fseek(file->stream, buffer->offset, SEEK_SET)) {
233 ERROR("Failed to seek within image file\n");
234 return false;
235 }
236 if (!fwrite(buffer->data, buffer->size, 1, file->stream)) {
237 ERROR("Failed to write to image file\n");
238 return false;
239 }
240 return true;
241}
242
243bool partitioned_file_read_region(struct buffer *dest,
244 const partitioned_file_t *file, const char *region)
245{
246 assert(dest);
247 assert(file);
248 assert(file->buffer.data);
249 assert(region);
250
251 if (file->fmap) {
252 const struct fmap_area *area = fmap_find_area(file->fmap,
253 region);
254 if (!area) {
255 ERROR("Image is missing '%s' region\n", region);
256 return false;
257 }
258 if (area->offset + area->size > file->buffer.size) {
259 ERROR("Region '%s' runs off the end of the image file\n",
260 region);
261 return false;
262 }
263 buffer_splice(dest, &file->buffer, area->offset, area->size);
264 } else {
265 if (strcmp(region, SECTION_NAME_PRIMARY_CBFS) != 0) {
266 ERROR("This is a legacy image that contains only a CBFS\n");
267 return false;
268 }
269 buffer_clone(dest, &file->buffer);
270 }
271
272 return true;
273}
274
275void partitioned_file_close(partitioned_file_t *file)
276{
277 if (!file)
278 return;
279
280 file->fmap = NULL;
281 buffer_delete(&file->buffer);
282 if (file->stream) {
283 fclose(file->stream);
284 file->stream = NULL;
285 }
286 free(file);
287}
288
289bool partitioned_file_is_partitioned(const partitioned_file_t *file)
290{
291 return partitioned_file_get_fmap(file) != NULL;
292}
293
Sol Boucher67d59982015-05-07 02:39:22 -0700294size_t partitioned_file_total_size(const partitioned_file_t *file)
295{
296 assert(file);
297
298 return file->buffer.size;
299}
300
Sol Bouchere3260a02015-03-25 13:40:08 -0700301bool partitioned_file_region_check_magic(const partitioned_file_t *file,
302 const char *region, const char *magic, size_t magic_len)
303{
304 struct buffer area;
305 return partitioned_file_read_region(&area, file, region) &&
306 buffer_check_magic(&area, magic, magic_len);
307}
308
309bool partitioned_file_region_contains_nested(const partitioned_file_t *file,
310 const char *region)
311{
312 assert(file);
313 assert(region);
314
315 if (!file->fmap)
316 return false;
317 const struct fmap_area *area = fmap_find_area(file->fmap, region);
318 return area && partitioned_file_fmap_count(file,
319 partitioned_file_fmap_select_children_of, area);
320}
321
322const struct fmap *partitioned_file_get_fmap(const partitioned_file_t *file)
323{
324 assert(file);
325
326 return file->fmap;
327}
328
329unsigned partitioned_file_fmap_count(const partitioned_file_t *file,
330 partitioned_file_fmap_selector_t callback, const void *arg)
331{
332 assert(file);
333 assert(callback);
334
335 if (!file->fmap)
336 return 0;
337 return count_selected_fmap_entries(file->fmap, callback, arg);
338}
339
340static bool select_all(unused const struct fmap_area *area,
341 unused const void *arg)
342{
343 return true;
344}
345const partitioned_file_fmap_selector_t partitioned_file_fmap_select_all =
346 select_all;
347
348static bool select_children_of(const struct fmap_area *child, const void *arg)
349{
350 assert(child);
351 assert(arg);
352
353 const struct fmap_area *parent = (const struct fmap_area *)arg;
354 if (child == arg || (child->offset == parent->offset &&
355 child->size == parent->size))
356 return false;
357 return child->offset >= parent->offset &&
358 child->offset + child->size <= parent->offset + parent->size;
359}
360const partitioned_file_fmap_selector_t
361 partitioned_file_fmap_select_children_of = select_children_of;
362
363static bool select_parents_of(const struct fmap_area *parent, const void *arg)
364{
365 return select_children_of((const struct fmap_area *)arg, parent);
366}
367const partitioned_file_fmap_selector_t partitioned_file_fmap_select_parents_of =
368 select_parents_of;