1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef UFDT_TYPES_H
18 #define UFDT_TYPES_H
19 
20 #include <libfdt.h>
21 
22 /* it has type : struct ufdt_node** */
23 #define for_each(it, node)                                                 \
24   if ((node) != NULL)                                                      \
25     for (it = (node)->nodes; it != (node)->nodes + (node)->mem_size; ++it) \
26       if (*it)
27 
28 #define for_each_child(it, node)                                    \
29   if (tag_of(node) == FDT_BEGIN_NODE)                               \
30     for ((it) = &(((struct fdt_node_ufdt_node *)node)->child); *it; \
31          it = &((*it)->sibling))
32 
33 #define for_each_prop(it, node) \
34   for_each_child(it, node) if (tag_of(*it) == FDT_PROP)
35 
36 #define for_each_node(it, node) \
37   for_each_child(it, node) if (tag_of(*it) == FDT_BEGIN_NODE)
38 
39 /*
40  * Gets prop name from FDT requires complicated manipulation.
41  * To avoid the manipulation, the *name pointer in fdt_prop_ufdt_node
42  * is pointed to the final destination of the prop name in FDT.
43  * For the FDT_BEGIN_NODE name, it can be obtained from FDT directly.
44  */
45 #define name_of(node)                                                    \
46   ((tag_of(node) == FDT_BEGIN_NODE)                                      \
47        ? (((const struct fdt_node_header *)((node)->fdt_tag_ptr))->name) \
48        : (((const struct fdt_prop_ufdt_node *)node)->name))
49 
50 struct ufdt_node {
51   fdt32_t *fdt_tag_ptr;
52   struct ufdt_node *sibling;
53 };
54 
55 struct fdt_prop_ufdt_node {
56   struct ufdt_node parent;
57   const char *name;
58 };
59 
60 struct fdt_node_ufdt_node {
61   struct ufdt_node parent;
62   struct ufdt_node *child;
63   struct ufdt_node **last_child_p;
64 };
65 
66 struct phandle_table_entry {
67   uint32_t phandle;
68   struct ufdt_node *node;
69 };
70 
71 struct static_phandle_table {
72   int len;
73   struct phandle_table_entry *data;
74 };
75 
76 struct ufdt {
77   void **fdtps;
78   int mem_size_fdtps;
79   int num_used_fdtps;
80 
81   struct ufdt_node *root;
82   struct static_phandle_table phandle_table;
83 };
84 
85 typedef void func_on_ufdt_node(struct ufdt_node *, void *);
86 
87 struct ufdt_node_closure {
88   func_on_ufdt_node *func;
89   void *env;
90 };
91 
tag_of(const struct ufdt_node * node)92 static uint32_t tag_of(const struct ufdt_node *node) {
93   if (!node) return FDT_END;
94   return fdt32_to_cpu(*node->fdt_tag_ptr);
95 }
96 
97 #endif /* UFDT_TYPES_H */
98