blob: 309c674e6479a378bac6abec8a6c5ff86c1c0eb1 [file] [log] [blame]
SzuWei Lin6ad4caa2017-04-06 17:12:27 +08001/*
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
Li Chenf6c209b2016-11-28 12:15:33 +080017#include "libufdt.h"
Li Chenf6c209b2016-11-28 12:15:33 +080018
SzuWei Lind62a8492017-04-24 10:17:15 +080019#include "ufdt_node_pool.h"
20
21struct 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);
Li Chenf6c209b2016-11-28 12:15:33 +080024 uint32_t tag = fdt32_to_cpu(*fdt_tag_ptr);
25 if (tag == FDT_PROP) {
SzuWei Lin1be68ae2017-03-30 11:44:54 +080026 const struct fdt_property *prop = (const struct fdt_property *)fdt_tag_ptr;
SzuWei Lind62a8492017-04-24 10:17:15 +080027 struct ufdt_node_fdt_prop *res = (struct ufdt_node_fdt_prop *)buf;
Li Chenf6c209b2016-11-28 12:15:33 +080028 if (res == NULL) return NULL;
29 res->parent.fdt_tag_ptr = fdt_tag_ptr;
30 res->parent.sibling = NULL;
SzuWei Lin1be68ae2017-03-30 11:44:54 +080031 res->name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
Li Chenf6c209b2016-11-28 12:15:33 +080032 return (struct ufdt_node *)res;
33 } else {
SzuWei Lind62a8492017-04-24 10:17:15 +080034 struct ufdt_node_fdt_node *res = (struct ufdt_node_fdt_node *)buf;
Li Chenf6c209b2016-11-28 12:15:33 +080035 if (res == NULL) return NULL;
36 res->parent.fdt_tag_ptr = fdt_tag_ptr;
37 res->parent.sibling = NULL;
38 res->child = NULL;
SzuWei Lin08dd7272017-02-20 10:38:20 +080039 res->last_child_p = &res->child;
Li Chenf6c209b2016-11-28 12:15:33 +080040 return (struct ufdt_node *)res;
41 }
42}
43
SzuWei Lind62a8492017-04-24 10:17:15 +080044void ufdt_node_destruct(struct ufdt_node *node, struct ufdt_node_pool *pool) {
Li Chenf6c209b2016-11-28 12:15:33 +080045 if (node == NULL) return;
46
SzuWei Lin8a7039c2017-04-14 15:38:15 +080047 if (ufdt_node_tag(node) == FDT_BEGIN_NODE) {
SzuWei Lin3f56fa72017-06-09 14:45:17 +080048 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 }
Li Chenf6c209b2016-11-28 12:15:33 +080054 }
55
SzuWei Lind62a8492017-04-24 10:17:15 +080056 ufdt_node_pool_free(pool, node);
Li Chenf6c209b2016-11-28 12:15:33 +080057}
58
59int ufdt_node_add_child(struct ufdt_node *parent, struct ufdt_node *child) {
60 if (!parent || !child) return -1;
SzuWei Lin8a7039c2017-04-14 15:38:15 +080061 if (ufdt_node_tag(parent) != FDT_BEGIN_NODE) return -1;
Li Chenf6c209b2016-11-28 12:15:33 +080062
63 int err = 0;
SzuWei Lin8a7039c2017-04-14 15:38:15 +080064 uint32_t child_tag = ufdt_node_tag(child);
Li Chenf6c209b2016-11-28 12:15:33 +080065 switch (child_tag) {
66 case FDT_PROP:
67 case FDT_BEGIN_NODE:
SzuWei Lin08dd7272017-02-20 10:38:20 +080068 // Append the child node to the last child of parant node
SzuWei Lin8a7039c2017-04-14 15:38:15 +080069 *((struct ufdt_node_fdt_node *)parent)->last_child_p = child;
70 ((struct ufdt_node_fdt_node *)parent)->last_child_p = &child->sibling;
Li Chenf6c209b2016-11-28 12:15:33 +080071 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
85struct 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) {
SzuWei Lin8a7039c2017-04-14 15:38:15 +080089 if (ufdt_node_name_eq(*it, name, len)) return *it;
Li Chenf6c209b2016-11-28 12:15:33 +080090 }
91 return NULL;
92}
93
94struct 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
99struct 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) {
SzuWei Lin8a7039c2017-04-14 15:38:15 +0800105 if (ufdt_node_name_eq(*it, name, len)) return *it;
Li Chenf6c209b2016-11-28 12:15:33 +0800106 }
107 return NULL;
108}
109
110struct 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
115char *ufdt_node_get_fdt_prop_data(const struct ufdt_node *node, int *out_len) {
SzuWei Lin8a7039c2017-04-14 15:38:15 +0800116 if (!node || ufdt_node_tag(node) != FDT_PROP) {
Li Chenf6c209b2016-11-28 12:15:33 +0800117 return NULL;
118 }
119 const struct fdt_property *prop = (struct fdt_property *)node->fdt_tag_ptr;
120 if (out_len != NULL) {
121 *out_len = fdt32_to_cpu(prop->len);
122 }
123 return (char *)prop->data;
124}
125
126char *ufdt_node_get_fdt_prop_data_by_name_len(const struct ufdt_node *node,
127 const char *name, int len,
128 int *out_len) {
129 return ufdt_node_get_fdt_prop_data(
130 ufdt_node_get_property_by_name_len(node, name, len), out_len);
131}
132
133char *ufdt_node_get_fdt_prop_data_by_name(const struct ufdt_node *node,
134 const char *name, int *out_len) {
135 return ufdt_node_get_fdt_prop_data(ufdt_node_get_property_by_name(node, name),
136 out_len);
137}
138
139/*
140 * END of FDT_PROP related methods.
141 */
142
143/*
144 * BEGIN of searching-in-ufdt_node methods.
145 */
146
147uint32_t ufdt_node_get_phandle(const struct ufdt_node *node) {
SzuWei Lin8a7039c2017-04-14 15:38:15 +0800148 if (!node || ufdt_node_tag(node) != FDT_BEGIN_NODE) {
Li Chenf6c209b2016-11-28 12:15:33 +0800149 return 0;
150 }
151 int len = 0;
152 void *ptr = ufdt_node_get_fdt_prop_data_by_name(node, "phandle", &len);
153 if (!ptr || len != sizeof(fdt32_t)) {
154 ptr = ufdt_node_get_fdt_prop_data_by_name(node, "linux,phandle", &len);
155 if (!ptr || len != sizeof(fdt32_t)) {
156 return 0;
157 }
158 }
159 return fdt32_to_cpu(*((fdt32_t *)ptr));
160}
161
162struct ufdt_node *ufdt_node_get_node_by_path_len(const struct ufdt_node *node,
163 const char *path, int len) {
164 const char *end = path + len;
165
166 struct ufdt_node *cur = (struct ufdt_node *)node;
167
168 while (path < end) {
169 while (path[0] == '/') path++;
170 if (path == end) return cur;
171
172 const char *next_slash;
173 next_slash = dto_memchr(path, '/', end - path);
174 if (!next_slash) next_slash = end;
175
176 struct ufdt_node *next = NULL;
177
178 next = ufdt_node_get_subnode_by_name_len(cur, path, next_slash - path);
179
180 cur = next;
181 path = next_slash;
182 if (!cur) return cur;
183 }
184
185 return cur;
186}
187
188struct ufdt_node *ufdt_node_get_node_by_path(const struct ufdt_node *node,
189 const char *path) {
190 return ufdt_node_get_node_by_path_len(node, path, dto_strlen(path));
191}
192
SzuWei Line78aa562017-04-17 10:24:45 +0800193bool ufdt_node_name_eq(const struct ufdt_node *node, const char *name, int len) {
194 if (!node) return false;
195 if (!name) return false;
196 if (dto_strncmp(ufdt_node_name(node), name, len) != 0) return false;
197 if (ufdt_node_name(node)[len] != '\0') return false;
198 return true;
199}
200
Li Chenf6c209b2016-11-28 12:15:33 +0800201/*
202 * END of searching-in-ufdt_node methods.
203 */
204
SzuWei Lind62a8492017-04-24 10:17:15 +0800205static int merge_children(struct ufdt_node *node_a, struct ufdt_node *node_b,
206 struct ufdt_node_pool *pool) {
SzuWei Line78aa562017-04-17 10:24:45 +0800207 int err = 0;
208 struct ufdt_node *it;
209 for (it = ((struct ufdt_node_fdt_node *)node_b)->child; it;) {
210 struct ufdt_node *cur_node = it;
211 it = it->sibling;
212 cur_node->sibling = NULL;
213 struct ufdt_node *target_node = NULL;
214 if (ufdt_node_tag(cur_node) == FDT_BEGIN_NODE) {
215 target_node =
216 ufdt_node_get_subnode_by_name(node_a, ufdt_node_name(cur_node));
217 } else {
218 target_node =
219 ufdt_node_get_property_by_name(node_a, ufdt_node_name(cur_node));
220 }
221 if (target_node == NULL) {
222 err = ufdt_node_add_child(node_a, cur_node);
223 } else {
SzuWei Lind62a8492017-04-24 10:17:15 +0800224 err = ufdt_node_merge_into(target_node, cur_node, pool);
225 ufdt_node_pool_free(pool, cur_node);
SzuWei Line78aa562017-04-17 10:24:45 +0800226 }
227 if (err < 0) return -1;
228 }
229 /*
230 * The ufdt_node* in node_b will be copied to node_a.
231 * To prevent the ufdt_node from being freed twice
232 * (main_tree and overlay_tree) at the end of function
233 * ufdt_apply_overlay(), set this node in node_b
234 * (overlay_tree) to NULL.
235 */
236 ((struct ufdt_node_fdt_node *)node_b)->child = NULL;
237
238 return 0;
239}
240
SzuWei Lind62a8492017-04-24 10:17:15 +0800241int ufdt_node_merge_into(struct ufdt_node *node_a, struct ufdt_node *node_b,
242 struct ufdt_node_pool *pool) {
SzuWei Line78aa562017-04-17 10:24:45 +0800243 if (ufdt_node_tag(node_a) == FDT_PROP) {
244 node_a->fdt_tag_ptr = node_b->fdt_tag_ptr;
245 return 0;
246 }
247
248 int err = 0;
SzuWei Lind62a8492017-04-24 10:17:15 +0800249 err = merge_children(node_a, node_b, pool);
SzuWei Line78aa562017-04-17 10:24:45 +0800250 if (err < 0) return -1;
251
252 return 0;
253}
254
Li Chenf6c209b2016-11-28 12:15:33 +0800255#define TAB_SIZE 2
256
257void ufdt_node_print(const struct ufdt_node *node, int depth) {
258 if (!node) return;
259
260 int i;
261 for (i = 0; i < depth * TAB_SIZE; i++) dto_print(" ");
262
263 uint32_t tag;
SzuWei Lin8a7039c2017-04-14 15:38:15 +0800264 tag = ufdt_node_tag(node);
Li Chenf6c209b2016-11-28 12:15:33 +0800265
266 switch (tag) {
267 case FDT_BEGIN_NODE:
268 dto_print("NODE ");
269 break;
270 case FDT_PROP:
271 dto_print("PROP ");
272 break;
273 default:
274 dto_print("UNKNOWN ");
275 break;
276 }
277
SzuWei Lin8a7039c2017-04-14 15:38:15 +0800278 if (ufdt_node_name(node)) {
279 dto_print(":%s:\n", ufdt_node_name(node));
Li Chenf6c209b2016-11-28 12:15:33 +0800280 } else {
281 dto_print("node name is NULL.\n");
282 }
283
SzuWei Lin8a7039c2017-04-14 15:38:15 +0800284 if (ufdt_node_tag(node) == FDT_BEGIN_NODE) {
Li Chenf6c209b2016-11-28 12:15:33 +0800285 struct ufdt_node **it;
286
287 for_each_prop(it, node) ufdt_node_print(*it, depth + 1);
288
289 for_each_node(it, node) ufdt_node_print(*it, depth + 1);
290 }
Li Chenf6c209b2016-11-28 12:15:33 +0800291}