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)
52 * Return a pointer to the string at the given string offset.
54 char *fdt_string(const void *fdt, int stroffset)
56 return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
60 * Return the node offset of the node specified by:
61 * parentoffset - starting place (0 to start at the root)
62 * name - name being searched for
63 * namelen - length of the name: typically strlen(name)
66 * If the start node has subnodes, the subnodes are _not_ searched for the
69 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
70 const char *name, int namelen)
74 int offset, nextoffset;
78 tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
79 if (tag != FDT_BEGIN_NODE)
80 return -FDT_ERR_BADOFFSET;
84 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
88 return -FDT_ERR_TRUNCATED;
93 * If we are nested down levels, ignore the strings
94 * until we get back to the proper level.
99 /* Return the offset if this is "our" string. */
100 if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
113 return -FDT_ERR_BADSTRUCTURE;
115 } while (level >= 0);
117 return -FDT_ERR_NOTFOUND;
121 * See fdt_subnode_offset_namelen()
123 int fdt_subnode_offset(const void *fdt, int parentoffset,
126 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
130 * Searches for the node corresponding to the given path and returns the
131 * offset of that node.
133 int fdt_path_offset(const void *fdt, const char *path)
135 const char *end = path + strlen(path);
136 const char *p = path;
141 /* Paths must be absolute */
143 return -FDT_ERR_BADPATH;
148 /* Skip path separator(s) */
152 return -FDT_ERR_BADPATH;
155 * Find the next path separator. The characters between
156 * p and q are the next segment of the the path to find.
163 * Find the offset corresponding to the this path segment.
165 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
167 /* Oops, error, abort abort abort */
178 * Given the offset of a node and a name of a property in that node, return
179 * a pointer to the property struct.
181 struct fdt_property *fdt_get_property(const void *fdt,
183 const char *name, int *lenp)
187 struct fdt_property *prop;
189 int offset, nextoffset;
192 if ((err = _fdt_check_header(fdt)) != 0)
195 err = -FDT_ERR_BADOFFSET;
196 if (nodeoffset % FDT_TAGSIZE)
199 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
200 if (tag != FDT_BEGIN_NODE)
206 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
209 err = -FDT_ERR_TRUNCATED;
222 * If we are nested down levels, ignore the strings
223 * until we get back to the proper level.
228 err = -FDT_ERR_BADSTRUCTURE;
229 prop = fdt_offset_ptr_typed(fdt, offset, prop);
232 namestroff = fdt32_to_cpu(prop->nameoff);
233 if (streq(fdt_string(fdt, namestroff), name)) {
235 int len = fdt32_to_cpu(prop->len);
236 prop = fdt_offset_ptr(fdt, offset,
252 err = -FDT_ERR_BADSTRUCTURE;
255 } while (level >= 0);
257 err = -FDT_ERR_NOTFOUND;
265 * Given the offset of a node and a name of a property in that node, return
266 * a pointer to the property data (ONLY).
268 void *fdt_getprop(const void *fdt, int nodeoffset,
269 const char *name, int *lenp)
271 const struct fdt_property *prop;
273 prop = fdt_get_property(fdt, nodeoffset, name, lenp);
277 return (void *)prop->data;
281 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
283 const uint32_t *tagp, *lenp;
287 if (offset % FDT_TAGSIZE)
290 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
292 return FDT_END; /* premature end */
293 tag = fdt32_to_cpu(*tagp);
294 offset += FDT_TAGSIZE;
299 *namep = fdt_offset_ptr(fdt, offset, 1);
303 p = fdt_offset_ptr(fdt, offset++, 1);
304 } while (p && (*p != '\0'));
309 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
313 * Get the property and set the namep to the name.
316 struct fdt_property *prop;
318 prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
320 return -FDT_ERR_BADSTRUCTURE;
321 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
323 /* skip name offset, length and value */
324 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
329 *nextoffset = ALIGN(offset, FDT_TAGSIZE);