blob: 267174386be242ac55839a4ece7dff66363567c8 [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) {
Sol Boucherb9740812015-03-18 10:13:48 -070031 ERROR("Section name ('%s') exceeds %d character FMAP format limit\n",
32 section->name, FMAP_STRLEN - 1);
Sol Boucher69b88bf2015-02-26 11:47:19 -080033 return false;
34 }
35
36 absolute_watermark += section->offset;
37
38 if (fmap_append_area(flashmap, absolute_watermark, section->size,
39 (uint8_t *)section->name, 0) < 0) {
Sol Boucherb9740812015-03-18 10:13:48 -070040 ERROR("Failed to insert section '%s' into FMAP\n",
41 section->name);
Sol Boucher69b88bf2015-02-26 11:47:19 -080042 return false;
43 }
44
45 fmd_foreach_child(subsection, section) {
46 if (!fmap_append_fmd_node(flashmap, subsection,
47 absolute_watermark))
48 return false;
49 }
50
51 return true;
52}
53
54struct fmap *fmap_from_fmd(const struct flashmap_descriptor *desc)
55{
56 assert(desc);
57 assert(desc->size_known);
58
59 if (strlen(desc->name) >= FMAP_STRLEN) {
Sol Boucherb9740812015-03-18 10:13:48 -070060 ERROR("Image name ('%s') exceeds %d character FMAP header limit\n",
Sol Boucher69b88bf2015-02-26 11:47:19 -080061 desc->name, FMAP_STRLEN - 1);
62 return NULL;
63 }
64
65 struct fmap *fmap = fmap_create(desc->offset_known ? desc->offset : 0,
66 desc->size, (uint8_t *)desc->name);
67 if (!fmap) {
Sol Boucherb9740812015-03-18 10:13:48 -070068 ERROR("Failed to allocate FMAP header\n");
Sol Boucher69b88bf2015-02-26 11:47:19 -080069 return fmap;
70 }
71
72 fmd_foreach_child(real_section, desc) {
73 if (!fmap_append_fmd_node(&fmap, real_section, 0)) {
74 fmap_destroy(fmap);
75 return NULL;
76 }
77 }
78
79 return fmap;
80}