mb/google/hatch: reflow comment
[coreboot.git] / util / cbfstool / fmap_from_fmd.c
blob374667a373a0a491633f299d855f7479ad8d24a1
1 /*
2 * fmap_from_fmd.c, tool to distill flashmap descriptors into raw FMAP sections
4 * Copyright (C) 2015 Google, Inc.
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.
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.
16 #include "fmap_from_fmd.h"
18 #include "common.h"
20 #include <assert.h>
21 #include <string.h>
23 static bool fmap_append_fmd_node(struct fmap **flashmap,
24 const struct flashmap_descriptor *section,
25 unsigned absolute_watermark) {
26 uint16_t flags = 0;
27 if (strlen(section->name) >= FMAP_STRLEN) {
28 ERROR("Section name ('%s') exceeds %d character FMAP format limit\n",
29 section->name, FMAP_STRLEN - 1);
30 return false;
33 absolute_watermark += section->offset;
35 if (section->flags.f.preserve)
36 flags |= FMAP_AREA_PRESERVE;
38 if (fmap_append_area(flashmap, absolute_watermark, section->size,
39 (uint8_t *)section->name, flags) < 0) {
40 ERROR("Failed to insert section '%s' into FMAP\n",
41 section->name);
42 return false;
45 fmd_foreach_child(subsection, section) {
46 if (!fmap_append_fmd_node(flashmap, subsection,
47 absolute_watermark))
48 return false;
51 return true;
54 struct fmap *fmap_from_fmd(const struct flashmap_descriptor *desc)
56 assert(desc);
57 assert(desc->size_known);
59 if (strlen(desc->name) >= FMAP_STRLEN) {
60 ERROR("Image name ('%s') exceeds %d character FMAP header limit\n",
61 desc->name, FMAP_STRLEN - 1);
62 return NULL;
65 struct fmap *fmap = fmap_create(desc->offset_known ? desc->offset : 0,
66 desc->size, (uint8_t *)desc->name);
67 if (!fmap) {
68 ERROR("Failed to allocate FMAP header\n");
69 return fmap;
72 fmd_foreach_child(real_section, desc) {
73 if (!fmap_append_fmd_node(&fmap, real_section, 0)) {
74 fmap_destroy(fmap);
75 return NULL;
79 return fmap;