blob: 2f3ff487b242aacad28c92c01445277b889024a1 [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 != '/') {
144 const char *q;
145 int aliasoffset = fdt_path_offset(fdt, "/aliases");
146
147 if (aliasoffset < 0)
148 return -FDT_ERR_BADPATH;
149
150 q = strchr(path, '/');
151 if (!q)
152 q = end;
153
154 p = fdt_getprop_namelen(fdt, aliasoffset, path, q - p, NULL);
155 if (!p)
156 return -FDT_ERR_BADPATH;
157 offset = fdt_path_offset(fdt, p);
158
159 p = q;
160 }
David Gibson3da0f9a2006-11-27 16:21:28 +1100161
162 while (*p) {
163 const char *q;
164
165 while (*p == '/')
166 p++;
167 if (! *p)
David Gibsonbd2ae2f2007-08-29 12:22:50 +1000168 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +1100169 q = strchr(p, '/');
170 if (! q)
171 q = end;
172
173 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
David Gibson9a9fdf52006-12-15 15:12:51 +1100174 if (offset < 0)
David Gibson3da0f9a2006-11-27 16:21:28 +1100175 return offset;
176
177 p = q;
178 }
179
David Gibson63dc9c72007-09-18 11:44:04 +1000180 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +1100181}
182
David Gibson9d26eab2007-08-30 14:54:04 +1000183const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
184{
David Gibsonaa1baab2008-05-20 17:19:11 +1000185 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
David Gibson9d26eab2007-08-30 14:54:04 +1000186 int err;
187
David Gibsonaa1baab2008-05-20 17:19:11 +1000188 if (((err = fdt_check_header(fdt)) != 0)
189 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
190 goto fail;
David Gibson9d26eab2007-08-30 14:54:04 +1000191
192 if (len)
193 *len = strlen(nh->name);
194
195 return nh->name;
196
197 fail:
198 if (len)
199 *len = err;
200 return NULL;
201}
202
David Gibsoncb650ae2008-08-06 14:50:49 +1000203const struct fdt_property *fdt_get_property_namelen(const void *fdt,
204 int nodeoffset,
205 const char *name,
206 int namelen, int *lenp)
David Gibson3da0f9a2006-11-27 16:21:28 +1100207{
David Gibson94993f42006-12-11 16:15:34 +1100208 uint32_t tag;
David Gibsona6c76f92007-06-13 14:18:10 +1000209 const struct fdt_property *prop;
David Gibson94993f42006-12-11 16:15:34 +1100210 int namestroff;
211 int offset, nextoffset;
David Gibsona7ee95d2006-12-15 15:12:49 +1100212 int err;
David Gibson3da0f9a2006-11-27 16:21:28 +1100213
David Gibsonaa1baab2008-05-20 17:19:11 +1000214 if (((err = fdt_check_header(fdt)) != 0)
215 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
216 goto fail;
David Gibson3da0f9a2006-11-27 16:21:28 +1100217
David Gibsonaa1baab2008-05-20 17:19:11 +1000218 nextoffset = err;
David Gibson94993f42006-12-11 16:15:34 +1100219 do {
220 offset = nextoffset;
David Gibson94993f42006-12-11 16:15:34 +1100221
David Gibson3c44c872007-10-24 11:06:09 +1000222 tag = fdt_next_tag(fdt, offset, &nextoffset);
David Gibson94993f42006-12-11 16:15:34 +1100223 switch (tag) {
224 case FDT_END:
David Gibson9a9fdf52006-12-15 15:12:51 +1100225 err = -FDT_ERR_TRUNCATED;
David Gibsona7ee95d2006-12-15 15:12:49 +1100226 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100227
228 case FDT_BEGIN_NODE:
David Gibson94993f42006-12-11 16:15:34 +1100229 case FDT_END_NODE:
David Gibson592ea582007-09-04 10:43:03 +1000230 case FDT_NOP:
David Gibson94993f42006-12-11 16:15:34 +1100231 break;
232
233 case FDT_PROP:
David Gibson9a9fdf52006-12-15 15:12:51 +1100234 err = -FDT_ERR_BADSTRUCTURE;
David Gibson2cf86932007-11-19 17:26:22 +1100235 prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
David Gibson94993f42006-12-11 16:15:34 +1100236 if (! prop)
David Gibsona7ee95d2006-12-15 15:12:49 +1100237 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100238 namestroff = fdt32_to_cpu(prop->nameoff);
David Gibsoncb650ae2008-08-06 14:50:49 +1000239 if (_fdt_string_eq(fdt, namestroff, name, namelen)) {
David Gibson94993f42006-12-11 16:15:34 +1100240 /* Found it! */
241 int len = fdt32_to_cpu(prop->len);
242 prop = fdt_offset_ptr(fdt, offset,
David Gibson9825f822006-12-14 15:29:25 +1100243 sizeof(*prop)+len);
David Gibson94993f42006-12-11 16:15:34 +1100244 if (! prop)
David Gibsona7ee95d2006-12-15 15:12:49 +1100245 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100246
247 if (lenp)
248 *lenp = len;
David Gibson63dc9c72007-09-18 11:44:04 +1000249
David Gibson94993f42006-12-11 16:15:34 +1100250 return prop;
251 }
252 break;
253
David Gibson94993f42006-12-11 16:15:34 +1100254 default:
David Gibson9a9fdf52006-12-15 15:12:51 +1100255 err = -FDT_ERR_BADSTRUCTURE;
David Gibsona7ee95d2006-12-15 15:12:49 +1100256 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100257 }
David Gibson592ea582007-09-04 10:43:03 +1000258 } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
David Gibson94993f42006-12-11 16:15:34 +1100259
David Gibson9a9fdf52006-12-15 15:12:51 +1100260 err = -FDT_ERR_NOTFOUND;
David Gibsona7ee95d2006-12-15 15:12:49 +1100261 fail:
262 if (lenp)
David Gibson9a9fdf52006-12-15 15:12:51 +1100263 *lenp = err;
David Gibsona7ee95d2006-12-15 15:12:49 +1100264 return NULL;
David Gibson3da0f9a2006-11-27 16:21:28 +1100265}
266
David Gibsoncb650ae2008-08-06 14:50:49 +1000267const struct fdt_property *fdt_get_property(const void *fdt,
268 int nodeoffset,
269 const char *name, int *lenp)
270{
271 return fdt_get_property_namelen(fdt, nodeoffset, name,
272 strlen(name), lenp);
273}
274
275const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
276 const char *name, int namelen, int *lenp)
David Gibson3da0f9a2006-11-27 16:21:28 +1100277{
278 const struct fdt_property *prop;
David Gibson3da0f9a2006-11-27 16:21:28 +1100279
David Gibsoncb650ae2008-08-06 14:50:49 +1000280 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
David Gibsona7ee95d2006-12-15 15:12:49 +1100281 if (! prop)
282 return NULL;
David Gibson3da0f9a2006-11-27 16:21:28 +1100283
284 return prop->data;
285}
David Gibson037db262007-08-30 14:54:04 +1000286
David Gibsoncb650ae2008-08-06 14:50:49 +1000287const void *fdt_getprop(const void *fdt, int nodeoffset,
288 const char *name, int *lenp)
289{
290 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
291}
292
David Gibson73468582007-11-13 09:59:38 +1100293uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
294{
295 const uint32_t *php;
296 int len;
297
298 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
299 if (!php || (len != sizeof(*php)))
300 return 0;
301
302 return fdt32_to_cpu(*php);
303}
304
David Gibson037db262007-08-30 14:54:04 +1000305int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
306{
David Gibsonfc9769a2008-02-12 11:58:31 +1100307 int pdepth = 0, p = 0;
308 int offset, depth, namelen;
David Gibson037db262007-08-30 14:54:04 +1000309 const char *name;
310
David Gibsonb6d80a22008-07-09 14:10:24 +1000311 FDT_CHECK_HEADER(fdt);
David Gibson037db262007-08-30 14:54:04 +1000312
David Gibson037db262007-08-30 14:54:04 +1000313 if (buflen < 2)
314 return -FDT_ERR_NOSPACE;
David Gibson037db262007-08-30 14:54:04 +1000315
David Gibsonfc9769a2008-02-12 11:58:31 +1100316 for (offset = 0, depth = 0;
317 (offset >= 0) && (offset <= nodeoffset);
318 offset = fdt_next_node(fdt, offset, &depth)) {
319 if (pdepth < depth)
320 continue; /* overflowed buffer */
David Gibson037db262007-08-30 14:54:04 +1000321
David Gibsonfc9769a2008-02-12 11:58:31 +1100322 while (pdepth > depth) {
323 do {
324 p--;
325 } while (buf[p-1] != '/');
326 pdepth--;
327 }
328
329 name = fdt_get_name(fdt, offset, &namelen);
330 if (!name)
331 return namelen;
332 if ((p + namelen + 1) <= buflen) {
David Gibson037db262007-08-30 14:54:04 +1000333 memcpy(buf + p, name, namelen);
334 p += namelen;
335 buf[p++] = '/';
David Gibsonfc9769a2008-02-12 11:58:31 +1100336 pdepth++;
337 }
David Gibson037db262007-08-30 14:54:04 +1000338
David Gibsonfc9769a2008-02-12 11:58:31 +1100339 if (offset == nodeoffset) {
340 if (pdepth < (depth + 1))
341 return -FDT_ERR_NOSPACE;
342
343 if (p > 1) /* special case so that root path is "/", not "" */
David Gibson037db262007-08-30 14:54:04 +1000344 p--;
David Gibsonfc9769a2008-02-12 11:58:31 +1100345 buf[p] = '\0';
346 return p;
David Gibson037db262007-08-30 14:54:04 +1000347 }
348 }
349
David Gibsonfc9769a2008-02-12 11:58:31 +1100350 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
351 return -FDT_ERR_BADOFFSET;
352 else if (offset == -FDT_ERR_BADOFFSET)
353 return -FDT_ERR_BADSTRUCTURE;
David Gibson037db262007-08-30 14:54:04 +1000354
David Gibsonfc9769a2008-02-12 11:58:31 +1100355 return offset; /* error from fdt_next_node() */
David Gibson037db262007-08-30 14:54:04 +1000356}
David Gibson12482372007-08-30 14:54:04 +1000357
358int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
359 int supernodedepth, int *nodedepth)
360{
David Gibsonfc9769a2008-02-12 11:58:31 +1100361 int offset, depth;
David Gibson12482372007-08-30 14:54:04 +1000362 int supernodeoffset = -FDT_ERR_INTERNAL;
363
David Gibsonb6d80a22008-07-09 14:10:24 +1000364 FDT_CHECK_HEADER(fdt);
David Gibson12482372007-08-30 14:54:04 +1000365
366 if (supernodedepth < 0)
367 return -FDT_ERR_NOTFOUND;
368
David Gibsonfc9769a2008-02-12 11:58:31 +1100369 for (offset = 0, depth = 0;
370 (offset >= 0) && (offset <= nodeoffset);
371 offset = fdt_next_node(fdt, offset, &depth)) {
372 if (depth == supernodedepth)
373 supernodeoffset = offset;
David Gibson12482372007-08-30 14:54:04 +1000374
David Gibsonfc9769a2008-02-12 11:58:31 +1100375 if (offset == nodeoffset) {
376 if (nodedepth)
377 *nodedepth = depth;
David Gibson12482372007-08-30 14:54:04 +1000378
David Gibsonfc9769a2008-02-12 11:58:31 +1100379 if (supernodedepth > depth)
380 return -FDT_ERR_NOTFOUND;
381 else
382 return supernodeoffset;
David Gibson12482372007-08-30 14:54:04 +1000383 }
David Gibsonfc9769a2008-02-12 11:58:31 +1100384 }
David Gibson12482372007-08-30 14:54:04 +1000385
David Gibsonfc9769a2008-02-12 11:58:31 +1100386 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
387 return -FDT_ERR_BADOFFSET;
388 else if (offset == -FDT_ERR_BADOFFSET)
389 return -FDT_ERR_BADSTRUCTURE;
David Gibson12482372007-08-30 14:54:04 +1000390
David Gibsonfc9769a2008-02-12 11:58:31 +1100391 return offset; /* error from fdt_next_node() */
David Gibson12482372007-08-30 14:54:04 +1000392}
393
394int fdt_node_depth(const void *fdt, int nodeoffset)
395{
396 int nodedepth;
397 int err;
398
399 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
400 if (err)
401 return (err < 0) ? err : -FDT_ERR_INTERNAL;
402 return nodedepth;
403}
404
405int fdt_parent_offset(const void *fdt, int nodeoffset)
406{
407 int nodedepth = fdt_node_depth(fdt, nodeoffset);
408
409 if (nodedepth < 0)
410 return nodedepth;
411 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
412 nodedepth - 1, NULL);
413}
David Gibsonae1454b2007-09-17 14:28:34 +1000414
415int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
416 const char *propname,
417 const void *propval, int proplen)
418{
David Gibsonfc9769a2008-02-12 11:58:31 +1100419 int offset;
David Gibsonae1454b2007-09-17 14:28:34 +1000420 const void *val;
421 int len;
422
David Gibsonb6d80a22008-07-09 14:10:24 +1000423 FDT_CHECK_HEADER(fdt);
David Gibsonae1454b2007-09-17 14:28:34 +1000424
David Gibsonae1454b2007-09-17 14:28:34 +1000425 /* FIXME: The algorithm here is pretty horrible: we scan each
426 * property of a node in fdt_getprop(), then if that didn't
427 * find what we want, we scan over them again making our way
428 * to the next node. Still it's the easiest to implement
429 * approach; performance can come later. */
David Gibsonfc9769a2008-02-12 11:58:31 +1100430 for (offset = fdt_next_node(fdt, startoffset, NULL);
431 offset >= 0;
432 offset = fdt_next_node(fdt, offset, NULL)) {
433 val = fdt_getprop(fdt, offset, propname, &len);
434 if (val && (len == proplen)
435 && (memcmp(val, propval, len) == 0))
436 return offset;
437 }
David Gibsonae1454b2007-09-17 14:28:34 +1000438
David Gibsonfc9769a2008-02-12 11:58:31 +1100439 return offset; /* error from fdt_next_node() */
David Gibsonae1454b2007-09-17 14:28:34 +1000440}
David Gibson333542f2007-10-16 13:58:25 +1000441
David Gibson73468582007-11-13 09:59:38 +1100442int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
443{
444 if ((phandle == 0) || (phandle == -1))
445 return -FDT_ERR_BADPHANDLE;
446 phandle = cpu_to_fdt32(phandle);
447 return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle",
448 &phandle, sizeof(phandle));
449}
450
David Gibsond5653612008-07-29 14:51:22 +1000451static int _fdt_stringlist_contains(const char *strlist, int listlen,
452 const char *str)
David Gibson333542f2007-10-16 13:58:25 +1000453{
454 int len = strlen(str);
David Gibson36786db2008-07-07 10:10:48 +1000455 const char *p;
David Gibson333542f2007-10-16 13:58:25 +1000456
457 while (listlen >= len) {
458 if (memcmp(str, strlist, len+1) == 0)
459 return 1;
460 p = memchr(strlist, '\0', listlen);
461 if (!p)
462 return 0; /* malformed strlist.. */
463 listlen -= (p-strlist) + 1;
464 strlist = p + 1;
465 }
466 return 0;
467}
468
469int fdt_node_check_compatible(const void *fdt, int nodeoffset,
470 const char *compatible)
471{
472 const void *prop;
473 int len;
474
475 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
476 if (!prop)
477 return len;
David Gibsond5653612008-07-29 14:51:22 +1000478 if (_fdt_stringlist_contains(prop, len, compatible))
David Gibson333542f2007-10-16 13:58:25 +1000479 return 0;
480 else
481 return 1;
482}
483
484int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
485 const char *compatible)
486{
David Gibson2512a7e2008-02-18 18:09:04 +1100487 int offset, err;
David Gibson333542f2007-10-16 13:58:25 +1000488
David Gibsonb6d80a22008-07-09 14:10:24 +1000489 FDT_CHECK_HEADER(fdt);
David Gibson333542f2007-10-16 13:58:25 +1000490
David Gibson333542f2007-10-16 13:58:25 +1000491 /* FIXME: The algorithm here is pretty horrible: we scan each
492 * property of a node in fdt_node_check_compatible(), then if
493 * that didn't find what we want, we scan over them again
494 * making our way to the next node. Still it's the easiest to
495 * implement approach; performance can come later. */
David Gibsonfc9769a2008-02-12 11:58:31 +1100496 for (offset = fdt_next_node(fdt, startoffset, NULL);
497 offset >= 0;
498 offset = fdt_next_node(fdt, offset, NULL)) {
499 err = fdt_node_check_compatible(fdt, offset, compatible);
500 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
501 return err;
502 else if (err == 0)
503 return offset;
504 }
David Gibson333542f2007-10-16 13:58:25 +1000505
David Gibsonfc9769a2008-02-12 11:58:31 +1100506 return offset; /* error from fdt_next_node() */
David Gibson333542f2007-10-16 13:58:25 +1000507}