Browse Source

libfdt: More consistent handling of returned error codes.

At present, libfdt functions returning a structure offset return a
zero-or-positive offset on success, and return a negative error code
on failure.  Functions which only return an error code return a
positive version of the error code, or 0 on success.

This patch improves consistency by always returning negative error
codes on failure, for both types of function.  With this change, we do
away with the special fdt_offset_error() macro for checking whether a
returned offset value is an error and extracting the encoded error
value within.  Instead an explicit (ret_value < 0) is now the
preferred way of checking return values for both offset-returning and
error-code-returning functions.

The fdt_strerror() function in the test code is updated
correspondingly to make more sense with the new conventions.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
David Gibson 18 years ago
parent
commit
9a9fdf5991
  1. 12
      fdt.c
  2. 36
      fdt_ro.c
  3. 53
      fdt_rw.c
  4. 22
      fdt_sw.c
  5. 15
      fdt_wip.c
  6. 6
      libfdt.h
  7. 2
      libfdt_internal.h
  8. 43
      tests/del_node.c
  9. 10
      tests/del_property.c
  10. 49
      tests/nop_node.c
  11. 4
      tests/nop_property.c
  12. 26
      tests/notfound.c
  13. 5
      tests/path_offset.c
  14. 6
      tests/rw_tree1.c
  15. 5
      tests/subnode_offset.c
  16. 8
      tests/testutils.c
  17. 5
      tests/truncated_property.c

12
fdt.c

@ -28,15 +28,15 @@ int _fdt_check_header(const void *fdt) @@ -28,15 +28,15 @@ int _fdt_check_header(const void *fdt)
if (fdt_magic(fdt) == FDT_MAGIC) {
/* Complete tree */
if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
return FDT_ERR_BADVERSION;
return -FDT_ERR_BADVERSION;
if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
return FDT_ERR_BADVERSION;
return -FDT_ERR_BADVERSION;
} else if (fdt_magic(fdt) == SW_MAGIC) {
/* Unfinished sequential-write blob */
if (fdt_size_dt_struct(fdt) == 0)
return FDT_ERR_BADSTATE;
return -FDT_ERR_BADSTATE;
} else {
return FDT_ERR_BADMAGIC;
return -FDT_ERR_BADMAGIC;
}

return 0;
@ -117,8 +117,8 @@ int fdt_move(const void *fdt, void *buf, int bufsize) @@ -117,8 +117,8 @@ int fdt_move(const void *fdt, void *buf, int bufsize)
return err;

if (fdt_totalsize(fdt) > bufsize)
return FDT_ERR_NOSPACE;
return -FDT_ERR_NOSPACE;

memmove(buf, fdt, fdt_totalsize(fdt));
return FDT_ERR_OK;
return 0;
}

36
fdt_ro.c

@ -23,11 +23,11 @@ @@ -23,11 +23,11 @@

#include "libfdt_internal.h"

#define OFFSET_CHECK_HEADER(fdt) \
#define CHECK_HEADER(fdt) \
{ \
int err; \
if ((err = _fdt_check_header(fdt)) != 0) \
return OFFSET_ERROR(err); \
return err; \
}

