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