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 * Return a pointer to the string at the given string offset.
53 char *fdt_string(const void *fdt, int stroffset)
55 return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
59 * Return the node offset of the node specified by:
60 * parentoffset - starting place (0 to start at the root)
61 * name - name being searched for
62 * namelen - length of the name: typically strlen(name)
65 * If the start node has subnodes, the subnodes are _not_ searched for the
68 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
69 const char *name, int namelen)
73 int offset, nextoffset;
77 tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
78 if (tag != FDT_BEGIN_NODE)
79 return -FDT_ERR_BADOFFSET;
83 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
87 return -FDT_ERR_TRUNCATED;
92 * If we are nested down levels, ignore the strings
93 * until we get back to the proper level.
98 /* Return the offset if this is "our" string. */
99 if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
112 return -FDT_ERR_BADSTRUCTURE;
114 } while (level >= 0);
116 return -FDT_ERR_NOTFOUND;
120 * See fdt_subnode_offset_namelen()
122 int fdt_subnode_offset(const void *fdt, int parentoffset,
125 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
129 * Searches for the node corresponding to the given path and returns the
130 * offset of that node.
132 int fdt_path_offset(const void *fdt, const char *path)
134 const char *end = path + strlen(path);
135 const char *p = path;
140 /* Paths must be absolute */
142 return -FDT_ERR_BADPATH;
147 /* Skip path separator(s) */
151 return -FDT_ERR_BADPATH;
154 * Find the next path separator. The characters between
155 * p and q are the next segment of the the path to find.
162 * Find the offset corresponding to the this path segment.
164 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
166 /* Oops, error, abort abort abort */
177 * Given the offset of a node and a name of a property in that node, return
178 * a pointer to the property struct.
180 struct fdt_property *fdt_get_property(const void *fdt,
182 const char *name, int *lenp)
186 struct fdt_property *prop;
188 int offset, nextoffset;
191 if ((err = fdt_check_header(fdt)) != 0)
194 err = -FDT_ERR_BADOFFSET;
195 if (nodeoffset % FDT_TAGSIZE)
198 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
199 if (tag != FDT_BEGIN_NODE)
205 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
208 err = -FDT_ERR_TRUNCATED;
221 * If we are nested down levels, ignore the strings
222 * until we get back to the proper level.
227 err = -FDT_ERR_BADSTRUCTURE;
228 prop = fdt_offset_ptr_typed(fdt, offset, prop);
231 namestroff = fdt32_to_cpu(prop->nameoff);
232 if (streq(fdt_string(fdt, namestroff), name)) {
234 int len = fdt32_to_cpu(prop->len);
235 prop = fdt_offset_ptr(fdt, offset,
251 err = -FDT_ERR_BADSTRUCTURE;
254 } while (level >= 0);
256 err = -FDT_ERR_NOTFOUND;
264 * Given the offset of a node and a name of a property in that node, return
265 * a pointer to the property data (ONLY).
267 void *fdt_getprop(const void *fdt, int nodeoffset,
268 const char *name, int *lenp)
270 const struct fdt_property *prop;
272 prop = fdt_get_property(fdt, nodeoffset, name, lenp);
276 return (void *)prop->data;
280 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
282 const uint32_t *tagp, *lenp;
286 if (offset % FDT_TAGSIZE)
289 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
291 return FDT_END; /* premature end */
292 tag = fdt32_to_cpu(*tagp);
293 offset += FDT_TAGSIZE;
298 *namep = fdt_offset_ptr(fdt, offset, 1);
302 p = fdt_offset_ptr(fdt, offset++, 1);
303 } while (p && (*p != '\0'));
308 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
312 * Get the property and set the namep to the name.
315 struct fdt_property *prop;
317 prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
319 return -FDT_ERR_BADSTRUCTURE;
320 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
322 /* skip name offset, length and value */
323 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
328 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
334 * Return the number of used reserve map entries and total slots available.
336 int fdt_num_reservemap(void *fdt, int *used, int *total)
338 struct fdt_reserve_entry *re;
341 int err = fdt_check_header(fdt);
346 start = fdt_off_mem_rsvmap(fdt);
349 * Convention is that the reserve map is before the dt_struct,
350 * but it does not have to be.
352 end = fdt_totalsize(fdt);
353 if (end > fdt_off_dt_struct(fdt))
354 end = fdt_off_dt_struct(fdt);
355 if (end > fdt_off_dt_strings(fdt))
356 end = fdt_off_dt_strings(fdt);
359 * Since the reserved area list is zero terminated, you get one fewer.
362 *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
366 while (start < end) {
367 re = (struct fdt_reserve_entry *)(fdt + start);
369 return 0; /* zero size terminates the list */
372 start += sizeof(struct fdt_reserve_entry);
375 * If we get here, there was no zero size termination.
377 return -FDT_ERR_BADLAYOUT;
383 * Return the nth reserve map entry.
385 int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
391 err = fdt_num_reservemap(fdt, &used, &total);
396 return -FDT_ERR_NOSPACE;
398 *re = *(struct fdt_reserve_entry *)
399 _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));