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