blob: 69af7bb843ae4e36ce4a3f89fb1190a52c27d254 [file] [log] [blame]
Gerald Van Baren35748172007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
Kumar Gala8d04f022007-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 Baren35748172007-03-31 12:00:56 -04007 *
Kumar Gala8d04f022007-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 Baren35748172007-03-31 12:00:56 -040012 *
Kumar Gala8d04f022007-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 Baren35748172007-03-31 12:00:56 -040050 */
51#include "libfdt_env.h"
52
Bartlomiej Sieka8cf30802008-02-29 16:00:24 +010053#ifndef USE_HOSTCC
Gerald Van Baren35748172007-03-31 12:00:56 -040054#include <fdt.h>
55#include <libfdt.h>
Bartlomiej Sieka8cf30802008-02-29 16:00:24 +010056#else
57#include "fdt_host.h"
58#endif
Gerald Van Baren35748172007-03-31 12:00:56 -040059
60#include "libfdt_internal.h"
61
Kumar Gala8d04f022007-10-24 11:04:22 -050062static int nodename_eq(const void *fdt, int offset,
63 const char *s, int len)
Gerald Van Baren35748172007-03-31 12:00:56 -040064{
David Gibsonae0b5902008-02-12 11:58:31 +110065 const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
Gerald Van Baren35748172007-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 Gala8d04f022007-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 Baren35748172007-03-31 12:00:56 -040079 return 0;
Gerald Van Baren35748172007-03-31 12:00:56 -040080}
81
Kumar Gala8d04f022007-10-24 11:04:22 -050082const char *fdt_string(const void *fdt, int stroffset)
Gerald Van Baren35748172007-03-31 12:00:56 -040083{
84 return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
85}
86
Kumar Gala8d04f022007-10-24 11:04:22 -050087int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
88{
89 CHECK_HEADER(fdt);
90 *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
91 *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
92 return 0;
93}
94
95int fdt_num_mem_rsv(const void *fdt)
96{
97 int i = 0;
98
99 while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
100 i++;
101 return i;
102}
103
David Gibsonae0b5902008-02-12 11:58:31 +1100104int fdt_subnode_offset_namelen(const void *fdt, int offset,
Gerald Van Baren35748172007-03-31 12:00:56 -0400105 const char *name, int namelen)
106{
David Gibsonae0b5902008-02-12 11:58:31 +1100107 int depth;
Gerald Van Baren35748172007-03-31 12:00:56 -0400108
109 CHECK_HEADER(fdt);
110
David Gibsonae0b5902008-02-12 11:58:31 +1100111 for (depth = 0;
112 offset >= 0;
113 offset = fdt_next_node(fdt, offset, &depth)) {
114 if (depth < 0)
115 return -FDT_ERR_NOTFOUND;
116 else if ((depth == 1)
117 && nodename_eq(fdt, offset, name, namelen))
118 return offset;
119 }
Gerald Van Baren35748172007-03-31 12:00:56 -0400120
David Gibsonae0b5902008-02-12 11:58:31 +1100121 return offset; /* error */
Gerald Van Baren35748172007-03-31 12:00:56 -0400122}
123
124int fdt_subnode_offset(const void *fdt, int parentoffset,
125 const char *name)
126{
127 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
128}
129
Kumar Gala8d04f022007-10-24 11:04:22 -0500130int fdt_path_offset(const void *fdt, const char *path)
Gerald Van Baren35748172007-03-31 12:00:56 -0400131{
132 const char *end = path + strlen(path);
133 const char *p = path;
134 int offset = 0;
135
136 CHECK_HEADER(fdt);
137
138 if (*path != '/')
139 return -FDT_ERR_BADPATH;
140
141 while (*p) {
142 const char *q;
143
144 while (*p == '/')
145 p++;
146 if (! *p)
Kumar Gala8d04f022007-10-24 11:04:22 -0500147 return offset;
Gerald Van Baren35748172007-03-31 12:00:56 -0400148 q = strchr(p, '/');
149 if (! q)
150 q = end;
151
152 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
153 if (offset < 0)
154 return offset;
155
156 p = q;
157 }
158
Gerald Van Barenaea03c42007-03-31 14:30:53 -0400159 return offset;
Gerald Van Baren35748172007-03-31 12:00:56 -0400160}
161
Kumar Gala8d04f022007-10-24 11:04:22 -0500162const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
Gerald Van Baren35748172007-03-31 12:00:56 -0400163{
David Gibson2f08bfa2008-05-20 17:19:11 +1000164 const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
Kumar Gala8d04f022007-10-24 11:04:22 -0500165 int err;
166
David Gibson2f08bfa2008-05-20 17:19:11 +1000167 if (((err = fdt_check_header(fdt)) != 0)
168 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
169 goto fail;
Kumar Gala8d04f022007-10-24 11:04:22 -0500170
171 if (len)
172 *len = strlen(nh->name);
173
174 return nh->name;
175
176 fail:
177 if (len)
178 *len = err;
179 return NULL;
180}
181
182const struct fdt_property *fdt_get_property(const void *fdt,
183 int nodeoffset,
184 const char *name, int *lenp)
185{
Gerald Van Baren35748172007-03-31 12:00:56 -0400186 uint32_t tag;
Kumar Gala8d04f022007-10-24 11:04:22 -0500187 const struct fdt_property *prop;
188 int namestroff;
Gerald Van Baren35748172007-03-31 12:00:56 -0400189 int offset, nextoffset;
190 int err;
191
David Gibson2f08bfa2008-05-20 17:19:11 +1000192 if (((err = fdt_check_header(fdt)) != 0)
193 || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
194 goto fail;
Gerald Van Baren35748172007-03-31 12:00:56 -0400195
David Gibson2f08bfa2008-05-20 17:19:11 +1000196 nextoffset = err;
Gerald Van Baren35748172007-03-31 12:00:56 -0400197 do {
198 offset = nextoffset;
199
Kumar Gala8d04f022007-10-24 11:04:22 -0500200 tag = fdt_next_tag(fdt, offset, &nextoffset);
Gerald Van Baren35748172007-03-31 12:00:56 -0400201 switch (tag) {
202 case FDT_END:
203 err = -FDT_ERR_TRUNCATED;
204 goto fail;
205
206 case FDT_BEGIN_NODE:
Gerald Van Baren35748172007-03-31 12:00:56 -0400207 case FDT_END_NODE:
Kumar Gala8d04f022007-10-24 11:04:22 -0500208 case FDT_NOP:
Gerald Van Baren35748172007-03-31 12:00:56 -0400209 break;
210
211 case FDT_PROP:
Kumar Gala8d04f022007-10-24 11:04:22 -0500212 err = -FDT_ERR_BADSTRUCTURE;
213 prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
214 if (! prop)
Gerald Van Baren9675ee72007-05-17 23:54:36 -0400215 goto fail;
Kumar Gala8d04f022007-10-24 11:04:22 -0500216 namestroff = fdt32_to_cpu(prop->nameoff);
217 if (streq(fdt_string(fdt, namestroff), name)) {
218 /* Found it! */
219 int len = fdt32_to_cpu(prop->len);
220 prop = fdt_offset_ptr(fdt, offset,
221 sizeof(*prop)+len);
222 if (! prop)
223 goto fail;
Gerald Van Baren35748172007-03-31 12:00:56 -0400224
Kumar Gala8d04f022007-10-24 11:04:22 -0500225 if (lenp)
226 *lenp = len;
227
228 return prop;
229 }
Gerald Van Baren35748172007-03-31 12:00:56 -0400230 break;
231
232 default:
233 err = -FDT_ERR_BADSTRUCTURE;
234 goto fail;
235 }
Kumar Gala8d04f022007-10-24 11:04:22 -0500236 } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
Gerald Van Baren35748172007-03-31 12:00:56 -0400237
238 err = -FDT_ERR_NOTFOUND;
Kumar Gala8d04f022007-10-24 11:04:22 -0500239 fail:
Gerald Van Baren35748172007-03-31 12:00:56 -0400240 if (lenp)
241 *lenp = err;
242 return NULL;
243}
244
Kumar Gala8d04f022007-10-24 11:04:22 -0500245const void *fdt_getprop(const void *fdt, int nodeoffset,
Gerald Van Baren35748172007-03-31 12:00:56 -0400246 const char *name, int *lenp)
247{
248 const struct fdt_property *prop;
249
250 prop = fdt_get_property(fdt, nodeoffset, name, lenp);
251 if (! prop)
252 return NULL;
253
Kumar Gala8d04f022007-10-24 11:04:22 -0500254 return prop->data;
Gerald Van Baren35748172007-03-31 12:00:56 -0400255}
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400256
Kumar Gala8d04f022007-10-24 11:04:22 -0500257uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400258{
Kumar Gala8d04f022007-10-24 11:04:22 -0500259 const uint32_t *php;
260 int len;
261
262 php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
263 if (!php || (len != sizeof(*php)))
264 return 0;
265
266 return fdt32_to_cpu(*php);
267}
268
269int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
270{
David Gibsonae0b5902008-02-12 11:58:31 +1100271 int pdepth = 0, p = 0;
272 int offset, depth, namelen;
Kumar Gala8d04f022007-10-24 11:04:22 -0500273 const char *name;
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400274
Kumar Gala8d04f022007-10-24 11:04:22 -0500275 CHECK_HEADER(fdt);
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400276
Kumar Gala8d04f022007-10-24 11:04:22 -0500277 if (buflen < 2)
Gerald Van Baren3f9f08c2007-04-14 22:46:41 -0400278 return -FDT_ERR_NOSPACE;
Kumar Gala8d04f022007-10-24 11:04:22 -0500279
David Gibsonae0b5902008-02-12 11:58:31 +1100280 for (offset = 0, depth = 0;
281 (offset >= 0) && (offset <= nodeoffset);
282 offset = fdt_next_node(fdt, offset, &depth)) {
283 if (pdepth < depth)
284 continue; /* overflowed buffer */
Kumar Gala8d04f022007-10-24 11:04:22 -0500285
David Gibsonae0b5902008-02-12 11:58:31 +1100286 while (pdepth > depth) {
287 do {
288 p--;
289 } while (buf[p-1] != '/');
290 pdepth--;
291 }
292
293 name = fdt_get_name(fdt, offset, &namelen);
294 if (!name)
295 return namelen;
296 if ((p + namelen + 1) <= buflen) {
Kumar Gala8d04f022007-10-24 11:04:22 -0500297 memcpy(buf + p, name, namelen);
298 p += namelen;
299 buf[p++] = '/';
David Gibsonae0b5902008-02-12 11:58:31 +1100300 pdepth++;
301 }
Kumar Gala8d04f022007-10-24 11:04:22 -0500302
David Gibsonae0b5902008-02-12 11:58:31 +1100303 if (offset == nodeoffset) {
304 if (pdepth < (depth + 1))
305 return -FDT_ERR_NOSPACE;
306
307 if (p > 1) /* special case so that root path is "/", not "" */
Kumar Gala8d04f022007-10-24 11:04:22 -0500308 p--;
David Gibsonae0b5902008-02-12 11:58:31 +1100309 buf[p] = '\0';
310 return p;
Kumar Gala8d04f022007-10-24 11:04:22 -0500311 }
312 }
313
David Gibsonae0b5902008-02-12 11:58:31 +1100314 if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
315 return -FDT_ERR_BADOFFSET;
316 else if (offset == -FDT_ERR_BADOFFSET)
317 return -FDT_ERR_BADSTRUCTURE;
Kumar Gala8d04f022007-10-24 11:04:22 -0500318
David Gibsonae0b5902008-02-12 11:58:31 +1100319 return offset; /* error from fdt_next_node() */
Kumar Gala8d04f022007-10-24 11:04:22 -0500320}
321
322int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
323 int supernodedepth, int *nodedepth)
324{
David Gibsonae0b5902008-02-12 11:58:31 +1100325 int offset, depth;
Kumar Gala8d04f022007-10-24 11:04:22 -0500326 int supernodeoffset = -FDT_ERR_INTERNAL;
327
328 CHECK_HEADER(fdt);
329
330 if (supernodedepth < 0)
331 return -FDT_ERR_NOTFOUND;
332
David Gibsonae0b5902008-02-12 11:58:31 +1100333 for (offset = 0, depth = 0;
334 (offset >= 0) && (offset <= nodeoffset);
335 offset = fdt_next_node(fdt, offset, &depth)) {
336 if (depth == supernodedepth)
337 supernodeoffset = offset;
Kumar Gala8d04f022007-10-24 11:04:22 -0500338
David Gibsonae0b5902008-02-12 11:58:31 +1100339 if (offset == nodeoffset) {
340 if (nodedepth)
341 *nodedepth = depth;
Kumar Gala8d04f022007-10-24 11:04:22 -0500342
David Gibsonae0b5902008-02-12 11:58:31 +1100343 if (supernodedepth > depth)
344 return -FDT_ERR_NOTFOUND;
345 else
346 return supernodeoffset;
Kumar Gala8d04f022007-10-24 11:04:22 -0500347 }
David Gibsonae0b5902008-02-12 11:58:31 +1100348 }
Kumar Gala8d04f022007-10-24 11:04:22 -0500349
David Gibsonae0b5902008-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;
Kumar Gala8d04f022007-10-24 11:04:22 -0500354
David Gibsonae0b5902008-02-12 11:58:31 +1100355 return offset; /* error from fdt_next_node() */
Kumar Gala8d04f022007-10-24 11:04:22 -0500356}
357
358int fdt_node_depth(const void *fdt, int nodeoffset)
359{
360 int nodedepth;
361 int err;
362
363 err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
364 if (err)
365 return (err < 0) ? err : -FDT_ERR_INTERNAL;
366 return nodedepth;
367}
368
369int fdt_parent_offset(const void *fdt, int nodeoffset)
370{
371 int nodedepth = fdt_node_depth(fdt, nodeoffset);
372
373 if (nodedepth < 0)
374 return nodedepth;
375 return fdt_supernode_atdepth_offset(fdt, nodeoffset,
376 nodedepth - 1, NULL);
377}
378
379int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
380 const char *propname,
381 const void *propval, int proplen)
382{
David Gibsonae0b5902008-02-12 11:58:31 +1100383 int offset;
Kumar Gala8d04f022007-10-24 11:04:22 -0500384 const void *val;
385 int len;
386
387 CHECK_HEADER(fdt);
388
Kumar Gala8d04f022007-10-24 11:04:22 -0500389 /* FIXME: The algorithm here is pretty horrible: we scan each
390 * property of a node in fdt_getprop(), then if that didn't
391 * find what we want, we scan over them again making our way
392 * to the next node. Still it's the easiest to implement
393 * approach; performance can come later. */
David Gibsonae0b5902008-02-12 11:58:31 +1100394 for (offset = fdt_next_node(fdt, startoffset, NULL);
395 offset >= 0;
396 offset = fdt_next_node(fdt, offset, NULL)) {
397 val = fdt_getprop(fdt, offset, propname, &len);
398 if (val && (len == proplen)
399 && (memcmp(val, propval, len) == 0))
400 return offset;
401 }
Kumar Gala8d04f022007-10-24 11:04:22 -0500402
David Gibsonae0b5902008-02-12 11:58:31 +1100403 return offset; /* error from fdt_next_node() */
Kumar Gala8d04f022007-10-24 11:04:22 -0500404}
405
406int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
407{
408 if ((phandle == 0) || (phandle == -1))
409 return -FDT_ERR_BADPHANDLE;
410 phandle = cpu_to_fdt32(phandle);
411 return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle",
412 &phandle, sizeof(phandle));
413}
414
415int _stringlist_contains(const void *strlist, int listlen, const char *str)
416{
417 int len = strlen(str);
418 const void *p;
419
420 while (listlen >= len) {
421 if (memcmp(str, strlist, len+1) == 0)
422 return 1;
423 p = memchr(strlist, '\0', listlen);
424 if (!p)
425 return 0; /* malformed strlist.. */
426 listlen -= (p-strlist) + 1;
427 strlist = p + 1;
Gerald Van Baren3f9f08c2007-04-14 22:46:41 -0400428 }
429 return 0;
430}
Kumar Gala8d04f022007-10-24 11:04:22 -0500431
432int fdt_node_check_compatible(const void *fdt, int nodeoffset,
433 const char *compatible)
434{
435 const void *prop;
436 int len;
437
438 prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
439 if (!prop)
440 return len;
441 if (_stringlist_contains(prop, len, compatible))
442 return 0;
443 else
444 return 1;
445}
446
447int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
448 const char *compatible)
449{
David Gibson11abe452008-02-18 18:09:04 +1100450 int offset, err;
Kumar Gala8d04f022007-10-24 11:04:22 -0500451
452 CHECK_HEADER(fdt);
453
Kumar Gala8d04f022007-10-24 11:04:22 -0500454 /* FIXME: The algorithm here is pretty horrible: we scan each
455 * property of a node in fdt_node_check_compatible(), then if
456 * that didn't find what we want, we scan over them again
457 * making our way to the next node. Still it's the easiest to
458 * implement approach; performance can come later. */
David Gibsonae0b5902008-02-12 11:58:31 +1100459 for (offset = fdt_next_node(fdt, startoffset, NULL);
460 offset >= 0;
461 offset = fdt_next_node(fdt, offset, NULL)) {
462 err = fdt_node_check_compatible(fdt, offset, compatible);
463 if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
464 return err;
465 else if (err == 0)
466 return offset;
467 }
Kumar Gala8d04f022007-10-24 11:04:22 -0500468
David Gibsonae0b5902008-02-12 11:58:31 +1100469 return offset; /* error from fdt_next_node() */
Kumar Gala8d04f022007-10-24 11:04:22 -0500470}