2 * libfdt - Flat Device Tree manipulation
3 * Testcase for string handling
4 * Copyright (C) 2015 NVIDIA Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 static void check_expected_failure(const void *fdt, const char *path,
36 offset = fdt_path_offset(fdt, "/");
38 FAIL("Couldn't find path %s", path);
40 err = fdt_stringlist_count(fdt, offset, "#address-cells");
41 if (err != -FDT_ERR_BADVALUE)
42 FAIL("unexpectedly succeeded in parsing #address-cells\n");
44 err = fdt_stringlist_search(fdt, offset, "#address-cells", "foo");
45 if (err != -FDT_ERR_BADVALUE)
46 FAIL("found string in #address-cells: %d\n", err);
49 * Note that the #address-cells property contains a small 32-bit
50 * unsigned integer, hence some bytes will be zero, and searching for
51 * the empty string will succeed.
53 * The reason for this oddity is that the function will exit when the
54 * first occurrence of the string is found, but in order to determine
55 * that the property does not contain a valid string list it would
56 * need to process the whole value.
58 err = fdt_stringlist_search(fdt, offset, "#address-cells", "");
60 FAIL("empty string not found in #address-cells: %d\n", err);
63 * fdt_get_string() can successfully extract strings from non-string
64 * properties. This is because it doesn't necessarily parse the whole
65 * property value, which would be necessary for it to determine if a
66 * valid string or string list is present.
70 static void check_string_count(const void *fdt, const char *path,
71 const char *property, int count)
75 offset = fdt_path_offset(fdt, path);
77 FAIL("Couldn't find path %s", path);
79 err = fdt_stringlist_count(fdt, offset, property);
81 FAIL("Couldn't count strings in property %s of node %s: %d\n",
85 FAIL("String count for property %s of node %s is %d instead of %d\n",
86 path, property, err, count);
89 static void check_string_index(const void *fdt, const char *path,
90 const char *property, const char *string,
95 offset = fdt_path_offset(fdt, path);
97 FAIL("Couldn't find path %s", path);
99 err = fdt_stringlist_search(fdt, offset, property, string);
102 FAIL("Index of %s in property %s of node %s is %d, expected %d\n",
103 string, property, path, err, idx);
106 static void check_string(const void *fdt, const char *path,
107 const char *property, int idx,
113 offset = fdt_path_offset(fdt, path);
115 FAIL("Couldn't find path %s", path);
117 result = fdt_stringlist_get(fdt, offset, property, idx, &len);
119 FAIL("Couldn't extract string %d from property %s of node %s: %d\n",
120 idx, property, path, len);
122 if (strcmp(string, result) != 0)
123 FAIL("String %d in property %s of node %s is %s, expected %s\n",
124 idx, property, path, result, string);
127 int main(int argc, char *argv[])
132 CONFIG("Usage: %s <dtb file>\n", argv[0]);
134 test_init(argc, argv);
135 fdt = load_blob(argv[1]);
137 check_expected_failure(fdt, "/", "#address-cells");
138 check_expected_failure(fdt, "/", "#size-cells");
140 check_string_count(fdt, "/", "compatible", 1);
141 check_string_count(fdt, "/device", "compatible", 2);
142 check_string_count(fdt, "/device", "big-endian", 0);
144 check_string_index(fdt, "/", "compatible", "test-strings", 0);
145 check_string_index(fdt, "/device", "compatible", "foo", 0);
146 check_string_index(fdt, "/device", "compatible", "bar", 1);
147 check_string_index(fdt, "/device", "big-endian", "baz", -1);
149 check_string(fdt, "/", "compatible", 0, "test-strings");
150 check_string(fdt, "/device", "compatible", 0, "foo");
151 check_string(fdt, "/device", "compatible", 1, "bar");