libfdt: Add fdt_parent_offset() and supporting functions

This patch adds an fdt_parent_offset() function which returns an
offset to the parent node of a given node.  It also adds two helper
functions which are used to implement fdt_parent_offset() but are also
exported: fdt_supernode_atdepth_offset() which returns the ancestor of
a given node at a specified depth from the root of the tree, and
fdt_node_depth() which returns the depth of a given node.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2007-08-30 14:54:04 +10:00 committed by Jon Loeliger
parent 037db263e0
commit 1248237c7e
6 changed files with 314 additions and 2 deletions

View file

@ -349,3 +349,71 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
buf[p] = '\0';
return p;
}
int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
int supernodedepth, int *nodedepth)
{
int level = -1;
uint32_t tag;
int offset, nextoffset = 0;
int supernodeoffset = -FDT_ERR_INTERNAL;
CHECK_HEADER(fdt);
if (supernodedepth < 0)
return -FDT_ERR_NOTFOUND;
do {
offset = nextoffset;
tag = _fdt_next_tag(fdt, offset, &nextoffset);
switch (tag) {
case FDT_END:
return -FDT_ERR_BADOFFSET;
case FDT_BEGIN_NODE:
level++;
if (level == supernodedepth)
supernodeoffset = offset;
break;
case FDT_END_NODE:
level--;
break;
case FDT_PROP:
case FDT_NOP:
break;
default:
return -FDT_ERR_BADSTRUCTURE;
}
} while (offset < nodeoffset);
if (nodedepth)
*nodedepth = level;
if (supernodedepth > level)
return -FDT_ERR_NOTFOUND;
return supernodeoffset;
}
int fdt_node_depth(const void *fdt, int nodeoffset)
{
int nodedepth;
int err;
err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
if (err)
return (err < 0) ? err : -FDT_ERR_INTERNAL;
return nodedepth;
}
int fdt_parent_offset(const void *fdt, int nodeoffset)
{
int nodedepth = fdt_node_depth(fdt, nodeoffset);
if (nodedepth < 0)
return nodedepth;
return fdt_supernode_atdepth_offset(fdt, nodeoffset,
nodedepth - 1, NULL);
}

View file

@ -74,7 +74,10 @@
#define FDT_ERR_BADSTRUCTURE 10
#define FDT_ERR_BADLAYOUT 11
#define FDT_ERR_MAX 11
/* "Can't happen" error indicating a bug in libfdt */
#define FDT_ERR_INTERNAL 12
#define FDT_ERR_MAX 12
#define fdt_get_header(fdt, field) \
(fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
@ -138,6 +141,11 @@ static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
int supernodedepth, int *nodedepth);
int fdt_node_depth(const void *fdt, int nodeoffset);
int fdt_parent_offset(const void *fdt, int nodeoffset);
/* Write-in-place functions */
int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
const void *val, int len);