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;
24 static int fdt_getprop_u32_by_index(void *fdt, const char *path,
25 const char *name, int index,
32 node_off = fdt_path_offset(fdt, path);
36 val = fdt_getprop(fdt, node_off, name, &len);
37 if (!val || (len < (sizeof(uint32_t) * (index + 1))))
38 return -FDT_ERR_NOTFOUND;
40 *out = fdt32_to_cpu(*(val + index));
45 static int fdt_getprop_u32(void *fdt, const char *path, const char *name,
48 return fdt_getprop_u32_by_index(fdt, path, name, 0, out);
51 static int fdt_getprop_str(void *fdt, const char *path, const char *name,
57 node_off = fdt_path_offset(fdt, path);
61 *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
63 return len < 0 ? len : 0;
66 static int fdt_overlay_change_int_property(struct unit_test_state *uts)
68 void *fdt = uts->priv;
71 ut_assertok(fdt_getprop_u32(fdt, "/test-node", "test-int-property",
75 return CMD_RET_SUCCESS;
77 OVERLAY_TEST(fdt_overlay_change_int_property, 0);
79 static int fdt_overlay_change_str_property(struct unit_test_state *uts)
81 void *fdt = uts->priv;
82 const char *val = NULL;
84 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
86 ut_asserteq_str("foobar", val);
88 return CMD_RET_SUCCESS;
90 OVERLAY_TEST(fdt_overlay_change_str_property, 0);
92 static int fdt_overlay_add_str_property(struct unit_test_state *uts)
94 void *fdt = uts->priv;
95 const char *val = NULL;
97 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
99 ut_asserteq_str("foobar2", val);
101 return CMD_RET_SUCCESS;
103 OVERLAY_TEST(fdt_overlay_add_str_property, 0);
105 static int fdt_overlay_add_node_by_phandle(struct unit_test_state *uts)
107 void *fdt = uts->priv;
110 off = fdt_path_offset(fdt, "/test-node/new-node");
113 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
115 return CMD_RET_SUCCESS;
117 OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0);
119 static int fdt_overlay_add_node_by_path(struct unit_test_state *uts)
121 void *fdt = uts->priv;
124 off = fdt_path_offset(fdt, "/new-node");
127 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
129 return CMD_RET_SUCCESS;
131 OVERLAY_TEST(fdt_overlay_add_node_by_path, 0);
133 static int fdt_overlay_add_subnode_property(struct unit_test_state *uts)
135 void *fdt = uts->priv;
138 off = fdt_path_offset(fdt, "/test-node/sub-test-node");
141 ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
142 ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
144 return CMD_RET_SUCCESS;
146 OVERLAY_TEST(fdt_overlay_add_subnode_property, 0);
148 static int fdt_overlay_local_phandle(struct unit_test_state *uts)
150 uint32_t local_phandle;
151 void *fdt = uts->priv;
155 off = fdt_path_offset(fdt, "/new-local-node");
158 local_phandle = fdt_get_phandle(fdt, off);
159 ut_assert(local_phandle);
161 ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
163 ut_asserteq(local_phandle, val);
165 ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
167 ut_asserteq(local_phandle, val);
169 return CMD_RET_SUCCESS;
171 OVERLAY_TEST(fdt_overlay_local_phandle, 0);
173 static int fdt_overlay_local_phandles(struct unit_test_state *uts)
175 uint32_t local_phandle, test_phandle;
176 void *fdt = uts->priv;
180 off = fdt_path_offset(fdt, "/new-local-node");
183 local_phandle = fdt_get_phandle(fdt, off);
184 ut_assert(local_phandle);
186 off = fdt_path_offset(fdt, "/test-node");
189 test_phandle = fdt_get_phandle(fdt, off);
190 ut_assert(test_phandle);
192 ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
194 ut_asserteq(test_phandle, val);
196 ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
198 ut_asserteq(local_phandle, val);
200 return CMD_RET_SUCCESS;
202 OVERLAY_TEST(fdt_overlay_local_phandles, 0);
204 int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
206 struct unit_test *tests = ll_entry_start(struct unit_test,
208 const int n_ents = ll_entry_count(struct unit_test, overlay_test);
209 struct unit_test_state *uts;
210 struct unit_test *test;
211 void *fdt_base = &__dtb_test_fdt_base_begin;
212 void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
213 void *fdt_base_copy, *fdt_overlay_copy;
215 uts = calloc(1, sizeof(*uts));
219 ut_assertok(fdt_check_header(fdt_base));
220 ut_assertok(fdt_check_header(fdt_overlay));
222 fdt_base_copy = malloc(FDT_COPY_SIZE);
225 uts->priv = fdt_base_copy;
227 fdt_overlay_copy = malloc(FDT_COPY_SIZE);
228 if (!fdt_overlay_copy)
232 * Resize the FDT to 4k so that we have room to operate on
234 * (and relocate it since the memory might be mapped
237 ut_assertok(fdt_open_into(fdt_base, fdt_base_copy, FDT_COPY_SIZE));
240 * Resize the overlay to 4k so that we have room to operate on
242 * (and relocate it since the memory might be mapped
245 ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
248 /* Apply the overlay */
249 ut_assertok(fdt_overlay_apply(fdt_base_copy, fdt_overlay_copy));
252 printf("Running %d environment tests\n", n_ents);
254 for (test = tests; test < tests + n_ents; test++) {
255 if (argc > 1 && strcmp(argv[1], test->name))
257 printf("Test: %s\n", test->name);
259 uts->start = mallinfo();
264 printf("Failures: %d\n", uts->fail_count);
266 free(fdt_overlay_copy);
270 return uts->fail_count ? CMD_RET_FAILURE : 0;