blob: ce996567c3c3a25a0b9baa9537c44d68a9251ea3 [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))
Simon Glass1a92f832024-08-22 07:57:48 -060050 ut_reportf("test needs the UTF_OTHER_FDT flag");
Simon Glassf912a722022-09-06 20:27:27 -060051
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,
Simon Glass1a92f832024-08-22 07:57:48 -0600106 UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -0600107
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 Glass1a92f832024-08-22 07:57:48 -0600119DM_TEST(dm_test_ofnode_compatible_ot, UTF_SCAN_FDT | UTF_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}
Simon Glass1a92f832024-08-22 07:57:48 -0600137DM_TEST(dm_test_ofnode_get_by_phandle, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Patrick Delaunay04fcfe72020-09-24 17:26:20 +0200138
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,
Simon Glass1a92f832024-08-22 07:57:48 -0600153 UTF_SCAN_FDT | UTF_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 Glass1a92f832024-08-22 07:57:48 -0600192DM_TEST(dm_test_ofnode_by_prop_value, UTF_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,
Simon Glass1a92f832024-08-22 07:57:48 -0600205 UTF_SCAN_FDT | UTF_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 Glass1a92f832024-08-22 07:57:48 -0600221DM_TEST(dm_test_ofnode_fmap, UTF_SCAN_PDATA | UTF_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 Glass1a92f832024-08-22 07:57:48 -0600251DM_TEST(dm_test_ofnode_read, UTF_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 Glass1a92f832024-08-22 07:57:48 -0600274DM_TEST(dm_test_ofnode_read_ot, UTF_SCAN_FDT | UTF_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}
Simon Glass1a92f832024-08-22 07:57:48 -0600350DM_TEST(dm_test_ofnode_phandle, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Patrick Delaunay8cd28012020-09-25 09:41:16 +0200351
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}
Simon Glass1a92f832024-08-22 07:57:48 -0600379DM_TEST(dm_test_ofnode_phandle_ot, UTF_OTHER_FDT);
Simon Glassf912a722022-09-06 20:27:27 -0600380
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 Glass1a92f832024-08-22 07:57:48 -0600409DM_TEST(dm_test_ofnode_read_chosen, UTF_SCAN_PDATA | UTF_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}
Simon Glass1a92f832024-08-22 07:57:48 -0600432DM_TEST(dm_test_ofnode_read_aliases, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Michal Simek92a88622020-07-28 12:51:08 +0200433
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 Glass1a92f832024-08-22 07:57:48 -0600453 UTF_SCAN_PDATA | UTF_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,
Simon Glass1a92f832024-08-22 07:57:48 -0600476 UTF_SCAN_FDT | UTF_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}
Simon Glass1a92f832024-08-22 07:57:48 -0600488DM_TEST(dm_test_ofnode_is_enabled, UTF_SCAN_PDATA | UTF_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}
Simon Glass1a92f832024-08-22 07:57:48 -0600502DM_TEST(dm_test_ofnode_is_enabled_ot, UTF_OTHER_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -0600503
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}
Simon Glass1a92f832024-08-22 07:57:48 -0600539DM_TEST(dm_test_ofnode_get_reg, UTF_SCAN_PDATA | UTF_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 Glass1a92f832024-08-22 07:57:48 -0600553DM_TEST(dm_test_ofnode_get_reg_ot, UTF_SCAN_FDT | UTF_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}
Simon Glass1a92f832024-08-22 07:57:48 -0600574DM_TEST(dm_test_ofnode_get_path, UTF_SCAN_PDATA | UTF_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 Glass1a92f832024-08-22 07:57:48 -0600594DM_TEST(dm_test_ofnode_get_path_ot, UTF_SCAN_FDT | UTF_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 Glass1a92f832024-08-22 07:57:48 -0600610DM_TEST(dm_test_ofnode_conf, UTF_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
Christian Marangicdc38152024-10-01 14:24:44 +0200617 ut_assert(!ofnode_options_read_bool("missing"));
618 ut_assert(ofnode_options_read_bool("testing-bool"));
619
620 ut_asserteq(123, ofnode_options_read_int("testing-int", 0));
621 ut_asserteq(6, ofnode_options_read_int("missing", 6));
622
623 ut_assertnull(ofnode_options_read_str("missing"));
624 ut_asserteq_str("testing", ofnode_options_read_str("testing-str"));
625
Michal Simek43c42bd2023-08-31 08:59:05 +0200626 ut_assertok(ofnode_read_bootscript_address(&bootscr_address,
627 &bootscr_offset));
628 ut_asserteq_64(0, bootscr_address);
629 ut_asserteq_64(0x12345678, bootscr_offset);
630
Michal Simek6a7c1ce2023-08-31 09:04:27 +0200631 ut_assertok(ofnode_read_bootscript_flash(&bootscr_flash_offset,
632 &bootscr_flash_size));
633 ut_asserteq_64(0, bootscr_flash_offset);
634 ut_asserteq_64(0x2000, bootscr_flash_size);
635
Michal Simek43c42bd2023-08-31 08:59:05 +0200636 return 0;
637}
638DM_TEST(dm_test_ofnode_options, 0);
639
Michael Wallef1f87402021-10-15 15:15:18 +0200640static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
641{
642 const char compatible[] = "denx,u-boot-fdt-test";
643 bool found = false;
644 ofnode node;
645
646 ofnode_for_each_compatible_node(node, compatible) {
647 ut_assert(ofnode_device_is_compatible(node, compatible));
648 found = true;
649 }
650
651 /* There should be at least one matching node */
652 ut_assert(found);
653
654 return 0;
655}
Simon Glass1a92f832024-08-22 07:57:48 -0600656DM_TEST(dm_test_ofnode_for_each_compatible_node, UTF_SCAN_FDT);
Simon Glass73025392021-10-23 17:26:04 -0600657
Simon Glass75b8a872023-09-26 08:14:39 -0600658/* test dm_test_ofnode_string_count/index/list() */
Simon Glass73025392021-10-23 17:26:04 -0600659static int dm_test_ofnode_string(struct unit_test_state *uts)
660{
Simon Glass9580bfc2021-10-23 17:26:07 -0600661 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600662 const char *out;
663 ofnode node;
664
665 node = ofnode_path("/a-test");
666 ut_assert(ofnode_valid(node));
667
668 /* single string */
669 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
670 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
671 ut_asserteq_str("test string", out);
672 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
673 "test string"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600674 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
675 ut_asserteq_str("test string", val[0]);
676 ut_assertnull(val[1]);
677 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600678
679 /* list of strings */
680 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
681 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
682 &out));
683 ut_asserteq_str("mux0", out);
684 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
685 "mux0"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600686 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
687 &val));
688 ut_asserteq_str("mux0", val[0]);
689 ut_asserteq_str("mux1", val[1]);
690 ut_asserteq_str("mux2", val[2]);
691 ut_asserteq_str("mux3", val[3]);
692 ut_asserteq_str("mux4", val[4]);
693 ut_assertnull(val[5]);
694 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600695
696 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
697 &out));
698 ut_asserteq_str("mux4", out);
699 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
700 "mux4"));
701
702 return 0;
703}
Simon Glass1a92f832024-08-22 07:57:48 -0600704DM_TEST(dm_test_ofnode_string, UTF_SCAN_FDT);
Simon Glass73025392021-10-23 17:26:04 -0600705
Simon Glass75b8a872023-09-26 08:14:39 -0600706/* test error returns from ofnode_read_string_count/index/list() */
Simon Glass73025392021-10-23 17:26:04 -0600707static int dm_test_ofnode_string_err(struct unit_test_state *uts)
708{
Simon Glass9580bfc2021-10-23 17:26:07 -0600709 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600710 const char *out;
711 ofnode node;
712
713 /*
714 * Test error codes only on livetree, as they are different with
715 * flattree
716 */
717 node = ofnode_path("/a-test");
718 ut_assert(ofnode_valid(node));
719
720 /* non-existent property */
721 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
722 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
723 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600724 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glass73025392021-10-23 17:26:04 -0600725
726 /* empty property */
727 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
728 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
729 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600730 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
731 &val));
Simon Glass73025392021-10-23 17:26:04 -0600732
733 /* badly formatted string list */
734 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
735 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
736 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600737 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
738 &val));
Simon Glass73025392021-10-23 17:26:04 -0600739
740 /* out of range / not found */
741 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
742 &out));
743 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
744 "other"));
745
746 /* negative value for index is not allowed, so don't test for that */
747
748 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
749 "mux-control-names", 5,
750 &out));
751
752 return 0;
753}
Simon Glass1a92f832024-08-22 07:57:48 -0600754DM_TEST(dm_test_ofnode_string_err, UTF_LIVE_TREE);
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200755
Simon Glass75b8a872023-09-26 08:14:39 -0600756static int dm_test_ofnode_read_phy_mode(struct unit_test_state *uts)
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200757{
758 ofnode eth_node, phy_node;
Marek Behúnbc194772022-04-07 00:33:01 +0200759 phy_interface_t mode;
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200760 u32 reg;
761
762 eth_node = ofnode_path("/phy-test-eth");
763 ut_assert(ofnode_valid(eth_node));
764
Marek Behúnbc194772022-04-07 00:33:01 +0200765 mode = ofnode_read_phy_mode(eth_node);
766 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
767
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200768 phy_node = ofnode_get_phy_node(eth_node);
769 ut_assert(ofnode_valid(phy_node));
770
771 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
772 ut_asserteq_64(0x1, reg);
773
774 return 0;
775}
Simon Glass1a92f832024-08-22 07:57:48 -0600776DM_TEST(dm_test_ofnode_read_phy_mode, UTF_SCAN_FDT);
Simon Glassef75c592022-07-30 15:52:08 -0600777
778/**
779 * make_ofnode_fdt() - Create an FDT for testing with ofnode
780 *
781 * The size is set to the minimum needed
782 *
783 * @uts: Test state
784 * @fdt: Place to write FDT
785 * @size: Maximum size of space for fdt
Simon Glass9e7a42a2022-09-06 20:27:30 -0600786 * @id: id value to add to the tree ('id' property in root node)
Simon Glassef75c592022-07-30 15:52:08 -0600787 */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600788static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size,
789 int id)
Simon Glassef75c592022-07-30 15:52:08 -0600790{
791 ut_assertok(fdt_create(fdt, size));
792 ut_assertok(fdt_finish_reservemap(fdt));
793 ut_assert(fdt_begin_node(fdt, "") >= 0);
794
Simon Glass9e7a42a2022-09-06 20:27:30 -0600795 ut_assertok(fdt_property_u32(fdt, "id", id));
796
Simon Glassef75c592022-07-30 15:52:08 -0600797 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
798 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
799 ut_assertok(fdt_end_node(fdt));
800
801 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
802 ut_assertok(fdt_end_node(fdt));
803
804 ut_assertok(fdt_end_node(fdt));
805 ut_assertok(fdt_finish(fdt));
806
807 return 0;
808}
809
Simon Glass75b8a872023-09-26 08:14:39 -0600810/* Check that aliases work on the control FDT */
811static int dm_test_ofnode_aliases(struct unit_test_state *uts)
Simon Glassef75c592022-07-30 15:52:08 -0600812{
Simon Glassef75c592022-07-30 15:52:08 -0600813 ofnode node;
814
Simon Glassef75c592022-07-30 15:52:08 -0600815 node = ofnode_get_aliases_node("ethernet3");
816 ut_assert(ofnode_valid(node));
817 ut_asserteq_str("sbe5", ofnode_get_name(node));
818
Simon Glass2b9b14582022-09-06 20:27:21 -0600819 ut_assert(!oftree_valid(oftree_null()));
820
Simon Glassc7599442022-10-20 18:22:49 -0600821 return 0;
822}
Simon Glass1a92f832024-08-22 07:57:48 -0600823DM_TEST(dm_test_ofnode_aliases, UTF_SCAN_FDT);
Simon Glassdad97512022-09-06 20:27:29 -0600824
Simon Glass75b8a872023-09-26 08:14:39 -0600825/**
826 * dm_test_ofnode_root_mult() - Check aliaes on control and 'other' tree
827 *
828 * Check that aliases work only with the control FDT, not with 'other' tree.
829 * This is not actually the desired behaviour. If aliases are implemented for
830 * any tree, then this test should be changed.
831 */
Simon Glassc7599442022-10-20 18:22:49 -0600832static int dm_test_ofnode_root_mult(struct unit_test_state *uts)
833{
834 char fdt[256];
835 oftree tree;
836 ofnode node;
Simon Glassdad97512022-09-06 20:27:29 -0600837
Simon Glassc7599442022-10-20 18:22:49 -0600838 /* skip this test if multiple FDTs are not supported */
839 if (!IS_ENABLED(CONFIG_OFNODE_MULTI_TREE))
840 return -EAGAIN;
841
842 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt), 0));
843 ut_assertok(get_oftree(uts, fdt, &tree));
Simon Glass2b9b14582022-09-06 20:27:21 -0600844 ut_assert(oftree_valid(tree));
Simon Glassef75c592022-07-30 15:52:08 -0600845
846 /* Make sure they don't work on this new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600847 node = oftree_path(tree, "mmc0");
Simon Glassef75c592022-07-30 15:52:08 -0600848 ut_assert(!ofnode_valid(node));
849
850 /* It should appear in the new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600851 node = oftree_path(tree, "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600852 ut_assert(ofnode_valid(node));
853
854 /* ...and not in the control FDT */
Simon Glass45ae59d2022-09-06 20:27:24 -0600855 node = oftree_path(oftree_default(), "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600856 ut_assert(!ofnode_valid(node));
857
Simon Glassdad97512022-09-06 20:27:29 -0600858 free_oftree(tree);
Simon Glassef75c592022-07-30 15:52:08 -0600859
860 return 0;
861}
Simon Glass1a92f832024-08-22 07:57:48 -0600862DM_TEST(dm_test_ofnode_root_mult, UTF_SCAN_FDT);
Simon Glass270a4432022-07-30 15:52:09 -0600863
Simon Glass75b8a872023-09-26 08:14:39 -0600864/* test ofnode_set_enabled(), ofnode_write_prop() on a livetree */
Simon Glass270a4432022-07-30 15:52:09 -0600865static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
866{
867 struct udevice *dev;
868 ofnode node;
869
Simon Glass270a4432022-07-30 15:52:09 -0600870 /* Test enabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600871 node = ofnode_path("/usb@2");
872
Simon Glass3ee3d152022-07-30 15:52:13 -0600873 ut_assert(!ofnode_is_enabled(node));
874 ut_assertok(ofnode_set_enabled(node, true));
875 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600876
877 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
878 &dev);
879 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
880
881 /* Test string property setting */
Simon Glass270a4432022-07-30 15:52:09 -0600882 ut_assert(device_is_compatible(dev, "sandbox,usb"));
883 ofnode_write_string(node, "compatible", "gdsys,super-usb");
884 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
885 ofnode_write_string(node, "compatible", "sandbox,usb");
886 ut_assert(device_is_compatible(dev, "sandbox,usb"));
887
888 /* Test setting generic properties */
889
890 /* Non-existent in DTB */
891 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
892 /* reg = 0x42, size = 0x100 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -0600893 ut_assertok(ofnode_write_prop(node, "reg",
Simon Glass17abed02022-09-06 20:27:32 -0600894 "\x00\x00\x00\x42\x00\x00\x01\x00", 8,
895 false));
Simon Glass270a4432022-07-30 15:52:09 -0600896 ut_asserteq(0x42, dev_read_addr(dev));
897
898 /* Test disabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600899 device_remove(dev, DM_REMOVE_NORMAL);
900 device_unbind(dev);
901
Simon Glass3ee3d152022-07-30 15:52:13 -0600902 ut_assert(ofnode_is_enabled(node));
903 ut_assertok(ofnode_set_enabled(node, false));
904 ut_assert(!ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600905
906 return 0;
907}
Simon Glassb75dc162022-07-30 15:52:11 -0600908DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass1a92f832024-08-22 07:57:48 -0600909 UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassd28e31e2022-07-30 15:52:14 -0600910
Simon Glass17abed02022-09-06 20:27:32 -0600911static int check_write_prop(struct unit_test_state *uts, ofnode node)
912{
913 char prop[] = "middle-name";
914 char name[10];
915 int len;
916
917 strcpy(name, "cecil");
918 len = strlen(name) + 1;
919 ut_assertok(ofnode_write_prop(node, prop, name, len, false));
920 ut_asserteq_str(name, ofnode_read_string(node, prop));
921
922 /* change the underlying value, this should mess up the live tree */
923 strcpy(name, "tony");
924 if (of_live_active()) {
925 ut_asserteq_str(name, ofnode_read_string(node, prop));
926 } else {
927 ut_asserteq_str("cecil", ofnode_read_string(node, prop));
928 }
929
930 /* try again, this time copying the property */
931 strcpy(name, "mary");
932 ut_assertok(ofnode_write_prop(node, prop, name, len, true));
933 ut_asserteq_str(name, ofnode_read_string(node, prop));
934 strcpy(name, "leah");
935
936 /* both flattree and livetree behave the same */
937 ut_asserteq_str("mary", ofnode_read_string(node, prop));
938
939 return 0;
940}
941
942/* writing the tree with and without copying the property */
943static int dm_test_ofnode_write_copy(struct unit_test_state *uts)
944{
945 ofnode node;
946
947 node = ofnode_path("/a-test");
948 ut_assertok(check_write_prop(uts, node));
949
950 return 0;
951}
Simon Glass1a92f832024-08-22 07:57:48 -0600952DM_TEST(dm_test_ofnode_write_copy, UTF_SCAN_FDT);
Simon Glass17abed02022-09-06 20:27:32 -0600953
Simon Glass75b8a872023-09-26 08:14:39 -0600954/* test writing a property to the 'other' tree */
Simon Glass17abed02022-09-06 20:27:32 -0600955static int dm_test_ofnode_write_copy_ot(struct unit_test_state *uts)
956{
957 oftree otree = get_other_oftree(uts);
958 ofnode node, check_node;
959
960 node = oftree_path(otree, "/node");
961 ut_assertok(check_write_prop(uts, node));
962
963 /* make sure the control FDT is not touched */
964 check_node = ofnode_path("/node");
965 ut_assertnull(ofnode_read_string(check_node, "middle-name"));
966
967 return 0;
968}
Simon Glass1a92f832024-08-22 07:57:48 -0600969DM_TEST(dm_test_ofnode_write_copy_ot, UTF_SCAN_FDT | UTF_OTHER_FDT);
Simon Glass17abed02022-09-06 20:27:32 -0600970
Simon Glass75b8a872023-09-26 08:14:39 -0600971/* test ofnode_read_u32_index/default() */
Simon Glassd28e31e2022-07-30 15:52:14 -0600972static int dm_test_ofnode_u32(struct unit_test_state *uts)
973{
974 ofnode node;
Simon Glasse3be5fc2022-09-06 20:27:18 -0600975 u32 val;
Simon Glassd28e31e2022-07-30 15:52:14 -0600976
977 node = ofnode_path("/lcd");
978 ut_assert(ofnode_valid(node));
979 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
980 ut_assertok(ofnode_write_u32(node, "xres", 1367));
981 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
982 ut_assertok(ofnode_write_u32(node, "xres", 1366));
983
Simon Glasse3be5fc2022-09-06 20:27:18 -0600984 node = ofnode_path("/backlight");
985 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
986 ut_asserteq(0, val);
987 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
988 ut_asserteq(16, val);
989 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
990 ut_asserteq(255, val);
991 ut_asserteq(-EOVERFLOW,
992 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
993 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
994
Simon Glassd28e31e2022-07-30 15:52:14 -0600995 return 0;
996}
Simon Glass1a92f832024-08-22 07:57:48 -0600997DM_TEST(dm_test_ofnode_u32, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass56bc3322022-09-06 20:27:02 -0600998
Simon Glass75b8a872023-09-26 08:14:39 -0600999/* test ofnode_read_u32_array() */
Simon Glasse3be5fc2022-09-06 20:27:18 -06001000static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
1001{
1002 ofnode node;
1003 u32 val[10];
1004
1005 node = ofnode_path("/a-test");
1006 ut_assert(ofnode_valid(node));
1007 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
1008 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
1009 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
1010 1));
1011
1012 memset(val, '\0', sizeof(val));
1013 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
1014 ut_asserteq(0, val[0]);
1015 ut_asserteq(5678, val[1]);
1016 ut_asserteq(9123, val[2]);
1017 ut_asserteq(4567, val[3]);
1018 ut_asserteq(0, val[4]);
1019 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
1020 4));
1021
1022 return 0;
1023}
Simon Glass1a92f832024-08-22 07:57:48 -06001024DM_TEST(dm_test_ofnode_u32_array, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -06001025
Simon Glassc681e092023-09-26 08:14:45 -06001026/* test ofnode_read_u64() and ofnode_write_u64() */
1027static int dm_test_ofnode_u64(struct unit_test_state *uts)
Simon Glasse3be5fc2022-09-06 20:27:18 -06001028{
1029 ofnode node;
1030 u64 val;
1031
1032 node = ofnode_path("/a-test");
1033 ut_assert(ofnode_valid(node));
1034 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
1035 ut_asserteq_64(0x1111222233334444, val);
Simon Glassc681e092023-09-26 08:14:45 -06001036 ut_assertok(ofnode_write_u64(node, "new-int64-value", 0x9876543210));
1037 ut_assertok(ofnode_read_u64(node, "new-int64-value", &val));
1038 ut_asserteq_64(0x9876543210, val);
1039
Simon Glasse3be5fc2022-09-06 20:27:18 -06001040 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
1041
Michal Simek08a194e2023-08-25 11:37:46 +02001042 ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val));
1043 ut_asserteq_64(0x1111222233334444, val);
1044 ut_assertok(ofnode_read_u64_index(node, "int64-array", 1, &val));
1045 ut_asserteq_64(0x4444333322221111, val);
1046 ut_asserteq(-EOVERFLOW,
1047 ofnode_read_u64_index(node, "int64-array", 2, &val));
1048 ut_asserteq(-EINVAL, ofnode_read_u64_index(node, "missing", 0, &val));
1049
Simon Glassc681e092023-09-26 08:14:45 -06001050 ut_assertok(ofnode_write_u64(node, "int64-array", 0x9876543210));
1051 ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val));
1052 ut_asserteq_64(0x9876543210, val);
1053 ut_asserteq(-EOVERFLOW,
1054 ofnode_read_u64_index(node, "int64-array", 1, &val));
1055
Simon Glasse3be5fc2022-09-06 20:27:18 -06001056 return 0;
1057}
Simon Glass1a92f832024-08-22 07:57:48 -06001058DM_TEST(dm_test_ofnode_u64, UTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -06001059
Simon Glass56bc3322022-09-06 20:27:02 -06001060static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
1061{
1062 ofnode node, check, subnode;
1063 char buf[128];
1064
Simon Glass56bc3322022-09-06 20:27:02 -06001065 node = ofnode_path("/lcd");
1066 ut_assert(ofnode_valid(node));
1067 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
1068 check = ofnode_path("/lcd/edmund");
1069 ut_asserteq(subnode.of_offset, check.of_offset);
1070 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1071 ut_asserteq_str("/lcd/edmund", buf);
1072
1073 if (of_live_active()) {
1074 struct device_node *child;
1075
1076 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
1077 2, &child));
1078 ut_asserteq_str("ed", child->name);
1079 ut_asserteq_str("/lcd/ed", child->full_name);
1080 check = ofnode_path("/lcd/ed");
1081 ut_asserteq_ptr(child, check.np);
1082 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
1083 sizeof(buf)));
1084 ut_asserteq_str("/lcd/ed", buf);
1085 }
1086
1087 /* An existing node should be returned with -EEXIST */
1088 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
1089 ut_asserteq(subnode.of_offset, check.of_offset);
1090
1091 /* add a root node */
1092 node = ofnode_path("/");
1093 ut_assert(ofnode_valid(node));
1094 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
1095 check = ofnode_path("/lcd2");
1096 ut_asserteq(subnode.of_offset, check.of_offset);
1097 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1098 ut_asserteq_str("/lcd2", buf);
1099
1100 if (of_live_active()) {
1101 ulong start;
1102 int i;
1103
1104 /*
1105 * Make sure each of the three malloc()checks in
1106 * of_add_subnode() work
1107 */
1108 for (i = 0; i < 3; i++) {
1109 malloc_enable_testing(i);
1110 start = ut_check_free();
1111 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
1112 &check));
1113 ut_assertok(ut_check_delta(start));
1114 }
1115
1116 /* This should pass since we allow 3 allocations */
1117 malloc_enable_testing(3);
1118 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
1119 malloc_disable_testing();
1120 }
1121
Simon Glass238afb52022-09-06 20:27:03 -06001122 /* write to the empty node */
1123 ut_assertok(ofnode_write_string(subnode, "example", "text"));
1124
Simon Glass56bc3322022-09-06 20:27:02 -06001125 return 0;
1126}
Simon Glass1a92f832024-08-22 07:57:48 -06001127DM_TEST(dm_test_ofnode_add_subnode, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass4caa79a2022-09-06 20:27:16 -06001128
1129static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
1130{
1131 ofnode node, subnode;
1132 struct ofprop prop;
1133 int count;
1134
Dzmitry Sankouski54f4c832023-01-22 18:21:23 +03001135 node = ofnode_path("/ofnode-foreach");
Simon Glass4caa79a2022-09-06 20:27:16 -06001136 count = 0;
1137
1138 /* we expect "compatible" for each node */
1139 ofnode_for_each_prop(prop, node)
1140 count++;
1141 ut_asserteq(1, count);
1142
1143 /* there are two nodes, each with 2 properties */
1144 ofnode_for_each_subnode(subnode, node)
1145 ofnode_for_each_prop(prop, subnode)
1146 count++;
1147 ut_asserteq(5, count);
1148
1149 return 0;
1150}
Simon Glass1a92f832024-08-22 07:57:48 -06001151DM_TEST(dm_test_ofnode_for_each_prop, UTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -06001152
1153static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
1154{
1155 const char *compat = "denx,u-boot-fdt-test";
1156 ofnode node;
1157 int count;
1158
1159 count = 0;
1160 for (node = ofnode_null();
1161 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1162 count++;
1163 ut_asserteq(11, count);
1164
1165 return 0;
1166}
Simon Glass1a92f832024-08-22 07:57:48 -06001167DM_TEST(dm_test_ofnode_by_compatible, UTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -06001168
Simon Glass75b8a872023-09-26 08:14:39 -06001169/* check ofnode_by_compatible() on the 'other' tree */
Simon Glass9e7a42a2022-09-06 20:27:30 -06001170static int dm_test_ofnode_by_compatible_ot(struct unit_test_state *uts)
1171{
1172 const char *compat = "sandbox-other2";
1173 oftree otree = get_other_oftree(uts);
1174 ofnode node;
1175 int count;
1176
1177 count = 0;
1178 for (node = oftree_root(otree);
1179 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1180 count++;
1181 ut_asserteq(2, count);
1182
1183 return 0;
1184}
Simon Glass1a92f832024-08-22 07:57:48 -06001185DM_TEST(dm_test_ofnode_by_compatible_ot, UTF_SCAN_FDT | UTF_OTHER_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -06001186
Simon Glasse3be5fc2022-09-06 20:27:18 -06001187static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
1188{
1189 ofnode node, subnode;
1190
1191 node = ofnode_path("/buttons");
1192
1193 subnode = ofnode_find_subnode(node, "btn1");
1194 ut_assert(ofnode_valid(subnode));
1195 ut_asserteq_str("btn1", ofnode_get_name(subnode));
1196
1197 subnode = ofnode_find_subnode(node, "btn");
1198 ut_assert(!ofnode_valid(subnode));
1199
1200 return 0;
1201}
Simon Glass1a92f832024-08-22 07:57:48 -06001202DM_TEST(dm_test_ofnode_find_subnode, UTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -06001203
Simon Glass75b8a872023-09-26 08:14:39 -06001204/* test ofnode_find_subnode() on the 'other' tree */
Simon Glass9e7a42a2022-09-06 20:27:30 -06001205static int dm_test_ofnode_find_subnode_ot(struct unit_test_state *uts)
1206{
1207 oftree otree = get_other_oftree(uts);
1208 ofnode node, subnode;
1209
1210 node = oftree_path(otree, "/node");
1211
1212 subnode = ofnode_find_subnode(node, "subnode");
1213 ut_assert(ofnode_valid(subnode));
1214 ut_asserteq_str("subnode", ofnode_get_name(subnode));
1215
1216 subnode = ofnode_find_subnode(node, "btn");
1217 ut_assert(!ofnode_valid(subnode));
1218
1219 return 0;
1220}
Simon Glass1a92f832024-08-22 07:57:48 -06001221DM_TEST(dm_test_ofnode_find_subnode_ot, UTF_OTHER_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -06001222
Simon Glasse3be5fc2022-09-06 20:27:18 -06001223static int dm_test_ofnode_get_name(struct unit_test_state *uts)
1224{
1225 ofnode node;
1226
1227 node = ofnode_path("/buttons");
1228 ut_assert(ofnode_valid(node));
1229 ut_asserteq_str("buttons", ofnode_get_name(node));
1230 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
1231
1232 return 0;
1233}
Simon Glass1a92f832024-08-22 07:57:48 -06001234DM_TEST(dm_test_ofnode_get_name, UTF_SCAN_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -06001235
1236/* try to access more FDTs than is supported */
1237static int dm_test_ofnode_too_many(struct unit_test_state *uts)
1238{
1239 const int max_trees = CONFIG_IS_ENABLED(OFNODE_MULTI_TREE,
1240 (CONFIG_OFNODE_MULTI_TREE_MAX), (1));
1241 const int fdt_size = 256;
1242 const int num_trees = max_trees + 1;
1243 char fdt[num_trees][fdt_size];
1244 int i;
1245
1246 for (i = 0; i < num_trees; i++) {
1247 oftree tree;
1248 int ret;
1249
1250 ut_assertok(make_ofnode_fdt(uts, fdt[i], fdt_size, i));
1251 ret = get_oftree(uts, fdt[i], &tree);
1252
1253 /*
1254 * With flat tree we have the control FDT using one slot. Live
1255 * tree has no limit since it uses pointers, not integer tree
1256 * IDs
1257 */
1258 if (of_live_active() || i < max_trees - 1) {
1259 ut_assertok(ret);
1260 } else {
1261 /*
1262 * tree should be invalid when we try to register too
1263 * many trees
1264 */
1265 ut_asserteq(-EOVERFLOW, ret);
1266 }
1267 }
1268
1269 return 0;
1270}
Simon Glass1a92f832024-08-22 07:57:48 -06001271DM_TEST(dm_test_ofnode_too_many, UTF_SCAN_FDT);
Simon Glass7a7229a2022-09-06 20:27:33 -06001272
Simon Glass68164892023-09-26 08:14:37 -06001273static int check_copy_props(struct unit_test_state *uts, ofnode dst, ofnode src)
Simon Glass7a7229a2022-09-06 20:27:33 -06001274{
1275 u32 reg[2], val;
1276
Simon Glass68164892023-09-26 08:14:37 -06001277 ut_assertok(ofnode_copy_props(dst, src));
Simon Glass7a7229a2022-09-06 20:27:33 -06001278
1279 ut_assertok(ofnode_read_u32(dst, "ping-expect", &val));
1280 ut_asserteq(3, val);
1281
1282 ut_asserteq_str("denx,u-boot-fdt-test",
1283 ofnode_read_string(dst, "compatible"));
1284
1285 /* check that a property with the same name is overwritten */
1286 ut_assertok(ofnode_read_u32_array(dst, "reg", reg, ARRAY_SIZE(reg)));
1287 ut_asserteq(3, reg[0]);
1288 ut_asserteq(1, reg[1]);
1289
1290 /* reset the compatible so the live tree does not change */
1291 ut_assertok(ofnode_write_string(dst, "compatible", "nothing"));
1292
1293 return 0;
1294}
1295
1296static int dm_test_ofnode_copy_props(struct unit_test_state *uts)
1297{
1298 ofnode src, dst;
1299
1300 /*
1301 * These nodes are chosen so that the src node is before the destination
1302 * node in the tree. This doesn't matter with livetree, but with
1303 * flattree any attempt to insert a property earlier in the tree will
1304 * mess up the offsets after it.
1305 */
1306 src = ofnode_path("/b-test");
1307 dst = ofnode_path("/some-bus");
1308
Simon Glass68164892023-09-26 08:14:37 -06001309 ut_assertok(check_copy_props(uts, dst, src));
Simon Glass7a7229a2022-09-06 20:27:33 -06001310
1311 /* check a property that is in the destination already */
1312 ut_asserteq_str("mux0", ofnode_read_string(dst, "mux-control-names"));
1313
1314 return 0;
1315}
Simon Glass1a92f832024-08-22 07:57:48 -06001316DM_TEST(dm_test_ofnode_copy_props, UTF_SCAN_FDT);
Simon Glass7a7229a2022-09-06 20:27:33 -06001317
Simon Glass75b8a872023-09-26 08:14:39 -06001318/* test ofnode_copy_props() with the 'other' tree */
Simon Glass7a7229a2022-09-06 20:27:33 -06001319static int dm_test_ofnode_copy_props_ot(struct unit_test_state *uts)
1320{
1321 ofnode src, dst;
1322 oftree otree = get_other_oftree(uts);
1323
1324 src = ofnode_path("/b-test");
1325 dst = oftree_path(otree, "/node/subnode2");
Simon Glass68164892023-09-26 08:14:37 -06001326 ut_assertok(check_copy_props(uts, dst, src));
Simon Glass7a7229a2022-09-06 20:27:33 -06001327
1328 return 0;
1329}
Simon Glass1a92f832024-08-22 07:57:48 -06001330DM_TEST(dm_test_ofnode_copy_props_ot, UTF_SCAN_FDT | UTF_OTHER_FDT);
Simon Glasse88e2fe2023-06-01 10:22:40 -06001331
1332/* check that the livetree is aligned to a structure boundary */
1333static int dm_test_livetree_align(struct unit_test_state *uts)
1334{
1335 const int align = __alignof__(struct unit_test_state);
1336 struct device_node *node;
1337 u32 *sentinel;
1338 ulong start;
1339
1340 start = (ulong)gd_of_root();
1341 ut_asserteq(start, ALIGN(start, align));
1342
1343 node = gd_of_root();
1344 sentinel = (void *)node - sizeof(u32);
1345
1346 /*
1347 * The sentinel should be overwritten with the root node. If it isn't,
1348 * then the root node is not at the very start of the livetree memory
1349 * area, and free(root) will fail to free the memory used by the
1350 * livetree.
1351 */
1352 ut_assert(*sentinel != BAD_OF_ROOT);
1353
1354 return 0;
1355}
Simon Glass1a92f832024-08-22 07:57:48 -06001356DM_TEST(dm_test_livetree_align, UTF_SCAN_FDT | UTF_LIVE_TREE);
Simon Glass722281b2023-06-01 10:22:42 -06001357
1358/* check that it is possible to load an arbitrary livetree */
1359static int dm_test_livetree_ensure(struct unit_test_state *uts)
1360{
1361 oftree tree;
1362 ofnode node;
1363
1364 /* read from other.dtb */
1365 ut_assertok(test_load_other_fdt(uts));
1366 tree = oftree_from_fdt(uts->other_fdt);
1367 ut_assert(oftree_valid(tree));
1368 node = oftree_path(tree, "/node/subnode");
1369 ut_assert(ofnode_valid(node));
1370 ut_asserteq_str("sandbox-other2",
1371 ofnode_read_string(node, "compatible"));
1372
1373 return 0;
1374}
Simon Glass1a92f832024-08-22 07:57:48 -06001375DM_TEST(dm_test_livetree_ensure, UTF_SCAN_FDT);
Simon Glassa869a1b2023-09-26 08:14:40 -06001376
1377static int dm_test_oftree_new(struct unit_test_state *uts)
1378{
1379 ofnode node, subnode, check;
1380 oftree tree;
1381
1382 ut_assertok(oftree_new(&tree));
1383 node = oftree_root(tree);
1384 ut_assert(ofnode_valid(node));
1385 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
1386 check = ofnode_find_subnode(node, "edmund");
1387 ut_asserteq(check.of_offset, subnode.of_offset);
1388
1389 return 0;
1390}
Simon Glass1a92f832024-08-22 07:57:48 -06001391DM_TEST(dm_test_oftree_new, UTF_SCAN_FDT);
Simon Glass7c33c962023-09-26 08:14:41 -06001392
1393static int check_copy_node(struct unit_test_state *uts, ofnode dst, ofnode src,
1394 ofnode *nodep)
1395{
1396 u32 reg[2], val;
1397 ofnode node;
1398
1399 ut_assertok(ofnode_copy_node(dst, "copy-test", src, &node));
1400
1401 ut_assertok(ofnode_read_u32(node, "ping-expect", &val));
1402 ut_asserteq(3, val);
1403
1404 ut_asserteq_str("denx,u-boot-fdt-test",
1405 ofnode_read_string(node, "compatible"));
1406
1407 /* check that a property with the same name is overwritten */
1408 ut_assertok(ofnode_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg)));
1409 ut_asserteq(3, reg[0]);
1410 ut_asserteq(1, reg[1]);
1411
1412 /* reset the compatible so the live tree does not change */
1413 ut_assertok(ofnode_write_string(node, "compatible", "nothing"));
1414 *nodep = node;
1415
1416 return 0;
1417}
1418
1419static int dm_test_ofnode_copy_node(struct unit_test_state *uts)
1420{
1421 ofnode src, dst, node, try;
1422
1423 /*
1424 * These nodes are chosen so that the src node is before the destination
1425 * node in the tree. This doesn't matter with livetree, but with
1426 * flattree any attempt to insert a property earlier in the tree will
1427 * mess up the offsets after it.
1428 */
1429 src = ofnode_path("/b-test");
1430 dst = ofnode_path("/some-bus");
1431
1432 ut_assertok(check_copy_node(uts, dst, src, &node));
1433
1434 /* check trying to copy over an existing node */
1435 ut_asserteq(-EEXIST, ofnode_copy_node(dst, "copy-test", src, &try));
1436 ut_asserteq(try.of_offset, node.of_offset);
1437
1438 return 0;
1439}
Simon Glass1a92f832024-08-22 07:57:48 -06001440DM_TEST(dm_test_ofnode_copy_node, UTF_SCAN_FDT);
Simon Glass7c33c962023-09-26 08:14:41 -06001441
1442/* test ofnode_copy_node() with the 'other' tree */
1443static int dm_test_ofnode_copy_node_ot(struct unit_test_state *uts)
1444{
1445 oftree otree = get_other_oftree(uts);
1446 ofnode src, dst, node;
1447
1448 src = ofnode_path("/b-test");
1449 dst = oftree_path(otree, "/node/subnode2");
1450 ut_assertok(check_copy_node(uts, dst, src, &node));
1451
1452 return 0;
1453}
Simon Glass1a92f832024-08-22 07:57:48 -06001454DM_TEST(dm_test_ofnode_copy_node_ot, UTF_SCAN_FDT | UTF_OTHER_FDT);
Simon Glass45448772023-09-26 08:14:42 -06001455
1456static int dm_test_ofnode_delete(struct unit_test_state *uts)
1457{
1458 ofnode node;
1459
1460 /*
1461 * At present the livetree is not restored after changes made in tests.
1462 * See test_pre_run() for how this is done with the other FDT and
1463 * dm_test_pre_run() where it sets up the root-tree pointer. So use
1464 * nodes which don't matter to other tests.
1465 *
1466 * We could fix this by detecting livetree changes and regenerating it
1467 * before the next test if needed.
1468 */
1469 node = ofnode_path("/leds/iracibble");
1470 ut_assert(ofnode_valid(node));
1471 ut_assertok(ofnode_delete(&node));
1472 ut_assert(!ofnode_valid(node));
1473 ut_assert(!ofnode_valid(ofnode_path("/leds/iracibble")));
1474
1475 node = ofnode_path("/leds/default_on");
1476 ut_assert(ofnode_valid(node));
1477 ut_assertok(ofnode_delete(&node));
1478 ut_assert(!ofnode_valid(node));
1479 ut_assert(!ofnode_valid(ofnode_path("/leds/default_on")));
1480
1481 ut_asserteq(2, ofnode_get_child_count(ofnode_path("/leds")));
1482
1483 return 0;
1484}
Simon Glass1a92f832024-08-22 07:57:48 -06001485DM_TEST(dm_test_ofnode_delete, UTF_SCAN_FDT);
Simon Glassebbe90b2023-09-26 08:14:43 -06001486
1487static int dm_test_oftree_to_fdt(struct unit_test_state *uts)
1488{
1489 oftree tree, check;
1490 struct abuf buf, buf2;
1491
1492 tree = oftree_default();
1493 ut_assertok(oftree_to_fdt(tree, &buf));
1494 ut_assert(abuf_size(&buf) > SZ_16K);
1495
1496 /* convert it back to a tree and see if it looks OK */
1497 check = oftree_from_fdt(abuf_data(&buf));
1498 ut_assert(oftree_valid(check));
1499
1500 ut_assertok(oftree_to_fdt(check, &buf2));
1501 ut_assert(abuf_size(&buf2) > SZ_16K);
1502 ut_asserteq(abuf_size(&buf), abuf_size(&buf2));
1503 ut_asserteq_mem(abuf_data(&buf), abuf_data(&buf2), abuf_size(&buf));
1504
1505 return 0;
1506}
Simon Glass1a92f832024-08-22 07:57:48 -06001507DM_TEST(dm_test_oftree_to_fdt, UTF_SCAN_FDT);
Simon Glass86ef3992023-09-26 08:14:44 -06001508
1509/* test ofnode_read_bool() and ofnode_write_bool() */
1510static int dm_test_bool(struct unit_test_state *uts)
1511{
1512 const char *propname = "missing-bool-value";
1513 ofnode node;
1514
1515 node = ofnode_path("/a-test");
1516 ut_assert(ofnode_read_bool(node, "bool-value"));
1517 ut_assert(!ofnode_read_bool(node, propname));
1518 ut_assert(!ofnode_has_property(node, propname));
1519
1520 ut_assertok(ofnode_write_bool(node, propname, true));
1521 ut_assert(ofnode_read_bool(node, propname));
1522 ut_assert(ofnode_has_property(node, propname));
1523 ut_assert(ofnode_read_bool(node, "bool-value"));
1524
1525 ut_assertok(ofnode_write_bool(node, propname, false));
1526 ut_assert(!ofnode_read_bool(node, propname));
1527 ut_assert(!ofnode_has_property(node, propname));
1528 ut_assert(ofnode_read_bool(node, "bool-value"));
1529
1530 return 0;
1531}
Simon Glass1a92f832024-08-22 07:57:48 -06001532DM_TEST(dm_test_bool, UTF_SCAN_FDT);