blob: d682a40c1451e5aec7484545d7a32be4cf2a3901 [file] [log] [blame]
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
Kumar Galac8ab7052007-10-24 11:04:22 -05005 * libfdt is dual licensed: you can use it either under the terms of
6 * the GPL, or the BSD license, at your option.
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04007 *
Kumar Galac8ab7052007-10-24 11:04:22 -05008 * 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.
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040012 *
Kumar Galac8ab7052007-10-24 11:04:22 -050013 * 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.
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040050 */
51#include "libfdt_env.h"
52
Bartlomiej Sieka61250862008-02-29 16:00:24 +010053#ifndef USE_HOSTCC
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040054#include <fdt.h>
55#include <libfdt.h>
Bartlomiej Sieka61250862008-02-29 16:00:24 +010056#else
57#include "fdt_host.h"
58#endif
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040059
60#include "libfdt_internal.h"
61
David Gibson9457d9d2008-07-09 14:10:24 +100062static int _fdt_nodename_eq(const void *fdt, int offset,
63 const char *s, int len)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040064{
David Gibsond9d793c2008-02-12 11:58:31 +110065 const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040066
67 if (! p)
68 /* short match */
69 return 0;
70
71 if (memcmp(p, s, len) != 0)
72 return 0;
73
Kumar Galac8ab7052007-10-24 11:04:22 -050074 if (p[len] == '\0')
75 return 1;
76 else if (!memchr(s, '@', len) && (p[len] == '@'))
77 return 1;
78 else
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040079 return 0;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040080}
81
Kumar Galac8ab7052007-10-24 11:04:22 -050082const char *fdt_string(const void *fdt, int stroffset)
Gerald Van Baren18c75c62007-05-17 23:54:36 -040083{
David Gibson870fa472008-07-07 10:14:15 +100084 return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
Kumar Galac8ab7052007-10-24 11:04:22 -050085}
Gerald Van Baren18c75c62007-05-17 23:54:36 -040086
David Gibsonc5b130e2008-08-06 14:50:49 +100087static int _fdt_string_eq(const void *fdt, int stroffset,
88 const char *s, int len)
89{
90 const char *p = fdt_string(fdt, stroffset);
91
92 return (strlen(p) == len) && (memcmp(p, s, len) == 0);
93}
94
Kumar Galac8ab7052007-10-24 11:04:22 -050095int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
96{
David Gibson9457d9d2008-07-09 14:10:24 +100097 FDT_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -050098 *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
99 *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
Gerald Van Baren18c75c62007-05-17 23:54:36 -0400100 return 0;
101}
102
Kumar Galac8ab7052007-10-24 11:04:22 -0500103int fdt_num_mem_rsv(const void *fdt)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400104{
Kumar Galac8ab7052007-10-24 11:04:22 -0500105 int i = 0;
106
107 while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
108 i++;
109 return i;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400110}
111
David Gibsond9d793c2008-02-12 11:58:31 +1100112int fdt_subnode_offset_namelen(const void *fdt, int offset,
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400113 const char *name, int namelen)
114{
David Gibson954914a2009-02-06 14:01:56 +1100115 int depth;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400116
David Gibson9457d9d2008-07-09 14:10:24 +1000117 FDT_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400118
David Gibson954914a2009-02-06 14:01:56 +1100119 for (depth = 0;
120 (offset >= 0) && (depth >= 0);
121 offset = fdt_next_node(fdt, offset, &depth))
122 if ((depth == 1)
123 && _fdt_nodename_eq(fdt, offset, name, namelen))
David Gibsond9d793c2008-02-12 11:58:31 +1100124 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400125
David Gibson954914a2009-02-06 14:01:56 +1100126 if (depth < 0)
David Gibson1c0320e2008-10-29 23:27:45 -0500127 return -FDT_ERR_NOTFOUND;
David Gibson954914a2009-02-06 14:01:56 +1100128 return offset; /* error */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400129}
130
131int fdt_subnode_offset(const void *fdt, int parentoffset,
132 const char *name)
133{
134 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
135}
136
Kumar Galac8ab7052007-10-24 11:04:22 -0500137int fdt_path_offset(const void *fdt, const char *path)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400138{
139 const char *end = path + strlen(path);
140 const char *p = path;
141 int offset = 0;
142
David Gibson9457d9d2008-07-09 14:10:24 +1000143 FDT_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400144
Kumar Galaeb45e032008-08-14 08:28:19 -0500145 /* see if we have an alias */
146 if (*path != '/') {
David Gibsond6656ea2008-08-20 16:55:14 +1000147 const char *q = strchr(path, '/');
Kumar Galaeb45e032008-08-14 08:28:19 -0500148
Kumar Galaeb45e032008-08-14 08:28:19 -0500149 if (!q)
150 q = end;
151
David Gibsond6656ea2008-08-20 16:55:14 +1000152 p = fdt_get_alias_namelen(fdt, p, q - p);
Kumar Galaeb45e032008-08-14 08:28:19 -0500153 if (!p)
154 return -FDT_ERR_BADPATH;
155 offset = fdt_path_offset(fdt, p);
156
157 p = q;
158 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400159
160 while (*p) {
161 const char *q;
162
163 while (*p == '/')
164 p++;
165 if (! *p)
Kumar Galac8ab7052007-10-24 11:04:22 -0500166 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400167 q = strchr(p, '/');
168 if (! q)
169 q = end;
170
171 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
172 if (offset < 0)
173 return offset;
174
175 p = q;
176 }
177
Gerald Van Baren32480892007-03-31 14:30:53 -0400178 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400179}
180
Kumar Galac8ab7052007-10-24 11:04:22 -0500181const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400182{
David Gibson148a49b2008-05-20 17:19:11 +1000183 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
Kumar Galac8ab7052007-10-24 11:04:22 -0500184 int err;
185
David Gibson148a49b2008-05-20 17:19:11 +1000186 if (((err = fdt_check_header(fdt)) != 0)
187 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
188 goto fail;
Kumar Galac8ab7052007-10-24 11:04:22 -0500189
190 if (len)
191 *len = strlen(nh->name);
192
193 return nh->name;
194
195 fail:
196 if (len)
197 *len = err;
198 return NULL;
199}
200
David Gibsonc5b130e2008-08-06 14:50:49 +1000201const struct fdt_property *fdt_get_property_namelen(const void *fdt,
202 int nodeoffset,
203 const char *name,
204 int namelen, int *lenp)
Kumar Galac8ab7052007-10-24 11:04:22 -0500205{
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400206 uint32_t tag;
Kumar Galac8ab7052007-10-24 11:04:22 -0500207 const struct fdt_property *prop;
208 int namestroff;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400209 int offset, nextoffset;
210 int err;
211
David Gibson148a49b2008-05-20 17:19:11 +1000212 if (((err = fdt_check_header(fdt)) != 0)
213 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
214 goto fail;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400215
David Gibson148a49b2008-05-20 17:19:11 +1000216 nextoffset = err;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400217 do {
218 offset = nextoffset;
219
Kumar Galac8ab7052007-10-24 11:04:22 -0500220 tag = fdt_next_tag(fdt, offset, &nextoffset);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400221 switch (tag) {
222 case FDT_END:
223 err = -FDT_ERR_TRUNCATED;
224 goto fail;
225
226 case FDT_BEGIN_NODE:
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400227 case FDT_END_NODE:
Kumar Galac8ab7052007-10-24 11:04:22 -0500228 case FDT_NOP:
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400229 break;
230
231 case FDT_PROP:
Kumar Galac8ab7052007-10-24 11:04:22 -0500232 err = -FDT_ERR_BADSTRUCTURE;
233 prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
234 if (! prop)
Gerald Van Baren18c75c62007-05-17 23:54:36 -0400235 goto fail;
Kumar Galac8ab7052007-10-24 11:04:22 -0500236 namestroff = fdt32_to_cpu(prop->nameoff);
David Gibsonc5b130e2008-08-06 14:50:49 +1000237 if (_fdt_string_eq(fdt, namestroff, name, namelen)) {
Kumar Galac8ab7052007-10-24 11:04:22 -0500238 /* Found it! */
239 int len = fdt32_to_cpu(prop->len);
240 prop = fdt_offset_ptr(fdt, offset,
241 sizeof(*prop)+len);
242 if (! prop)
243 goto fail;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400244
Kumar Galac8ab7052007-10-24 11:04:22 -0500245 if (lenp)
246 *lenp = len;
247
248 return prop;
249 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400250 break;
251
252 default:
253 err = -FDT_ERR_BADSTRUCTURE;
254 goto fail;
255 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500256 } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400257
258 err = -FDT_ERR_NOTFOUND;
Kumar Galac8ab7052007-10-24 11:04:22 -0500259 fail:
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400260 if (lenp)
261 *lenp = err;
262 return NULL;
263}
264
David Gibsonc5b130e2008-08-06 14:50:49 +1000265const struct fdt_property *fdt_get_property(const void *fdt,
266 int nodeoffset,
267 const char *name, int *lenp)
268{
269 return fdt_get_property_namelen(fdt, nodeoffset, name,
270 strlen(name), lenp);
271}
272
273const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
274 const char *name, int namelen, int *lenp)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400275{
276 const struct fdt_property *prop;
277
David Gibsonc5b130e2008-08-06 14:50:49 +1000278 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400279 if (! prop)
280 return NULL;
281
Kumar Galac8ab7052007-10-24 11:04:22 -0500282 return prop->data;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400283}
284
David Gibsonc5b130e2008-08-06 14:50:49 +1000285const void *fdt_getprop(const void *fdt, int nodeoffset,
286 const char *name, int *lenp)
287{
288 return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
289}
290
Kumar Galac8ab7052007-10-24 11:04:22 -0500291uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
292{
293 const uint32_t *php;
294 int len;
295
296 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
297 if (!php || (len != sizeof(*php)))
298 return 0;
299
300 return fdt32_to_cpu(*php);
301}
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400302
David Gibsond6656ea2008-08-20 16:55:14 +1000303const char *fdt_get_alias_namelen(const void *fdt,
304 const char *name, int namelen)
305{
306 int aliasoffset;
307
308 aliasoffset = fdt_path_offset(fdt, "/aliases");
309 if (aliasoffset < 0)
310 return NULL;
311
312 return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
313}
314
315const char *fdt_get_alias(const void *fdt, const char *name)
316{
317 return fdt_get_alias_namelen(fdt, name, strlen(name));
318}
319
Kumar Galac8ab7052007-10-24 11:04:22 -0500320int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400321{
David Gibsond9d793c2008-02-12 11:58:31 +1100322 int pdepth = 0, p = 0;
323 int offset, depth, namelen;
Kumar Galac8ab7052007-10-24 11:04:22 -0500324 const char *name;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400325
David Gibson9457d9d2008-07-09 14:10:24 +1000326 FDT_CHECK_HEADER(fdt);
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400327
Kumar Galac8ab7052007-10-24 11:04:22 -0500328 if (buflen < 2)
329 return -FDT_ERR_NOSPACE;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400330
David Gibsond9d793c2008-02-12 11:58:31 +1100331 for (offset = 0, depth = 0;
332 (offset >= 0) && (offset <= nodeoffset);
333 offset = fdt_next_node(fdt, offset, &depth)) {
David Gibsond9d793c2008-02-12 11:58:31 +1100334 while (pdepth > depth) {
335 do {
336 p--;
337 } while (buf[p-1] != '/');
338 pdepth--;
339 }
340
David Gibson8d2810d2008-08-29 14:19:13 +1000341 if (pdepth >= depth) {
342 name = fdt_get_name(fdt, offset, &namelen);
343 if (!name)
344 return namelen;
345 if ((p + namelen + 1) <= buflen) {
346 memcpy(buf + p, name, namelen);
347 p += namelen;
348 buf[p++] = '/';
349 pdepth++;
350 }
David Gibsond9d793c2008-02-12 11:58:31 +1100351 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500352
David Gibsond9d793c2008-02-12 11:58:31 +1100353 if (offset == nodeoffset) {
354 if (pdepth < (depth + 1))
355 return -FDT_ERR_NOSPACE;
Kumar Galac8ab7052007-10-24 11:04:22 -0500356
David Gibsond9d793c2008-02-12 11:58:31 +1100357 if (p > 1) /* special case so that root path is "/", not "" */
358 p--;
359 buf[p] = '\0';
David Gibson8d2810d2008-08-29 14:19:13 +1000360 return 0;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400361 }
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400362 }
363
David Gibsond9d793c2008-02-12 11:58:31 +1100364 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
365 return -FDT_ERR_BADOFFSET;
366 else if (offset == -FDT_ERR_BADOFFSET)
367 return -FDT_ERR_BADSTRUCTURE;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400368
David Gibsond9d793c2008-02-12 11:58:31 +1100369 return offset; /* error from fdt_next_node() */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400370}
Gerald Van Barenc077a132007-04-14 22:46:41 -0400371
Kumar Galac8ab7052007-10-24 11:04:22 -0500372int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
373 int supernodedepth, int *nodedepth)
Gerald Van Barenc077a132007-04-14 22:46:41 -0400374{
David Gibsond9d793c2008-02-12 11:58:31 +1100375 int offset, depth;
Kumar Galac8ab7052007-10-24 11:04:22 -0500376 int supernodeoffset = -FDT_ERR_INTERNAL;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400377
David Gibson9457d9d2008-07-09 14:10:24 +1000378 FDT_CHECK_HEADER(fdt);
Gerald Van Barenc077a132007-04-14 22:46:41 -0400379
Kumar Galac8ab7052007-10-24 11:04:22 -0500380 if (supernodedepth < 0)
381 return -FDT_ERR_NOTFOUND;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400382
David Gibsond9d793c2008-02-12 11:58:31 +1100383 for (offset = 0, depth = 0;
384 (offset >= 0) && (offset <= nodeoffset);
385 offset = fdt_next_node(fdt, offset, &depth)) {
386 if (depth == supernodedepth)
387 supernodeoffset = offset;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400388
David Gibsond9d793c2008-02-12 11:58:31 +1100389 if (offset == nodeoffset) {
390 if (nodedepth)
391 *nodedepth = depth;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400392
David Gibsond9d793c2008-02-12 11:58:31 +1100393 if (supernodedepth > depth)
394 return -FDT_ERR_NOTFOUND;
395 else
396 return supernodeoffset;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400397 }
David Gibsond9d793c2008-02-12 11:58:31 +1100398 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500399
David Gibsond9d793c2008-02-12 11:58:31 +1100400 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
401 return -FDT_ERR_BADOFFSET;
402 else if (offset == -FDT_ERR_BADOFFSET)
403 return -FDT_ERR_BADSTRUCTURE;
Kumar Galac8ab7052007-10-24 11:04:22 -0500404
David Gibsond9d793c2008-02-12 11:58:31 +1100405 return offset; /* error from fdt_next_node() */
Kumar Galac8ab7052007-10-24 11:04:22 -0500406}
407
408int fdt_node_depth(const void *fdt, int nodeoffset)
409{
410 int nodedepth;
411 int err;
412
413 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
414 if (err)
415 return (err < 0) ? err : -FDT_ERR_INTERNAL;
416 return nodedepth;
417}
418
419int fdt_parent_offset(const void *fdt, int nodeoffset)
420{
421 int nodedepth = fdt_node_depth(fdt, nodeoffset);
422
423 if (nodedepth < 0)
424 return nodedepth;
425 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
426 nodedepth - 1, NULL);
427}
428
429int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
430 const char *propname,
431 const void *propval, int proplen)
432{
David Gibsond9d793c2008-02-12 11:58:31 +1100433 int offset;
Kumar Galac8ab7052007-10-24 11:04:22 -0500434 const void *val;
435 int len;
436
David Gibson9457d9d2008-07-09 14:10:24 +1000437 FDT_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500438
Kumar Galac8ab7052007-10-24 11:04:22 -0500439 /* FIXME: The algorithm here is pretty horrible: we scan each
440 * property of a node in fdt_getprop(), then if that didn't
441 * find what we want, we scan over them again making our way
442 * to the next node. Still it's the easiest to implement
443 * approach; performance can come later. */
David Gibsond9d793c2008-02-12 11:58:31 +1100444 for (offset = fdt_next_node(fdt, startoffset, NULL);
445 offset >= 0;
446 offset = fdt_next_node(fdt, offset, NULL)) {
447 val = fdt_getprop(fdt, offset, propname, &len);
448 if (val && (len == proplen)
449 && (memcmp(val, propval, len) == 0))
450 return offset;
451 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500452
David Gibsond9d793c2008-02-12 11:58:31 +1100453 return offset; /* error from fdt_next_node() */
Gerald Van Barenc077a132007-04-14 22:46:41 -0400454}
455
Kumar Galac8ab7052007-10-24 11:04:22 -0500456int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
Gerald Van Barenc077a132007-04-14 22:46:41 -0400457{
Kumar Galac8ab7052007-10-24 11:04:22 -0500458 if ((phandle == 0) || (phandle == -1))
459 return -FDT_ERR_BADPHANDLE;
460 phandle = cpu_to_fdt32(phandle);
461 return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle",
462 &phandle, sizeof(phandle));
463}
Gerald Van Barenc077a132007-04-14 22:46:41 -0400464
David Gibson0a1617a2008-07-29 14:51:22 +1000465static int _fdt_stringlist_contains(const char *strlist, int listlen,
466 const char *str)
Kumar Galac8ab7052007-10-24 11:04:22 -0500467{
468 int len = strlen(str);
David Gibsonb4141b82008-07-07 10:10:48 +1000469 const char *p;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400470
Kumar Galac8ab7052007-10-24 11:04:22 -0500471 while (listlen >= len) {
472 if (memcmp(str, strlist, len+1) == 0)
473 return 1;
474 p = memchr(strlist, '\0', listlen);
475 if (!p)
476 return 0; /* malformed strlist.. */
477 listlen -= (p-strlist) + 1;
478 strlist = p + 1;
Gerald Van Barenc077a132007-04-14 22:46:41 -0400479 }
480 return 0;
481}
Kumar Galac8ab7052007-10-24 11:04:22 -0500482
483int fdt_node_check_compatible(const void *fdt, int nodeoffset,
484 const char *compatible)
485{
486 const void *prop;
487 int len;
488
489 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
490 if (!prop)
491 return len;
David Gibson0a1617a2008-07-29 14:51:22 +1000492 if (_fdt_stringlist_contains(prop, len, compatible))
Kumar Galac8ab7052007-10-24 11:04:22 -0500493 return 0;
494 else
495 return 1;
496}
497
498int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
499 const char *compatible)
500{
David Gibsone8f3f7f2008-02-18 18:09:04 +1100501 int offset, err;
Kumar Galac8ab7052007-10-24 11:04:22 -0500502
David Gibson9457d9d2008-07-09 14:10:24 +1000503 FDT_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500504
Kumar Galac8ab7052007-10-24 11:04:22 -0500505 /* FIXME: The algorithm here is pretty horrible: we scan each
506 * property of a node in fdt_node_check_compatible(), then if
507 * that didn't find what we want, we scan over them again
508 * making our way to the next node. Still it's the easiest to
509 * implement approach; performance can come later. */
David Gibsond9d793c2008-02-12 11:58:31 +1100510 for (offset = fdt_next_node(fdt, startoffset, NULL);
511 offset >= 0;
512 offset = fdt_next_node(fdt, offset, NULL)) {
513 err = fdt_node_check_compatible(fdt, offset, compatible);
514 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
515 return err;
516 else if (err == 0)
517 return offset;
518 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500519
David Gibsond9d793c2008-02-12 11:58:31 +1100520 return offset; /* error from fdt_next_node() */
Kumar Galac8ab7052007-10-24 11:04:22 -0500521}