static int offset_streq(const void *fdt, int offset,
@ -60,11 +60,11 @@ int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, @@ -60,11 +60,11 @@ int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
uint32_t tag;
int offset, nextoffset;

OFFSET_CHECK_HEADER(fdt);
CHECK_HEADER(fdt);

tag = _fdt_next_tag(fdt, parentoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return OFFSET_ERROR(FDT_ERR_BADOFFSET);
return -FDT_ERR_BADOFFSET;

do {
offset = nextoffset;
@ -72,7 +72,7 @@ int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, @@ -72,7 +72,7 @@ int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,

switch (tag) {
case FDT_END:
return OFFSET_ERROR(FDT_ERR_TRUNCATED);
return -FDT_ERR_TRUNCATED;

case FDT_BEGIN_NODE:
level++;
@ -92,11 +92,11 @@ int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, @@ -92,11 +92,11 @@ int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
break;

default:
return OFFSET_ERROR(FDT_ERR_BADSTRUCTURE);
return -FDT_ERR_BADSTRUCTURE;
}
} while (level >= 0);

return OFFSET_ERROR(FDT_ERR_NOTFOUND);
return -FDT_ERR_NOTFOUND;
}

int fdt_subnode_offset(const void *fdt, int parentoffset,
@ -111,10 +111,10 @@ int fdt_path_offset(const void *fdt, const char *path) @@ -111,10 +111,10 @@ int fdt_path_offset(const void *fdt, const char *path)
const char *p = path;
int offset = 0;

OFFSET_CHECK_HEADER(fdt);
CHECK_HEADER(fdt);

if (*path != '/')
return OFFSET_ERROR(FDT_ERR_BADPATH);
return -FDT_ERR_BADPATH;

while (*p) {
const char *q;
@ -122,13 +122,13 @@ int fdt_path_offset(const void *fdt, const char *path) @@ -122,13 +122,13 @@ int fdt_path_offset(const void *fdt, const char *path)
while (*p == '/')
p++;
if (! *p)
return OFFSET_ERROR(FDT_ERR_BADPATH);
return -FDT_ERR_BADPATH;
q = strchr(p, '/');
if (! q)
q = end;

offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
if (fdt_offset_error(offset))
if (offset < 0)
return offset;

p = q;
@ -151,7 +151,7 @@ struct fdt_property *fdt_get_property(const void *fdt, @@ -151,7 +151,7 @@ struct fdt_property *fdt_get_property(const void *fdt,
if ((err = _fdt_check_header(fdt)) != 0)
goto fail;

err = FDT_ERR_BADOFFSET;
err = -FDT_ERR_BADOFFSET;
if (nodeoffset % FDT_TAGSIZE)
goto fail;

@ -161,14 +161,14 @@ struct fdt_property *fdt_get_property(const void *fdt, @@ -161,14 +161,14 @@ struct fdt_property *fdt_get_property(const void *fdt,

do {
offset = nextoffset;
err = FDT_ERR_INTERNAL;
err = -FDT_ERR_INTERNAL;
if (offset % FDT_TAGSIZE)
goto fail;

tag = _fdt_next_tag(fdt, offset, &nextoffset);
switch (tag) {
case FDT_END:
err = FDT_ERR_TRUNCATED;
err = -FDT_ERR_TRUNCATED;
goto fail;

case FDT_BEGIN_NODE:
@ -183,7 +183,7 @@ struct fdt_property *fdt_get_property(const void *fdt, @@ -183,7 +183,7 @@ struct fdt_property *fdt_get_property(const void *fdt,
if (level != 0)
continue;

err = FDT_ERR_BADSTRUCTURE;
err = -FDT_ERR_BADSTRUCTURE;
prop = fdt_offset_ptr_typed(fdt, offset, prop);
if (! prop)
goto fail;
@ -207,15 +207,15 @@ struct fdt_property *fdt_get_property(const void *fdt, @@ -207,15 +207,15 @@ struct fdt_property *fdt_get_property(const void *fdt,
break;

default:
err = FDT_ERR_BADSTRUCTURE;
err = -FDT_ERR_BADSTRUCTURE;
goto fail;
}
} while (level >= 0);

err = FDT_ERR_NOTFOUND;
err = -FDT_ERR_NOTFOUND;
fail:
if (lenp)
*lenp = -err;
*lenp = err;
return NULL;
}


53
fdt_rw.c

@ -30,26 +30,26 @@ static int rw_check_header(void *fdt) @@ -30,26 +30,26 @@ static int rw_check_header(void *fdt)
if ((err = _fdt_check_header(fdt)))
return err;
if (fdt_version(fdt) < 0x11)
return FDT_ERR_BADVERSION;
return -FDT_ERR_BADVERSION;
if (fdt_off_mem_rsvmap(fdt) < ALIGN(sizeof(struct fdt_header), 8))
return FDT_ERR_BADLAYOUT;
return -FDT_ERR_BADLAYOUT;
if (fdt_off_dt_struct(fdt) <
(fdt_off_mem_rsvmap(fdt) + sizeof(struct fdt_reserve_entry)))
return FDT_ERR_BADLAYOUT;
return -FDT_ERR_BADLAYOUT;
if (fdt_off_dt_strings(fdt) <
(fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt)))
return FDT_ERR_BADLAYOUT;
return -FDT_ERR_BADLAYOUT;
if (fdt_totalsize(fdt) <
(fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)))
return FDT_ERR_BADLAYOUT;
return -FDT_ERR_BADLAYOUT;
return 0;
}

#define RW_OFFSET_CHECK_HEADER(fdt) \
#define RW_CHECK_HEADER(fdt) \
{ \
int err; \
if ((err = rw_check_header(fdt)) != 0) \
return OFFSET_ERROR(err); \
return err; \
}

static inline int _blob_data_size(void *fdt)
@ -62,9 +62,9 @@ static int _blob_splice(void *fdt, void *p, int oldlen, int newlen) @@ -62,9 +62,9 @@ static int _blob_splice(void *fdt, void *p, int oldlen, int newlen)
void *end = fdt + _blob_data_size(fdt);

if (((p + oldlen) < p) || ((p + oldlen) > end))
return FDT_ERR_BADOFFSET;
return -FDT_ERR_BADOFFSET;
if ((end - oldlen + newlen) > (fdt + fdt_totalsize(fdt)))
return FDT_ERR_NOSPACE;
return -FDT_ERR_NOSPACE;
memmove(p + newlen, p + oldlen, end - p - oldlen);
return 0;
}
@ -111,7 +111,7 @@ static int _find_add_string(void *fdt, const char *s) @@ -111,7 +111,7 @@ static int _find_add_string(void *fdt, const char *s)
new = strtab + fdt_size_dt_strings(fdt);
err = _blob_splice_string(fdt, len);
if (err)
return -err;
return err;

memcpy(new, s, len);
return (new - strtab);
@ -125,7 +125,7 @@ static int _resize_property(void *fdt, int nodeoffset, const char *name, int len @@ -125,7 +125,7 @@ static int _resize_property(void *fdt, int nodeoffset, const char *name, int len

*prop = fdt_get_property(fdt, nodeoffset, name, &oldlen);
if (! (*prop))
return -oldlen;
return oldlen;

if ((err = _blob_splice_struct(fdt, (*prop)->data,
ALIGN(oldlen, FDT_TAGSIZE),
@ -147,11 +147,11 @@ static int _add_property(void *fdt, int nodeoffset, const char *name, int len, @@ -147,11 +147,11 @@ static int _add_property(void *fdt, int nodeoffset, const char *name, int len,

tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return FDT_ERR_BADOFFSET;
return -FDT_ERR_BADOFFSET;

namestroff = _find_add_string(fdt, name);
if (namestroff < 0)
return -namestroff;
return namestroff;

*prop = _fdt_offset_ptr(fdt, nextoffset);
proplen = sizeof(**prop) + ALIGN(len, FDT_TAGSIZE);
@ -176,7 +176,7 @@ int fdt_setprop(void *fdt, int nodeoffset, const char *name, @@ -176,7 +176,7 @@ int fdt_setprop(void *fdt, int nodeoffset, const char *name,
return err;

err = _resize_property(fdt, nodeoffset, name, len, &prop);
if (err == FDT_ERR_NOTFOUND)
if (err == -FDT_ERR_NOTFOUND)
err = _add_property(fdt, nodeoffset, name, len, &prop);
if (err)
return err;
@ -190,11 +190,11 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name) @@ -190,11 +190,11 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name)
struct fdt_property *prop;
int len, proplen;

RW_OFFSET_CHECK_HEADER(fdt);
RW_CHECK_HEADER(fdt);

prop = fdt_get_property(fdt, nodeoffset, name, &len);
if (! prop)
return -len;
return len;

proplen = sizeof(*prop) + ALIGN(len, FDT_TAGSIZE);
return _blob_splice_struct(fdt, prop, proplen, 0);
@ -210,13 +210,13 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset, @@ -210,13 +210,13 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,
uint32_t tag;
uint32_t *endtag;

RW_OFFSET_CHECK_HEADER(fdt);
RW_CHECK_HEADER(fdt);

offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
if ((err = fdt_offset_error(offset)) == 0)
return OFFSET_ERROR(FDT_ERR_EXISTS);
else if (err != FDT_ERR_NOTFOUND)
return OFFSET_ERROR(err);
if (offset >= 0)
return -FDT_ERR_EXISTS;
else if (offset != -FDT_ERR_NOTFOUND)
return offset;

/* Try to place the new node after the parent's properties */
_fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
@ -230,7 +230,7 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset, @@ -230,7 +230,7 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset,

err = _blob_splice_struct(fdt, nh, 0, nodelen);
if (err)
return OFFSET_ERROR(err);
return err;

nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
memset(nh->name, 0, ALIGN(namelen+1, FDT_TAGSIZE));
@ -249,11 +249,10 @@ int fdt_add_subnode(void *fdt, int parentoffset, const char *name) @@ -249,11 +249,10 @@ int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
int fdt_del_node(void *fdt, int nodeoffset)
{
int endoffset;
int err;

endoffset = _fdt_node_end_offset(fdt, nodeoffset);
if ((err = fdt_offset_error(endoffset)))
return err;
if (endoffset < 0)
return endoffset;

return _blob_splice_struct(fdt, _fdt_offset_ptr(fdt, nodeoffset),
endoffset - nodeoffset, 0);
@ -277,7 +276,7 @@ int fdt_open_into(void *fdt, void *buf, int bufsize) @@ -277,7 +276,7 @@ int fdt_open_into(void *fdt, void *buf, int bufsize)
if (err)
return err;

return FDT_ERR_OK;
return 0;
}

int fdt_pack(void *fdt)
@ -290,5 +289,5 @@ int fdt_pack(void *fdt) @@ -290,5 +289,5 @@ int fdt_pack(void *fdt)

/* FIXME: pack components */
fdt_set_header(fdt, totalsize, _blob_data_size(fdt));
return FDT_ERR_OK;
return 0;
}

22
fdt_sw.c

@ -26,7 +26,7 @@ @@ -26,7 +26,7 @@
static int check_header_sw(void *fdt)
{
if (fdt_magic(fdt) != SW_MAGIC)
return FDT_ERR_BADMAGIC;
return -FDT_ERR_BADMAGIC;
return 0;
}

@ -50,7 +50,7 @@ int fdt_create(void *buf, int bufsize) @@ -50,7 +50,7 @@ int fdt_create(void *buf, int bufsize)
void *fdt = buf;

if (bufsize < sizeof(struct fdt_header))
return FDT_ERR_NOSPACE;
return -FDT_ERR_NOSPACE;

memset(buf, 0, bufsize);

@ -64,7 +64,7 @@ int fdt_create(void *buf, int bufsize) @@ -64,7 +64,7 @@ int fdt_create(void *buf, int bufsize)
fdt_set_header(fdt, off_dt_struct, fdt_off_mem_rsvmap(fdt));
fdt_set_header(fdt, off_dt_strings, bufsize);

return FDT_ERR_OK;
return 0;
}

int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
@ -76,11 +76,11 @@ int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size) @@ -76,11 +76,11 @@ int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
if (err)
return err;
if (fdt_size_dt_struct(fdt))
return FDT_ERR_BADSTATE;
return -FDT_ERR_BADSTATE;

offset = fdt_off_dt_struct(fdt);
if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
return FDT_ERR_NOSPACE;
return -FDT_ERR_NOSPACE;

re = (struct fdt_reserve_entry *)((void *)fdt + offset);
re->address = cpu_to_fdt64(addr);
@ -107,7 +107,7 @@ int fdt_begin_node(void *fdt, const char *name) @@ -107,7 +107,7 @@ int fdt_begin_node(void *fdt, const char *name)

nh = grab_space(fdt, sizeof(*nh) + ALIGN(namelen, FDT_TAGSIZE));
if (! nh)
return FDT_ERR_NOSPACE;
return -FDT_ERR_NOSPACE;

nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
memcpy(nh->name, name, namelen);
@ -124,7 +124,7 @@ int fdt_end_node(void *fdt) @@ -124,7 +124,7 @@ int fdt_end_node(void *fdt)

en = grab_space(fdt, FDT_TAGSIZE);
if (! en)
return FDT_ERR_NOSPACE;
return -FDT_ERR_NOSPACE;

*en = cpu_to_fdt32(FDT_END_NODE);
return 0;
@ -164,11 +164,11 @@ int fdt_property(void *fdt, const char *name, const void *val, int len) @@ -164,11 +164,11 @@ int fdt_property(void *fdt, const char *name, const void *val, int len)

nameoff = find_add_string(fdt, name);
if (nameoff == 0)
return FDT_ERR_NOSPACE;
return -FDT_ERR_NOSPACE;

prop = grab_space(fdt, sizeof(*prop) + ALIGN(len, FDT_TAGSIZE));
if (! prop)
return FDT_ERR_NOSPACE;
return -FDT_ERR_NOSPACE;

prop->tag = cpu_to_fdt32(FDT_PROP);
prop->nameoff = cpu_to_fdt32(nameoff);
@ -192,7 +192,7 @@ int fdt_finish(void *fdt) @@ -192,7 +192,7 @@ int fdt_finish(void *fdt)
/* Add terminator */
end = grab_space(fdt, sizeof(*end));
if (! end)
return FDT_ERR_NOSPACE;
return -FDT_ERR_NOSPACE;
*end = cpu_to_fdt32(FDT_END);

/* Relocate the string table */
@ -210,7 +210,7 @@ int fdt_finish(void *fdt) @@ -210,7 +210,7 @@ int fdt_finish(void *fdt)
int nameoff;

if (! prop)
return FDT_ERR_BADSTRUCTURE;
return -FDT_ERR_BADSTRUCTURE;

nameoff = fdt32_to_cpu(prop->nameoff);
nameoff += fdt_size_dt_strings(fdt);

15
fdt_wip.c

@ -31,10 +31,10 @@ int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, @@ -31,10 +31,10 @@ int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,

propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
if (! propval)
return -proplen;
return proplen;

if (proplen != len)
return FDT_ERR_SIZE_MISMATCH;
return -FDT_ERR_SIZE_MISMATCH;

memcpy(propval, val, len);
return 0;
@ -55,7 +55,7 @@ int fdt_nop_property(void *fdt, int nodeoffset, const char *name) @@ -55,7 +55,7 @@ int fdt_nop_property(void *fdt, int nodeoffset, const char *name)

prop = fdt_get_property(fdt, nodeoffset, name, &len);
if (! prop)
return -len;
return len;

nop_region(prop, len + sizeof(*prop));

@ -70,7 +70,7 @@ int _fdt_node_end_offset(void *fdt, int nodeoffset) @@ -70,7 +70,7 @@ int _fdt_node_end_offset(void *fdt, int nodeoffset)

tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return FDT_ERR_BADOFFSET;
return -FDT_ERR_BADOFFSET;
do {
offset = nextoffset;
tag = _fdt_next_tag(fdt, offset, &nextoffset);
@ -92,7 +92,7 @@ int _fdt_node_end_offset(void *fdt, int nodeoffset) @@ -92,7 +92,7 @@ int _fdt_node_end_offset(void *fdt, int nodeoffset)
break;

default:
return FDT_ERR_BADSTRUCTURE;
return -FDT_ERR_BADSTRUCTURE;
}
} while (level >= 0);

@ -102,11 +102,10 @@ int _fdt_node_end_offset(void *fdt, int nodeoffset) @@ -102,11 +102,10 @@ int _fdt_node_end_offset(void *fdt, int nodeoffset)
int fdt_nop_node(void *fdt, int nodeoffset)
{
int endoffset;
int err;

endoffset = _fdt_node_end_offset(fdt, nodeoffset);
if ((err = fdt_offset_error(endoffset)))
return err;
if (endoffset < 0)
return endoffset;

nop_region(fdt_offset_ptr(fdt, nodeoffset, 0), endoffset - nodeoffset);
return 0;

6
libfdt.h

@ -66,12 +66,6 @@ void *fdt_offset_ptr(const void *fdt, int offset, int checklen); @@ -66,12 +66,6 @@ void *fdt_offset_ptr(const void *fdt, int offset, int checklen);
#define fdt_offset_ptr_typed(fdt, offset, var) \
((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var)))))

#define fdt_offset_error(offset) \
( (offset) < 0 ? -(offset) : 0 )

#define fdt_ptrlen_error(p, len) \
( (p) ? 0 : -(len) )

int fdt_move(const void *fdt, void *buf, int bufsize);

/* Read-only functions */

2
libfdt_internal.h

@ -36,8 +36,6 @@ static inline void *_fdt_offset_ptr(const struct fdt_header *fdt, int offset) @@ -36,8 +36,6 @@ static inline void *_fdt_offset_ptr(const struct fdt_header *fdt, int offset)
return (void *)fdt + fdt_off_dt_struct(fdt) + offset;
}

#define OFFSET_ERROR(code) -(code)

#define SW_MAGIC (~FDT_MAGIC)

#endif /* _LIBFDT_INTERNAL_H */

43
tests/del_node.c

@ -42,19 +42,21 @@ int main(int argc, char *argv[]) @@ -42,19 +42,21 @@ int main(int argc, char *argv[])
oldsize = fdt_totalsize(fdt);

subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if ((err = fdt_offset_error(subnode1_offset)))
FAIL("Couldn't find \"/subnode1\": %s", fdt_strerror(err));
if (subnode1_offset < 0)
FAIL("Couldn't find \"/subnode1\": %s",
fdt_strerror(subnode1_offset));
check_getprop_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);

subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if ((err = fdt_offset_error(subnode2_offset)))
FAIL("Couldn't find \"/subnode2\": %s", fdt_strerror(err));
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);

subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if ((err = fdt_offset_error(subsubnode2_offset)))
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(err));
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);

err = fdt_del_node(fdt, subnode1_offset);
@ -62,19 +64,21 @@ int main(int argc, char *argv[]) @@ -62,19 +64,21 @@ int main(int argc, char *argv[])
FAIL("fdt_del_node(subnode1): %s", fdt_strerror(err));

subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if ((err = fdt_offset_error(subnode1_offset)) != FDT_ERR_NOTFOUND)
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));

subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if ((err = fdt_offset_error(subnode2_offset)))
FAIL("Couldn't find \"/subnode2\": %s", fdt_strerror(err));
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);

subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if ((err = fdt_offset_error(subsubnode2_offset)))
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(err));
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);

err = fdt_del_node(fdt, subnode2_offset);
@ -82,19 +86,22 @@ int main(int argc, char *argv[]) @@ -82,19 +86,22 @@ int main(int argc, char *argv[])
FAIL("fdt_del_node(subnode2): %s", fdt_strerror(err));

subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if ((err = fdt_offset_error(subnode1_offset)) != FDT_ERR_NOTFOUND)
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));

subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if ((err = fdt_offset_error(subnode2_offset)) != FDT_ERR_NOTFOUND)
if (subnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
fdt_strerror(subnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));

subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if ((err = fdt_offset_error(subsubnode2_offset)) != FDT_ERR_NOTFOUND)
if (subsubnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
fdt_strerror(subsubnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));

delsize = fdt_totalsize(fdt);


10
tests/del_property.c

@ -52,8 +52,9 @@ int main(int argc, char *argv[]) @@ -52,8 +52,9 @@ int main(int argc, char *argv[])
intp = fdt_getprop(fdt, 0, "prop-int", &lenerr);
if (intp)
FAIL("prop-int still present after deletion");
if ((err = fdt_ptrlen_error(intp, lenerr)) != FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s",
fdt_strerror(lenerr));

strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
@ -65,8 +66,9 @@ int main(int argc, char *argv[]) @@ -65,8 +66,9 @@ int main(int argc, char *argv[])
strp = fdt_getprop(fdt, 0, "prop-str", &lenerr);
if (strp)
FAIL("prop-str still present after deletion");
if ((err = fdt_ptrlen_error(strp, lenerr)) != FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s",
fdt_strerror(lenerr));

delsize = fdt_totalsize(fdt);


49
tests/nop_node.c

@ -39,19 +39,21 @@ int main(int argc, char *argv[]) @@ -39,19 +39,21 @@ int main(int argc, char *argv[])
fdt = load_blob_arg(argc, argv);

subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if ((err = fdt_offset_error(subnode1_offset)))
FAIL("Couldn't find \"/subnode1\": %s", fdt_strerror(err));
if (subnode1_offset < 0)
FAIL("Couldn't find \"/subnode1\": %s",
fdt_strerror(subnode1_offset));
check_getprop_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);

subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if ((err = fdt_offset_error(subnode2_offset)))
FAIL("Couldn't find \"/subnode2\": %s", fdt_strerror(err));
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);

subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if ((err = fdt_offset_error(subsubnode2_offset)))
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(err));
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);

err = fdt_nop_node(fdt, subnode1_offset);
@ -59,19 +61,21 @@ int main(int argc, char *argv[]) @@ -59,19 +61,21 @@ int main(int argc, char *argv[])
FAIL("fdt_nop_node(subnode1): %s", fdt_strerror(err));

subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if ((err = fdt_offset_error(subnode1_offset)) != FDT_ERR_NOTFOUND)
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));

subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if ((err = fdt_offset_error(subnode2_offset)))
FAIL("Couldn't find \"/subnode2\": %s", fdt_strerror(err));
if (subnode2_offset < 0)
FAIL("Couldn't find \"/subnode2\": %s",
fdt_strerror(subnode2_offset));
check_getprop_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);

subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if ((err = fdt_offset_error(subsubnode2_offset)))
if (subsubnode2_offset < 0)
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(err));
fdt_strerror(subsubnode2_offset));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);

err = fdt_nop_node(fdt, subnode2_offset);
@ -79,19 +83,22 @@ int main(int argc, char *argv[]) @@ -79,19 +83,22 @@ int main(int argc, char *argv[])
FAIL("fdt_nop_node(subnode2): %s", fdt_strerror(err));

subnode1_offset = fdt_path_offset(fdt, "/subnode1");
if ((err = fdt_offset_error(subnode1_offset)) != FDT_ERR_NOTFOUND)
if (subnode1_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
fdt_strerror(subnode1_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));

subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if ((err = fdt_offset_error(subnode2_offset)) != FDT_ERR_NOTFOUND)
if (subnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
fdt_strerror(subnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));

subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if ((err = fdt_offset_error(subsubnode2_offset)) != FDT_ERR_NOTFOUND)
if (subsubnode2_offset != -FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
fdt_strerror(subsubnode2_offset),
fdt_strerror(-FDT_ERR_NOTFOUND));

PASS();
}

4
tests/nop_property.c

@ -50,7 +50,7 @@ int main(int argc, char *argv[]) @@ -50,7 +50,7 @@ int main(int argc, char *argv[])
intp = fdt_getprop(fdt, 0, "prop-int", &lenerr);
if (intp)
FAIL("prop-int still present after nopping");
if ((err = fdt_ptrlen_error(intp, lenerr)) != FDT_ERR_NOTFOUND)
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
@ -63,7 +63,7 @@ int main(int argc, char *argv[]) @@ -63,7 +63,7 @@ int main(int argc, char *argv[])
strp = fdt_getprop(fdt, 0, "prop-str", &lenerr);
if (strp)
FAIL("prop-str still present after nopping");
if ((err = fdt_ptrlen_error(strp, lenerr)) != FDT_ERR_NOTFOUND)
if (lenerr != -FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));

PASS();

26
tests/notfound.c

@ -29,8 +29,8 @@ @@ -29,8 +29,8 @@

void check_error(const char *s, int err)
{
if (err != FDT_ERR_NOTFOUND)
FAIL("%s return error %s instead of FDT_ERR_NOTFOUND", s,
if (err != -FDT_ERR_NOTFOUND)
FAIL("%s return error %s instead of -FDT_ERR_NOTFOUND", s,
fdt_strerror(err));
}

@ -41,38 +41,32 @@ int main(int argc, char *argv[]) @@ -41,38 +41,32 @@ int main(int argc, char *argv[])
int offset;
int subnode1_offset;
void *val;
int err;
int lenerr;

test_init(argc, argv);
fdt = load_blob_arg(argc, argv);

prop = fdt_get_property(fdt, 0, "nonexistant-property", &lenerr);
check_error("fdt_get_property(\"nonexistant-property\")",
fdt_ptrlen_error(prop, lenerr));
check_error("fdt_get_property(\"nonexistant-property\")", lenerr);

val = fdt_getprop(fdt, 0, "nonexistant-property", &lenerr);
check_error("fdt_getprop(\"nonexistant-property\"",
fdt_ptrlen_error(val, lenerr));
check_error("fdt_getprop(\"nonexistant-property\"", lenerr);

subnode1_offset = fdt_subnode_offset(fdt, 0, "subnode1");
if ((err = fdt_offset_error(subnode1_offset)))
FAIL("Couldn't find subnode1: %s", fdt_strerror(err));
if (subnode1_offset < 0)
FAIL("Couldn't find subnode1: %s", fdt_strerror(subnode1_offset));

val = fdt_getprop(fdt, subnode1_offset, "prop-str", &lenerr);
check_error("fdt_getprop(\"prop-str\")", fdt_ptrlen_error(val, lenerr));
check_error("fdt_getprop(\"prop-str\")", lenerr);

offset = fdt_subnode_offset(fdt, 0, "nonexistant-subnode");
check_error("fdt_subnode_offset(\"nonexistant-subnode\")",
fdt_offset_error(offset));
check_error("fdt_subnode_offset(\"nonexistant-subnode\")", offset);

offset = fdt_subnode_offset(fdt, 0, "subsubnode");
check_error("fdt_subnode_offset(\"subsubnode\")",
fdt_offset_error(offset));
check_error("fdt_subnode_offset(\"subsubnode\")", offset);

offset = fdt_path_offset(fdt, "/nonexistant-subnode");
check_error("fdt_path_offset(\"/nonexistant-subnode\")",
fdt_offset_error(offset));
check_error("fdt_path_offset(\"/nonexistant-subnode\")", offset);
PASS();
}

