• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "libufdt.h"
18 
19 #include "ufdt_node_pool.h"
20 
ufdt_node_construct(void * fdtp,fdt32_t * fdt_tag_ptr,struct ufdt_node_pool * pool)21 struct ufdt_node *ufdt_node_construct(void *fdtp, fdt32_t *fdt_tag_ptr,
22                                       struct ufdt_node_pool *pool) {
23   void *buf = ufdt_node_pool_alloc(pool);
24   uint32_t tag = fdt32_to_cpu(*fdt_tag_ptr);
25   if (tag == FDT_PROP) {
26     const struct fdt_property *prop = (const struct fdt_property *)fdt_tag_ptr;
27     struct ufdt_node_fdt_prop *res = (struct ufdt_node_fdt_prop *)buf;
28     if (res == NULL) return NULL;
29     res->parent.fdt_tag_ptr = fdt_tag_ptr;
30     res->parent.sibling = NULL;
31     res->name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
32     return (struct ufdt_node *)res;
33   } else {
34     struct ufdt_node_fdt_node *res = (struct ufdt_node_fdt_node *)buf;
35     if (res == NULL) return NULL;
36     res->parent.fdt_tag_ptr = fdt_tag_ptr;
37     res->parent.sibling = NULL;
38     res->child = NULL;
39     res->last_child_p = &res->child;
40     return (struct ufdt_node *)res;
41   }
42 }
43 
ufdt_node_destruct(struct ufdt_node * node,struct ufdt_node_pool * pool)44 void ufdt_node_destruct(struct ufdt_node *node, struct ufdt_node_pool *pool) {
45   if (node == NULL) return;
46 
47   if (ufdt_node_tag(node) == FDT_BEGIN_NODE) {
48     struct ufdt_node *it = ((struct ufdt_node_fdt_node *)node)->child;
49     while (it != NULL) {
50       struct ufdt_node *next = it->sibling;
51       ufdt_node_destruct(it, pool);
52       it = next;
53     }
54   }
55 
56   ufdt_node_pool_free(pool, node);
57 }
58 
ufdt_node_add_child(struct ufdt_node * parent,struct ufdt_node * child)59 int ufdt_node_add_child(struct ufdt_node *parent, struct ufdt_node *child) {
60   if (!parent || !child) return -1;
61   if (ufdt_node_tag(parent) != FDT_BEGIN_NODE) return -1;
62 
63   int err = 0;
64   uint32_t child_tag = ufdt_node_tag(child);
65   switch (child_tag) {
66     case FDT_PROP:
67     case FDT_BEGIN_NODE:
68       // Append the child node to the last child of parant node
69       *((struct ufdt_node_fdt_node *)parent)->last_child_p = child;
70       ((struct ufdt_node_fdt_node *)parent)->last_child_p = &child->sibling;
71       break;
72 
73     default:
74       err = -1;
75       dto_error("invalid children tag type\n");
76   }
77 
78   return err;
79 }
80 
81 /*
82  * BEGIN of FDT_PROP related methods.
83  */
84 
ufdt_node_get_subnode_by_name_len(const struct ufdt_node * node,const char * name,int len)85 struct ufdt_node *ufdt_node_get_subnode_by_name_len(const struct ufdt_node *node,
86                                                   const char *name, int len) {
87   struct ufdt_node **it = NULL;
88   for_each_node(it, node) {
89     if (ufdt_node_name_eq(*it, name, len)) return *it;
90   }
91   return NULL;
92 }
93 
ufdt_node_get_subnode_by_name(const struct ufdt_node * node,const char * name)94 struct ufdt_node *ufdt_node_get_subnode_by_name(const struct ufdt_node *node,
95                                               const char *name) {
96   return ufdt_node_get_subnode_by_name_len(node, name, strlen(name));
97 }
98 
ufdt_node_get_property_by_name_len(const struct ufdt_node * node,const char * name,int len)99 struct ufdt_node *ufdt_node_get_property_by_name_len(
100     const struct ufdt_node *node, const char *name, int len) {
101   if (!node) return NULL;
102 
103   struct ufdt_node **it = NULL;
104   for_each_prop(it, node) {
105     if (ufdt_node_name_eq(*it, name, len)) return *it;
106   }
107   return NULL;
108 }
109 
ufdt_node_get_property_by_name(const struct ufdt_node * node,const char * name)110 struct ufdt_node *ufdt_node_get_property_by_name(const struct ufdt_node *node,
111                                                  const char *name) {
112   return ufdt_node_get_property_by_name_len(node, name, dto_strlen(name));
113 }
114 
ufdt_node_get_fdt_prop_data(const struct ufdt_node * node,int * out_len)115 char *ufdt_node_get_fdt_prop_data(const struct ufdt_node *node, int *out_len) {
116   if (!node || ufdt_node_tag(node) != FDT_PROP) {
117     return NULL;
118   }
119   const struct fdt_property *prop = (struct fdt_property *)node->fdt_tag_ptr;
120   if (out_len != NULL) {
121     uint32_t prop_len = fdt32_to_cpu(prop->len);
122 
123     if (prop_len > INT_MAX) {
124       return NULL;
125     }
126 
127     *out_len = prop_len;
128   }
129   return (char *)prop->data;
130 }
131 
ufdt_node_get_fdt_prop_data_by_name_len(const struct ufdt_node * node,const char * name,int len,int * out_len)132 char *ufdt_node_get_fdt_prop_data_by_name_len(const struct ufdt_node *node,
133                                               const char *name, int len,
134                                               int *out_len) {
135   return ufdt_node_get_fdt_prop_data(
136       ufdt_node_get_property_by_name_len(node, name, len), out_len);
137 }
138 
ufdt_node_get_fdt_prop_data_by_name(const struct ufdt_node * node,const char * name,int * out_len)139 char *ufdt_node_get_fdt_prop_data_by_name(const struct ufdt_node *node,
140                                           const char *name, int *out_len) {
141   return ufdt_node_get_fdt_prop_data(ufdt_node_get_property_by_name(node, name),
142                                      out_len);
143 }
144 
145 /*
146  * END of FDT_PROP related methods.
147  */
148 
149 /*
150  * BEGIN of searching-in-ufdt_node methods.
151  */
152 
ufdt_node_get_phandle(const struct ufdt_node * node)153 uint32_t ufdt_node_get_phandle(const struct ufdt_node *node) {
154   if (!node || ufdt_node_tag(node) != FDT_BEGIN_NODE) {
155     return 0;
156   }
157   int len = 0;
158   void *ptr = ufdt_node_get_fdt_prop_data_by_name(node, "phandle", &len);
159   if (!ptr || len != sizeof(fdt32_t)) {
160     ptr = ufdt_node_get_fdt_prop_data_by_name(node, "linux,phandle", &len);
161     if (!ptr || len != sizeof(fdt32_t)) {
162       return 0;
163     }
164   }
165   return fdt32_to_cpu(*((fdt32_t *)ptr));
166 }
167 
ufdt_node_get_node_by_path_len(const struct ufdt_node * node,const char * path,int len)168 struct ufdt_node *ufdt_node_get_node_by_path_len(const struct ufdt_node *node,
169                                                  const char *path, int len) {
170   const char *end = path + len;
171 
172   struct ufdt_node *cur = (struct ufdt_node *)node;
173 
174   while (path < end) {
175     while (path[0] == '/') path++;
176     if (path == end) return cur;
177 
178     const char *next_slash;
179     next_slash = dto_memchr(path, '/', end - path);
180     if (!next_slash) next_slash = end;
181 
182     struct ufdt_node *next = NULL;
183 
184     next = ufdt_node_get_subnode_by_name_len(cur, path, next_slash - path);
185 
186     cur = next;
187     path = next_slash;
188     if (!cur) return cur;
189   }
190 
191   return cur;
192 }
193 
ufdt_node_get_node_by_path(const struct ufdt_node * node,const char * path)194 struct ufdt_node *ufdt_node_get_node_by_path(const struct ufdt_node *node,
195                                              const char *path) {
196   return ufdt_node_get_node_by_path_len(node, path, dto_strlen(path));
197 }
198 
ufdt_node_name_eq(const struct ufdt_node * node,const char * name,int len)199 bool ufdt_node_name_eq(const struct ufdt_node *node, const char *name, int len) {
200   if (!node) return false;
201   if (!name) return false;
202   if (dto_strncmp(ufdt_node_name(node), name, len) != 0) return false;
203   if (ufdt_node_name(node)[len] != '\0') return false;
204   return true;
205 }
206 
207 /*
208  * END of searching-in-ufdt_node methods.
209  */
210 
merge_children(struct ufdt_node * node_a,struct ufdt_node * node_b,struct ufdt_node_pool * pool)211 static int merge_children(struct ufdt_node *node_a, struct ufdt_node *node_b,
212                           struct ufdt_node_pool *pool) {
213   int err = 0;
214   struct ufdt_node *it;
215   for (it = ((struct ufdt_node_fdt_node *)node_b)->child; it;) {
216     struct ufdt_node *cur_node = it;
217     it = it->sibling;
218     cur_node->sibling = NULL;
219     struct ufdt_node *target_node = NULL;
220     if (ufdt_node_tag(cur_node) == FDT_BEGIN_NODE) {
221       target_node =
222           ufdt_node_get_subnode_by_name(node_a, ufdt_node_name(cur_node));
223     } else {
224       target_node =
225           ufdt_node_get_property_by_name(node_a, ufdt_node_name(cur_node));
226     }
227     if (target_node == NULL) {
228       err = ufdt_node_add_child(node_a, cur_node);
229     } else {
230       err = ufdt_node_merge_into(target_node, cur_node, pool);
231       ufdt_node_pool_free(pool, cur_node);
232     }
233     if (err < 0) return -1;
234   }
235   /*
236    * The ufdt_node* in node_b will be copied to node_a.
237    * To prevent the ufdt_node from being freed twice
238    * (main_tree and overlay_tree) at the end of function
239    * ufdt_apply_overlay(), set this node in node_b
240    * (overlay_tree) to NULL.
241    */
242   ((struct ufdt_node_fdt_node *)node_b)->child = NULL;
243 
244   return 0;
245 }
246 
ufdt_node_merge_into(struct ufdt_node * node_a,struct ufdt_node * node_b,struct ufdt_node_pool * pool)247 int ufdt_node_merge_into(struct ufdt_node *node_a, struct ufdt_node *node_b,
248                          struct ufdt_node_pool *pool) {
249   if (ufdt_node_tag(node_a) == FDT_PROP) {
250     node_a->fdt_tag_ptr = node_b->fdt_tag_ptr;
251     return 0;
252   }
253 
254   int err = 0;
255   err = merge_children(node_a, node_b, pool);
256   if (err < 0) return -1;
257 
258   return 0;
259 }
260 
261 #define TAB_SIZE 2
262 
ufdt_node_print(const struct ufdt_node * node,int depth)263 void ufdt_node_print(const struct ufdt_node *node, int depth) {
264   if (!node) return;
265 
266   int i;
267   for (i = 0; i < depth * TAB_SIZE; i++) dto_print(" ");
268 
269   uint32_t tag;
270   tag = ufdt_node_tag(node);
271 
272   switch (tag) {
273     case FDT_BEGIN_NODE:
274       dto_print("NODE ");
275       break;
276     case FDT_PROP:
277       dto_print("PROP ");
278       break;
279     default:
280       dto_print("UNKNOWN ");
281       break;
282   }
283 
284   if (ufdt_node_name(node)) {
285     dto_print(":%s:\n", ufdt_node_name(node));
286   } else {
287     dto_print("node name is NULL.\n");
288   }
289 
290   if (ufdt_node_tag(node) == FDT_BEGIN_NODE) {
291     struct ufdt_node **it;
292 
293     for_each_prop(it, node) ufdt_node_print(*it, depth + 1);
294 
295     for_each_node(it, node) ufdt_node_print(*it, depth + 1);
296   }
297 }
298