]> git.ipfire.org Git - people/ms/u-boot.git/blame - test/overlay/cmd_ut_overlay.c
Merge git://git.denx.de/u-boot-sunxi
[people/ms/u-boot.git] / test / overlay / cmd_ut_overlay.c
CommitLineData
f2a9942f
MR
1/*
2 * Copyright (c) 2016 NextThing Co
3 * Copyright (c) 2016 Free Electrons
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <command.h>
10#include <errno.h>
11#include <malloc.h>
12
13#include <linux/sizes.h>
14
15#include <test/ut.h>
16#include <test/overlay.h>
e93232e1 17#include <test/suites.h>
f2a9942f
MR
18
19/* 4k ought to be enough for anybody */
20#define FDT_COPY_SIZE (4 * SZ_1K)
21
22extern u32 __dtb_test_fdt_base_begin;
23extern u32 __dtb_test_fdt_overlay_begin;
ea28e488 24extern u32 __dtb_test_fdt_overlay_stacked_begin;
f2a9942f 25
706708d3 26static int ut_fdt_getprop_u32_by_index(void *fdt, const char *path,
f2a9942f
MR
27 const char *name, int index,
28 u32 *out)
29{
30 const fdt32_t *val;
31 int node_off;
32 int len;
33
34 node_off = fdt_path_offset(fdt, path);
35 if (node_off < 0)
36 return node_off;
37
38 val = fdt_getprop(fdt, node_off, name, &len);
39 if (!val || (len < (sizeof(uint32_t) * (index + 1))))
40 return -FDT_ERR_NOTFOUND;
41
42 *out = fdt32_to_cpu(*(val + index));
43
44 return 0;
45}
46
706708d3 47static int ut_fdt_getprop_u32(void *fdt, const char *path, const char *name,
f2a9942f
MR
48 u32 *out)
49{
706708d3 50 return ut_fdt_getprop_u32_by_index(fdt, path, name, 0, out);
f2a9942f
MR
51}
52
53static int fdt_getprop_str(void *fdt, const char *path, const char *name,
54 const char **out)
55{
56 int node_off;
b02e4044 57 int len;
f2a9942f
MR
58
59 node_off = fdt_path_offset(fdt, path);
60 if (node_off < 0)
61 return node_off;
62
b02e4044
SG
63 *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
64
65 return len < 0 ? len : 0;
f2a9942f
MR
66}
67
68static int fdt_overlay_change_int_property(struct unit_test_state *uts)
69{
70 void *fdt = uts->priv;
71 u32 val = 0;
72
706708d3 73 ut_assertok(ut_fdt_getprop_u32(fdt, "/test-node", "test-int-property",
f2a9942f
MR
74 &val));
75 ut_asserteq(43, val);
76
77 return CMD_RET_SUCCESS;
78}
79OVERLAY_TEST(fdt_overlay_change_int_property, 0);
80
81static int fdt_overlay_change_str_property(struct unit_test_state *uts)
82{
83 void *fdt = uts->priv;
84 const char *val = NULL;
85
86 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
87 &val));
88 ut_asserteq_str("foobar", val);
89
90 return CMD_RET_SUCCESS;
91}
92OVERLAY_TEST(fdt_overlay_change_str_property, 0);
93
94static int fdt_overlay_add_str_property(struct unit_test_state *uts)
95{
96 void *fdt = uts->priv;
97 const char *val = NULL;
98
99 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
100 &val));
101 ut_asserteq_str("foobar2", val);
102
103 return CMD_RET_SUCCESS;
104}
105OVERLAY_TEST(fdt_overlay_add_str_property, 0);
106
107static int fdt_overlay_add_node_by_phandle(struct unit_test_state *uts)
108{
109 void *fdt = uts->priv;
110 int off;
111
112 off = fdt_path_offset(fdt, "/test-node/new-node");
113 ut_assert(off >= 0);
114
115 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
116
117 return CMD_RET_SUCCESS;
118}
119OVERLAY_TEST(fdt_overlay_add_node_by_phandle, 0);
120
121static int fdt_overlay_add_node_by_path(struct unit_test_state *uts)
122{
123 void *fdt = uts->priv;
124 int off;
125
126 off = fdt_path_offset(fdt, "/new-node");
127 ut_assert(off >= 0);
128
129 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
130
131 return CMD_RET_SUCCESS;
132}
133OVERLAY_TEST(fdt_overlay_add_node_by_path, 0);
134
135static int fdt_overlay_add_subnode_property(struct unit_test_state *uts)
136{
137 void *fdt = uts->priv;
138 int off;
139
140 off = fdt_path_offset(fdt, "/test-node/sub-test-node");
141 ut_assert(off >= 0);
142
143 ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
144 ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
145
146 return CMD_RET_SUCCESS;
147}
148OVERLAY_TEST(fdt_overlay_add_subnode_property, 0);
149
150static int fdt_overlay_local_phandle(struct unit_test_state *uts)
151{
152 uint32_t local_phandle;
153 void *fdt = uts->priv;
154 u32 val = 0;
155 int off;
156
157 off = fdt_path_offset(fdt, "/new-local-node");
158 ut_assert(off >= 0);
159
160 local_phandle = fdt_get_phandle(fdt, off);
161 ut_assert(local_phandle);
162
706708d3 163 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
f2a9942f
MR
164 0, &val));
165 ut_asserteq(local_phandle, val);
166
706708d3 167 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-several-phandle",
f2a9942f
MR
168 1, &val));
169 ut_asserteq(local_phandle, val);
170
171 return CMD_RET_SUCCESS;
172}
173OVERLAY_TEST(fdt_overlay_local_phandle, 0);
174
175static int fdt_overlay_local_phandles(struct unit_test_state *uts)
176{
177 uint32_t local_phandle, test_phandle;
178 void *fdt = uts->priv;
179 u32 val = 0;
180 int off;
181
182 off = fdt_path_offset(fdt, "/new-local-node");
183 ut_assert(off >= 0);
184
185 local_phandle = fdt_get_phandle(fdt, off);
186 ut_assert(local_phandle);
187
188 off = fdt_path_offset(fdt, "/test-node");
189 ut_assert(off >= 0);
190
191 test_phandle = fdt_get_phandle(fdt, off);
192 ut_assert(test_phandle);
193
706708d3 194 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 0,
f2a9942f
MR
195 &val));
196 ut_asserteq(test_phandle, val);
197
706708d3 198 ut_assertok(ut_fdt_getprop_u32_by_index(fdt, "/", "test-phandle", 1,
f2a9942f
MR
199 &val));
200 ut_asserteq(local_phandle, val);
201
202 return CMD_RET_SUCCESS;
203}
204OVERLAY_TEST(fdt_overlay_local_phandles, 0);
205
ea28e488
PA
206static int fdt_overlay_stacked(struct unit_test_state *uts)
207{
208 void *fdt = uts->priv;
209 u32 val = 0;
210
211 ut_assertok(ut_fdt_getprop_u32(fdt, "/new-local-node",
212 "stacked-test-int-property", &val));
213 ut_asserteq(43, val);
214
215 return CMD_RET_SUCCESS;
216}
217OVERLAY_TEST(fdt_overlay_stacked, 0);
218
f2a9942f
MR
219int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
220{
221 struct unit_test *tests = ll_entry_start(struct unit_test,
222 overlay_test);
223 const int n_ents = ll_entry_count(struct unit_test, overlay_test);
224 struct unit_test_state *uts;
f2a9942f
MR
225 void *fdt_base = &__dtb_test_fdt_base_begin;
226 void *fdt_overlay = &__dtb_test_fdt_overlay_begin;
ea28e488
PA
227 void *fdt_overlay_stacked = &__dtb_test_fdt_overlay_stacked_begin;
228 void *fdt_base_copy, *fdt_overlay_copy, *fdt_overlay_stacked_copy;
d91062c4 229 int ret = -ENOMEM;
f2a9942f
MR
230
231 uts = calloc(1, sizeof(*uts));
232 if (!uts)
233 return -ENOMEM;
234
235 ut_assertok(fdt_check_header(fdt_base));
236 ut_assertok(fdt_check_header(fdt_overlay));
237
238 fdt_base_copy = malloc(FDT_COPY_SIZE);
239 if (!fdt_base_copy)
d91062c4 240 goto err1;
f2a9942f
MR
241 uts->priv = fdt_base_copy;
242
243 fdt_overlay_copy = malloc(FDT_COPY_SIZE);
244 if (!fdt_overlay_copy)
d91062c4 245 goto err2;
f2a9942f 246
ea28e488
PA
247 fdt_overlay_stacked_copy = malloc(FDT_COPY_SIZE);
248 if (!fdt_overlay_stacked_copy)
d91062c4 249 goto err3;
ea28e488 250
f2a9942f
MR
251 /*
252 * Resize the FDT to 4k so that we have room to operate on
253 *
254 * (and relocate it since the memory might be mapped
255 * read-only)
256 */
257 ut_assertok(fdt_open_into(fdt_base, fdt_base_copy, FDT_COPY_SIZE));
258
259 /*
260 * Resize the overlay to 4k so that we have room to operate on
261 *
262 * (and relocate it since the memory might be mapped
263 * read-only)
264 */
265 ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
266 FDT_COPY_SIZE));
267
ea28e488
PA
268 /*
269 * Resize the stacked overlay to 4k so that we have room to operate on
270 *
271 * (and relocate it since the memory might be mapped
272 * read-only)
273 */
274 ut_assertok(fdt_open_into(fdt_overlay_stacked, fdt_overlay_stacked_copy,
275 FDT_COPY_SIZE));
276
f2a9942f
MR
277 /* Apply the overlay */
278 ut_assertok(fdt_overlay_apply(fdt_base_copy, fdt_overlay_copy));
279
ea28e488
PA
280 /* Apply the stacked overlay */
281 ut_assertok(fdt_overlay_apply(fdt_base_copy, fdt_overlay_stacked_copy));
282
e93232e1 283 ret = cmd_ut_category("overlay", tests, n_ents, argc, argv);
f2a9942f 284
ea28e488 285 free(fdt_overlay_stacked_copy);
d91062c4 286err3:
f2a9942f 287 free(fdt_overlay_copy);
d91062c4 288err2:
f2a9942f 289 free(fdt_base_copy);
d91062c4 290err1:
f2a9942f
MR
291 free(uts);
292
d91062c4 293 return ret;
f2a9942f 294}