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