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 (ufdt_node_tag(node) == FDT_BEGIN_NODE)                            \
30     for ((it) = &(((struct ufdt_node_fdt_node *)(node))->child); *(it); \
31          (it) = &((*(it))->sibling))
32 
33 #define for_each_prop(it, node) \
34   for_each_child(it, node) if (ufdt_node_tag(*(it)) == FDT_PROP)
35 
36 #define for_each_node(it, node) \
37   for_each_child(it, node) if (ufdt_node_tag(*(it)) == FDT_BEGIN_NODE)
38 
39 /*
40  * Gets prop name from FDT requires complicated manipulation.
41  * To avoid the manipulation, the *name pointer in ufdt_node_fdt_prop
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 ufdt_node_name(node)                                             \
46   ((ufdt_node_tag(node) == FDT_BEGIN_NODE)                               \
47        ? (((const struct fdt_node_header *)((node)->fdt_tag_ptr))->name) \
48        : (((const struct ufdt_node_fdt_prop *)(node))->name))
49 
50 #define ufdt_node_tag(node) \
51   ((node) ? fdt32_to_cpu(*(node)->fdt_tag_ptr) : FDT_END)
52 
53 struct ufdt_node {
54   fdt32_t *fdt_tag_ptr;
55   struct ufdt_node *sibling;
56 };
57 
58 struct ufdt_node_fdt_prop {
59   struct ufdt_node parent;
60   const char *name;
61 };
62 
63 struct ufdt_node_fdt_node {
64   struct ufdt_node parent;
65   struct ufdt_node *child;
66   struct ufdt_node **last_child_p;
67 };
68 
69 struct ufdt_phandle_table_entry {
70   uint32_t phandle;
71   struct ufdt_node *node;
72 };
73 
74 struct ufdt_static_phandle_table {
75   int len;
76   struct ufdt_phandle_table_entry *data;
77 };
78 
79 struct ufdt {
80   void **fdtps;
81   int mem_size_fdtps;
82   int num_used_fdtps;
83 
84   struct ufdt_node *root;
85   struct ufdt_static_phandle_table phandle_table;
86 };
87 
88 struct ufdt_node_pool;
89 
90 #endif /* UFDT_TYPES_H */
91