Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* fmap_from_fmd.c, tool to distill flashmap descriptors into raw FMAP sections */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 3 | |
| 4 | #include "fmap_from_fmd.h" |
| 5 | |
| 6 | #include "common.h" |
| 7 | |
| 8 | #include <assert.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | static bool fmap_append_fmd_node(struct fmap **flashmap, |
| 12 | const struct flashmap_descriptor *section, |
| 13 | unsigned absolute_watermark) { |
Hung-Te Lin | 49a4450 | 2019-03-04 15:41:09 +0800 | [diff] [blame] | 14 | uint16_t flags = 0; |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 15 | if (strlen(section->name) >= FMAP_STRLEN) { |
Sol Boucher | b974081 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 16 | ERROR("Section name ('%s') exceeds %d character FMAP format limit\n", |
| 17 | section->name, FMAP_STRLEN - 1); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 18 | return false; |
| 19 | } |
| 20 | |
| 21 | absolute_watermark += section->offset; |
| 22 | |
Hung-Te Lin | 49a4450 | 2019-03-04 15:41:09 +0800 | [diff] [blame] | 23 | if (section->flags.f.preserve) |
| 24 | flags |= FMAP_AREA_PRESERVE; |
| 25 | |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 26 | if (fmap_append_area(flashmap, absolute_watermark, section->size, |
Hung-Te Lin | 49a4450 | 2019-03-04 15:41:09 +0800 | [diff] [blame] | 27 | (uint8_t *)section->name, flags) < 0) { |
Sol Boucher | b974081 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 28 | ERROR("Failed to insert section '%s' into FMAP\n", |
| 29 | section->name); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 30 | return false; |
| 31 | } |
| 32 | |
| 33 | fmd_foreach_child(subsection, section) { |
| 34 | if (!fmap_append_fmd_node(flashmap, subsection, |
| 35 | absolute_watermark)) |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | struct fmap *fmap_from_fmd(const struct flashmap_descriptor *desc) |
| 43 | { |
| 44 | assert(desc); |
| 45 | assert(desc->size_known); |
| 46 | |
| 47 | if (strlen(desc->name) >= FMAP_STRLEN) { |
Sol Boucher | b974081 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 48 | ERROR("Image name ('%s') exceeds %d character FMAP header limit\n", |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 49 | desc->name, FMAP_STRLEN - 1); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | struct fmap *fmap = fmap_create(desc->offset_known ? desc->offset : 0, |
| 54 | desc->size, (uint8_t *)desc->name); |
| 55 | if (!fmap) { |
Sol Boucher | b974081 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 56 | ERROR("Failed to allocate FMAP header\n"); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 57 | return fmap; |
| 58 | } |
| 59 | |
| 60 | fmd_foreach_child(real_section, desc) { |
| 61 | if (!fmap_append_fmd_node(&fmap, real_section, 0)) { |
| 62 | fmap_destroy(fmap); |
| 63 | return NULL; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return fmap; |
| 68 | } |