blob: 5a0771cc8987defd8e9c22dc9eafd3d8f3eaa391 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Self tests for device tree subsystem
3 */
4
5#define pr_fmt(fmt) "### %s(): " fmt, __func__
6
7#include <linux/clk.h>
8#include <linux/err.h>
9#include <linux/errno.h>
10#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/list.h>
13#include <linux/mutex.h>
14#include <linux/slab.h>
15#include <linux/device.h>
16
17static bool selftest_passed = true;
18#define selftest(result, fmt, ...) { \
19 selftest_passed &= (result); \
20 if (!(result)) \
21 pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
22}
23
24static void __init of_selftest_parse_phandle_with_args(void)
25{
26 struct device_node *np;
27 struct of_phandle_args args;
28 int rc, i;
29 bool passed_all = true;
30
31 pr_info("start\n");
32 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
33 if (!np) {
34 pr_err("missing testcase data\n");
35 return;
36 }
37
38 for (i = 0; i < 7; i++) {
39 bool passed = true;
40 rc = of_parse_phandle_with_args(np, "phandle-list",
41 "#phandle-cells", i, &args);
42
43 /* Test the values from tests-phandle.dtsi */
44 switch (i) {
45 case 0:
46 passed &= !rc;
47 passed &= (args.args_count == 1);
48 passed &= (args.args[0] == (i + 1));
49 break;
50 case 1:
51 passed &= !rc;
52 passed &= (args.args_count == 2);
53 passed &= (args.args[0] == (i + 1));
54 passed &= (args.args[1] == 0);
55 break;
56 case 2:
57 passed &= (rc == -ENOENT);
58 break;
59 case 3:
60 passed &= !rc;
61 passed &= (args.args_count == 3);
62 passed &= (args.args[0] == (i + 1));
63 passed &= (args.args[1] == 4);
64 passed &= (args.args[2] == 3);
65 break;
66 case 4:
67 passed &= !rc;
68 passed &= (args.args_count == 2);
69 passed &= (args.args[0] == (i + 1));
70 passed &= (args.args[1] == 100);
71 break;
72 case 5:
73 passed &= !rc;
74 passed &= (args.args_count == 0);
75 break;
76 case 6:
77 passed &= !rc;
78 passed &= (args.args_count == 1);
79 passed &= (args.args[0] == (i + 1));
80 break;
81 case 7:
82 passed &= (rc == -EINVAL);
83 break;
84 default:
85 passed = false;
86 }
87
88 if (!passed) {
89 int j;
90 pr_err("index %i - data error on node %s rc=%i regs=[",
91 i, args.np->full_name, rc);
92 for (j = 0; j < args.args_count; j++)
93 printk(" %i", args.args[j]);
94 printk(" ]\n");
95
96 passed_all = false;
97 }
98 }
99
100 /* Check for missing list property */
101 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
102 "#phandle-cells", 0, &args);
103 passed_all &= (rc == -EINVAL);
104
105 /* Check for missing cells property */
106 rc = of_parse_phandle_with_args(np, "phandle-list",
107 "#phandle-cells-missing", 0, &args);
108 passed_all &= (rc == -EINVAL);
109
110 /* Check for bad phandle in list */
111 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
112 "#phandle-cells", 0, &args);
113 passed_all &= (rc == -EINVAL);
114
115 /* Check for incorrectly formed argument list */
116 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
117 "#phandle-cells", 1, &args);
118 passed_all &= (rc == -EINVAL);
119
120 pr_info("end - %s\n", passed_all ? "PASS" : "FAIL");
121}
122
123static void __init of_selftest_property_string(void)
124{
125 const char *strings[4];
126 struct device_node *np;
127 int rc;
128
129 pr_info("start\n");
130 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
131 if (!np) {
132 pr_err("No testcase data in device tree\n");
133 return;
134 }
135
136 rc = of_property_match_string(np, "phandle-list-names", "first");
137 selftest(rc == 0, "first expected:0 got:%i\n", rc);
138 rc = of_property_match_string(np, "phandle-list-names", "second");
139 selftest(rc == 1, "second expected:0 got:%i\n", rc);
140 rc = of_property_match_string(np, "phandle-list-names", "third");
141 selftest(rc == 2, "third expected:0 got:%i\n", rc);
142 rc = of_property_match_string(np, "phandle-list-names", "fourth");
143 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
144 rc = of_property_match_string(np, "missing-property", "blah");
145 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
146 rc = of_property_match_string(np, "empty-property", "blah");
147 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
148 rc = of_property_match_string(np, "unterminated-string", "blah");
149 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
150
151 /* of_property_count_strings() tests */
152 rc = of_property_count_strings(np, "string-property");
153 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
154 rc = of_property_count_strings(np, "phandle-list-names");
155 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
156 rc = of_property_count_strings(np, "unterminated-string");
157 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
158 rc = of_property_count_strings(np, "unterminated-string-list");
159 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
160
161 /* of_property_read_string_index() tests */
162 rc = of_property_read_string_index(np, "string-property", 0, strings);
163 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
164 strings[0] = NULL;
165 rc = of_property_read_string_index(np, "string-property", 1, strings);
166 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
167 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
168 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
169 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
170 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
171 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
172 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
173 strings[0] = NULL;
174 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
175 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
176 strings[0] = NULL;
177 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
178 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
179 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
180 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
181 strings[0] = NULL;
182 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
183 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
184 strings[1] = NULL;
185
186 /* of_property_read_string_array() tests */
187 rc = of_property_read_string_array(np, "string-property", strings, 4);
188 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
189 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
190 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
191 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
192 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
193 /* -- An incorrectly formed string should cause a failure */
194 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
195 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
196 /* -- parsing the correctly formed strings should still work: */
197 strings[2] = NULL;
198 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
199 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
200 strings[1] = NULL;
201 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
202 selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]);
203}
204
205static int __init of_selftest(void)
206{
207 struct device_node *np;
208
209 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
210 if (!np) {
211 pr_info("No testcase data in device tree; not running tests\n");
212 return 0;
213 }
214 of_node_put(np);
215
216 pr_info("start of selftest - you will see error messages\n");
217 of_selftest_parse_phandle_with_args();
218 of_selftest_property_string();
219 pr_info("end of selftest - %s\n", selftest_passed ? "PASS" : "FAIL");
220 return 0;
221}
222late_initcall(of_selftest);