1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2016 NextThing Co
4 * Copyright (c) 2016 Free Electrons
5 */
6
7 #include <command.h>
8 #include <errno.h>
9 #include <fdtdec.h>
10 #include <fdt_support.h>
11 #include <image.h>
12 #include <log.h>
13 #include <malloc.h>
14
15 #include <linux/sizes.h>
16
17 #include <test/fdt_overlay.h>
18 #include <test/ut.h>
19
20 /* 4k ought to be enough for anybody */
21 #define FDT_COPY_SIZE (4 * SZ_1K)
22
23 extern u32 __dtb_test_fdt_base_begin;
24 extern u32 __dtbo_test_fdt_overlay_begin;
25 extern u32 __dtbo_test_fdt_overlay_stacked_begin;
26
27 static void *fdt;
28
fdt_overlay_init(struct unit_test_state * uts)29 static int fdt_overlay_init(struct unit_test_state *uts)
30 {
31 void *fdt_base = &__dtb_test_fdt_base_begin;
32 void *fdt_overlay = &__dtbo_test_fdt_overlay_begin;
33 void *fdt_overlay_stacked = &__dtbo_test_fdt_overlay_stacked_begin;
34 void *fdt_overlay_copy, *fdt_overlay_stacked_copy;
35
36 ut_assertok(fdt_check_header(fdt_base));
37 ut_assertok(fdt_check_header(fdt_overlay));
38
39 fdt = malloc(FDT_COPY_SIZE);
40 fdt_overlay_copy = malloc(FDT_COPY_SIZE);
41 fdt_overlay_stacked_copy = malloc(FDT_COPY_SIZE);
42 ut_assertnonnull(fdt);
43 ut_assertnonnull(fdt_overlay_copy);
44 ut_assertnonnull(fdt_overlay_stacked_copy);
45
46 /*
47 * Resize the FDT to 4k so that we have room to operate on
48 *
49 * (and relocate it since the memory might be mapped
50 * read-only)
51 */
52 ut_assertok(fdt_open_into(fdt_base, fdt, FDT_COPY_SIZE));
53
54 /*
55 * Resize the overlay to 4k so that we have room to operate on
56 *
57 * (and relocate it since the memory might be mapped
58 * read-only)
59 */
60 ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
61 FDT_COPY_SIZE));
62
63 /*
64 * Resize the stacked overlay to 4k so that we have room to operate on
65 *
66 * (and relocate it since the memory might be mapped
67 * read-only)
68 */
69 ut_assertok(fdt_open_into(fdt_overlay_stacked, fdt_overlay_stacked_copy,
70 FDT_COPY_SIZE));
71
72 /* Apply the overlay */
73 ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_copy));
74
75 /* Apply the stacked overlay */
76 ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_stacked_copy));
77
78 free(fdt_overlay_stacked_copy);
79 free(fdt_overlay_copy);
80
81 return 0;
82 }
83 FDT_OVERLAY_TEST_INIT(fdt_overlay_init, 0);
84
fdt_getprop_str(void * fdt,const char * path,const char * name,const char ** out)85 static int fdt_getprop_str(void *fdt, const char *path, const char *name,
86 const char **out)
87 {
88 int node_off;
89 int len;
90
91 node_off = fdt_path_offset(fdt, path);
92 if (node_off < 0)
93 return node_off;
94
95 *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
96
97 return len < 0 ? len : 0;
98 }
99
fdt_overlay_test_change_int_property(struct unit_test_state * uts)100 static int fdt_overlay_test_change_int_property(struct unit_test_state *uts)
101 {
102 int off;
103
104 off = fdt_path_offset(fdt, "/test-node");
105 ut_assert(off >= 0);
106
107 ut_asserteq(43, fdtdec_get_uint(fdt, off, "test-int-property", 0));
108
109 return CMD_RET_SUCCESS;
110 }
111 FDT_OVERLAY_TEST(fdt_overlay_test_change_int_property, 0);
112
fdt_overlay_test_change_str_property(struct unit_test_state * uts)113 static int fdt_overlay_test_change_str_property(struct unit_test_state *uts)
114 {
115 const char *val = NULL;
116
117 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
118 &val));
119 ut_asserteq_str("foobar", val);
120
121 return CMD_RET_SUCCESS;
122 }
123 FDT_OVERLAY_TEST(fdt_overlay_test_change_str_property, 0);
124
fdt_overlay_test_add_str_property(struct unit_test_state * uts)125 static int fdt_overlay_test_add_str_property(struct unit_test_state *uts)
126 {
127 const char *val = NULL;
128
129 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
130 &val));
131 ut_asserteq_str("foobar2", val);
132
133 return CMD_RET_SUCCESS;
134 }
135 FDT_OVERLAY_TEST(fdt_overlay_test_add_str_property, 0);
136
fdt_overlay_test_add_node_by_phandle(struct unit_test_state * uts)137 static int fdt_overlay_test_add_node_by_phandle(struct unit_test_state *uts)
138 {
139 int off;
140
141 off = fdt_path_offset(fdt, "/test-node/new-node");
142 ut_assert(off >= 0);
143
144 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
145
146 return CMD_RET_SUCCESS;
147 }
148 FDT_OVERLAY_TEST(fdt_overlay_test_add_node_by_phandle, 0);
149
fdt_overlay_test_add_node_by_path(struct unit_test_state * uts)150 static int fdt_overlay_test_add_node_by_path(struct unit_test_state *uts)
151 {
152 int off;
153
154 off = fdt_path_offset(fdt, "/new-node");
155 ut_assert(off >= 0);
156
157 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
158
159 return CMD_RET_SUCCESS;
160 }
161 FDT_OVERLAY_TEST(fdt_overlay_test_add_node_by_path, 0);
162
fdt_overlay_test_add_subnode_property(struct unit_test_state * uts)163 static int fdt_overlay_test_add_subnode_property(struct unit_test_state *uts)
164 {
165 int off;
166
167 off = fdt_path_offset(fdt, "/test-node/sub-test-node");
168 ut_assert(off >= 0);
169
170 ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
171 ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
172
173 return CMD_RET_SUCCESS;
174 }
175 FDT_OVERLAY_TEST(fdt_overlay_test_add_subnode_property, 0);
176
fdt_overlay_test_local_phandle(struct unit_test_state * uts)177 static int fdt_overlay_test_local_phandle(struct unit_test_state *uts)
178 {
179 uint32_t local_phandle;
180 u32 val[2];
181 int off;
182
183 off = fdt_path_offset(fdt, "/new-local-node");
184 ut_assert(off >= 0);
185
186 local_phandle = fdt_get_phandle(fdt, off);
187 ut_assert(local_phandle);
188
189 ut_assertok(fdtdec_get_int_array(fdt, 0, "test-several-phandle", val,
190 ARRAY_SIZE(val)));
191 ut_asserteq(local_phandle, val[0]);
192 ut_asserteq(local_phandle, val[1]);
193
194 return CMD_RET_SUCCESS;
195 }
196 FDT_OVERLAY_TEST(fdt_overlay_test_local_phandle, 0);
197
fdt_overlay_test_local_phandles(struct unit_test_state * uts)198 static int fdt_overlay_test_local_phandles(struct unit_test_state *uts)
199 {
200 uint32_t local_phandle, test_phandle;
201 u32 val[2];
202 int off;
203
204 off = fdt_path_offset(fdt, "/new-local-node");
205 ut_assert(off >= 0);
206
207 local_phandle = fdt_get_phandle(fdt, off);
208 ut_assert(local_phandle);
209
210 off = fdt_path_offset(fdt, "/test-node");
211 ut_assert(off >= 0);
212
213 test_phandle = fdt_get_phandle(fdt, off);
214 ut_assert(test_phandle);
215
216 ut_assertok(fdtdec_get_int_array(fdt, 0, "test-phandle", val,
217 ARRAY_SIZE(val)));
218 ut_asserteq(test_phandle, val[0]);
219 ut_asserteq(local_phandle, val[1]);
220
221 return CMD_RET_SUCCESS;
222 }
223 FDT_OVERLAY_TEST(fdt_overlay_test_local_phandles, 0);
224
fdt_overlay_test_stacked(struct unit_test_state * uts)225 static int fdt_overlay_test_stacked(struct unit_test_state *uts)
226 {
227 int off;
228
229 off = fdt_path_offset(fdt, "/new-local-node");
230 ut_assert(off > 0);
231
232 ut_asserteq(43,
233 fdtdec_get_uint(fdt, off, "stacked-test-int-property", 0));
234
235 return CMD_RET_SUCCESS;
236 }
237 FDT_OVERLAY_TEST(fdt_overlay_test_stacked, 0);
238