blob: ceeb8e57791e7c33b5663a151cd6239d91e587d8 [file] [log] [blame]
Masahiro Yamada04ca8712018-04-27 01:02:02 +09001// SPDX-License-Identifier: GPL-2.0+
Simon Glass5e969252022-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 Yamada04ca8712018-04-27 01:02:02 +090018
19#include <common.h>
Simon Glass62b1db32023-09-26 08:14:43 -060020#include <abuf.h>
Masahiro Yamada04ca8712018-04-27 01:02:02 +090021#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060022#include <log.h>
Simon Glass33104842022-07-30 15:52:08 -060023#include <of_live.h>
Simon Glass65715592022-07-30 15:52:09 -060024#include <dm/device-internal.h>
25#include <dm/lists.h>
Simon Glasse6c5c942018-10-01 12:22:08 -060026#include <dm/of_extra.h>
Simon Glass65715592022-07-30 15:52:09 -060027#include <dm/root.h>
Masahiro Yamada04ca8712018-04-27 01:02:02 +090028#include <dm/test.h>
Simon Glass65715592022-07-30 15:52:09 -060029#include <dm/uclass-internal.h>
Simon Glass62b1db32023-09-26 08:14:43 -060030#include <linux/sizes.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060031#include <test/test.h>
Masahiro Yamada04ca8712018-04-27 01:02:02 +090032#include <test/ut.h>
33
Simon Glass5e969252022-09-06 20:27:27 -060034/**
35 * get_other_oftree() - Convert a flat tree into an oftree object
36 *
37 * @uts: Test state
38 * @return: oftree object for the 'other' FDT (see sandbox' other.dts)
39 */
40oftree get_other_oftree(struct unit_test_state *uts)
41{
42 oftree tree;
43
44 if (of_live_active())
45 tree = oftree_from_np(uts->of_other);
46 else
47 tree = oftree_from_fdt(uts->other_fdt);
48
49 /* An invalid tree may cause failure or crashes */
50 if (!oftree_valid(tree))
51 ut_reportf("test needs the UT_TESTF_OTHER_FDT flag");
52
53 return tree;
54}
55
Simon Glass88a1ae82022-09-06 20:27:29 -060056/**
57 * get_oftree() - Convert a flat tree into an oftree object
58 *
59 * @uts: Test state
60 * @fdt: Pointer to flat tree
61 * @treep: Returns the tree, on success
62 * Return: 0 if OK, 1 if the tree failed to unflatten, -EOVERFLOW if there are
63 * too many flat trees to allow another one to be registers (see
64 * oftree_ensure())
65 */
66int get_oftree(struct unit_test_state *uts, void *fdt, oftree *treep)
67{
68 oftree tree;
69
70 if (of_live_active()) {
71 struct device_node *root;
72
73 ut_assertok(unflatten_device_tree(fdt, &root));
74 tree = oftree_from_np(root);
75 } else {
76 tree = oftree_from_fdt(fdt);
77 if (!oftree_valid(tree))
78 return -EOVERFLOW;
79 }
80 *treep = tree;
81
82 return 0;
83}
84
85/**
86 * free_oftree() - Free memory used by get_oftree()
87 *
88 * @tree: Tree to free
89 */
90void free_oftree(oftree tree)
91{
92 if (of_live_active())
93 free(tree.np);
94}
95
Simon Glass9bf78a52023-09-26 08:14:39 -060096/* test ofnode_device_is_compatible() */
Masahiro Yamada04ca8712018-04-27 01:02:02 +090097static int dm_test_ofnode_compatible(struct unit_test_state *uts)
98{
99 ofnode root_node = ofnode_path("/");
100
101 ut_assert(ofnode_valid(root_node));
102 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
103
104 return 0;
105}
Simon Glass47a677c2022-09-06 20:27:30 -0600106DM_TEST(dm_test_ofnode_compatible,
107 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
108
109/* check ofnode_device_is_compatible() with the 'other' FDT */
110static int dm_test_ofnode_compatible_ot(struct unit_test_state *uts)
111{
112 oftree otree = get_other_oftree(uts);
113 ofnode oroot = oftree_root(otree);
114
115 ut_assert(ofnode_valid(oroot));
116 ut_assert(ofnode_device_is_compatible(oroot, "sandbox-other"));
117
118 return 0;
119}
Simon Glass3a1fc172023-09-26 08:14:38 -0600120DM_TEST(dm_test_ofnode_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200121
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200122static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
123{
124 /* test invalid phandle */
125 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
126 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
127
128 /* test first valid phandle */
129 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
130
131 /* test unknown phandle */
132 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
133
Simon Glass928d2672022-09-06 20:27:22 -0600134 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
135
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200136 return 0;
137}
138DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
139
Simon Glass9bf78a52023-09-26 08:14:39 -0600140/* test oftree_get_by_phandle() with a the 'other' oftree */
Simon Glass5e969252022-09-06 20:27:27 -0600141static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts)
142{
143 oftree otree = get_other_oftree(uts);
144 ofnode node;
145
146 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
147 node = oftree_get_by_phandle(otree, 1);
148 ut_assert(ofnode_valid(node));
149 ut_asserteq_str("target", ofnode_get_name(node));
150
151 return 0;
152}
Simon Glass3a1fc172023-09-26 08:14:38 -0600153DM_TEST(dm_test_ofnode_get_by_phandle_ot,
154 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass5e969252022-09-06 20:27:27 -0600155
Simon Glass47a677c2022-09-06 20:27:30 -0600156static int check_prop_values(struct unit_test_state *uts, ofnode start,
157 const char *propname, const char *propval,
158 int expect_count)
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200159{
Simon Glass47a677c2022-09-06 20:27:30 -0600160 int proplen = strlen(propval) + 1;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200161 const char *str;
Simon Glass47a677c2022-09-06 20:27:30 -0600162 ofnode node;
163 int count;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200164
165 /* Find first matching node, there should be at least one */
Simon Glass47a677c2022-09-06 20:27:30 -0600166 node = ofnode_by_prop_value(start, propname, propval, proplen);
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200167 ut_assert(ofnode_valid(node));
168 str = ofnode_read_string(node, propname);
169 ut_assert(str && !strcmp(str, propval));
170
171 /* Find the rest of the matching nodes */
Simon Glass47a677c2022-09-06 20:27:30 -0600172 count = 1;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200173 while (true) {
Simon Glass47a677c2022-09-06 20:27:30 -0600174 node = ofnode_by_prop_value(node, propname, propval, proplen);
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200175 if (!ofnode_valid(node))
176 break;
177 str = ofnode_read_string(node, propname);
Simon Glass47a677c2022-09-06 20:27:30 -0600178 ut_asserteq_str(propval, str);
179 count++;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200180 }
Simon Glass47a677c2022-09-06 20:27:30 -0600181 ut_asserteq(expect_count, count);
182
183 return 0;
184}
185
186static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
187{
188 ut_assertok(check_prop_values(uts, ofnode_null(), "compatible",
189 "denx,u-boot-fdt-test", 11));
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200190
191 return 0;
192}
Simon Glasse180c2b2020-07-28 19:41:12 -0600193DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glasse6c5c942018-10-01 12:22:08 -0600194
Simon Glass9bf78a52023-09-26 08:14:39 -0600195/* test ofnode_by_prop_value() with a the 'other' oftree */
Simon Glass47a677c2022-09-06 20:27:30 -0600196static int dm_test_ofnode_by_prop_value_ot(struct unit_test_state *uts)
197{
198 oftree otree = get_other_oftree(uts);
199
200 ut_assertok(check_prop_values(uts, oftree_root(otree), "str-prop",
201 "other", 2));
202
203 return 0;
204}
Simon Glass3a1fc172023-09-26 08:14:38 -0600205DM_TEST(dm_test_ofnode_by_prop_value_ot,
206 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600207
Simon Glass9bf78a52023-09-26 08:14:39 -0600208/* test ofnode_read_fmap_entry() */
Simon Glasse6c5c942018-10-01 12:22:08 -0600209static int dm_test_ofnode_fmap(struct unit_test_state *uts)
210{
211 struct fmap_entry entry;
212 ofnode node;
213
214 node = ofnode_path("/cros-ec/flash");
215 ut_assert(ofnode_valid(node));
216 ut_assertok(ofnode_read_fmap_entry(node, &entry));
217 ut_asserteq(0x08000000, entry.offset);
218 ut_asserteq(0x20000, entry.length);
219
220 return 0;
221}
Simon Glasse180c2b2020-07-28 19:41:12 -0600222DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass14ca9f72020-01-27 08:49:43 -0700223
Simon Glass9bf78a52023-09-26 08:14:39 -0600224/* test ofnode_read_prop() */
Simon Glassa8167d82020-01-27 08:49:44 -0700225static int dm_test_ofnode_read(struct unit_test_state *uts)
226{
227 const u32 *val;
228 ofnode node;
229 int size;
230
231 node = ofnode_path("/a-test");
232 ut_assert(ofnode_valid(node));
233
234 val = ofnode_read_prop(node, "int-value", &size);
235 ut_assertnonnull(val);
236 ut_asserteq(4, size);
237 ut_asserteq(1234, fdt32_to_cpu(val[0]));
238
239 val = ofnode_read_prop(node, "missing", &size);
240 ut_assertnull(val);
241 ut_asserteq(-FDT_ERR_NOTFOUND, size);
242
243 /* Check it works without a size parameter */
244 val = ofnode_read_prop(node, "missing", NULL);
245 ut_assertnull(val);
246
247 return 0;
248}
Simon Glass3a1fc172023-09-26 08:14:38 -0600249DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_FDT);
Simon Glassa8167d82020-01-27 08:49:44 -0700250
Simon Glass9bf78a52023-09-26 08:14:39 -0600251/* test ofnode_read_prop() with the 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600252static int dm_test_ofnode_read_ot(struct unit_test_state *uts)
253{
254 oftree otree = get_other_oftree(uts);
255 const char *val;
256 ofnode node;
257 int size;
258
259 node = oftree_path(otree, "/node/subnode");
260 ut_assert(ofnode_valid(node));
261
262 val = ofnode_read_prop(node, "str-prop", &size);
263 ut_assertnonnull(val);
264 ut_asserteq_str("other", val);
265 ut_asserteq(6, size);
266
267 return 0;
268}
Simon Glass3a1fc172023-09-26 08:14:38 -0600269DM_TEST(dm_test_ofnode_read_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600270
Simon Glass9bf78a52023-09-26 08:14:39 -0600271/* test ofnode_count_/parse_phandle_with_args() */
Patrick Delaunaycc72f3e2020-09-25 09:41:16 +0200272static int dm_test_ofnode_phandle(struct unit_test_state *uts)
273{
274 struct ofnode_phandle_args args;
275 ofnode node;
276 int ret;
277 const char prop[] = "test-gpios";
278 const char cell[] = "#gpio-cells";
279 const char prop2[] = "phandle-value";
280
281 node = ofnode_path("/a-test");
282 ut_assert(ofnode_valid(node));
283
284 /* Test ofnode_count_phandle_with_args with cell name */
285 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
286 ut_asserteq(-ENOENT, ret);
287 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
288 ut_asserteq(-EINVAL, ret);
289 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
290 ut_asserteq(5, ret);
291
292 /* Test ofnode_parse_phandle_with_args with cell name */
293 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
294 &args);
295 ut_asserteq(-ENOENT, ret);
296 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
297 &args);
298 ut_asserteq(-EINVAL, ret);
299 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
300 ut_assertok(ret);
301 ut_asserteq(1, args.args_count);
302 ut_asserteq(1, args.args[0]);
303 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
304 ut_assertok(ret);
305 ut_asserteq(1, args.args_count);
306 ut_asserteq(4, args.args[0]);
307 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
308 ut_assertok(ret);
309 ut_asserteq(5, args.args_count);
310 ut_asserteq(5, args.args[0]);
311 ut_asserteq(1, args.args[4]);
312 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
313 ut_asserteq(-ENOENT, ret);
314 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
315 ut_assertok(ret);
316 ut_asserteq(1, args.args_count);
317 ut_asserteq(12, args.args[0]);
318 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
319 ut_asserteq(-ENOENT, ret);
320
321 /* Test ofnode_count_phandle_with_args with cell count */
322 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
323 ut_asserteq(-ENOENT, ret);
324 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
325 ut_asserteq(3, ret);
326
327 /* Test ofnode_parse_phandle_with_args with cell count */
328 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
329 ut_assertok(ret);
330 ut_asserteq(1, ofnode_valid(args.node));
331 ut_asserteq(1, args.args_count);
332 ut_asserteq(10, args.args[0]);
333 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
334 ut_asserteq(-EINVAL, ret);
335 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
336 ut_assertok(ret);
337 ut_asserteq(1, ofnode_valid(args.node));
338 ut_asserteq(1, args.args_count);
339 ut_asserteq(30, args.args[0]);
340 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
341 ut_asserteq(-ENOENT, ret);
342
343 return 0;
344}
345DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
346
Simon Glass9bf78a52023-09-26 08:14:39 -0600347/* test ofnode_count_/parse_phandle_with_args() with 'other' tree */
Simon Glass5e969252022-09-06 20:27:27 -0600348static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
349{
350 oftree otree = get_other_oftree(uts);
351 struct ofnode_phandle_args args;
352 ofnode node;
353 int ret;
354
355 node = oftree_path(otree, "/node");
356
357 /* Test ofnode_count_phandle_with_args with cell name */
358 ret = ofnode_count_phandle_with_args(node, "missing", "#gpio-cells", 0);
359 ut_asserteq(-ENOENT, ret);
360 ret = ofnode_count_phandle_with_args(node, "target", "#invalid", 0);
361 ut_asserteq(-EINVAL, ret);
362 ret = ofnode_count_phandle_with_args(node, "target", "#gpio-cells", 0);
363 ut_asserteq(1, ret);
364
365 ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
366 0, &args);
367 ut_assertok(ret);
368 ut_asserteq(2, args.args_count);
369 ut_asserteq(3, args.args[0]);
370 ut_asserteq(4, args.args[1]);
371
372 return 0;
373}
374DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
375
Simon Glass9bf78a52023-09-26 08:14:39 -0600376/* test ofnode_read_chosen_string/node/prop() */
Simon Glass14ca9f72020-01-27 08:49:43 -0700377static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
378{
379 const char *str;
Simon Glassbd933bf2020-01-27 08:49:46 -0700380 const u32 *val;
Simon Glass14ca9f72020-01-27 08:49:43 -0700381 ofnode node;
Simon Glassbd933bf2020-01-27 08:49:46 -0700382 int size;
Simon Glass14ca9f72020-01-27 08:49:43 -0700383
384 str = ofnode_read_chosen_string("setting");
385 ut_assertnonnull(str);
386 ut_asserteq_str("sunrise ohoka", str);
387 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
388
389 node = ofnode_get_chosen_node("other-node");
390 ut_assert(ofnode_valid(node));
391 ut_asserteq_str("c-test@5", ofnode_get_name(node));
392
393 node = ofnode_get_chosen_node("setting");
394 ut_assert(!ofnode_valid(node));
395
Simon Glassbd933bf2020-01-27 08:49:46 -0700396 val = ofnode_read_chosen_prop("int-values", &size);
397 ut_assertnonnull(val);
398 ut_asserteq(8, size);
399 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
400 ut_asserteq(72993, fdt32_to_cpu(val[1]));
401
Simon Glass14ca9f72020-01-27 08:49:43 -0700402 return 0;
403}
Simon Glasse180c2b2020-07-28 19:41:12 -0600404DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chunfeng Yunbf6ad912020-05-02 11:35:10 +0200405
Simon Glass9bf78a52023-09-26 08:14:39 -0600406/* test ofnode_get_aliases_node/prop() */
Michal Simek305d3182020-07-28 12:51:08 +0200407static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
408{
409 const void *val;
410 ofnode node;
411 int size;
412
Michael Walle82a3c9e2021-02-25 16:51:11 +0100413 node = ofnode_get_aliases_node("ethernet3");
Michal Simek305d3182020-07-28 12:51:08 +0200414 ut_assert(ofnode_valid(node));
415 ut_asserteq_str("sbe5", ofnode_get_name(node));
416
417 node = ofnode_get_aliases_node("unknown");
418 ut_assert(!ofnode_valid(node));
419
420 val = ofnode_read_aliases_prop("spi0", &size);
421 ut_assertnonnull(val);
422 ut_asserteq(7, size);
423 ut_asserteq_str("/spi@0", (const char *)val);
424
425 return 0;
426}
427DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
428
Chunfeng Yunbf6ad912020-05-02 11:35:10 +0200429static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
430{
431 ofnode node, child_node;
432 u32 val;
433
434 node = ofnode_path("/i-test");
435 ut_assert(ofnode_valid(node));
436
437 val = ofnode_get_child_count(node);
438 ut_asserteq(3, val);
439
440 child_node = ofnode_first_subnode(node);
441 ut_assert(ofnode_valid(child_node));
442 val = ofnode_get_child_count(child_node);
443 ut_asserteq(0, val);
444
445 return 0;
446}
447DM_TEST(dm_test_ofnode_get_child_count,
Simon Glasse180c2b2020-07-28 19:41:12 -0600448 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0de1b072020-11-28 17:50:02 -0700449
Simon Glass9bf78a52023-09-26 08:14:39 -0600450/* test ofnode_get_child_count() with 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600451static int dm_test_ofnode_get_child_count_ot(struct unit_test_state *uts)
452{
453 oftree otree = get_other_oftree(uts);
454 ofnode node, child_node;
455 u32 val;
456
457 node = oftree_path(otree, "/node");
458 ut_assert(ofnode_valid(node));
459
460 val = ofnode_get_child_count(node);
461 ut_asserteq(2, val);
462
463 child_node = ofnode_first_subnode(node);
464 ut_assert(ofnode_valid(child_node));
465 val = ofnode_get_child_count(child_node);
466 ut_asserteq(0, val);
467
468 return 0;
469}
Simon Glass3a1fc172023-09-26 08:14:38 -0600470DM_TEST(dm_test_ofnode_get_child_count_ot,
471 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600472
Simon Glass0de1b072020-11-28 17:50:02 -0700473static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
474{
475 ofnode root_node = ofnode_path("/");
476 ofnode node = ofnode_path("/usb@0");
477
478 ut_assert(ofnode_is_enabled(root_node));
479 ut_assert(!ofnode_is_enabled(node));
480
481 return 0;
482}
483DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800484
Simon Glass9bf78a52023-09-26 08:14:39 -0600485/* test ofnode_is_enabled() with 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600486static int dm_test_ofnode_is_enabled_ot(struct unit_test_state *uts)
487{
488 oftree otree = get_other_oftree(uts);
489 ofnode root_node = oftree_root(otree);
490 ofnode node = oftree_path(otree, "/target");
491
492 ut_assert(ofnode_is_enabled(root_node));
493 ut_assert(!ofnode_is_enabled(node));
494
495 return 0;
496}
497DM_TEST(dm_test_ofnode_is_enabled_ot, UT_TESTF_OTHER_FDT);
498
Simon Glass9bf78a52023-09-26 08:14:39 -0600499/* test ofnode_get_addr/size() */
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800500static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
501{
502 ofnode node;
503 fdt_addr_t addr;
504 fdt_size_t size;
505
506 node = ofnode_path("/translation-test@8000");
507 ut_assert(ofnode_valid(node));
508 addr = ofnode_get_addr(node);
509 size = ofnode_get_size(node);
510 ut_asserteq(0x8000, addr);
511 ut_asserteq(0x4000, size);
512
513 node = ofnode_path("/translation-test@8000/dev@1,100");
514 ut_assert(ofnode_valid(node));
515 addr = ofnode_get_addr(node);
516 size = ofnode_get_size(node);
517 ut_asserteq(0x9000, addr);
518 ut_asserteq(0x1000, size);
519
520 node = ofnode_path("/emul-mux-controller");
521 ut_assert(ofnode_valid(node));
522 addr = ofnode_get_addr(node);
523 size = ofnode_get_size(node);
Patrice Chotard9876ae72022-01-04 08:42:48 +0100524 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800525 ut_asserteq(FDT_SIZE_T_NONE, size);
526
Marek Behún31a7b712021-05-26 14:08:17 +0200527 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
528 ut_assert(ofnode_valid(node));
529 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
530 ut_asserteq_64(0x42, addr);
531
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800532 return 0;
533}
534DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Behún0e116be2021-05-26 14:08:18 +0200535
Simon Glass9bf78a52023-09-26 08:14:39 -0600536/* test ofnode_get_addr() with 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600537static int dm_test_ofnode_get_reg_ot(struct unit_test_state *uts)
538{
539 oftree otree = get_other_oftree(uts);
540 ofnode node = oftree_path(otree, "/target");
541 fdt_addr_t addr;
542
543 addr = ofnode_get_addr(node);
544 ut_asserteq(0x8000, addr);
545
546 return 0;
547}
Simon Glass3a1fc172023-09-26 08:14:38 -0600548DM_TEST(dm_test_ofnode_get_reg_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600549
Marek Behún0e116be2021-05-26 14:08:18 +0200550static int dm_test_ofnode_get_path(struct unit_test_state *uts)
551{
552 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
553 char buf[64];
554 ofnode node;
Marek Behún0e116be2021-05-26 14:08:18 +0200555
556 node = ofnode_path(path);
557 ut_assert(ofnode_valid(node));
558
Simon Glass47a677c2022-09-06 20:27:30 -0600559 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
Marek Behún0e116be2021-05-26 14:08:18 +0200560 ut_asserteq_str(path, buf);
561
Simon Glass47a677c2022-09-06 20:27:30 -0600562 ut_asserteq(-ENOSPC, ofnode_get_path(node, buf, 32));
Marek Behún0e116be2021-05-26 14:08:18 +0200563
Simon Glass47a677c2022-09-06 20:27:30 -0600564 ut_assertok(ofnode_get_path(ofnode_root(), buf, 32));
Simon Glass66d0d0c2022-09-06 20:27:18 -0600565 ut_asserteq_str("/", buf);
566
Marek Behún0e116be2021-05-26 14:08:18 +0200567 return 0;
568}
569DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7de8bd02021-08-07 07:24:01 -0600570
Simon Glass9bf78a52023-09-26 08:14:39 -0600571/* test ofnode_get_path() with 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600572static int dm_test_ofnode_get_path_ot(struct unit_test_state *uts)
573{
574 oftree otree = get_other_oftree(uts);
575 const char *path = "/node/subnode";
576 ofnode node = oftree_path(otree, path);
577 char buf[64];
578
579 ut_assert(ofnode_valid(node));
580
581 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
582 ut_asserteq_str(path, buf);
583
584 ut_assertok(ofnode_get_path(oftree_root(otree), buf, 32));
585 ut_asserteq_str("/", buf);
586
587 return 0;
588}
Simon Glass3a1fc172023-09-26 08:14:38 -0600589DM_TEST(dm_test_ofnode_get_path_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600590
Simon Glass9bf78a52023-09-26 08:14:39 -0600591/* test ofnode_conf_read_bool/int/str() */
Simon Glass7de8bd02021-08-07 07:24:01 -0600592static int dm_test_ofnode_conf(struct unit_test_state *uts)
593{
594 ut_assert(!ofnode_conf_read_bool("missing"));
595 ut_assert(ofnode_conf_read_bool("testing-bool"));
596
597 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
598 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
599
600 ut_assertnull(ofnode_conf_read_str("missing"));
601 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
602
603 return 0;
604}
Simon Glass3a1fc172023-09-26 08:14:38 -0600605DM_TEST(dm_test_ofnode_conf, UT_TESTF_SCAN_FDT);
Michael Wallebce039a2021-10-15 15:15:18 +0200606
Michal Simekdb5e3492023-08-31 08:59:05 +0200607static int dm_test_ofnode_options(struct unit_test_state *uts)
608{
Michal Simek44f35e12023-08-31 09:04:27 +0200609 u64 bootscr_address, bootscr_offset;
610 u64 bootscr_flash_offset, bootscr_flash_size;
Michal Simekdb5e3492023-08-31 08:59:05 +0200611
612 ut_assertok(ofnode_read_bootscript_address(&bootscr_address,
613 &bootscr_offset));
614 ut_asserteq_64(0, bootscr_address);
615 ut_asserteq_64(0x12345678, bootscr_offset);
616
Michal Simek44f35e12023-08-31 09:04:27 +0200617 ut_assertok(ofnode_read_bootscript_flash(&bootscr_flash_offset,
618 &bootscr_flash_size));
619 ut_asserteq_64(0, bootscr_flash_offset);
620 ut_asserteq_64(0x2000, bootscr_flash_size);
621
Michal Simekdb5e3492023-08-31 08:59:05 +0200622 return 0;
623}
624DM_TEST(dm_test_ofnode_options, 0);
625
Michael Wallebce039a2021-10-15 15:15:18 +0200626static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
627{
628 const char compatible[] = "denx,u-boot-fdt-test";
629 bool found = false;
630 ofnode node;
631
632 ofnode_for_each_compatible_node(node, compatible) {
633 ut_assert(ofnode_device_is_compatible(node, compatible));
634 found = true;
635 }
636
637 /* There should be at least one matching node */
638 ut_assert(found);
639
640 return 0;
641}
642DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glassfb933d02021-10-23 17:26:04 -0600643
Simon Glass9bf78a52023-09-26 08:14:39 -0600644/* test dm_test_ofnode_string_count/index/list() */
Simon Glassfb933d02021-10-23 17:26:04 -0600645static int dm_test_ofnode_string(struct unit_test_state *uts)
646{
Simon Glass075bfc92021-10-23 17:26:07 -0600647 const char **val;
Simon Glassfb933d02021-10-23 17:26:04 -0600648 const char *out;
649 ofnode node;
650
651 node = ofnode_path("/a-test");
652 ut_assert(ofnode_valid(node));
653
654 /* single string */
655 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
656 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
657 ut_asserteq_str("test string", out);
658 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
659 "test string"));
Simon Glass075bfc92021-10-23 17:26:07 -0600660 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
661 ut_asserteq_str("test string", val[0]);
662 ut_assertnull(val[1]);
663 free(val);
Simon Glassfb933d02021-10-23 17:26:04 -0600664
665 /* list of strings */
666 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
667 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
668 &out));
669 ut_asserteq_str("mux0", out);
670 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
671 "mux0"));
Simon Glass075bfc92021-10-23 17:26:07 -0600672 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
673 &val));
674 ut_asserteq_str("mux0", val[0]);
675 ut_asserteq_str("mux1", val[1]);
676 ut_asserteq_str("mux2", val[2]);
677 ut_asserteq_str("mux3", val[3]);
678 ut_asserteq_str("mux4", val[4]);
679 ut_assertnull(val[5]);
680 free(val);
Simon Glassfb933d02021-10-23 17:26:04 -0600681
682 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
683 &out));
684 ut_asserteq_str("mux4", out);
685 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
686 "mux4"));
687
688 return 0;
689}
Simon Glass3a1fc172023-09-26 08:14:38 -0600690DM_TEST(dm_test_ofnode_string, UT_TESTF_SCAN_FDT);
Simon Glassfb933d02021-10-23 17:26:04 -0600691
Simon Glass9bf78a52023-09-26 08:14:39 -0600692/* test error returns from ofnode_read_string_count/index/list() */
Simon Glassfb933d02021-10-23 17:26:04 -0600693static int dm_test_ofnode_string_err(struct unit_test_state *uts)
694{
Simon Glass075bfc92021-10-23 17:26:07 -0600695 const char **val;
Simon Glassfb933d02021-10-23 17:26:04 -0600696 const char *out;
697 ofnode node;
698
699 /*
700 * Test error codes only on livetree, as they are different with
701 * flattree
702 */
703 node = ofnode_path("/a-test");
704 ut_assert(ofnode_valid(node));
705
706 /* non-existent property */
707 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
708 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
709 &out));
Simon Glass075bfc92021-10-23 17:26:07 -0600710 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600711
712 /* empty property */
713 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
714 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
715 &out));
Simon Glass075bfc92021-10-23 17:26:07 -0600716 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
717 &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600718
719 /* badly formatted string list */
720 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
721 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
722 &out));
Simon Glass075bfc92021-10-23 17:26:07 -0600723 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
724 &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600725
726 /* out of range / not found */
727 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
728 &out));
729 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
730 "other"));
731
732 /* negative value for index is not allowed, so don't test for that */
733
734 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
735 "mux-control-names", 5,
736 &out));
737
738 return 0;
739}
740DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf3dd2132022-04-07 00:32:57 +0200741
Simon Glass9bf78a52023-09-26 08:14:39 -0600742static int dm_test_ofnode_read_phy_mode(struct unit_test_state *uts)
Marek Behúnf3dd2132022-04-07 00:32:57 +0200743{
744 ofnode eth_node, phy_node;
Marek Behún123ca112022-04-07 00:33:01 +0200745 phy_interface_t mode;
Marek Behúnf3dd2132022-04-07 00:32:57 +0200746 u32 reg;
747
748 eth_node = ofnode_path("/phy-test-eth");
749 ut_assert(ofnode_valid(eth_node));
750
Marek Behún123ca112022-04-07 00:33:01 +0200751 mode = ofnode_read_phy_mode(eth_node);
752 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
753
Marek Behúnf3dd2132022-04-07 00:32:57 +0200754 phy_node = ofnode_get_phy_node(eth_node);
755 ut_assert(ofnode_valid(phy_node));
756
757 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
758 ut_asserteq_64(0x1, reg);
759
760 return 0;
761}
Simon Glass9bf78a52023-09-26 08:14:39 -0600762DM_TEST(dm_test_ofnode_read_phy_mode, UT_TESTF_SCAN_FDT);
Simon Glass33104842022-07-30 15:52:08 -0600763
764/**
765 * make_ofnode_fdt() - Create an FDT for testing with ofnode
766 *
767 * The size is set to the minimum needed
768 *
769 * @uts: Test state
770 * @fdt: Place to write FDT
771 * @size: Maximum size of space for fdt
Simon Glass47a677c2022-09-06 20:27:30 -0600772 * @id: id value to add to the tree ('id' property in root node)
Simon Glass33104842022-07-30 15:52:08 -0600773 */
Simon Glass47a677c2022-09-06 20:27:30 -0600774static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size,
775 int id)
Simon Glass33104842022-07-30 15:52:08 -0600776{
777 ut_assertok(fdt_create(fdt, size));
778 ut_assertok(fdt_finish_reservemap(fdt));
779 ut_assert(fdt_begin_node(fdt, "") >= 0);
780
Simon Glass47a677c2022-09-06 20:27:30 -0600781 ut_assertok(fdt_property_u32(fdt, "id", id));
782
Simon Glass33104842022-07-30 15:52:08 -0600783 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
784 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
785 ut_assertok(fdt_end_node(fdt));
786
787 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
788 ut_assertok(fdt_end_node(fdt));
789
790 ut_assertok(fdt_end_node(fdt));
791 ut_assertok(fdt_finish(fdt));
792
793 return 0;
794}
795
Simon Glass9bf78a52023-09-26 08:14:39 -0600796/* Check that aliases work on the control FDT */
797static int dm_test_ofnode_aliases(struct unit_test_state *uts)
Simon Glass33104842022-07-30 15:52:08 -0600798{
Simon Glass33104842022-07-30 15:52:08 -0600799 ofnode node;
800
Simon Glass33104842022-07-30 15:52:08 -0600801 node = ofnode_get_aliases_node("ethernet3");
802 ut_assert(ofnode_valid(node));
803 ut_asserteq_str("sbe5", ofnode_get_name(node));
804
Simon Glass085d5942022-09-06 20:27:21 -0600805 ut_assert(!oftree_valid(oftree_null()));
806
Simon Glassc43635b2022-10-20 18:22:49 -0600807 return 0;
808}
Simon Glass9bf78a52023-09-26 08:14:39 -0600809DM_TEST(dm_test_ofnode_aliases, UT_TESTF_SCAN_FDT);
Simon Glassc43635b2022-10-20 18:22:49 -0600810
Simon Glass9bf78a52023-09-26 08:14:39 -0600811/**
812 * dm_test_ofnode_root_mult() - Check aliaes on control and 'other' tree
813 *
814 * Check that aliases work only with the control FDT, not with 'other' tree.
815 * This is not actually the desired behaviour. If aliases are implemented for
816 * any tree, then this test should be changed.
817 */
Simon Glassc43635b2022-10-20 18:22:49 -0600818static int dm_test_ofnode_root_mult(struct unit_test_state *uts)
819{
820 char fdt[256];
821 oftree tree;
822 ofnode node;
823
824 /* skip this test if multiple FDTs are not supported */
825 if (!IS_ENABLED(CONFIG_OFNODE_MULTI_TREE))
826 return -EAGAIN;
827
Simon Glass47a677c2022-09-06 20:27:30 -0600828 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt), 0));
Simon Glassc43635b2022-10-20 18:22:49 -0600829 ut_assertok(get_oftree(uts, fdt, &tree));
Simon Glass085d5942022-09-06 20:27:21 -0600830 ut_assert(oftree_valid(tree));
Simon Glass33104842022-07-30 15:52:08 -0600831
832 /* Make sure they don't work on this new tree */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600833 node = oftree_path(tree, "mmc0");
Simon Glass33104842022-07-30 15:52:08 -0600834 ut_assert(!ofnode_valid(node));
835
836 /* It should appear in the new tree */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600837 node = oftree_path(tree, "/new-mmc");
Simon Glass33104842022-07-30 15:52:08 -0600838 ut_assert(ofnode_valid(node));
839
840 /* ...and not in the control FDT */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600841 node = oftree_path(oftree_default(), "/new-mmc");
Simon Glass33104842022-07-30 15:52:08 -0600842 ut_assert(!ofnode_valid(node));
843
Simon Glass88a1ae82022-09-06 20:27:29 -0600844 free_oftree(tree);
Simon Glass33104842022-07-30 15:52:08 -0600845
846 return 0;
847}
Simon Glassc43635b2022-10-20 18:22:49 -0600848DM_TEST(dm_test_ofnode_root_mult, UT_TESTF_SCAN_FDT);
Simon Glass65715592022-07-30 15:52:09 -0600849
Simon Glass9bf78a52023-09-26 08:14:39 -0600850/* test ofnode_set_enabled(), ofnode_write_prop() on a livetree */
Simon Glass65715592022-07-30 15:52:09 -0600851static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
852{
853 struct udevice *dev;
854 ofnode node;
855
Simon Glass65715592022-07-30 15:52:09 -0600856 /* Test enabling devices */
Simon Glass65715592022-07-30 15:52:09 -0600857 node = ofnode_path("/usb@2");
858
Simon Glass39e42be2022-07-30 15:52:13 -0600859 ut_assert(!ofnode_is_enabled(node));
860 ut_assertok(ofnode_set_enabled(node, true));
861 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass65715592022-07-30 15:52:09 -0600862
863 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
864 &dev);
865 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
866
867 /* Test string property setting */
Simon Glass65715592022-07-30 15:52:09 -0600868 ut_assert(device_is_compatible(dev, "sandbox,usb"));
869 ofnode_write_string(node, "compatible", "gdsys,super-usb");
870 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
871 ofnode_write_string(node, "compatible", "sandbox,usb");
872 ut_assert(device_is_compatible(dev, "sandbox,usb"));
873
874 /* Test setting generic properties */
875
876 /* Non-existent in DTB */
877 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
878 /* reg = 0x42, size = 0x100 */
Simon Glassbe0789a2022-07-30 15:52:10 -0600879 ut_assertok(ofnode_write_prop(node, "reg",
Simon Glass0b58eaa2022-09-06 20:27:32 -0600880 "\x00\x00\x00\x42\x00\x00\x01\x00", 8,
881 false));
Simon Glass65715592022-07-30 15:52:09 -0600882 ut_asserteq(0x42, dev_read_addr(dev));
883
884 /* Test disabling devices */
Simon Glass65715592022-07-30 15:52:09 -0600885 device_remove(dev, DM_REMOVE_NORMAL);
886 device_unbind(dev);
887
Simon Glass39e42be2022-07-30 15:52:13 -0600888 ut_assert(ofnode_is_enabled(node));
889 ut_assertok(ofnode_set_enabled(node, false));
890 ut_assert(!ofnode_is_enabled(node));
Simon Glass65715592022-07-30 15:52:09 -0600891
892 return 0;
893}
Simon Glassb7eaa4f2022-07-30 15:52:11 -0600894DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass2b90e0d2022-09-06 20:27:07 -0600895 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass55f79902022-07-30 15:52:14 -0600896
Simon Glass0b58eaa2022-09-06 20:27:32 -0600897static int check_write_prop(struct unit_test_state *uts, ofnode node)
898{
899 char prop[] = "middle-name";
900 char name[10];
901 int len;
902
903 strcpy(name, "cecil");
904 len = strlen(name) + 1;
905 ut_assertok(ofnode_write_prop(node, prop, name, len, false));
906 ut_asserteq_str(name, ofnode_read_string(node, prop));
907
908 /* change the underlying value, this should mess up the live tree */
909 strcpy(name, "tony");
910 if (of_live_active()) {
911 ut_asserteq_str(name, ofnode_read_string(node, prop));
912 } else {
913 ut_asserteq_str("cecil", ofnode_read_string(node, prop));
914 }
915
916 /* try again, this time copying the property */
917 strcpy(name, "mary");
918 ut_assertok(ofnode_write_prop(node, prop, name, len, true));
919 ut_asserteq_str(name, ofnode_read_string(node, prop));
920 strcpy(name, "leah");
921
922 /* both flattree and livetree behave the same */
923 ut_asserteq_str("mary", ofnode_read_string(node, prop));
924
925 return 0;
926}
927
928/* writing the tree with and without copying the property */
929static int dm_test_ofnode_write_copy(struct unit_test_state *uts)
930{
931 ofnode node;
932
933 node = ofnode_path("/a-test");
934 ut_assertok(check_write_prop(uts, node));
935
936 return 0;
937}
938DM_TEST(dm_test_ofnode_write_copy, UT_TESTF_SCAN_FDT);
939
Simon Glass9bf78a52023-09-26 08:14:39 -0600940/* test writing a property to the 'other' tree */
Simon Glass0b58eaa2022-09-06 20:27:32 -0600941static int dm_test_ofnode_write_copy_ot(struct unit_test_state *uts)
942{
943 oftree otree = get_other_oftree(uts);
944 ofnode node, check_node;
945
946 node = oftree_path(otree, "/node");
947 ut_assertok(check_write_prop(uts, node));
948
949 /* make sure the control FDT is not touched */
950 check_node = ofnode_path("/node");
951 ut_assertnull(ofnode_read_string(check_node, "middle-name"));
952
953 return 0;
954}
Simon Glass3a1fc172023-09-26 08:14:38 -0600955DM_TEST(dm_test_ofnode_write_copy_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass0b58eaa2022-09-06 20:27:32 -0600956
Simon Glass9bf78a52023-09-26 08:14:39 -0600957/* test ofnode_read_u32_index/default() */
Simon Glass55f79902022-07-30 15:52:14 -0600958static int dm_test_ofnode_u32(struct unit_test_state *uts)
959{
960 ofnode node;
Simon Glass66d0d0c2022-09-06 20:27:18 -0600961 u32 val;
Simon Glass55f79902022-07-30 15:52:14 -0600962
963 node = ofnode_path("/lcd");
964 ut_assert(ofnode_valid(node));
965 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
966 ut_assertok(ofnode_write_u32(node, "xres", 1367));
967 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
968 ut_assertok(ofnode_write_u32(node, "xres", 1366));
969
Simon Glass66d0d0c2022-09-06 20:27:18 -0600970 node = ofnode_path("/backlight");
971 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
972 ut_asserteq(0, val);
973 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
974 ut_asserteq(16, val);
975 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
976 ut_asserteq(255, val);
977 ut_asserteq(-EOVERFLOW,
978 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
979 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
980
Simon Glass55f79902022-07-30 15:52:14 -0600981 return 0;
982}
Simon Glass2b90e0d2022-09-06 20:27:07 -0600983DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassffe90392022-09-06 20:27:02 -0600984
Simon Glass9bf78a52023-09-26 08:14:39 -0600985/* test ofnode_read_u32_array() */
Simon Glass66d0d0c2022-09-06 20:27:18 -0600986static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
987{
988 ofnode node;
989 u32 val[10];
990
991 node = ofnode_path("/a-test");
992 ut_assert(ofnode_valid(node));
993 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
994 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
995 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
996 1));
997
998 memset(val, '\0', sizeof(val));
999 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
1000 ut_asserteq(0, val[0]);
1001 ut_asserteq(5678, val[1]);
1002 ut_asserteq(9123, val[2]);
1003 ut_asserteq(4567, val[3]);
1004 ut_asserteq(0, val[4]);
1005 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
1006 4));
1007
1008 return 0;
1009}
1010DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1011
Simon Glass9bf78a52023-09-26 08:14:39 -06001012static int dm_test_ofnode_read_u64(struct unit_test_state *uts)
Simon Glass66d0d0c2022-09-06 20:27:18 -06001013{
1014 ofnode node;
1015 u64 val;
1016
1017 node = ofnode_path("/a-test");
1018 ut_assert(ofnode_valid(node));
1019 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
1020 ut_asserteq_64(0x1111222233334444, val);
1021 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
1022
Michal Simekfa12dfa2023-08-25 11:37:46 +02001023 ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val));
1024 ut_asserteq_64(0x1111222233334444, val);
1025 ut_assertok(ofnode_read_u64_index(node, "int64-array", 1, &val));
1026 ut_asserteq_64(0x4444333322221111, val);
1027 ut_asserteq(-EOVERFLOW,
1028 ofnode_read_u64_index(node, "int64-array", 2, &val));
1029 ut_asserteq(-EINVAL, ofnode_read_u64_index(node, "missing", 0, &val));
1030
Simon Glass66d0d0c2022-09-06 20:27:18 -06001031 return 0;
1032}
Simon Glass9bf78a52023-09-26 08:14:39 -06001033DM_TEST(dm_test_ofnode_read_u64, UT_TESTF_SCAN_FDT);
Simon Glass66d0d0c2022-09-06 20:27:18 -06001034
Simon Glassffe90392022-09-06 20:27:02 -06001035static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
1036{
1037 ofnode node, check, subnode;
1038 char buf[128];
1039
Simon Glassffe90392022-09-06 20:27:02 -06001040 node = ofnode_path("/lcd");
1041 ut_assert(ofnode_valid(node));
1042 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
1043 check = ofnode_path("/lcd/edmund");
1044 ut_asserteq(subnode.of_offset, check.of_offset);
1045 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1046 ut_asserteq_str("/lcd/edmund", buf);
1047
1048 if (of_live_active()) {
1049 struct device_node *child;
1050
1051 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
1052 2, &child));
1053 ut_asserteq_str("ed", child->name);
1054 ut_asserteq_str("/lcd/ed", child->full_name);
1055 check = ofnode_path("/lcd/ed");
1056 ut_asserteq_ptr(child, check.np);
1057 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
1058 sizeof(buf)));
1059 ut_asserteq_str("/lcd/ed", buf);
1060 }
1061
1062 /* An existing node should be returned with -EEXIST */
1063 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
1064 ut_asserteq(subnode.of_offset, check.of_offset);
1065
1066 /* add a root node */
1067 node = ofnode_path("/");
1068 ut_assert(ofnode_valid(node));
1069 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
1070 check = ofnode_path("/lcd2");
1071 ut_asserteq(subnode.of_offset, check.of_offset);
1072 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1073 ut_asserteq_str("/lcd2", buf);
1074
1075 if (of_live_active()) {
1076 ulong start;
1077 int i;
1078
1079 /*
1080 * Make sure each of the three malloc()checks in
1081 * of_add_subnode() work
1082 */
1083 for (i = 0; i < 3; i++) {
1084 malloc_enable_testing(i);
1085 start = ut_check_free();
1086 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
1087 &check));
1088 ut_assertok(ut_check_delta(start));
1089 }
1090
1091 /* This should pass since we allow 3 allocations */
1092 malloc_enable_testing(3);
1093 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
1094 malloc_disable_testing();
1095 }
1096
Simon Glassc3a194d2022-09-06 20:27:03 -06001097 /* write to the empty node */
1098 ut_assertok(ofnode_write_string(subnode, "example", "text"));
1099
Simon Glassffe90392022-09-06 20:27:02 -06001100 return 0;
1101}
Simon Glass2b90e0d2022-09-06 20:27:07 -06001102DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass52ad21a2022-09-06 20:27:16 -06001103
1104static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
1105{
1106 ofnode node, subnode;
1107 struct ofprop prop;
1108 int count;
1109
Dzmitry Sankouski298ffdd2023-01-22 18:21:23 +03001110 node = ofnode_path("/ofnode-foreach");
Simon Glass52ad21a2022-09-06 20:27:16 -06001111 count = 0;
1112
1113 /* we expect "compatible" for each node */
1114 ofnode_for_each_prop(prop, node)
1115 count++;
1116 ut_asserteq(1, count);
1117
1118 /* there are two nodes, each with 2 properties */
1119 ofnode_for_each_subnode(subnode, node)
1120 ofnode_for_each_prop(prop, subnode)
1121 count++;
1122 ut_asserteq(5, count);
1123
1124 return 0;
1125}
1126DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
Simon Glass66d0d0c2022-09-06 20:27:18 -06001127
1128static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
1129{
1130 const char *compat = "denx,u-boot-fdt-test";
1131 ofnode node;
1132 int count;
1133
1134 count = 0;
1135 for (node = ofnode_null();
1136 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1137 count++;
1138 ut_asserteq(11, count);
1139
1140 return 0;
1141}
1142DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
1143
Simon Glass9bf78a52023-09-26 08:14:39 -06001144/* check ofnode_by_compatible() on the 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -06001145static int dm_test_ofnode_by_compatible_ot(struct unit_test_state *uts)
1146{
1147 const char *compat = "sandbox-other2";
1148 oftree otree = get_other_oftree(uts);
1149 ofnode node;
1150 int count;
1151
1152 count = 0;
1153 for (node = oftree_root(otree);
1154 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1155 count++;
1156 ut_asserteq(2, count);
1157
1158 return 0;
1159}
Simon Glass3a1fc172023-09-26 08:14:38 -06001160DM_TEST(dm_test_ofnode_by_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -06001161
Simon Glass66d0d0c2022-09-06 20:27:18 -06001162static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
1163{
1164 ofnode node, subnode;
1165
1166 node = ofnode_path("/buttons");
1167
1168 subnode = ofnode_find_subnode(node, "btn1");
1169 ut_assert(ofnode_valid(subnode));
1170 ut_asserteq_str("btn1", ofnode_get_name(subnode));
1171
1172 subnode = ofnode_find_subnode(node, "btn");
1173 ut_assert(!ofnode_valid(subnode));
1174
1175 return 0;
1176}
1177DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
1178
Simon Glass9bf78a52023-09-26 08:14:39 -06001179/* test ofnode_find_subnode() on the 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -06001180static int dm_test_ofnode_find_subnode_ot(struct unit_test_state *uts)
1181{
1182 oftree otree = get_other_oftree(uts);
1183 ofnode node, subnode;
1184
1185 node = oftree_path(otree, "/node");
1186
1187 subnode = ofnode_find_subnode(node, "subnode");
1188 ut_assert(ofnode_valid(subnode));
1189 ut_asserteq_str("subnode", ofnode_get_name(subnode));
1190
1191 subnode = ofnode_find_subnode(node, "btn");
1192 ut_assert(!ofnode_valid(subnode));
1193
1194 return 0;
1195}
1196DM_TEST(dm_test_ofnode_find_subnode_ot, UT_TESTF_OTHER_FDT);
1197
Simon Glass66d0d0c2022-09-06 20:27:18 -06001198static int dm_test_ofnode_get_name(struct unit_test_state *uts)
1199{
1200 ofnode node;
1201
1202 node = ofnode_path("/buttons");
1203 ut_assert(ofnode_valid(node));
1204 ut_asserteq_str("buttons", ofnode_get_name(node));
1205 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
1206
1207 return 0;
1208}
1209DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -06001210
1211/* try to access more FDTs than is supported */
1212static int dm_test_ofnode_too_many(struct unit_test_state *uts)
1213{
1214 const int max_trees = CONFIG_IS_ENABLED(OFNODE_MULTI_TREE,
1215 (CONFIG_OFNODE_MULTI_TREE_MAX), (1));
1216 const int fdt_size = 256;
1217 const int num_trees = max_trees + 1;
1218 char fdt[num_trees][fdt_size];
1219 int i;
1220
1221 for (i = 0; i < num_trees; i++) {
1222 oftree tree;
1223 int ret;
1224
1225 ut_assertok(make_ofnode_fdt(uts, fdt[i], fdt_size, i));
1226 ret = get_oftree(uts, fdt[i], &tree);
1227
1228 /*
1229 * With flat tree we have the control FDT using one slot. Live
1230 * tree has no limit since it uses pointers, not integer tree
1231 * IDs
1232 */
1233 if (of_live_active() || i < max_trees - 1) {
1234 ut_assertok(ret);
1235 } else {
1236 /*
1237 * tree should be invalid when we try to register too
1238 * many trees
1239 */
1240 ut_asserteq(-EOVERFLOW, ret);
1241 }
1242 }
1243
1244 return 0;
1245}
1246DM_TEST(dm_test_ofnode_too_many, UT_TESTF_SCAN_FDT);
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001247
Simon Glass24797092023-09-26 08:14:37 -06001248static int check_copy_props(struct unit_test_state *uts, ofnode dst, ofnode src)
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001249{
1250 u32 reg[2], val;
1251
Simon Glass24797092023-09-26 08:14:37 -06001252 ut_assertok(ofnode_copy_props(dst, src));
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001253
1254 ut_assertok(ofnode_read_u32(dst, "ping-expect", &val));
1255 ut_asserteq(3, val);
1256
1257 ut_asserteq_str("denx,u-boot-fdt-test",
1258 ofnode_read_string(dst, "compatible"));
1259
1260 /* check that a property with the same name is overwritten */
1261 ut_assertok(ofnode_read_u32_array(dst, "reg", reg, ARRAY_SIZE(reg)));
1262 ut_asserteq(3, reg[0]);
1263 ut_asserteq(1, reg[1]);
1264
1265 /* reset the compatible so the live tree does not change */
1266 ut_assertok(ofnode_write_string(dst, "compatible", "nothing"));
1267
1268 return 0;
1269}
1270
1271static int dm_test_ofnode_copy_props(struct unit_test_state *uts)
1272{
1273 ofnode src, dst;
1274
1275 /*
1276 * These nodes are chosen so that the src node is before the destination
1277 * node in the tree. This doesn't matter with livetree, but with
1278 * flattree any attempt to insert a property earlier in the tree will
1279 * mess up the offsets after it.
1280 */
1281 src = ofnode_path("/b-test");
1282 dst = ofnode_path("/some-bus");
1283
Simon Glass24797092023-09-26 08:14:37 -06001284 ut_assertok(check_copy_props(uts, dst, src));
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001285
1286 /* check a property that is in the destination already */
1287 ut_asserteq_str("mux0", ofnode_read_string(dst, "mux-control-names"));
1288
1289 return 0;
1290}
1291DM_TEST(dm_test_ofnode_copy_props, UT_TESTF_SCAN_FDT);
1292
Simon Glass9bf78a52023-09-26 08:14:39 -06001293/* test ofnode_copy_props() with the 'other' tree */
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001294static int dm_test_ofnode_copy_props_ot(struct unit_test_state *uts)
1295{
1296 ofnode src, dst;
1297 oftree otree = get_other_oftree(uts);
1298
1299 src = ofnode_path("/b-test");
1300 dst = oftree_path(otree, "/node/subnode2");
Simon Glass24797092023-09-26 08:14:37 -06001301 ut_assertok(check_copy_props(uts, dst, src));
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001302
1303 return 0;
1304}
1305DM_TEST(dm_test_ofnode_copy_props_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass9cf39bb2023-06-01 10:22:40 -06001306
1307/* check that the livetree is aligned to a structure boundary */
1308static int dm_test_livetree_align(struct unit_test_state *uts)
1309{
1310 const int align = __alignof__(struct unit_test_state);
1311 struct device_node *node;
1312 u32 *sentinel;
1313 ulong start;
1314
1315 start = (ulong)gd_of_root();
1316 ut_asserteq(start, ALIGN(start, align));
1317
1318 node = gd_of_root();
1319 sentinel = (void *)node - sizeof(u32);
1320
1321 /*
1322 * The sentinel should be overwritten with the root node. If it isn't,
1323 * then the root node is not at the very start of the livetree memory
1324 * area, and free(root) will fail to free the memory used by the
1325 * livetree.
1326 */
1327 ut_assert(*sentinel != BAD_OF_ROOT);
1328
1329 return 0;
1330}
Simon Glass3a1fc172023-09-26 08:14:38 -06001331DM_TEST(dm_test_livetree_align, UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_TREE);
Simon Glassa8f2ac22023-06-01 10:22:42 -06001332
1333/* check that it is possible to load an arbitrary livetree */
1334static int dm_test_livetree_ensure(struct unit_test_state *uts)
1335{
1336 oftree tree;
1337 ofnode node;
1338
1339 /* read from other.dtb */
1340 ut_assertok(test_load_other_fdt(uts));
1341 tree = oftree_from_fdt(uts->other_fdt);
1342 ut_assert(oftree_valid(tree));
1343 node = oftree_path(tree, "/node/subnode");
1344 ut_assert(ofnode_valid(node));
1345 ut_asserteq_str("sandbox-other2",
1346 ofnode_read_string(node, "compatible"));
1347
1348 return 0;
1349}
Simon Glass3a1fc172023-09-26 08:14:38 -06001350DM_TEST(dm_test_livetree_ensure, UT_TESTF_SCAN_FDT);
Simon Glasse0c3c212023-09-26 08:14:40 -06001351
1352static int dm_test_oftree_new(struct unit_test_state *uts)
1353{
1354 ofnode node, subnode, check;
1355 oftree tree;
1356
1357 ut_assertok(oftree_new(&tree));
1358 node = oftree_root(tree);
1359 ut_assert(ofnode_valid(node));
1360 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
1361 check = ofnode_find_subnode(node, "edmund");
1362 ut_asserteq(check.of_offset, subnode.of_offset);
1363
1364 return 0;
1365}
1366DM_TEST(dm_test_oftree_new, UT_TESTF_SCAN_FDT);
Simon Glassc15862f2023-09-26 08:14:41 -06001367
1368static int check_copy_node(struct unit_test_state *uts, ofnode dst, ofnode src,
1369 ofnode *nodep)
1370{
1371 u32 reg[2], val;
1372 ofnode node;
1373
1374 ut_assertok(ofnode_copy_node(dst, "copy-test", src, &node));
1375
1376 ut_assertok(ofnode_read_u32(node, "ping-expect", &val));
1377 ut_asserteq(3, val);
1378
1379 ut_asserteq_str("denx,u-boot-fdt-test",
1380 ofnode_read_string(node, "compatible"));
1381
1382 /* check that a property with the same name is overwritten */
1383 ut_assertok(ofnode_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg)));
1384 ut_asserteq(3, reg[0]);
1385 ut_asserteq(1, reg[1]);
1386
1387 /* reset the compatible so the live tree does not change */
1388 ut_assertok(ofnode_write_string(node, "compatible", "nothing"));
1389 *nodep = node;
1390
1391 return 0;
1392}
1393
1394static int dm_test_ofnode_copy_node(struct unit_test_state *uts)
1395{
1396 ofnode src, dst, node, try;
1397
1398 /*
1399 * These nodes are chosen so that the src node is before the destination
1400 * node in the tree. This doesn't matter with livetree, but with
1401 * flattree any attempt to insert a property earlier in the tree will
1402 * mess up the offsets after it.
1403 */
1404 src = ofnode_path("/b-test");
1405 dst = ofnode_path("/some-bus");
1406
1407 ut_assertok(check_copy_node(uts, dst, src, &node));
1408
1409 /* check trying to copy over an existing node */
1410 ut_asserteq(-EEXIST, ofnode_copy_node(dst, "copy-test", src, &try));
1411 ut_asserteq(try.of_offset, node.of_offset);
1412
1413 return 0;
1414}
1415DM_TEST(dm_test_ofnode_copy_node, UT_TESTF_SCAN_FDT);
1416
1417/* test ofnode_copy_node() with the 'other' tree */
1418static int dm_test_ofnode_copy_node_ot(struct unit_test_state *uts)
1419{
1420 oftree otree = get_other_oftree(uts);
1421 ofnode src, dst, node;
1422
1423 src = ofnode_path("/b-test");
1424 dst = oftree_path(otree, "/node/subnode2");
1425 ut_assertok(check_copy_node(uts, dst, src, &node));
1426
1427 return 0;
1428}
1429DM_TEST(dm_test_ofnode_copy_node_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass67fb2152023-09-26 08:14:42 -06001430
1431static int dm_test_ofnode_delete(struct unit_test_state *uts)
1432{
1433 ofnode node;
1434
1435 /*
1436 * At present the livetree is not restored after changes made in tests.
1437 * See test_pre_run() for how this is done with the other FDT and
1438 * dm_test_pre_run() where it sets up the root-tree pointer. So use
1439 * nodes which don't matter to other tests.
1440 *
1441 * We could fix this by detecting livetree changes and regenerating it
1442 * before the next test if needed.
1443 */
1444 node = ofnode_path("/leds/iracibble");
1445 ut_assert(ofnode_valid(node));
1446 ut_assertok(ofnode_delete(&node));
1447 ut_assert(!ofnode_valid(node));
1448 ut_assert(!ofnode_valid(ofnode_path("/leds/iracibble")));
1449
1450 node = ofnode_path("/leds/default_on");
1451 ut_assert(ofnode_valid(node));
1452 ut_assertok(ofnode_delete(&node));
1453 ut_assert(!ofnode_valid(node));
1454 ut_assert(!ofnode_valid(ofnode_path("/leds/default_on")));
1455
1456 ut_asserteq(2, ofnode_get_child_count(ofnode_path("/leds")));
1457
1458 return 0;
1459}
1460DM_TEST(dm_test_ofnode_delete, UT_TESTF_SCAN_FDT);
Simon Glass62b1db32023-09-26 08:14:43 -06001461
1462static int dm_test_oftree_to_fdt(struct unit_test_state *uts)
1463{
1464 oftree tree, check;
1465 struct abuf buf, buf2;
1466
1467 tree = oftree_default();
1468 ut_assertok(oftree_to_fdt(tree, &buf));
1469 ut_assert(abuf_size(&buf) > SZ_16K);
1470
1471 /* convert it back to a tree and see if it looks OK */
1472 check = oftree_from_fdt(abuf_data(&buf));
1473 ut_assert(oftree_valid(check));
1474
1475 ut_assertok(oftree_to_fdt(check, &buf2));
1476 ut_assert(abuf_size(&buf2) > SZ_16K);
1477 ut_asserteq(abuf_size(&buf), abuf_size(&buf2));
1478 ut_asserteq_mem(abuf_data(&buf), abuf_data(&buf2), abuf_size(&buf));
1479
1480 return 0;
1481}
1482DM_TEST(dm_test_oftree_to_fdt, UT_TESTF_SCAN_FDT);