2 * Self tests for device tree subsystem
5 #define pr_fmt(fmt) "### dt-test ### " fmt
9 #include <linux/errno.h>
10 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 #include <linux/device.h>
17 #include "of_private.h"
19 static struct selftest_results {
24 #define selftest(result, fmt, ...) { \
26 pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
27 selftest_results.passed = false; \
29 pr_info("pass %s:%i\n", __FILE__, __LINE__); \
33 static void __init of_selftest_parse_phandle_with_args(void)
35 struct device_node *np;
36 struct of_phandle_args args;
39 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
41 pr_err("missing testcase data\n");
45 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
46 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
48 for (i = 0; i < 8; i++) {
50 rc = of_parse_phandle_with_args(np, "phandle-list",
51 "#phandle-cells", i, &args);
53 /* Test the values from tests-phandle.dtsi */
57 passed &= (args.args_count == 1);
58 passed &= (args.args[0] == (i + 1));
62 passed &= (args.args_count == 2);
63 passed &= (args.args[0] == (i + 1));
64 passed &= (args.args[1] == 0);
67 passed &= (rc == -ENOENT);
71 passed &= (args.args_count == 3);
72 passed &= (args.args[0] == (i + 1));
73 passed &= (args.args[1] == 4);
74 passed &= (args.args[2] == 3);
78 passed &= (args.args_count == 2);
79 passed &= (args.args[0] == (i + 1));
80 passed &= (args.args[1] == 100);
84 passed &= (args.args_count == 0);
88 passed &= (args.args_count == 1);
89 passed &= (args.args[0] == (i + 1));
92 passed &= (rc == -ENOENT);
98 selftest(passed, "index %i - data error on node %s rc=%i\n",
99 i, args.np->full_name, rc);
102 /* Check for missing list property */
103 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
104 "#phandle-cells", 0, &args);
105 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
106 rc = of_count_phandle_with_args(np, "phandle-list-missing",
108 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
110 /* Check for missing cells property */
111 rc = of_parse_phandle_with_args(np, "phandle-list",
112 "#phandle-cells-missing", 0, &args);
113 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
114 rc = of_count_phandle_with_args(np, "phandle-list",
115 "#phandle-cells-missing");
116 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
118 /* Check for bad phandle in list */
119 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
120 "#phandle-cells", 0, &args);
121 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
122 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
124 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
126 /* Check for incorrectly formed argument list */
127 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
128 "#phandle-cells", 1, &args);
129 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
130 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
132 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
135 static void __init of_selftest_property_string(void)
137 const char *strings[4];
138 struct device_node *np;
142 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
144 pr_err("No testcase data in device tree\n");
148 rc = of_property_match_string(np, "phandle-list-names", "first");
149 selftest(rc == 0, "first expected:0 got:%i\n", rc);
150 rc = of_property_match_string(np, "phandle-list-names", "second");
151 selftest(rc == 1, "second expected:0 got:%i\n", rc);
152 rc = of_property_match_string(np, "phandle-list-names", "third");
153 selftest(rc == 2, "third expected:0 got:%i\n", rc);
154 rc = of_property_match_string(np, "phandle-list-names", "fourth");
155 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
156 rc = of_property_match_string(np, "missing-property", "blah");
157 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
158 rc = of_property_match_string(np, "empty-property", "blah");
159 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
160 rc = of_property_match_string(np, "unterminated-string", "blah");
161 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
163 /* of_property_count_strings() tests */
164 rc = of_property_count_strings(np, "string-property");
165 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
166 rc = of_property_count_strings(np, "phandle-list-names");
167 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
168 rc = of_property_count_strings(np, "unterminated-string");
169 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
170 rc = of_property_count_strings(np, "unterminated-string-list");
171 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
173 /* of_property_read_string_index() tests */
174 rc = of_property_read_string_index(np, "string-property", 0, strings);
175 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
177 rc = of_property_read_string_index(np, "string-property", 1, strings);
178 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
179 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
180 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
181 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
182 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
183 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
184 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
186 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
187 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
189 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
190 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
191 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
192 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
194 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
195 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
198 /* of_property_read_string_array() tests */
199 rc = of_property_read_string_array(np, "string-property", strings, 4);
200 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
201 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
202 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
203 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
204 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
205 /* -- An incorrectly formed string should cause a failure */
206 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
207 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
208 /* -- parsing the correctly formed strings should still work: */
210 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
211 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
213 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
214 selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
217 #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
218 (p1)->value && (p2)->value && \
219 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
220 !strcmp((p1)->name, (p2)->name))
221 static void __init of_selftest_property_copy(void)
223 #ifdef CONFIG_OF_DYNAMIC
224 struct property p1 = { .name = "p1", .length = 0, .value = "" };
225 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
226 struct property *new;
228 new = __of_prop_dup(&p1, GFP_KERNEL);
229 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
234 new = __of_prop_dup(&p2, GFP_KERNEL);
235 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
242 static void __init of_selftest_changeset(void)
244 #ifdef CONFIG_OF_DYNAMIC
245 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
246 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
247 struct property *ppremove;
248 struct device_node *n1, *n2, *n21, *nremove, *parent;
249 struct of_changeset chgset;
251 of_changeset_init(&chgset);
252 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
253 selftest(n1, "testcase setup failure\n");
254 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
255 selftest(n2, "testcase setup failure\n");
256 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
257 selftest(n21, "testcase setup failure %p\n", n21);
258 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
259 selftest(nremove, "testcase setup failure\n");
260 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
261 selftest(ppadd, "testcase setup failure\n");
262 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
263 selftest(ppupdate, "testcase setup failure\n");
264 parent = nremove->parent;
269 ppremove = of_find_property(parent, "prop-remove", NULL);
270 selftest(ppremove, "failed to find removal prop");
272 of_changeset_init(&chgset);
273 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
274 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
275 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
276 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
277 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
278 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
279 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
280 mutex_lock(&of_mutex);
281 selftest(!of_changeset_apply(&chgset), "apply failed\n");
282 mutex_unlock(&of_mutex);
284 mutex_lock(&of_mutex);
285 selftest(!of_changeset_revert(&chgset), "revert failed\n");
286 mutex_unlock(&of_mutex);
288 of_changeset_destroy(&chgset);
292 static int __init of_selftest(void)
294 struct device_node *np;
296 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
298 pr_info("No testcase data in device tree; not running tests\n");
303 pr_info("start of selftest - you will see error messages\n");
304 of_selftest_parse_phandle_with_args();
305 of_selftest_property_match_string();
306 of_selftest_property_copy();
307 of_selftest_changeset();
308 of_selftest_property_string();
309 pr_info("end of selftest - %i passed, %i failed\n",
310 selftest_results.passed, selftest_results.failed);
313 late_initcall(of_selftest);