Browse Source

dtc: Implement checks for the format of node and property names

This patch adds checks to the checking framework to verify that node
and property names contain only legal characters, and in the case of
node names there is at most one '@'.

At present when coming from dts input, this is mostly already ensured
by the grammer, however putting the check later means its easier to
generate helpful error messages rather than just "syntax error".  For
dtb input, these checks replace the older similar check built into
flattree.c.

Testcases for the checks are also implemented.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
David Gibson 17 years ago committed by Jon Loeliger
parent
commit
fa5b520ccb
  1. 37
      checks.c
  2. 28
      flattree.c
  3. 1
      tests/dumptrees.c
  4. 3
      tests/run_tests.sh
  5. 3
      tests/testdata.h
  6. 48
      tests/trees.S

37
checks.c

@ -242,6 +242,42 @@ static void check_duplicate_property_names(struct check *c, struct node *dt, @@ -242,6 +242,42 @@ static void check_duplicate_property_names(struct check *c, struct node *dt,
}
NODE_CHECK(duplicate_property_names, NULL, ERROR);

#define LOWERCASE "abcdefghijklmnopqrstuvwxyz"
#define UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define DIGITS "0123456789"
#define PROPNODECHARS LOWERCASE UPPERCASE DIGITS ",._+*#?-"

static void check_node_name_chars(struct check *c, struct node *dt,
struct node *node)
{
int n = strspn(node->name, c->data);

if (n < strlen(node->name))
FAIL(c, "Bad character '%c' in node %s",
node->name[n], node->fullpath);
}
NODE_CHECK(node_name_chars, PROPNODECHARS "@", ERROR);

static void check_node_name_format(struct check *c, struct node *dt,
struct node *node)
{
if (strchr(get_unitname(node), '@'))
FAIL(c, "Node %s has multiple '@' characters in name",
node->fullpath);
}
NODE_CHECK(node_name_format, NULL, ERROR, &node_name_chars);

static void check_property_name_chars(struct check *c, struct node *dt,
struct node *node, struct property *prop)
{
int n = strspn(prop->name, c->data);

if (n < strlen(prop->name))
FAIL(c, "Bad character '%c' in property name \"%s\", node %s",
prop->name[n], prop->name, node->fullpath);
}
PROP_CHECK(property_name_chars, PROPNODECHARS, ERROR);

static void check_explicit_phandles(struct check *c, struct node *root,
struct node *node)
{
@ -498,6 +534,7 @@ TREE_CHECK(obsolete_chosen_interrupt_controller, NULL, WARN); @@ -498,6 +534,7 @@ TREE_CHECK(obsolete_chosen_interrupt_controller, NULL, WARN);

static struct check *check_table[] = {
&duplicate_node_names, &duplicate_property_names,
&node_name_chars, &node_name_format, &property_name_chars,
&name_is_string, &name_properties,
&explicit_phandles,
&phandle_references, &path_references,

28
flattree.c

@ -729,29 +729,14 @@ static char *nodename_from_path(const char *ppath, const char *cpath) @@ -729,29 +729,14 @@ static char *nodename_from_path(const char *ppath, const char *cpath)
return strdup(lslash+1);
}

static const char PROPCHAR[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,._+*#?-";
static const char UNITCHAR[] = "0123456789abcdef,";

static int check_node_name(const char *name)
static int find_basenamelen(const char *name)
{
const char *atpos;
int basenamelen;

atpos = strrchr(name, '@');
const char *atpos = strchr(name, '@');

if (atpos)
basenamelen = atpos - name;
return atpos - name;
else
basenamelen = strlen(name);

if (strspn(name, PROPCHAR) < basenamelen)
return -1;

if (atpos
&& ((basenamelen + 1 + strspn(atpos+1, UNITCHAR)) < strlen(name)))
return -1;

return basenamelen;
return strlen(name);
}

static struct node *unflatten_tree(struct inbuf *dtbuf,
@ -775,10 +760,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf, @@ -775,10 +760,7 @@ static struct node *unflatten_tree(struct inbuf *dtbuf,
node->fullpath = join_path(parent_path, node->name);
}

node->basenamelen = check_node_name(node->name);
if (node->basenamelen < 0) {
fprintf(stderr, "Warning \"%s\" has incorrect format\n", node->name);
}
node->basenamelen = find_basenamelen(node->name);

do {
struct property *prop;

1
tests/dumptrees.c

@ -37,6 +37,7 @@ struct { @@ -37,6 +37,7 @@ struct {
} trees[] = {
#define TREE(name) { &_##name, #name ".dtb" }
TREE(test_tree1),
TREE(bad_node_char), TREE(bad_node_format), TREE(bad_prop_char),
};

#define NUM_TREES (sizeof(trees) / sizeof(trees[0]))

3
tests/run_tests.sh

@ -188,6 +188,9 @@ dtc_tests () { @@ -188,6 +188,9 @@ dtc_tests () {
run_test dtc-checkfails.sh ranges_format -- -I dts -O dtb bad-empty-ranges.dts
run_test dtc-checkfails.sh avoid_default_addr_size -- -I dts -O dtb default-addr-size.dts
run_test dtc-checkfails.sh obsolete_chosen_interrupt_controller -- -I dts -O dtb obsolete-chosen-interrupt-controller.dts
run_test dtc-checkfails.sh node_name_chars -- -I dtb -O dtb bad_node_char.dtb
run_test dtc-checkfails.sh node_name_format -- -I dtb -O dtb bad_node_format.dtb
run_test dtc-checkfails.sh prop_name_chars -- -I dtb -O dtb bad_prop_char.dtb
}

while getopts "vt:m" ARG ; do

3
tests/testdata.h

@ -33,4 +33,7 @@ @@ -33,4 +33,7 @@
#ifndef __ASSEMBLY__
extern struct fdt_header _test_tree1;
extern struct fdt_header _truncated_property;
extern struct fdt_header _bad_node_char;
extern struct fdt_header _bad_node_format;
extern struct fdt_header _bad_prop_char;
#endif /* ! __ASSEMBLY */

48
tests/trees.S

@ -136,3 +136,51 @@ truncated_property_strings: @@ -136,3 +136,51 @@ truncated_property_strings:
truncated_property_strings_end:

truncated_property_end:


TREE_HDR(bad_node_char)
EMPTY_RSVMAP(bad_node_char)

bad_node_char_struct:
BEGIN_NODE("")
BEGIN_NODE("sub$node")
END_NODE
END_NODE
FDTLONG(FDT_END)
bad_node_char_struct_end:

bad_node_char_strings:
bad_node_char_strings_end:
bad_node_char_end:


TREE_HDR(bad_node_format)
EMPTY_RSVMAP(bad_node_format)

bad_node_format_struct:
BEGIN_NODE("")
BEGIN_NODE("subnode@1@2")
END_NODE
END_NODE
FDTLONG(FDT_END)
bad_node_format_struct_end:

bad_node_format_strings:
bad_node_format_strings_end:
bad_node_format_end:


TREE_HDR(bad_prop_char)
EMPTY_RSVMAP(bad_prop_char)

bad_prop_char_struct:
BEGIN_NODE("")
PROP_INT(bad_prop_char, prop, TEST_VALUE_1)
END_NODE
FDTLONG(FDT_END)
bad_prop_char_struct_end:

bad_prop_char_strings:
STRING(bad_prop_char, prop, "prop$erty")
bad_prop_char_strings_end:
bad_prop_char_end:

Loading…
Cancel
Save