Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1 | /* |
| 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. |
| 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 |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "partitioned_file.h" |
| 21 | |
| 22 | #include "cbfs_sections.h" |
| 23 | |
| 24 | #include <assert.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | struct partitioned_file { |
| 29 | struct fmap *fmap; |
| 30 | struct buffer buffer; |
| 31 | FILE *stream; |
| 32 | }; |
| 33 | |
| 34 | static bool fill_ones_through(struct partitioned_file *file) |
| 35 | { |
| 36 | assert(file); |
| 37 | |
| 38 | memset(file->buffer.data, 0xff, file->buffer.size); |
| 39 | return partitioned_file_write_region(file, &file->buffer); |
| 40 | } |
| 41 | |
| 42 | static unsigned count_selected_fmap_entries(const struct fmap *fmap, |
| 43 | partitioned_file_fmap_selector_t callback, const void *arg) |
| 44 | { |
| 45 | assert(fmap); |
| 46 | assert(callback); |
| 47 | |
| 48 | unsigned count = 0; |
| 49 | for (unsigned index = 0; index < fmap->nareas; ++index) { |
| 50 | if (callback(fmap->areas + index, arg)) |
| 51 | ++count; |
| 52 | } |
| 53 | return count; |
| 54 | } |
| 55 | |
| 56 | static partitioned_file_t *reopen_flat_file(const char *filename) |
| 57 | { |
| 58 | assert(filename); |
| 59 | |
| 60 | struct partitioned_file *file = calloc(1, sizeof(*file)); |
| 61 | if (!file) { |
| 62 | ERROR("Failed to allocate partitioned file structure\n"); |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | if (buffer_from_file(&file->buffer, filename)) { |
| 67 | free(file); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | file->stream = fopen(filename, "rb+"); |
| 72 | if (!file->stream) { |
| 73 | perror(filename); |
| 74 | partitioned_file_close(file); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | return file; |
| 79 | } |
| 80 | |
| 81 | partitioned_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 | |
| 112 | partitioned_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 | |
| 168 | partitioned_file_t *partitioned_file_reopen(const char *filename, |
| 169 | partitioned_file_flat_decider_t flat_override) |
| 170 | { |
| 171 | assert(filename); |
| 172 | |
| 173 | partitioned_file_t *file = reopen_flat_file(filename); |
| 174 | if (!file) |
| 175 | return NULL; |
| 176 | |
| 177 | if (flat_override && flat_override(&file->buffer)) { |
| 178 | INFO("Opening image as a flat file in response to explicit request\n"); |
| 179 | return file; |
| 180 | } |
| 181 | |
| 182 | long fmap_region_offset = fmap_find((const uint8_t *)file->buffer.data, |
| 183 | file->buffer.size); |
| 184 | if (fmap_region_offset < 0) { |
| 185 | INFO("Opening image as a flat file because it doesn't contain any FMAP\n"); |
| 186 | return file; |
| 187 | } |
| 188 | file->fmap = (struct fmap *)(file->buffer.data + fmap_region_offset); |
| 189 | |
| 190 | if (file->fmap->size > file->buffer.size) { |
| 191 | int fmap_region_size = fmap_size(file->fmap); |
| 192 | ERROR("FMAP records image size as %u, but file is only %zu bytes%s\n", |
| 193 | file->fmap->size, file->buffer.size, |
| 194 | fmap_region_offset == 0 && |
| 195 | (signed)file->buffer.size == fmap_region_size ? |
| 196 | " (is it really an image, or *just* an FMAP?)" : |
| 197 | " (did something truncate this file?)"); |
| 198 | partitioned_file_close(file); |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | const struct fmap_area *fmap_fmap_entry = |
| 203 | fmap_find_area(file->fmap, SECTION_NAME_FMAP); |
| 204 | if (fmap_fmap_entry->offset != fmap_region_offset) { |
| 205 | ERROR("FMAP's '%s' section doesn't point back to FMAP start (did something corrupt this file?)\n", |
| 206 | SECTION_NAME_FMAP); |
| 207 | partitioned_file_close(file); |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | return file; |
| 212 | } |
| 213 | |
| 214 | bool partitioned_file_write_region(partitioned_file_t *file, |
| 215 | const struct buffer *buffer) |
| 216 | { |
| 217 | assert(file); |
| 218 | assert(file->stream); |
| 219 | assert(buffer); |
| 220 | assert(buffer->data); |
| 221 | |
| 222 | if (buffer->data - buffer->offset != file->buffer.data) { |
| 223 | ERROR("Attempted to write a partition buffer back to a different file than it came from\n"); |
| 224 | return false; |
| 225 | } |
| 226 | if (buffer->offset + buffer->size > file->buffer.size) { |
| 227 | ERROR("Attempted to write data off the end of image file\n"); |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | if (fseek(file->stream, buffer->offset, SEEK_SET)) { |
| 232 | ERROR("Failed to seek within image file\n"); |
| 233 | return false; |
| 234 | } |
| 235 | if (!fwrite(buffer->data, buffer->size, 1, file->stream)) { |
| 236 | ERROR("Failed to write to image file\n"); |
| 237 | return false; |
| 238 | } |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | bool partitioned_file_read_region(struct buffer *dest, |
| 243 | const partitioned_file_t *file, const char *region) |
| 244 | { |
| 245 | assert(dest); |
| 246 | assert(file); |
| 247 | assert(file->buffer.data); |
| 248 | assert(region); |
| 249 | |
| 250 | if (file->fmap) { |
| 251 | const struct fmap_area *area = fmap_find_area(file->fmap, |
| 252 | region); |
| 253 | if (!area) { |
| 254 | ERROR("Image is missing '%s' region\n", region); |
| 255 | return false; |
| 256 | } |
| 257 | if (area->offset + area->size > file->buffer.size) { |
| 258 | ERROR("Region '%s' runs off the end of the image file\n", |
| 259 | region); |
| 260 | return false; |
| 261 | } |
| 262 | buffer_splice(dest, &file->buffer, area->offset, area->size); |
| 263 | } else { |
| 264 | if (strcmp(region, SECTION_NAME_PRIMARY_CBFS) != 0) { |
| 265 | ERROR("This is a legacy image that contains only a CBFS\n"); |
| 266 | return false; |
| 267 | } |
| 268 | buffer_clone(dest, &file->buffer); |
| 269 | } |
| 270 | |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | void partitioned_file_close(partitioned_file_t *file) |
| 275 | { |
| 276 | if (!file) |
| 277 | return; |
| 278 | |
| 279 | file->fmap = NULL; |
| 280 | buffer_delete(&file->buffer); |
| 281 | if (file->stream) { |
| 282 | fclose(file->stream); |
| 283 | file->stream = NULL; |
| 284 | } |
| 285 | free(file); |
| 286 | } |
| 287 | |
| 288 | bool partitioned_file_is_partitioned(const partitioned_file_t *file) |
| 289 | { |
| 290 | return partitioned_file_get_fmap(file) != NULL; |
| 291 | } |
| 292 | |
| 293 | bool partitioned_file_region_check_magic(const partitioned_file_t *file, |
| 294 | const char *region, const char *magic, size_t magic_len) |
| 295 | { |
| 296 | struct buffer area; |
| 297 | return partitioned_file_read_region(&area, file, region) && |
| 298 | buffer_check_magic(&area, magic, magic_len); |
| 299 | } |
| 300 | |
| 301 | bool partitioned_file_region_contains_nested(const partitioned_file_t *file, |
| 302 | const char *region) |
| 303 | { |
| 304 | assert(file); |
| 305 | assert(region); |
| 306 | |
| 307 | if (!file->fmap) |
| 308 | return false; |
| 309 | const struct fmap_area *area = fmap_find_area(file->fmap, region); |
| 310 | return area && partitioned_file_fmap_count(file, |
| 311 | partitioned_file_fmap_select_children_of, area); |
| 312 | } |
| 313 | |
| 314 | const struct fmap *partitioned_file_get_fmap(const partitioned_file_t *file) |
| 315 | { |
| 316 | assert(file); |
| 317 | |
| 318 | return file->fmap; |
| 319 | } |
| 320 | |
| 321 | unsigned partitioned_file_fmap_count(const partitioned_file_t *file, |
| 322 | partitioned_file_fmap_selector_t callback, const void *arg) |
| 323 | { |
| 324 | assert(file); |
| 325 | assert(callback); |
| 326 | |
| 327 | if (!file->fmap) |
| 328 | return 0; |
| 329 | return count_selected_fmap_entries(file->fmap, callback, arg); |
| 330 | } |
| 331 | |
| 332 | static bool select_all(unused const struct fmap_area *area, |
| 333 | unused const void *arg) |
| 334 | { |
| 335 | return true; |
| 336 | } |
| 337 | const partitioned_file_fmap_selector_t partitioned_file_fmap_select_all = |
| 338 | select_all; |
| 339 | |
| 340 | static bool select_children_of(const struct fmap_area *child, const void *arg) |
| 341 | { |
| 342 | assert(child); |
| 343 | assert(arg); |
| 344 | |
| 345 | const struct fmap_area *parent = (const struct fmap_area *)arg; |
| 346 | if (child == arg || (child->offset == parent->offset && |
| 347 | child->size == parent->size)) |
| 348 | return false; |
| 349 | return child->offset >= parent->offset && |
| 350 | child->offset + child->size <= parent->offset + parent->size; |
| 351 | } |
| 352 | const partitioned_file_fmap_selector_t |
| 353 | partitioned_file_fmap_select_children_of = select_children_of; |
| 354 | |
| 355 | static bool select_parents_of(const struct fmap_area *parent, const void *arg) |
| 356 | { |
| 357 | return select_children_of((const struct fmap_area *)arg, parent); |
| 358 | } |
| 359 | const partitioned_file_fmap_selector_t partitioned_file_fmap_select_parents_of = |
| 360 | select_parents_of; |
| 361 | |
| 362 | static bool open_as_flat(unused struct buffer *buffer) |
| 363 | { |
| 364 | return true; |
| 365 | } |
| 366 | const partitioned_file_flat_decider_t partitioned_file_open_as_flat = |
| 367 | open_as_flat; |