Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | f912a72 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 2 | /* |
| 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 Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 18 | |
Simon Glass | ebbe90b | 2023-09-26 08:14:43 -0600 | [diff] [blame] | 19 | #include <abuf.h> |
Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 20 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 21 | #include <log.h> |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 22 | #include <of_live.h> |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 23 | #include <dm/device-internal.h> |
| 24 | #include <dm/lists.h> |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 25 | #include <dm/of_extra.h> |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 26 | #include <dm/root.h> |
Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 27 | #include <dm/test.h> |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 28 | #include <dm/uclass-internal.h> |
Simon Glass | ebbe90b | 2023-09-26 08:14:43 -0600 | [diff] [blame] | 29 | #include <linux/sizes.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 30 | #include <test/test.h> |
Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 31 | #include <test/ut.h> |
| 32 | |
Simon Glass | f912a72 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 33 | /** |
| 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 | */ |
| 39 | oftree get_other_oftree(struct unit_test_state *uts) |
| 40 | { |
| 41 | oftree tree; |
| 42 | |
| 43 | if (of_live_active()) |
| 44 | tree = oftree_from_np(uts->of_other); |
| 45 | else |
| 46 | tree = oftree_from_fdt(uts->other_fdt); |
| 47 | |
| 48 | /* An invalid tree may cause failure or crashes */ |
| 49 | if (!oftree_valid(tree)) |
| 50 | ut_reportf("test needs the UT_TESTF_OTHER_FDT flag"); |
| 51 | |
| 52 | return tree; |
| 53 | } |
| 54 | |
Simon Glass | dad9751 | 2022-09-06 20:27:29 -0600 | [diff] [blame] | 55 | /** |
| 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 | */ |
| 65 | int 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 | */ |
| 89 | void free_oftree(oftree tree) |
| 90 | { |
| 91 | if (of_live_active()) |
| 92 | free(tree.np); |
| 93 | } |
| 94 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 95 | /* test ofnode_device_is_compatible() */ |
Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 96 | static 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 Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 105 | DM_TEST(dm_test_ofnode_compatible, |
| 106 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 107 | |
| 108 | /* check ofnode_device_is_compatible() with the 'other' FDT */ |
| 109 | static 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 Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 119 | DM_TEST(dm_test_ofnode_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 120 | |
Patrick Delaunay | 04fcfe7 | 2020-09-24 17:26:20 +0200 | [diff] [blame] | 121 | static 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 Glass | 95fd209 | 2022-09-06 20:27:22 -0600 | [diff] [blame] | 133 | ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1))); |
| 134 | |
Patrick Delaunay | 04fcfe7 | 2020-09-24 17:26:20 +0200 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 138 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 139 | /* test oftree_get_by_phandle() with a the 'other' oftree */ |
Simon Glass | f912a72 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 140 | static 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 Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 152 | DM_TEST(dm_test_ofnode_get_by_phandle_ot, |
| 153 | UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | f912a72 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 154 | |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 155 | static int check_prop_values(struct unit_test_state *uts, ofnode start, |
| 156 | const char *propname, const char *propval, |
| 157 | int expect_count) |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 158 | { |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 159 | int proplen = strlen(propval) + 1; |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 160 | const char *str; |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 161 | ofnode node; |
| 162 | int count; |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 163 | |
| 164 | /* Find first matching node, there should be at least one */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 165 | node = ofnode_by_prop_value(start, propname, propval, proplen); |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 166 | 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 Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 171 | count = 1; |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 172 | while (true) { |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 173 | node = ofnode_by_prop_value(node, propname, propval, proplen); |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 174 | if (!ofnode_valid(node)) |
| 175 | break; |
| 176 | str = ofnode_read_string(node, propname); |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 177 | ut_asserteq_str(propval, str); |
| 178 | count++; |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 179 | } |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 180 | ut_asserteq(expect_count, count); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static 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 Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 192 | DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT); |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 193 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 194 | /* test ofnode_by_prop_value() with a the 'other' oftree */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 195 | static 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 Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 204 | DM_TEST(dm_test_ofnode_by_prop_value_ot, |
| 205 | UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 206 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 207 | /* test ofnode_read_fmap_entry() */ |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 208 | static 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 Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 221 | DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 222 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 223 | /* test ofnode_read_prop() */ |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 224 | static int dm_test_ofnode_read(struct unit_test_state *uts) |
| 225 | { |
| 226 | const u32 *val; |
| 227 | ofnode node; |
| 228 | int size; |
| 229 | |
Simon Glass | 310487d | 2023-09-26 08:14:46 -0600 | [diff] [blame] | 230 | node = oftree_path(oftree_default(), "/"); |
| 231 | ut_assert(ofnode_valid(node)); |
| 232 | |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 233 | 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 Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 251 | DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_FDT); |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 252 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 253 | /* test ofnode_read_prop() with the 'other' tree */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 254 | static 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 Glass | 310487d | 2023-09-26 08:14:46 -0600 | [diff] [blame] | 261 | node = oftree_path(otree, "/"); |
| 262 | ut_assert(ofnode_valid(node)); |
| 263 | |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 264 | 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 Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 274 | DM_TEST(dm_test_ofnode_read_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 275 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 276 | /* test ofnode_count_/parse_phandle_with_args() */ |
Patrick Delaunay | 8cd2801 | 2020-09-25 09:41:16 +0200 | [diff] [blame] | 277 | static 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 | } |
| 350 | DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 351 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 352 | /* test ofnode_count_/parse_phandle_with_args() with 'other' tree */ |
Simon Glass | f912a72 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 353 | static 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 | } |
| 379 | DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT); |
| 380 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 381 | /* test ofnode_read_chosen_string/node/prop() */ |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 382 | static int dm_test_ofnode_read_chosen(struct unit_test_state *uts) |
| 383 | { |
| 384 | const char *str; |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 385 | const u32 *val; |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 386 | ofnode node; |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 387 | int size; |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 388 | |
| 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 Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 401 | 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 Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 407 | return 0; |
| 408 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 409 | DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
developer | cf8bc13 | 2020-05-02 11:35:10 +0200 | [diff] [blame] | 410 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 411 | /* test ofnode_get_aliases_node/prop() */ |
Michal Simek | 92a8862 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 412 | static int dm_test_ofnode_read_aliases(struct unit_test_state *uts) |
| 413 | { |
| 414 | const void *val; |
| 415 | ofnode node; |
| 416 | int size; |
| 417 | |
Michael Walle | 7efcdfd | 2021-02-25 16:51:11 +0100 | [diff] [blame] | 418 | node = ofnode_get_aliases_node("ethernet3"); |
Michal Simek | 92a8862 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 419 | 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 | } |
| 432 | DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 433 | |
developer | cf8bc13 | 2020-05-02 11:35:10 +0200 | [diff] [blame] | 434 | static 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 | } |
| 452 | DM_TEST(dm_test_ofnode_get_child_count, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 453 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 5de5b3b | 2020-11-28 17:50:02 -0700 | [diff] [blame] | 454 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 455 | /* test ofnode_get_child_count() with 'other' tree */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 456 | static 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 Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 475 | DM_TEST(dm_test_ofnode_get_child_count_ot, |
| 476 | UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 477 | |
Simon Glass | 5de5b3b | 2020-11-28 17:50:02 -0700 | [diff] [blame] | 478 | static 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 | } |
| 488 | DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Chen Guanqiao | be0512f | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 489 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 490 | /* test ofnode_is_enabled() with 'other' tree */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 491 | static 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 | } |
| 502 | DM_TEST(dm_test_ofnode_is_enabled_ot, UT_TESTF_OTHER_FDT); |
| 503 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 504 | /* test ofnode_get_addr/size() */ |
Chen Guanqiao | be0512f | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 505 | static 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 Chotard | b4c5211 | 2022-01-04 08:42:48 +0100 | [diff] [blame] | 529 | ut_asserteq_64(FDT_ADDR_T_NONE, addr); |
Chen Guanqiao | be0512f | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 530 | ut_asserteq(FDT_SIZE_T_NONE, size); |
| 531 | |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 532 | 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 Guanqiao | be0512f | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Marek Behún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 540 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 541 | /* test ofnode_get_addr() with 'other' tree */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 542 | static 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 Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 553 | DM_TEST(dm_test_ofnode_get_reg_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 554 | |
Marek Behún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 555 | static 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ún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 560 | |
| 561 | node = ofnode_path(path); |
| 562 | ut_assert(ofnode_valid(node)); |
| 563 | |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 564 | ut_assertok(ofnode_get_path(node, buf, sizeof(buf))); |
Marek Behún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 565 | ut_asserteq_str(path, buf); |
| 566 | |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 567 | ut_asserteq(-ENOSPC, ofnode_get_path(node, buf, 32)); |
Marek Behún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 568 | |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 569 | ut_assertok(ofnode_get_path(ofnode_root(), buf, 32)); |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 570 | ut_asserteq_str("/", buf); |
| 571 | |
Marek Behún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 575 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 576 | /* test ofnode_get_path() with 'other' tree */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 577 | static 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 Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 594 | DM_TEST(dm_test_ofnode_get_path_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 595 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 596 | /* test ofnode_conf_read_bool/int/str() */ |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 597 | static 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 Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 610 | DM_TEST(dm_test_ofnode_conf, UT_TESTF_SCAN_FDT); |
Michael Walle | f1f8740 | 2021-10-15 15:15:18 +0200 | [diff] [blame] | 611 | |
Michal Simek | 43c42bd | 2023-08-31 08:59:05 +0200 | [diff] [blame] | 612 | static int dm_test_ofnode_options(struct unit_test_state *uts) |
| 613 | { |
Michal Simek | 6a7c1ce | 2023-08-31 09:04:27 +0200 | [diff] [blame] | 614 | u64 bootscr_address, bootscr_offset; |
| 615 | u64 bootscr_flash_offset, bootscr_flash_size; |
Michal Simek | 43c42bd | 2023-08-31 08:59:05 +0200 | [diff] [blame] | 616 | |
| 617 | ut_assertok(ofnode_read_bootscript_address(&bootscr_address, |
| 618 | &bootscr_offset)); |
| 619 | ut_asserteq_64(0, bootscr_address); |
| 620 | ut_asserteq_64(0x12345678, bootscr_offset); |
| 621 | |
Michal Simek | 6a7c1ce | 2023-08-31 09:04:27 +0200 | [diff] [blame] | 622 | ut_assertok(ofnode_read_bootscript_flash(&bootscr_flash_offset, |
| 623 | &bootscr_flash_size)); |
| 624 | ut_asserteq_64(0, bootscr_flash_offset); |
| 625 | ut_asserteq_64(0x2000, bootscr_flash_size); |
| 626 | |
Michal Simek | 43c42bd | 2023-08-31 08:59:05 +0200 | [diff] [blame] | 627 | return 0; |
| 628 | } |
| 629 | DM_TEST(dm_test_ofnode_options, 0); |
| 630 | |
Michael Walle | f1f8740 | 2021-10-15 15:15:18 +0200 | [diff] [blame] | 631 | static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts) |
| 632 | { |
| 633 | const char compatible[] = "denx,u-boot-fdt-test"; |
| 634 | bool found = false; |
| 635 | ofnode node; |
| 636 | |
| 637 | ofnode_for_each_compatible_node(node, compatible) { |
| 638 | ut_assert(ofnode_device_is_compatible(node, compatible)); |
| 639 | found = true; |
| 640 | } |
| 641 | |
| 642 | /* There should be at least one matching node */ |
| 643 | ut_assert(found); |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 648 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 649 | /* test dm_test_ofnode_string_count/index/list() */ |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 650 | static int dm_test_ofnode_string(struct unit_test_state *uts) |
| 651 | { |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 652 | const char **val; |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 653 | const char *out; |
| 654 | ofnode node; |
| 655 | |
| 656 | node = ofnode_path("/a-test"); |
| 657 | ut_assert(ofnode_valid(node)); |
| 658 | |
| 659 | /* single string */ |
| 660 | ut_asserteq(1, ofnode_read_string_count(node, "str-value")); |
| 661 | ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out)); |
| 662 | ut_asserteq_str("test string", out); |
| 663 | ut_asserteq(0, ofnode_stringlist_search(node, "str-value", |
| 664 | "test string")); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 665 | ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val)); |
| 666 | ut_asserteq_str("test string", val[0]); |
| 667 | ut_assertnull(val[1]); |
| 668 | free(val); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 669 | |
| 670 | /* list of strings */ |
| 671 | ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names")); |
| 672 | ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0, |
| 673 | &out)); |
| 674 | ut_asserteq_str("mux0", out); |
| 675 | ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names", |
| 676 | "mux0")); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 677 | ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names", |
| 678 | &val)); |
| 679 | ut_asserteq_str("mux0", val[0]); |
| 680 | ut_asserteq_str("mux1", val[1]); |
| 681 | ut_asserteq_str("mux2", val[2]); |
| 682 | ut_asserteq_str("mux3", val[3]); |
| 683 | ut_asserteq_str("mux4", val[4]); |
| 684 | ut_assertnull(val[5]); |
| 685 | free(val); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 686 | |
| 687 | ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4, |
| 688 | &out)); |
| 689 | ut_asserteq_str("mux4", out); |
| 690 | ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names", |
| 691 | "mux4")); |
| 692 | |
| 693 | return 0; |
| 694 | } |
Simon Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 695 | DM_TEST(dm_test_ofnode_string, UT_TESTF_SCAN_FDT); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 696 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 697 | /* test error returns from ofnode_read_string_count/index/list() */ |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 698 | static int dm_test_ofnode_string_err(struct unit_test_state *uts) |
| 699 | { |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 700 | const char **val; |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 701 | const char *out; |
| 702 | ofnode node; |
| 703 | |
| 704 | /* |
| 705 | * Test error codes only on livetree, as they are different with |
| 706 | * flattree |
| 707 | */ |
| 708 | node = ofnode_path("/a-test"); |
| 709 | ut_assert(ofnode_valid(node)); |
| 710 | |
| 711 | /* non-existent property */ |
| 712 | ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing")); |
| 713 | ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0, |
| 714 | &out)); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 715 | ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val)); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 716 | |
| 717 | /* empty property */ |
| 718 | ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value")); |
| 719 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0, |
| 720 | &out)); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 721 | ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value", |
| 722 | &val)); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 723 | |
| 724 | /* badly formatted string list */ |
| 725 | ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value")); |
| 726 | ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0, |
| 727 | &out)); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 728 | ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value", |
| 729 | &val)); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 730 | |
| 731 | /* out of range / not found */ |
| 732 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1, |
| 733 | &out)); |
| 734 | ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value", |
| 735 | "other")); |
| 736 | |
| 737 | /* negative value for index is not allowed, so don't test for that */ |
| 738 | |
| 739 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, |
| 740 | "mux-control-names", 5, |
| 741 | &out)); |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE); |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 746 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 747 | static int dm_test_ofnode_read_phy_mode(struct unit_test_state *uts) |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 748 | { |
| 749 | ofnode eth_node, phy_node; |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 750 | phy_interface_t mode; |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 751 | u32 reg; |
| 752 | |
| 753 | eth_node = ofnode_path("/phy-test-eth"); |
| 754 | ut_assert(ofnode_valid(eth_node)); |
| 755 | |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 756 | mode = ofnode_read_phy_mode(eth_node); |
| 757 | ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX); |
| 758 | |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 759 | phy_node = ofnode_get_phy_node(eth_node); |
| 760 | ut_assert(ofnode_valid(phy_node)); |
| 761 | |
| 762 | reg = ofnode_read_u32_default(phy_node, "reg", -1U); |
| 763 | ut_asserteq_64(0x1, reg); |
| 764 | |
| 765 | return 0; |
| 766 | } |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 767 | DM_TEST(dm_test_ofnode_read_phy_mode, UT_TESTF_SCAN_FDT); |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 768 | |
| 769 | /** |
| 770 | * make_ofnode_fdt() - Create an FDT for testing with ofnode |
| 771 | * |
| 772 | * The size is set to the minimum needed |
| 773 | * |
| 774 | * @uts: Test state |
| 775 | * @fdt: Place to write FDT |
| 776 | * @size: Maximum size of space for fdt |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 777 | * @id: id value to add to the tree ('id' property in root node) |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 778 | */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 779 | static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size, |
| 780 | int id) |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 781 | { |
| 782 | ut_assertok(fdt_create(fdt, size)); |
| 783 | ut_assertok(fdt_finish_reservemap(fdt)); |
| 784 | ut_assert(fdt_begin_node(fdt, "") >= 0); |
| 785 | |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 786 | ut_assertok(fdt_property_u32(fdt, "id", id)); |
| 787 | |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 788 | ut_assert(fdt_begin_node(fdt, "aliases") >= 0); |
| 789 | ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc")); |
| 790 | ut_assertok(fdt_end_node(fdt)); |
| 791 | |
| 792 | ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0); |
| 793 | ut_assertok(fdt_end_node(fdt)); |
| 794 | |
| 795 | ut_assertok(fdt_end_node(fdt)); |
| 796 | ut_assertok(fdt_finish(fdt)); |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 801 | /* Check that aliases work on the control FDT */ |
| 802 | static int dm_test_ofnode_aliases(struct unit_test_state *uts) |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 803 | { |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 804 | ofnode node; |
| 805 | |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 806 | node = ofnode_get_aliases_node("ethernet3"); |
| 807 | ut_assert(ofnode_valid(node)); |
| 808 | ut_asserteq_str("sbe5", ofnode_get_name(node)); |
| 809 | |
Simon Glass | 2b9b1458 | 2022-09-06 20:27:21 -0600 | [diff] [blame] | 810 | ut_assert(!oftree_valid(oftree_null())); |
| 811 | |
Simon Glass | c759944 | 2022-10-20 18:22:49 -0600 | [diff] [blame] | 812 | return 0; |
| 813 | } |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 814 | DM_TEST(dm_test_ofnode_aliases, UT_TESTF_SCAN_FDT); |
Simon Glass | dad9751 | 2022-09-06 20:27:29 -0600 | [diff] [blame] | 815 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 816 | /** |
| 817 | * dm_test_ofnode_root_mult() - Check aliaes on control and 'other' tree |
| 818 | * |
| 819 | * Check that aliases work only with the control FDT, not with 'other' tree. |
| 820 | * This is not actually the desired behaviour. If aliases are implemented for |
| 821 | * any tree, then this test should be changed. |
| 822 | */ |
Simon Glass | c759944 | 2022-10-20 18:22:49 -0600 | [diff] [blame] | 823 | static int dm_test_ofnode_root_mult(struct unit_test_state *uts) |
| 824 | { |
| 825 | char fdt[256]; |
| 826 | oftree tree; |
| 827 | ofnode node; |
Simon Glass | dad9751 | 2022-09-06 20:27:29 -0600 | [diff] [blame] | 828 | |
Simon Glass | c759944 | 2022-10-20 18:22:49 -0600 | [diff] [blame] | 829 | /* skip this test if multiple FDTs are not supported */ |
| 830 | if (!IS_ENABLED(CONFIG_OFNODE_MULTI_TREE)) |
| 831 | return -EAGAIN; |
| 832 | |
| 833 | ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt), 0)); |
| 834 | ut_assertok(get_oftree(uts, fdt, &tree)); |
Simon Glass | 2b9b1458 | 2022-09-06 20:27:21 -0600 | [diff] [blame] | 835 | ut_assert(oftree_valid(tree)); |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 836 | |
| 837 | /* Make sure they don't work on this new tree */ |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 838 | node = oftree_path(tree, "mmc0"); |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 839 | ut_assert(!ofnode_valid(node)); |
| 840 | |
| 841 | /* It should appear in the new tree */ |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 842 | node = oftree_path(tree, "/new-mmc"); |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 843 | ut_assert(ofnode_valid(node)); |
| 844 | |
| 845 | /* ...and not in the control FDT */ |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 846 | node = oftree_path(oftree_default(), "/new-mmc"); |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 847 | ut_assert(!ofnode_valid(node)); |
| 848 | |
Simon Glass | dad9751 | 2022-09-06 20:27:29 -0600 | [diff] [blame] | 849 | free_oftree(tree); |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 850 | |
| 851 | return 0; |
| 852 | } |
Simon Glass | c759944 | 2022-10-20 18:22:49 -0600 | [diff] [blame] | 853 | DM_TEST(dm_test_ofnode_root_mult, UT_TESTF_SCAN_FDT); |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 854 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 855 | /* test ofnode_set_enabled(), ofnode_write_prop() on a livetree */ |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 856 | static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts) |
| 857 | { |
| 858 | struct udevice *dev; |
| 859 | ofnode node; |
| 860 | |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 861 | /* Test enabling devices */ |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 862 | node = ofnode_path("/usb@2"); |
| 863 | |
Simon Glass | 3ee3d15 | 2022-07-30 15:52:13 -0600 | [diff] [blame] | 864 | ut_assert(!ofnode_is_enabled(node)); |
| 865 | ut_assertok(ofnode_set_enabled(node, true)); |
| 866 | ut_asserteq(true, ofnode_is_enabled(node)); |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 867 | |
| 868 | device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node, |
| 869 | &dev); |
| 870 | ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev)); |
| 871 | |
| 872 | /* Test string property setting */ |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 873 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 874 | ofnode_write_string(node, "compatible", "gdsys,super-usb"); |
| 875 | ut_assert(device_is_compatible(dev, "gdsys,super-usb")); |
| 876 | ofnode_write_string(node, "compatible", "sandbox,usb"); |
| 877 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 878 | |
| 879 | /* Test setting generic properties */ |
| 880 | |
| 881 | /* Non-existent in DTB */ |
| 882 | ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev)); |
| 883 | /* reg = 0x42, size = 0x100 */ |
Simon Glass | 5e2cd5e | 2022-07-30 15:52:10 -0600 | [diff] [blame] | 884 | ut_assertok(ofnode_write_prop(node, "reg", |
Simon Glass | 17abed0 | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 885 | "\x00\x00\x00\x42\x00\x00\x01\x00", 8, |
| 886 | false)); |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 887 | ut_asserteq(0x42, dev_read_addr(dev)); |
| 888 | |
| 889 | /* Test disabling devices */ |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 890 | device_remove(dev, DM_REMOVE_NORMAL); |
| 891 | device_unbind(dev); |
| 892 | |
Simon Glass | 3ee3d15 | 2022-07-30 15:52:13 -0600 | [diff] [blame] | 893 | ut_assert(ofnode_is_enabled(node)); |
| 894 | ut_assertok(ofnode_set_enabled(node, false)); |
| 895 | ut_assert(!ofnode_is_enabled(node)); |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 896 | |
| 897 | return 0; |
| 898 | } |
Simon Glass | b75dc16 | 2022-07-30 15:52:11 -0600 | [diff] [blame] | 899 | DM_TEST(dm_test_ofnode_livetree_writing, |
Simon Glass | 8ae9f96 | 2022-09-06 20:27:07 -0600 | [diff] [blame] | 900 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | d28e31e | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 901 | |
Simon Glass | 17abed0 | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 902 | static int check_write_prop(struct unit_test_state *uts, ofnode node) |
| 903 | { |
| 904 | char prop[] = "middle-name"; |
| 905 | char name[10]; |
| 906 | int len; |
| 907 | |
| 908 | strcpy(name, "cecil"); |
| 909 | len = strlen(name) + 1; |
| 910 | ut_assertok(ofnode_write_prop(node, prop, name, len, false)); |
| 911 | ut_asserteq_str(name, ofnode_read_string(node, prop)); |
| 912 | |
| 913 | /* change the underlying value, this should mess up the live tree */ |
| 914 | strcpy(name, "tony"); |
| 915 | if (of_live_active()) { |
| 916 | ut_asserteq_str(name, ofnode_read_string(node, prop)); |
| 917 | } else { |
| 918 | ut_asserteq_str("cecil", ofnode_read_string(node, prop)); |
| 919 | } |
| 920 | |
| 921 | /* try again, this time copying the property */ |
| 922 | strcpy(name, "mary"); |
| 923 | ut_assertok(ofnode_write_prop(node, prop, name, len, true)); |
| 924 | ut_asserteq_str(name, ofnode_read_string(node, prop)); |
| 925 | strcpy(name, "leah"); |
| 926 | |
| 927 | /* both flattree and livetree behave the same */ |
| 928 | ut_asserteq_str("mary", ofnode_read_string(node, prop)); |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | /* writing the tree with and without copying the property */ |
| 934 | static int dm_test_ofnode_write_copy(struct unit_test_state *uts) |
| 935 | { |
| 936 | ofnode node; |
| 937 | |
| 938 | node = ofnode_path("/a-test"); |
| 939 | ut_assertok(check_write_prop(uts, node)); |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | DM_TEST(dm_test_ofnode_write_copy, UT_TESTF_SCAN_FDT); |
| 944 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 945 | /* test writing a property to the 'other' tree */ |
Simon Glass | 17abed0 | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 946 | static int dm_test_ofnode_write_copy_ot(struct unit_test_state *uts) |
| 947 | { |
| 948 | oftree otree = get_other_oftree(uts); |
| 949 | ofnode node, check_node; |
| 950 | |
| 951 | node = oftree_path(otree, "/node"); |
| 952 | ut_assertok(check_write_prop(uts, node)); |
| 953 | |
| 954 | /* make sure the control FDT is not touched */ |
| 955 | check_node = ofnode_path("/node"); |
| 956 | ut_assertnull(ofnode_read_string(check_node, "middle-name")); |
| 957 | |
| 958 | return 0; |
| 959 | } |
Simon Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 960 | DM_TEST(dm_test_ofnode_write_copy_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | 17abed0 | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 961 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 962 | /* test ofnode_read_u32_index/default() */ |
Simon Glass | d28e31e | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 963 | static int dm_test_ofnode_u32(struct unit_test_state *uts) |
| 964 | { |
| 965 | ofnode node; |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 966 | u32 val; |
Simon Glass | d28e31e | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 967 | |
| 968 | node = ofnode_path("/lcd"); |
| 969 | ut_assert(ofnode_valid(node)); |
| 970 | ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123)); |
| 971 | ut_assertok(ofnode_write_u32(node, "xres", 1367)); |
| 972 | ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123)); |
| 973 | ut_assertok(ofnode_write_u32(node, "xres", 1366)); |
| 974 | |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 975 | node = ofnode_path("/backlight"); |
| 976 | ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val)); |
| 977 | ut_asserteq(0, val); |
| 978 | ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val)); |
| 979 | ut_asserteq(16, val); |
| 980 | ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val)); |
| 981 | ut_asserteq(255, val); |
| 982 | ut_asserteq(-EOVERFLOW, |
| 983 | ofnode_read_u32_index(node, "brightness-levels", 9, &val)); |
| 984 | ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val)); |
| 985 | |
Simon Glass | d28e31e | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 986 | return 0; |
| 987 | } |
Simon Glass | 8ae9f96 | 2022-09-06 20:27:07 -0600 | [diff] [blame] | 988 | DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 56bc332 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 989 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 990 | /* test ofnode_read_u32_array() */ |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 991 | static int dm_test_ofnode_u32_array(struct unit_test_state *uts) |
| 992 | { |
| 993 | ofnode node; |
| 994 | u32 val[10]; |
| 995 | |
| 996 | node = ofnode_path("/a-test"); |
| 997 | ut_assert(ofnode_valid(node)); |
| 998 | ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1)); |
| 999 | ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1)); |
| 1000 | ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val, |
| 1001 | 1)); |
| 1002 | |
| 1003 | memset(val, '\0', sizeof(val)); |
| 1004 | ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3)); |
| 1005 | ut_asserteq(0, val[0]); |
| 1006 | ut_asserteq(5678, val[1]); |
| 1007 | ut_asserteq(9123, val[2]); |
| 1008 | ut_asserteq(4567, val[3]); |
| 1009 | ut_asserteq(0, val[4]); |
| 1010 | ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val, |
| 1011 | 4)); |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 1016 | |
Simon Glass | c681e09 | 2023-09-26 08:14:45 -0600 | [diff] [blame] | 1017 | /* test ofnode_read_u64() and ofnode_write_u64() */ |
| 1018 | static int dm_test_ofnode_u64(struct unit_test_state *uts) |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1019 | { |
| 1020 | ofnode node; |
| 1021 | u64 val; |
| 1022 | |
| 1023 | node = ofnode_path("/a-test"); |
| 1024 | ut_assert(ofnode_valid(node)); |
| 1025 | ut_assertok(ofnode_read_u64(node, "int64-value", &val)); |
| 1026 | ut_asserteq_64(0x1111222233334444, val); |
Simon Glass | c681e09 | 2023-09-26 08:14:45 -0600 | [diff] [blame] | 1027 | ut_assertok(ofnode_write_u64(node, "new-int64-value", 0x9876543210)); |
| 1028 | ut_assertok(ofnode_read_u64(node, "new-int64-value", &val)); |
| 1029 | ut_asserteq_64(0x9876543210, val); |
| 1030 | |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1031 | ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val)); |
| 1032 | |
Michal Simek | 08a194e | 2023-08-25 11:37:46 +0200 | [diff] [blame] | 1033 | ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val)); |
| 1034 | ut_asserteq_64(0x1111222233334444, val); |
| 1035 | ut_assertok(ofnode_read_u64_index(node, "int64-array", 1, &val)); |
| 1036 | ut_asserteq_64(0x4444333322221111, val); |
| 1037 | ut_asserteq(-EOVERFLOW, |
| 1038 | ofnode_read_u64_index(node, "int64-array", 2, &val)); |
| 1039 | ut_asserteq(-EINVAL, ofnode_read_u64_index(node, "missing", 0, &val)); |
| 1040 | |
Simon Glass | c681e09 | 2023-09-26 08:14:45 -0600 | [diff] [blame] | 1041 | ut_assertok(ofnode_write_u64(node, "int64-array", 0x9876543210)); |
| 1042 | ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val)); |
| 1043 | ut_asserteq_64(0x9876543210, val); |
| 1044 | ut_asserteq(-EOVERFLOW, |
| 1045 | ofnode_read_u64_index(node, "int64-array", 1, &val)); |
| 1046 | |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1047 | return 0; |
| 1048 | } |
Simon Glass | c681e09 | 2023-09-26 08:14:45 -0600 | [diff] [blame] | 1049 | DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT); |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1050 | |
Simon Glass | 56bc332 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 1051 | static int dm_test_ofnode_add_subnode(struct unit_test_state *uts) |
| 1052 | { |
| 1053 | ofnode node, check, subnode; |
| 1054 | char buf[128]; |
| 1055 | |
Simon Glass | 56bc332 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 1056 | node = ofnode_path("/lcd"); |
| 1057 | ut_assert(ofnode_valid(node)); |
| 1058 | ut_assertok(ofnode_add_subnode(node, "edmund", &subnode)); |
| 1059 | check = ofnode_path("/lcd/edmund"); |
| 1060 | ut_asserteq(subnode.of_offset, check.of_offset); |
| 1061 | ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf))); |
| 1062 | ut_asserteq_str("/lcd/edmund", buf); |
| 1063 | |
| 1064 | if (of_live_active()) { |
| 1065 | struct device_node *child; |
| 1066 | |
| 1067 | ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund", |
| 1068 | 2, &child)); |
| 1069 | ut_asserteq_str("ed", child->name); |
| 1070 | ut_asserteq_str("/lcd/ed", child->full_name); |
| 1071 | check = ofnode_path("/lcd/ed"); |
| 1072 | ut_asserteq_ptr(child, check.np); |
| 1073 | ut_assertok(ofnode_get_path(np_to_ofnode(child), buf, |
| 1074 | sizeof(buf))); |
| 1075 | ut_asserteq_str("/lcd/ed", buf); |
| 1076 | } |
| 1077 | |
| 1078 | /* An existing node should be returned with -EEXIST */ |
| 1079 | ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check)); |
| 1080 | ut_asserteq(subnode.of_offset, check.of_offset); |
| 1081 | |
| 1082 | /* add a root node */ |
| 1083 | node = ofnode_path("/"); |
| 1084 | ut_assert(ofnode_valid(node)); |
| 1085 | ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode)); |
| 1086 | check = ofnode_path("/lcd2"); |
| 1087 | ut_asserteq(subnode.of_offset, check.of_offset); |
| 1088 | ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf))); |
| 1089 | ut_asserteq_str("/lcd2", buf); |
| 1090 | |
| 1091 | if (of_live_active()) { |
| 1092 | ulong start; |
| 1093 | int i; |
| 1094 | |
| 1095 | /* |
| 1096 | * Make sure each of the three malloc()checks in |
| 1097 | * of_add_subnode() work |
| 1098 | */ |
| 1099 | for (i = 0; i < 3; i++) { |
| 1100 | malloc_enable_testing(i); |
| 1101 | start = ut_check_free(); |
| 1102 | ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony", |
| 1103 | &check)); |
| 1104 | ut_assertok(ut_check_delta(start)); |
| 1105 | } |
| 1106 | |
| 1107 | /* This should pass since we allow 3 allocations */ |
| 1108 | malloc_enable_testing(3); |
| 1109 | ut_assertok(ofnode_add_subnode(node, "anthony", &check)); |
| 1110 | malloc_disable_testing(); |
| 1111 | } |
| 1112 | |
Simon Glass | 238afb5 | 2022-09-06 20:27:03 -0600 | [diff] [blame] | 1113 | /* write to the empty node */ |
| 1114 | ut_assertok(ofnode_write_string(subnode, "example", "text")); |
| 1115 | |
Simon Glass | 56bc332 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 1116 | return 0; |
| 1117 | } |
Simon Glass | 8ae9f96 | 2022-09-06 20:27:07 -0600 | [diff] [blame] | 1118 | DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 4caa79a | 2022-09-06 20:27:16 -0600 | [diff] [blame] | 1119 | |
| 1120 | static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts) |
| 1121 | { |
| 1122 | ofnode node, subnode; |
| 1123 | struct ofprop prop; |
| 1124 | int count; |
| 1125 | |
Dzmitry Sankouski | 54f4c83 | 2023-01-22 18:21:23 +0300 | [diff] [blame] | 1126 | node = ofnode_path("/ofnode-foreach"); |
Simon Glass | 4caa79a | 2022-09-06 20:27:16 -0600 | [diff] [blame] | 1127 | count = 0; |
| 1128 | |
| 1129 | /* we expect "compatible" for each node */ |
| 1130 | ofnode_for_each_prop(prop, node) |
| 1131 | count++; |
| 1132 | ut_asserteq(1, count); |
| 1133 | |
| 1134 | /* there are two nodes, each with 2 properties */ |
| 1135 | ofnode_for_each_subnode(subnode, node) |
| 1136 | ofnode_for_each_prop(prop, subnode) |
| 1137 | count++; |
| 1138 | ut_asserteq(5, count); |
| 1139 | |
| 1140 | return 0; |
| 1141 | } |
| 1142 | DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT); |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1143 | |
| 1144 | static int dm_test_ofnode_by_compatible(struct unit_test_state *uts) |
| 1145 | { |
| 1146 | const char *compat = "denx,u-boot-fdt-test"; |
| 1147 | ofnode node; |
| 1148 | int count; |
| 1149 | |
| 1150 | count = 0; |
| 1151 | for (node = ofnode_null(); |
| 1152 | node = ofnode_by_compatible(node, compat), ofnode_valid(node);) |
| 1153 | count++; |
| 1154 | ut_asserteq(11, count); |
| 1155 | |
| 1156 | return 0; |
| 1157 | } |
| 1158 | DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT); |
| 1159 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 1160 | /* check ofnode_by_compatible() on the 'other' tree */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 1161 | static int dm_test_ofnode_by_compatible_ot(struct unit_test_state *uts) |
| 1162 | { |
| 1163 | const char *compat = "sandbox-other2"; |
| 1164 | oftree otree = get_other_oftree(uts); |
| 1165 | ofnode node; |
| 1166 | int count; |
| 1167 | |
| 1168 | count = 0; |
| 1169 | for (node = oftree_root(otree); |
| 1170 | node = ofnode_by_compatible(node, compat), ofnode_valid(node);) |
| 1171 | count++; |
| 1172 | ut_asserteq(2, count); |
| 1173 | |
| 1174 | return 0; |
| 1175 | } |
Simon Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 1176 | DM_TEST(dm_test_ofnode_by_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 1177 | |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1178 | static int dm_test_ofnode_find_subnode(struct unit_test_state *uts) |
| 1179 | { |
| 1180 | ofnode node, subnode; |
| 1181 | |
| 1182 | node = ofnode_path("/buttons"); |
| 1183 | |
| 1184 | subnode = ofnode_find_subnode(node, "btn1"); |
| 1185 | ut_assert(ofnode_valid(subnode)); |
| 1186 | ut_asserteq_str("btn1", ofnode_get_name(subnode)); |
| 1187 | |
| 1188 | subnode = ofnode_find_subnode(node, "btn"); |
| 1189 | ut_assert(!ofnode_valid(subnode)); |
| 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT); |
| 1194 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 1195 | /* test ofnode_find_subnode() on the 'other' tree */ |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 1196 | static int dm_test_ofnode_find_subnode_ot(struct unit_test_state *uts) |
| 1197 | { |
| 1198 | oftree otree = get_other_oftree(uts); |
| 1199 | ofnode node, subnode; |
| 1200 | |
| 1201 | node = oftree_path(otree, "/node"); |
| 1202 | |
| 1203 | subnode = ofnode_find_subnode(node, "subnode"); |
| 1204 | ut_assert(ofnode_valid(subnode)); |
| 1205 | ut_asserteq_str("subnode", ofnode_get_name(subnode)); |
| 1206 | |
| 1207 | subnode = ofnode_find_subnode(node, "btn"); |
| 1208 | ut_assert(!ofnode_valid(subnode)); |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | DM_TEST(dm_test_ofnode_find_subnode_ot, UT_TESTF_OTHER_FDT); |
| 1213 | |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1214 | static int dm_test_ofnode_get_name(struct unit_test_state *uts) |
| 1215 | { |
| 1216 | ofnode node; |
| 1217 | |
| 1218 | node = ofnode_path("/buttons"); |
| 1219 | ut_assert(ofnode_valid(node)); |
| 1220 | ut_asserteq_str("buttons", ofnode_get_name(node)); |
| 1221 | ut_asserteq_str("", ofnode_get_name(ofnode_root())); |
| 1222 | |
| 1223 | return 0; |
| 1224 | } |
| 1225 | DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT); |
Simon Glass | 9e7a42a | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 1226 | |
| 1227 | /* try to access more FDTs than is supported */ |
| 1228 | static int dm_test_ofnode_too_many(struct unit_test_state *uts) |
| 1229 | { |
| 1230 | const int max_trees = CONFIG_IS_ENABLED(OFNODE_MULTI_TREE, |
| 1231 | (CONFIG_OFNODE_MULTI_TREE_MAX), (1)); |
| 1232 | const int fdt_size = 256; |
| 1233 | const int num_trees = max_trees + 1; |
| 1234 | char fdt[num_trees][fdt_size]; |
| 1235 | int i; |
| 1236 | |
| 1237 | for (i = 0; i < num_trees; i++) { |
| 1238 | oftree tree; |
| 1239 | int ret; |
| 1240 | |
| 1241 | ut_assertok(make_ofnode_fdt(uts, fdt[i], fdt_size, i)); |
| 1242 | ret = get_oftree(uts, fdt[i], &tree); |
| 1243 | |
| 1244 | /* |
| 1245 | * With flat tree we have the control FDT using one slot. Live |
| 1246 | * tree has no limit since it uses pointers, not integer tree |
| 1247 | * IDs |
| 1248 | */ |
| 1249 | if (of_live_active() || i < max_trees - 1) { |
| 1250 | ut_assertok(ret); |
| 1251 | } else { |
| 1252 | /* |
| 1253 | * tree should be invalid when we try to register too |
| 1254 | * many trees |
| 1255 | */ |
| 1256 | ut_asserteq(-EOVERFLOW, ret); |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | return 0; |
| 1261 | } |
| 1262 | DM_TEST(dm_test_ofnode_too_many, UT_TESTF_SCAN_FDT); |
Simon Glass | 7a7229a | 2022-09-06 20:27:33 -0600 | [diff] [blame] | 1263 | |
Simon Glass | 6816489 | 2023-09-26 08:14:37 -0600 | [diff] [blame] | 1264 | static int check_copy_props(struct unit_test_state *uts, ofnode dst, ofnode src) |
Simon Glass | 7a7229a | 2022-09-06 20:27:33 -0600 | [diff] [blame] | 1265 | { |
| 1266 | u32 reg[2], val; |
| 1267 | |
Simon Glass | 6816489 | 2023-09-26 08:14:37 -0600 | [diff] [blame] | 1268 | ut_assertok(ofnode_copy_props(dst, src)); |
Simon Glass | 7a7229a | 2022-09-06 20:27:33 -0600 | [diff] [blame] | 1269 | |
| 1270 | ut_assertok(ofnode_read_u32(dst, "ping-expect", &val)); |
| 1271 | ut_asserteq(3, val); |
| 1272 | |
| 1273 | ut_asserteq_str("denx,u-boot-fdt-test", |
| 1274 | ofnode_read_string(dst, "compatible")); |
| 1275 | |
| 1276 | /* check that a property with the same name is overwritten */ |
| 1277 | ut_assertok(ofnode_read_u32_array(dst, "reg", reg, ARRAY_SIZE(reg))); |
| 1278 | ut_asserteq(3, reg[0]); |
| 1279 | ut_asserteq(1, reg[1]); |
| 1280 | |
| 1281 | /* reset the compatible so the live tree does not change */ |
| 1282 | ut_assertok(ofnode_write_string(dst, "compatible", "nothing")); |
| 1283 | |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | static int dm_test_ofnode_copy_props(struct unit_test_state *uts) |
| 1288 | { |
| 1289 | ofnode src, dst; |
| 1290 | |
| 1291 | /* |
| 1292 | * These nodes are chosen so that the src node is before the destination |
| 1293 | * node in the tree. This doesn't matter with livetree, but with |
| 1294 | * flattree any attempt to insert a property earlier in the tree will |
| 1295 | * mess up the offsets after it. |
| 1296 | */ |
| 1297 | src = ofnode_path("/b-test"); |
| 1298 | dst = ofnode_path("/some-bus"); |
| 1299 | |
Simon Glass | 6816489 | 2023-09-26 08:14:37 -0600 | [diff] [blame] | 1300 | ut_assertok(check_copy_props(uts, dst, src)); |
Simon Glass | 7a7229a | 2022-09-06 20:27:33 -0600 | [diff] [blame] | 1301 | |
| 1302 | /* check a property that is in the destination already */ |
| 1303 | ut_asserteq_str("mux0", ofnode_read_string(dst, "mux-control-names")); |
| 1304 | |
| 1305 | return 0; |
| 1306 | } |
| 1307 | DM_TEST(dm_test_ofnode_copy_props, UT_TESTF_SCAN_FDT); |
| 1308 | |
Simon Glass | 75b8a87 | 2023-09-26 08:14:39 -0600 | [diff] [blame] | 1309 | /* test ofnode_copy_props() with the 'other' tree */ |
Simon Glass | 7a7229a | 2022-09-06 20:27:33 -0600 | [diff] [blame] | 1310 | static int dm_test_ofnode_copy_props_ot(struct unit_test_state *uts) |
| 1311 | { |
| 1312 | ofnode src, dst; |
| 1313 | oftree otree = get_other_oftree(uts); |
| 1314 | |
| 1315 | src = ofnode_path("/b-test"); |
| 1316 | dst = oftree_path(otree, "/node/subnode2"); |
Simon Glass | 6816489 | 2023-09-26 08:14:37 -0600 | [diff] [blame] | 1317 | ut_assertok(check_copy_props(uts, dst, src)); |
Simon Glass | 7a7229a | 2022-09-06 20:27:33 -0600 | [diff] [blame] | 1318 | |
| 1319 | return 0; |
| 1320 | } |
| 1321 | DM_TEST(dm_test_ofnode_copy_props_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | e88e2fe | 2023-06-01 10:22:40 -0600 | [diff] [blame] | 1322 | |
| 1323 | /* check that the livetree is aligned to a structure boundary */ |
| 1324 | static int dm_test_livetree_align(struct unit_test_state *uts) |
| 1325 | { |
| 1326 | const int align = __alignof__(struct unit_test_state); |
| 1327 | struct device_node *node; |
| 1328 | u32 *sentinel; |
| 1329 | ulong start; |
| 1330 | |
| 1331 | start = (ulong)gd_of_root(); |
| 1332 | ut_asserteq(start, ALIGN(start, align)); |
| 1333 | |
| 1334 | node = gd_of_root(); |
| 1335 | sentinel = (void *)node - sizeof(u32); |
| 1336 | |
| 1337 | /* |
| 1338 | * The sentinel should be overwritten with the root node. If it isn't, |
| 1339 | * then the root node is not at the very start of the livetree memory |
| 1340 | * area, and free(root) will fail to free the memory used by the |
| 1341 | * livetree. |
| 1342 | */ |
| 1343 | ut_assert(*sentinel != BAD_OF_ROOT); |
| 1344 | |
| 1345 | return 0; |
| 1346 | } |
Simon Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 1347 | DM_TEST(dm_test_livetree_align, UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_TREE); |
Simon Glass | 722281b | 2023-06-01 10:22:42 -0600 | [diff] [blame] | 1348 | |
| 1349 | /* check that it is possible to load an arbitrary livetree */ |
| 1350 | static int dm_test_livetree_ensure(struct unit_test_state *uts) |
| 1351 | { |
| 1352 | oftree tree; |
| 1353 | ofnode node; |
| 1354 | |
| 1355 | /* read from other.dtb */ |
| 1356 | ut_assertok(test_load_other_fdt(uts)); |
| 1357 | tree = oftree_from_fdt(uts->other_fdt); |
| 1358 | ut_assert(oftree_valid(tree)); |
| 1359 | node = oftree_path(tree, "/node/subnode"); |
| 1360 | ut_assert(ofnode_valid(node)); |
| 1361 | ut_asserteq_str("sandbox-other2", |
| 1362 | ofnode_read_string(node, "compatible")); |
| 1363 | |
| 1364 | return 0; |
| 1365 | } |
Simon Glass | db6d872 | 2023-09-26 08:14:38 -0600 | [diff] [blame] | 1366 | DM_TEST(dm_test_livetree_ensure, UT_TESTF_SCAN_FDT); |
Simon Glass | a869a1b | 2023-09-26 08:14:40 -0600 | [diff] [blame] | 1367 | |
| 1368 | static int dm_test_oftree_new(struct unit_test_state *uts) |
| 1369 | { |
| 1370 | ofnode node, subnode, check; |
| 1371 | oftree tree; |
| 1372 | |
| 1373 | ut_assertok(oftree_new(&tree)); |
| 1374 | node = oftree_root(tree); |
| 1375 | ut_assert(ofnode_valid(node)); |
| 1376 | ut_assertok(ofnode_add_subnode(node, "edmund", &subnode)); |
| 1377 | check = ofnode_find_subnode(node, "edmund"); |
| 1378 | ut_asserteq(check.of_offset, subnode.of_offset); |
| 1379 | |
| 1380 | return 0; |
| 1381 | } |
| 1382 | DM_TEST(dm_test_oftree_new, UT_TESTF_SCAN_FDT); |
Simon Glass | 7c33c96 | 2023-09-26 08:14:41 -0600 | [diff] [blame] | 1383 | |
| 1384 | static int check_copy_node(struct unit_test_state *uts, ofnode dst, ofnode src, |
| 1385 | ofnode *nodep) |
| 1386 | { |
| 1387 | u32 reg[2], val; |
| 1388 | ofnode node; |
| 1389 | |
| 1390 | ut_assertok(ofnode_copy_node(dst, "copy-test", src, &node)); |
| 1391 | |
| 1392 | ut_assertok(ofnode_read_u32(node, "ping-expect", &val)); |
| 1393 | ut_asserteq(3, val); |
| 1394 | |
| 1395 | ut_asserteq_str("denx,u-boot-fdt-test", |
| 1396 | ofnode_read_string(node, "compatible")); |
| 1397 | |
| 1398 | /* check that a property with the same name is overwritten */ |
| 1399 | ut_assertok(ofnode_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg))); |
| 1400 | ut_asserteq(3, reg[0]); |
| 1401 | ut_asserteq(1, reg[1]); |
| 1402 | |
| 1403 | /* reset the compatible so the live tree does not change */ |
| 1404 | ut_assertok(ofnode_write_string(node, "compatible", "nothing")); |
| 1405 | *nodep = node; |
| 1406 | |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | static int dm_test_ofnode_copy_node(struct unit_test_state *uts) |
| 1411 | { |
| 1412 | ofnode src, dst, node, try; |
| 1413 | |
| 1414 | /* |
| 1415 | * These nodes are chosen so that the src node is before the destination |
| 1416 | * node in the tree. This doesn't matter with livetree, but with |
| 1417 | * flattree any attempt to insert a property earlier in the tree will |
| 1418 | * mess up the offsets after it. |
| 1419 | */ |
| 1420 | src = ofnode_path("/b-test"); |
| 1421 | dst = ofnode_path("/some-bus"); |
| 1422 | |
| 1423 | ut_assertok(check_copy_node(uts, dst, src, &node)); |
| 1424 | |
| 1425 | /* check trying to copy over an existing node */ |
| 1426 | ut_asserteq(-EEXIST, ofnode_copy_node(dst, "copy-test", src, &try)); |
| 1427 | ut_asserteq(try.of_offset, node.of_offset); |
| 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | DM_TEST(dm_test_ofnode_copy_node, UT_TESTF_SCAN_FDT); |
| 1432 | |
| 1433 | /* test ofnode_copy_node() with the 'other' tree */ |
| 1434 | static int dm_test_ofnode_copy_node_ot(struct unit_test_state *uts) |
| 1435 | { |
| 1436 | oftree otree = get_other_oftree(uts); |
| 1437 | ofnode src, dst, node; |
| 1438 | |
| 1439 | src = ofnode_path("/b-test"); |
| 1440 | dst = oftree_path(otree, "/node/subnode2"); |
| 1441 | ut_assertok(check_copy_node(uts, dst, src, &node)); |
| 1442 | |
| 1443 | return 0; |
| 1444 | } |
| 1445 | DM_TEST(dm_test_ofnode_copy_node_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | 4544877 | 2023-09-26 08:14:42 -0600 | [diff] [blame] | 1446 | |
| 1447 | static int dm_test_ofnode_delete(struct unit_test_state *uts) |
| 1448 | { |
| 1449 | ofnode node; |
| 1450 | |
| 1451 | /* |
| 1452 | * At present the livetree is not restored after changes made in tests. |
| 1453 | * See test_pre_run() for how this is done with the other FDT and |
| 1454 | * dm_test_pre_run() where it sets up the root-tree pointer. So use |
| 1455 | * nodes which don't matter to other tests. |
| 1456 | * |
| 1457 | * We could fix this by detecting livetree changes and regenerating it |
| 1458 | * before the next test if needed. |
| 1459 | */ |
| 1460 | node = ofnode_path("/leds/iracibble"); |
| 1461 | ut_assert(ofnode_valid(node)); |
| 1462 | ut_assertok(ofnode_delete(&node)); |
| 1463 | ut_assert(!ofnode_valid(node)); |
| 1464 | ut_assert(!ofnode_valid(ofnode_path("/leds/iracibble"))); |
| 1465 | |
| 1466 | node = ofnode_path("/leds/default_on"); |
| 1467 | ut_assert(ofnode_valid(node)); |
| 1468 | ut_assertok(ofnode_delete(&node)); |
| 1469 | ut_assert(!ofnode_valid(node)); |
| 1470 | ut_assert(!ofnode_valid(ofnode_path("/leds/default_on"))); |
| 1471 | |
| 1472 | ut_asserteq(2, ofnode_get_child_count(ofnode_path("/leds"))); |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | DM_TEST(dm_test_ofnode_delete, UT_TESTF_SCAN_FDT); |
Simon Glass | ebbe90b | 2023-09-26 08:14:43 -0600 | [diff] [blame] | 1477 | |
| 1478 | static int dm_test_oftree_to_fdt(struct unit_test_state *uts) |
| 1479 | { |
| 1480 | oftree tree, check; |
| 1481 | struct abuf buf, buf2; |
| 1482 | |
| 1483 | tree = oftree_default(); |
| 1484 | ut_assertok(oftree_to_fdt(tree, &buf)); |
| 1485 | ut_assert(abuf_size(&buf) > SZ_16K); |
| 1486 | |
| 1487 | /* convert it back to a tree and see if it looks OK */ |
| 1488 | check = oftree_from_fdt(abuf_data(&buf)); |
| 1489 | ut_assert(oftree_valid(check)); |
| 1490 | |
| 1491 | ut_assertok(oftree_to_fdt(check, &buf2)); |
| 1492 | ut_assert(abuf_size(&buf2) > SZ_16K); |
| 1493 | ut_asserteq(abuf_size(&buf), abuf_size(&buf2)); |
| 1494 | ut_asserteq_mem(abuf_data(&buf), abuf_data(&buf2), abuf_size(&buf)); |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | DM_TEST(dm_test_oftree_to_fdt, UT_TESTF_SCAN_FDT); |
Simon Glass | 86ef399 | 2023-09-26 08:14:44 -0600 | [diff] [blame] | 1499 | |
| 1500 | /* test ofnode_read_bool() and ofnode_write_bool() */ |
| 1501 | static int dm_test_bool(struct unit_test_state *uts) |
| 1502 | { |
| 1503 | const char *propname = "missing-bool-value"; |
| 1504 | ofnode node; |
| 1505 | |
| 1506 | node = ofnode_path("/a-test"); |
| 1507 | ut_assert(ofnode_read_bool(node, "bool-value")); |
| 1508 | ut_assert(!ofnode_read_bool(node, propname)); |
| 1509 | ut_assert(!ofnode_has_property(node, propname)); |
| 1510 | |
| 1511 | ut_assertok(ofnode_write_bool(node, propname, true)); |
| 1512 | ut_assert(ofnode_read_bool(node, propname)); |
| 1513 | ut_assert(ofnode_has_property(node, propname)); |
| 1514 | ut_assert(ofnode_read_bool(node, "bool-value")); |
| 1515 | |
| 1516 | ut_assertok(ofnode_write_bool(node, propname, false)); |
| 1517 | ut_assert(!ofnode_read_bool(node, propname)); |
| 1518 | ut_assert(!ofnode_has_property(node, propname)); |
| 1519 | ut_assert(ofnode_read_bool(node, "bool-value")); |
| 1520 | |
| 1521 | return 0; |
| 1522 | } |
| 1523 | DM_TEST(dm_test_bool, UT_TESTF_SCAN_FDT); |