Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Tests for fdt command |
| 4 | * |
Marek Vasut | d578efb | 2023-04-22 14:59:58 +0200 | [diff] [blame] | 5 | * Copyright 2022 Google LLC |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 8 | #include <console.h> |
| 9 | #include <fdt_support.h> |
| 10 | #include <mapmem.h> |
| 11 | #include <asm/global_data.h> |
| 12 | #include <linux/libfdt.h> |
| 13 | #include <test/suites.h> |
| 14 | #include <test/ut.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
Marek Vasut | f2def36 | 2023-03-02 04:08:45 +0100 | [diff] [blame] | 17 | /* |
| 18 | * Missing tests: |
| 19 | * fdt boardsetup - Do board-specific set up |
| 20 | * fdt checksign [<addr>] - check FIT signature |
| 21 | * <addr> - address of key blob |
| 22 | * default gd->fdt_blob |
| 23 | */ |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 24 | |
| 25 | /* Declare a new fdt test */ |
| 26 | #define FDT_TEST(_name, _flags) UNIT_TEST(_name, _flags, fdt_test) |
| 27 | |
| 28 | /** |
| 29 | * make_test_fdt() - Create an FDT with just a root node |
| 30 | * |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 31 | * The size is set to the minimum needed. This also sets the working FDT and |
| 32 | * checks that the expected output is received from doing so. |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 33 | * |
| 34 | * @uts: Test state |
| 35 | * @fdt: Place to write FDT |
| 36 | * @size: Maximum size of space for fdt |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 37 | * @addrp: Returns address of the devicetree |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 38 | */ |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 39 | static int make_test_fdt(struct unit_test_state *uts, void *fdt, int size, |
| 40 | ulong *addrp) |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 41 | { |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 42 | ulong addr; |
| 43 | |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 44 | ut_assertok(fdt_create(fdt, size)); |
| 45 | ut_assertok(fdt_finish_reservemap(fdt)); |
| 46 | ut_assert(fdt_begin_node(fdt, "") >= 0); |
| 47 | ut_assertok(fdt_end_node(fdt)); |
| 48 | ut_assertok(fdt_finish(fdt)); |
| 49 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 50 | addr = map_to_sysmem(fdt); |
| 51 | set_working_fdt_addr(addr); |
| 52 | ut_assert_nextline("Working FDT set to %lx", addr); |
| 53 | *addrp = addr; |
| 54 | |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
Marek Vasut | 15503d0 | 2023-03-02 04:08:27 +0100 | [diff] [blame] | 58 | /** |
| 59 | * make_fuller_fdt() - Create an FDT with root node and properties |
| 60 | * |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 61 | * The size is set to the minimum needed. This also sets the working FDT and |
| 62 | * checks that the expected output is received from doing so. |
Marek Vasut | 15503d0 | 2023-03-02 04:08:27 +0100 | [diff] [blame] | 63 | * |
| 64 | * @uts: Test state |
| 65 | * @fdt: Place to write FDT |
| 66 | * @size: Maximum size of space for fdt |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 67 | * @addrp: Returns address of the devicetree |
Marek Vasut | 15503d0 | 2023-03-02 04:08:27 +0100 | [diff] [blame] | 68 | */ |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 69 | static int make_fuller_fdt(struct unit_test_state *uts, void *fdt, int size, |
| 70 | ulong *addrp) |
Marek Vasut | 15503d0 | 2023-03-02 04:08:27 +0100 | [diff] [blame] | 71 | { |
| 72 | fdt32_t regs[2] = { cpu_to_fdt32(0x1234), cpu_to_fdt32(0x1000) }; |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 73 | ulong addr; |
Marek Vasut | 15503d0 | 2023-03-02 04:08:27 +0100 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Assemble the following DT for test purposes: |
| 77 | * |
| 78 | * / { |
| 79 | * #address-cells = <0x00000001>; |
| 80 | * #size-cells = <0x00000001>; |
| 81 | * compatible = "u-boot,fdt-test"; |
| 82 | * model = "U-Boot FDT test"; |
| 83 | * |
| 84 | * aliases { |
| 85 | * badalias = "/bad/alias"; |
| 86 | * subnodealias = "/test-node@1234/subnode"; |
| 87 | * testnodealias = "/test-node@1234"; |
| 88 | * }; |
| 89 | * |
| 90 | * test-node@1234 { |
| 91 | * #address-cells = <0x00000000>; |
| 92 | * #size-cells = <0x00000000>; |
| 93 | * compatible = "u-boot,fdt-test-device1"; |
| 94 | * clock-names = "fixed", "i2c", "spi", "uart2", "uart1"; |
| 95 | * u-boot,empty-property; |
| 96 | * clock-frequency = <0x00fde800>; |
| 97 | * regs = <0x00001234 0x00001000>; |
| 98 | * |
| 99 | * subnode { |
| 100 | * #address-cells = <0x00000000>; |
| 101 | * #size-cells = <0x00000000>; |
| 102 | * compatible = "u-boot,fdt-subnode-test-device"; |
| 103 | * }; |
| 104 | * }; |
| 105 | * }; |
| 106 | */ |
| 107 | |
| 108 | ut_assertok(fdt_create(fdt, size)); |
| 109 | ut_assertok(fdt_finish_reservemap(fdt)); |
| 110 | ut_assert(fdt_begin_node(fdt, "") >= 0); |
| 111 | |
| 112 | ut_assertok(fdt_property_u32(fdt, "#address-cells", 1)); |
| 113 | ut_assertok(fdt_property_u32(fdt, "#size-cells", 1)); |
| 114 | /* <string> */ |
| 115 | ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test")); |
| 116 | /* <string> */ |
| 117 | ut_assertok(fdt_property_string(fdt, "model", "U-Boot FDT test")); |
| 118 | |
| 119 | ut_assert(fdt_begin_node(fdt, "aliases") >= 0); |
| 120 | /* <string> */ |
| 121 | ut_assertok(fdt_property_string(fdt, "badalias", "/bad/alias")); |
| 122 | /* <string> */ |
| 123 | ut_assertok(fdt_property_string(fdt, "subnodealias", "/test-node@1234/subnode")); |
| 124 | /* <string> */ |
| 125 | ut_assertok(fdt_property_string(fdt, "testnodealias", "/test-node@1234")); |
| 126 | ut_assertok(fdt_end_node(fdt)); |
| 127 | |
| 128 | ut_assert(fdt_begin_node(fdt, "test-node@1234") >= 0); |
| 129 | ut_assertok(fdt_property_cell(fdt, "#address-cells", 0)); |
| 130 | ut_assertok(fdt_property_cell(fdt, "#size-cells", 0)); |
| 131 | /* <string> */ |
| 132 | ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test-device1")); |
| 133 | /* <stringlist> */ |
| 134 | ut_assertok(fdt_property(fdt, "clock-names", "fixed\0i2c\0spi\0uart2\0uart1\0", 26)); |
| 135 | /* <empty> */ |
| 136 | ut_assertok(fdt_property(fdt, "u-boot,empty-property", NULL, 0)); |
| 137 | /* |
| 138 | * <u32> |
| 139 | * This value is deliberate as it used to break cmd/fdt.c |
| 140 | * is_printable_string() implementation. |
| 141 | */ |
| 142 | ut_assertok(fdt_property_u32(fdt, "clock-frequency", 16640000)); |
| 143 | /* <prop-encoded-array> */ |
| 144 | ut_assertok(fdt_property(fdt, "regs", ®s, sizeof(regs))); |
| 145 | ut_assert(fdt_begin_node(fdt, "subnode") >= 0); |
| 146 | ut_assertok(fdt_property_cell(fdt, "#address-cells", 0)); |
| 147 | ut_assertok(fdt_property_cell(fdt, "#size-cells", 0)); |
| 148 | ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-subnode-test-device")); |
| 149 | ut_assertok(fdt_end_node(fdt)); |
| 150 | ut_assertok(fdt_end_node(fdt)); |
| 151 | |
| 152 | ut_assertok(fdt_end_node(fdt)); |
| 153 | ut_assertok(fdt_finish(fdt)); |
| 154 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 155 | addr = map_to_sysmem(fdt); |
| 156 | set_working_fdt_addr(addr); |
| 157 | ut_assert_nextline("Working FDT set to %lx", addr); |
| 158 | *addrp = addr; |
| 159 | |
Marek Vasut | 15503d0 | 2023-03-02 04:08:27 +0100 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 163 | /* Test 'fdt addr' getting/setting address */ |
| 164 | static int fdt_test_addr(struct unit_test_state *uts) |
| 165 | { |
| 166 | const void *fdt_blob, *new_fdt; |
| 167 | char fdt[256]; |
| 168 | ulong addr; |
| 169 | int ret; |
| 170 | |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 171 | ut_assertok(run_command("fdt addr -c", 0)); |
| 172 | ut_assert_nextline("Control fdt: %08lx", |
| 173 | (ulong)map_to_sysmem(gd->fdt_blob)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 174 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 175 | |
| 176 | /* The working fdt is not set, so this should fail */ |
| 177 | set_working_fdt_addr(0); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 178 | ut_assert_nextline("Working FDT set to 0"); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 179 | ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0)); |
Simon Glass | 5ad263f | 2023-10-01 19:15:18 -0600 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * sandbox fails the check for !blob since the 0 pointer is mapped to |
| 183 | * memory somewhere other than at 0x0 |
| 184 | */ |
| 185 | if (IS_ENABLED(CONFIG_SANDBOX)) |
| 186 | ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 187 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 188 | |
| 189 | /* Set up a working FDT and try again */ |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 190 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr)); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 191 | ut_assertok(run_command("fdt addr", 0)); |
| 192 | ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 193 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 194 | |
| 195 | /* Set the working FDT */ |
| 196 | set_working_fdt_addr(0); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 197 | ut_assert_nextline("Working FDT set to 0"); |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 198 | ut_assertok(run_commandf("fdt addr %08lx", addr)); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 199 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 200 | ut_asserteq(addr, map_to_sysmem(working_fdt)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 201 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 202 | set_working_fdt_addr(0); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 203 | ut_assert_nextline("Working FDT set to 0"); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 204 | |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 205 | /* Set the control FDT */ |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 206 | fdt_blob = gd->fdt_blob; |
| 207 | gd->fdt_blob = NULL; |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 208 | ret = run_commandf("fdt addr -c %08lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 209 | new_fdt = gd->fdt_blob; |
| 210 | gd->fdt_blob = fdt_blob; |
| 211 | ut_assertok(ret); |
| 212 | ut_asserteq(addr, map_to_sysmem(new_fdt)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 213 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 214 | |
| 215 | /* Test setting an invalid FDT */ |
| 216 | fdt[0] = 123; |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 217 | ut_asserteq(1, run_commandf("fdt addr %08lx", addr)); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 218 | ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 219 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 220 | |
| 221 | /* Test detecting an invalid FDT */ |
| 222 | fdt[0] = 123; |
| 223 | set_working_fdt_addr(addr); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 224 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 225 | ut_asserteq(1, run_commandf("fdt addr")); |
| 226 | ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 227 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 228 | |
| 229 | return 0; |
| 230 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 231 | FDT_TEST(fdt_test_addr, UTF_CONSOLE); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 232 | |
| 233 | /* Test 'fdt addr' resizing an fdt */ |
Marek Vasut | 6e04a89 | 2023-03-02 04:08:25 +0100 | [diff] [blame] | 234 | static int fdt_test_addr_resize(struct unit_test_state *uts) |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 235 | { |
| 236 | char fdt[256]; |
| 237 | const int newsize = sizeof(fdt) / 2; |
| 238 | ulong addr; |
| 239 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 240 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr)); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 241 | |
| 242 | /* Test setting and resizing the working FDT to a larger size */ |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 243 | ut_assertok(run_commandf("fdt addr %08lx %x", addr, newsize)); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 244 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 245 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 246 | |
| 247 | /* Try shrinking it */ |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 248 | ut_assertok(run_commandf("fdt addr %08lx %zx", addr, sizeof(fdt) / 4)); |
Simon Glass | 84328cf | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 249 | ut_assert_nextline("Working FDT set to %lx", addr); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 250 | ut_assert_nextline("New length %d < existing length %d, ignoring", |
| 251 | (int)sizeof(fdt) / 4, newsize); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 252 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 253 | |
| 254 | /* ...quietly */ |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 255 | ut_assertok(run_commandf("fdt addr -q %08lx %zx", addr, sizeof(fdt) / 4)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 256 | ut_assert_console_end(); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 257 | |
| 258 | /* We cannot easily provoke errors in fdt_open_into(), so ignore that */ |
| 259 | |
| 260 | return 0; |
| 261 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 262 | FDT_TEST(fdt_test_addr_resize, UTF_CONSOLE); |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 263 | |
Marek Vasut | ddf8620 | 2023-03-02 04:08:30 +0100 | [diff] [blame] | 264 | static int fdt_test_move(struct unit_test_state *uts) |
| 265 | { |
| 266 | char fdt[256]; |
| 267 | ulong addr, newaddr = 0x10000; |
| 268 | const int size = sizeof(fdt); |
| 269 | uint32_t ts; |
| 270 | void *buf; |
| 271 | |
| 272 | /* Original source DT */ |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 273 | ut_assertok(make_test_fdt(uts, fdt, size, &addr)); |
Marek Vasut | ddf8620 | 2023-03-02 04:08:30 +0100 | [diff] [blame] | 274 | ts = fdt_totalsize(fdt); |
Marek Vasut | ddf8620 | 2023-03-02 04:08:30 +0100 | [diff] [blame] | 275 | |
| 276 | /* Moved target DT location */ |
| 277 | buf = map_sysmem(newaddr, size); |
| 278 | memset(buf, 0, size); |
| 279 | |
| 280 | /* Test moving the working FDT to a new location */ |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 281 | ut_assertok(run_commandf("fdt move %08lx %08lx %x", addr, newaddr, ts)); |
Marek Vasut | ddf8620 | 2023-03-02 04:08:30 +0100 | [diff] [blame] | 282 | ut_assert_nextline("Working FDT set to %lx", newaddr); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 283 | ut_assert_console_end(); |
Marek Vasut | ddf8620 | 2023-03-02 04:08:30 +0100 | [diff] [blame] | 284 | |
| 285 | /* Compare the source and destination DTs */ |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 286 | ut_assertok(run_commandf("cmp.b %08lx %08lx %x", addr, newaddr, ts)); |
Marek Vasut | ddf8620 | 2023-03-02 04:08:30 +0100 | [diff] [blame] | 287 | ut_assert_nextline("Total of %d byte(s) were the same", ts); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 288 | ut_assert_console_end(); |
Marek Vasut | ddf8620 | 2023-03-02 04:08:30 +0100 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 292 | FDT_TEST(fdt_test_move, UTF_CONSOLE); |
Marek Vasut | ddf8620 | 2023-03-02 04:08:30 +0100 | [diff] [blame] | 293 | |
Marek Vasut | 6ab4fe2 | 2023-03-02 04:08:31 +0100 | [diff] [blame] | 294 | static int fdt_test_resize(struct unit_test_state *uts) |
| 295 | { |
| 296 | char fdt[256]; |
| 297 | const unsigned int newsize = 0x2000; |
| 298 | uint32_t ts; |
| 299 | ulong addr; |
| 300 | |
| 301 | /* Original source DT */ |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 302 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | 6ab4fe2 | 2023-03-02 04:08:31 +0100 | [diff] [blame] | 303 | fdt_shrink_to_minimum(fdt, 0); /* Resize with 0 extra bytes */ |
| 304 | ts = fdt_totalsize(fdt); |
Marek Vasut | 6ab4fe2 | 2023-03-02 04:08:31 +0100 | [diff] [blame] | 305 | |
| 306 | /* Test resizing the working FDT and verify the new space was added */ |
Marek Vasut | 6ab4fe2 | 2023-03-02 04:08:31 +0100 | [diff] [blame] | 307 | ut_assertok(run_commandf("fdt resize %x", newsize)); |
| 308 | ut_asserteq(ts + newsize, fdt_totalsize(fdt)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 309 | ut_assert_console_end(); |
Marek Vasut | 6ab4fe2 | 2023-03-02 04:08:31 +0100 | [diff] [blame] | 310 | |
| 311 | return 0; |
| 312 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 313 | FDT_TEST(fdt_test_resize, UTF_CONSOLE); |
Marek Vasut | 6ab4fe2 | 2023-03-02 04:08:31 +0100 | [diff] [blame] | 314 | |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 315 | static int fdt_test_print_list_common(struct unit_test_state *uts, |
| 316 | const char *opc, const char *node) |
| 317 | { |
| 318 | /* |
| 319 | * Test printing/listing the working FDT |
| 320 | * subnode $node/subnode |
| 321 | */ |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 322 | ut_assertok(run_commandf("fdt %s %s/subnode", opc, node)); |
| 323 | ut_assert_nextline("subnode {"); |
| 324 | ut_assert_nextline("\t#address-cells = <0x00000000>;"); |
| 325 | ut_assert_nextline("\t#size-cells = <0x00000000>;"); |
| 326 | ut_assert_nextline("\tcompatible = \"u-boot,fdt-subnode-test-device\";"); |
| 327 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 328 | ut_assert_console_end(); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Test printing/listing the working FDT |
| 332 | * path / string property model |
| 333 | */ |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 334 | ut_assertok(run_commandf("fdt %s / model", opc)); |
| 335 | ut_assert_nextline("model = \"U-Boot FDT test\""); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 336 | ut_assert_console_end(); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 337 | |
| 338 | /* |
| 339 | * Test printing/listing the working FDT |
| 340 | * path $node string property compatible |
| 341 | */ |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 342 | ut_assertok(run_commandf("fdt %s %s compatible", opc, node)); |
| 343 | ut_assert_nextline("compatible = \"u-boot,fdt-test-device1\""); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 344 | ut_assert_console_end(); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 345 | |
| 346 | /* |
| 347 | * Test printing/listing the working FDT |
| 348 | * path $node stringlist property clock-names |
| 349 | */ |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 350 | ut_assertok(run_commandf("fdt %s %s clock-names", opc, node)); |
| 351 | ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\""); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 352 | ut_assert_console_end(); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * Test printing/listing the working FDT |
| 356 | * path $node u32 property clock-frequency |
| 357 | */ |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 358 | ut_assertok(run_commandf("fdt %s %s clock-frequency", opc, node)); |
| 359 | ut_assert_nextline("clock-frequency = <0x00fde800>"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 360 | ut_assert_console_end(); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * Test printing/listing the working FDT |
| 364 | * path $node empty property u-boot,empty-property |
| 365 | */ |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 366 | ut_assertok(run_commandf("fdt %s %s u-boot,empty-property", opc, node)); |
| 367 | /* |
| 368 | * This is the only 'fdt print' / 'fdt list' incantation which |
| 369 | * prefixes the property with node path. This has been in U-Boot |
| 370 | * since the beginning of the command 'fdt', keep it. |
| 371 | */ |
| 372 | ut_assert_nextline("%s u-boot,empty-property", node); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 373 | ut_assert_console_end(); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 374 | |
| 375 | /* |
| 376 | * Test printing/listing the working FDT |
| 377 | * path $node prop-encoded array property regs |
| 378 | */ |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 379 | ut_assertok(run_commandf("fdt %s %s regs", opc, node)); |
| 380 | ut_assert_nextline("regs = <0x00001234 0x00001000>"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 381 | ut_assert_console_end(); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static int fdt_test_print_list(struct unit_test_state *uts, bool print) |
| 387 | { |
| 388 | const char *opc = print ? "print" : "list"; |
| 389 | char fdt[4096]; |
| 390 | ulong addr; |
| 391 | int ret; |
| 392 | |
| 393 | /* Original source DT */ |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 394 | ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 395 | |
| 396 | /* Test printing/listing the working FDT -- node / */ |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 397 | ut_assertok(run_commandf("fdt %s", opc)); |
| 398 | ut_assert_nextline("/ {"); |
| 399 | ut_assert_nextline("\t#address-cells = <0x00000001>;"); |
| 400 | ut_assert_nextline("\t#size-cells = <0x00000001>;"); |
| 401 | ut_assert_nextline("\tcompatible = \"u-boot,fdt-test\";"); |
| 402 | ut_assert_nextline("\tmodel = \"U-Boot FDT test\";"); |
| 403 | ut_assert_nextline("\taliases {"); |
| 404 | if (print) { |
| 405 | ut_assert_nextline("\t\tbadalias = \"/bad/alias\";"); |
| 406 | ut_assert_nextline("\t\tsubnodealias = \"/test-node@1234/subnode\";"); |
| 407 | ut_assert_nextline("\t\ttestnodealias = \"/test-node@1234\";"); |
| 408 | } |
| 409 | ut_assert_nextline("\t};"); |
| 410 | ut_assert_nextline("\ttest-node@1234 {"); |
| 411 | if (print) { |
| 412 | ut_assert_nextline("\t\t#address-cells = <0x00000000>;"); |
| 413 | ut_assert_nextline("\t\t#size-cells = <0x00000000>;"); |
| 414 | ut_assert_nextline("\t\tcompatible = \"u-boot,fdt-test-device1\";"); |
| 415 | ut_assert_nextline("\t\tclock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\";"); |
| 416 | ut_assert_nextline("\t\tu-boot,empty-property;"); |
| 417 | ut_assert_nextline("\t\tclock-frequency = <0x00fde800>;"); |
| 418 | ut_assert_nextline("\t\tregs = <0x00001234 0x00001000>;"); |
| 419 | ut_assert_nextline("\t\tsubnode {"); |
| 420 | ut_assert_nextline("\t\t\t#address-cells = <0x00000000>;"); |
| 421 | ut_assert_nextline("\t\t\t#size-cells = <0x00000000>;"); |
| 422 | ut_assert_nextline("\t\t\tcompatible = \"u-boot,fdt-subnode-test-device\";"); |
| 423 | ut_assert_nextline("\t\t};"); |
| 424 | } |
| 425 | ut_assert_nextline("\t};"); |
| 426 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 427 | ut_assert_console_end(); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 428 | |
| 429 | ret = fdt_test_print_list_common(uts, opc, "/test-node@1234"); |
| 430 | if (!ret) |
| 431 | ret = fdt_test_print_list_common(uts, opc, "testnodealias"); |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int fdt_test_print(struct unit_test_state *uts) |
| 437 | { |
| 438 | return fdt_test_print_list(uts, true); |
| 439 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 440 | FDT_TEST(fdt_test_print, UTF_CONSOLE); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 441 | |
| 442 | static int fdt_test_list(struct unit_test_state *uts) |
| 443 | { |
| 444 | return fdt_test_print_list(uts, false); |
| 445 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 446 | FDT_TEST(fdt_test_list, UTF_CONSOLE); |
Marek Vasut | 76a652a | 2023-03-02 04:08:32 +0100 | [diff] [blame] | 447 | |
Marek Vasut | fbeca45 | 2023-03-02 04:08:26 +0100 | [diff] [blame] | 448 | /* Test 'fdt get value' reading an fdt */ |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 449 | static int fdt_test_get_value_string(struct unit_test_state *uts, |
| 450 | const char *node, const char *prop, |
| 451 | const char *idx, const char *strres, |
| 452 | const int intres) |
| 453 | { |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 454 | ut_assertok(run_commandf("fdt get value var %s %s %s", |
| 455 | node, prop, idx ? : "")); |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 456 | if (strres) |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 457 | ut_asserteq_str(strres, env_get("var")); |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 458 | else |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 459 | ut_asserteq(intres, env_get_hex("var", 0x1234)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 460 | ut_assert_console_end(); |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 465 | static int fdt_test_get_value_common(struct unit_test_state *uts, |
| 466 | const char *node) |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 467 | { |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 468 | /* Test getting default element of $node node clock-names property */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 469 | ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", NULL, |
| 470 | "fixed", 0)); |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 471 | |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 472 | /* Test getting 0th element of $node node clock-names property */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 473 | ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", "0", |
| 474 | "fixed", 0)); |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 475 | |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 476 | /* Test getting 1st element of $node node clock-names property */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 477 | ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", "1", |
| 478 | "i2c", 0)); |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 479 | |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 480 | /* Test getting 2nd element of $node node clock-names property */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 481 | ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", "2", |
| 482 | "spi", 0)); |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * Test getting default element of $node node regs property. |
| 486 | * The result here is highly unusual, the non-index value read from |
| 487 | * integer array is a string of concatenated values from the array, |
| 488 | * but only if the array is shorter than 40 characters. Anything |
| 489 | * longer is an error. This is a special case for handling hashes. |
| 490 | */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 491 | ut_assertok(fdt_test_get_value_string(uts, node, "regs", NULL, |
| 492 | "3412000000100000", 0)); |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 493 | |
| 494 | /* Test getting 0th element of $node node regs property */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 495 | ut_assertok(fdt_test_get_value_string(uts, node, "regs", "0", NULL, |
| 496 | 0x1234)); |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 497 | |
| 498 | /* Test getting 1st element of $node node regs property */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 499 | ut_assertok(fdt_test_get_value_string(uts, node, "regs", "1", NULL, |
| 500 | 0x1000)); |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 501 | |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 502 | /* Test missing 10th element of $node node clock-names property */ |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 503 | ut_asserteq(1, run_commandf("fdt get value ften %s clock-names 10", node)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 504 | ut_assert_console_end(); |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 505 | |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 506 | /* Test missing 10th element of $node node regs property */ |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 507 | ut_asserteq(1, run_commandf("fdt get value ften %s regs 10", node)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 508 | ut_assert_console_end(); |
Marek Vasut | 9f90cad | 2023-03-02 04:08:29 +0100 | [diff] [blame] | 509 | |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 510 | /* Test getting default element of $node node nonexistent property */ |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 511 | ut_asserteq(1, run_commandf("fdt get value fnone %s nonexistent", node)); |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 512 | ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 513 | ut_assert_console_end(); |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 514 | |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int fdt_test_get_value(struct unit_test_state *uts) |
| 519 | { |
| 520 | char fdt[4096]; |
| 521 | ulong addr; |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 522 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 523 | ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 524 | |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 525 | ut_assertok(fdt_test_get_value_common(uts, "/test-node@1234")); |
| 526 | ut_assertok(fdt_test_get_value_common(uts, "testnodealias")); |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 527 | |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 528 | /* Test getting default element of /nonexistent node */ |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 529 | ut_asserteq(1, run_command("fdt get value fnode /nonexistent nonexistent", 1)); |
| 530 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 531 | ut_assert_console_end(); |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 532 | |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 533 | /* Test getting default element of bad alias */ |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 534 | ut_asserteq(1, run_command("fdt get value vbadalias badalias nonexistent", 1)); |
| 535 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 536 | ut_assert_console_end(); |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 537 | |
| 538 | /* Test getting default element of nonexistent alias */ |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 539 | ut_asserteq(1, run_command("fdt get value vnoalias noalias nonexistent", 1)); |
| 540 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 541 | ut_assert_console_end(); |
Marek Vasut | b7517b4 | 2023-03-02 04:08:28 +0100 | [diff] [blame] | 542 | |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 543 | return 0; |
| 544 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 545 | FDT_TEST(fdt_test_get_value, UTF_CONSOLE); |
Marek Vasut | 78c060d | 2022-11-14 22:50:00 +0100 | [diff] [blame] | 546 | |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 547 | static int fdt_test_get_name(struct unit_test_state *uts) |
| 548 | { |
| 549 | char fdt[4096]; |
| 550 | ulong addr; |
| 551 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 552 | ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 553 | |
| 554 | /* Test getting name of node 0 in /, which is /aliases node */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 555 | ut_assertok(run_command("fdt get name nzero / 0", 0)); |
| 556 | ut_asserteq_str("aliases", env_get("nzero")); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 557 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 558 | |
| 559 | /* Test getting name of node 1 in /, which is /test-node@1234 node */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 560 | ut_assertok(run_command("fdt get name none / 1", 0)); |
| 561 | ut_asserteq_str("test-node@1234", env_get("none")); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 562 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 563 | |
| 564 | /* Test getting name of node -1 in /, which is /aliases node, same as 0 */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 565 | ut_assertok(run_command("fdt get name nmone / -1", 0)); |
| 566 | ut_asserteq_str("aliases", env_get("nmone")); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 567 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 568 | |
| 569 | /* Test getting name of node 2 in /, which does not exist */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 570 | ut_asserteq(1, run_command("fdt get name ntwo / 2", 1)); |
| 571 | ut_assert_nextline("libfdt node not found"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 572 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 573 | |
| 574 | /* Test getting name of node 0 in /test-node@1234, which is /subnode node */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 575 | ut_assertok(run_command("fdt get name snzero /test-node@1234 0", 0)); |
| 576 | ut_asserteq_str("subnode", env_get("snzero")); |
| 577 | ut_assertok(run_command("fdt get name asnzero testnodealias 0", 0)); |
| 578 | ut_asserteq_str("subnode", env_get("asnzero")); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 579 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 580 | |
| 581 | /* Test getting name of node 1 in /test-node@1234, which does not exist */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 582 | ut_asserteq(1, run_command("fdt get name snone /test-node@1234 1", 1)); |
| 583 | ut_assert_nextline("libfdt node not found"); |
| 584 | ut_asserteq(1, run_command("fdt get name asnone testnodealias 1", 1)); |
| 585 | ut_assert_nextline("libfdt node not found"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 586 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 587 | |
| 588 | /* Test getting name of node -1 in /test-node@1234, which is /subnode node, same as 0 */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 589 | ut_assertok(run_command("fdt get name snmone /test-node@1234 -1", 0)); |
| 590 | ut_asserteq_str("subnode", env_get("snmone")); |
| 591 | ut_assertok(run_command("fdt get name asnmone testnodealias -1", 0)); |
| 592 | ut_asserteq_str("subnode", env_get("asnmone")); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 593 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 594 | |
| 595 | /* Test getting name of nonexistent node */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 596 | ut_asserteq(1, run_command("fdt get name nonode /nonexistent 0", 1)); |
| 597 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 598 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 599 | |
| 600 | /* Test getting name of bad alias */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 601 | ut_asserteq(1, run_command("fdt get name vbadalias badalias 0", 1)); |
| 602 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 603 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 604 | |
| 605 | /* Test getting name of nonexistent alias */ |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 606 | ut_asserteq(1, run_command("fdt get name vnoalias noalias 0", 1)); |
| 607 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 608 | ut_assert_console_end(); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 609 | |
| 610 | return 0; |
| 611 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 612 | FDT_TEST(fdt_test_get_name, UTF_CONSOLE); |
Marek Vasut | 8449993 | 2023-03-02 04:08:33 +0100 | [diff] [blame] | 613 | |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 614 | static int fdt_test_get_addr_common(struct unit_test_state *uts, char *fdt, |
| 615 | const char *path, const char *prop) |
| 616 | { |
| 617 | unsigned int offset; |
| 618 | int path_offset; |
| 619 | void *prop_ptr; |
| 620 | int len = 0; |
| 621 | |
| 622 | path_offset = fdt_path_offset(fdt, path); |
| 623 | ut_assert(path_offset >= 0); |
| 624 | prop_ptr = (void *)fdt_getprop(fdt, path_offset, prop, &len); |
| 625 | ut_assertnonnull(prop_ptr); |
| 626 | offset = (char *)prop_ptr - fdt; |
| 627 | |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 628 | ut_assertok(run_commandf("fdt get addr pstr %s %s", path, prop)); |
| 629 | ut_asserteq((ulong)map_sysmem(env_get_hex("fdtaddr", 0x1234), 0), |
| 630 | (ulong)(map_sysmem(env_get_hex("pstr", 0x1234), 0) - offset)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 631 | ut_assert_console_end(); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static int fdt_test_get_addr(struct unit_test_state *uts) |
| 637 | { |
| 638 | char fdt[4096]; |
| 639 | ulong addr; |
| 640 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 641 | ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 642 | |
| 643 | /* Test getting address of root node / string property "compatible" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 644 | ut_assertok(fdt_test_get_addr_common(uts, fdt, "/", "compatible")); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 645 | |
| 646 | /* Test getting address of node /test-node@1234 stringlist property "clock-names" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 647 | ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234", |
| 648 | "clock-names")); |
| 649 | ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias", |
| 650 | "clock-names")); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 651 | |
| 652 | /* Test getting address of node /test-node@1234 u32 property "clock-frequency" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 653 | ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234", |
| 654 | "clock-frequency")); |
| 655 | ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias", |
| 656 | "clock-frequency")); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 657 | |
| 658 | /* Test getting address of node /test-node@1234 empty property "u-boot,empty-property" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 659 | ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234", |
| 660 | "u-boot,empty-property")); |
| 661 | ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias", |
| 662 | "u-boot,empty-property")); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 663 | |
| 664 | /* Test getting address of node /test-node@1234 array property "regs" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 665 | ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234", |
| 666 | "regs")); |
| 667 | ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias", |
| 668 | "regs")); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 669 | |
| 670 | /* Test getting address of node /test-node@1234/subnode non-existent property "noprop" */ |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 671 | ut_asserteq(1, run_command("fdt get addr pnoprop /test-node@1234/subnode noprop", 1)); |
| 672 | ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 673 | ut_assert_console_end(); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 674 | |
| 675 | /* Test getting address of non-existent node /test-node@1234/nonode@1 property "noprop" */ |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 676 | ut_asserteq(1, run_command("fdt get addr pnonode /test-node@1234/nonode@1 noprop", 1)); |
| 677 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 678 | ut_assert_console_end(); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 679 | |
| 680 | return 0; |
| 681 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 682 | FDT_TEST(fdt_test_get_addr, UTF_CONSOLE); |
Marek Vasut | cadef86 | 2023-03-02 04:08:34 +0100 | [diff] [blame] | 683 | |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 684 | static int fdt_test_get_size_common(struct unit_test_state *uts, |
| 685 | const char *path, const char *prop, |
| 686 | const unsigned int val) |
| 687 | { |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 688 | if (prop) { |
| 689 | ut_assertok(run_commandf("fdt get size sstr %s %s", path, prop)); |
| 690 | } else { |
| 691 | ut_assertok(run_commandf("fdt get size sstr %s", path)); |
| 692 | } |
| 693 | ut_asserteq(val, env_get_hex("sstr", 0x1234)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 694 | ut_assert_console_end(); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int fdt_test_get_size(struct unit_test_state *uts) |
| 700 | { |
| 701 | char fdt[4096]; |
| 702 | ulong addr; |
| 703 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 704 | ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 705 | |
| 706 | /* Test getting size of root node / string property "compatible" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 707 | ut_assertok(fdt_test_get_size_common(uts, "/", "compatible", 16)); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 708 | |
| 709 | /* Test getting size of node /test-node@1234 stringlist property "clock-names" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 710 | ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234", |
| 711 | "clock-names", 26)); |
| 712 | ut_assertok(fdt_test_get_size_common(uts, "testnodealias", |
| 713 | "clock-names", 26)); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 714 | |
| 715 | /* Test getting size of node /test-node@1234 u32 property "clock-frequency" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 716 | ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234", |
| 717 | "clock-frequency", 4)); |
| 718 | ut_assertok(fdt_test_get_size_common(uts, "testnodealias", |
| 719 | "clock-frequency", 4)); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 720 | |
| 721 | /* Test getting size of node /test-node@1234 empty property "u-boot,empty-property" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 722 | ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234", |
| 723 | "u-boot,empty-property", 0)); |
| 724 | ut_assertok(fdt_test_get_size_common(uts, "testnodealias", |
| 725 | "u-boot,empty-property", 0)); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 726 | |
| 727 | /* Test getting size of node /test-node@1234 array property "regs" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 728 | ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234", "regs", |
| 729 | 8)); |
| 730 | ut_assertok(fdt_test_get_size_common(uts, "testnodealias", "regs", 8)); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 731 | |
| 732 | /* Test getting node count of node / */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 733 | ut_assertok(fdt_test_get_size_common(uts, "/", NULL, 2)); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 734 | |
| 735 | /* Test getting node count of node /test-node@1234/subnode */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 736 | ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234/subnode", |
| 737 | NULL, 0)); |
| 738 | ut_assertok(fdt_test_get_size_common(uts, "subnodealias", NULL, 0)); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 739 | |
| 740 | /* Test getting size of node /test-node@1234/subnode non-existent property "noprop" */ |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 741 | ut_asserteq(1, run_command("fdt get size pnoprop /test-node@1234/subnode noprop", 1)); |
| 742 | ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND"); |
| 743 | ut_asserteq(1, run_command("fdt get size pnoprop subnodealias noprop", 1)); |
| 744 | ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 745 | ut_assert_console_end(); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 746 | |
| 747 | /* Test getting size of non-existent node /test-node@1234/nonode@1 property "noprop" */ |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 748 | ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1 noprop", 1)); |
| 749 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 750 | ut_assert_console_end(); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 751 | |
| 752 | /* Test getting node count of non-existent node /test-node@1234/nonode@1 */ |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 753 | ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1", 1)); |
| 754 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 755 | ut_assert_console_end(); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 756 | |
| 757 | /* Test getting node count of bad alias badalias */ |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 758 | ut_asserteq(1, run_command("fdt get size pnonode badalias noprop", 1)); |
| 759 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 760 | ut_assert_console_end(); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 761 | |
| 762 | /* Test getting node count of non-existent alias noalias */ |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 763 | ut_asserteq(1, run_command("fdt get size pnonode noalias", 1)); |
| 764 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 765 | ut_assert_console_end(); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 766 | |
| 767 | return 0; |
| 768 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 769 | FDT_TEST(fdt_test_get_size, UTF_CONSOLE); |
Marek Vasut | ce1b048 | 2023-03-02 04:08:35 +0100 | [diff] [blame] | 770 | |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 771 | static int fdt_test_set_single(struct unit_test_state *uts, |
| 772 | const char *path, const char *prop, |
| 773 | const char *sval, int ival, bool integer) |
| 774 | { |
| 775 | /* |
| 776 | * Set single element string/integer/<empty> property into DT, that is: |
| 777 | * => fdt set /path property string |
| 778 | * => fdt set /path property integer |
| 779 | * => fdt set /path property |
| 780 | */ |
Marek Vasut | bc8c140 | 2023-03-10 04:33:14 +0100 | [diff] [blame] | 781 | if (sval) |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 782 | ut_assertok(run_commandf("fdt set %s %s %s", path, prop, sval)); |
Marek Vasut | bc8c140 | 2023-03-10 04:33:14 +0100 | [diff] [blame] | 783 | else if (integer) |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 784 | ut_assertok(run_commandf("fdt set %s %s <%d>", path, prop, ival)); |
Marek Vasut | bc8c140 | 2023-03-10 04:33:14 +0100 | [diff] [blame] | 785 | else |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 786 | ut_assertok(run_commandf("fdt set %s %s", path, prop)); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 787 | |
| 788 | /* Validate the property is present and has correct value. */ |
| 789 | ut_assertok(run_commandf("fdt get value svar %s %s", path, prop)); |
Marek Vasut | bc8c140 | 2023-03-10 04:33:14 +0100 | [diff] [blame] | 790 | if (sval) |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 791 | ut_asserteq_str(sval, env_get("svar")); |
Marek Vasut | bc8c140 | 2023-03-10 04:33:14 +0100 | [diff] [blame] | 792 | else if (integer) |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 793 | ut_asserteq(ival, env_get_hex("svar", 0x1234)); |
Marek Vasut | bc8c140 | 2023-03-10 04:33:14 +0100 | [diff] [blame] | 794 | else |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 795 | ut_assertnull(env_get("svar")); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 796 | ut_assert_console_end(); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | static int fdt_test_set_multi(struct unit_test_state *uts, |
| 802 | const char *path, const char *prop, |
| 803 | const char *sval1, const char *sval2, |
| 804 | int ival1, int ival2) |
| 805 | { |
| 806 | /* |
| 807 | * Set multi element string/integer array property in DT, that is: |
| 808 | * => fdt set /path property <string1 string2> |
| 809 | * => fdt set /path property <integer1 integer2> |
| 810 | * |
| 811 | * The set is done twice in here deliberately, The first set adds |
| 812 | * the property with an extra trailing element in its array to make |
| 813 | * the array longer, the second set is the expected final content of |
| 814 | * the array property. The longer array is used to verify that the |
| 815 | * new array is correctly sized and read past the new array length |
| 816 | * triggers failure. |
| 817 | */ |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 818 | if (sval1 && sval2) { |
| 819 | ut_assertok(run_commandf("fdt set %s %s %s %s end", path, prop, sval1, sval2)); |
| 820 | ut_assertok(run_commandf("fdt set %s %s %s %s", path, prop, sval1, sval2)); |
| 821 | } else { |
| 822 | ut_assertok(run_commandf("fdt set %s %s <%d %d 10>", path, prop, ival1, ival2)); |
| 823 | ut_assertok(run_commandf("fdt set %s %s <%d %d>", path, prop, ival1, ival2)); |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Validate the property is present and has correct value. |
| 828 | * |
| 829 | * The "end/10" above and "svarn" below is used to validate that |
| 830 | * previous 'fdt set' to longer array does not polute newly set |
| 831 | * shorter array. |
| 832 | */ |
| 833 | ut_assertok(run_commandf("fdt get value svar1 %s %s 0", path, prop)); |
| 834 | ut_assertok(run_commandf("fdt get value svar2 %s %s 1", path, prop)); |
| 835 | ut_asserteq(1, run_commandf("fdt get value svarn %s %s 2", path, prop)); |
| 836 | if (sval1 && sval2) { |
| 837 | ut_asserteq_str(sval1, env_get("svar1")); |
| 838 | ut_asserteq_str(sval2, env_get("svar2")); |
| 839 | ut_assertnull(env_get("svarn")); |
| 840 | } else { |
| 841 | ut_asserteq(ival1, env_get_hex("svar1", 0x1234)); |
| 842 | ut_asserteq(ival2, env_get_hex("svar2", 0x1234)); |
| 843 | ut_assertnull(env_get("svarn")); |
| 844 | } |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 845 | ut_assert_console_end(); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | static int fdt_test_set_node(struct unit_test_state *uts, |
| 851 | const char *path, const char *prop) |
| 852 | { |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 853 | ut_assertok(fdt_test_set_single(uts, path, prop, "new", 0, false)); |
| 854 | ut_assertok(fdt_test_set_single(uts, path, prop, "rewrite", 0, false)); |
| 855 | ut_assertok(fdt_test_set_single(uts, path, prop, NULL, 42, true)); |
| 856 | ut_assertok(fdt_test_set_single(uts, path, prop, NULL, 0, false)); |
| 857 | ut_assertok(fdt_test_set_multi(uts, path, prop, NULL, NULL, 42, 1701)); |
| 858 | ut_assertok(fdt_test_set_multi(uts, path, prop, NULL, NULL, 74656, 9)); |
| 859 | ut_assertok(fdt_test_set_multi(uts, path, prop, "42", "1701", 0, 0)); |
| 860 | ut_assertok(fdt_test_set_multi(uts, path, prop, "74656", "9", 0, 0)); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | static int fdt_test_set(struct unit_test_state *uts) |
| 866 | { |
| 867 | char fdt[8192]; |
| 868 | ulong addr; |
| 869 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 870 | ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 871 | fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */ |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 872 | |
| 873 | /* Test setting of root node / existing property "compatible" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 874 | ut_assertok(fdt_test_set_node(uts, "/", "compatible")); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 875 | |
| 876 | /* Test setting of root node / new property "newproperty" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 877 | ut_assertok(fdt_test_set_node(uts, "/", "newproperty")); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 878 | |
| 879 | /* Test setting of subnode existing property "compatible" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 880 | ut_assertok(fdt_test_set_node(uts, "/test-node@1234/subnode", |
| 881 | "compatible")); |
| 882 | ut_assertok(fdt_test_set_node(uts, "subnodealias", "compatible")); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 883 | |
| 884 | /* Test setting of subnode new property "newproperty" */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 885 | ut_assertok(fdt_test_set_node(uts, "/test-node@1234/subnode", |
| 886 | "newproperty")); |
| 887 | ut_assertok(fdt_test_set_node(uts, "subnodealias", "newproperty")); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 888 | |
| 889 | /* Test setting property of non-existent node */ |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 890 | ut_asserteq(1, run_command("fdt set /no-node noprop", 1)); |
| 891 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 892 | ut_assert_console_end(); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 893 | |
| 894 | /* Test setting property of non-existent alias */ |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 895 | ut_asserteq(1, run_command("fdt set noalias noprop", 1)); |
| 896 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 897 | ut_assert_console_end(); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 898 | |
| 899 | /* Test setting property of bad alias */ |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 900 | ut_asserteq(1, run_command("fdt set badalias noprop", 1)); |
| 901 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 902 | ut_assert_console_end(); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 903 | |
| 904 | return 0; |
| 905 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 906 | FDT_TEST(fdt_test_set, UTF_CONSOLE); |
Marek Vasut | f180de4 | 2023-03-02 04:08:36 +0100 | [diff] [blame] | 907 | |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 908 | static int fdt_test_mknode(struct unit_test_state *uts) |
| 909 | { |
| 910 | char fdt[8192]; |
| 911 | ulong addr; |
| 912 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 913 | ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 914 | fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */ |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 915 | |
| 916 | /* Test creation of new node in / */ |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 917 | ut_assertok(run_commandf("fdt mknode / newnode")); |
| 918 | ut_assertok(run_commandf("fdt list /newnode")); |
| 919 | ut_assert_nextline("newnode {"); |
| 920 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 921 | ut_assert_console_end(); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 922 | |
| 923 | /* Test creation of new node in /test-node@1234 */ |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 924 | ut_assertok(run_commandf("fdt mknode /test-node@1234 newsubnode")); |
| 925 | ut_assertok(run_commandf("fdt list /test-node@1234/newsubnode")); |
| 926 | ut_assert_nextline("newsubnode {"); |
| 927 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 928 | ut_assert_console_end(); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 929 | |
| 930 | /* Test creation of new node in /test-node@1234 by alias */ |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 931 | ut_assertok(run_commandf("fdt mknode testnodealias newersubnode")); |
| 932 | ut_assertok(run_commandf("fdt list testnodealias/newersubnode")); |
| 933 | ut_assert_nextline("newersubnode {"); |
| 934 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 935 | ut_assert_console_end(); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 936 | |
| 937 | /* Test creation of new node in /test-node@1234 over existing node */ |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 938 | ut_asserteq(1, run_commandf("fdt mknode testnodealias newsubnode")); |
| 939 | ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 940 | ut_assert_console_end(); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 941 | |
| 942 | /* Test creation of new node in /test-node@1234 by alias over existing node */ |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 943 | ut_asserteq(1, run_commandf("fdt mknode testnodealias newersubnode")); |
| 944 | ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 945 | ut_assert_console_end(); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 946 | |
| 947 | /* Test creation of new node in non-existent node */ |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 948 | ut_asserteq(1, run_commandf("fdt mknode /no-node newnosubnode")); |
| 949 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 950 | ut_assert_console_end(); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 951 | |
| 952 | /* Test creation of new node in non-existent alias */ |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 953 | ut_asserteq(1, run_commandf("fdt mknode noalias newfailsubnode")); |
| 954 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 955 | ut_assert_console_end(); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 956 | |
| 957 | /* Test creation of new node in bad alias */ |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 958 | ut_asserteq(1, run_commandf("fdt mknode badalias newbadsubnode")); |
| 959 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 960 | ut_assert_console_end(); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 961 | |
| 962 | return 0; |
| 963 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 964 | FDT_TEST(fdt_test_mknode, UTF_CONSOLE); |
Marek Vasut | e4d26c9 | 2023-03-02 04:08:37 +0100 | [diff] [blame] | 965 | |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 966 | static int fdt_test_rm(struct unit_test_state *uts) |
| 967 | { |
| 968 | char fdt[4096]; |
| 969 | ulong addr; |
| 970 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 971 | ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 972 | |
| 973 | /* Test removal of property in root node / */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 974 | ut_assertok(run_commandf("fdt print / compatible")); |
| 975 | ut_assert_nextline("compatible = \"u-boot,fdt-test\""); |
| 976 | ut_assertok(run_commandf("fdt rm / compatible")); |
| 977 | ut_asserteq(1, run_commandf("fdt print / compatible")); |
| 978 | ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 979 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 980 | |
| 981 | /* Test removal of property clock-names in subnode /test-node@1234 */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 982 | ut_assertok(run_commandf("fdt print /test-node@1234 clock-names")); |
| 983 | ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\""); |
| 984 | ut_assertok(run_commandf("fdt rm /test-node@1234 clock-names")); |
| 985 | ut_asserteq(1, run_commandf("fdt print /test-node@1234 clock-names")); |
| 986 | ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 987 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 988 | |
| 989 | /* Test removal of property u-boot,empty-property in subnode /test-node@1234 by alias */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 990 | ut_assertok(run_commandf("fdt print testnodealias u-boot,empty-property")); |
| 991 | ut_assert_nextline("testnodealias u-boot,empty-property"); |
| 992 | ut_assertok(run_commandf("fdt rm testnodealias u-boot,empty-property")); |
| 993 | ut_asserteq(1, run_commandf("fdt print testnodealias u-boot,empty-property")); |
| 994 | ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 995 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 996 | |
| 997 | /* Test removal of non-existent property noprop in subnode /test-node@1234 */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 998 | ut_asserteq(1, run_commandf("fdt rm /test-node@1234 noprop")); |
| 999 | ut_assert_nextline("libfdt fdt_delprop(): FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1000 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1001 | |
| 1002 | /* Test removal of non-existent node /no-node@5678 */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1003 | ut_asserteq(1, run_commandf("fdt rm /no-node@5678")); |
| 1004 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1005 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1006 | |
| 1007 | /* Test removal of subnode /test-node@1234/subnode by alias */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1008 | ut_assertok(run_commandf("fdt rm subnodealias")); |
| 1009 | ut_asserteq(1, run_commandf("fdt print /test-node@1234/subnode")); |
| 1010 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1011 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1012 | |
| 1013 | /* Test removal of node by non-existent alias */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1014 | ut_asserteq(1, run_commandf("fdt rm noalias")); |
| 1015 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1016 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1017 | |
| 1018 | /* Test removal of node by bad alias */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1019 | ut_asserteq(1, run_commandf("fdt rm noalias")); |
| 1020 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1021 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1022 | |
| 1023 | /* Test removal of node /test-node@1234 */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1024 | ut_assertok(run_commandf("fdt rm /test-node@1234")); |
| 1025 | ut_asserteq(1, run_commandf("fdt print /test-node@1234")); |
| 1026 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1027 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1028 | |
| 1029 | /* Test removal of node / */ |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1030 | ut_assertok(run_commandf("fdt rm /")); |
| 1031 | ut_asserteq(1, run_commandf("fdt print /")); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1032 | ut_assert_console_end(); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1033 | |
| 1034 | return 0; |
| 1035 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 1036 | FDT_TEST(fdt_test_rm, UTF_CONSOLE); |
Marek Vasut | c3e350f | 2023-03-02 04:08:38 +0100 | [diff] [blame] | 1037 | |
Marek Vasut | 2081b1d | 2023-03-02 04:08:40 +0100 | [diff] [blame] | 1038 | static int fdt_test_bootcpu(struct unit_test_state *uts) |
| 1039 | { |
| 1040 | char fdt[256]; |
| 1041 | ulong addr; |
| 1042 | int i; |
| 1043 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 1044 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | 2081b1d | 2023-03-02 04:08:40 +0100 | [diff] [blame] | 1045 | |
| 1046 | /* Test getting default bootcpu entry */ |
Marek Vasut | 2081b1d | 2023-03-02 04:08:40 +0100 | [diff] [blame] | 1047 | ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys")); |
| 1048 | ut_asserteq(0, env_get_ulong("bootcpu", 10, 0x1234)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1049 | ut_assert_console_end(); |
Marek Vasut | 2081b1d | 2023-03-02 04:08:40 +0100 | [diff] [blame] | 1050 | |
| 1051 | /* Test setting and getting new bootcpu entry, twice, to test overwrite */ |
| 1052 | for (i = 42; i <= 43; i++) { |
Marek Vasut | 2081b1d | 2023-03-02 04:08:40 +0100 | [diff] [blame] | 1053 | ut_assertok(run_commandf("fdt bootcpu %d", i)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1054 | ut_assert_console_end(); |
Marek Vasut | 2081b1d | 2023-03-02 04:08:40 +0100 | [diff] [blame] | 1055 | |
| 1056 | /* Test getting new bootcpu entry */ |
Marek Vasut | 2081b1d | 2023-03-02 04:08:40 +0100 | [diff] [blame] | 1057 | ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys")); |
| 1058 | ut_asserteq(i, env_get_ulong("bootcpu", 10, 0x1234)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1059 | ut_assert_console_end(); |
Marek Vasut | 2081b1d | 2023-03-02 04:08:40 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 1064 | FDT_TEST(fdt_test_bootcpu, UTF_CONSOLE); |
Marek Vasut | 2081b1d | 2023-03-02 04:08:40 +0100 | [diff] [blame] | 1065 | |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1066 | static int fdt_test_header_get(struct unit_test_state *uts, |
| 1067 | const char *field, const unsigned long val) |
| 1068 | { |
| 1069 | /* Test getting valid header entry */ |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1070 | ut_assertok(run_commandf("fdt header get fvar %s", field)); |
| 1071 | ut_asserteq(val, env_get_hex("fvar", 0x1234)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1072 | ut_assert_console_end(); |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1073 | |
| 1074 | /* Test getting malformed header entry */ |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1075 | ut_asserteq(1, run_commandf("fdt header get fvar typo%stypo", field)); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1076 | ut_assert_console_end(); |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | static int fdt_test_header(struct unit_test_state *uts) |
| 1082 | { |
| 1083 | char fdt[256]; |
| 1084 | ulong addr; |
| 1085 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 1086 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1087 | |
| 1088 | /* Test header print */ |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1089 | ut_assertok(run_commandf("fdt header")); |
| 1090 | ut_assert_nextline("magic:\t\t\t0x%x", fdt_magic(fdt)); |
| 1091 | ut_assert_nextline("totalsize:\t\t0x%x (%d)", fdt_totalsize(fdt), fdt_totalsize(fdt)); |
| 1092 | ut_assert_nextline("off_dt_struct:\t\t0x%x", fdt_off_dt_struct(fdt)); |
| 1093 | ut_assert_nextline("off_dt_strings:\t\t0x%x", fdt_off_dt_strings(fdt)); |
| 1094 | ut_assert_nextline("off_mem_rsvmap:\t\t0x%x", fdt_off_mem_rsvmap(fdt)); |
| 1095 | ut_assert_nextline("version:\t\t%d", fdt_version(fdt)); |
| 1096 | ut_assert_nextline("last_comp_version:\t%d", fdt_last_comp_version(fdt)); |
| 1097 | ut_assert_nextline("boot_cpuid_phys:\t0x%x", fdt_boot_cpuid_phys(fdt)); |
| 1098 | ut_assert_nextline("size_dt_strings:\t0x%x", fdt_size_dt_strings(fdt)); |
| 1099 | ut_assert_nextline("size_dt_struct:\t\t0x%x", fdt_size_dt_struct(fdt)); |
| 1100 | ut_assert_nextline("number mem_rsv:\t\t0x%x", fdt_num_mem_rsv(fdt)); |
| 1101 | ut_assert_nextline_empty(); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1102 | ut_assert_console_end(); |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1103 | |
| 1104 | /* Test header get */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 1105 | ut_assertok(fdt_test_header_get(uts, "magic", fdt_magic(fdt))); |
| 1106 | ut_assertok(fdt_test_header_get(uts, "totalsize", fdt_totalsize(fdt))); |
| 1107 | ut_assertok(fdt_test_header_get(uts, "off_dt_struct", |
| 1108 | fdt_off_dt_struct(fdt))); |
| 1109 | ut_assertok(fdt_test_header_get(uts, "off_dt_strings", |
| 1110 | fdt_off_dt_strings(fdt))); |
| 1111 | ut_assertok(fdt_test_header_get(uts, "off_mem_rsvmap", |
| 1112 | fdt_off_mem_rsvmap(fdt))); |
| 1113 | ut_assertok(fdt_test_header_get(uts, "version", fdt_version(fdt))); |
| 1114 | ut_assertok(fdt_test_header_get(uts, "last_comp_version", |
| 1115 | fdt_last_comp_version(fdt))); |
| 1116 | ut_assertok(fdt_test_header_get(uts, "boot_cpuid_phys", |
| 1117 | fdt_boot_cpuid_phys(fdt))); |
| 1118 | ut_assertok(fdt_test_header_get(uts, "size_dt_strings", |
| 1119 | fdt_size_dt_strings(fdt))); |
| 1120 | ut_assertok(fdt_test_header_get(uts, "size_dt_struct", |
| 1121 | fdt_size_dt_struct(fdt))); |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1122 | |
| 1123 | return 0; |
| 1124 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 1125 | FDT_TEST(fdt_test_header, UTF_CONSOLE); |
Marek Vasut | 06c13ed | 2023-03-02 04:08:39 +0100 | [diff] [blame] | 1126 | |
Marek Vasut | 65b6f46 | 2023-03-02 04:08:41 +0100 | [diff] [blame] | 1127 | static int fdt_test_memory_cells(struct unit_test_state *uts, |
| 1128 | const unsigned int cells) |
| 1129 | { |
| 1130 | unsigned char *pada, *pads; |
| 1131 | unsigned char *seta, *sets; |
| 1132 | char fdt[8192]; |
| 1133 | const int size = sizeof(fdt); |
| 1134 | fdt32_t *regs; |
| 1135 | ulong addr; |
| 1136 | char *spc; |
| 1137 | int i; |
| 1138 | |
| 1139 | /* Create DT with node /memory { regs = <0x100 0x200>; } and #*cells */ |
| 1140 | ut_assertnonnull(regs = calloc(2 * cells, sizeof(*regs))); |
| 1141 | ut_assertnonnull(pada = calloc(12, cells)); |
| 1142 | ut_assertnonnull(pads = calloc(12, cells)); |
| 1143 | ut_assertnonnull(seta = calloc(12, cells)); |
| 1144 | ut_assertnonnull(sets = calloc(12, cells)); |
| 1145 | for (i = cells; i >= 1; i--) { |
| 1146 | regs[cells - 1] = cpu_to_fdt32(i * 0x10000); |
| 1147 | regs[(cells * 2) - 1] = cpu_to_fdt32(~i); |
| 1148 | snprintf(seta + (8 * (cells - i)), 9, "%08x", i * 0x10000); |
| 1149 | snprintf(sets + (8 * (cells - i)), 9, "%08x", ~i); |
| 1150 | spc = (i != 1) ? " " : ""; |
| 1151 | snprintf(pada + (11 * (cells - i)), 12, "0x%08x%s", i * 0x10000, spc); |
| 1152 | snprintf(pads + (11 * (cells - i)), 12, "0x%08x%s", ~i, spc); |
| 1153 | } |
| 1154 | |
| 1155 | ut_assertok(fdt_create(fdt, size)); |
| 1156 | ut_assertok(fdt_finish_reservemap(fdt)); |
| 1157 | ut_assert(fdt_begin_node(fdt, "") >= 0); |
| 1158 | ut_assertok(fdt_property_u32(fdt, "#address-cells", cells)); |
| 1159 | ut_assertok(fdt_property_u32(fdt, "#size-cells", cells)); |
| 1160 | ut_assert(fdt_begin_node(fdt, "memory") >= 0); |
| 1161 | ut_assertok(fdt_property_string(fdt, "device_type", "memory")); |
| 1162 | ut_assertok(fdt_property(fdt, "reg", ®s, cells * 2)); |
| 1163 | ut_assertok(fdt_end_node(fdt)); |
| 1164 | ut_assertok(fdt_end_node(fdt)); |
| 1165 | ut_assertok(fdt_finish(fdt)); |
| 1166 | fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */ |
| 1167 | addr = map_to_sysmem(fdt); |
| 1168 | set_working_fdt_addr(addr); |
Simon Glass | 5622825 | 2024-08-22 07:57:58 -0600 | [diff] [blame] | 1169 | ut_assert_nextline("Working FDT set to %lx", addr); |
Marek Vasut | 65b6f46 | 2023-03-02 04:08:41 +0100 | [diff] [blame] | 1170 | |
| 1171 | /* Test updating the memory node */ |
Marek Vasut | 65b6f46 | 2023-03-02 04:08:41 +0100 | [diff] [blame] | 1172 | ut_assertok(run_commandf("fdt memory 0x%s 0x%s", seta, sets)); |
| 1173 | ut_assertok(run_commandf("fdt print /memory")); |
| 1174 | ut_assert_nextline("memory {"); |
| 1175 | ut_assert_nextline("\tdevice_type = \"memory\";"); |
| 1176 | ut_assert_nextline("\treg = <%s %s>;", pada, pads); |
| 1177 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1178 | ut_assert_console_end(); |
Marek Vasut | 65b6f46 | 2023-03-02 04:08:41 +0100 | [diff] [blame] | 1179 | |
| 1180 | free(sets); |
| 1181 | free(seta); |
| 1182 | free(pads); |
| 1183 | free(pada); |
| 1184 | free(regs); |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | static int fdt_test_memory(struct unit_test_state *uts) |
| 1190 | { |
| 1191 | /* |
| 1192 | * Test memory fixup for 32 and 64 bit systems, anything bigger is |
| 1193 | * so far unsupported and fails because of simple_stroull() being |
| 1194 | * 64bit tops in the 'fdt memory' command implementation. |
| 1195 | */ |
Simon Glass | f80a914 | 2024-08-22 07:57:55 -0600 | [diff] [blame] | 1196 | ut_assertok(fdt_test_memory_cells(uts, 1)); |
| 1197 | ut_assertok(fdt_test_memory_cells(uts, 2)); |
Marek Vasut | 65b6f46 | 2023-03-02 04:08:41 +0100 | [diff] [blame] | 1198 | |
| 1199 | /* |
| 1200 | * The 'fdt memory' command is limited to /memory node, it does |
| 1201 | * not support any other valid DT memory node format, which is |
| 1202 | * either one or multiple /memory@adresss nodes. Therefore, this |
| 1203 | * DT variant is not tested here. |
| 1204 | */ |
| 1205 | |
| 1206 | return 0; |
| 1207 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 1208 | FDT_TEST(fdt_test_memory, UTF_CONSOLE); |
Marek Vasut | 65b6f46 | 2023-03-02 04:08:41 +0100 | [diff] [blame] | 1209 | |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1210 | static int fdt_test_rsvmem(struct unit_test_state *uts) |
| 1211 | { |
| 1212 | char fdt[8192]; |
| 1213 | ulong addr; |
| 1214 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 1215 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1216 | fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */ |
| 1217 | fdt_add_mem_rsv(fdt, 0x42, 0x1701); |
| 1218 | fdt_add_mem_rsv(fdt, 0x74656, 0x9); |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1219 | |
| 1220 | /* Test default reserved memory node presence */ |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1221 | ut_assertok(run_commandf("fdt rsvmem print")); |
| 1222 | ut_assert_nextline("index\t\t start\t\t size"); |
| 1223 | ut_assert_nextline("------------------------------------------------"); |
| 1224 | ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701); |
| 1225 | ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1226 | ut_assert_console_end(); |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1227 | |
| 1228 | /* Test add new reserved memory node */ |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1229 | ut_assertok(run_commandf("fdt rsvmem add 0x1234 0x5678")); |
| 1230 | ut_assertok(run_commandf("fdt rsvmem print")); |
| 1231 | ut_assert_nextline("index\t\t start\t\t size"); |
| 1232 | ut_assert_nextline("------------------------------------------------"); |
| 1233 | ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701); |
| 1234 | ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9); |
| 1235 | ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x1234, 0x5678); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1236 | ut_assert_console_end(); |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1237 | |
| 1238 | /* Test delete reserved memory node */ |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1239 | ut_assertok(run_commandf("fdt rsvmem delete 0")); |
| 1240 | ut_assertok(run_commandf("fdt rsvmem print")); |
| 1241 | ut_assert_nextline("index\t\t start\t\t size"); |
| 1242 | ut_assert_nextline("------------------------------------------------"); |
| 1243 | ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9); |
| 1244 | ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1245 | ut_assert_console_end(); |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1246 | |
| 1247 | /* Test re-add new reserved memory node */ |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1248 | ut_assertok(run_commandf("fdt rsvmem add 0x42 0x1701")); |
| 1249 | ut_assertok(run_commandf("fdt rsvmem print")); |
| 1250 | ut_assert_nextline("index\t\t start\t\t size"); |
| 1251 | ut_assert_nextline("------------------------------------------------"); |
| 1252 | ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9); |
| 1253 | ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678); |
| 1254 | ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x42, 0x1701); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1255 | ut_assert_console_end(); |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1256 | |
| 1257 | /* Test delete nonexistent reserved memory node */ |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1258 | ut_asserteq(1, run_commandf("fdt rsvmem delete 10")); |
| 1259 | ut_assert_nextline("libfdt fdt_del_mem_rsv(): FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1260 | ut_assert_console_end(); |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1261 | |
| 1262 | return 0; |
| 1263 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 1264 | FDT_TEST(fdt_test_rsvmem, UTF_CONSOLE); |
Marek Vasut | cd162df | 2023-03-02 04:08:42 +0100 | [diff] [blame] | 1265 | |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1266 | static int fdt_test_chosen(struct unit_test_state *uts) |
| 1267 | { |
| 1268 | const char *env_bootargs = env_get("bootargs"); |
| 1269 | char fdt[8192]; |
| 1270 | ulong addr; |
| 1271 | |
Simon Glass | 27c46ab | 2024-08-22 07:57:56 -0600 | [diff] [blame] | 1272 | ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr)); |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1273 | fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */ |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1274 | |
| 1275 | /* Test default chosen node presence, fail as there is no /chosen node */ |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1276 | ut_asserteq(1, run_commandf("fdt print /chosen")); |
| 1277 | ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1278 | ut_assert_console_end(); |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1279 | |
| 1280 | /* Test add new chosen node without initrd */ |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1281 | ut_assertok(run_commandf("fdt chosen")); |
| 1282 | ut_assertok(run_commandf("fdt print /chosen")); |
| 1283 | ut_assert_nextline("chosen {"); |
| 1284 | ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */ |
| 1285 | if (env_bootargs) |
| 1286 | ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs); |
Tim Harvey | 009d156 | 2024-06-18 14:06:09 -0700 | [diff] [blame] | 1287 | if (IS_ENABLED(CONFIG_DM_RNG) && |
| 1288 | !IS_ENABLED(CONFIG_MEASURED_BOOT) && |
| 1289 | !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)) |
| 1290 | ut_assert_nextlinen("\tkaslr-seed = "); |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1291 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1292 | ut_assert_console_end(); |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1293 | |
| 1294 | /* Test add new chosen node with initrd */ |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1295 | ut_assertok(run_commandf("fdt chosen 0x1234 0x5678")); |
| 1296 | ut_assertok(run_commandf("fdt print /chosen")); |
| 1297 | ut_assert_nextline("chosen {"); |
| 1298 | ut_assert_nextline("\tlinux,initrd-end = <0x%08x 0x%08x>;", |
| 1299 | upper_32_bits(0x1234 + 0x5678 - 1), |
| 1300 | lower_32_bits(0x1234 + 0x5678 - 1)); |
| 1301 | ut_assert_nextline("\tlinux,initrd-start = <0x%08x 0x%08x>;", |
| 1302 | upper_32_bits(0x1234), lower_32_bits(0x1234)); |
| 1303 | ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */ |
| 1304 | if (env_bootargs) |
| 1305 | ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs); |
Tim Harvey | 009d156 | 2024-06-18 14:06:09 -0700 | [diff] [blame] | 1306 | if (IS_ENABLED(CONFIG_DM_RNG) && |
| 1307 | !IS_ENABLED(CONFIG_MEASURED_BOOT) && |
| 1308 | !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)) |
| 1309 | ut_assert_nextlinen("\tkaslr-seed = "); |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1310 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1311 | ut_assert_console_end(); |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1312 | |
| 1313 | return 0; |
| 1314 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 1315 | FDT_TEST(fdt_test_chosen, UTF_CONSOLE); |
Marek Vasut | 8d450da | 2023-03-02 04:08:43 +0100 | [diff] [blame] | 1316 | |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1317 | static int fdt_test_apply(struct unit_test_state *uts) |
| 1318 | { |
| 1319 | char fdt[8192], fdto[8192]; |
| 1320 | ulong addr, addro; |
| 1321 | |
| 1322 | /* Create base DT with __symbols__ node */ |
| 1323 | ut_assertok(fdt_create(fdt, sizeof(fdt))); |
| 1324 | ut_assertok(fdt_finish_reservemap(fdt)); |
| 1325 | ut_assert(fdt_begin_node(fdt, "") >= 0); |
| 1326 | ut_assert(fdt_begin_node(fdt, "__symbols__") >= 0); |
| 1327 | ut_assertok(fdt_end_node(fdt)); |
| 1328 | ut_assertok(fdt_end_node(fdt)); |
| 1329 | ut_assertok(fdt_finish(fdt)); |
| 1330 | fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */ |
| 1331 | addr = map_to_sysmem(fdt); |
| 1332 | set_working_fdt_addr(addr); |
Simon Glass | 5622825 | 2024-08-22 07:57:58 -0600 | [diff] [blame] | 1333 | ut_assert_nextline("Working FDT set to %lx", addr); |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1334 | |
| 1335 | /* Create DTO which adds single property to root node / */ |
| 1336 | ut_assertok(fdt_create(fdto, sizeof(fdto))); |
| 1337 | ut_assertok(fdt_finish_reservemap(fdto)); |
| 1338 | ut_assert(fdt_begin_node(fdto, "") >= 0); |
| 1339 | ut_assert(fdt_begin_node(fdto, "fragment") >= 0); |
| 1340 | ut_assertok(fdt_property_string(fdto, "target-path", "/")); |
| 1341 | ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0); |
| 1342 | ut_assertok(fdt_property_string(fdto, "newstring", "newvalue")); |
| 1343 | ut_assertok(fdt_end_node(fdto)); |
| 1344 | ut_assertok(fdt_end_node(fdto)); |
| 1345 | ut_assertok(fdt_finish(fdto)); |
| 1346 | addro = map_to_sysmem(fdto); |
| 1347 | |
| 1348 | /* Test default DT print */ |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1349 | ut_assertok(run_commandf("fdt print /")); |
| 1350 | ut_assert_nextline("/ {"); |
| 1351 | ut_assert_nextline("\t__symbols__ {"); |
| 1352 | ut_assert_nextline("\t};"); |
| 1353 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1354 | ut_assert_console_end(); |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1355 | |
| 1356 | /* Test simple DTO application */ |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 1357 | ut_assertok(run_commandf("fdt apply 0x%08lx", addro)); |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1358 | ut_assertok(run_commandf("fdt print /")); |
| 1359 | ut_assert_nextline("/ {"); |
| 1360 | ut_assert_nextline("\tnewstring = \"newvalue\";"); |
| 1361 | ut_assert_nextline("\t__symbols__ {"); |
| 1362 | ut_assert_nextline("\t};"); |
| 1363 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1364 | ut_assert_console_end(); |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1365 | |
| 1366 | /* |
| 1367 | * Create complex DTO which: |
| 1368 | * - modifies newstring property in root node / |
| 1369 | * - adds new properties to root node / |
| 1370 | * - adds new subnode with properties to root node / |
| 1371 | * - adds phandle to the subnode and therefore __symbols__ node |
| 1372 | */ |
| 1373 | ut_assertok(fdt_create(fdto, sizeof(fdto))); |
| 1374 | ut_assertok(fdt_finish_reservemap(fdto)); |
| 1375 | ut_assert(fdt_begin_node(fdto, "") >= 0); |
| 1376 | ut_assertok(fdt_property_cell(fdto, "#address-cells", 1)); |
| 1377 | ut_assertok(fdt_property_cell(fdto, "#size-cells", 0)); |
| 1378 | |
| 1379 | ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0); |
| 1380 | ut_assertok(fdt_property_string(fdto, "target-path", "/")); |
| 1381 | ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0); |
| 1382 | ut_assertok(fdt_property_string(fdto, "newstring", "newervalue")); |
| 1383 | ut_assertok(fdt_property_u32(fdto, "newu32", 0x12345678)); |
| 1384 | ut_assertok(fdt_property(fdto, "empty-property", NULL, 0)); |
| 1385 | ut_assert(fdt_begin_node(fdto, "subnode") >= 0); |
| 1386 | ut_assertok(fdt_property_string(fdto, "subnewstring", "newervalue")); |
| 1387 | ut_assertok(fdt_property_u32(fdto, "subnewu32", 0x12345678)); |
| 1388 | ut_assertok(fdt_property(fdto, "subempty-property", NULL, 0)); |
| 1389 | ut_assertok(fdt_property_u32(fdto, "phandle", 0x01)); |
| 1390 | ut_assertok(fdt_end_node(fdto)); |
| 1391 | ut_assertok(fdt_end_node(fdto)); |
| 1392 | ut_assertok(fdt_end_node(fdto)); |
| 1393 | |
| 1394 | ut_assert(fdt_begin_node(fdto, "__symbols__") >= 0); |
| 1395 | ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0/__overlay__/subnode")); |
| 1396 | ut_assertok(fdt_end_node(fdto)); |
| 1397 | ut_assertok(fdt_finish(fdto)); |
| 1398 | addro = map_to_sysmem(fdto); |
| 1399 | |
| 1400 | /* Test complex DTO application */ |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 1401 | ut_assertok(run_commandf("fdt apply 0x%08lx", addro)); |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1402 | ut_assertok(run_commandf("fdt print /")); |
| 1403 | ut_assert_nextline("/ {"); |
| 1404 | ut_assert_nextline("\tempty-property;"); |
| 1405 | ut_assert_nextline("\tnewu32 = <0x12345678>;"); |
| 1406 | ut_assert_nextline("\tnewstring = \"newervalue\";"); |
| 1407 | ut_assert_nextline("\tsubnode {"); |
| 1408 | ut_assert_nextline("\t\tphandle = <0x00000001>;"); |
| 1409 | ut_assert_nextline("\t\tsubempty-property;"); |
| 1410 | ut_assert_nextline("\t\tsubnewu32 = <0x12345678>;"); |
| 1411 | ut_assert_nextline("\t\tsubnewstring = \"newervalue\";"); |
| 1412 | ut_assert_nextline("\t};"); |
| 1413 | ut_assert_nextline("\t__symbols__ {"); |
| 1414 | ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";"); |
| 1415 | ut_assert_nextline("\t};"); |
| 1416 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1417 | ut_assert_console_end(); |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1418 | |
| 1419 | /* |
| 1420 | * Create complex DTO which: |
| 1421 | * - modifies subnewu32 property in subnode via phandle and uses __fixups__ node |
| 1422 | */ |
| 1423 | ut_assertok(fdt_create(fdto, sizeof(fdto))); |
| 1424 | ut_assertok(fdt_finish_reservemap(fdto)); |
| 1425 | ut_assert(fdt_begin_node(fdto, "") >= 0); |
| 1426 | ut_assertok(fdt_property_cell(fdto, "#address-cells", 1)); |
| 1427 | ut_assertok(fdt_property_cell(fdto, "#size-cells", 0)); |
| 1428 | |
| 1429 | ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0); |
| 1430 | ut_assertok(fdt_property_u32(fdto, "target", 0xffffffff)); |
| 1431 | ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0); |
| 1432 | ut_assertok(fdt_property_u32(fdto, "subnewu32", 0xabcdef01)); |
| 1433 | ut_assertok(fdt_end_node(fdto)); |
| 1434 | ut_assertok(fdt_end_node(fdto)); |
| 1435 | |
| 1436 | ut_assert(fdt_begin_node(fdto, "__fixups__") >= 0); |
| 1437 | ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0:target:0")); |
| 1438 | ut_assertok(fdt_end_node(fdto)); |
| 1439 | ut_assertok(fdt_end_node(fdto)); |
| 1440 | ut_assertok(fdt_finish(fdto)); |
| 1441 | addro = map_to_sysmem(fdto); |
| 1442 | |
| 1443 | /* Test complex DTO application */ |
Evgeny Bachinin | e604bc0 | 2023-03-20 11:23:13 +0300 | [diff] [blame] | 1444 | ut_assertok(run_commandf("fdt apply 0x%08lx", addro)); |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1445 | ut_assertok(run_commandf("fdt print /")); |
| 1446 | ut_assert_nextline("/ {"); |
| 1447 | ut_assert_nextline("\tempty-property;"); |
| 1448 | ut_assert_nextline("\tnewu32 = <0x12345678>;"); |
| 1449 | ut_assert_nextline("\tnewstring = \"newervalue\";"); |
| 1450 | ut_assert_nextline("\tsubnode {"); |
| 1451 | ut_assert_nextline("\t\tphandle = <0x00000001>;"); |
| 1452 | ut_assert_nextline("\t\tsubempty-property;"); |
| 1453 | ut_assert_nextline("\t\tsubnewu32 = <0xabcdef01>;"); |
| 1454 | ut_assert_nextline("\t\tsubnewstring = \"newervalue\";"); |
| 1455 | ut_assert_nextline("\t};"); |
| 1456 | ut_assert_nextline("\t__symbols__ {"); |
| 1457 | ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";"); |
| 1458 | ut_assert_nextline("\t};"); |
| 1459 | ut_assert_nextline("};"); |
Simon Glass | c579bd4 | 2024-08-22 07:58:03 -0600 | [diff] [blame] | 1460 | ut_assert_console_end(); |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1461 | |
| 1462 | return 0; |
| 1463 | } |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 1464 | FDT_TEST(fdt_test_apply, UTF_CONSOLE); |
Marek Vasut | dc0ccea | 2023-03-02 04:08:44 +0100 | [diff] [blame] | 1465 | |
Simon Glass | f3c6a1d | 2022-07-13 06:06:59 -0600 | [diff] [blame] | 1466 | int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 1467 | { |
| 1468 | struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test); |
| 1469 | const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test); |
| 1470 | |
| 1471 | return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv); |
| 1472 | } |