2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libfdt_env.h"
24 #include "libfdt_internal.h"
26 #define CHECK_HEADER(fdt) \
29 if ((err = _fdt_check_header(fdt)) != 0) \
33 static int offset_streq(const void *fdt, int offset,
34 const char *s, int len)
36 const char *p = fdt_offset_ptr(fdt, offset, len+1);
42 if (memcmp(p, s, len) != 0)
51 char *fdt_string(const void *fdt, int stroffset)
53 return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
56 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
57 const char *name, int namelen)
61 int offset, nextoffset;
65 tag = _fdt_next_tag(fdt, parentoffset, &nextoffset);
66 if (tag != FDT_BEGIN_NODE)
67 return -FDT_ERR_BADOFFSET;
71 tag = _fdt_next_tag(fdt, offset, &nextoffset);
75 return -FDT_ERR_TRUNCATED;
81 if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
95 return -FDT_ERR_BADSTRUCTURE;
99 return -FDT_ERR_NOTFOUND;
102 int fdt_subnode_offset(const void *fdt, int parentoffset,
105 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
108 int fdt_path_offset(const void *fdt, const char *path)
110 const char *end = path + strlen(path);
111 const char *p = path;
117 return -FDT_ERR_BADPATH;
125 return -FDT_ERR_BADPATH;
130 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
140 struct fdt_property *fdt_get_property(const void *fdt,
142 const char *name, int *lenp)
146 struct fdt_property *prop;
148 int offset, nextoffset;
151 if ((err = _fdt_check_header(fdt)) != 0)
154 err = -FDT_ERR_BADOFFSET;
155 if (nodeoffset % FDT_TAGSIZE)
158 tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
159 if (tag != FDT_BEGIN_NODE)
165 tag = _fdt_next_tag(fdt, offset, &nextoffset);
168 err = -FDT_ERR_TRUNCATED;
183 err = -FDT_ERR_BADSTRUCTURE;
184 prop = fdt_offset_ptr_typed(fdt, offset, prop);
187 namestroff = fdt32_to_cpu(prop->nameoff);
188 if (streq(fdt_string(fdt, namestroff), name)) {
190 int len = fdt32_to_cpu(prop->len);
191 prop = fdt_offset_ptr(fdt, offset,
207 err = -FDT_ERR_BADSTRUCTURE;
210 } while (level >= 0);
212 err = -FDT_ERR_NOTFOUND;
219 void *fdt_getprop(const void *fdt, int nodeoffset,
220 const char *name, int *lenp)
222 const struct fdt_property *prop;
224 prop = fdt_get_property(fdt, nodeoffset, name, lenp);