blob: fa1024bd572eadc9b8e4ef26a550ab6cbd342fde [file] [log] [blame]
Sol Boucher69b88bf2015-02-26 11:47:19 -08001/*
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.
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 "fmap_from_fmd.h"
21
22#include "common.h"
23
24#include <assert.h>
25#include <string.h>
26
27static bool fmap_append_fmd_node(struct fmap **flashmap,
28 const struct flashmap_descriptor *section,
29 unsigned absolute_watermark) {
30 if (strlen(section->name) >= FMAP_STRLEN) {
31 fprintf(stderr,
32 "ERROR: Section name ('%s') exceeds %d character FMAP format limit\n",
33 section->name, FMAP_STRLEN - 1);
34 return false;
35 }
36
37 absolute_watermark += section->offset;
38
39 if (fmap_append_area(flashmap, absolute_watermark, section->size,
40 (uint8_t *)section->name, 0) < 0) {
41 fprintf(stderr,
42 "ERROR: Failed to insert section '%s' into FMAP\n",
43 section->name);
44 return false;
45 }
46
47 fmd_foreach_child(subsection, section) {
48 if (!fmap_append_fmd_node(flashmap, subsection,
49 absolute_watermark))
50 return false;
51 }
52
53 return true;
54}
55
56struct fmap *fmap_from_fmd(const struct flashmap_descriptor *desc)
57{
58 assert(desc);
59 assert(desc->size_known);
60
61 if (strlen(desc->name) >= FMAP_STRLEN) {
62 fprintf(stderr,
63 "ERROR: Image name ('%s') exceeds %d character FMAP header limit\n",
64 desc->name, FMAP_STRLEN - 1);
65 return NULL;
66 }
67
68 struct fmap *fmap = fmap_create(desc->offset_known ? desc->offset : 0,
69 desc->size, (uint8_t *)desc->name);
70 if (!fmap) {
71 fputs("ERROR: Failed to allocate FMAP header\n", stderr);
72 return fmap;
73 }
74
75 fmd_foreach_child(real_section, desc) {
76 if (!fmap_append_fmd_node(&fmap, real_section, 0)) {
77 fmap_destroy(fmap);
78 return NULL;
79 }
80 }
81
82 return fmap;
83}