[POWERPC] bootwrapper: Make ft_get_phandle() accept and return NULL.
[linux-2.6] / arch / powerpc / boot / flatdevtree.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
15  *
16  * Copyright Pantelis Antoniou 2006
17  * Copyright (C) IBM Corporation 2006
18  *
19  * Authors: Pantelis Antoniou <pantelis@embeddedalley.com>
20  *          Hollis Blanchard <hollisb@us.ibm.com>
21  *          Mark A. Greer <mgreer@mvista.com>
22  *          Paul Mackerras <paulus@samba.org>
23  */
24
25 #include <string.h>
26 #include <stddef.h>
27 #include "flatdevtree.h"
28 #include "flatdevtree_env.h"
29
30 #define _ALIGN(x, al)   (((x) + (al) - 1) & ~((al) - 1))
31
32 static char *ft_root_node(struct ft_cxt *cxt)
33 {
34         return cxt->rgn[FT_STRUCT].start;
35 }
36
37 /* Routines for keeping node ptrs returned by ft_find_device current */
38 /* First entry not used b/c it would return 0 and be taken as NULL/error */
39 static void *ft_get_phandle(struct ft_cxt *cxt, char *node)
40 {
41         unsigned int i;
42
43         if (!node)
44                 return NULL;
45
46         for (i = 1; i < cxt->nodes_used; i++)   /* already there? */
47                 if (cxt->node_tbl[i] == node)
48                         return (void *)i;
49
50         if (cxt->nodes_used < cxt->node_max) {
51                 cxt->node_tbl[cxt->nodes_used] = node;
52                 return (void *)cxt->nodes_used++;
53         }
54
55         return NULL;
56 }
57
58 static char *ft_node_ph2node(struct ft_cxt *cxt, const void *phandle)
59 {
60         unsigned int i = (unsigned int)phandle;
61
62         if (i < cxt->nodes_used)
63                 return cxt->node_tbl[i];
64         return NULL;
65 }
66
67 static void ft_node_update_before(struct ft_cxt *cxt, char *addr, int shift)
68 {
69         unsigned int i;
70
71         if (shift == 0)
72                 return;
73
74         for (i = 1; i < cxt->nodes_used; i++)
75                 if (cxt->node_tbl[i] < addr)
76                         cxt->node_tbl[i] += shift;
77 }
78
79 static void ft_node_update_after(struct ft_cxt *cxt, char *addr, int shift)
80 {
81         unsigned int i;
82
83         if (shift == 0)
84                 return;
85
86         for (i = 1; i < cxt->nodes_used; i++)
87                 if (cxt->node_tbl[i] >= addr)
88                         cxt->node_tbl[i] += shift;
89 }
90
91 /* Struct used to return info from ft_next() */
92 struct ft_atom {
93         u32 tag;
94         const char *name;
95         void *data;
96         u32 size;
97 };
98
99 /* Set ptrs to current one's info; return addr of next one */
100 static char *ft_next(struct ft_cxt *cxt, char *p, struct ft_atom *ret)
101 {
102         u32 sz;
103
104         if (p >= cxt->rgn[FT_STRUCT].start + cxt->rgn[FT_STRUCT].size)
105                 return NULL;
106
107         ret->tag = be32_to_cpu(*(u32 *) p);
108         p += 4;
109
110         switch (ret->tag) {     /* Tag */
111         case OF_DT_BEGIN_NODE:
112                 ret->name = p;
113                 ret->data = (void *)(p - 4);    /* start of node */
114                 p += _ALIGN(strlen(p) + 1, 4);
115                 break;
116         case OF_DT_PROP:
117                 ret->size = sz = be32_to_cpu(*(u32 *) p);
118                 ret->name = cxt->str_anchor + be32_to_cpu(*(u32 *) (p + 4));
119                 ret->data = (void *)(p + 8);
120                 p += 8 + _ALIGN(sz, 4);
121                 break;
122         case OF_DT_END_NODE:
123         case OF_DT_NOP:
124                 break;
125         case OF_DT_END:
126         default:
127                 p = NULL;
128                 break;
129         }
130
131         return p;
132 }
133
134 #define HDR_SIZE        _ALIGN(sizeof(struct boot_param_header), 8)
135 #define EXPAND_INCR     1024    /* alloc this much extra when expanding */
136
137 /* See if the regions are in the standard order and non-overlapping */
138 static int ft_ordered(struct ft_cxt *cxt)
139 {
140         char *p = (char *)cxt->bph + HDR_SIZE;
141         enum ft_rgn_id r;
142
143         for (r = FT_RSVMAP; r <= FT_STRINGS; ++r) {
144                 if (p > cxt->rgn[r].start)
145                         return 0;
146                 p = cxt->rgn[r].start + cxt->rgn[r].size;
147         }
148         return p <= (char *)cxt->bph + cxt->max_size;
149 }
150
151 /* Copy the tree to a newly-allocated region and put things in order */
152 static int ft_reorder(struct ft_cxt *cxt, int nextra)
153 {
154         unsigned long tot;
155         enum ft_rgn_id r;
156         char *p, *pend;
157         int stroff;
158
159         tot = HDR_SIZE + EXPAND_INCR;
160         for (r = FT_RSVMAP; r <= FT_STRINGS; ++r)
161                 tot += cxt->rgn[r].size;
162         if (nextra > 0)
163                 tot += nextra;
164         tot = _ALIGN(tot, 8);
165
166         if (!cxt->realloc)
167                 return 0;
168         p = cxt->realloc(NULL, tot);
169         if (!p)
170                 return 0;
171
172         memcpy(p, cxt->bph, sizeof(struct boot_param_header));
173         /* offsets get fixed up later */
174
175         cxt->bph = (struct boot_param_header *)p;
176         cxt->max_size = tot;
177         pend = p + tot;
178         p += HDR_SIZE;
179
180         memcpy(p, cxt->rgn[FT_RSVMAP].start, cxt->rgn[FT_RSVMAP].size);
181         cxt->rgn[FT_RSVMAP].start = p;
182         p += cxt->rgn[FT_RSVMAP].size;
183
184         memcpy(p, cxt->rgn[FT_STRUCT].start, cxt->rgn[FT_STRUCT].size);
185         ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start,
186                         p - cxt->rgn[FT_STRUCT].start);
187         cxt->p += p - cxt->rgn[FT_STRUCT].start;
188         cxt->rgn[FT_STRUCT].start = p;
189
190         p = pend - cxt->rgn[FT_STRINGS].size;
191         memcpy(p, cxt->rgn[FT_STRINGS].start, cxt->rgn[FT_STRINGS].size);
192         stroff = cxt->str_anchor - cxt->rgn[FT_STRINGS].start;
193         cxt->rgn[FT_STRINGS].start = p;
194         cxt->str_anchor = p + stroff;
195
196         cxt->isordered = 1;
197         return 1;
198 }
199
200 static inline char *prev_end(struct ft_cxt *cxt, enum ft_rgn_id r)
201 {
202         if (r > FT_RSVMAP)
203                 return cxt->rgn[r - 1].start + cxt->rgn[r - 1].size;
204         return (char *)cxt->bph + HDR_SIZE;
205 }
206
207 static inline char *next_start(struct ft_cxt *cxt, enum ft_rgn_id r)
208 {
209         if (r < FT_STRINGS)
210                 return cxt->rgn[r + 1].start;
211         return (char *)cxt->bph + cxt->max_size;
212 }
213
214 /*
215  * See if we can expand region rgn by nextra bytes by using up
216  * free space after or before the region.
217  */
218 static int ft_shuffle(struct ft_cxt *cxt, char **pp, enum ft_rgn_id rgn,
219                 int nextra)
220 {
221         char *p = *pp;
222         char *rgn_start, *rgn_end;
223
224         rgn_start = cxt->rgn[rgn].start;
225         rgn_end = rgn_start + cxt->rgn[rgn].size;
226         if (nextra <= 0 || rgn_end + nextra <= next_start(cxt, rgn)) {
227                 /* move following stuff */
228                 if (p < rgn_end) {
229                         if (nextra < 0)
230                                 memmove(p, p - nextra, rgn_end - p + nextra);
231                         else
232                                 memmove(p + nextra, p, rgn_end - p);
233                         if (rgn == FT_STRUCT)
234                                 ft_node_update_after(cxt, p, nextra);
235                 }
236                 cxt->rgn[rgn].size += nextra;
237                 if (rgn == FT_STRINGS)
238                         /* assumes strings only added at beginning */
239                         cxt->str_anchor += nextra;
240                 return 1;
241         }
242         if (prev_end(cxt, rgn) <= rgn_start - nextra) {
243                 /* move preceding stuff */
244                 if (p > rgn_start) {
245                         memmove(rgn_start - nextra, rgn_start, p - rgn_start);
246                         if (rgn == FT_STRUCT)
247                                 ft_node_update_before(cxt, p, -nextra);
248                 }
249                 *p -= nextra;
250                 cxt->rgn[rgn].start -= nextra;
251                 cxt->rgn[rgn].size += nextra;
252                 return 1;
253         }
254         return 0;
255 }
256
257 static int ft_make_space(struct ft_cxt *cxt, char **pp, enum ft_rgn_id rgn,
258                          int nextra)
259 {
260         unsigned long size, ssize, tot;
261         char *str, *next;
262         enum ft_rgn_id r;
263
264         if (!cxt->isordered && !ft_reorder(cxt, nextra))
265                 return 0;
266         if (ft_shuffle(cxt, pp, rgn, nextra))
267                 return 1;
268
269         /* See if there is space after the strings section */
270         ssize = cxt->rgn[FT_STRINGS].size;
271         if (cxt->rgn[FT_STRINGS].start + ssize
272                         < (char *)cxt->bph + cxt->max_size) {
273                 /* move strings up as far as possible */
274                 str = (char *)cxt->bph + cxt->max_size - ssize;
275                 cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
276                 memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
277                 cxt->rgn[FT_STRINGS].start = str;
278                 /* enough space now? */
279                 if (rgn >= FT_STRUCT && ft_shuffle(cxt, pp, rgn, nextra))
280                         return 1;
281         }
282
283         /* how much total free space is there following this region? */
284         tot = 0;
285         for (r = rgn; r < FT_STRINGS; ++r) {
286                 char *r_end = cxt->rgn[r].start + cxt->rgn[r].size;
287                 tot += next_start(cxt, rgn) - r_end;
288         }
289
290         /* cast is to shut gcc up; we know nextra >= 0 */
291         if (tot < (unsigned int)nextra) {
292                 /* have to reallocate */
293                 char *newp, *new_start;
294                 int shift;
295
296                 if (!cxt->realloc)
297                         return 0;
298                 size = _ALIGN(cxt->max_size + (nextra - tot) + EXPAND_INCR, 8);
299                 newp = cxt->realloc(cxt->bph, size);
300                 if (!newp)
301                         return 0;
302                 cxt->max_size = size;
303                 shift = newp - (char *)cxt->bph;
304
305                 if (shift) { /* realloc can return same addr */
306                         cxt->bph = (struct boot_param_header *)newp;
307                         ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start,
308                                         shift);
309                         for (r = FT_RSVMAP; r <= FT_STRINGS; ++r) {
310                                 new_start = cxt->rgn[r].start + shift;
311                                 cxt->rgn[r].start = new_start;
312                         }
313                         *pp += shift;
314                         cxt->str_anchor += shift;
315                 }
316
317                 /* move strings up to the end */
318                 str = newp + size - ssize;
319                 cxt->str_anchor += str - cxt->rgn[FT_STRINGS].start;
320                 memmove(str, cxt->rgn[FT_STRINGS].start, ssize);
321                 cxt->rgn[FT_STRINGS].start = str;
322
323                 if (ft_shuffle(cxt, pp, rgn, nextra))
324                         return 1;
325         }
326
327         /* must be FT_RSVMAP and we need to move FT_STRUCT up */
328         if (rgn == FT_RSVMAP) {
329                 next = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
330                         + nextra;
331                 ssize = cxt->rgn[FT_STRUCT].size;
332                 if (next + ssize >= cxt->rgn[FT_STRINGS].start)
333                         return 0;       /* "can't happen" */
334                 memmove(next, cxt->rgn[FT_STRUCT].start, ssize);
335                 ft_node_update_after(cxt, cxt->rgn[FT_STRUCT].start, nextra);
336                 cxt->rgn[FT_STRUCT].start = next;
337
338                 if (ft_shuffle(cxt, pp, rgn, nextra))
339                         return 1;
340         }
341
342         return 0;               /* "can't happen" */
343 }
344
345 static void ft_put_word(struct ft_cxt *cxt, u32 v)
346 {
347         *(u32 *) cxt->p = cpu_to_be32(v);
348         cxt->p += 4;
349 }
350
351 static void ft_put_bin(struct ft_cxt *cxt, const void *data, unsigned int sz)
352 {
353         unsigned long sza = _ALIGN(sz, 4);
354
355         /* zero out the alignment gap if necessary */
356         if (sz < sza)
357                 *(u32 *) (cxt->p + sza - 4) = 0;
358
359         /* copy in the data */
360         memcpy(cxt->p, data, sz);
361
362         cxt->p += sza;
363 }
364
365 int ft_begin_node(struct ft_cxt *cxt, const char *name)
366 {
367         unsigned long nlen = strlen(name) + 1;
368         unsigned long len = 8 + _ALIGN(nlen, 4);
369
370         if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
371                 return -1;
372         ft_put_word(cxt, OF_DT_BEGIN_NODE);
373         ft_put_bin(cxt, name, strlen(name) + 1);
374         return 0;
375 }
376
377 void ft_end_node(struct ft_cxt *cxt)
378 {
379         ft_put_word(cxt, OF_DT_END_NODE);
380 }
381
382 void ft_nop(struct ft_cxt *cxt)
383 {
384         if (ft_make_space(cxt, &cxt->p, FT_STRUCT, 4))
385                 ft_put_word(cxt, OF_DT_NOP);
386 }
387
388 #define NO_STRING       0x7fffffff
389
390 static int lookup_string(struct ft_cxt *cxt, const char *name)
391 {
392         char *p, *end;
393
394         p = cxt->rgn[FT_STRINGS].start;
395         end = p + cxt->rgn[FT_STRINGS].size;
396         while (p < end) {
397                 if (strcmp(p, (char *)name) == 0)
398                         return p - cxt->str_anchor;
399                 p += strlen(p) + 1;
400         }
401
402         return NO_STRING;
403 }
404
405 /* lookup string and insert if not found */
406 static int map_string(struct ft_cxt *cxt, const char *name)
407 {
408         int off;
409         char *p;
410
411         off = lookup_string(cxt, name);
412         if (off != NO_STRING)
413                 return off;
414         p = cxt->rgn[FT_STRINGS].start;
415         if (!ft_make_space(cxt, &p, FT_STRINGS, strlen(name) + 1))
416                 return NO_STRING;
417         strcpy(p, name);
418         return p - cxt->str_anchor;
419 }
420
421 int ft_prop(struct ft_cxt *cxt, const char *name, const void *data,
422                 unsigned int sz)
423 {
424         int off, len;
425
426         off = lookup_string(cxt, name);
427         if (off == NO_STRING)
428                 return -1;
429
430         len = 12 + _ALIGN(sz, 4);
431         if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, len))
432                 return -1;
433
434         ft_put_word(cxt, OF_DT_PROP);
435         ft_put_word(cxt, sz);
436         ft_put_word(cxt, off);
437         ft_put_bin(cxt, data, sz);
438         return 0;
439 }
440
441 int ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str)
442 {
443         return ft_prop(cxt, name, str, strlen(str) + 1);
444 }
445
446 int ft_prop_int(struct ft_cxt *cxt, const char *name, unsigned int val)
447 {
448         u32 v = cpu_to_be32((u32) val);
449
450         return ft_prop(cxt, name, &v, 4);
451 }
452
453 /* Calculate the size of the reserved map */
454 static unsigned long rsvmap_size(struct ft_cxt *cxt)
455 {
456         struct ft_reserve *res;
457
458         res = (struct ft_reserve *)cxt->rgn[FT_RSVMAP].start;
459         while (res->start || res->len)
460                 ++res;
461         return (char *)(res + 1) - cxt->rgn[FT_RSVMAP].start;
462 }
463
464 /* Calculate the size of the struct region by stepping through it */
465 static unsigned long struct_size(struct ft_cxt *cxt)
466 {
467         char *p = cxt->rgn[FT_STRUCT].start;
468         char *next;
469         struct ft_atom atom;
470
471         /* make check in ft_next happy */
472         if (cxt->rgn[FT_STRUCT].size == 0)
473                 cxt->rgn[FT_STRUCT].size = 0xfffffffful - (unsigned long)p;
474
475         while ((next = ft_next(cxt, p, &atom)) != NULL)
476                 p = next;
477         return p + 4 - cxt->rgn[FT_STRUCT].start;
478 }
479
480 /* add `adj' on to all string offset values in the struct area */
481 static void adjust_string_offsets(struct ft_cxt *cxt, int adj)
482 {
483         char *p = cxt->rgn[FT_STRUCT].start;
484         char *next;
485         struct ft_atom atom;
486         int off;
487
488         while ((next = ft_next(cxt, p, &atom)) != NULL) {
489                 if (atom.tag == OF_DT_PROP) {
490                         off = be32_to_cpu(*(u32 *) (p + 8));
491                         *(u32 *) (p + 8) = cpu_to_be32(off + adj);
492                 }
493                 p = next;
494         }
495 }
496
497 /* start construction of the flat OF tree from scratch */
498 void ft_begin(struct ft_cxt *cxt, void *blob, unsigned int max_size,
499                 void *(*realloc_fn) (void *, unsigned long))
500 {
501         struct boot_param_header *bph = blob;
502         char *p;
503         struct ft_reserve *pres;
504
505         /* clear the cxt */
506         memset(cxt, 0, sizeof(*cxt));
507
508         cxt->bph = bph;
509         cxt->max_size = max_size;
510         cxt->realloc = realloc_fn;
511         cxt->isordered = 1;
512
513         /* zero everything in the header area */
514         memset(bph, 0, sizeof(*bph));
515
516         bph->magic = cpu_to_be32(OF_DT_HEADER);
517         bph->version = cpu_to_be32(0x10);
518         bph->last_comp_version = cpu_to_be32(0x10);
519
520         /* start pointers */
521         cxt->rgn[FT_RSVMAP].start = p = blob + HDR_SIZE;
522         cxt->rgn[FT_RSVMAP].size = sizeof(struct ft_reserve);
523         pres = (struct ft_reserve *)p;
524         cxt->rgn[FT_STRUCT].start = p += sizeof(struct ft_reserve);
525         cxt->rgn[FT_STRUCT].size = 4;
526         cxt->rgn[FT_STRINGS].start = blob + max_size;
527         cxt->rgn[FT_STRINGS].size = 0;
528
529         /* init rsvmap and struct */
530         pres->start = 0;
531         pres->len = 0;
532         *(u32 *) p = cpu_to_be32(OF_DT_END);
533
534         cxt->str_anchor = blob;
535 }
536
537 /* open up an existing blob to be examined or modified */
538 int ft_open(struct ft_cxt *cxt, void *blob, unsigned int max_size,
539                 unsigned int max_find_device,
540                 void *(*realloc_fn) (void *, unsigned long))
541 {
542         struct boot_param_header *bph = blob;
543
544         /* can't cope with version < 16 */
545         if (be32_to_cpu(bph->version) < 16)
546                 return -1;
547
548         /* clear the cxt */
549         memset(cxt, 0, sizeof(*cxt));
550
551         /* alloc node_tbl to track node ptrs returned by ft_find_device */
552         ++max_find_device;
553         cxt->node_tbl = realloc_fn(NULL, max_find_device * sizeof(char *));
554         if (!cxt->node_tbl)
555                 return -1;
556         memset(cxt->node_tbl, 0, max_find_device * sizeof(char *));
557         cxt->node_max = max_find_device;
558         cxt->nodes_used = 1;    /* don't use idx 0 b/c looks like NULL */
559
560         cxt->bph = bph;
561         cxt->max_size = max_size;
562         cxt->realloc = realloc_fn;
563
564         cxt->rgn[FT_RSVMAP].start = blob + be32_to_cpu(bph->off_mem_rsvmap);
565         cxt->rgn[FT_RSVMAP].size = rsvmap_size(cxt);
566         cxt->rgn[FT_STRUCT].start = blob + be32_to_cpu(bph->off_dt_struct);
567         cxt->rgn[FT_STRUCT].size = struct_size(cxt);
568         cxt->rgn[FT_STRINGS].start = blob + be32_to_cpu(bph->off_dt_strings);
569         cxt->rgn[FT_STRINGS].size = be32_to_cpu(bph->dt_strings_size);
570         /* Leave as '0' to force first ft_make_space call to do a ft_reorder
571          * and move dt to an area allocated by realloc.
572         cxt->isordered = ft_ordered(cxt);
573         */
574
575         cxt->p = cxt->rgn[FT_STRUCT].start;
576         cxt->str_anchor = cxt->rgn[FT_STRINGS].start;
577
578         return 0;
579 }
580
581 /* add a reserver physical area to the rsvmap */
582 int ft_add_rsvmap(struct ft_cxt *cxt, u64 physaddr, u64 size)
583 {
584         char *p;
585         struct ft_reserve *pres;
586
587         p = cxt->rgn[FT_RSVMAP].start + cxt->rgn[FT_RSVMAP].size
588                 - sizeof(struct ft_reserve);
589         if (!ft_make_space(cxt, &p, FT_RSVMAP, sizeof(struct ft_reserve)))
590                 return -1;
591
592         pres = (struct ft_reserve *)p;
593         pres->start = cpu_to_be64(physaddr);
594         pres->len = cpu_to_be64(size);
595
596         return 0;
597 }
598
599 void ft_begin_tree(struct ft_cxt *cxt)
600 {
601         cxt->p = ft_root_node(cxt);
602 }
603
604 void ft_end_tree(struct ft_cxt *cxt)
605 {
606         struct boot_param_header *bph = cxt->bph;
607         char *p, *oldstr, *str, *endp;
608         unsigned long ssize;
609         int adj;
610
611         if (!cxt->isordered)
612                 return;         /* we haven't touched anything */
613
614         /* adjust string offsets */
615         oldstr = cxt->rgn[FT_STRINGS].start;
616         adj = cxt->str_anchor - oldstr;
617         if (adj)
618                 adjust_string_offsets(cxt, adj);
619
620         /* make strings end on 8-byte boundary */
621         ssize = cxt->rgn[FT_STRINGS].size;
622         endp = (char *)_ALIGN((unsigned long)cxt->rgn[FT_STRUCT].start
623                         + cxt->rgn[FT_STRUCT].size + ssize, 8);
624         str = endp - ssize;
625
626         /* move strings down to end of structs */
627         memmove(str, oldstr, ssize);
628         cxt->str_anchor = str;
629         cxt->rgn[FT_STRINGS].start = str;
630
631         /* fill in header fields */
632         p = (char *)bph;
633         bph->totalsize = cpu_to_be32(endp - p);
634         bph->off_mem_rsvmap = cpu_to_be32(cxt->rgn[FT_RSVMAP].start - p);
635         bph->off_dt_struct = cpu_to_be32(cxt->rgn[FT_STRUCT].start - p);
636         bph->off_dt_strings = cpu_to_be32(cxt->rgn[FT_STRINGS].start - p);
637         bph->dt_strings_size = cpu_to_be32(ssize);
638 }
639
640 void *ft_find_device(struct ft_cxt *cxt, const char *srch_path)
641 {
642         char *node;
643
644         /* require absolute path */
645         if (srch_path[0] != '/')
646                 return NULL;
647         node = ft_find_descendent(cxt, ft_root_node(cxt), srch_path);
648         return ft_get_phandle(cxt, node);
649 }
650
651 void *ft_find_descendent(struct ft_cxt *cxt, void *top, const char *srch_path)
652 {
653         struct ft_atom atom;
654         char *p;
655         const char *cp, *q;
656         int cl;
657         int depth = -1;
658         int dmatch = 0;
659         const char *path_comp[FT_MAX_DEPTH];
660
661         cp = srch_path;
662         cl = 0;
663         p = top;
664
665         while ((p = ft_next(cxt, p, &atom)) != NULL) {
666                 switch (atom.tag) {
667                 case OF_DT_BEGIN_NODE:
668                         ++depth;
669                         if (depth != dmatch)
670                                 break;
671                         cxt->genealogy[depth] = atom.data;
672                         cxt->genealogy[depth + 1] = NULL;
673                         if (depth && !(strncmp(atom.name, cp, cl) == 0
674                                         && (atom.name[cl] == '/'
675                                                 || atom.name[cl] == '\0'
676                                                 || atom.name[cl] == '@')))
677                                 break;
678                         path_comp[dmatch] = cp;
679                         /* it matches so far, advance to next path component */
680                         cp += cl;
681                         /* skip slashes */
682                         while (*cp == '/')
683                                 ++cp;
684                         /* we're done if this is the end of the string */
685                         if (*cp == 0)
686                                 return atom.data;
687                         /* look for end of this component */
688                         q = strchr(cp, '/');
689                         if (q)
690                                 cl = q - cp;
691                         else
692                                 cl = strlen(cp);
693                         ++dmatch;
694                         break;
695                 case OF_DT_END_NODE:
696                         if (depth == 0)
697                                 return NULL;
698                         if (dmatch > depth) {
699                                 --dmatch;
700                                 cl = cp - path_comp[dmatch] - 1;
701                                 cp = path_comp[dmatch];
702                                 while (cl > 0 && cp[cl - 1] == '/')
703                                         --cl;
704                         }
705                         --depth;
706                         break;
707                 }
708         }
709         return NULL;
710 }
711
712 void *ft_get_parent(struct ft_cxt *cxt, const void *phandle)
713 {
714         void *node;
715         int d;
716         struct ft_atom atom;
717         char *p;
718
719         node = ft_node_ph2node(cxt, phandle);
720         if (node == NULL)
721                 return NULL;
722
723         for (d = 0; cxt->genealogy[d] != NULL; ++d)
724                 if (cxt->genealogy[d] == node)
725                         return cxt->genealogy[d > 0 ? d - 1 : 0];
726
727         /* have to do it the hard way... */
728         p = ft_root_node(cxt);
729         d = 0;
730         while ((p = ft_next(cxt, p, &atom)) != NULL) {
731                 switch (atom.tag) {
732                 case OF_DT_BEGIN_NODE:
733                         cxt->genealogy[d] = atom.data;
734                         if (node == atom.data) {
735                                 /* found it */
736                                 cxt->genealogy[d + 1] = NULL;
737                                 return d > 0 ? cxt->genealogy[d - 1] : node;
738                         }
739                         ++d;
740                         break;
741                 case OF_DT_END_NODE:
742                         --d;
743                         break;
744                 }
745         }
746         return NULL;
747 }
748
749 int ft_get_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
750                 void *buf, const unsigned int buflen)
751 {
752         struct ft_atom atom;
753         void *node;
754         char *p;
755         int depth;
756         unsigned int size;
757
758         node = ft_node_ph2node(cxt, phandle);
759         if (node == NULL)
760                 return -1;
761
762         depth = 0;
763         p = (char *)node;
764
765         while ((p = ft_next(cxt, p, &atom)) != NULL) {
766                 switch (atom.tag) {
767                 case OF_DT_BEGIN_NODE:
768                         ++depth;
769                         break;
770                 case OF_DT_PROP:
771                         if ((depth != 1) || strcmp(atom.name, propname))
772                                 break;
773                         size = min(atom.size, buflen);
774                         memcpy(buf, atom.data, size);
775                         return atom.size;
776                 case OF_DT_END_NODE:
777                         if (--depth <= 0)
778                                 return -1;
779                 }
780         }
781         return -1;
782 }
783
784 int ft_set_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
785                 const void *buf, const unsigned int buflen)
786 {
787         struct ft_atom atom;
788         void *node;
789         char *p, *next;
790         int nextra, depth;
791
792         node = ft_node_ph2node(cxt, phandle);
793         if (node == NULL)
794                 return -1;
795
796         depth = 0;
797         p = node;
798
799         while ((next = ft_next(cxt, p, &atom)) != NULL) {
800                 switch (atom.tag) {
801                 case OF_DT_BEGIN_NODE:
802                         ++depth;
803                         break;
804                 case OF_DT_END_NODE:
805                         if (--depth > 0)
806                                 break;
807                         /* haven't found the property, insert here */
808                         cxt->p = p;
809                         return ft_prop(cxt, propname, buf, buflen);
810                 case OF_DT_PROP:
811                         if ((depth != 1) || strcmp(atom.name, propname))
812                                 break;
813                         /* found an existing property, overwrite it */
814                         nextra = _ALIGN(buflen, 4) - _ALIGN(atom.size, 4);
815                         cxt->p = atom.data;
816                         if (nextra && !ft_make_space(cxt, &cxt->p, FT_STRUCT,
817                                                 nextra))
818                                 return -1;
819                         *(u32 *) (cxt->p - 8) = cpu_to_be32(buflen);
820                         ft_put_bin(cxt, buf, buflen);
821                         return 0;
822                 }
823                 p = next;
824         }
825         return -1;
826 }
827
828 int ft_del_prop(struct ft_cxt *cxt, const void *phandle, const char *propname)
829 {
830         struct ft_atom atom;
831         void *node;
832         char *p, *next;
833         int size;
834
835         node = ft_node_ph2node(cxt, phandle);
836         if (node == NULL)
837                 return -1;
838
839         p = node;
840         while ((next = ft_next(cxt, p, &atom)) != NULL) {
841                 switch (atom.tag) {
842                 case OF_DT_BEGIN_NODE:
843                 case OF_DT_END_NODE:
844                         return -1;
845                 case OF_DT_PROP:
846                         if (strcmp(atom.name, propname))
847                                 break;
848                         /* found the property, remove it */
849                         size = 12 + -_ALIGN(atom.size, 4);
850                         cxt->p = p;
851                         if (!ft_make_space(cxt, &cxt->p, FT_STRUCT, -size))
852                                 return -1;
853                         return 0;
854                 }
855                 p = next;
856         }
857         return -1;
858 }
859
860 void *ft_create_node(struct ft_cxt *cxt, const void *parent, const char *path)
861 {
862         struct ft_atom atom;
863         char *p, *next;
864         int depth = 0;
865
866         p = ft_root_node(cxt);
867         while ((next = ft_next(cxt, p, &atom)) != NULL) {
868                 switch (atom.tag) {
869                 case OF_DT_BEGIN_NODE:
870                         ++depth;
871                         if (depth == 1 && strcmp(atom.name, path) == 0)
872                                 /* duplicate node path, return error */
873                                 return NULL;
874                         break;
875                 case OF_DT_END_NODE:
876                         --depth;
877                         if (depth > 0)
878                                 break;
879                         /* end of node, insert here */
880                         cxt->p = p;
881                         ft_begin_node(cxt, path);
882                         ft_end_node(cxt);
883                         return p;
884                 }
885                 p = next;
886         }
887         return NULL;
888 }