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;
286 /* Handle the root path: root offset is 0 */
287 if (strcmp(path, "/") == 0)
293 /* Skip path separator(s) */
297 return -FDT_ERR_BADPATH;
300 * Find the next path separator. The characters between
301 * p and q are the next segment of the the path to find.
308 * Find the offset corresponding to the this path segment.
310 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
312 /* Oops, error, abort abort abort */
323 * Given the offset of a node and a name of a property in that node, return
324 * a pointer to the property struct.
326 struct fdt_property *fdt_get_property(const void *fdt,
328 const char *name, int *lenp)
332 struct fdt_property *prop;
333 int offset, nextoffset;
336 if ((err = fdt_check_header(fdt)) != 0)
339 err = -FDT_ERR_BADOFFSET;
340 if (nodeoffset % FDT_TAGSIZE)
343 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
344 if (tag != FDT_BEGIN_NODE)
350 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
353 err = -FDT_ERR_TRUNCATED;
366 * If we are nested down levels, ignore the strings
367 * until we get back to the proper level.
372 err = prop_name_eq(fdt, offset, name, &prop, lenp);
383 err = -FDT_ERR_BADSTRUCTURE;
386 } while (level >= 0);
388 err = -FDT_ERR_NOTFOUND;
396 * Given the offset of a node and a name of a property in that node, return
397 * a pointer to the property data (ONLY).
399 void *fdt_getprop(const void *fdt, int nodeoffset,
400 const char *name, int *lenp)
402 const struct fdt_property *prop;
404 prop = fdt_get_property(fdt, nodeoffset, name, lenp);
408 return (void *)prop->data;
412 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
414 const uint32_t *tagp, *lenp;
418 if (offset % FDT_TAGSIZE)
421 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
423 return FDT_END; /* premature end */
424 tag = fdt32_to_cpu(*tagp);
425 offset += FDT_TAGSIZE;
430 *namep = fdt_offset_ptr(fdt, offset, 1);
434 p = fdt_offset_ptr(fdt, offset++, 1);
435 } while (p && (*p != '\0'));
440 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
444 * Get the property and set the namep to the name.
447 struct fdt_property *prop;
449 prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
451 return -FDT_ERR_BADSTRUCTURE;
452 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
454 /* skip name offset, length and value */
455 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
460 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
466 * Return the number of used reserve map entries and total slots available.
468 int fdt_num_reservemap(void *fdt, int *used, int *total)
470 struct fdt_reserve_entry *re;
473 int err = fdt_check_header(fdt);
478 start = fdt_off_mem_rsvmap(fdt);
481 * Convention is that the reserve map is before the dt_struct,
482 * but it does not have to be.
484 end = fdt_totalsize(fdt);
485 if (end > fdt_off_dt_struct(fdt))
486 end = fdt_off_dt_struct(fdt);
487 if (end > fdt_off_dt_strings(fdt))
488 end = fdt_off_dt_strings(fdt);
491 * Since the reserved area list is zero terminated, you get one fewer.
494 *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
498 while (start < end) {
499 re = (struct fdt_reserve_entry *)(fdt + start);
501 return 0; /* zero size terminates the list */
504 start += sizeof(struct fdt_reserve_entry);
507 * If we get here, there was no zero size termination.
509 return -FDT_ERR_BADLAYOUT;
515 * Return the nth reserve map entry.
517 int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
523 err = fdt_num_reservemap(fdt, &used, &total);
528 return -FDT_ERR_NOSPACE;
530 *re = *(struct fdt_reserve_entry *)
531 _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));
536 #endif /* CONFIG_OF_LIBFDT */