blob: 051914d94b479c257ae615757526226b43e27231 [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* fmap_from_fmd.c, tool to distill flashmap descriptors into raw FMAP sections */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Sol Boucher69b88bf2015-02-26 11:47:19 -08003
4#include "fmap_from_fmd.h"
5
6#include "common.h"
7
8#include <assert.h>
9#include <string.h>
10
11static bool fmap_append_fmd_node(struct fmap **flashmap,
12 const struct flashmap_descriptor *section,
13 unsigned absolute_watermark) {
Hung-Te Lin49a44502019-03-04 15:41:09 +080014 uint16_t flags = 0;
Sol Boucher69b88bf2015-02-26 11:47:19 -080015 if (strlen(section->name) >= FMAP_STRLEN) {
Sol Boucherb9740812015-03-18 10:13:48 -070016 ERROR("Section name ('%s') exceeds %d character FMAP format limit\n",
17 section->name, FMAP_STRLEN - 1);
Sol Boucher69b88bf2015-02-26 11:47:19 -080018 return false;
19 }
20
21 absolute_watermark += section->offset;
22
Hung-Te Lin49a44502019-03-04 15:41:09 +080023 if (section->flags.f.preserve)
24 flags |= FMAP_AREA_PRESERVE;
25
Sol Boucher69b88bf2015-02-26 11:47:19 -080026 if (fmap_append_area(flashmap, absolute_watermark, section->size,
Hung-Te Lin49a44502019-03-04 15:41:09 +080027 (uint8_t *)section->name, flags) < 0) {
Sol Boucherb9740812015-03-18 10:13:48 -070028 ERROR("Failed to insert section '%s' into FMAP\n",
29 section->name);
Sol Boucher69b88bf2015-02-26 11:47:19 -080030 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
42struct 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 Boucherb9740812015-03-18 10:13:48 -070048 ERROR("Image name ('%s') exceeds %d character FMAP header limit\n",
Sol Boucher69b88bf2015-02-26 11:47:19 -080049 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 Boucherb9740812015-03-18 10:13:48 -070056 ERROR("Failed to allocate FMAP header\n");
Sol Boucher69b88bf2015-02-26 11:47:19 -080057 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}