2 * Copyright (c) 2016 NextThing Co
3 * Copyright (c) 2016 Free Electrons
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/sizes.h>
16 #include <test/overlay.h>
18 /* 4k ought to be enough for anybody */
19 #define FDT_COPY_SIZE (4 * SZ_1K)
21 extern u32 __dtb_test_fdt_base_begin;
22 extern u32 __dtb_test_fdt_overlay_begin;
23 extern u32 __dtb_test_fdt_overlay_stacked_begin;
25 static int ut_fdt_getprop_u32_by_index(void *fdt, const char *path,
26 const char *name, int index,
33 node_off = fdt_path_offset(fdt, path);
37 val = fdt_getprop(fdt, node_off, name, &len);
38 if (!val || (len < (sizeof(uint32_t) * (index + 1))))
39 return -FDT_ERR_NOTFOUND;
41 *out = fdt32_to_cpu(*(val + index));
46 static int ut_fdt_getprop_u32(void *fdt, const char *path, const char *name,
49 return ut_fdt_getprop_u32_by_index(fdt, path, name, 0, out);
52 static int fdt_getprop_str(void *fdt, const char *path, const char *name,
58 node_off = fdt_path_offset(fdt, path);
62 *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
64 return len < 0 ? len : 0;
67 static int fdt_overlay_change_int_property(struct unit_test_state *uts)
69 void *fdt = uts->priv;
72 ut_assertok(ut_fdt_getprop_u32(fdt, "/test-node", "test-int-property",
76 return CMD_RET_SUCCESS;
78 OVERLAY_TEST(fdt_overlay_change_int_property, 0);
80 static int fdt_overlay_change_str_property(struct unit_test_state *uts)
82 void *fdt = uts->priv;
83 const char *val = NULL;
85 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
87 ut_asserteq_str("foobar", val);
89 return CMD_RET_SUCCESS;
91 OVERLAY_TEST(fdt_overlay_change_str_property, 0);
93 static int fdt_overlay_add_str_property(struct unit_test_state *uts)
95 void *fdt = uts->priv;
96 const char *val = NULL;
98 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
100 ut_asserteq_str("foobar2", val);
102 return CMD_RET_SUCCESS;
104 OVERLAY_TEST(fdt_overlay_add_str_property, 0);
106 static int fdt_overlay_add_node_by_phandle(struct unit_test_state *uts)
108 void *fdt = uts->priv;
111 off = fdt_path_offset(fdt, "/test-node/new-node");
114 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
116 return CMD_RET_SUCCESS;
118 OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0);
120 static int fdt_overlay_add_node_by_path(struct unit_test_state *uts)
122 void *fdt = uts->priv;
125 off = fdt_path_offset(fdt, "/new-node");
128 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
130 return CMD_RET_SUCCESS;
132 OVERLAY_TEST(fdt_overlay_add_node_by_path, 0);
134 static int fdt_overlay_add_subnode_property(struct unit_test_state *uts)
136 void *fdt = uts->priv;
139 off = fdt_path_offset(fdt, "/test-node/sub-test-node");
142 ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
143 ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
145 return CMD_RET_SUCCESS;
147 OVERLAY_TEST(fdt_overlay_add_subnode_property, 0);
149 static int fdt_overlay_local_phandle(struct unit_test_state *uts)
151 uint32_t local_phandle;
152 void *fdt = uts->priv;
156 off = fdt_path_offset(fdt, "/new-local-node");
159 local_phandle = fdt_get_phandle(fdt, off);
160 ut_assert(local_phandle);
162 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
164 ut_asserteq(local_phandle, val);
166 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
168 ut_asserteq(local_phandle, val);
170 return CMD_RET_SUCCESS;
172 OVERLAY_TEST(fdt_overlay_local_phandle, 0);
174 static int fdt_overlay_local_phandles(struct unit_test_state *uts)
176 uint32_t local_phandle, test_phandle;
177 void *fdt = uts->priv;
181 off = fdt_path_offset(fdt, "/new-local-node");
184 local_phandle = fdt_get_phandle(fdt, off);
185 ut_assert(local_phandle);
187 off = fdt_path_offset(fdt, "/test-node");
190 test_phandle = fdt_get_phandle(fdt, off);
191 ut_assert(test_phandle);
193 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
195 ut_asserteq(test_phandle, val);
197 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
199 ut_asserteq(local_phandle, val);
201 return CMD_RET_SUCCESS;
203 OVERLAY_TEST(fdt_overlay_local_phandles, 0);
205 static int fdt_overlay_stacked(struct unit_test_state *uts)
207 void *fdt = uts->priv;
210 ut_assertok(ut_fdt_getprop_u32(fdt, "/new-local-node",
211 "stacked-test-int-property", &val));
212 ut_asserteq(43, val);
214 return CMD_RET_SUCCESS;
216 OVERLAY_TEST(fdt_overlay_stacked, 0);
218 int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
220 struct unit_test *tests = ll_entry_start(struct unit_test,
222 const int n_ents = ll_entry_count(struct unit_test, overlay_test);
223 struct unit_test_state *uts;
224 struct unit_test *test;
225 void *fdt_base = &__dtb_test_fdt_base_begin;
226 void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
227 void *fdt_overlay_stacked = &__dtb_test_fdt_overlay_stacked_begin;
228 void *fdt_base_copy, *fdt_overlay_copy, *fdt_overlay_stacked_copy;
231 uts = calloc(1, sizeof(*uts));
235 ut_assertok(fdt_check_header(fdt_base));
236 ut_assertok(fdt_check_header(fdt_overlay));
238 fdt_base_copy = malloc(FDT_COPY_SIZE);
241 uts->priv = fdt_base_copy;
243 fdt_overlay_copy = malloc(FDT_COPY_SIZE);
244 if (!fdt_overlay_copy)
247 fdt_overlay_stacked_copy = malloc(FDT_COPY_SIZE);
248 if (!fdt_overlay_stacked_copy)
252 * Resize the FDT to 4k so that we have room to operate on
254 * (and relocate it since the memory might be mapped
257 ut_assertok(fdt_open_into(fdt_base, fdt_base_copy, FDT_COPY_SIZE));
260 * Resize the overlay to 4k so that we have room to operate on
262 * (and relocate it since the memory might be mapped
265 ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
269 * Resize the stacked overlay to 4k so that we have room to operate on
271 * (and relocate it since the memory might be mapped
274 ut_assertok(fdt_open_into(fdt_overlay_stacked, fdt_overlay_stacked_copy,
277 /* Apply the overlay */
278 ut_assertok(fdt_overlay_apply(fdt_base_copy, fdt_overlay_copy));
280 /* Apply the stacked overlay */
281 ut_assertok(fdt_overlay_apply(fdt_base_copy, fdt_overlay_stacked_copy));
284 printf("Running %d environment tests\n", n_ents);
286 for (test = tests; test < tests + n_ents; test++) {
287 if (argc > 1 && strcmp(argv[1], test->name))
289 printf("Test: %s\n", test->name);
291 uts->start = mallinfo();
296 printf("Failures: %d\n", uts->fail_count);
297 if (!uts->fail_count)
300 ret = CMD_RET_FAILURE;
302 free(fdt_overlay_stacked_copy);
304 free(fdt_overlay_copy);