blob: a4131c36f53c3dfeea0001d80c8f88dd2cae0338 [file] [log] [blame]
Sol Boucher69b88bf2015-02-26 11:47:19 -08001/*
2 * fmd.h, parser frontend and utility functions for flashmap descriptor language
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.
Sol Boucher69b88bf2015-02-26 11:47:19 -080014 */
15
16#ifndef FMD_H_
17#define FMD_H_
18
19#include <limits.h>
20#include <stdbool.h>
21#include <stddef.h>
22#include <stdio.h>
23
24#define FMD_NOTFOUND UINT_MAX
25
26struct flashmap_descriptor {
27 char *name;
28 bool offset_known;
29 /**
30 * Offset relative to that of the parent node.
31 * Exception: for the root node in the descriptor tree, it is optional.
32 * In this case, if absent, it indicates that the flash chip will not be
33 * memory mapped at runtime; otherwise, its value indicates the base
34 * address of the flash chip in the virtual address space rather than
35 * representing an offset into the flash image itself.
36 * It is an error to read this field unless offset_known is set.
37 */
38 unsigned offset;
39 bool size_known;
40 /** It is an error to read this field unless size_known is set. */
41 unsigned size;
42 size_t list_len;
43 /** It is an error to dereference this array if list_len is 0. */
44 struct flashmap_descriptor **list;
45};
46
47/**
48 * **Client-defined** callback.
49 * This call is used to notify client code that the user has annotated the given
50 * section node by accompanying it with a string enclosed in parentheses. It is
51 * only invoked for nodes that have annotations, and then only once per node.
52 * The annotations' syntactic validity and semantic meaning are not determined
53 * by the compiler; rather, implementations of this function should use their
54 * return type to tell the compiler whether the annotation was valid syntax, as
55 * well as perform whatever actions are necessary given the particular
56 * annotation. It's worth reiterating that this is only called on section nodes,
57 * and will never be called with the final, complete flashmap_descriptor because
58 * it is impossible to annotate the image as a whole. Note that, although the
59 * node received by this function will be preserved in memory as part of the
60 * ultimate flashmap_descriptor, the annotation string will only persist during
61 * this function call: if the implementation needs it longer, it must copy it.
62 *
63 * @param flashmap_descriptor The section node carrying the annotation
64 * @param annotation What the user wrote (only valid during callback)
65 * @return Whether this annotation represented valid syntax
66 */
67bool fmd_process_annotation_impl(const struct flashmap_descriptor *node,
68 const char *annotation);
69
70/**
71 * Parse and validate a flashmap descriptor from the specified stream.
72 * As part of this process, any fields that were omitted in the input are
73 * inferred from whatever information is known, if possible. The result is a
74 * tree with all its offset and size fields filled, except possibly the former
75 * part of the root node in the case of non--memory mapped flash. If a syntax
76 * error causes the parser to fail, or if there is not enough information given
77 * in the input file to determine any single missing value, the specific error
78 * is reported to standard error and this function returns NULL.
79 *
80 * @param stream File from which to read the (partial) flashmap descriptor
81 * @return Populated flashmap descriptor tree, or NULL on failure
82 */
83struct flashmap_descriptor *fmd_create(FILE *stream);
84
85/** @param victim Valid descriptor tree to be cleaned up, or NULL for no-op */
86void fmd_cleanup(struct flashmap_descriptor *victim);
87
88/**
89 * @param tree Must be non-NULL
90 * @return The number of nodes in the tree, including the root
91 */
92size_t fmd_count_nodes(const struct flashmap_descriptor *tree);
93
94/**
95 * @param root The flashmap descriptor to search
96 * @param name The name of the sought-after section
97 * @return The desired section node, or NULL if none was found
98 */
99const struct flashmap_descriptor *fmd_find_node(
100 const struct flashmap_descriptor *root, const char *name);
101
102/**
103 * @param root Parent node to whose start the "absolute" offset will be relative
104 * @param name The name of the node whose offset to determine
105 * @return The "absolute" offset, or FMD_NOTFOUND if the node wasn't found
106 */
107unsigned fmd_calc_absolute_offset(const struct flashmap_descriptor *root,
108 const char *name);
109
110/** @param tree Must be non-NULL */
111void fmd_print(const struct flashmap_descriptor *tree);
112
113typedef struct flashmap_descriptor **flashmap_descriptor_iterator_t;
114
115/*
116 * Run the subsequent statement once on each descendant of the specified node.
117 *
118 * @param iterator A flashmap_descriptor_iterator_t (automatically declared)
119 * @param parent The parent node of those over which the loop should iterate
120 */
121#define fmd_foreach_child_iterator(iterator, parent) \
122 for (flashmap_descriptor_iterator_t iterator = parent->list; \
123 iterator < parent->list + parent->list_len; ++iterator)
124
125/*
126 * Run the subsequent statement once on each descendant of the specified node.
127 *
128 * @param child A struct flashmap_descriptor * (automatically declared)
129 * @param parent The parent node of those over which the loop should iterate
130 */
131#define fmd_foreach_child(child, parent) \
132 for (struct flashmap_descriptor **fmd_foreach_child_iterator_ = \
133 parent->list, *child = NULL; \
134 fmd_foreach_child_iterator_ < \
135 parent->list + parent->list_len && \
136 (child = *fmd_foreach_child_iterator_); \
137 ++fmd_foreach_child_iterator_)
138
139#endif