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) { \
28 if ((err = fdt_check_header(fdt)) != 0) \
32 static int offset_streq(const void *fdt, int offset,
33 const char *s, int len)
35 const char *p = fdt_offset_ptr(fdt, offset, len+1);
41 if (memcmp(p, s, len) != 0)
51 * Checks if the property name matches.
53 static int prop_name_eq(const void *fdt, int offset, const char *name,
54 struct fdt_property **prop, int *lenp)
58 *prop = fdt_offset_ptr_typed(fdt, offset, *prop);
60 return -FDT_ERR_BADSTRUCTURE;
62 namestroff = fdt32_to_cpu((*prop)->nameoff);
63 if (streq(fdt_string(fdt, namestroff), name)) {
64 len = fdt32_to_cpu((*prop)->len);
65 *prop = fdt_offset_ptr(fdt, offset,
66 sizeof(**prop) + len);
72 return -FDT_ERR_BADSTRUCTURE;
78 * Return a pointer to the string at the given string offset.
80 char *fdt_string(const void *fdt, int stroffset)
82 return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
86 * Return the node offset of the node specified by:
87 * parentoffset - starting place (0 to start at the root)
88 * name - name being searched for
89 * namelen - length of the name: typically strlen(name)
92 * If the start node has subnodes, the subnodes are _not_ searched for the
95 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
96 const char *name, int namelen)
100 int offset, nextoffset;
104 tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
105 if (tag != FDT_BEGIN_NODE)
106 return -FDT_ERR_BADOFFSET;
110 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
114 return -FDT_ERR_TRUNCATED;
119 * If we are nested down levels, ignore the strings
120 * until we get back to the proper level.
125 /* Return the offset if this is "our" string. */
126 if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
139 return -FDT_ERR_BADSTRUCTURE;
141 } while (level >= 0);
143 return -FDT_ERR_NOTFOUND;
147 * See fdt_subnode_offset_namelen()
149 int fdt_subnode_offset(const void *fdt, int parentoffset,
152 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
156 * Searches for the node corresponding to the given path and returns the
157 * offset of that node.
159 int fdt_find_node_by_path(const void *fdt, const char *path)
161 const char *end = path + strlen(path);
162 const char *p = path;
167 /* Paths must be absolute */
169 return -FDT_ERR_BADPATH;
171 /* Handle the root path: root offset is 0 */
172 if (strcmp(path, "/") == 0)
178 /* Skip path separator(s) */
182 return -FDT_ERR_BADPATH;
185 * Find the next path separator. The characters between
186 * p and q are the next segment of the the path to find.
193 * Find the offset corresponding to the this path segment.
195 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
197 /* Oops, error, abort abort abort */
208 * Given the offset of a node and a name of a property in that node, return
209 * a pointer to the property struct.
211 struct fdt_property *fdt_get_property(const void *fdt,
213 const char *name, int *lenp)
217 struct fdt_property *prop;
218 int offset, nextoffset;
221 if ((err = fdt_check_header(fdt)) != 0)
224 err = -FDT_ERR_BADOFFSET;
225 if (nodeoffset % FDT_TAGSIZE)
228 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
229 if (tag != FDT_BEGIN_NODE)
235 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
238 err = -FDT_ERR_TRUNCATED;
251 * If we are nested down levels, ignore the strings
252 * until we get back to the proper level.
257 err = prop_name_eq(fdt, offset, name, &prop, lenp);
268 err = -FDT_ERR_BADSTRUCTURE;
271 } while (level >= 0);
273 err = -FDT_ERR_NOTFOUND;
281 * Given the offset of a node and a name of a property in that node, return
282 * a pointer to the property data (ONLY).
284 void *fdt_getprop(const void *fdt, int nodeoffset,
285 const char *name, int *lenp)
287 const struct fdt_property *prop;
289 prop = fdt_get_property(fdt, nodeoffset, name, lenp);
293 return (void *)prop->data;
297 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
299 const uint32_t *tagp, *lenp;
303 if (offset % FDT_TAGSIZE)
306 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
308 return FDT_END; /* premature end */
309 tag = fdt32_to_cpu(*tagp);
310 offset += FDT_TAGSIZE;
315 *namep = fdt_offset_ptr(fdt, offset, 1);
319 p = fdt_offset_ptr(fdt, offset++, 1);
320 } while (p && (*p != '\0'));
325 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
329 * Get the property and set the namep to the name.
332 struct fdt_property *prop;
334 prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
336 return -FDT_ERR_BADSTRUCTURE;
337 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
339 /* skip name offset, length and value */
340 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
345 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
351 * Return the number of used reserve map entries and total slots available.
353 int fdt_num_reservemap(void *fdt, int *used, int *total)
355 struct fdt_reserve_entry *re;
358 int err = fdt_check_header(fdt);
363 start = fdt_off_mem_rsvmap(fdt);
366 * Convention is that the reserve map is before the dt_struct,
367 * but it does not have to be.
369 end = fdt_totalsize(fdt);
370 if (end > fdt_off_dt_struct(fdt))
371 end = fdt_off_dt_struct(fdt);
372 if (end > fdt_off_dt_strings(fdt))
373 end = fdt_off_dt_strings(fdt);
376 * Since the reserved area list is zero terminated, you get one fewer.
379 *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
383 while (start < end) {
384 re = (struct fdt_reserve_entry *)(fdt + start);
386 return 0; /* zero size terminates the list */
389 start += sizeof(struct fdt_reserve_entry);
392 * If we get here, there was no zero size termination.
394 return -FDT_ERR_BADLAYOUT;
400 * Return the nth reserve map entry.
402 int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
408 err = fdt_num_reservemap(fdt, &used, &total);
413 return -FDT_ERR_NOSPACE;
415 *re = *(struct fdt_reserve_entry *)
416 _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));