blob: d12c7ee42da6237de0b22e8528875de879d194ed [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 Gibsonf99cd152008-10-30 13:41:08 +1100111 int depth = 0;
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 Gibsonf99cd152008-10-30 13:41:08 +1100115 for (depth = 0, offset = fdt_next_node(fdt, offset, &depth);
116 (offset >= 0) && (depth > 0);
David Gibsonfc9769a2008-02-12 11:58:31 +1100117 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 Gibsonf99cd152008-10-30 13:41:08 +1100125 if (offset < 0)
126 return offset; /* error */
127 else
128 return -FDT_ERR_NOTFOUND;
David Gibson3da0f9a2006-11-27 16:21:28 +1100129}
130
David Gibson73d60922006-12-15 15:12:47 +1100131int fdt_subnode_offset(const void *fdt, int parentoffset,
David Gibson3da0f9a2006-11-27 16:21:28 +1100132 const char *name)
133{
134 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
135}
136
David Gibson73d60922006-12-15 15:12:47 +1100137int fdt_path_offset(const void *fdt, const char *path)
David Gibson3da0f9a2006-11-27 16:21:28 +1100138{
139 const char *end = path + strlen(path);
140 const char *p = path;
141 int offset = 0;
142
David Gibsonb6d80a22008-07-09 14:10:24 +1000143 FDT_CHECK_HEADER(fdt);
David Gibson3da0f9a2006-11-27 16:21:28 +1100144
Kumar Gala02cc8352008-08-14 08:28:19 -0500145 /* see if we have an alias */
146 if (*path != '/') {
David Gibson9c831152008-08-20 16:55:14 +1000147 const char *q = strchr(path, '/');
Kumar Gala02cc8352008-08-14 08:28:19 -0500148
Kumar Gala02cc8352008-08-14 08:28:19 -0500149 if (!q)
150 q = end;
151
David Gibson9c831152008-08-20 16:55:14 +1000152 p = fdt_get_alias_namelen(fdt, p, q - p);
Kumar Gala02cc8352008-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 }
David Gibson3da0f9a2006-11-27 16:21:28 +1100159
160 while (*p) {
161 const char *q;
162
163 while (*p == '/')
164 p++;
165 if (! *p)
David Gibsonbd2ae2f2007-08-29 12:22:50 +1000166 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +1100167 q = strchr(p, '/');
168 if (! q)
169 q = end;
170
171 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
David Gibson9a9fdf52006-12-15 15:12:51 +1100172 if (offset < 0)
David Gibson3da0f9a2006-11-27 16:21:28 +1100173 return offset;
174
175 p = q;
176 }
177
David Gibson63dc9c72007-09-18 11:44:04 +1000178 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +1100179}
180
David Gibson9d26eab2007-08-30 14:54:04 +1000181const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
182{
David Gibsonaa1baab2008-05-20 17:19:11 +1000183 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
David Gibson9d26eab2007-08-30 14:54:04 +1000184 int err;
185
David Gibsonaa1baab2008-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;
David Gibson9d26eab2007-08-30 14:54:04 +1000189
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 Gibsoncb650ae2008-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)
David Gibson3da0f9a2006-11-27 16:21:28 +1100205{
David Gibson94993f42006-12-11 16:15:34 +1100206 uint32_t tag;
David Gibsona6c76f92007-06-13 14:18:10 +1000207 const struct fdt_property *prop;
David Gibson94993f42006-12-11 16:15:34 +1100208 int namestroff;
209 int offset, nextoffset;
David Gibsona7ee95d2006-12-15 15:12:49 +1100210 int err;
David Gibson3da0f9a2006-11-27 16:21:28 +1100211
David Gibsonaa1baab2008-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;
David Gibson3da0f9a2006-11-27 16:21:28 +1100215
David Gibsonaa1baab2008-05-20 17:19:11 +1000216 nextoffset = err;
David Gibson94993f42006-12-11 16:15:34 +1100217 do {
218 offset = nextoffset;
David Gibson94993f42006-12-11 16:15:34 +1100219
David Gibson3c44c872007-10-24 11:06:09 +1000220 tag = fdt_next_tag(fdt, offset, &nextoffset);
David Gibson94993f42006-12-11 16:15:34 +1100221 switch (tag) {
222 case FDT_END:
David Gibson9a9fdf52006-12-15 15:12:51 +1100223 err = -FDT_ERR_TRUNCATED;
David Gibsona7ee95d2006-12-15 15:12:49 +1100224 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100225
226 case FDT_BEGIN_NODE:
David Gibson94993f42006-12-11 16:15:34 +1100227 case FDT_END_NODE:
David Gibson592ea582007-09-04 10:43:03 +1000228 case FDT_NOP:
David Gibson94993f42006-12-11 16:15:34 +1100229 break;
230
231 case FDT_PROP:
David Gibson9a9fdf52006-12-15 15:12:51 +1100232 err = -FDT_ERR_BADSTRUCTURE;
David Gibson2cf86932007-11-19 17:26:22 +1100233 prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
David Gibson94993f42006-12-11 16:15:34 +1100234 if (! prop)
David Gibsona7ee95d2006-12-15 15:12:49 +1100235 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100236 namestroff = fdt32_to_cpu(prop->nameoff);
David Gibsoncb650ae2008-08-06 14:50:49 +1000237 if (_fdt_string_eq(fdt, namestroff, name, namelen)) {
David Gibson94993f42006-12-11 16:15:34 +1100238 /* Found it! */
239 int len = fdt32_to_cpu(prop->len);
240 prop = fdt_offset_ptr(fdt, offset,
David Gibson9825f822006-12-14 15:29:25 +1100241 sizeof(*prop)+len);
David Gibson94993f42006-12-11 16:15:34 +1100242 if (! prop)
David Gibsona7ee95d2006-12-15 15:12:49 +1100243 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100244
245 if (lenp)
246 *lenp = len;
David Gibson63dc9c72007-09-18 11:44:04 +1000247
David Gibson94993f42006-12-11 16:15:34 +1100248 return prop;
249 }
250 break;
251
David Gibson94993f42006-12-11 16:15:34 +1100252 default:
David Gibson9a9fdf52006-12-15 15:12:51 +1100253 err = -FDT_ERR_BADSTRUCTURE;
David Gibsona7ee95d2006-12-15 15:12:49 +1100254 goto fail;
David Gibson94993f42006-12-11 16:15:34 +1100255 }
David Gibson592ea582007-09-04 10:43:03 +1000256 } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
David Gibson94993f42006-12-11 16:15:34 +1100257
David Gibson9a9fdf52006-12-15 15:12:51 +1100258 err = -FDT_ERR_NOTFOUND;
David Gibsona7ee95d2006-12-15 15:12:49 +1100259 fail:
260 if (lenp)
David Gibson9a9fdf52006-12-15 15:12:51 +1100261 *lenp = err;
David Gibsona7ee95d2006-12-15 15:12:49 +1100262 return NULL;
David Gibson3da0f9a2006-11-27 16:21:28 +1100263}
264
David Gibsoncb650ae2008-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)
David Gibson3da0f9a2006-11-27 16:21:28 +1100275{
276 const struct fdt_property *prop;
David Gibson3da0f9a2006-11-27 16:21:28 +1100277
David Gibsoncb650ae2008-08-06 14:50:49 +1000278 prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
David Gibsona7ee95d2006-12-15 15:12:49 +1100279 if (! prop)
280 return NULL;
David Gibson3da0f9a2006-11-27 16:21:28 +1100281
282 return prop->data;
283}
David Gibson037db262007-08-30 14:54:04 +1000284
David Gibsoncb650ae2008-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
David Gibson73468582007-11-13 09:59:38 +1100291uint32_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}
302
David Gibson9c831152008-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
David Gibson037db262007-08-30 14:54:04 +1000320int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
321{
David Gibsonfc9769a2008-02-12 11:58:31 +1100322 int pdepth = 0, p = 0;
323 int offset, depth, namelen;
David Gibson037db262007-08-30 14:54:04 +1000324 const char *name;
325
David Gibsonb6d80a22008-07-09 14:10:24 +1000326 FDT_CHECK_HEADER(fdt);
David Gibson037db262007-08-30 14:54:04 +1000327
David Gibson037db262007-08-30 14:54:04 +1000328 if (buflen < 2)
329 return -FDT_ERR_NOSPACE;
David Gibson037db262007-08-30 14:54:04 +1000330
David Gibsonfc9769a2008-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 Gibsonfc9769a2008-02-12 11:58:31 +1100334 while (pdepth > depth) {
335 do {
336 p--;
337 } while (buf[p-1] != '/');
338 pdepth--;
339 }
340
David Gibson8daae142008-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 Gibsonfc9769a2008-02-12 11:58:31 +1100351 }
David Gibson037db262007-08-30 14:54:04 +1000352
David Gibsonfc9769a2008-02-12 11:58:31 +1100353 if (offset == nodeoffset) {
354 if (pdepth < (depth + 1))
355 return -FDT_ERR_NOSPACE;
356
357 if (p > 1) /* special case so that root path is "/", not "" */
David Gibson037db262007-08-30 14:54:04 +1000358 p--;
David Gibsonfc9769a2008-02-12 11:58:31 +1100359 buf[p] = '\0';
David Gibson8daae142008-08-29 14:19:13 +1000360 return 0;
David Gibson037db262007-08-30 14:54:04 +1000361 }
362 }
363
David Gibsonfc9769a2008-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;
David Gibson037db262007-08-30 14:54:04 +1000368
David Gibsonfc9769a2008-02-12 11:58:31 +1100369 return offset; /* error from fdt_next_node() */
David Gibson037db262007-08-30 14:54:04 +1000370}
David Gibson12482372007-08-30 14:54:04 +1000371
372int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
373 int supernodedepth, int *nodedepth)
374{
David Gibsonfc9769a2008-02-12 11:58:31 +1100375 int offset, depth;
David Gibson12482372007-08-30 14:54:04 +1000376 int supernodeoffset = -FDT_ERR_INTERNAL;
377
David Gibsonb6d80a22008-07-09 14:10:24 +1000378 FDT_CHECK_HEADER(fdt);
David Gibson12482372007-08-30 14:54:04 +1000379
380 if (supernodedepth < 0)
381 return -FDT_ERR_NOTFOUND;
382
David Gibsonfc9769a2008-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;
David Gibson12482372007-08-30 14:54:04 +1000388
David Gibsonfc9769a2008-02-12 11:58:31 +1100389 if (offset == nodeoffset) {
390 if (nodedepth)
391 *nodedepth = depth;
David Gibson12482372007-08-30 14:54:04 +1000392
David Gibsonfc9769a2008-02-12 11:58:31 +1100393 if (supernodedepth > depth)
394 return -FDT_ERR_NOTFOUND;
395 else
396 return supernodeoffset;
David Gibson12482372007-08-30 14:54:04 +1000397 }
David Gibsonfc9769a2008-02-12 11:58:31 +1100398 }
David Gibson12482372007-08-30 14:54:04 +1000399
David Gibsonfc9769a2008-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;
David Gibson12482372007-08-30 14:54:04 +1000404
David Gibsonfc9769a2008-02-12 11:58:31 +1100405 return offset; /* error from fdt_next_node() */
David Gibson12482372007-08-30 14:54:04 +1000406}
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}
David Gibsonae1454b2007-09-17 14:28:34 +1000428
429int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
430 const char *propname,
431 const void *propval, int proplen)
432{
David Gibsonfc9769a2008-02-12 11:58:31 +1100433 int offset;
David Gibsonae1454b2007-09-17 14:28:34 +1000434 const void *val;
435 int len;
436
David Gibsonb6d80a22008-07-09 14:10:24 +1000437 FDT_CHECK_HEADER(fdt);
David Gibsonae1454b2007-09-17 14:28:34 +1000438
David Gibsonae1454b2007-09-17 14:28:34 +1000439 /* 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 Gibsonfc9769a2008-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 }
David Gibsonae1454b2007-09-17 14:28:34 +1000452
David Gibsonfc9769a2008-02-12 11:58:31 +1100453 return offset; /* error from fdt_next_node() */
David Gibsonae1454b2007-09-17 14:28:34 +1000454}
David Gibson333542f2007-10-16 13:58:25 +1000455
David Gibson73468582007-11-13 09:59:38 +1100456int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
457{
458 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}
464
David Gibsond5653612008-07-29 14:51:22 +1000465static int _fdt_stringlist_contains(const char *strlist, int listlen,
466 const char *str)
David Gibson333542f2007-10-16 13:58:25 +1000467{
468 int len = strlen(str);
David Gibson36786db2008-07-07 10:10:48 +1000469 const char *p;
David Gibson333542f2007-10-16 13:58:25 +1000470
471 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;
479 }
480 return 0;
481}
482
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 Gibsond5653612008-07-29 14:51:22 +1000492 if (_fdt_stringlist_contains(prop, len, compatible))
David Gibson333542f2007-10-16 13:58:25 +1000493 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 Gibson2512a7e2008-02-18 18:09:04 +1100501 int offset, err;
David Gibson333542f2007-10-16 13:58:25 +1000502
David Gibsonb6d80a22008-07-09 14:10:24 +1000503 FDT_CHECK_HEADER(fdt);
David Gibson333542f2007-10-16 13:58:25 +1000504
David Gibson333542f2007-10-16 13:58:25 +1000505 /* 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 Gibsonfc9769a2008-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 }
David Gibson333542f2007-10-16 13:58:25 +1000519
David Gibsonfc9769a2008-02-12 11:58:31 +1100520 return offset; /* error from fdt_next_node() */
David Gibson333542f2007-10-16 13:58:25 +1000521}