libfdt - library for manipulating device trees in flattened format

Initial revision, read-only and "in-place" (no memmove() required)
write operations only.
This commit is contained in:
David Gibson 2006-11-27 16:21:28 +11:00
commit 3da0f9a10d
25 changed files with 1868 additions and 0 deletions

0
tests/.gitignore vendored Normal file
View file

50
tests/Makefile Normal file
View file

@ -0,0 +1,50 @@
PREFIX = /usr/local
LIB_TESTS =
LIBTREE_TESTS = root_node property_offset subnode_offset path_offset getprop \
notfound \
setprop_inplace nop_property nop_node
TESTS = $(LIB_TESTS) $(LIBTREE_TESTS)
CFLAGS = -Wall -g
CPPFLAGS = -I..
LDFLAGS = -L..
LIBFDT = ../libfdt.a
ifdef V
VECHO = :
else
VECHO = echo " "
.SILENT:
endif
DEPFILES = $(TESTS:%=%.d) testutils.d
all: $(TESTS)
%.o: %.c
@$(VECHO) CC $@
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
%.o: %.S
@$(VECHO) AS $@
$(CC) -D__ASSEMBLY__ $(CPPFLAGS) -o $@ -c $<
$(LIB_TESTS): %: %.o testutils.o $(LIBFDT)
@$(VECHO) LD "(testcase)" $@
$(CC) $(LDFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(LIBTREE_TESTS): %: %.o testutils.o trees.o $(LIBFDT)
@$(VECHO) LD "(testcase + trees)" $@
$(CC) $(LDFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
clean:
@$(VECHO) CLEAN "(tests)"
rm -f *~ *.o *.so *.a *.d *.s core a.out
rm -f $(TESTS)
%.d: %.c
@$(CC) $(CPPFLAGS) -MM -MT "$*.o $@" $< > $@
-include $(DEPFILES)

41
tests/getprop.c Normal file
View file

@ -0,0 +1,41 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_getprop()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
struct fdt_header *fdt = &_test_tree1;
test_init(argc, argv);
check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1, TEST_STRING_1);
PASS();
}

96
tests/nop_node.c Normal file
View file

@ -0,0 +1,96 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_node()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
struct fdt_header *fdt = &_test_tree1;
int subnode1_offset, subnode2_offset, subsubnode2_offset;
int err;
test_init(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));
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));
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)))
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(err));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_nop_node(fdt, subnode1_offset);
if (err)
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)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), 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));
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)))
FAIL("Couldn't find \"/subnode2/subsubnode\": %s",
fdt_strerror(err));
check_getprop_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
err = fdt_nop_node(fdt, subnode2_offset);
if (err)
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)
FAIL("fdt_path_offset(subnode1) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
subnode2_offset = fdt_path_offset(fdt, "/subnode2");
if ((err = fdt_offset_error(subnode2_offset)) != FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
subsubnode2_offset = fdt_path_offset(fdt, "/subnode2/subsubnode");
if ((err = fdt_offset_error(subsubnode2_offset)) != FDT_ERR_NOTFOUND)
FAIL("fdt_path_offset(subsubnode2) returned \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_NOTFOUND));
PASS();
}

70
tests/nop_property.c Normal file
View file

@ -0,0 +1,70 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_nop_property()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
struct fdt_header *fdt = &_test_tree1;
uint32_t *intp;
char *strp;
int err;
test_init(argc, argv);
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("int value was 0x%08x\n", *intp);
err = fdt_nop_property(fdt, 0, "prop-int");
if (err)
FAIL("Failed to nop \"prop-int\": %s", fdt_strerror(err));
intp = fdt_getprop(fdt, 0, "prop-int", NULL);
err = fdt_ptr_error(intp);
if (! err)
FAIL("prop-int still present after nopping");
if (err != 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,
TEST_STRING_1);
verbose_printf("string value was \"%s\"\n", strp);
err = fdt_nop_property(fdt, 0, "prop-str");
err = fdt_ptr_error(intp);
if (! err)
FAIL("prop-str still present after nopping");
if (err != FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));
strp = fdt_getprop(fdt, 0, "prop-str", NULL);
if (fdt_ptr_error(intp) != FDT_ERR_NOTFOUND)
FAIL("prop-str still present after nopping");
PASS();
}

75
tests/notfound.c Normal file
View file

