blob: ebd12607d5f1eb3474ec748a5daf5805280929f8 [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
142 if (*path != '/')
David Gibson9a9fdf52006-12-15 15:12:51 +1100143 return -FDT_ERR_BADPATH;
David Gibson3da0f9a2006-11-27 16:21:28 +1100144
145 while (*p) {
146 const char *q;
147
148 while (*p == '/')
149 p++;
150 if (! *p)
David Gibsonbd2ae2f2007-08-29 12:22:50 +1000151 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +1100152 q = strchr(p, '/');
153 if (! q)
154 q = end;
155
156 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
David Gibson9a9fdf52006-12-15 15:12:51 +1100157 if (offset < 0)
David Gibson3da0f9a2006-11-27 16:21:28 +1100158 return offset;
159
160 p = q;
161 }
162
David Gibson63dc9c72007-09-18 11:44:04 +1000163 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +1100164}
165
David Gibson9d26eab2007-08-30 14:54:04 +1000166const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
167{
David Gibsonaa1baab2008-05-20 17:19:11 +1000168 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
David Gibson9d26eab2007-08-30 14:54:04 +1000169 int err;
170
David Gibsonaa1baab2008-05-20 17:19:11 +1000171 if (((err = fdt_check_header(fdt)) != 0)
172 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
173 goto fail;
David Gibson9d26eab2007-08-30 14:54:04 +1000174
175 if (len)
176 *len = strlen(nh->name);
177
178 return nh->name;
179
180 fail:
181 if (len)
182 *len = err;
183 return NULL;
184}
185
David Gibsoncb650ae2008-08-06 14:50:49 +1000186const struct fdt_property *fdt_get_property_namelen(const void *fdt,
187 int nodeoffset,
188 const char *name,
189 int namelen, int *lenp)
David Gibson3da0f9a2006-11-27 16:21:28 +1100190{
David Gibson94993f42006-12-11 16:15:34 +1100191 uint32_t tag;
David Gibsona6c76f92007-06-13 14:18:10 +1000192 const struct fdt_property *prop;
David Gibson94993f42006-12-11 16:15:34 +1100193 int namestroff;
194 int offset, nextoffset;
David Gibsona7ee95d2006-12-15 15:12:49 +1100195 int err;
David Gibson3da0f9a2006-11-27 16:21:28 +1100196
David Gibsonaa1baab2008-05-20 17:19:11 +1000197 if (((err = fdt_check_header(fdt)) != 0)
198 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
199 goto fail;
David Gibson3da0f9a2006-11-27 16:21:28 +1100200
David Gibsonaa1baab2008-05-20 17:19:11 +1000201 nextoffset = err;
David Gibson94993f42006-12-11 16:15:34 +1100202 do {
203 offset = nextoffset;
David Gibson94993f42006-12-11 16:15:34 +1100204
David Gibson3c44c872007-10-24 11:06:09 +1000205 tag = fdt_next_tag(fdt, offset, &nextoffset);
David Gibson94993f42006-12-11 16:15:34 +1100206 switch (tag) {
207 case FDT_END:
David Gibson9a9fdf52006-12-15 15:12:51 +1100208 err = -FDT_ERR_TRUNCATED;
David Gibsona7ee95d2006-12-15 15:12:49 +1100209 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100210
211 case FDT_BEGIN_NODE:
David Gibson94993f42006-12-11 16:15:34 +1100212 case FDT_END_NODE:
David Gibson592ea582007-09-04 10:43:03 +1000213 case FDT_NOP:
David Gibson94993f42006-12-11 16:15:34 +1100214 break;
215
216 case FDT_PROP:
David Gibson9a9fdf52006-12-15 15:12:51 +1100217 err = -FDT_ERR_BADSTRUCTURE;
David Gibson2cf86932007-11-19 17:26:22 +1100218 prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
David Gibson94993f42006-12-11 16:15:34 +1100219 if (! prop)
David Gibsona7ee95d2006-12-15 15:12:49 +1100220 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100221 namestroff = fdt32_to_cpu(prop->nameoff);
David Gibsoncb650ae2008-08-06 14:50:49 +1000222 if (_fdt_string_eq(fdt, namestroff, name, namelen)) {
David Gibson94993f42006-12-11 16:15:34 +1100223 /* Found it! */
224 int len = fdt32_to_cpu(prop->len);
225 prop = fdt_offset_ptr(fdt, offset,
David Gibson9825f822006-12-14 15:29:25 +1100226 sizeof(*prop)+len);
David Gibson94993f42006-12-11 16:15:34 +1100227 if (! prop)
David Gibsona7ee95d2006-12-15 15:12:49 +1100228 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100229
230 if (lenp)
231 *lenp = len;
David Gibson63dc9c72007-09-18 11:44:04 +1000232
David Gibson94993f42006-12-11 16:15:34 +1100233 return prop;
234 }
235 break;
236
David Gibson94993f42006-12-11 16:15:34 +1100237 default:
David Gibson9a9fdf52006-12-15 15:12:51 +1100238 err = -FDT_ERR_BADSTRUCTURE;
David Gibsona7ee95d2006-12-15 15:12:49 +1100239 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100240 }
David Gibson592ea582007-09-04 10:43:03 +1000241 } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
David Gibson94993f42006-12-11 16:15:34 +1100242
David Gibson9a9fdf52006-12-15 15:12:51 +1100243 err = -FDT_ERR_NOTFOUND;
David Gibsona7ee95d2006-12-15 15:12:49 +1100244 fail:
245 if (lenp)
David Gibson9a9fdf52006-12-15 15:12:51 +1100246 *lenp = err;
David Gibsona7ee95d2006-12-15 15:12:49 +1100247 return NULL;
David Gibson3da0f9a2006-11-27 16:21:28 +1100248}
249
David Gibsoncb650ae2008-08-06 14:50:49 +1000250const struct fdt_property *fdt_get_property(const void *fdt,
251 int nodeoffset,
252 const char *name, int *lenp)
253{
254 return fdt_get_property_namelen(fdt, nodeoffset, name,
255 strlen(name), lenp);
256}
257
258const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
259 const char *name, int namelen, int *lenp)
David Gibson3da0f9a2006-11-27 16:21:28 +1100260{
261 const struct fdt_property *prop;
David Gibson3da0f9a2006-11-27 16:21:28 +1100262
David Gibsoncb650ae2008-08-06 14:50:49 +1000263 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
David Gibsona7ee95d2006-12-15 15:12:49 +1100264 if (! prop)
265 return NULL;
David Gibson3da0f9a2006-11-27 16:21:28 +1100266
267 return prop->data;
268}
David Gibson037db262007-08-30 14:54:04 +1000269
David Gibsoncb650ae2008-08-06 14:50:49 +1000270const void *fdt_getprop(const void *fdt, int nodeoffset,
271 const char *name, int *lenp)
272{
273 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
274}
275
David Gibson73468582007-11-13 09:59:38 +1100276uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
277{
278 const uint32_t *php;
279 int len;
280
281 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
282 if (!php || (len != sizeof(*php)))
283 return 0;
284
285 return fdt32_to_cpu(*php);
286}
287
David Gibson037db262007-08-30 14:54:04 +1000288int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
289{
David Gibsonfc9769a2008-02-12 11:58:31 +1100290 int pdepth = 0, p = 0;
291 int offset, depth, namelen;
David Gibson037db262007-08-30 14:54:04 +1000292 const char *name;
293
David Gibsonb6d80a22008-07-09 14:10:24 +1000294 FDT_CHECK_HEADER(fdt);
David Gibson037db262007-08-30 14:54:04 +1000295
David Gibson037db262007-08-30 14:54:04 +1000296 if (buflen < 2)
297 return -FDT_ERR_NOSPACE;
David Gibson037db262007-08-30 14:54:04 +1000298
David Gibsonfc9769a2008-02-12 11:58:31 +1100299 for (offset = 0, depth = 0;
300 (offset >= 0) && (offset <= nodeoffset);
301 offset = fdt_next_node(fdt, offset, &depth)) {
302 if (pdepth < depth)
303 continue; /* overflowed buffer */
David Gibson037db262007-08-30 14:54:04 +1000304
David Gibsonfc9769a2008-02-12 11:58:31 +1100305 while (pdepth > depth) {
306 do {
307 p--;
308 } while (buf[p-1] != '/');
309 pdepth--;
310 }
311
312 name = fdt_get_name(fdt, offset, &namelen);
313 if (!name)
314 return namelen;
315 if ((p + namelen + 1) <= buflen) {
David Gibson037db262007-08-30 14:54:04 +1000316 memcpy(buf + p, name, namelen);
317 p += namelen;
318 buf[p++] = '/';
David Gibsonfc9769a2008-02-12 11:58:31 +1100319 pdepth++;
320 }
David Gibson037db262007-08-30 14:54:04 +1000321
David Gibsonfc9769a2008-02-12 11:58:31 +1100322 if (offset == nodeoffset) {
323 if (pdepth < (depth + 1))
324 return -FDT_ERR_NOSPACE;
325
326 if (p > 1) /* special case so that root path is "/", not "" */
David Gibson037db262007-08-30 14:54:04 +1000327 p--;
David Gibsonfc9769a2008-02-12 11:58:31 +1100328 buf[p] = '\0';
329 return p;
David Gibson037db262007-08-30 14:54:04 +1000330 }
331 }
332
David Gibsonfc9769a2008-02-12 11:58:31 +1100333 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
334 return -FDT_ERR_BADOFFSET;
335 else if (offset == -FDT_ERR_BADOFFSET)
336 return -FDT_ERR_BADSTRUCTURE;
David Gibson037db262007-08-30 14:54:04 +1000337
David Gibsonfc9769a2008-02-12 11:58:31 +1100338 return offset; /* error from fdt_next_node() */
David Gibson037db262007-08-30 14:54:04 +1000339}
David Gibson12482372007-08-30 14:54:04 +1000340
341int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
342 int supernodedepth, int *nodedepth)
343{
David Gibsonfc9769a2008-02-12 11:58:31 +1100344 int offset, depth;
David Gibson12482372007-08-30 14:54:04 +1000345 int supernodeoffset = -FDT_ERR_INTERNAL;
346
David Gibsonb6d80a22008-07-09 14:10:24 +1000347 FDT_CHECK_HEADER(fdt);
David Gibson12482372007-08-30 14:54:04 +1000348
349 if (supernodedepth < 0)
350 return -FDT_ERR_NOTFOUND;
351
David Gibsonfc9769a2008-02-12 11:58:31 +1100352 for (offset = 0, depth = 0;
353 (offset >= 0) && (offset <= nodeoffset);
354 offset = fdt_next_node(fdt, offset, &depth)) {
355 if (depth == supernodedepth)
356 supernodeoffset = offset;
David Gibson12482372007-08-30 14:54:04 +1000357
David Gibsonfc9769a2008-02-12 11:58:31 +1100358 if (offset == nodeoffset) {
359 if (nodedepth)
360 *nodedepth = depth;
David Gibson12482372007-08-30 14:54:04 +1000361
David Gibsonfc9769a2008-02-12 11:58:31 +1100362 if (supernodedepth > depth)
363 return -FDT_ERR_NOTFOUND;
364 else
365 return supernodeoffset;
David Gibson12482372007-08-30 14:54:04 +1000366 }
David Gibsonfc9769a2008-02-12 11:58:31 +1100367 }
David Gibson12482372007-08-30 14:54:04 +1000368
David Gibsonfc9769a2008-02-12 11:58:31 +1100369 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
370 return -FDT_ERR_BADOFFSET;
371 else if (offset == -FDT_ERR_BADOFFSET)
372 return -FDT_ERR_BADSTRUCTURE;
David Gibson12482372007-08-30 14:54:04 +1000373
David Gibsonfc9769a2008-02-12 11:58:31 +1100374 return offset; /* error from fdt_next_node() */
David Gibson12482372007-08-30 14:54:04 +1000375}
376
377int fdt_node_depth(const void *fdt, int nodeoffset)
378{
379 int nodedepth;
380 int err;
381
382 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
383 if (err)
384 return (err < 0) ? err : -FDT_ERR_INTERNAL;
385 return nodedepth;
386}
387
388int fdt_parent_offset(const void *fdt, int nodeoffset)
389{
390 int nodedepth = fdt_node_depth(fdt, nodeoffset);
391
392 if (nodedepth < 0)
393 return nodedepth;
394 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
395 nodedepth - 1, NULL);
396}
David Gibsonae1454b2007-09-17 14:28:34 +1000397
398int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
399 const char *propname,
400 const void *propval, int proplen)
401{
David Gibsonfc9769a2008-02-12 11:58:31 +1100402 int offset;
David Gibsonae1454b2007-09-17 14:28:34 +1000403 const void *val;
404 int len;
405
David Gibsonb6d80a22008-07-09 14:10:24 +1000406 FDT_CHECK_HEADER(fdt);
David Gibsonae1454b2007-09-17 14:28:34 +1000407
David Gibsonae1454b2007-09-17 14:28:34 +1000408 /* FIXME: The algorithm here is pretty horrible: we scan each
409 * property of a node in fdt_getprop(), then if that didn't
410 * find what we want, we scan over them again making our way
411 * to the next node. Still it's the easiest to implement
412 * approach; performance can come later. */
David Gibsonfc9769a2008-02-12 11:58:31 +1100413 for (offset = fdt_next_node(fdt, startoffset, NULL);
414 offset >= 0;
415 offset = fdt_next_node(fdt, offset, NULL)) {
416 val = fdt_getprop(fdt, offset, propname, &len);
417 if (val && (len == proplen)
418 && (memcmp(val, propval, len) == 0))
419 return offset;
420 }
David Gibsonae1454b2007-09-17 14:28:34 +1000421
David Gibsonfc9769a2008-02-12 11:58:31 +1100422 return offset; /* error from fdt_next_node() */
David Gibsonae1454b2007-09-17 14:28:34 +1000423}
David Gibson333542f2007-10-16 13:58:25 +1000424
David Gibson73468582007-11-13 09:59:38 +1100425int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
426{
427 if ((phandle == 0) || (phandle == -1))
428 return -FDT_ERR_BADPHANDLE;
429 phandle = cpu_to_fdt32(phandle);
430 return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle",
431 &phandle, sizeof(phandle));
432}
433
David Gibsond5653612008-07-29 14:51:22 +1000434static int _fdt_stringlist_contains(const char *strlist, int listlen,
435 const char *str)
David Gibson333542f2007-10-16 13:58:25 +1000436{
437 int len = strlen(str);
David Gibson36786db2008-07-07 10:10:48 +1000438 const char *p;
David Gibson333542f2007-10-16 13:58:25 +1000439
440 while (listlen >= len) {
441 if (memcmp(str, strlist, len+1) == 0)
442 return 1;
443 p = memchr(strlist, '\0', listlen);
444 if (!p)
445 return 0; /* malformed strlist.. */
446 listlen -= (p-strlist) + 1;
447 strlist = p + 1;
448 }
449 return 0;
450}
451
452int fdt_node_check_compatible(const void *fdt, int nodeoffset,
453 const char *compatible)
454{
455 const void *prop;
456 int len;
457
458 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
459 if (!prop)
460 return len;
David Gibsond5653612008-07-29 14:51:22 +1000461 if (_fdt_stringlist_contains(prop, len, compatible))
David Gibson333542f2007-10-16 13:58:25 +1000462 return 0;
463 else
464 return 1;
465}
466
467int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
468 const char *compatible)
469{
David Gibson2512a7e2008-02-18 18:09:04 +1100470 int offset, err;
David Gibson333542f2007-10-16 13:58:25 +1000471
David Gibsonb6d80a22008-07-09 14:10:24 +1000472 FDT_CHECK_HEADER(fdt);
David Gibson333542f2007-10-16 13:58:25 +1000473
David Gibson333542f2007-10-16 13:58:25 +1000474 /* FIXME: The algorithm here is pretty horrible: we scan each
475 * property of a node in fdt_node_check_compatible(), then if
476 * that didn't find what we want, we scan over them again
477 * making our way to the next node. Still it's the easiest to
478 * implement approach; performance can come later. */
David Gibsonfc9769a2008-02-12 11:58:31 +1100479 for (offset = fdt_next_node(fdt, startoffset, NULL);
480 offset >= 0;
481 offset = fdt_next_node(fdt, offset, NULL)) {
482 err = fdt_node_check_compatible(fdt, offset, compatible);
483 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
484 return err;
485 else if (err == 0)
486 return offset;
487 }
David Gibson333542f2007-10-16 13:58:25 +1000488
David Gibsonfc9769a2008-02-12 11:58:31 +1100489 return offset; /* error from fdt_next_node() */
David Gibson333542f2007-10-16 13:58:25 +1000490}