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 * Checks if the property name matches.
56 static int prop_name_eq(const void *fdt, int offset, const char *name,
57 struct fdt_property **prop, int *lenp)
61 *prop = fdt_offset_ptr_typed(fdt, offset, *prop);
63 return -FDT_ERR_BADSTRUCTURE;
65 namestroff = fdt32_to_cpu((*prop)->nameoff);
66 if (streq(fdt_string(fdt, namestroff), name)) {
67 len = fdt32_to_cpu((*prop)->len);
68 *prop = fdt_offset_ptr(fdt, offset,
69 sizeof(**prop) + len);
75 return -FDT_ERR_BADSTRUCTURE;
81 * Return a pointer to the string at the given string offset.
83 char *fdt_string(const void *fdt, int stroffset)
85 return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
89 * Check if the specified node is compatible by comparing the tokens
90 * in its "compatible" property with the specified string:
92 * nodeoffset - starting place of the node
93 * compat - the string to match to one of the tokens in the
96 int fdt_node_is_compatible(const void *fdt, int nodeoffset,
102 cp = fdt_getprop(fdt, nodeoffset, "compatible", &cplen);
106 if (strncmp(cp, compat, strlen(compat)) == 0)
108 len = strlen(cp) + 1;
117 * Find a node by its device type property. On success, the offset of that
118 * node is returned or an error code otherwise:
120 * nodeoffset - the node to start searching from or 0, the node you pass
121 * will not be searched, only the next one will; typically,
122 * you pass 0 to start the search and then what the previous
124 * type - the device type string to match against.
126 int fdt_find_node_by_type(const void *fdt, int nodeoffset, const char *type)
128 int offset, nextoffset;
129 struct fdt_property *prop;
135 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
136 if (tag != FDT_BEGIN_NODE)
137 return -FDT_ERR_BADOFFSET;
139 nodeoffset = 0; /* start searching with next node */
143 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
153 ret = prop_name_eq(fdt, offset, "device_type",
158 strncmp(prop->data, type, len - 1) == 0)
167 return -FDT_ERR_NOTFOUND;
170 return -FDT_ERR_BADSTRUCTURE;
176 * Find a node based on its device type and one of the tokens in its its
177 * "compatible" property. On success, the offset of that node is returned
178 * or an error code otherwise:
180 * nodeoffset - the node to start searching from or 0, the node you pass
181 * will not be searched, only the next one will; typically,
182 * you pass 0 to start the search and then what the previous
184 * type - the device type string to match against.
185 * compat - the string to match to one of the tokens in the
188 int fdt_find_compatible_node(const void *fdt, int nodeoffset,
189 const char *type, const char *compat)
193 offset = fdt_find_node_by_type(fdt, nodeoffset, type);
194 if (offset < 0 || fdt_node_is_compatible(fdt, offset, compat))
197 return -FDT_ERR_NOTFOUND;
201 * Return the node offset of the node specified by:
202 * parentoffset - starting place (0 to start at the root)
203 * name - name being searched for
204 * namelen - length of the name: typically strlen(name)
207 * If the start node has subnodes, the subnodes are _not_ searched for the
210 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
211 const char *name, int namelen)
215 int offset, nextoffset;
219 tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
220 if (tag != FDT_BEGIN_NODE)
221 return -FDT_ERR_BADOFFSET;
225 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
229 return -FDT_ERR_TRUNCATED;
234 * If we are nested down levels, ignore the strings
235 * until we get back to the proper level.
240 /* Return the offset if this is "our" string. */
241 if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
254 return -FDT_ERR_BADSTRUCTURE;
256 } while (level >= 0);
258 return -FDT_ERR_NOTFOUND;
262 * See fdt_subnode_offset_namelen()
264 int fdt_subnode_offset(const void *fdt, int parentoffset,
267 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
271 * Searches for the node corresponding to the given path and returns the
272 * offset of that node.
274 int fdt_find_node_by_path(const void *fdt, const char *path)
276 const char *end = path + strlen(path);
277 const char *p = path;
282 /* Paths must be absolute */
284 return -FDT_ERR_BADPATH;
289 /* Skip path separator(s) */
293 return -FDT_ERR_BADPATH;
296 * Find the next path separator. The characters between
297 * p and q are the next segment of the the path to find.
304 * Find the offset corresponding to the this path segment.
306 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
308 /* Oops, error, abort abort abort */
319 * Given the offset of a node and a name of a property in that node, return
320 * a pointer to the property struct.
322 struct fdt_property *fdt_get_property(const void *fdt,
324 const char *name, int *lenp)
328 struct fdt_property *prop;
329 int offset, nextoffset;
332 if ((err = fdt_check_header(fdt)) != 0)
335 err = -FDT_ERR_BADOFFSET;
336 if (nodeoffset % FDT_TAGSIZE)
339 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
340 if (tag != FDT_BEGIN_NODE)
346 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
349 err = -FDT_ERR_TRUNCATED;
362 * If we are nested down levels, ignore the strings
363 * until we get back to the proper level.
368 err = prop_name_eq(fdt, offset, name, &prop, lenp);
379 err = -FDT_ERR_BADSTRUCTURE;
382 } while (level >= 0);
384 err = -FDT_ERR_NOTFOUND;
392 * Given the offset of a node and a name of a property in that node, return
393 * a pointer to the property data (ONLY).
395 void *fdt_getprop(const void *fdt, int nodeoffset,
396 const char *name, int *lenp)
398 const struct fdt_property *prop;
400 prop = fdt_get_property(fdt, nodeoffset, name, lenp);
404 return (void *)prop->data;
408 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
410 const uint32_t *tagp, *lenp;
414 if (offset % FDT_TAGSIZE)
417 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
419 return FDT_END; /* premature end */
420 tag = fdt32_to_cpu(*tagp);
421 offset += FDT_TAGSIZE;
426 *namep = fdt_offset_ptr(fdt, offset, 1);
430 p = fdt_offset_ptr(fdt, offset++, 1);
431 } while (p && (*p != '\0'));
436 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
440 * Get the property and set the namep to the name.
443 struct fdt_property *prop;
445 prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
447 return -FDT_ERR_BADSTRUCTURE;
448 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
450 /* skip name offset, length and value */
451 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
456 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
462 * Return the number of used reserve map entries and total slots available.
464 int fdt_num_reservemap(void *fdt, int *used, int *total)
466 struct fdt_reserve_entry *re;
469 int err = fdt_check_header(fdt);
474 start = fdt_off_mem_rsvmap(fdt);
477 * Convention is that the reserve map is before the dt_struct,
478 * but it does not have to be.
480 end = fdt_totalsize(fdt);
481 if (end > fdt_off_dt_struct(fdt))
482 end = fdt_off_dt_struct(fdt);
483 if (end > fdt_off_dt_strings(fdt))
484 end = fdt_off_dt_strings(fdt);
487 * Since the reserved area list is zero terminated, you get one fewer.
490 *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
494 while (start < end) {
495 re = (struct fdt_reserve_entry *)(fdt + start);
497 return 0; /* zero size terminates the list */
500 start += sizeof(struct fdt_reserve_entry);
503 * If we get here, there was no zero size termination.
505 return -FDT_ERR_BADLAYOUT;
511 * Return the nth reserve map entry.
513 int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
519 err = fdt_num_reservemap(fdt, &used, &total);
524 return -FDT_ERR_NOSPACE;
526 *re = *(struct fdt_reserve_entry *)
527 _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));
532 #endif /* CONFIG_OF_LIBFDT */