Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * fmap_from_fmd.c, tool to distill flashmap descriptors into raw FMAP sections |
| 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 Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "fmap_from_fmd.h" |
| 17 | |
| 18 | #include "common.h" |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | static bool fmap_append_fmd_node(struct fmap **flashmap, |
| 24 | const struct flashmap_descriptor *section, |
| 25 | unsigned absolute_watermark) { |
| 26 | if (strlen(section->name) >= FMAP_STRLEN) { |
Sol Boucher | b974081 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 27 | ERROR("Section name ('%s') exceeds %d character FMAP format limit\n", |
| 28 | section->name, FMAP_STRLEN - 1); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 29 | return false; |
| 30 | } |
| 31 | |
| 32 | absolute_watermark += section->offset; |
| 33 | |
| 34 | if (fmap_append_area(flashmap, absolute_watermark, section->size, |
| 35 | (uint8_t *)section->name, 0) < 0) { |
Sol Boucher | b974081 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 36 | ERROR("Failed to insert section '%s' into FMAP\n", |
| 37 | section->name); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 38 | return false; |
| 39 | } |
| 40 | |
| 41 | fmd_foreach_child(subsection, section) { |
| 42 | if (!fmap_append_fmd_node(flashmap, subsection, |
| 43 | absolute_watermark)) |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | struct fmap *fmap_from_fmd(const struct flashmap_descriptor *desc) |
| 51 | { |
| 52 | assert(desc); |
| 53 | assert(desc->size_known); |
| 54 | |
| 55 | if (strlen(desc->name) >= FMAP_STRLEN) { |
Sol Boucher | b974081 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 56 | ERROR("Image name ('%s') exceeds %d character FMAP header limit\n", |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 57 | desc->name, FMAP_STRLEN - 1); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | struct fmap *fmap = fmap_create(desc->offset_known ? desc->offset : 0, |
| 62 | desc->size, (uint8_t *)desc->name); |
| 63 | if (!fmap) { |
Sol Boucher | b974081 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 64 | ERROR("Failed to allocate FMAP header\n"); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 65 | return fmap; |
| 66 | } |
| 67 | |
| 68 | fmd_foreach_child(real_section, desc) { |
| 69 | if (!fmap_append_fmd_node(&fmap, real_section, 0)) { |
| 70 | fmap_destroy(fmap); |
| 71 | return NULL; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return fmap; |
| 76 | } |