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