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,
56 node_off = fdt_path_offset(fdt, path);
60 return fdt_get_string(fdt, node_off, name, out);
63 static int fdt_overlay_change_int_property(struct unit_test_state *uts)
65 void *fdt = uts->priv;
68 ut_assertok(fdt_getprop_u32(fdt, "/test-node", "test-int-property",
72 return CMD_RET_SUCCESS;
74 OVERLAY_TEST(fdt_overlay_change_int_property, 0);
76 static int fdt_overlay_change_str_property(struct unit_test_state *uts)
78 void *fdt = uts->priv;
79 const char *val = NULL;
81 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
83 ut_asserteq_str("foobar", val);
85 return CMD_RET_SUCCESS;
87 OVERLAY_TEST(fdt_overlay_change_str_property, 0);
89 static int fdt_overlay_add_str_property(struct unit_test_state *uts)
91 void *fdt = uts->priv;
92 const char *val = NULL;
94 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
96 ut_asserteq_str("foobar2", val);
98 return CMD_RET_SUCCESS;
100 OVERLAY_TEST(fdt_overlay_add_str_property, 0);
102 static int fdt_overlay_add_node_by_phandle(struct unit_test_state *uts)
104 void *fdt = uts->priv;
107 off = fdt_path_offset(fdt, "/test-node/new-node");
110 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
112 return CMD_RET_SUCCESS;
114 OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0);
116 static int fdt_overlay_add_node_by_path(struct unit_test_state *uts)
118 void *fdt = uts->priv;
121 off = fdt_path_offset(fdt, "/new-node");
124 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
126 return CMD_RET_SUCCESS;
128 OVERLAY_TEST(fdt_overlay_add_node_by_path, 0);
130 static int fdt_overlay_add_subnode_property(struct unit_test_state *uts)
132 void *fdt = uts->priv;
135 off = fdt_path_offset(fdt, "/test-node/sub-test-node");
138 ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
139 ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
141 return CMD_RET_SUCCESS;
143 OVERLAY_TEST(fdt_overlay_add_subnode_property, 0);
145 static int fdt_overlay_local_phandle(struct unit_test_state *uts)
147 uint32_t local_phandle;
148 void *fdt = uts->priv;
152 off = fdt_path_offset(fdt, "/new-local-node");
155 local_phandle = fdt_get_phandle(fdt, off);
156 ut_assert(local_phandle);
158 ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
160 ut_asserteq(local_phandle, val);
162 ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
164 ut_asserteq(local_phandle, val);
166 return CMD_RET_SUCCESS;
168 OVERLAY_TEST(fdt_overlay_local_phandle, 0);
170 static int fdt_overlay_local_phandles(struct unit_test_state *uts)
172 uint32_t local_phandle, test_phandle;
173 void *fdt = uts->priv;
177 off = fdt_path_offset(fdt, "/new-local-node");
180 local_phandle = fdt_get_phandle(fdt, off);
181 ut_assert(local_phandle);
183 off = fdt_path_offset(fdt, "/test-node");
186 test_phandle = fdt_get_phandle(fdt, off);
187 ut_assert(test_phandle);
189 ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
191 ut_asserteq(test_phandle, val);
193 ut_assertok(fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
195 ut_asserteq(local_phandle, val);
197 return CMD_RET_SUCCESS;
199 OVERLAY_TEST(fdt_overlay_local_phandles, 0);
201 int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
203 struct unit_test *tests = ll_entry_start(struct unit_test,
205 const int n_ents = ll_entry_count(struct unit_test, overlay_test);
206 struct unit_test_state *uts;
207 struct unit_test *test;
208 void *fdt_base = &__dtb_test_fdt_base_begin;
209 void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
210 void *fdt_base_copy, *fdt_overlay_copy;
212 uts = calloc(1, sizeof(*uts));
216 ut_assertok(fdt_check_header(fdt_base));
217 ut_assertok(fdt_check_header(fdt_overlay));
219 fdt_base_copy = malloc(FDT_COPY_SIZE);
222 uts->priv = fdt_base_copy;
224 fdt_overlay_copy = malloc(FDT_COPY_SIZE);
225 if (!fdt_overlay_copy)
229 * Resize the FDT to 4k so that we have room to operate on
231 * (and relocate it since the memory might be mapped
234 ut_assertok(fdt_open_into(fdt_base, fdt_base_copy, FDT_COPY_SIZE));
237 * Resize the overlay to 4k so that we have room to operate on
239 * (and relocate it since the memory might be mapped
242 ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
245 /* Apply the overlay */
246 ut_assertok(fdt_overlay_apply(fdt_base_copy, fdt_overlay_copy));
249 printf("Running %d environment tests\n", n_ents);
251 for (test = tests; test < tests + n_ents; test++) {
252 if (argc > 1 && strcmp(argv[1], test->name))
254 printf("Test: %s\n", test->name);
256 uts->start = mallinfo();
261 printf("Failures: %d\n", uts->fail_count);
263 free(fdt_overlay_copy);
267 return uts->fail_count ? CMD_RET_FAILURE : 0;