f08941a7adecb6ac23beeb310b075c845f19d382
[platform/kernel/u-boot.git] / libfdt / fdt_ro.c
1 /*
2  * libfdt - Flat Device Tree manipulation
3  * Copyright (C) 2006 David Gibson, IBM Corporation.
4  *
5  * libfdt is dual licensed: you can use it either under the terms of
6  * the GPL, or the BSD license, at your option.
7  *
8  *  a) This library is free software; you can redistribute it and/or
9  *     modify it under the terms of the GNU General Public License as
10  *     published by the Free Software Foundation; either version 2 of the
11  *     License, or (at your option) any later version.
12  *
13  *     This library is distributed in the hope that it will be useful,
14  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *     GNU General Public License for more details.
17  *
18  *     You should have received a copy of the GNU General Public
19  *     License along with this library; if not, write to the Free
20  *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21  *     MA 02110-1301 USA
22  *
23  * Alternatively,
24  *
25  *  b) Redistribution and use in source and binary forms, with or
26  *     without modification, are permitted provided that the following
27  *     conditions are met:
28  *
29  *     1. Redistributions of source code must retain the above
30  *        copyright notice, this list of conditions and the following
31  *        disclaimer.
32  *     2. Redistributions in binary form must reproduce the above
33  *        copyright notice, this list of conditions and the following
34  *        disclaimer in the documentation and/or other materials
35  *        provided with the distribution.
36  *
37  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  */
51 #include "libfdt_env.h"
52
53 #include <fdt.h>
54 #include <libfdt.h>
55
56 #include "libfdt_internal.h"
57
58 #define CHECK_HEADER(fdt) \
59         { \
60                 int err; \
61                 if ((err = fdt_check_header(fdt)) != 0) \
62                         return err; \
63         }
64
65 static int nodename_eq(const void *fdt, int offset,
66                        const char *s, int len)
67 {
68         const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
69
70         if (! p)
71                 /* short match */
72                 return 0;
73
74         if (memcmp(p, s, len) != 0)
75                 return 0;
76
77         if (p[len] == '\0')
78                 return 1;
79         else if (!memchr(s, '@', len) && (p[len] == '@'))
80                 return 1;
81         else
82                 return 0;
83 }
84
85 const char *fdt_string(const void *fdt, int stroffset)
86 {
87         return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
88 }
89
90 int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
91 {
92         CHECK_HEADER(fdt);
93         *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
94         *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
95         return 0;
96 }
97
98 int fdt_num_mem_rsv(const void *fdt)
99 {
100         int i = 0;
101
102         while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
103                 i++;
104         return i;
105 }
106
107 int fdt_subnode_offset_namelen(const void *fdt, int offset,
108                                const char *name, int namelen)
109 {
110         int depth;
111
112         CHECK_HEADER(fdt);
113
114         for (depth = 0;
115              offset >= 0;
116              offset = fdt_next_node(fdt, offset, &depth)) {
117                 if (depth < 0)
118                         return -FDT_ERR_NOTFOUND;
119                 else if ((depth == 1)
120                          && nodename_eq(fdt, offset, name, namelen))
121                         return offset;
122         }
123
124         return offset; /* error */
125 }
126
127 int fdt_subnode_offset(const void *fdt, int parentoffset,
128                        const char *name)
129 {
130         return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
131 }
132
133 int fdt_path_offset(const void *fdt, const char *path)
134 {
135         const char *end = path + strlen(path);
136         const char *p = path;
137         int offset = 0;
138
139         CHECK_HEADER(fdt);
140
141         if (*path != '/')
142                 return -FDT_ERR_BADPATH;
143
144         while (*p) {
145                 const char *q;
146
147                 while (*p == '/')
148                         p++;
149                 if (! *p)
150                         return offset;
151                 q = strchr(p, '/');
152                 if (! q)
153                         q = end;
154
155                 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
156                 if (offset < 0)
157                         return offset;
158
159                 p = q;
160         }
161
162         return offset;
163 }
164
165 const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
166 {
167         const struct fdt_node_header *nh;
168         int err;
169
170         if ((err = fdt_check_header(fdt)) != 0)
171                 goto fail;
172
173         err = -FDT_ERR_BADOFFSET;
174         nh = fdt_offset_ptr(fdt, nodeoffset, sizeof(*nh));
175         if (!nh || (fdt32_to_cpu(nh->tag) != FDT_BEGIN_NODE))
176                 goto fail;
177
178         if (len)
179                 *len = strlen(nh->name);
180
181         return nh->name;
182
183  fail:
184         if (len)
185                 *len = err;
186         return NULL;
187 }
188
189 const struct fdt_property *fdt_get_property(const void *fdt,
190                                             int nodeoffset,
191                                             const char *name, int *lenp)
192 {
193         uint32_t tag;
194         const struct fdt_property *prop;
195         int namestroff;
196         int offset, nextoffset;
197         int err;
198
199         if ((err = fdt_check_header(fdt)) != 0)
200                 goto fail;
201
202         err = -FDT_ERR_BADOFFSET;
203         if (nodeoffset % FDT_TAGSIZE)
204                 goto fail;
205
206         tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
207         if (tag != FDT_BEGIN_NODE)
208                 goto fail;
209
210         do {
211                 offset = nextoffset;
212
213                 tag = fdt_next_tag(fdt, offset, &nextoffset);
214                 switch (tag) {
215                 case FDT_END:
216                         err = -FDT_ERR_TRUNCATED;
217                         goto fail;
218
219                 case FDT_BEGIN_NODE:
220                 case FDT_END_NODE:
221                 case FDT_NOP:
222                         break;
223
224                 case FDT_PROP:
225                         err = -FDT_ERR_BADSTRUCTURE;
226                         prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
227                         if (! prop)
228                                 goto fail;
229                         namestroff = fdt32_to_cpu(prop->nameoff);
230                         if (streq(fdt_string(fdt, namestroff), name)) {
231                                 /* Found it! */
232                                 int len = fdt32_to_cpu(prop->len);
233                                 prop = fdt_offset_ptr(fdt, offset,
234                                                       sizeof(*prop)+len);
235                                 if (! prop)
236                                         goto fail;
237
238                                 if (lenp)
239                                         *lenp = len;
240
241                                 return prop;
242                         }
243                         break;
244
245                 default:
246                         err = -FDT_ERR_BADSTRUCTURE;
247                         goto fail;
248                 }
249         } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
250
251         err = -FDT_ERR_NOTFOUND;
252  fail:
253         if (lenp)
254                 *lenp = err;
255         return NULL;
256 }
257
258 const void *fdt_getprop(const void *fdt, int nodeoffset,
259                   const char *name, int *lenp)
260 {
261         const struct fdt_property *prop;
262
263         prop = fdt_get_property(fdt, nodeoffset, name, lenp);
264         if (! prop)
265                 return NULL;
266
267         return prop->data;
268 }
269
270 uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
271 {
272         const uint32_t *php;
273         int len;
274
275         php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
276         if (!php || (len != sizeof(*php)))
277                 return 0;
278
279         return fdt32_to_cpu(*php);
280 }
281
282 int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
283 {
284         int pdepth = 0, p = 0;
285         int offset, depth, namelen;
286         const char *name;
287
288         CHECK_HEADER(fdt);
289
290         if (buflen < 2)
291                 return -FDT_ERR_NOSPACE;
292
293         for (offset = 0, depth = 0;
294              (offset >= 0) && (offset <= nodeoffset);
295              offset = fdt_next_node(fdt, offset, &depth)) {
296                 if (pdepth < depth)
297                         continue; /* overflowed buffer */
298
299                 while (pdepth > depth) {
300                         do {
301                                 p--;
302                         } while (buf[p-1] != '/');
303                         pdepth--;
304                 }
305
306                 name = fdt_get_name(fdt, offset, &namelen);
307                 if (!name)
308                         return namelen;
309                 if ((p + namelen + 1) <= buflen) {
310                         memcpy(buf + p, name, namelen);
311                         p += namelen;
312                         buf[p++] = '/';
313                         pdepth++;
314                 }
315
316                 if (offset == nodeoffset) {
317                         if (pdepth < (depth + 1))
318                                 return -FDT_ERR_NOSPACE;
319
320                         if (p > 1) /* special case so that root path is "/", not "" */
321                                 p--;
322                         buf[p] = '\0';
323                         return p;
324                 }
325         }
326
327         if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
328                 return -FDT_ERR_BADOFFSET;
329         else if (offset == -FDT_ERR_BADOFFSET)
330                 return -FDT_ERR_BADSTRUCTURE;
331
332         return offset; /* error from fdt_next_node() */
333 }
334
335 int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
336                                  int supernodedepth, int *nodedepth)
337 {
338         int offset, depth;
339         int supernodeoffset = -FDT_ERR_INTERNAL;
340
341         CHECK_HEADER(fdt);
342
343         if (supernodedepth < 0)
344                 return -FDT_ERR_NOTFOUND;
345
346         for (offset = 0, depth = 0;
347              (offset >= 0) && (offset <= nodeoffset);
348              offset = fdt_next_node(fdt, offset, &depth)) {
349                 if (depth == supernodedepth)
350                         supernodeoffset = offset;
351
352                 if (offset == nodeoffset) {
353                         if (nodedepth)
354                                 *nodedepth = depth;
355
356                         if (supernodedepth > depth)
357                                 return -FDT_ERR_NOTFOUND;
358                         else
359                                 return supernodeoffset;
360                 }
361         }
362
363         if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
364                 return -FDT_ERR_BADOFFSET;
365         else if (offset == -FDT_ERR_BADOFFSET)
366                 return -FDT_ERR_BADSTRUCTURE;
367
368         return offset; /* error from fdt_next_node() */
369 }
370
371 int fdt_node_depth(const void *fdt, int nodeoffset)
372 {
373         int nodedepth;
374         int err;
375
376         err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
377         if (err)
378                 return (err < 0) ? err : -FDT_ERR_INTERNAL;
379         return nodedepth;
380 }
381
382 int fdt_parent_offset(const void *fdt, int nodeoffset)
383 {
384         int nodedepth = fdt_node_depth(fdt, nodeoffset);
385
386         if (nodedepth < 0)
387                 return nodedepth;
388         return fdt_supernode_atdepth_offset(fdt, nodeoffset,
389                                             nodedepth - 1, NULL);
390 }
391
392 int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
393                                   const char *propname,
394                                   const void *propval, int proplen)
395 {
396         int offset;
397         const void *val;
398         int len;
399
400         CHECK_HEADER(fdt);
401
402         /* FIXME: The algorithm here is pretty horrible: we scan each
403          * property of a node in fdt_getprop(), then if that didn't
404          * find what we want, we scan over them again making our way
405          * to the next node.  Still it's the easiest to implement
406          * approach; performance can come later. */
407         for (offset = fdt_next_node(fdt, startoffset, NULL);
408              offset >= 0;
409              offset = fdt_next_node(fdt, offset, NULL)) {
410                 val = fdt_getprop(fdt, offset, propname, &len);
411                 if (val && (len == proplen)
412                     && (memcmp(val, propval, len) == 0))
413                         return offset;
414         }
415
416         return offset; /* error from fdt_next_node() */
417 }
418
419 int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
420 {
421         if ((phandle == 0) || (phandle == -1))
422                 return -FDT_ERR_BADPHANDLE;
423         phandle = cpu_to_fdt32(phandle);
424         return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle",
425                                              &phandle, sizeof(phandle));
426 }
427
428 int _stringlist_contains(const void *strlist, int listlen, const char *str)
429 {
430         int len = strlen(str);
431         const void *p;
432
433         while (listlen >= len) {
434                 if (memcmp(str, strlist, len+1) == 0)
435                         return 1;
436                 p = memchr(strlist, '\0', listlen);
437                 if (!p)
438                         return 0; /* malformed strlist.. */
439                 listlen -= (p-strlist) + 1;
440                 strlist = p + 1;
441         }
442         return 0;
443 }
444
445 int fdt_node_check_compatible(const void *fdt, int nodeoffset,
446                               const char *compatible)
447 {
448         const void *prop;
449         int len;
450
451         prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
452         if (!prop)
453                 return len;
454         if (_stringlist_contains(prop, len, compatible))
455                 return 0;
456         else
457                 return 1;
458 }
459
460 int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
461                                   const char *compatible)
462 {
463         uint32_t tag;
464         int offset, nextoffset;
465         int err;
466
467         CHECK_HEADER(fdt);
468
469         if (startoffset >= 0) {
470                 tag = fdt_next_tag(fdt, startoffset, &nextoffset);
471                 if (tag != FDT_BEGIN_NODE)
472                         return -FDT_ERR_BADOFFSET;
473         } else {
474                 nextoffset = 0;
475         }
476
477         /* FIXME: The algorithm here is pretty horrible: we scan each
478          * property of a node in fdt_node_check_compatible(), then if
479          * that didn't find what we want, we scan over them again
480          * making our way to the next node.  Still it's the easiest to
481          * implement approach; performance can come later. */
482         for (offset = fdt_next_node(fdt, startoffset, NULL);
483              offset >= 0;
484              offset = fdt_next_node(fdt, offset, NULL)) {
485                 err = fdt_node_check_compatible(fdt, offset, compatible);
486                 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
487                         return err;
488                 else if (err == 0)
489                         return offset;
490         }
491
492         return offset; /* error from fdt_next_node() */
493 }