@ -0,0 +1,75 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for behaviour on searching for a non-existent node
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
void check_error(const char *s, int err)
{
if (err != FDT_ERR_NOTFOUND)
FAIL("%s return error %s instead of FDT_ERR_NOTFOUND", s,
fdt_strerror(err));
}
int main(int argc, char *argv[])
{
struct fdt_header *fdt = &_test_tree1;
int offset;
int subnode1_offset;
void *val;
int err;
test_init(argc, argv);
offset = fdt_property_offset(fdt, 0, "nonexistant-property");
check_error("fdt_property_offset(\"nonexistant-property\")",
fdt_offset_error(offset));
val = fdt_getprop(fdt, 0, "nonexistant-property", NULL);
check_error("fdt_getprop(\"nonexistant-property\"",
fdt_ptr_error(val));
subnode1_offset = fdt_subnode_offset(fdt, 0, "subnode1");
if ((err = fdt_offset_error(subnode1_offset)))
FAIL("Couldn't find subnode1: %s", fdt_strerror(err));
val = fdt_getprop(fdt, subnode1_offset, "prop-str", NULL);
check_error("fdt_getprop(\"prop-str\")", fdt_ptr_error(val));
offset = fdt_subnode_offset(fdt, 0, "nonexistant-subnode");
check_error("fdt_subnode_offset(\"nonexistant-subnode\")",
fdt_offset_error(offset));
offset = fdt_subnode_offset(fdt, 0, "subsubnode");
check_error("fdt_subnode_offset(\"subsubnode\")",
fdt_offset_error(offset));
offset = fdt_path_offset(fdt, "/nonexistant-subnode");
check_error("fdt_path_offset(\"/nonexistant-subnode\")",
fdt_offset_error(offset));
PASS();
}

97
tests/path_offset.c Normal file
View file

@ -0,0 +1,97 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_path_offset()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
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));
nh = fdt_offset_ptr_typed(fdt, offset, nh);
verbose_printf("pointer %p\n", nh);
if (! nh)
FAIL("NULL retrieving subnode \"%s\"", name);
tag = fdt32_to_cpu(nh->tag);
if (tag != FDT_BEGIN_NODE)
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
if (!streq(nh->name, name))
FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
nh->name, name);
return offset;
}
int main(int argc, char *argv[])
{
struct fdt_header *fdt = &_test_tree1;
int subnode1_offset, subnode2_offset;
int subnode1_offset_p, subnode2_offset_p;
int subsubnode1_offset, subsubnode2_offset;
int subsubnode1_offset_p, subsubnode2_offset_p;
test_init(argc, argv);
subnode1_offset = check_subnode(fdt, 0, "subnode1");
subnode2_offset = check_subnode(fdt, 0, "subnode2");
subnode1_offset_p = fdt_path_offset(fdt, "/subnode1");
subnode2_offset_p = fdt_path_offset(fdt, "/subnode2");
if (subnode1_offset != subnode1_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subnode1_offset, subnode1_offset_p);
if (subnode2_offset != subnode2_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subnode2_offset, subnode2_offset_p);
subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode");
subsubnode1_offset_p = fdt_path_offset(fdt, "/subnode1/subsubnode");
subsubnode2_offset_p = fdt_path_offset(fdt, "/subnode2/subsubnode");
if (subsubnode1_offset != subsubnode1_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subsubnode1_offset, subsubnode1_offset_p);
if (subsubnode2_offset != subsubnode2_offset_p)
FAIL("Mismatch between subnode_offset (%d) and path_offset (%d)",
subsubnode2_offset, subsubnode2_offset_p);
PASS();
}

40
tests/property_offset.c Normal file
View file

@ -0,0 +1,40 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_property_offset()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
struct fdt_header *fdt = &_test_tree1;
test_init(argc, argv);
check_property_typed(fdt, 0, "prop-int", TEST_VALUE_1);
check_property(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1, TEST_STRING_1);
PASS();
}

51
tests/root_node.c Normal file
View file