5
tests/path_offset.c

@ -30,15 +30,14 @@ @@ -30,15 +30,14 @@
int check_subnode(void *fdt, int parent, const char *name)
{
int offset;
int err;
struct fdt_node_header *nh;
uint32_t tag;

verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
offset = fdt_subnode_offset(fdt, parent, name);
verbose_printf("offset %d...", offset);
if ((err = fdt_offset_error(offset)))
FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(err));
if (offset < 0)
FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
nh = fdt_offset_ptr_typed(fdt, offset, nh);
verbose_printf("pointer %p\n", nh);
if (! nh)

6
tests/rw_tree1.c

@ -40,9 +40,9 @@ @@ -40,9 +40,9 @@

#define OFF_CHECK(off, code) \
{ \
err = fdt_offset_error((off) = (code)); \
if (err) \
FAIL(#code ": %s", fdt_strerror(err)); \
(off) = (code); \
if (off < 0) \
FAIL(#code ": %s", fdt_strerror(off)); \
}

int main(int argc, char *argv[])

5
tests/subnode_offset.c

@ -30,15 +30,14 @@ @@ -30,15 +30,14 @@
int check_subnode(struct fdt_header *fdt, int parent, const char *name)
{
int offset;
int err;
struct fdt_node_header *nh;
uint32_t tag;

verbose_printf("Checking subnode \"%s\" of %d...", name, parent);
offset = fdt_subnode_offset(fdt, parent, name);
verbose_printf("offset %d...", offset);
if ((err = fdt_offset_error(offset)))
FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(err));
if (offset < 0)
FAIL("fdt_subnode_offset(\"%s\"): %s", name, fdt_strerror(offset));
nh = fdt_offset_ptr_typed(fdt, offset, nh);
verbose_printf("pointer %p\n", nh);
if (! nh)

8
tests/testutils.c

@ -97,10 +97,12 @@ static struct errtabent errtable[] = { @@ -97,10 +97,12 @@ static struct errtabent errtable[] = {

const char *fdt_strerror(int errval)
{
if ((errval >= 0) && (errval < ERRTABSIZE))
return errtable[errval].str;
if (errval > 0)
return "<valid offset>";
else if (errval < -ERRTABSIZE)
return "<unknown error>";
else
return "Unknown FDT error code";
return errtable[-errval].str;
}

void check_property(void *fdt, int nodeoffset, const char *name,

5
tests/truncated_property.c

@ -40,10 +40,9 @@ int main(int argc, char *argv[]) @@ -40,10 +40,9 @@ int main(int argc, char *argv[])
prop = fdt_getprop(fdt, 0, "truncated", &len);
if (prop)
FAIL("fdt_getprop() succeeded on truncated property");
err = fdt_ptrlen_error(prop, len);
if (err != FDT_ERR_BADSTRUCTURE)
if (len != -FDT_ERR_BADSTRUCTURE)
FAIL("fdt_getprop() failed with \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_BADSTRUCTURE));
fdt_strerror(err), fdt_strerror(-FDT_ERR_BADSTRUCTURE));

PASS();
}

Loading…
Cancel
Save