libfdt: Abolish fdt_property_offset()

fdt_property_offset() is the only function in the library returning a
direct offset to a property, and no function takes such an offset
(they only take offsets to nodes, not properties).  Furthermore the
only client uses for this function I can think of involve immediately
translating the offset into a pointer, effectively duplicating the
internal function _fdt_getprop()

This function abolishes fdt_property_offset(), replacing it with
fdt_get_property(), a renamed and now externally visible version of
_fdt_getprop().
This commit is contained in:
David Gibson 2006-12-11 16:15:34 +11:00
parent 1a765f51a4
commit 94993f4fc4
10 changed files with 84 additions and 100 deletions

134
fdt_ro.c
View file

@ -30,6 +30,13 @@
return OFFSET_ERROR(err); \
}
#define PTR_CHECK_HEADER(fdt) \
{ \
int err; \
if ((err = _fdt_check_header(fdt)) != 0) \
return PTR_ERROR(err); \
}
static int offset_streq(const struct fdt_header *fdt, int offset,
const char *s, int len)
{
@ -53,66 +60,6 @@ char *fdt_string(const struct fdt_header *fdt, int stroffset)
return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
}
int fdt_property_offset(const struct fdt_header *fdt, int nodeoffset,
const char *name)
{
int level = 0;
uint32_t tag;
struct fdt_property *prop;
int namestroff;
int offset, nextoffset;
OFFSET_CHECK_HEADER(fdt);
if (nodeoffset % FDT_TAGSIZE)
return OFFSET_ERROR(FDT_ERR_BADOFFSET);
tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return OFFSET_ERROR(FDT_ERR_BADOFFSET);
do {
offset = nextoffset;
if (offset % FDT_TAGSIZE)
return OFFSET_ERROR(FDT_ERR_INTERNAL);
tag = _fdt_next_tag(fdt, offset, &nextoffset);
switch (tag) {
case FDT_END:
return OFFSET_ERROR(FDT_ERR_TRUNCATED);
case FDT_BEGIN_NODE:
level++;
break;
case FDT_END_NODE:
level--;
break;
case FDT_PROP:
if (level != 0)
continue;
prop = fdt_offset_ptr_typed(fdt, offset, prop);
if (! prop)
return OFFSET_ERROR(FDT_ERR_BADSTRUCTURE);
namestroff = fdt32_to_cpu(prop->nameoff);
if (streq(fdt_string(fdt, namestroff), name))
/* Found it! */
return offset;
break;
case FDT_NOP:
break;
default:
return OFFSET_ERROR(FDT_ERR_BADSTRUCTURE);
}
} while (level >= 0);
return OFFSET_ERROR(FDT_ERR_NOTFOUND);
}
int fdt_subnode_offset_namelen(const struct fdt_header *fdt, int parentoffset,
const char *name, int namelen)
{
@ -197,23 +144,56 @@ int fdt_path_offset(const struct fdt_header *fdt, const char *path)
return offset;
}
struct fdt_property *_fdt_getprop(const struct fdt_header *fdt, int nodeoffset,
struct fdt_property *fdt_get_property(const struct fdt_header *fdt,
int nodeoffset,
const char *name, int *lenp)
{
int propoffset;
int level = 0;
uint32_t tag;
struct fdt_property *prop;
int err;
int len;
int namestroff;
int offset, nextoffset;
propoffset = fdt_property_offset(fdt, nodeoffset, name);
if ((err = fdt_offset_error(propoffset)))
return PTR_ERROR(err);
PTR_CHECK_HEADER(fdt);
prop = fdt_offset_ptr(fdt, propoffset, sizeof(prop));
if (nodeoffset % FDT_TAGSIZE)
return PTR_ERROR(FDT_ERR_BADOFFSET);
tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return PTR_ERROR(FDT_ERR_BADOFFSET);
do {
offset = nextoffset;
if (offset % FDT_TAGSIZE)
return PTR_ERROR(FDT_ERR_INTERNAL);
tag = _fdt_next_tag(fdt, offset, &nextoffset);
switch (tag) {
case FDT_END:
return PTR_ERROR(FDT_ERR_TRUNCATED);
case FDT_BEGIN_NODE:
level++;
break;
case FDT_END_NODE:
level--;
break;
case FDT_PROP:
if (level != 0)
continue;
prop = fdt_offset_ptr_typed(fdt, offset, prop);
if (! prop)
return PTR_ERROR(FDT_ERR_BADSTRUCTURE);
len = fdt32_to_cpu(prop->len);
prop = fdt_offset_ptr(fdt, propoffset, sizeof(prop) + len);
namestroff = fdt32_to_cpu(prop->nameoff);
if (streq(fdt_string(fdt, namestroff), name)) {
/* Found it! */
int len = fdt32_to_cpu(prop->len);
prop = fdt_offset_ptr(fdt, offset,
sizeof(prop)+len);
if (! prop)
return PTR_ERROR(FDT_ERR_BADSTRUCTURE);
@ -222,6 +202,18 @@ struct fdt_property *_fdt_getprop(const struct fdt_header *fdt, int nodeoffset,
return prop;
}
break;
case FDT_NOP:
break;
default:
return PTR_ERROR(FDT_ERR_BADSTRUCTURE);
}
} while (level >= 0);
return PTR_ERROR(FDT_ERR_NOTFOUND);
}
void *fdt_getprop(const struct fdt_header *fdt, int nodeoffset,
const char *name, int *lenp)
@ -229,7 +221,7 @@ void *fdt_getprop(const struct fdt_header *fdt, int nodeoffset,
const struct fdt_property *prop;
int err;
prop = _fdt_getprop(fdt, nodeoffset, name, lenp);
prop = fdt_get_property(fdt, nodeoffset, name, lenp);
if ((err = fdt_ptr_error(prop)))
return PTR_ERROR(err);

View file

@ -126,7 +126,7 @@ static struct fdt_property *_resize_property(struct fdt_header *fdt, int nodeoff
int oldlen;
int err;
prop = _fdt_getprop(fdt, nodeoffset, name, &oldlen);
prop = fdt_get_property(fdt, nodeoffset, name, &oldlen);
if ((err = fdt_ptr_error(prop)))
return PTR_ERROR(err);
@ -203,7 +203,7 @@ int fdt_delprop(struct fdt_header *fdt, int nodeoffset, const char *name)
RW_OFFSET_CHECK_HEADER(fdt);
prop = _fdt_getprop(fdt, nodeoffset, name, &len);
prop = fdt_get_property(fdt, nodeoffset, name, &len);
if ((err = fdt_ptr_error(prop)))
return err;

View file

@ -55,7 +55,7 @@ int fdt_nop_property(struct fdt_header *fdt, int nodeoffset, const char *name)
int len;
int err;
prop = _fdt_getprop(fdt, nodeoffset, name, &len);
prop = fdt_get_property(fdt, nodeoffset, name, &len);
if ((err = fdt_ptr_error(prop)))
return err;

View file

@ -72,8 +72,6 @@ struct fdt_header *fdt_move(const struct fdt_header *fdt, void *buf, int bufsize
/* Read-only functions */
char *fdt_string(const struct fdt_header *fdt, int stroffset);
int fdt_property_offset(const struct fdt_header *fdt, int nodeoffset,
const char *name);
int fdt_subnode_offset_namelen(const struct fdt_header *fdt, int parentoffset,
const char *name, int namelen);
int fdt_subnode_offset(const struct fdt_header *fdt, int parentoffset,
@ -81,6 +79,9 @@ int fdt_subnode_offset(const struct fdt_header *fdt, int parentoffset,
int fdt_path_offset(const struct fdt_header *fdt, const char *path);
struct fdt_property *fdt_get_property(const struct fdt_header *fdt,
int nodeoffset,
const char *name, int *lenp);
void *fdt_getprop(const struct fdt_header *fdt, int nodeoffset,
const char *name, int *lenp);

View file

@ -35,8 +35,6 @@ static inline int _ptr_offset(struct fdt_header *fdt, void *p)
int _fdt_check_header(const struct fdt_header *fdt);
uint32_t _fdt_next_tag(const struct fdt_header *fdt, int startoffset, int *nextoffset);
struct fdt_property *_fdt_getprop(const struct fdt_header *fdt, int nodeoffset,
const char *name, int *lenp);
const char *_fdt_find_string(const char *strtab, int tabsize, const char *s);
int _fdt_node_end_offset(struct fdt_header *fdt, int nodeoffset);

View file

@ -1,6 +1,6 @@
PREFIX = /usr/local
LIB_TESTS = root_node property_offset subnode_offset path_offset getprop \
LIB_TESTS = root_node find_property subnode_offset path_offset getprop \
notfound \
setprop_inplace nop_property nop_node \
sw_tree1 \

View file

@ -36,6 +36,7 @@ void check_error(const char *s, int err)
int main(int argc, char *argv[])
{
struct fdt_property *prop;
struct fdt_header *fdt;
int offset;
int subnode1_offset;
@ -45,9 +46,9 @@ int main(int argc, char *argv[])
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
offset = fdt_property_offset(fdt, 0, "nonexistant-property");
check_error("fdt_property_offset(\"nonexistant-property\")",
fdt_offset_error(offset));
prop = fdt_get_property(fdt, 0, "nonexistant-property", NULL);
check_error("fdt_get_property(\"nonexistant-property\")",
fdt_ptr_error(prop));
val = fdt_getprop(fdt, 0, "nonexistant-property", NULL);
check_error("fdt_getprop(\"nonexistant-property\"",

View file

@ -14,7 +14,7 @@ tree1_tests () {
# Read-only tests
run_test root_node $TREE
run_test property_offset $TREE
run_test find_property $TREE
run_test subnode_offset $TREE
run_test path_offset $TREE
run_test getprop $TREE

View file

@ -106,20 +106,12 @@ const char *fdt_strerror(int errval)
void check_property(struct fdt_header *fdt, int nodeoffset, const char *name,
int len, const void *val)
{
int offset;
const struct fdt_property *prop;
uint32_t tag, nameoff, proplen;
const char *propname;
int err;
verbose_printf("Checking property \"%s\"...", name);
offset = fdt_property_offset(fdt, nodeoffset, name);
verbose_printf("offset %d...", offset);
if ((err = fdt_offset_error(offset)))
FAIL("fdt_property_offset(\"%s\"): %s", name,
fdt_strerror(err));
prop = fdt_offset_ptr_typed(fdt, offset, prop);
prop = fdt_get_property(fdt, nodeoffset, name, NULL);
verbose_printf("pointer %p\n", prop);
if (! prop)
FAIL("NULL retreiving \"%s\" pointer", name);