1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016 NextThing Co
4 * Copyright (c) 2016 Free Electrons
10 #include <fdt_support.h>
15 #include <linux/sizes.h>
18 #include <test/overlay.h>
19 #include <test/suites.h>
21 /* 4k ought to be enough for anybody */
22 #define FDT_COPY_SIZE (4 * SZ_1K)
24 extern u32 __dtb_test_fdt_base_begin;
25 extern u32 __dtb_test_fdt_overlay_begin;
26 extern u32 __dtb_test_fdt_overlay_stacked_begin;
30 static int ut_fdt_getprop_u32_by_index(void *fdt, const char *path,
31 const char *name, int index,
38 node_off = fdt_path_offset(fdt, path);
42 val = fdt_getprop(fdt, node_off, name, &len);
43 if (!val || (len < (sizeof(uint32_t) * (index + 1))))
44 return -FDT_ERR_NOTFOUND;
46 *out = fdt32_to_cpu(*(val + index));
51 static int ut_fdt_getprop_u32(void *fdt, const char *path, const char *name,
54 return ut_fdt_getprop_u32_by_index(fdt, path, name, 0, out);
57 static int fdt_getprop_str(void *fdt, const char *path, const char *name,
63 node_off = fdt_path_offset(fdt, path);
67 *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
69 return len < 0 ? len : 0;
72 static int fdt_overlay_change_int_property(struct unit_test_state *uts)
76 ut_assertok(ut_fdt_getprop_u32(fdt, "/test-node", "test-int-property",
80 return CMD_RET_SUCCESS;
82 OVERLAY_TEST(fdt_overlay_change_int_property, 0);
84 static int fdt_overlay_change_str_property(struct unit_test_state *uts)
86 const char *val = NULL;
88 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
90 ut_asserteq_str("foobar", val);
92 return CMD_RET_SUCCESS;
94 OVERLAY_TEST(fdt_overlay_change_str_property, 0);
96 static int fdt_overlay_add_str_property(struct unit_test_state *uts)
98 const char *val = NULL;
100 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
102 ut_asserteq_str("foobar2", val);
104 return CMD_RET_SUCCESS;
106 OVERLAY_TEST(fdt_overlay_add_str_property, 0);
108 static int fdt_overlay_add_node_by_phandle(struct unit_test_state *uts)
112 off = fdt_path_offset(fdt, "/test-node/new-node");
115 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
117 return CMD_RET_SUCCESS;
119 OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0);
121 static int fdt_overlay_add_node_by_path(struct unit_test_state *uts)
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)
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;
154 off = fdt_path_offset(fdt, "/new-local-node");
157 local_phandle = fdt_get_phandle(fdt, off);
158 ut_assert(local_phandle);
160 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
162 ut_asserteq(local_phandle, val);
164 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
166 ut_asserteq(local_phandle, val);
168 return CMD_RET_SUCCESS;
170 OVERLAY_TEST(fdt_overlay_local_phandle, 0);
172 static int fdt_overlay_local_phandles(struct unit_test_state *uts)
174 uint32_t local_phandle, test_phandle;
178 off = fdt_path_offset(fdt, "/new-local-node");
181 local_phandle = fdt_get_phandle(fdt, off);
182 ut_assert(local_phandle);
184 off = fdt_path_offset(fdt, "/test-node");
187 test_phandle = fdt_get_phandle(fdt, off);
188 ut_assert(test_phandle);
190 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
192 ut_asserteq(test_phandle, val);
194 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
196 ut_asserteq(local_phandle, val);
198 return CMD_RET_SUCCESS;
200 OVERLAY_TEST(fdt_overlay_local_phandles, 0);
202 static int fdt_overlay_stacked(struct unit_test_state *uts)
206 ut_assertok(ut_fdt_getprop_u32(fdt, "/new-local-node",
207 "stacked-test-int-property", &val));
208 ut_asserteq(43, val);
210 return CMD_RET_SUCCESS;
212 OVERLAY_TEST(fdt_overlay_stacked, 0);
214 int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
216 struct unit_test *tests = UNIT_TEST_SUITE_START(overlay_test);
217 const int n_ents = UNIT_TEST_SUITE_COUNT(overlay_test);
218 struct unit_test_state *uts;
219 void *fdt_base = &__dtb_test_fdt_base_begin;
220 void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
221 void *fdt_overlay_stacked = &__dtb_test_fdt_overlay_stacked_begin;
222 void *fdt_overlay_copy, *fdt_overlay_stacked_copy;
225 uts = calloc(1, sizeof(*uts));
229 ut_assertok(fdt_check_header(fdt_base));
230 ut_assertok(fdt_check_header(fdt_overlay));
232 fdt = malloc(FDT_COPY_SIZE);
236 fdt_overlay_copy = malloc(FDT_COPY_SIZE);
237 if (!fdt_overlay_copy)
240 fdt_overlay_stacked_copy = malloc(FDT_COPY_SIZE);
241 if (!fdt_overlay_stacked_copy)
245 * Resize the FDT to 4k so that we have room to operate on
247 * (and relocate it since the memory might be mapped
250 ut_assertok(fdt_open_into(fdt_base, fdt, FDT_COPY_SIZE));
253 * Resize the overlay to 4k so that we have room to operate on
255 * (and relocate it since the memory might be mapped
258 ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
262 * Resize the stacked overlay to 4k so that we have room to operate on
264 * (and relocate it since the memory might be mapped
267 ut_assertok(fdt_open_into(fdt_overlay_stacked, fdt_overlay_stacked_copy,
270 /* Apply the overlay */
271 ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_copy));
273 /* Apply the stacked overlay */
274 ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_stacked_copy));
276 ret = cmd_ut_category("overlay", "", tests, n_ents, argc, argv);
278 free(fdt_overlay_stacked_copy);
280 free(fdt_overlay_copy);