Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 NextThing Co |
| 4 | * Copyright (c) 2016 Free Electrons |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 7 | #include <command.h> |
| 8 | #include <errno.h> |
Heinrich Schuchardt | b210fc7 | 2018-10-11 02:16:46 +0200 | [diff] [blame] | 9 | #include <fdt_support.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 10 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | |
| 14 | #include <linux/sizes.h> |
| 15 | |
| 16 | #include <test/ut.h> |
| 17 | #include <test/overlay.h> |
Simon Glass | 17e9903 | 2017-11-25 11:57:30 -0700 | [diff] [blame] | 18 | #include <test/suites.h> |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 19 | |
| 20 | /* 4k ought to be enough for anybody */ |
| 21 | #define FDT_COPY_SIZE (4 * SZ_1K) |
| 22 | |
| 23 | extern u32 __dtb_test_fdt_base_begin; |
| 24 | extern u32 __dtb_test_fdt_overlay_begin; |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 25 | extern u32 __dtb_test_fdt_overlay_stacked_begin; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 26 | |
Heinrich Schuchardt | a418900 | 2018-12-08 09:53:05 +0100 | [diff] [blame] | 27 | static void *fdt; |
| 28 | |
Pantelis Antoniou | 6865002 | 2017-09-04 23:12:22 +0300 | [diff] [blame] | 29 | static int ut_fdt_getprop_u32_by_index(void *fdt, const char *path, |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 30 | const char *name, int index, |
| 31 | u32 *out) |
| 32 | { |
| 33 | const fdt32_t *val; |
| 34 | int node_off; |
| 35 | int len; |
| 36 | |
| 37 | node_off = fdt_path_offset(fdt, path); |
| 38 | if (node_off < 0) |
| 39 | return node_off; |
| 40 | |
| 41 | val = fdt_getprop(fdt, node_off, name, &len); |
| 42 | if (!val || (len < (sizeof(uint32_t) * (index + 1)))) |
| 43 | return -FDT_ERR_NOTFOUND; |
| 44 | |
| 45 | *out = fdt32_to_cpu(*(val + index)); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
Pantelis Antoniou | 6865002 | 2017-09-04 23:12:22 +0300 | [diff] [blame] | 50 | static int ut_fdt_getprop_u32(void *fdt, const char *path, const char *name, |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 51 | u32 *out) |
| 52 | { |
Pantelis Antoniou | 6865002 | 2017-09-04 23:12:22 +0300 | [diff] [blame] | 53 | return ut_fdt_getprop_u32_by_index(fdt, path, name, 0, out); |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static int fdt_getprop_str(void *fdt, const char *path, const char *name, |
| 57 | const char **out) |
| 58 | { |
| 59 | int node_off; |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 60 | int len; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 61 | |
| 62 | node_off = fdt_path_offset(fdt, path); |
| 63 | if (node_off < 0) |
| 64 | return node_off; |
| 65 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 66 | *out = fdt_stringlist_get(fdt, node_off, name, 0, &len); |
| 67 | |
| 68 | return len < 0 ? len : 0; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static int fdt_overlay_change_int_property(struct unit_test_state *uts) |
| 72 | { |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 73 | u32 val = 0; |
| 74 | |
Pantelis Antoniou | 6865002 | 2017-09-04 23:12:22 +0300 | [diff] [blame] | 75 | ut_assertok(ut_fdt_getprop_u32(fdt, "/test-node", "test-int-property", |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 76 | &val)); |
| 77 | ut_asserteq(43, val); |
| 78 | |
| 79 | return CMD_RET_SUCCESS; |
| 80 | } |
| 81 | OVERLAY_TEST(fdt_overlay_change_int_property, 0); |
| 82 | |
| 83 | static int fdt_overlay_change_str_property(struct unit_test_state *uts) |
| 84 | { |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 85 | const char *val = NULL; |
| 86 | |
| 87 | ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property", |
| 88 | &val)); |
| 89 | ut_asserteq_str("foobar", val); |
| 90 | |
| 91 | return CMD_RET_SUCCESS; |
| 92 | } |
| 93 | OVERLAY_TEST(fdt_overlay_change_str_property, 0); |
| 94 | |
| 95 | static int fdt_overlay_add_str_property(struct unit_test_state *uts) |
| 96 | { |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 97 | const char *val = NULL; |
| 98 | |
| 99 | ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2", |
| 100 | &val)); |
| 101 | ut_asserteq_str("foobar2", val); |
| 102 | |
| 103 | return CMD_RET_SUCCESS; |
| 104 | } |
| 105 | OVERLAY_TEST(fdt_overlay_add_str_property, 0); |
| 106 | |
| 107 | static int fdt_overlay_add_node_by_phandle(struct unit_test_state *uts) |
| 108 | { |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 109 | int off; |
| 110 | |
| 111 | off = fdt_path_offset(fdt, "/test-node/new-node"); |
| 112 | ut_assert(off >= 0); |
| 113 | |
| 114 | ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL)); |
| 115 | |
| 116 | return CMD_RET_SUCCESS; |
| 117 | } |
| 118 | OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0); |
| 119 | |
| 120 | static int fdt_overlay_add_node_by_path(struct unit_test_state *uts) |
| 121 | { |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 122 | int off; |
| 123 | |
| 124 | off = fdt_path_offset(fdt, "/new-node"); |
| 125 | ut_assert(off >= 0); |
| 126 | |
| 127 | ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL)); |
| 128 | |
| 129 | return CMD_RET_SUCCESS; |
| 130 | } |
| 131 | OVERLAY_TEST(fdt_overlay_add_node_by_path, 0); |
| 132 | |
| 133 | static int fdt_overlay_add_subnode_property(struct unit_test_state *uts) |
| 134 | { |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 135 | int off; |
| 136 | |
| 137 | off = fdt_path_offset(fdt, "/test-node/sub-test-node"); |
| 138 | ut_assert(off >= 0); |
| 139 | |
| 140 | ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL)); |
| 141 | ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL)); |
| 142 | |
| 143 | return CMD_RET_SUCCESS; |
| 144 | } |
| 145 | OVERLAY_TEST(fdt_overlay_add_subnode_property, 0); |
| 146 | |
| 147 | static int fdt_overlay_local_phandle(struct unit_test_state *uts) |
| 148 | { |
| 149 | uint32_t local_phandle; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 150 | u32 val = 0; |
| 151 | int off; |
| 152 | |
| 153 | off = fdt_path_offset(fdt, "/new-local-node"); |
| 154 | ut_assert(off >= 0); |
| 155 | |
| 156 | local_phandle = fdt_get_phandle(fdt, off); |
| 157 | ut_assert(local_phandle); |
| 158 | |
Pantelis Antoniou | 6865002 | 2017-09-04 23:12:22 +0300 | [diff] [blame] | 159 | ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle", |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 160 | 0, &val)); |
| 161 | ut_asserteq(local_phandle, val); |
| 162 | |
Pantelis Antoniou | 6865002 | 2017-09-04 23:12:22 +0300 | [diff] [blame] | 163 | ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle", |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 164 | 1, &val)); |
| 165 | ut_asserteq(local_phandle, val); |
| 166 | |
| 167 | return CMD_RET_SUCCESS; |
| 168 | } |
| 169 | OVERLAY_TEST(fdt_overlay_local_phandle, 0); |
| 170 | |
| 171 | static int fdt_overlay_local_phandles(struct unit_test_state *uts) |
| 172 | { |
| 173 | uint32_t local_phandle, test_phandle; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 174 | u32 val = 0; |
| 175 | int off; |
| 176 | |
| 177 | off = fdt_path_offset(fdt, "/new-local-node"); |
| 178 | ut_assert(off >= 0); |
| 179 | |
| 180 | local_phandle = fdt_get_phandle(fdt, off); |
| 181 | ut_assert(local_phandle); |
| 182 | |
| 183 | off = fdt_path_offset(fdt, "/test-node"); |
| 184 | ut_assert(off >= 0); |
| 185 | |
| 186 | test_phandle = fdt_get_phandle(fdt, off); |
| 187 | ut_assert(test_phandle); |
| 188 | |
Pantelis Antoniou | 6865002 | 2017-09-04 23:12:22 +0300 | [diff] [blame] | 189 | ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0, |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 190 | &val)); |
| 191 | ut_asserteq(test_phandle, val); |
| 192 | |
Pantelis Antoniou | 6865002 | 2017-09-04 23:12:22 +0300 | [diff] [blame] | 193 | ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1, |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 194 | &val)); |
| 195 | ut_asserteq(local_phandle, val); |
| 196 | |
| 197 | return CMD_RET_SUCCESS; |
| 198 | } |
| 199 | OVERLAY_TEST(fdt_overlay_local_phandles, 0); |
| 200 | |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 201 | static int fdt_overlay_stacked(struct unit_test_state *uts) |
| 202 | { |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 203 | u32 val = 0; |
| 204 | |
| 205 | ut_assertok(ut_fdt_getprop_u32(fdt, "/new-local-node", |
| 206 | "stacked-test-int-property", &val)); |
| 207 | ut_asserteq(43, val); |
| 208 | |
| 209 | return CMD_RET_SUCCESS; |
| 210 | } |
| 211 | OVERLAY_TEST(fdt_overlay_stacked, 0); |
| 212 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 213 | int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 214 | { |
Simon Glass | b50211f | 2021-03-07 17:35:10 -0700 | [diff] [blame] | 215 | struct unit_test *tests = UNIT_TEST_SUITE_START(overlay_test); |
| 216 | const int n_ents = UNIT_TEST_SUITE_COUNT(overlay_test); |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 217 | struct unit_test_state *uts; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 218 | void *fdt_base = &__dtb_test_fdt_base_begin; |
| 219 | void *fdt_overlay = &__dtb_test_fdt_overlay_begin; |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 220 | void *fdt_overlay_stacked = &__dtb_test_fdt_overlay_stacked_begin; |
Heinrich Schuchardt | a418900 | 2018-12-08 09:53:05 +0100 | [diff] [blame] | 221 | void *fdt_overlay_copy, *fdt_overlay_stacked_copy; |
Tom Rini | aaa968b | 2017-09-26 12:43:12 -0400 | [diff] [blame] | 222 | int ret = -ENOMEM; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 223 | |
| 224 | uts = calloc(1, sizeof(*uts)); |
| 225 | if (!uts) |
| 226 | return -ENOMEM; |
| 227 | |
| 228 | ut_assertok(fdt_check_header(fdt_base)); |
| 229 | ut_assertok(fdt_check_header(fdt_overlay)); |
| 230 | |
Heinrich Schuchardt | a418900 | 2018-12-08 09:53:05 +0100 | [diff] [blame] | 231 | fdt = malloc(FDT_COPY_SIZE); |
| 232 | if (!fdt) |
Tom Rini | aaa968b | 2017-09-26 12:43:12 -0400 | [diff] [blame] | 233 | goto err1; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 234 | |
| 235 | fdt_overlay_copy = malloc(FDT_COPY_SIZE); |
| 236 | if (!fdt_overlay_copy) |
Tom Rini | aaa968b | 2017-09-26 12:43:12 -0400 | [diff] [blame] | 237 | goto err2; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 238 | |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 239 | fdt_overlay_stacked_copy = malloc(FDT_COPY_SIZE); |
| 240 | if (!fdt_overlay_stacked_copy) |
Tom Rini | aaa968b | 2017-09-26 12:43:12 -0400 | [diff] [blame] | 241 | goto err3; |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 242 | |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 243 | /* |
| 244 | * Resize the FDT to 4k so that we have room to operate on |
| 245 | * |
| 246 | * (and relocate it since the memory might be mapped |
| 247 | * read-only) |
| 248 | */ |
Heinrich Schuchardt | a418900 | 2018-12-08 09:53:05 +0100 | [diff] [blame] | 249 | ut_assertok(fdt_open_into(fdt_base, fdt, FDT_COPY_SIZE)); |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * Resize the overlay to 4k so that we have room to operate on |
| 253 | * |
| 254 | * (and relocate it since the memory might be mapped |
| 255 | * read-only) |
| 256 | */ |
| 257 | ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy, |
| 258 | FDT_COPY_SIZE)); |
| 259 | |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 260 | /* |
| 261 | * Resize the stacked overlay to 4k so that we have room to operate on |
| 262 | * |
| 263 | * (and relocate it since the memory might be mapped |
| 264 | * read-only) |
| 265 | */ |
| 266 | ut_assertok(fdt_open_into(fdt_overlay_stacked, fdt_overlay_stacked_copy, |
| 267 | FDT_COPY_SIZE)); |
| 268 | |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 269 | /* Apply the overlay */ |
Heinrich Schuchardt | a418900 | 2018-12-08 09:53:05 +0100 | [diff] [blame] | 270 | ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_copy)); |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 271 | |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 272 | /* Apply the stacked overlay */ |
Heinrich Schuchardt | a418900 | 2018-12-08 09:53:05 +0100 | [diff] [blame] | 273 | ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_stacked_copy)); |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 274 | |
Philippe Reynes | 1f99f84 | 2019-12-17 19:07:04 +0100 | [diff] [blame] | 275 | ret = cmd_ut_category("overlay", "", tests, n_ents, argc, argv); |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 276 | |
Pantelis Antoniou | ccdc28b | 2017-09-04 23:12:23 +0300 | [diff] [blame] | 277 | free(fdt_overlay_stacked_copy); |
Tom Rini | aaa968b | 2017-09-26 12:43:12 -0400 | [diff] [blame] | 278 | err3: |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 279 | free(fdt_overlay_copy); |
Tom Rini | aaa968b | 2017-09-26 12:43:12 -0400 | [diff] [blame] | 280 | err2: |
Heinrich Schuchardt | a418900 | 2018-12-08 09:53:05 +0100 | [diff] [blame] | 281 | free(fdt); |
Tom Rini | aaa968b | 2017-09-26 12:43:12 -0400 | [diff] [blame] | 282 | err1: |
Tom Rini | aaa968b | 2017-09-26 12:43:12 -0400 | [diff] [blame] | 283 | return ret; |
Maxime Ripard | 0e31a11 | 2016-07-05 10:26:46 +0200 | [diff] [blame] | 284 | } |