@ -0,0 +1,51 @@
/*
* libfdt - Flat Device Tree manipulation
* Basic testcase for read-only access
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
struct fdt_header *fdt = &_test_tree1;
struct fdt_node_header *nh;
test_init(argc, argv);
nh = fdt_offset_ptr_typed(fdt, 0, nh);
if (! nh)
FAIL("NULL retrieving root node");
if (nh->tag != FDT_BEGIN_NODE)
FAIL("Wrong tag on root node");
if (strlen(nh->name) != 0)
FAIL("Wrong name for root node, \"%s\" instead of empty",
nh->name);
PASS();
}

55
tests/run_tests.sh Executable file
View file

@ -0,0 +1,55 @@
#! /bin/bash
export QUIET_TEST=1
ENV=/usr/bin/env
run_test () {
echo -n "$@: "
PATH=".:$PATH" $ENV "$@"
}
functional_tests () {
# Read-only tests
run_test root_node
run_test property_offset
run_test subnode_offset
run_test path_offset
run_test getprop
run_test notfound
# Write-in-place tests
run_test setprop_inplace
run_test nop_property
run_test nop_node
}
stress_tests () {
ITERATIONS=10 # Number of iterations for looping tests
}
while getopts "vdt:" ARG ; do
case $ARG in
"v")
unset QUIET_TEST
;;
"t")
TESTSETS=$OPTARG
;;
esac
done
if [ -z "$TESTSETS" ]; then
TESTSETS="func stress"
fi
for set in $TESTSETS; do
case $set in
"func")
functional_tests
;;
"stress")
stress_tests
;;
esac
done

69
tests/setprop_inplace.c Normal file
View file

@ -0,0 +1,69 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_setprop_inplace()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
int main(int argc, char *argv[])
{
struct fdt_header *fdt = &_test_tree1;
uint32_t *intp;
char *strp, *xstr;
int xlen, i;
int err;
test_init(argc, argv);
intp = check_getprop_typed(fdt, 0, "prop-int", TEST_VALUE_1);
verbose_printf("Old int value was 0x%08x\n", *intp);
err = fdt_setprop_inplace_typed(fdt, 0, "prop-int", ~TEST_VALUE_1);
if (err)
FAIL("Failed to set \"prop-int\" to 0x08%x: %s",
~TEST_VALUE_1, fdt_strerror(err));
intp = check_getprop_typed(fdt, 0, "prop-int", ~TEST_VALUE_1);
verbose_printf("New int value is 0x%08x\n", *intp);
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
TEST_STRING_1);
verbose_printf("Old string value was \"%s\"\n", strp);
xstr = strdup(strp);
xlen = strlen(xstr);
for (i = 0; i < xlen; i++)
xstr[i] = toupper(xstr[i]);
err = fdt_setprop_inplace(fdt, 0, "prop-str", xstr, xlen+1);
if (err)
FAIL("Failed to set \"prop-str\" to \"%s\": %s",
xstr, fdt_strerror(err));
strp = check_getprop(fdt, 0, "prop-str", xlen+1, xstr);
verbose_printf("New string value is \"%s\"\n", strp);
PASS();
}

82
tests/subnode_offset.c Normal file
View file

@ -0,0 +1,82 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase for fdt_subnode_offset()
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fdt.h>
#include <libfdt.h>
#include "tests.h"
#include "testdata.h"
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));
nh = fdt_offset_ptr_typed(fdt, offset, nh);
verbose_printf("pointer %p\n", nh);
if (! nh)
FAIL("NULL retrieving subnode \"%s\"", name);
tag = fdt32_to_cpu(nh->tag);
if (tag != FDT_BEGIN_NODE)
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
if (!streq(nh->name, name))
FAIL("Subnode name mismatch \"%s\" instead of \"%s\"",
nh->name, name);
return offset;
}
int main(int argc, char *argv[])
{
struct fdt_header *fdt = &_test_tree1;
int subnode1_offset, subnode2_offset;
int subsubnode1_offset, subsubnode2_offset;
test_init(argc, argv);
subnode1_offset = check_subnode(fdt, 0, "subnode1");
subnode2_offset = check_subnode(fdt, 0, "subnode2");
if (subnode1_offset == subnode2_offset)
FAIL("Different subnodes have same offset");
check_property_typed(fdt, subnode1_offset, "prop-int", TEST_VALUE_1);
check_property_typed(fdt, subnode2_offset, "prop-int", TEST_VALUE_2);
subsubnode1_offset = check_subnode(fdt, subnode1_offset, "subsubnode");
subsubnode2_offset = check_subnode(fdt, subnode2_offset, "subsubnode");
check_property_typed(fdt, subsubnode1_offset, "prop-int", TEST_VALUE_1);
check_property_typed(fdt, subsubnode2_offset, "prop-int", TEST_VALUE_2);
PASS();
}

8
tests/testdata.h Normal file
View file

@ -0,0 +1,8 @@
#define TEST_VALUE_1 0xdeadbeef
#define TEST_VALUE_2 0xabcd1234
#define TEST_STRING_1 "hello world"
#ifndef __ASSEMBLY__
extern struct fdt_header _test_tree1;
#endif /* ! __ASSEMBLY */

