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
22 #include "libfdt_env.h"
27 #include "libfdt_internal.h"
29 #define CHECK_HEADER(fdt) { \
31 if ((err = fdt_check_header(fdt)) != 0) \
35 static int offset_streq(const void *fdt, int offset,
36 const char *s, int len)
38 const char *p = fdt_offset_ptr(fdt, offset, len+1);
44 if (memcmp(p, s, len) != 0)
54 * Return a pointer to the string at the given string offset.
56 char *fdt_string(const void *fdt, int stroffset)
58 return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
62 * Return the node offset of the node specified by:
63 * parentoffset - starting place (0 to start at the root)
64 * name - name being searched for
65 * namelen - length of the name: typically strlen(name)
68 * If the start node has subnodes, the subnodes are _not_ searched for the
71 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
72 const char *name, int namelen)
76 int offset, nextoffset;
80 tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
81 if (tag != FDT_BEGIN_NODE)
82 return -FDT_ERR_BADOFFSET;
86 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
90 return -FDT_ERR_TRUNCATED;
95 * If we are nested down levels, ignore the strings
96 * until we get back to the proper level.
101 /* Return the offset if this is "our" string. */
102 if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
115 return -FDT_ERR_BADSTRUCTURE;
117 } while (level >= 0);
119 return -FDT_ERR_NOTFOUND;
123 * See fdt_subnode_offset_namelen()
125 int fdt_subnode_offset(const void *fdt, int parentoffset,
128 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
132 * Searches for the node corresponding to the given path and returns the
133 * offset of that node.
135 int fdt_path_offset(const void *fdt, const char *path)
137 const char *end = path + strlen(path);
138 const char *p = path;
143 /* Paths must be absolute */
145 return -FDT_ERR_BADPATH;
150 /* Skip path separator(s) */
154 return -FDT_ERR_BADPATH;
157 * Find the next path separator. The characters between
158 * p and q are the next segment of the the path to find.
165 * Find the offset corresponding to the this path segment.
167 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
169 /* Oops, error, abort abort abort */
180 * Given the offset of a node and a name of a property in that node, return
181 * a pointer to the property struct.
183 struct fdt_property *fdt_get_property(const void *fdt,
185 const char *name, int *lenp)
189 struct fdt_property *prop;
191 int offset, nextoffset;
194 if ((err = fdt_check_header(fdt)) != 0)
197 err = -FDT_ERR_BADOFFSET;
198 if (nodeoffset % FDT_TAGSIZE)
201 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
202 if (tag != FDT_BEGIN_NODE)
208 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
211 err = -FDT_ERR_TRUNCATED;
224 * If we are nested down levels, ignore the strings
225 * until we get back to the proper level.
230 err = -FDT_ERR_BADSTRUCTURE;
231 prop = fdt_offset_ptr_typed(fdt, offset, prop);
234 namestroff = fdt32_to_cpu(prop->nameoff);
235 if (streq(fdt_string(fdt, namestroff), name)) {
237 int len = fdt32_to_cpu(prop->len);
238 prop = fdt_offset_ptr(fdt, offset,
254 err = -FDT_ERR_BADSTRUCTURE;
257 } while (level >= 0);
259 err = -FDT_ERR_NOTFOUND;
267 * Given the offset of a node and a name of a property in that node, return
268 * a pointer to the property data (ONLY).
270 void *fdt_getprop(const void *fdt, int nodeoffset,
271 const char *name, int *lenp)
273 const struct fdt_property *prop;
275 prop = fdt_get_property(fdt, nodeoffset, name, lenp);
279 return (void *)prop->data;
283 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
285 const uint32_t *tagp, *lenp;
289 if (offset % FDT_TAGSIZE)
292 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
294 return FDT_END; /* premature end */
295 tag = fdt32_to_cpu(*tagp);
296 offset += FDT_TAGSIZE;
301 *namep = fdt_offset_ptr(fdt, offset, 1);
305 p = fdt_offset_ptr(fdt, offset++, 1);
306 } while (p && (*p != '\0'));
311 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
315 * Get the property and set the namep to the name.
318 struct fdt_property *prop;
320 prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
322 return -FDT_ERR_BADSTRUCTURE;
323 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
325 /* skip name offset, length and value */
326 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
331 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
337 * Return the number of used reserve map entries and total slots available.
339 int fdt_num_reservemap(void *fdt, int *used, int *total)
341 struct fdt_reserve_entry *re;
344 int err = fdt_check_header(fdt);
349 start = fdt_off_mem_rsvmap(fdt);
352 * Convention is that the reserve map is before the dt_struct,
353 * but it does not have to be.
355 end = fdt_totalsize(fdt);
356 if (end > fdt_off_dt_struct(fdt))
357 end = fdt_off_dt_struct(fdt);
358 if (end > fdt_off_dt_strings(fdt))
359 end = fdt_off_dt_strings(fdt);
362 * Since the reserved area list is zero terminated, you get one fewer.
365 *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
369 while (start < end) {
370 re = (struct fdt_reserve_entry *)(fdt + start);
372 return 0; /* zero size terminates the list */
375 start += sizeof(struct fdt_reserve_entry);
378 * If we get here, there was no zero size termination.
380 return -FDT_ERR_BADLAYOUT;
386 * Return the nth reserve map entry.
388 int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
394 err = fdt_num_reservemap(fdt, &used, &total);
399 return -FDT_ERR_NOSPACE;
401 *re = *(struct fdt_reserve_entry *)
402 _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));
407 #endif /* CONFIG_OF_LIBFDT */