blob: cdd1ab52c18bb976b621eea03e1bc79069962199 [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
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 GNU
13 * General Public License for more details.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
20
21#include "dtc.h"
22
23/*
24 * Tree building functions
25 */
26
David Gibson4102d842005-06-16 14:36:37 +100027struct property *build_property(char *name, struct data val, char *label)
David Gibsonfc14dad2005-06-08 17:18:34 +100028{
29 struct property *new = xmalloc(sizeof(*new));
30
31 new->name = name;
32 new->val = val;
33
34 new->next = NULL;
35
David Gibson4102d842005-06-16 14:36:37 +100036 new->label = label;
37
David Gibsonfc14dad2005-06-08 17:18:34 +100038 return new;
39}
40
David Gibsonfc14dad2005-06-08 17:18:34 +100041struct property *chain_property(struct property *first, struct property *list)
42{
43 assert(first->next == NULL);
David Gibson63dc9c72007-09-18 11:44:04 +100044
David Gibsonfc14dad2005-06-08 17:18:34 +100045 first->next = list;
David Gibson63dc9c72007-09-18 11:44:04 +100046 return first;
David Gibsonfc14dad2005-06-08 17:18:34 +100047}
48
Jon Loeliger7b3fb782007-10-22 16:09:56 -050049struct property *reverse_properties(struct property *first)
50{
51 struct property *p = first;
52 struct property *head = NULL;
53 struct property *next;
54
55 while (p) {
56 next = p->next;
57 p->next = head;
58 head = p;
59 p = next;
60 }
61 return head;
62}
63
David Gibsonfc14dad2005-06-08 17:18:34 +100064struct node *build_node(struct property *proplist, struct node *children)
65{
66 struct node *new = xmalloc(sizeof(*new));
67 struct node *child;
68
69 memset(new, 0, sizeof(*new));
70
Jon Loeliger7b3fb782007-10-22 16:09:56 -050071 new->proplist = reverse_properties(proplist);
David Gibsonfc14dad2005-06-08 17:18:34 +100072 new->children = children;
73
74 for_each_child(new, child) {
75 child->parent = new;
76 }
77
78 return new;
79}
80
David Gibson4102d842005-06-16 14:36:37 +100081struct node *name_node(struct node *node, char *name, char * label)
David Gibsonfc14dad2005-06-08 17:18:34 +100082{
83 assert(node->name == NULL);
84
85 node->name = name;
David Gibson4102d842005-06-16 14:36:37 +100086
87 node->label = label;
88
David Gibsonfc14dad2005-06-08 17:18:34 +100089 return node;
90}
91
92struct node *chain_node(struct node *first, struct node *list)
93{
94 assert(first->next_sibling == NULL);
95
96 first->next_sibling = list;
97 return first;
98}
99
100void add_property(struct node *node, struct property *prop)
101{
David Gibson740a19a2005-10-21 17:26:45 +1000102 struct property **p;
103
104 prop->next = NULL;
105
106 p = &node->proplist;
107 while (*p)
108 p = &((*p)->next);
109
110 *p = prop;
David Gibsonfc14dad2005-06-08 17:18:34 +1000111}
112
113void add_child(struct node *parent, struct node *child)
114{
David Gibson740a19a2005-10-21 17:26:45 +1000115 struct node **p;
116
117 child->next_sibling = NULL;
118
119 p = &parent->children;
120 while (*p)
121 p = &((*p)->next_sibling);
122
123 *p = child;
David Gibsonfc14dad2005-06-08 17:18:34 +1000124}
125
David Gibsonf040d952005-10-24 18:18:38 +1000126struct reserve_info *build_reserve_entry(u64 address, u64 size, char *label)
127{
128 struct reserve_info *new = xmalloc(sizeof(*new));
129
130 new->re.address = address;
131 new->re.size = size;
132
133 new->next = NULL;
134
135 new->label = label;
136
137 return new;
138}
139
140struct reserve_info *chain_reserve_entry(struct reserve_info *first,
141 struct reserve_info *list)
142{
143 assert(first->next == NULL);
144
145 first->next = list;
146 return first;
147}
148
149struct reserve_info *add_reserve_entry(struct reserve_info *list,
150 struct reserve_info *new)
151{
152 struct reserve_info *last;
153
154 new->next = NULL;
155
156 if (! list)
157 return new;
158
159 for (last = list; last->next; last = last->next)
160 ;
161
162 last->next = new;
163
164 return list;
165}
David Gibsonfc14dad2005-06-08 17:18:34 +1000166
David Gibson169f0b12007-10-18 17:22:30 +1000167struct boot_info *build_boot_info(struct reserve_info *reservelist,
168 struct node *tree)
169{
170 struct boot_info *bi;
171
172 bi = xmalloc(sizeof(*bi));
173 bi->reservelist = reservelist;
174 bi->dt = tree;
175
176 return bi;
177}
178
David Gibsonfc14dad2005-06-08 17:18:34 +1000179/*
180 * Tree accessor functions
181 */
182
David Gibson2f1ccc32007-11-01 16:49:26 +1100183char *get_unitname(struct node *node)
David Gibsonfc14dad2005-06-08 17:18:34 +1000184{
185 if (node->name[node->basenamelen] == '\0')
186 return "";
187 else
188 return node->name + node->basenamelen + 1;
189}
190
David Gibson2f1ccc32007-11-01 16:49:26 +1100191struct property *get_property(struct node *node, char *propname)
David Gibsonfc14dad2005-06-08 17:18:34 +1000192{
193 struct property *prop;
194
195 for_each_property(node, prop)
196 if (streq(prop->name, propname))
197 return prop;
198
199 return NULL;
200}
201
David Gibson2f1ccc32007-11-01 16:49:26 +1100202cell_t propval_cell(struct property *prop)
David Gibsonfc14dad2005-06-08 17:18:34 +1000203{
204 assert(prop->val.len == sizeof(cell_t));
205 return be32_to_cpu(*((cell_t *)prop->val.val));
206}
207
David Gibson2f1ccc32007-11-01 16:49:26 +1100208struct node *get_subnode(struct node *node, char *nodename)
David Gibsonfc14dad2005-06-08 17:18:34 +1000209{
210 struct node *child;
211
David Gibson81f2e892005-06-16 17:04:00 +1000212 for_each_child(node, child)
213 if (streq(child->name, nodename))
214 return child;
215
216 return NULL;
217}
218
219static struct node *get_node_by_path(struct node *tree, char *path)
220{
221 char *p;
222 struct node *child;
223
224 if (!path || ! (*path))
225 return tree;
226
227 while (path[0] == '/')
228 path++;
229
230 p = strchr(path, '/');
231
232 for_each_child(tree, child) {
233 if (p && strneq(path, child->name, p-path))
234 return get_node_by_path(child, p+1);
235 else if (!p && streq(path, child->name))
David Gibsonfc14dad2005-06-08 17:18:34 +1000236 return child;
237 }
238
239 return NULL;
240}
241
David Gibsonc226ddc2007-02-07 14:29:07 +1100242static struct node *get_node_by_label(struct node *tree, const char *label)
243{
244 struct node *child, *node;
245
246 assert(label && (strlen(label) > 0));
247
248 if (tree->label && streq(tree->label, label))
249 return tree;
250
251 for_each_child(tree, child) {
252 node = get_node_by_label(child, label);
253 if (node)
254 return node;
255 }
256
257 return NULL;
258}
259
David Gibson2f1ccc32007-11-01 16:49:26 +1100260struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
David Gibsonfc14dad2005-06-08 17:18:34 +1000261{
David Gibson63dc9c72007-09-18 11:44:04 +1000262 struct node *child, *node;
David Gibsonfc14dad2005-06-08 17:18:34 +1000263
264 assert((phandle != 0) && (phandle != -1));
265
266 if (tree->phandle == phandle)
267 return tree;
268
269 for_each_child(tree, child) {
270 node = get_node_by_phandle(child, phandle);
271 if (node)
272 return node;
273 }
274
275 return NULL;
276}
David Gibsonc226ddc2007-02-07 14:29:07 +1100277
David Gibson169f0b12007-10-18 17:22:30 +1000278static cell_t get_node_phandle(struct node *root, struct node *node)
279{
280 static cell_t phandle = 1; /* FIXME: ick, static local */
281
282 if ((node->phandle != 0) && (node->phandle != -1))
283 return node->phandle;
284
285 assert(! get_property(node, "linux,phandle"));
286
287 while (get_node_by_phandle(root, phandle))
288 phandle++;
289
290 node->phandle = phandle;
291 add_property(node,
292 build_property("linux,phandle",
293 data_append_cell(empty_data, phandle),
294 NULL));
295
296 return node->phandle;
297}
298
David Gibsonfc14dad2005-06-08 17:18:34 +1000299/*
David Gibson169f0b12007-10-18 17:22:30 +1000300 * Reference fixup functions
301 */
302
303static void apply_fixup(struct node *root, struct property *prop,
304 struct fixup *f)
305{
306 struct node *refnode;
307 cell_t phandle;
308
309 if (f->ref[0] == '/') {
310 /* Reference to full path */
311 refnode = get_node_by_path(root, f->ref);
312 if (! refnode)
313 die("Reference to non-existent node \"%s\"\n", f->ref);
314 } else {
315 refnode = get_node_by_label(root, f->ref);
316 if (! refnode)
317 die("Reference to non-existent node label \"%s\"\n", f->ref);
318 }
319
320 phandle = get_node_phandle(root, refnode);
321
322 assert(f->offset + sizeof(cell_t) <= prop->val.len);
323
324 *((cell_t *)(prop->val.val + f->offset)) = cpu_to_be32(phandle);
325}
326
327static void fixup_phandles(struct node *root, struct node *node)
328{
329 struct property *prop;
330 struct node *child;
331
332 for_each_property(node, prop) {
333 struct fixup *f = prop->val.refs;
334
335 while (f) {
336 apply_fixup(root, prop, f);
337 prop->val.refs = f->next;
338 fixup_free(f);
339 f = prop->val.refs;
340 }
341 }
342
343 for_each_child(node, child)
344 fixup_phandles(root, child);
345}
346
347void fixup_references(struct node *dt)
348{
349 fixup_phandles(dt, dt);
350}