126
tests/tests.h Normal file
View file

@ -0,0 +1,126 @@
#ifndef _TESTS_H
#define _TESTS_H
/*
* libfdt - Flat Device Tree manipulation
* Testcase definitions
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define DEBUG
/* Test return codes */
#define RC_PASS 0
#define RC_CONFIG 1
#define RC_FAIL 2
#define RC_BUG 99
extern int verbose_test;
extern char *test_name;
void test_init(int argc, char *argv[]);
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define PALIGN(p, a) ((void *)ALIGN((unsigned long)(p), (a)))
#define streq(s1, s2) (strcmp((s1),(s2)) == 0)
#if __BYTE_ORDER == __BIG_ENDIAN
#define fdt32_to_cpu(x) (x)
#define cpu_to_fdt32(x) (x)
#define fdt64_to_cpu(x) (x)
#define cpu_to_fdt64(x) (x)
#else
#define fdt32_to_cpu(x) (bswap_32((x)))
#define cpu_to_fdt32(x) (bswap_32((x)))
#define fdt64_to_cpu(x) (bswap_64((x)))
#define cpu_to_fdt64(x) (bswap_64((x)))
#endif
/* Each test case must define this function */
void cleanup(void);
#define verbose_printf(...) \
if (verbose_test) { \
printf(__VA_ARGS__); \
fflush(stdout); \
}
#define ERR "ERR: "
#define ERROR(fmt, args...) fprintf(stderr, ERR fmt, ## args)
#define PASS() \
do { \
cleanup(); \
printf("PASS\n"); \
exit(RC_PASS); \
} while (0)
#define PASS_INCONCLUSIVE() \
do { \
cleanup(); \
printf("PASS (inconclusive)\n"); \
exit(RC_PASS); \
} while (0)
#define IRRELEVANT() \
do { \
cleanup(); \
printf("PASS (irrelevant)\n"); \
exit(RC_PASS); \
} while (0)
/* Look out, gcc extension below... */
#define FAIL(fmt, ...) \
do { \
cleanup(); \
printf("FAIL\t" fmt "\n", ##__VA_ARGS__); \
exit(RC_FAIL); \
} while (0)
#define CONFIG(fmt, ...) \
do { \
cleanup(); \
printf("Bad configuration: " fmt "\n", ##__VA_ARGS__); \
exit(RC_CONFIG); \
} while (0)
#define TEST_BUG(fmt, ...) \
do { \
cleanup(); \
printf("BUG in testsuite: " fmt "\n", ##__VA_ARGS__); \
exit(RC_BUG); \
} while (0)
const char *fdt_strerror(int errval);
void check_property(struct fdt_header *fdt, int nodeoffset, const char *name,
int len, const void *val);
#define check_property_typed(fdt, nodeoffset, name, val) \
({ \
typeof(val) x = val; \
check_property(fdt, nodeoffset, name, sizeof(x), &x); \
})
void *check_getprop(struct fdt_header *fdt, int nodeoffset, const char *name,
int len, const void *val);
#define check_getprop_typed(fdt, nodeoffset, name, val) \
({ \
typeof(val) x = val; \
check_getprop(fdt, nodeoffset, name, sizeof(x), &x); \
})
#endif /* _TESTS_H */

163
tests/testutils.c Normal file
View file

@ -0,0 +1,163 @@
/*
* libfdt - Flat Device Tree manipulation
* Testcase common utility functions
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE /* for strsignal() */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <libfdt.h>
#include "tests.h"
int verbose_test = 1;
char *test_name;
void __attribute__((weak)) cleanup(void)
{
}
static void sigint_handler(int signum, siginfo_t *si, void *uc)
{
cleanup();
fprintf(stderr, "%s: %s (pid=%d)\n", test_name,
strsignal(signum), getpid());
exit(RC_BUG);
}
void test_init(int argc, char *argv[])
{
int err;
struct sigaction sa_int = {
.sa_sigaction = sigint_handler,
};
test_name = argv[0];
err = sigaction(SIGINT, &sa_int, NULL);
if (err)
FAIL("Can't install SIGINT handler");
if (getenv("QUIET_TEST"))
verbose_test = 0;
verbose_printf("Starting testcase \"%s\", pid %d\n",
test_name, getpid());
}
struct errtabent {
const char *str;
};
#define ERRTABENT(val) \
[(val)] = { .str = #val, }
static struct errtabent errtable[] = {
ERRTABENT(FDT_ERR_OK),
ERRTABENT(FDT_ERR_BADMAGIC),
ERRTABENT(FDT_ERR_BADVERSION),
ERRTABENT(FDT_ERR_BADPOINTER),
ERRTABENT(FDT_ERR_BADHEADER),
ERRTABENT(FDT_ERR_BADSTRUCTURE),
ERRTABENT(FDT_ERR_BADOFFSET),
ERRTABENT(FDT_ERR_NOTFOUND),
ERRTABENT(FDT_ERR_BADPATH),
ERRTABENT(FDT_ERR_TRUNCATED),
ERRTABENT(FDT_ERR_NOSPACE),
ERRTABENT(FDT_ERR_BADSTATE),
ERRTABENT(FDT_ERR_SIZE_MISMATCH),
ERRTABENT(FDT_ERR_INTERNAL),
};
#define ERRTABSIZE (sizeof(errtable) / sizeof(errtable[0]))
const char *fdt_strerror(int errval)
{
if ((errval >= 0) && (errval < ERRTABSIZE))
return errtable[errval].str;
else
return "Unknown FDT error code";
}
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);
verbose_printf("pointer %p\n", prop);
if (! prop)
FAIL("NULL retreiving \"%s\" pointer", name);
tag = fdt32_to_cpu(prop->tag);
nameoff = fdt32_to_cpu(prop->nameoff);
proplen = fdt32_to_cpu(prop->len);
if (tag != FDT_PROP)
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
propname = fdt_string(fdt, nameoff);
if (!propname || !streq(propname, name))
FAIL("Property name mismatch \"%s\" instead of \"%s\"",
propname, name);
if (proplen != len)
FAIL("Size mismatch on property \"%s\": %d insead of %d",
name, proplen, len);
if (memcmp(val, prop->data, len) != 0)
FAIL("Data mismatch on property \"%s\"", name);
}
void *check_getprop(struct fdt_header *fdt, int nodeoffset, const char *name,
int len, const void *val)
{
void *propval;
int proplen;
int err;
propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
if ((err = fdt_ptr_error(propval)))
FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(err));
if (proplen != len)
FAIL("Size mismatch on property \"%s\": %d insead of %d",
name, proplen, len);
if (memcmp(val, propval, len) != 0)
FAIL("Data mismatch on property \"%s\"", name);
return propval;
}

84
tests/trees.S Normal file
View file

@ -0,0 +1,84 @@
#include <fdt.h>
#include "testdata.h"
#define TREE_HDR(tree) \
.globl _##tree ; \
_##tree: \
tree: \
.long FDT_MAGIC ; \
.long tree##_end - tree ; \
.long tree##_struct - tree ; \
.long tree##_strings - tree ; \
.long tree##_rsvmap - tree ; \
.long 0x10 ; \
.long 0x10 ; \
.long 0 ; \
.long tree##_end - tree##_strings ;
#define RSVMAP_ENTRY(addr, len) \
.quad addr ; \
.quad len ;
#define PROPHDR(tree, name, len) \
.long FDT_PROP ; \
.long tree##_##name - tree##_strings ; \
.long len ;
#define PROP_INT(tree, name, val) \
PROPHDR(tree, name, 4) \
.long val
#define PROP_STR(tree, name, str) \
PROPHDR(tree, name, 55f - 54f) \
54: \
.string str ; \
55: \
.balign 4
#define BEGIN_NODE(name) \
.long FDT_BEGIN_NODE ; \
.string name ; \
.balign 4
#define END_NODE \
.long FDT_END_NODE ;
#define STRING(tree, name, str) \
tree##_##name: \
.string str
.data
TREE_HDR(test_tree1)
test_tree1_rsvmap:
RSVMAP_ENTRY(0, 0)
test_tree1_struct:
BEGIN_NODE("")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
PROP_STR(test_tree1, prop_str, TEST_STRING_1)
BEGIN_NODE("subnode1")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
BEGIN_NODE("subsubnode")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
END_NODE
END_NODE
BEGIN_NODE("subnode2")
PROP_INT(test_tree1, prop_int, TEST_VALUE_2)
BEGIN_NODE("subsubnode")
PROP_INT(test_tree1, prop_int, TEST_VALUE_2)
END_NODE
END_NODE
END_NODE
.long FDT_END
test_tree1_strings:
STRING(test_tree1, prop_int, "prop-int")
STRING(test_tree1, prop_str, "prop-str")
test_tree1_end: