blob: 6365c2654e820f768d2ea27d4bdf9ad422ae211b [file] [log] [blame]
David Gibson3da0f9a2006-11-27 16:21:28 +11001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
David Gibson94816052007-06-13 16:30:48 +10005 * libfdt is dual licensed: you can use it either under the terms of
6 * the GPL, or the BSD license, at your option.
David Gibson3da0f9a2006-11-27 16:21:28 +11007 *
David Gibson94816052007-06-13 16:30:48 +10008 * 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.
David Gibson3da0f9a2006-11-27 16:21:28 +110012 *
David Gibson94816052007-06-13 16:30:48 +100013 * 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.
David Gibson3da0f9a2006-11-27 16:21:28 +110050 */
51#include "libfdt_env.h"
52
53#include <fdt.h>
54#include <libfdt.h>
55
56#include "libfdt_internal.h"
57
David Gibsonb6d80a22008-07-09 14:10:24 +100058static int _fdt_nodename_eq(const void *fdt, int offset,
59 const char *s, int len)
David Gibson3da0f9a2006-11-27 16:21:28 +110060{
David Gibsonfc9769a2008-02-12 11:58:31 +110061 const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
David Gibson3da0f9a2006-11-27 16:21:28 +110062
63 if (! p)
64 /* short match */
65 return 0;
66
67 if (memcmp(p, s, len) != 0)
68 return 0;
69
David Gibsond2a9da02007-09-28 15:51:04 +100070 if (p[len] == '\0')
71 return 1;
72 else if (!memchr(s, '@', len) && (p[len] == '@'))
73 return 1;
74 else
David Gibson3da0f9a2006-11-27 16:21:28 +110075 return 0;
David Gibson3da0f9a2006-11-27 16:21:28 +110076}
77
David Gibson11d53022007-10-18 12:10:42 +100078const char *fdt_string(const void *fdt, int stroffset)
David Gibson3aa4cfd2006-11-29 16:34:30 +110079{
David Gibson14090972008-07-07 10:14:15 +100080 return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
David Gibson3aa4cfd2006-11-29 16:34:30 +110081}
82
David Gibsoncb650ae2008-08-06 14:50:49 +100083static int _fdt_string_eq(const void *fdt, int stroffset,
84 const char *s, int len)
85{
86 const char *p = fdt_string(fdt, stroffset);
87
88 return (strlen(p) == len) && (memcmp(p, s, len) == 0);
89}
90
David Gibsonfd1bf3a2007-10-10 17:12:12 +100091int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
92{
David Gibsonb6d80a22008-07-09 14:10:24 +100093 FDT_CHECK_HEADER(fdt);
David Gibsonfd1bf3a2007-10-10 17:12:12 +100094 *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
95 *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
96 return 0;
97}
98
99int fdt_num_mem_rsv(const void *fdt)
100{
101 int i = 0;
102
103 while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
104 i++;
105 return i;
106}
107
David Gibsonfc9769a2008-02-12 11:58:31 +1100108int fdt_subnode_offset_namelen(const void *fdt, int offset,
David Gibson3da0f9a2006-11-27 16:21:28 +1100109 const char *name, int namelen)
110{
David Gibsonfc9769a2008-02-12 11:58:31 +1100111 int depth;
David Gibson3da0f9a2006-11-27 16:21:28 +1100112
David Gibsonb6d80a22008-07-09 14:10:24 +1000113 FDT_CHECK_HEADER(fdt);
David Gibson3da0f9a2006-11-27 16:21:28 +1100114
David Gibsonfc9769a2008-02-12 11:58:31 +1100115 for (depth = 0;
116 offset >= 0;
117 offset = fdt_next_node(fdt, offset, &depth)) {
118 if (depth < 0)
119 return -FDT_ERR_NOTFOUND;
120 else if ((depth == 1)
David Gibsonb6d80a22008-07-09 14:10:24 +1000121 && _fdt_nodename_eq(fdt, offset, name, namelen))
David Gibsonfc9769a2008-02-12 11:58:31 +1100122 return offset;
123 }
David Gibson3da0f9a2006-11-27 16:21:28 +1100124
David Gibsonfc9769a2008-02-12 11:58:31 +1100125 return offset; /* error */
David Gibson3da0f9a2006-11-27 16:21:28 +1100126}
127
David Gibson73d60922006-12-15 15:12:47 +1100128int fdt_subnode_offset(const void *fdt, int parentoffset,
David Gibson3da0f9a2006-11-27 16:21:28 +1100129 const char *name)
130{
131 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
132}
133
David Gibson73d60922006-12-15 15:12:47 +1100134int fdt_path_offset(const void *fdt, const char *path)
David Gibson3da0f9a2006-11-27 16:21:28 +1100135{
136 const char *end = path + strlen(path);
137 const char *p = path;
138 int offset = 0;
139
David Gibsonb6d80a22008-07-09 14:10:24 +1000140 FDT_CHECK_HEADER(fdt);
David Gibson3da0f9a2006-11-27 16:21:28 +1100141
Kumar Gala02cc8352008-08-14 08:28:19 -0500142 /* see if we have an alias */
143 if (*path != '/') {
David Gibson9c831152008-08-20 16:55:14 +1000144 const char *q = strchr(path, '/');
Kumar Gala02cc8352008-08-14 08:28:19 -0500145
Kumar Gala02cc8352008-08-14 08:28:19 -0500146 if (!q)
147 q = end;
148
David Gibson9c831152008-08-20 16:55:14 +1000149 p = fdt_get_alias_namelen(fdt, p, q - p);
Kumar Gala02cc8352008-08-14 08:28:19 -0500150 if (!p)
151 return -FDT_ERR_BADPATH;
152 offset = fdt_path_offset(fdt, p);
153
154 p = q;
155 }
David Gibson3da0f9a2006-11-27 16:21:28 +1100156
157 while (*p) {
158 const char *q;
159
160 while (*p == '/')
161 p++;
162 if (! *p)
David Gibsonbd2ae2f2007-08-29 12:22:50 +1000163 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +1100164 q = strchr(p, '/');
165 if (! q)
166 q = end;
167
168 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
David Gibson9a9fdf52006-12-15 15:12:51 +1100169 if (offset < 0)
David Gibson3da0f9a2006-11-27 16:21:28 +1100170 return offset;
171
172 p = q;
173 }
174
David Gibson63dc9c72007-09-18 11:44:04 +1000175 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +1100176}
177
David Gibson9d26eab2007-08-30 14:54:04 +1000178const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
179{
David Gibsonaa1baab2008-05-20 17:19:11 +1000180 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
David Gibson9d26eab2007-08-30 14:54:04 +1000181 int err;
182
David Gibsonaa1baab2008-05-20 17:19:11 +1000183 if (((err = fdt_check_header(fdt)) != 0)
184 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
185 goto fail;
David Gibson9d26eab2007-08-30 14:54:04 +1000186
187 if (len)
188 *len = strlen(nh->name);
189
190 return nh->name;
191
192 fail:
193 if (len)
194 *len = err;
195 return NULL;
196}
197
David Gibsoncb650ae2008-08-06 14:50:49 +1000198const struct fdt_property *fdt_get_property_namelen(const void *fdt,
199 int nodeoffset,
200 const char *name,
201 int namelen, int *lenp)
David Gibson3da0f9a2006-11-27 16:21:28 +1100202{
David Gibson94993f42006-12-11 16:15:34 +1100203 uint32_t tag;
David Gibsona6c76f92007-06-13 14:18:10 +1000204 const struct fdt_property *prop;
David Gibson94993f42006-12-11 16:15:34 +1100205 int namestroff;
206 int offset, nextoffset;
David Gibsona7ee95d2006-12-15 15:12:49 +1100207 int err;
David Gibson3da0f9a2006-11-27 16:21:28 +1100208
David Gibsonaa1baab2008-05-20 17:19:11 +1000209 if (((err = fdt_check_header(fdt)) != 0)
210 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
211 goto fail;
David Gibson3da0f9a2006-11-27 16:21:28 +1100212
David Gibsonaa1baab2008-05-20 17:19:11 +1000213 nextoffset = err;
David Gibson94993f42006-12-11 16:15:34 +1100214 do {
215 offset = nextoffset;
David Gibson94993f42006-12-11 16:15:34 +1100216
David Gibson3c44c872007-10-24 11:06:09 +1000217 tag = fdt_next_tag(fdt, offset, &nextoffset);
David Gibson94993f42006-12-11 16:15:34 +1100218 switch (tag) {
219 case FDT_END:
David Gibson9a9fdf52006-12-15 15:12:51 +1100220 err = -FDT_ERR_TRUNCATED;
David Gibsona7ee95d2006-12-15 15:12:49 +1100221 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100222
223 case FDT_BEGIN_NODE:
David Gibson94993f42006-12-11 16:15:34 +1100224 case FDT_END_NODE:
David Gibson592ea582007-09-04 10:43:03 +1000225 case FDT_NOP:
David Gibson94993f42006-12-11 16:15:34 +1100226 break;
227
228 case FDT_PROP:
David Gibson9a9fdf52006-12-15 15:12:51 +1100229 err = -FDT_ERR_BADSTRUCTURE;
David Gibson2cf86932007-11-19 17:26:22 +1100230 prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
David Gibson94993f42006-12-11 16:15:34 +1100231 if (! prop)
David Gibsona7ee95d2006-12-15 15:12:49 +1100232 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100233 namestroff = fdt32_to_cpu(prop->nameoff);
David Gibsoncb650ae2008-08-06 14:50:49 +1000234 if (_fdt_string_eq(fdt, namestroff, name, namelen)) {
David Gibson94993f42006-12-11 16:15:34 +1100235 /* Found it! */
236 int len = fdt32_to_cpu(prop->len);
237 prop = fdt_offset_ptr(fdt, offset,
David Gibson9825f822006-12-14 15:29:25 +1100238 sizeof(*prop)+len);
David Gibson94993f42006-12-11 16:15:34 +1100239 if (! prop)
David Gibsona7ee95d2006-12-15 15:12:49 +1100240 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100241
242 if (lenp)
243 *lenp = len;
David Gibson63dc9c72007-09-18 11:44:04 +1000244
David Gibson94993f42006-12-11 16:15:34 +1100245 return prop;
246 }
247 break;
248
David Gibson94993f42006-12-11 16:15:34 +1100249 default:
David Gibson9a9fdf52006-12-15 15:12:51 +1100250 err = -FDT_ERR_BADSTRUCTURE;
David Gibsona7ee95d2006-12-15 15:12:49 +1100251 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100252 }
David Gibson592ea582007-09-04 10:43:03 +1000253 } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
David Gibson94993f42006-12-11 16:15:34 +1100254
David Gibson9a9fdf52006-12-15 15:12:51 +1100255 err = -FDT_ERR_NOTFOUND;
David Gibsona7ee95d2006-12-15 15:12:49 +1100256 fail:
257 if (lenp)
David Gibson9a9fdf52006-12-15 15:12:51 +1100258 *lenp = err;
David Gibsona7ee95d2006-12-15 15:12:49 +1100259 return NULL;
David Gibson3da0f9a2006-11-27 16:21:28 +1100260}
261
David Gibsoncb650ae2008-08-06 14:50:49 +1000262const struct fdt_property *fdt_get_property(const void *fdt,
263 int nodeoffset,
264 const char *name, int *lenp)
265{
266 return fdt_get_property_namelen(fdt, nodeoffset, name,
267 strlen(name), lenp);
268}
269
270const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
271 const char *name, int namelen, int *lenp)
David Gibson3da0f9a2006-11-27 16:21:28 +1100272{
273 const struct fdt_property *prop;
David Gibson3da0f9a2006-11-27 16:21:28 +1100274
David Gibsoncb650ae2008-08-06 14:50:49 +1000275 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
David Gibsona7ee95d2006-12-15 15:12:49 +1100276 if (! prop)
277 return NULL;
David Gibson3da0f9a2006-11-27 16:21:28 +1100278
279 return prop->data;
280}
David Gibson037db262007-08-30 14:54:04 +1000281
David Gibsoncb650ae2008-08-06 14:50:49 +1000282const void *fdt_getprop(const void *fdt, int nodeoffset,
283 const char *name, int *lenp)
284{
285 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
286}
287
David Gibson73468582007-11-13 09:59:38 +1100288uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
289{
290 const uint32_t *php;
291 int len;
292
293 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
294 if (!php || (len != sizeof(*php)))
295 return 0;
296
297 return fdt32_to_cpu(*php);
298}
299
David Gibson9c831152008-08-20 16:55:14 +1000300const char *fdt_get_alias_namelen(const void *fdt,
301 const char *name, int namelen)
302{
303 int aliasoffset;
304
305 aliasoffset = fdt_path_offset(fdt, "/aliases");
306 if (aliasoffset < 0)
307 return NULL;
308
309 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
310}
311
312const char *fdt_get_alias(const void *fdt, const char *name)
313{
314 return fdt_get_alias_namelen(fdt, name, strlen(name));
315}
316
David Gibson037db262007-08-30 14:54:04 +1000317int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
318{
David Gibsonfc9769a2008-02-12 11:58:31 +1100319 int pdepth = 0, p = 0;
320 int offset, depth, namelen;
David Gibson037db262007-08-30 14:54:04 +1000321 const char *name;
322
David Gibsonb6d80a22008-07-09 14:10:24 +1000323 FDT_CHECK_HEADER(fdt);
David Gibson037db262007-08-30 14:54:04 +1000324
David Gibson037db262007-08-30 14:54:04 +1000325 if (buflen < 2)
326 return -FDT_ERR_NOSPACE;
David Gibson037db262007-08-30 14:54:04 +1000327
David Gibsonfc9769a2008-02-12 11:58:31 +1100328 for (offset = 0, depth = 0;
329 (offset >= 0) && (offset <= nodeoffset);
330 offset = fdt_next_node(fdt, offset, &depth)) {
David Gibsonfc9769a2008-02-12 11:58:31 +1100331 while (pdepth > depth) {
332 do {
333 p--;
334 } while (buf[p-1] != '/');
335 pdepth--;
336 }
337
David Gibson8daae142008-08-29 14:19:13 +1000338 if (pdepth >= depth) {
339 name = fdt_get_name(fdt, offset, &namelen);
340 if (!name)
341 return namelen;
342 if ((p + namelen + 1) <= buflen) {
343 memcpy(buf + p, name, namelen);
344 p += namelen;
345 buf[p++] = '/';
346 pdepth++;
347 }
David Gibsonfc9769a2008-02-12 11:58:31 +1100348 }
David Gibson037db262007-08-30 14:54:04 +1000349
David Gibsonfc9769a2008-02-12 11:58:31 +1100350 if (offset == nodeoffset) {
351 if (pdepth < (depth + 1))
352 return -FDT_ERR_NOSPACE;
353
354 if (p > 1) /* special case so that root path is "/", not "" */
David Gibson037db262007-08-30 14:54:04 +1000355 p--;
David Gibsonfc9769a2008-02-12 11:58:31 +1100356 buf[p] = '\0';
David Gibson8daae142008-08-29 14:19:13 +1000357 return 0;
David Gibson037db262007-08-30 14:54:04 +1000358 }
359 }
360
David Gibsonfc9769a2008-02-12 11:58:31 +1100361 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
362 return -FDT_ERR_BADOFFSET;
363 else if (offset == -FDT_ERR_BADOFFSET)
364 return -FDT_ERR_BADSTRUCTURE;
David Gibson037db262007-08-30 14:54:04 +1000365
David Gibsonfc9769a2008-02-12 11:58:31 +1100366 return offset; /* error from fdt_next_node() */
David Gibson037db262007-08-30 14:54:04 +1000367}
David Gibson12482372007-08-30 14:54:04 +1000368
369int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
370 int supernodedepth, int *nodedepth)
371{
David Gibsonfc9769a2008-02-12 11:58:31 +1100372 int offset, depth;
David Gibson12482372007-08-30 14:54:04 +1000373 int supernodeoffset = -FDT_ERR_INTERNAL;
374
David Gibsonb6d80a22008-07-09 14:10:24 +1000375 FDT_CHECK_HEADER(fdt);
David Gibson12482372007-08-30 14:54:04 +1000376
377 if (supernodedepth < 0)
378 return -FDT_ERR_NOTFOUND;
379
David Gibsonfc9769a2008-02-12 11:58:31 +1100380 for (offset = 0, depth = 0;
381 (offset >= 0) && (offset <= nodeoffset);
382 offset = fdt_next_node(fdt, offset, &depth)) {
383 if (depth == supernodedepth)
384 supernodeoffset = offset;
David Gibson12482372007-08-30 14:54:04 +1000385
David Gibsonfc9769a2008-02-12 11:58:31 +1100386 if (offset == nodeoffset) {
387 if (nodedepth)
388 *nodedepth = depth;
David Gibson12482372007-08-30 14:54:04 +1000389
David Gibsonfc9769a2008-02-12 11:58:31 +1100390 if (supernodedepth > depth)
391 return -FDT_ERR_NOTFOUND;
392 else
393 return supernodeoffset;
David Gibson12482372007-08-30 14:54:04 +1000394 }
David Gibsonfc9769a2008-02-12 11:58:31 +1100395 }
David Gibson12482372007-08-30 14:54:04 +1000396
David Gibsonfc9769a2008-02-12 11:58:31 +1100397 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
398 return -FDT_ERR_BADOFFSET;
399 else if (offset == -FDT_ERR_BADOFFSET)
400 return -FDT_ERR_BADSTRUCTURE;
David Gibson12482372007-08-30 14:54:04 +1000401
David Gibsonfc9769a2008-02-12 11:58:31 +1100402 return offset; /* error from fdt_next_node() */
David Gibson12482372007-08-30 14:54:04 +1000403}
404
405int fdt_node_depth(const void *fdt, int nodeoffset)
406{
407 int nodedepth;
408 int err;
409
410 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
411 if (err)
412 return (err < 0) ? err : -FDT_ERR_INTERNAL;
413 return nodedepth;
414}
415
416int fdt_parent_offset(const void *fdt, int nodeoffset)
417{
418 int nodedepth = fdt_node_depth(fdt, nodeoffset);
419
420 if (nodedepth < 0)
421 return nodedepth;
422 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
423 nodedepth - 1, NULL);
424}
David Gibsonae1454b2007-09-17 14:28:34 +1000425
426int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
427 const char *propname,
428 const void *propval, int proplen)
429{
David Gibsonfc9769a2008-02-12 11:58:31 +1100430 int offset;
David Gibsonae1454b2007-09-17 14:28:34 +1000431 const void *val;
432 int len;
433
David Gibsonb6d80a22008-07-09 14:10:24 +1000434 FDT_CHECK_HEADER(fdt);
David Gibsonae1454b2007-09-17 14:28:34 +1000435
David Gibsonae1454b2007-09-17 14:28:34 +1000436 /* FIXME: The algorithm here is pretty horrible: we scan each
437 * property of a node in fdt_getprop(), then if that didn't
438 * find what we want, we scan over them again making our way
439 * to the next node. Still it's the easiest to implement
440 * approach; performance can come later. */
David Gibsonfc9769a2008-02-12 11:58:31 +1100441 for (offset = fdt_next_node(fdt, startoffset, NULL);
442 offset >= 0;
443 offset = fdt_next_node(fdt, offset, NULL)) {
444 val = fdt_getprop(fdt, offset, propname, &len);
445 if (val && (len == proplen)
446 && (memcmp(val, propval, len) == 0))
447 return offset;
448 }
David Gibsonae1454b2007-09-17 14:28:34 +1000449
David Gibsonfc9769a2008-02-12 11:58:31 +1100450 return offset; /* error from fdt_next_node() */
David Gibsonae1454b2007-09-17 14:28:34 +1000451}
David Gibson333542f2007-10-16 13:58:25 +1000452
David Gibson73468582007-11-13 09:59:38 +1100453int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
454{
455 if ((phandle == 0) || (phandle == -1))
456 return -FDT_ERR_BADPHANDLE;
457 phandle = cpu_to_fdt32(phandle);
458 return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle",
459 &phandle, sizeof(phandle));
460}
461
David Gibsond5653612008-07-29 14:51:22 +1000462static int _fdt_stringlist_contains(const char *strlist, int listlen,
463 const char *str)
David Gibson333542f2007-10-16 13:58:25 +1000464{
465 int len = strlen(str);
David Gibson36786db2008-07-07 10:10:48 +1000466 const char *p;
David Gibson333542f2007-10-16 13:58:25 +1000467
468 while (listlen >= len) {
469 if (memcmp(str, strlist, len+1) == 0)
470 return 1;
471 p = memchr(strlist, '\0', listlen);
472 if (!p)
473 return 0; /* malformed strlist.. */
474 listlen -= (p-strlist) + 1;
475 strlist = p + 1;
476 }
477 return 0;
478}
479
480int fdt_node_check_compatible(const void *fdt, int nodeoffset,
481 const char *compatible)
482{
483 const void *prop;
484 int len;
485
486 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
487 if (!prop)
488 return len;
David Gibsond5653612008-07-29 14:51:22 +1000489 if (_fdt_stringlist_contains(prop, len, compatible))
David Gibson333542f2007-10-16 13:58:25 +1000490 return 0;
491 else
492 return 1;
493}
494
495int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
496 const char *compatible)
497{
David Gibson2512a7e2008-02-18 18:09:04 +1100498 int offset, err;
David Gibson333542f2007-10-16 13:58:25 +1000499
David Gibsonb6d80a22008-07-09 14:10:24 +1000500 FDT_CHECK_HEADER(fdt);
David Gibson333542f2007-10-16 13:58:25 +1000501
David Gibson333542f2007-10-16 13:58:25 +1000502 /* FIXME: The algorithm here is pretty horrible: we scan each
503 * property of a node in fdt_node_check_compatible(), then if
504 * that didn't find what we want, we scan over them again
505 * making our way to the next node. Still it's the easiest to
506 * implement approach; performance can come later. */
David Gibsonfc9769a2008-02-12 11:58:31 +1100507 for (offset = fdt_next_node(fdt, startoffset, NULL);
508 offset >= 0;
509 offset = fdt_next_node(fdt, offset, NULL)) {
510 err = fdt_node_check_compatible(fdt, offset, compatible);
511 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
512 return err;
513 else if (err == 0)
514 return offset;
515 }
David Gibson333542f2007-10-16 13:58:25 +1000516
David Gibsonfc9769a2008-02-12 11:58:31 +1100517 return offset; /* error from fdt_next_node() */
David Gibson333542f2007-10-16 13:58:25 +1000518}