blob: 39191d7f52b553f635ab20d99b0d3360bed57832 [file] [log] [blame]
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +09001// SPDX-License-Identifier: GPL-2.0+
Simon Glassf912a722022-09-06 20:27:27 -06002/*
3 * Copyright 2022 Google LLC
4 *
5 * There are two types of tests in this file:
6 * - normal ones which act on the control FDT (gd->fdt_blob or gd->of_root)
7 * - 'other' ones which act on the 'other' FDT (other.dts)
8 *
9 * The 'other' ones have an _ot suffix.
10 *
11 * The latter are used to check behaviour with multiple device trees,
12 * particularly with flat tree, where a tree ID is included in ofnode as part of
13 * the node offset. These tests are typically just for making sure that the
14 * offset makes it to libfdt correctly and that the resulting return value is
15 * correctly turned into an ofnode. The 'other' tests do not fully check the
16 * behaviour of each ofnode function, since that is done by the normal ones.
17 */
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090018
Simon Glassebbe90b2023-09-26 08:14:43 -060019#include <abuf.h>
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090020#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060021#include <log.h>
Simon Glassef75c592022-07-30 15:52:08 -060022#include <of_live.h>
Simon Glass270a4432022-07-30 15:52:09 -060023#include <dm/device-internal.h>
24#include <dm/lists.h>
Simon Glass699c9ca2018-10-01 12:22:08 -060025#include <dm/of_extra.h>
Simon Glass270a4432022-07-30 15:52:09 -060026#include <dm/root.h>
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090027#include <dm/test.h>
Simon Glass270a4432022-07-30 15:52:09 -060028#include <dm/uclass-internal.h>
Simon Glassebbe90b2023-09-26 08:14:43 -060029#include <linux/sizes.h>
Simon Glass75c4d412020-07-19 10:15:37 -060030#include <test/test.h>
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090031#include <test/ut.h>
32
Simon Glassf912a722022-09-06 20:27:27 -060033/**
34 * get_other_oftree() - Convert a flat tree into an oftree object
35 *
36 * @uts: Test state
37 * @return: oftree object for the 'other' FDT (see sandbox' other.dts)
38 */
39oftree get_other_oftree(struct unit_test_state *uts)
40{
41 oftree tree;
42
43 if (of_live_active())
44 tree = oftree_from_np(uts->of_other);
45 else
46 tree = oftree_from_fdt(uts->other_fdt);
47
48 /* An invalid tree may cause failure or crashes */
49 if (!oftree_valid(tree))
50 ut_reportf("test needs the UT_TESTF_OTHER_FDT flag");
51
52 return tree;
53}
54
Simon Glassdad97512022-09-06 20:27:29 -060055/**
56 * get_oftree() - Convert a flat tree into an oftree object
57 *
58 * @uts: Test state
59 * @fdt: Pointer to flat tree
60 * @treep: Returns the tree, on success
61 * Return: 0 if OK, 1 if the tree failed to unflatten, -EOVERFLOW if there are
62 * too many flat trees to allow another one to be registers (see
63 * oftree_ensure())
64 */
65int get_oftree(struct unit_test_state *uts, void *fdt, oftree *treep)
66{
67 oftree tree;
68
69 if (of_live_active()) {
70 struct device_node *root;
71
72 ut_assertok(unflatten_device_tree(fdt, &root));
73 tree = oftree_from_np(root);
74 } else {
75 tree = oftree_from_fdt(fdt);
76 if (!oftree_valid(tree))
77 return -EOVERFLOW;
78 }
79 *treep = tree;
80
81 return 0;
82}
83
84/**
85 * free_oftree() - Free memory used by get_oftree()
86 *
87 * @tree: Tree to free
88 */
89void free_oftree(oftree tree)
90{
91 if (of_live_active())
92 free(tree.np);
93}
94
Simon Glass75b8a872023-09-26 08:14:39 -060095/* test ofnode_device_is_compatible() */
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090096static int dm_test_ofnode_compatible(struct unit_test_state *uts)
97{
98 ofnode root_node = ofnode_path("/");
99
100 ut_assert(ofnode_valid(root_node));
101 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
102
103 return 0;
104}
Simon Glass9e7a42a2022-09-06 20:27:30 -0600105DM_TEST(dm_test_ofnode_compatible,
106 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
107
108/* check ofnode_device_is_compatible() with the 'other' FDT */
109static int dm_test_ofnode_compatible_ot(struct unit_test_state *uts)
110{
111 oftree otree = get_other_oftree(uts);
112 ofnode oroot = oftree_root(otree);
113
114 ut_assert(ofnode_valid(oroot));
115 ut_assert(ofnode_device_is_compatible(oroot, "sandbox-other"));
116
117 return 0;
118}
Simon Glassdb6d8722023-09-26 08:14:38 -0600119DM_TEST(dm_test_ofnode_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200120
Patrick Delaunay04fcfe72020-09-24 17:26:20 +0200121static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
122{
123 /* test invalid phandle */
124 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
125 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
126
127 /* test first valid phandle */
128 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
129
130 /* test unknown phandle */
131 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
132
Simon Glass95fd2092022-09-06 20:27:22 -0600133 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
134
Patrick Delaunay04fcfe72020-09-24 17:26:20 +0200135 return 0;
136}
137DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
138
Simon Glass75b8a872023-09-26 08:14:39 -0600139/* test oftree_get_by_phandle() with a the 'other' oftree */
Simon Glassf912a722022-09-06 20:27:27 -0600140static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts)
141{
142 oftree otree = get_other_oftree(uts);
143 ofnode node;
144
145 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
146 node = oftree_get_by_phandle(otree, 1);
147 ut_assert(ofnode_valid(node));
148 ut_asserteq_str("target", ofnode_get_name(node));
149
150 return 0;
151}
Simon Glassdb6d8722023-09-26 08:14:38 -0600152DM_TEST(dm_test_ofnode_get_by_phandle_ot,
153 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glassf912a722022-09-06 20:27:27 -0600154
Simon Glass9e7a42a2022-09-06 20:27:30 -0600155static int check_prop_values(struct unit_test_state *uts, ofnode start,
156 const char *propname, const char *propval,
157 int expect_count)
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200158{
Simon Glass9e7a42a2022-09-06 20:27:30 -0600159 int proplen = strlen(propval) + 1;
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200160 const char *str;
Simon Glass9e7a42a2022-09-06 20:27:30 -0600161 ofnode node;
162 int count;
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200163
164 /* Find first matching node, there should be at least one */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600165 node = ofnode_by_prop_value(start, propname, propval, proplen);
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200166 ut_assert(ofnode_valid(node));
167 str = ofnode_read_string(node, propname);
168 ut_assert(str && !strcmp(str, propval));
169
170 /* Find the rest of the matching nodes */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600171 count = 1;
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200172 while (true) {
Simon Glass9e7a42a2022-09-06 20:27:30 -0600173 node = ofnode_by_prop_value(node, propname, propval, proplen);
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200174 if (!ofnode_valid(node))
175 break;
176 str = ofnode_read_string(node, propname);
Simon Glass9e7a42a2022-09-06 20:27:30 -0600177 ut_asserteq_str(propval, str);
178 count++;
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200179 }
Simon Glass9e7a42a2022-09-06 20:27:30 -0600180 ut_asserteq(expect_count, count);
181
182 return 0;
183}
184
185static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
186{
187 ut_assertok(check_prop_values(uts, ofnode_null(), "compatible",
188 "denx,u-boot-fdt-test", 11));
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200189
190 return 0;
191}
Simon Glass974dccd2020-07-28 19:41:12 -0600192DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glass699c9ca2018-10-01 12:22:08 -0600193
Simon Glass75b8a872023-09-26 08:14:39 -0600194/* test ofnode_by_prop_value() with a the 'other' oftree */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600195static int dm_test_ofnode_by_prop_value_ot(struct unit_test_state *uts)
196{
197 oftree otree = get_other_oftree(uts);
198
199 ut_assertok(check_prop_values(uts, oftree_root(otree), "str-prop",
200 "other", 2));
201
202 return 0;
203}
Simon Glassdb6d8722023-09-26 08:14:38 -0600204DM_TEST(dm_test_ofnode_by_prop_value_ot,
205 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -0600206
Simon Glass75b8a872023-09-26 08:14:39 -0600207/* test ofnode_read_fmap_entry() */
Simon Glass699c9ca2018-10-01 12:22:08 -0600208static int dm_test_ofnode_fmap(struct unit_test_state *uts)
209{
210 struct fmap_entry entry;
211 ofnode node;
212
213 node = ofnode_path("/cros-ec/flash");
214 ut_assert(ofnode_valid(node));
215 ut_assertok(ofnode_read_fmap_entry(node, &entry));
216 ut_asserteq(0x08000000, entry.offset);
217 ut_asserteq(0x20000, entry.length);
218
219 return 0;
220}
Simon Glass974dccd2020-07-28 19:41:12 -0600221DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf3455962020-01-27 08:49:43 -0700222
Simon Glass75b8a872023-09-26 08:14:39 -0600223/* test ofnode_read_prop() */
Simon Glass0c2e9802020-01-27 08:49:44 -0700224static int dm_test_ofnode_read(struct unit_test_state *uts)
225{
226 const u32 *val;
227 ofnode node;
228 int size;
229
Simon Glass310487d2023-09-26 08:14:46 -0600230 node = oftree_path(oftree_default(), "/");
231 ut_assert(ofnode_valid(node));
232
Simon Glass0c2e9802020-01-27 08:49:44 -0700233 node = ofnode_path("/a-test");
234 ut_assert(ofnode_valid(node));
235
236 val = ofnode_read_prop(node, "int-value", &size);
237 ut_assertnonnull(val);
238 ut_asserteq(4, size);
239 ut_asserteq(1234, fdt32_to_cpu(val[0]));
240
241 val = ofnode_read_prop(node, "missing", &size);
242 ut_assertnull(val);
243 ut_asserteq(-FDT_ERR_NOTFOUND, size);
244
245 /* Check it works without a size parameter */
246 val = ofnode_read_prop(node, "missing", NULL);
247 ut_assertnull(val);
248
249 return 0;
250}
Simon Glassdb6d8722023-09-26 08:14:38 -0600251DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_FDT);
Simon Glass0c2e9802020-01-27 08:49:44 -0700252
Simon Glass75b8a872023-09-26 08:14:39 -0600253/* test ofnode_read_prop() with the 'other' tree */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600254static int dm_test_ofnode_read_ot(struct unit_test_state *uts)
255{
256 oftree otree = get_other_oftree(uts);
257 const char *val;
258 ofnode node;
259 int size;
260
Simon Glass310487d2023-09-26 08:14:46 -0600261 node = oftree_path(otree, "/");
262 ut_assert(ofnode_valid(node));
263
Simon Glass9e7a42a2022-09-06 20:27:30 -0600264 node = oftree_path(otree, "/node/subnode");
265 ut_assert(ofnode_valid(node));
266
267 val = ofnode_read_prop(node, "str-prop", &size);
268 ut_assertnonnull(val);
269 ut_asserteq_str("other", val);
270 ut_asserteq(6, size);
271
272 return 0;
273}
Simon Glassdb6d8722023-09-26 08:14:38 -0600274DM_TEST(dm_test_ofnode_read_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -0600275
Simon Glass75b8a872023-09-26 08:14:39 -0600276/* test ofnode_count_/parse_phandle_with_args() */
Patrick Delaunay8cd28012020-09-25 09:41:16 +0200277static int dm_test_ofnode_phandle(struct unit_test_state *uts)
278{
279 struct ofnode_phandle_args args;
280 ofnode node;
281 int ret;
282 const char prop[] = "test-gpios";
283 const char cell[] = "#gpio-cells";
284 const char prop2[] = "phandle-value";
285
286 node = ofnode_path("/a-test");
287 ut_assert(ofnode_valid(node));
288
289 /* Test ofnode_count_phandle_with_args with cell name */
290 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
291 ut_asserteq(-ENOENT, ret);
292 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
293 ut_asserteq(-EINVAL, ret);
294 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
295 ut_asserteq(5, ret);
296
297 /* Test ofnode_parse_phandle_with_args with cell name */
298 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
299 &args);
300 ut_asserteq(-ENOENT, ret);
301 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
302 &args);
303 ut_asserteq(-EINVAL, ret);
304 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
305 ut_assertok(ret);
306 ut_asserteq(1, args.args_count);
307 ut_asserteq(1, args.args[0]);
308 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
309 ut_assertok(ret);
310 ut_asserteq(1, args.args_count);
311 ut_asserteq(4, args.args[0]);
312 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
313 ut_assertok(ret);
314 ut_asserteq(5, args.args_count);
315 ut_asserteq(5, args.args[0]);
316 ut_asserteq(1, args.args[4]);
317 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
318 ut_asserteq(-ENOENT, ret);
319 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
320 ut_assertok(ret);
321 ut_asserteq(1, args.args_count);
322 ut_asserteq(12, args.args[0]);
323 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
324 ut_asserteq(-ENOENT, ret);
325
326 /* Test ofnode_count_phandle_with_args with cell count */
327 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
328 ut_asserteq(-ENOENT, ret);
329 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
330 ut_asserteq(3, ret);
331
332 /* Test ofnode_parse_phandle_with_args with cell count */
333 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
334 ut_assertok(ret);
335 ut_asserteq(1, ofnode_valid(args.node));
336 ut_asserteq(1, args.args_count);
337 ut_asserteq(10, args.args[0]);
338 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
339 ut_asserteq(-EINVAL, ret);
340 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
341 ut_assertok(ret);
342 ut_asserteq(1, ofnode_valid(args.node));
343 ut_asserteq(1, args.args_count);
344 ut_asserteq(30, args.args[0]);
345 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
346 ut_asserteq(-ENOENT, ret);
347
348 return 0;
349}
350DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
351
Simon Glass75b8a872023-09-26 08:14:39 -0600352/* test ofnode_count_/parse_phandle_with_args() with 'other' tree */
Simon Glassf912a722022-09-06 20:27:27 -0600353static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
354{
355 oftree otree = get_other_oftree(uts);
356 struct ofnode_phandle_args args;
357 ofnode node;
358 int ret;
359
360 node = oftree_path(otree, "/node");
361
362 /* Test ofnode_count_phandle_with_args with cell name */
363 ret = ofnode_count_phandle_with_args(node, "missing", "#gpio-cells", 0);
364 ut_asserteq(-ENOENT, ret);
365 ret = ofnode_count_phandle_with_args(node, "target", "#invalid", 0);
366 ut_asserteq(-EINVAL, ret);
367 ret = ofnode_count_phandle_with_args(node, "target", "#gpio-cells", 0);
368 ut_asserteq(1, ret);
369
370 ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
371 0, &args);
372 ut_assertok(ret);
373 ut_asserteq(2, args.args_count);
374 ut_asserteq(3, args.args[0]);
375 ut_asserteq(4, args.args[1]);
376
377 return 0;
378}
379DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
380
Simon Glass75b8a872023-09-26 08:14:39 -0600381/* test ofnode_read_chosen_string/node/prop() */
Simon Glassf3455962020-01-27 08:49:43 -0700382static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
383{
384 const char *str;
Simon Glasse09223c2020-01-27 08:49:46 -0700385 const u32 *val;
Simon Glassf3455962020-01-27 08:49:43 -0700386 ofnode node;
Simon Glasse09223c2020-01-27 08:49:46 -0700387 int size;
Simon Glassf3455962020-01-27 08:49:43 -0700388
389 str = ofnode_read_chosen_string("setting");
390 ut_assertnonnull(str);
391 ut_asserteq_str("sunrise ohoka", str);
392 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
393
394 node = ofnode_get_chosen_node("other-node");
395 ut_assert(ofnode_valid(node));
396 ut_asserteq_str("c-test@5", ofnode_get_name(node));
397
398 node = ofnode_get_chosen_node("setting");
399 ut_assert(!ofnode_valid(node));
400
Simon Glasse09223c2020-01-27 08:49:46 -0700401 val = ofnode_read_chosen_prop("int-values", &size);
402 ut_assertnonnull(val);
403 ut_asserteq(8, size);
404 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
405 ut_asserteq(72993, fdt32_to_cpu(val[1]));
406
Simon Glassf3455962020-01-27 08:49:43 -0700407 return 0;
408}
Simon Glass974dccd2020-07-28 19:41:12 -0600409DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
developercf8bc132020-05-02 11:35:10 +0200410
Simon Glass75b8a872023-09-26 08:14:39 -0600411/* test ofnode_get_aliases_node/prop() */
Michal Simek92a88622020-07-28 12:51:08 +0200412static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
413{
414 const void *val;
415 ofnode node;
416 int size;
417
Michael Walle7efcdfd2021-02-25 16:51:11 +0100418 node = ofnode_get_aliases_node("ethernet3");
Michal Simek92a88622020-07-28 12:51:08 +0200419 ut_assert(ofnode_valid(node));
420 ut_asserteq_str("sbe5", ofnode_get_name(node));
421
422 node = ofnode_get_aliases_node("unknown");
423 ut_assert(!ofnode_valid(node));
424
425 val = ofnode_read_aliases_prop("spi0", &size);
426 ut_assertnonnull(val);
427 ut_asserteq(7, size);
428 ut_asserteq_str("/spi@0", (const char *)val);
429
430 return 0;
431}
432DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
433
developercf8bc132020-05-02 11:35:10 +0200434static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
435{
436 ofnode node, child_node;
437 u32 val;
438
439 node = ofnode_path("/i-test");
440 ut_assert(ofnode_valid(node));
441
442 val = ofnode_get_child_count(node);
443 ut_asserteq(3, val);
444
445 child_node = ofnode_first_subnode(node);
446 ut_assert(ofnode_valid(child_node));
447 val = ofnode_get_child_count(child_node);
448 ut_asserteq(0, val);
449
450 return 0;
451}
452DM_TEST(dm_test_ofnode_get_child_count,
Simon Glass974dccd2020-07-28 19:41:12 -0600453 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass5de5b3b2020-11-28 17:50:02 -0700454
Simon Glass75b8a872023-09-26 08:14:39 -0600455/* test ofnode_get_child_count() with 'other' tree */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600456static int dm_test_ofnode_get_child_count_ot(struct unit_test_state *uts)
457{
458 oftree otree = get_other_oftree(uts);
459 ofnode node, child_node;
460 u32 val;
461
462 node = oftree_path(otree, "/node");
463 ut_assert(ofnode_valid(node));
464
465 val = ofnode_get_child_count(node);
466 ut_asserteq(2, val);
467
468 child_node = ofnode_first_subnode(node);
469 ut_assert(ofnode_valid(child_node));
470 val = ofnode_get_child_count(child_node);
471 ut_asserteq(0, val);
472
473 return 0;
474}
Simon Glassdb6d8722023-09-26 08:14:38 -0600475DM_TEST(dm_test_ofnode_get_child_count_ot,
476 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -0600477
Simon Glass5de5b3b2020-11-28 17:50:02 -0700478static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
479{
480 ofnode root_node = ofnode_path("/");
481 ofnode node = ofnode_path("/usb@0");
482
483 ut_assert(ofnode_is_enabled(root_node));
484 ut_assert(!ofnode_is_enabled(node));
485
486 return 0;
487}
488DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800489
Simon Glass75b8a872023-09-26 08:14:39 -0600490/* test ofnode_is_enabled() with 'other' tree */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600491static int dm_test_ofnode_is_enabled_ot(struct unit_test_state *uts)
492{
493 oftree otree = get_other_oftree(uts);
494 ofnode root_node = oftree_root(otree);
495 ofnode node = oftree_path(otree, "/target");
496
497 ut_assert(ofnode_is_enabled(root_node));
498 ut_assert(!ofnode_is_enabled(node));
499
500 return 0;
501}
502DM_TEST(dm_test_ofnode_is_enabled_ot, UT_TESTF_OTHER_FDT);
503
Simon Glass75b8a872023-09-26 08:14:39 -0600504/* test ofnode_get_addr/size() */
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800505static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
506{
507 ofnode node;
508 fdt_addr_t addr;
509 fdt_size_t size;
510
511 node = ofnode_path("/translation-test@8000");
512 ut_assert(ofnode_valid(node));
513 addr = ofnode_get_addr(node);
514 size = ofnode_get_size(node);
515 ut_asserteq(0x8000, addr);
516 ut_asserteq(0x4000, size);
517
518 node = ofnode_path("/translation-test@8000/dev@1,100");
519 ut_assert(ofnode_valid(node));
520 addr = ofnode_get_addr(node);
521 size = ofnode_get_size(node);
522 ut_asserteq(0x9000, addr);
523 ut_asserteq(0x1000, size);
524
525 node = ofnode_path("/emul-mux-controller");
526 ut_assert(ofnode_valid(node));
527 addr = ofnode_get_addr(node);
528 size = ofnode_get_size(node);
Patrice Chotardb4c52112022-01-04 08:42:48 +0100529 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800530 ut_asserteq(FDT_SIZE_T_NONE, size);
531
Marek Behún177ab7f2021-05-26 14:08:17 +0200532 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
533 ut_assert(ofnode_valid(node));
534 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
535 ut_asserteq_64(0x42, addr);
536
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800537 return 0;
538}
539DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Behúne897e3c2021-05-26 14:08:18 +0200540
Simon Glass75b8a872023-09-26 08:14:39 -0600541/* test ofnode_get_addr() with 'other' tree */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600542static int dm_test_ofnode_get_reg_ot(struct unit_test_state *uts)
543{
544 oftree otree = get_other_oftree(uts);
545 ofnode node = oftree_path(otree, "/target");
546 fdt_addr_t addr;
547
548 addr = ofnode_get_addr(node);
549 ut_asserteq(0x8000, addr);
550
551 return 0;
552}
Simon Glassdb6d8722023-09-26 08:14:38 -0600553DM_TEST(dm_test_ofnode_get_reg_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -0600554
Marek Behúne897e3c2021-05-26 14:08:18 +0200555static int dm_test_ofnode_get_path(struct unit_test_state *uts)
556{
557 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
558 char buf[64];
559 ofnode node;
Marek Behúne897e3c2021-05-26 14:08:18 +0200560
561 node = ofnode_path(path);
562 ut_assert(ofnode_valid(node));
563
Simon Glass9e7a42a2022-09-06 20:27:30 -0600564 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
Marek Behúne897e3c2021-05-26 14:08:18 +0200565 ut_asserteq_str(path, buf);
566
Simon Glass9e7a42a2022-09-06 20:27:30 -0600567 ut_asserteq(-ENOSPC, ofnode_get_path(node, buf, 32));
Marek Behúne897e3c2021-05-26 14:08:18 +0200568
Simon Glass9e7a42a2022-09-06 20:27:30 -0600569 ut_assertok(ofnode_get_path(ofnode_root(), buf, 32));
Simon Glasse3be5fc2022-09-06 20:27:18 -0600570 ut_asserteq_str("/", buf);
571
Marek Behúne897e3c2021-05-26 14:08:18 +0200572 return 0;
573}
574DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0034d962021-08-07 07:24:01 -0600575
Simon Glass75b8a872023-09-26 08:14:39 -0600576/* test ofnode_get_path() with 'other' tree */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600577static int dm_test_ofnode_get_path_ot(struct unit_test_state *uts)
578{
579 oftree otree = get_other_oftree(uts);
580 const char *path = "/node/subnode";
581 ofnode node = oftree_path(otree, path);
582 char buf[64];
583
584 ut_assert(ofnode_valid(node));
585
586 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
587 ut_asserteq_str(path, buf);
588
589 ut_assertok(ofnode_get_path(oftree_root(otree), buf, 32));
590 ut_asserteq_str("/", buf);
591
592 return 0;
593}
Simon Glassdb6d8722023-09-26 08:14:38 -0600594DM_TEST(dm_test_ofnode_get_path_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -0600595
Simon Glass75b8a872023-09-26 08:14:39 -0600596/* test ofnode_conf_read_bool/int/str() */
Simon Glass0034d962021-08-07 07:24:01 -0600597static int dm_test_ofnode_conf(struct unit_test_state *uts)
598{
599 ut_assert(!ofnode_conf_read_bool("missing"));
600 ut_assert(ofnode_conf_read_bool("testing-bool"));
601
602 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
603 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
604
605 ut_assertnull(ofnode_conf_read_str("missing"));
606 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
607
608 return 0;
609}
Simon Glassdb6d8722023-09-26 08:14:38 -0600610DM_TEST(dm_test_ofnode_conf, UT_TESTF_SCAN_FDT);
Michael Wallef1f87402021-10-15 15:15:18 +0200611
Michal Simek43c42bd2023-08-31 08:59:05 +0200612static int dm_test_ofnode_options(struct unit_test_state *uts)
613{
Michal Simek6a7c1ce2023-08-31 09:04:27 +0200614 u64 bootscr_address, bootscr_offset;
615 u64 bootscr_flash_offset, bootscr_flash_size;
Michal Simek43c42bd2023-08-31 08:59:05 +0200616
617 ut_assertok(ofnode_read_bootscript_address(&bootscr_address,
618 &bootscr_offset));
619 ut_asserteq_64(0, bootscr_address);
620 ut_asserteq_64(0x12345678, bootscr_offset);
621
Michal Simek6a7c1ce2023-08-31 09:04:27 +0200622 ut_assertok(ofnode_read_bootscript_flash(&bootscr_flash_offset,
623 &bootscr_flash_size));
624 ut_asserteq_64(0, bootscr_flash_offset);
625 ut_asserteq_64(0x2000, bootscr_flash_size);
626
Michal Simek43c42bd2023-08-31 08:59:05 +0200627 return 0;
628}
629DM_TEST(dm_test_ofnode_options, 0);
630
Michael Wallef1f87402021-10-15 15:15:18 +0200631static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
632{
633 const char compatible[] = "denx,u-boot-fdt-test";
634 bool found = false;
635 ofnode node;
636
637 ofnode_for_each_compatible_node(node, compatible) {
638 ut_assert(ofnode_device_is_compatible(node, compatible));
639 found = true;
640 }
641
642 /* There should be at least one matching node */
643 ut_assert(found);
644
645 return 0;
646}
647DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glass73025392021-10-23 17:26:04 -0600648
Simon Glass75b8a872023-09-26 08:14:39 -0600649/* test dm_test_ofnode_string_count/index/list() */
Simon Glass73025392021-10-23 17:26:04 -0600650static int dm_test_ofnode_string(struct unit_test_state *uts)
651{
Simon Glass9580bfc2021-10-23 17:26:07 -0600652 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600653 const char *out;
654 ofnode node;
655
656 node = ofnode_path("/a-test");
657 ut_assert(ofnode_valid(node));
658
659 /* single string */
660 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
661 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
662 ut_asserteq_str("test string", out);
663 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
664 "test string"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600665 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
666 ut_asserteq_str("test string", val[0]);
667 ut_assertnull(val[1]);
668 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600669
670 /* list of strings */
671 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
672 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
673 &out));
674 ut_asserteq_str("mux0", out);
675 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
676 "mux0"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600677 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
678 &val));
679 ut_asserteq_str("mux0", val[0]);
680 ut_asserteq_str("mux1", val[1]);
681 ut_asserteq_str("mux2", val[2]);
682 ut_asserteq_str("mux3", val[3]);
683 ut_asserteq_str("mux4", val[4]);
684 ut_assertnull(val[5]);
685 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600686
687 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
688 &out));
689 ut_asserteq_str("mux4", out);
690 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
691 "mux4"));
692
693 return 0;
694}
Simon Glassdb6d8722023-09-26 08:14:38 -0600695DM_TEST(dm_test_ofnode_string, UT_TESTF_SCAN_FDT);
Simon Glass73025392021-10-23 17:26:04 -0600696
Simon Glass75b8a872023-09-26 08:14:39 -0600697/* test error returns from ofnode_read_string_count/index/list() */
Simon Glass73025392021-10-23 17:26:04 -0600698static int dm_test_ofnode_string_err(struct unit_test_state *uts)
699{
Simon Glass9580bfc2021-10-23 17:26:07 -0600700 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600701 const char *out;
702 ofnode node;
703
704 /*
705 * Test error codes only on livetree, as they are different with
706 * flattree
707 */
708 node = ofnode_path("/a-test");
709 ut_assert(ofnode_valid(node));
710
711 /* non-existent property */
712 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
713 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
714 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600715 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glass73025392021-10-23 17:26:04 -0600716
717 /* empty property */
718 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
719 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
720 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600721 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
722 &val));
Simon Glass73025392021-10-23 17:26:04 -0600723
724 /* badly formatted string list */
725 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
726 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
727 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600728 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
729 &val));
Simon Glass73025392021-10-23 17:26:04 -0600730
731 /* out of range / not found */
732 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
733 &out));
734 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
735 "other"));
736
737 /* negative value for index is not allowed, so don't test for that */
738
739 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
740 "mux-control-names", 5,
741 &out));
742
743 return 0;
744}
745DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200746
Simon Glass75b8a872023-09-26 08:14:39 -0600747static int dm_test_ofnode_read_phy_mode(struct unit_test_state *uts)
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200748{
749 ofnode eth_node, phy_node;
Marek Behúnbc194772022-04-07 00:33:01 +0200750 phy_interface_t mode;
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200751 u32 reg;
752
753 eth_node = ofnode_path("/phy-test-eth");
754 ut_assert(ofnode_valid(eth_node));
755
Marek Behúnbc194772022-04-07 00:33:01 +0200756 mode = ofnode_read_phy_mode(eth_node);
757 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
758
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200759 phy_node = ofnode_get_phy_node(eth_node);
760 ut_assert(ofnode_valid(phy_node));
761
762 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
763 ut_asserteq_64(0x1, reg);
764
765 return 0;
766}
Simon Glass75b8a872023-09-26 08:14:39 -0600767DM_TEST(dm_test_ofnode_read_phy_mode, UT_TESTF_SCAN_FDT);
Simon Glassef75c592022-07-30 15:52:08 -0600768
769/**
770 * make_ofnode_fdt() - Create an FDT for testing with ofnode
771 *
772 * The size is set to the minimum needed
773 *
774 * @uts: Test state
775 * @fdt: Place to write FDT
776 * @size: Maximum size of space for fdt
Simon Glass9e7a42a2022-09-06 20:27:30 -0600777 * @id: id value to add to the tree ('id' property in root node)
Simon Glassef75c592022-07-30 15:52:08 -0600778 */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600779static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size,
780 int id)
Simon Glassef75c592022-07-30 15:52:08 -0600781{
782 ut_assertok(fdt_create(fdt, size));
783 ut_assertok(fdt_finish_reservemap(fdt));
784 ut_assert(fdt_begin_node(fdt, "") >= 0);
785
Simon Glass9e7a42a2022-09-06 20:27:30 -0600786 ut_assertok(fdt_property_u32(fdt, "id", id));
787
Simon Glassef75c592022-07-30 15:52:08 -0600788 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
789 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
790 ut_assertok(fdt_end_node(fdt));
791
792 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
793 ut_assertok(fdt_end_node(fdt));
794
795 ut_assertok(fdt_end_node(fdt));
796 ut_assertok(fdt_finish(fdt));
797
798 return 0;
799}
800
Simon Glass75b8a872023-09-26 08:14:39 -0600801/* Check that aliases work on the control FDT */
802static int dm_test_ofnode_aliases(struct unit_test_state *uts)
Simon Glassef75c592022-07-30 15:52:08 -0600803{
Simon Glassef75c592022-07-30 15:52:08 -0600804 ofnode node;
805
Simon Glassef75c592022-07-30 15:52:08 -0600806 node = ofnode_get_aliases_node("ethernet3");
807 ut_assert(ofnode_valid(node));
808 ut_asserteq_str("sbe5", ofnode_get_name(node));
809
Simon Glass2b9b14582022-09-06 20:27:21 -0600810 ut_assert(!oftree_valid(oftree_null()));
811
Simon Glassc7599442022-10-20 18:22:49 -0600812 return 0;
813}
Simon Glass75b8a872023-09-26 08:14:39 -0600814DM_TEST(dm_test_ofnode_aliases, UT_TESTF_SCAN_FDT);
Simon Glassdad97512022-09-06 20:27:29 -0600815
Simon Glass75b8a872023-09-26 08:14:39 -0600816/**
817 * dm_test_ofnode_root_mult() - Check aliaes on control and 'other' tree
818 *
819 * Check that aliases work only with the control FDT, not with 'other' tree.
820 * This is not actually the desired behaviour. If aliases are implemented for
821 * any tree, then this test should be changed.
822 */
Simon Glassc7599442022-10-20 18:22:49 -0600823static int dm_test_ofnode_root_mult(struct unit_test_state *uts)
824{
825 char fdt[256];
826 oftree tree;
827 ofnode node;
Simon Glassdad97512022-09-06 20:27:29 -0600828
Simon Glassc7599442022-10-20 18:22:49 -0600829 /* skip this test if multiple FDTs are not supported */
830 if (!IS_ENABLED(CONFIG_OFNODE_MULTI_TREE))
831 return -EAGAIN;
832
833 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt), 0));
834 ut_assertok(get_oftree(uts, fdt, &tree));
Simon Glass2b9b14582022-09-06 20:27:21 -0600835 ut_assert(oftree_valid(tree));
Simon Glassef75c592022-07-30 15:52:08 -0600836
837 /* Make sure they don't work on this new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600838 node = oftree_path(tree, "mmc0");
Simon Glassef75c592022-07-30 15:52:08 -0600839 ut_assert(!ofnode_valid(node));
840
841 /* It should appear in the new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600842 node = oftree_path(tree, "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600843 ut_assert(ofnode_valid(node));
844
845 /* ...and not in the control FDT */
Simon Glass45ae59d2022-09-06 20:27:24 -0600846 node = oftree_path(oftree_default(), "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600847 ut_assert(!ofnode_valid(node));
848
Simon Glassdad97512022-09-06 20:27:29 -0600849 free_oftree(tree);
Simon Glassef75c592022-07-30 15:52:08 -0600850
851 return 0;
852}
Simon Glassc7599442022-10-20 18:22:49 -0600853DM_TEST(dm_test_ofnode_root_mult, UT_TESTF_SCAN_FDT);
Simon Glass270a4432022-07-30 15:52:09 -0600854
Simon Glass75b8a872023-09-26 08:14:39 -0600855/* test ofnode_set_enabled(), ofnode_write_prop() on a livetree */
Simon Glass270a4432022-07-30 15:52:09 -0600856static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
857{
858 struct udevice *dev;
859 ofnode node;
860
Simon Glass270a4432022-07-30 15:52:09 -0600861 /* Test enabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600862 node = ofnode_path("/usb@2");
863
Simon Glass3ee3d152022-07-30 15:52:13 -0600864 ut_assert(!ofnode_is_enabled(node));
865 ut_assertok(ofnode_set_enabled(node, true));
866 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600867
868 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
869 &dev);
870 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
871
872 /* Test string property setting */
Simon Glass270a4432022-07-30 15:52:09 -0600873 ut_assert(device_is_compatible(dev, "sandbox,usb"));
874 ofnode_write_string(node, "compatible", "gdsys,super-usb");
875 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
876 ofnode_write_string(node, "compatible", "sandbox,usb");
877 ut_assert(device_is_compatible(dev, "sandbox,usb"));
878
879 /* Test setting generic properties */
880
881 /* Non-existent in DTB */
882 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
883 /* reg = 0x42, size = 0x100 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -0600884 ut_assertok(ofnode_write_prop(node, "reg",
Simon Glass17abed02022-09-06 20:27:32 -0600885 "\x00\x00\x00\x42\x00\x00\x01\x00", 8,
886 false));
Simon Glass270a4432022-07-30 15:52:09 -0600887 ut_asserteq(0x42, dev_read_addr(dev));
888
889 /* Test disabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600890 device_remove(dev, DM_REMOVE_NORMAL);
891 device_unbind(dev);
892
Simon Glass3ee3d152022-07-30 15:52:13 -0600893 ut_assert(ofnode_is_enabled(node));
894 ut_assertok(ofnode_set_enabled(node, false));
895 ut_assert(!ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600896
897 return 0;
898}
Simon Glassb75dc162022-07-30 15:52:11 -0600899DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass8ae9f962022-09-06 20:27:07 -0600900 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd28e31e2022-07-30 15:52:14 -0600901
Simon Glass17abed02022-09-06 20:27:32 -0600902static int check_write_prop(struct unit_test_state *uts, ofnode node)
903{
904 char prop[] = "middle-name";
905 char name[10];
906 int len;
907
908 strcpy(name, "cecil");
909 len = strlen(name) + 1;
910 ut_assertok(ofnode_write_prop(node, prop, name, len, false));
911 ut_asserteq_str(name, ofnode_read_string(node, prop));
912
913 /* change the underlying value, this should mess up the live tree */
914 strcpy(name, "tony");
915 if (of_live_active()) {
916 ut_asserteq_str(name, ofnode_read_string(node, prop));
917 } else {
918 ut_asserteq_str("cecil", ofnode_read_string(node, prop));
919 }
920
921 /* try again, this time copying the property */
922 strcpy(name, "mary");
923 ut_assertok(ofnode_write_prop(node, prop, name, len, true));
924 ut_asserteq_str(name, ofnode_read_string(node, prop));
925 strcpy(name, "leah");
926
927 /* both flattree and livetree behave the same */
928 ut_asserteq_str("mary", ofnode_read_string(node, prop));
929
930 return 0;
931}
932
933/* writing the tree with and without copying the property */
934static int dm_test_ofnode_write_copy(struct unit_test_state *uts)
935{
936 ofnode node;
937
938 node = ofnode_path("/a-test");
939 ut_assertok(check_write_prop(uts, node));
940
941 return 0;
942}
943DM_TEST(dm_test_ofnode_write_copy, UT_TESTF_SCAN_FDT);
944
Simon Glass75b8a872023-09-26 08:14:39 -0600945/* test writing a property to the 'other' tree */
Simon Glass17abed02022-09-06 20:27:32 -0600946static int dm_test_ofnode_write_copy_ot(struct unit_test_state *uts)
947{
948 oftree otree = get_other_oftree(uts);
949 ofnode node, check_node;
950
951 node = oftree_path(otree, "/node");
952 ut_assertok(check_write_prop(uts, node));
953
954 /* make sure the control FDT is not touched */
955 check_node = ofnode_path("/node");
956 ut_assertnull(ofnode_read_string(check_node, "middle-name"));
957
958 return 0;
959}
Simon Glassdb6d8722023-09-26 08:14:38 -0600960DM_TEST(dm_test_ofnode_write_copy_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass17abed02022-09-06 20:27:32 -0600961
Simon Glass75b8a872023-09-26 08:14:39 -0600962/* test ofnode_read_u32_index/default() */
Simon Glassd28e31e2022-07-30 15:52:14 -0600963static int dm_test_ofnode_u32(struct unit_test_state *uts)
964{
965 ofnode node;
Simon Glasse3be5fc2022-09-06 20:27:18 -0600966 u32 val;
Simon Glassd28e31e2022-07-30 15:52:14 -0600967
968 node = ofnode_path("/lcd");
969 ut_assert(ofnode_valid(node));
970 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
971 ut_assertok(ofnode_write_u32(node, "xres", 1367));
972 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
973 ut_assertok(ofnode_write_u32(node, "xres", 1366));
974
Simon Glasse3be5fc2022-09-06 20:27:18 -0600975 node = ofnode_path("/backlight");
976 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
977 ut_asserteq(0, val);
978 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
979 ut_asserteq(16, val);
980 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
981 ut_asserteq(255, val);
982 ut_asserteq(-EOVERFLOW,
983 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
984 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
985
Simon Glassd28e31e2022-07-30 15:52:14 -0600986 return 0;
987}
Simon Glass8ae9f962022-09-06 20:27:07 -0600988DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass56bc3322022-09-06 20:27:02 -0600989
Simon Glass75b8a872023-09-26 08:14:39 -0600990/* test ofnode_read_u32_array() */
Simon Glasse3be5fc2022-09-06 20:27:18 -0600991static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
992{
993 ofnode node;
994 u32 val[10];
995
996 node = ofnode_path("/a-test");
997 ut_assert(ofnode_valid(node));
998 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
999 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
1000 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
1001 1));
1002
1003 memset(val, '\0', sizeof(val));
1004 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
1005 ut_asserteq(0, val[0]);
1006 ut_asserteq(5678, val[1]);
1007 ut_asserteq(9123, val[2]);
1008 ut_asserteq(4567, val[3]);
1009 ut_asserteq(0, val[4]);
1010 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
1011 4));
1012
1013 return 0;
1014}
1015DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1016
Simon Glassc681e092023-09-26 08:14:45 -06001017/* test ofnode_read_u64() and ofnode_write_u64() */
1018static int dm_test_ofnode_u64(struct unit_test_state *uts)
Simon Glasse3be5fc2022-09-06 20:27:18 -06001019{
1020 ofnode node;
1021 u64 val;
1022
1023 node = ofnode_path("/a-test");
1024 ut_assert(ofnode_valid(node));
1025 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
1026 ut_asserteq_64(0x1111222233334444, val);
Simon Glassc681e092023-09-26 08:14:45 -06001027 ut_assertok(ofnode_write_u64(node, "new-int64-value", 0x9876543210));
1028 ut_assertok(ofnode_read_u64(node, "new-int64-value", &val));
1029 ut_asserteq_64(0x9876543210, val);
1030
Simon Glasse3be5fc2022-09-06 20:27:18 -06001031 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
1032
Michal Simek08a194e2023-08-25 11:37:46 +02001033 ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val));
1034 ut_asserteq_64(0x1111222233334444, val);
1035 ut_assertok(ofnode_read_u64_index(node, "int64-array", 1, &val));
1036 ut_asserteq_64(0x4444333322221111, val);
1037 ut_asserteq(-EOVERFLOW,
1038 ofnode_read_u64_index(node, "int64-array", 2, &val));
1039 ut_asserteq(-EINVAL, ofnode_read_u64_index(node, "missing", 0, &val));
1040
Simon Glassc681e092023-09-26 08:14:45 -06001041 ut_assertok(ofnode_write_u64(node, "int64-array", 0x9876543210));
1042 ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val));
1043 ut_asserteq_64(0x9876543210, val);
1044 ut_asserteq(-EOVERFLOW,
1045 ofnode_read_u64_index(node, "int64-array", 1, &val));
1046
Simon Glasse3be5fc2022-09-06 20:27:18 -06001047 return 0;
1048}
Simon Glassc681e092023-09-26 08:14:45 -06001049DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -06001050
Simon Glass56bc3322022-09-06 20:27:02 -06001051static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
1052{
1053 ofnode node, check, subnode;
1054 char buf[128];
1055
Simon Glass56bc3322022-09-06 20:27:02 -06001056 node = ofnode_path("/lcd");
1057 ut_assert(ofnode_valid(node));
1058 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
1059 check = ofnode_path("/lcd/edmund");
1060 ut_asserteq(subnode.of_offset, check.of_offset);
1061 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1062 ut_asserteq_str("/lcd/edmund", buf);
1063
1064 if (of_live_active()) {
1065 struct device_node *child;
1066
1067 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
1068 2, &child));
1069 ut_asserteq_str("ed", child->name);
1070 ut_asserteq_str("/lcd/ed", child->full_name);
1071 check = ofnode_path("/lcd/ed");
1072 ut_asserteq_ptr(child, check.np);
1073 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
1074 sizeof(buf)));
1075 ut_asserteq_str("/lcd/ed", buf);
1076 }
1077
1078 /* An existing node should be returned with -EEXIST */
1079 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
1080 ut_asserteq(subnode.of_offset, check.of_offset);
1081
1082 /* add a root node */
1083 node = ofnode_path("/");
1084 ut_assert(ofnode_valid(node));
1085 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
1086 check = ofnode_path("/lcd2");
1087 ut_asserteq(subnode.of_offset, check.of_offset);
1088 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1089 ut_asserteq_str("/lcd2", buf);
1090
1091 if (of_live_active()) {
1092 ulong start;
1093 int i;
1094
1095 /*
1096 * Make sure each of the three malloc()checks in
1097 * of_add_subnode() work
1098 */
1099 for (i = 0; i < 3; i++) {
1100 malloc_enable_testing(i);
1101 start = ut_check_free();
1102 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
1103 &check));
1104 ut_assertok(ut_check_delta(start));
1105 }
1106
1107 /* This should pass since we allow 3 allocations */
1108 malloc_enable_testing(3);
1109 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
1110 malloc_disable_testing();
1111 }
1112
Simon Glass238afb52022-09-06 20:27:03 -06001113 /* write to the empty node */
1114 ut_assertok(ofnode_write_string(subnode, "example", "text"));
1115
Simon Glass56bc3322022-09-06 20:27:02 -06001116 return 0;
1117}
Simon Glass8ae9f962022-09-06 20:27:07 -06001118DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass4caa79a2022-09-06 20:27:16 -06001119
1120static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
1121{
1122 ofnode node, subnode;
1123 struct ofprop prop;
1124 int count;
1125
Dzmitry Sankouski54f4c832023-01-22 18:21:23 +03001126 node = ofnode_path("/ofnode-foreach");
Simon Glass4caa79a2022-09-06 20:27:16 -06001127 count = 0;
1128
1129 /* we expect "compatible" for each node */
1130 ofnode_for_each_prop(prop, node)
1131 count++;
1132 ut_asserteq(1, count);
1133
1134 /* there are two nodes, each with 2 properties */
1135 ofnode_for_each_subnode(subnode, node)
1136 ofnode_for_each_prop(prop, subnode)
1137 count++;
1138 ut_asserteq(5, count);
1139
1140 return 0;
1141}
1142DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -06001143
1144static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
1145{
1146 const char *compat = "denx,u-boot-fdt-test";
1147 ofnode node;
1148 int count;
1149
1150 count = 0;
1151 for (node = ofnode_null();
1152 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1153 count++;
1154 ut_asserteq(11, count);
1155
1156 return 0;
1157}
1158DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
1159
Simon Glass75b8a872023-09-26 08:14:39 -06001160/* check ofnode_by_compatible() on the 'other' tree */
Simon Glass9e7a42a2022-09-06 20:27:30 -06001161static int dm_test_ofnode_by_compatible_ot(struct unit_test_state *uts)
1162{
1163 const char *compat = "sandbox-other2";
1164 oftree otree = get_other_oftree(uts);
1165 ofnode node;
1166 int count;
1167
1168 count = 0;
1169 for (node = oftree_root(otree);
1170 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1171 count++;
1172 ut_asserteq(2, count);
1173
1174 return 0;
1175}
Simon Glassdb6d8722023-09-26 08:14:38 -06001176DM_TEST(dm_test_ofnode_by_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -06001177
Simon Glasse3be5fc2022-09-06 20:27:18 -06001178static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
1179{
1180 ofnode node, subnode;
1181
1182 node = ofnode_path("/buttons");
1183
1184 subnode = ofnode_find_subnode(node, "btn1");
1185 ut_assert(ofnode_valid(subnode));
1186 ut_asserteq_str("btn1", ofnode_get_name(subnode));
1187
1188 subnode = ofnode_find_subnode(node, "btn");
1189 ut_assert(!ofnode_valid(subnode));
1190
1191 return 0;
1192}
1193DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
1194
Simon Glass75b8a872023-09-26 08:14:39 -06001195/* test ofnode_find_subnode() on the 'other' tree */
Simon Glass9e7a42a2022-09-06 20:27:30 -06001196static int dm_test_ofnode_find_subnode_ot(struct unit_test_state *uts)
1197{
1198 oftree otree = get_other_oftree(uts);
1199 ofnode node, subnode;
1200
1201 node = oftree_path(otree, "/node");
1202
1203 subnode = ofnode_find_subnode(node, "subnode");
1204 ut_assert(ofnode_valid(subnode));
1205 ut_asserteq_str("subnode", ofnode_get_name(subnode));
1206
1207 subnode = ofnode_find_subnode(node, "btn");
1208 ut_assert(!ofnode_valid(subnode));
1209
1210 return 0;
1211}
1212DM_TEST(dm_test_ofnode_find_subnode_ot, UT_TESTF_OTHER_FDT);
1213
Simon Glasse3be5fc2022-09-06 20:27:18 -06001214static int dm_test_ofnode_get_name(struct unit_test_state *uts)
1215{
1216 ofnode node;
1217
1218 node = ofnode_path("/buttons");
1219 ut_assert(ofnode_valid(node));
1220 ut_asserteq_str("buttons", ofnode_get_name(node));
1221 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
1222
1223 return 0;
1224}
1225DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -06001226
1227/* try to access more FDTs than is supported */
1228static int dm_test_ofnode_too_many(struct unit_test_state *uts)
1229{
1230 const int max_trees = CONFIG_IS_ENABLED(OFNODE_MULTI_TREE,
1231 (CONFIG_OFNODE_MULTI_TREE_MAX), (1));
1232 const int fdt_size = 256;
1233 const int num_trees = max_trees + 1;
1234 char fdt[num_trees][fdt_size];
1235 int i;
1236
1237 for (i = 0; i < num_trees; i++) {
1238 oftree tree;
1239 int ret;
1240
1241 ut_assertok(make_ofnode_fdt(uts, fdt[i], fdt_size, i));
1242 ret = get_oftree(uts, fdt[i], &tree);
1243
1244 /*
1245 * With flat tree we have the control FDT using one slot. Live
1246 * tree has no limit since it uses pointers, not integer tree
1247 * IDs
1248 */
1249 if (of_live_active() || i < max_trees - 1) {
1250 ut_assertok(ret);
1251 } else {
1252 /*
1253 * tree should be invalid when we try to register too
1254 * many trees
1255 */
1256 ut_asserteq(-EOVERFLOW, ret);
1257 }
1258 }
1259
1260 return 0;
1261}
1262DM_TEST(dm_test_ofnode_too_many, UT_TESTF_SCAN_FDT);
Simon Glass7a7229a2022-09-06 20:27:33 -06001263
Simon Glass68164892023-09-26 08:14:37 -06001264static int check_copy_props(struct unit_test_state *uts, ofnode dst, ofnode src)
Simon Glass7a7229a2022-09-06 20:27:33 -06001265{
1266 u32 reg[2], val;
1267
Simon Glass68164892023-09-26 08:14:37 -06001268 ut_assertok(ofnode_copy_props(dst, src));
Simon Glass7a7229a2022-09-06 20:27:33 -06001269
1270 ut_assertok(ofnode_read_u32(dst, "ping-expect", &val));
1271 ut_asserteq(3, val);
1272
1273 ut_asserteq_str("denx,u-boot-fdt-test",
1274 ofnode_read_string(dst, "compatible"));
1275
1276 /* check that a property with the same name is overwritten */
1277 ut_assertok(ofnode_read_u32_array(dst, "reg", reg, ARRAY_SIZE(reg)));
1278 ut_asserteq(3, reg[0]);
1279 ut_asserteq(1, reg[1]);
1280
1281 /* reset the compatible so the live tree does not change */
1282 ut_assertok(ofnode_write_string(dst, "compatible", "nothing"));
1283
1284 return 0;
1285}
1286
1287static int dm_test_ofnode_copy_props(struct unit_test_state *uts)
1288{
1289 ofnode src, dst;
1290
1291 /*
1292 * These nodes are chosen so that the src node is before the destination
1293 * node in the tree. This doesn't matter with livetree, but with
1294 * flattree any attempt to insert a property earlier in the tree will
1295 * mess up the offsets after it.
1296 */
1297 src = ofnode_path("/b-test");
1298 dst = ofnode_path("/some-bus");
1299
Simon Glass68164892023-09-26 08:14:37 -06001300 ut_assertok(check_copy_props(uts, dst, src));
Simon Glass7a7229a2022-09-06 20:27:33 -06001301
1302 /* check a property that is in the destination already */
1303 ut_asserteq_str("mux0", ofnode_read_string(dst, "mux-control-names"));
1304
1305 return 0;
1306}
1307DM_TEST(dm_test_ofnode_copy_props, UT_TESTF_SCAN_FDT);
1308
Simon Glass75b8a872023-09-26 08:14:39 -06001309/* test ofnode_copy_props() with the 'other' tree */
Simon Glass7a7229a2022-09-06 20:27:33 -06001310static int dm_test_ofnode_copy_props_ot(struct unit_test_state *uts)
1311{
1312 ofnode src, dst;
1313 oftree otree = get_other_oftree(uts);
1314
1315 src = ofnode_path("/b-test");
1316 dst = oftree_path(otree, "/node/subnode2");
Simon Glass68164892023-09-26 08:14:37 -06001317 ut_assertok(check_copy_props(uts, dst, src));
Simon Glass7a7229a2022-09-06 20:27:33 -06001318
1319 return 0;
1320}
1321DM_TEST(dm_test_ofnode_copy_props_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glasse88e2fe2023-06-01 10:22:40 -06001322
1323/* check that the livetree is aligned to a structure boundary */
1324static int dm_test_livetree_align(struct unit_test_state *uts)
1325{
1326 const int align = __alignof__(struct unit_test_state);
1327 struct device_node *node;
1328 u32 *sentinel;
1329 ulong start;
1330
1331 start = (ulong)gd_of_root();
1332 ut_asserteq(start, ALIGN(start, align));
1333
1334 node = gd_of_root();
1335 sentinel = (void *)node - sizeof(u32);
1336
1337 /*
1338 * The sentinel should be overwritten with the root node. If it isn't,
1339 * then the root node is not at the very start of the livetree memory
1340 * area, and free(root) will fail to free the memory used by the
1341 * livetree.
1342 */
1343 ut_assert(*sentinel != BAD_OF_ROOT);
1344
1345 return 0;
1346}
Simon Glassdb6d8722023-09-26 08:14:38 -06001347DM_TEST(dm_test_livetree_align, UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_TREE);
Simon Glass722281b2023-06-01 10:22:42 -06001348
1349/* check that it is possible to load an arbitrary livetree */
1350static int dm_test_livetree_ensure(struct unit_test_state *uts)
1351{
1352 oftree tree;
1353 ofnode node;
1354
1355 /* read from other.dtb */
1356 ut_assertok(test_load_other_fdt(uts));
1357 tree = oftree_from_fdt(uts->other_fdt);
1358 ut_assert(oftree_valid(tree));
1359 node = oftree_path(tree, "/node/subnode");
1360 ut_assert(ofnode_valid(node));
1361 ut_asserteq_str("sandbox-other2",
1362 ofnode_read_string(node, "compatible"));
1363
1364 return 0;
1365}
Simon Glassdb6d8722023-09-26 08:14:38 -06001366DM_TEST(dm_test_livetree_ensure, UT_TESTF_SCAN_FDT);
Simon Glassa869a1b2023-09-26 08:14:40 -06001367
1368static int dm_test_oftree_new(struct unit_test_state *uts)
1369{
1370 ofnode node, subnode, check;
1371 oftree tree;
1372
1373 ut_assertok(oftree_new(&tree));
1374 node = oftree_root(tree);
1375 ut_assert(ofnode_valid(node));
1376 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
1377 check = ofnode_find_subnode(node, "edmund");
1378 ut_asserteq(check.of_offset, subnode.of_offset);
1379
1380 return 0;
1381}
1382DM_TEST(dm_test_oftree_new, UT_TESTF_SCAN_FDT);
Simon Glass7c33c962023-09-26 08:14:41 -06001383
1384static int check_copy_node(struct unit_test_state *uts, ofnode dst, ofnode src,
1385 ofnode *nodep)
1386{
1387 u32 reg[2], val;
1388 ofnode node;
1389
1390 ut_assertok(ofnode_copy_node(dst, "copy-test", src, &node));
1391
1392 ut_assertok(ofnode_read_u32(node, "ping-expect", &val));
1393 ut_asserteq(3, val);
1394
1395 ut_asserteq_str("denx,u-boot-fdt-test",
1396 ofnode_read_string(node, "compatible"));
1397
1398 /* check that a property with the same name is overwritten */
1399 ut_assertok(ofnode_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg)));
1400 ut_asserteq(3, reg[0]);
1401 ut_asserteq(1, reg[1]);
1402
1403 /* reset the compatible so the live tree does not change */
1404 ut_assertok(ofnode_write_string(node, "compatible", "nothing"));
1405 *nodep = node;
1406
1407 return 0;
1408}
1409
1410static int dm_test_ofnode_copy_node(struct unit_test_state *uts)
1411{
1412 ofnode src, dst, node, try;
1413
1414 /*
1415 * These nodes are chosen so that the src node is before the destination
1416 * node in the tree. This doesn't matter with livetree, but with
1417 * flattree any attempt to insert a property earlier in the tree will
1418 * mess up the offsets after it.
1419 */
1420 src = ofnode_path("/b-test");
1421 dst = ofnode_path("/some-bus");
1422
1423 ut_assertok(check_copy_node(uts, dst, src, &node));
1424
1425 /* check trying to copy over an existing node */
1426 ut_asserteq(-EEXIST, ofnode_copy_node(dst, "copy-test", src, &try));
1427 ut_asserteq(try.of_offset, node.of_offset);
1428
1429 return 0;
1430}
1431DM_TEST(dm_test_ofnode_copy_node, UT_TESTF_SCAN_FDT);
1432
1433/* test ofnode_copy_node() with the 'other' tree */
1434static int dm_test_ofnode_copy_node_ot(struct unit_test_state *uts)
1435{
1436 oftree otree = get_other_oftree(uts);
1437 ofnode src, dst, node;
1438
1439 src = ofnode_path("/b-test");
1440 dst = oftree_path(otree, "/node/subnode2");
1441 ut_assertok(check_copy_node(uts, dst, src, &node));
1442
1443 return 0;
1444}
1445DM_TEST(dm_test_ofnode_copy_node_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass45448772023-09-26 08:14:42 -06001446
1447static int dm_test_ofnode_delete(struct unit_test_state *uts)
1448{
1449 ofnode node;
1450
1451 /*
1452 * At present the livetree is not restored after changes made in tests.
1453 * See test_pre_run() for how this is done with the other FDT and
1454 * dm_test_pre_run() where it sets up the root-tree pointer. So use
1455 * nodes which don't matter to other tests.
1456 *
1457 * We could fix this by detecting livetree changes and regenerating it
1458 * before the next test if needed.
1459 */
1460 node = ofnode_path("/leds/iracibble");
1461 ut_assert(ofnode_valid(node));
1462 ut_assertok(ofnode_delete(&node));
1463 ut_assert(!ofnode_valid(node));
1464 ut_assert(!ofnode_valid(ofnode_path("/leds/iracibble")));
1465
1466 node = ofnode_path("/leds/default_on");
1467 ut_assert(ofnode_valid(node));
1468 ut_assertok(ofnode_delete(&node));
1469 ut_assert(!ofnode_valid(node));
1470 ut_assert(!ofnode_valid(ofnode_path("/leds/default_on")));
1471
1472 ut_asserteq(2, ofnode_get_child_count(ofnode_path("/leds")));
1473
1474 return 0;
1475}
1476DM_TEST(dm_test_ofnode_delete, UT_TESTF_SCAN_FDT);
Simon Glassebbe90b2023-09-26 08:14:43 -06001477
1478static int dm_test_oftree_to_fdt(struct unit_test_state *uts)
1479{
1480 oftree tree, check;
1481 struct abuf buf, buf2;
1482
1483 tree = oftree_default();
1484 ut_assertok(oftree_to_fdt(tree, &buf));
1485 ut_assert(abuf_size(&buf) > SZ_16K);
1486
1487 /* convert it back to a tree and see if it looks OK */
1488 check = oftree_from_fdt(abuf_data(&buf));
1489 ut_assert(oftree_valid(check));
1490
1491 ut_assertok(oftree_to_fdt(check, &buf2));
1492 ut_assert(abuf_size(&buf2) > SZ_16K);
1493 ut_asserteq(abuf_size(&buf), abuf_size(&buf2));
1494 ut_asserteq_mem(abuf_data(&buf), abuf_data(&buf2), abuf_size(&buf));
1495
1496 return 0;
1497}
1498DM_TEST(dm_test_oftree_to_fdt, UT_TESTF_SCAN_FDT);
Simon Glass86ef3992023-09-26 08:14:44 -06001499
1500/* test ofnode_read_bool() and ofnode_write_bool() */
1501static int dm_test_bool(struct unit_test_state *uts)
1502{
1503 const char *propname = "missing-bool-value";
1504 ofnode node;
1505
1506 node = ofnode_path("/a-test");
1507 ut_assert(ofnode_read_bool(node, "bool-value"));
1508 ut_assert(!ofnode_read_bool(node, propname));
1509 ut_assert(!ofnode_has_property(node, propname));
1510
1511 ut_assertok(ofnode_write_bool(node, propname, true));
1512 ut_assert(ofnode_read_bool(node, propname));
1513 ut_assert(ofnode_has_property(node, propname));
1514 ut_assert(ofnode_read_bool(node, "bool-value"));
1515
1516 ut_assertok(ofnode_write_bool(node, propname, false));
1517 ut_assert(!ofnode_read_bool(node, propname));
1518 ut_assert(!ofnode_has_property(node, propname));
1519 ut_assert(ofnode_read_bool(node, "bool-value"));
1520
1521 return 0;
1522}
1523DM_TEST(dm_test_bool, UT_TESTF_SCAN_FDT);