AUTHORS, util/: Drop individual copyright notices
[coreboot.git] / util / cbfstool / fmap_from_fmd.c
blob1773050125b0d873c3fd29ec0537f7f62c4bd5da
1 /* fmap_from_fmd.c, tool to distill flashmap descriptors into raw FMAP sections */
2 /*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2 of the License.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include "fmap_from_fmd.h"
15 #include "common.h"
17 #include <assert.h>
18 #include <string.h>
20 static bool fmap_append_fmd_node(struct fmap **flashmap,
21 const struct flashmap_descriptor *section,
22 unsigned absolute_watermark) {
23 uint16_t flags = 0;
24 if (strlen(section->name) >= FMAP_STRLEN) {
25 ERROR("Section name ('%s') exceeds %d character FMAP format limit\n",
26 section->name, FMAP_STRLEN - 1);
27 return false;
30 absolute_watermark += section->offset;
32 if (section->flags.f.preserve)
33 flags |= FMAP_AREA_PRESERVE;
35 if (fmap_append_area(flashmap, absolute_watermark, section->size,
36 (uint8_t *)section->name, flags) < 0) {
37 ERROR("Failed to insert section '%s' into FMAP\n",
38 section->name);
39 return false;
42 fmd_foreach_child(subsection, section) {
43 if (!fmap_append_fmd_node(flashmap, subsection,
44 absolute_watermark))
45 return false;
48 return true;
51 struct fmap *fmap_from_fmd(const struct flashmap_descriptor *desc)
53 assert(desc);
54 assert(desc->size_known);
56 if (strlen(desc->name) >= FMAP_STRLEN) {
57 ERROR("Image name ('%s') exceeds %d character FMAP header limit\n",
58 desc->name, FMAP_STRLEN - 1);
59 return NULL;
62 struct fmap *fmap = fmap_create(desc->offset_known ? desc->offset : 0,
63 desc->size, (uint8_t *)desc->name);
64 if (!fmap) {
65 ERROR("Failed to allocate FMAP header\n");
66 return fmap;
69 fmd_foreach_child(real_section, desc) {
70 if (!fmap_append_fmd_node(&fmap, real_section, 0)) {
71 fmap_destroy(fmap);
72 return NULL;
76 return fmap;