blob: c716c6bd6b0cc0d807bd31b7a2e9b6eca36eb68f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Maxime Ripard0e31a112016-07-05 10:26:46 +02002/*
3 * Copyright (c) 2016 NextThing Co
4 * Copyright (c) 2016 Free Electrons
Maxime Ripard0e31a112016-07-05 10:26:46 +02005 */
6
Maxime Ripard0e31a112016-07-05 10:26:46 +02007#include <command.h>
8#include <errno.h>
Simon Glass2bac0992025-02-07 11:30:45 -07009#include <fdtdec.h>
Heinrich Schuchardtb210fc72018-10-11 02:16:46 +020010#include <fdt_support.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060011#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Maxime Ripard0e31a112016-07-05 10:26:46 +020013#include <malloc.h>
14
15#include <linux/sizes.h>
16
Simon Glass04e76d52025-02-07 11:30:39 -070017#include <test/fdt_overlay.h>
Maxime Ripard0e31a112016-07-05 10:26:46 +020018#include <test/ut.h>
Simon Glass17e99032017-11-25 11:57:30 -070019#include <test/suites.h>
Maxime Ripard0e31a112016-07-05 10:26:46 +020020
21/* 4k ought to be enough for anybody */
22#define FDT_COPY_SIZE (4 * SZ_1K)
23
24extern u32 __dtb_test_fdt_base_begin;
Rasmus Villemoesb0b7e682024-07-10 09:16:10 +020025extern u32 __dtbo_test_fdt_overlay_begin;
26extern u32 __dtbo_test_fdt_overlay_stacked_begin;
Maxime Ripard0e31a112016-07-05 10:26:46 +020027
Heinrich Schuchardta4189002018-12-08 09:53:05 +010028static void *fdt;
29
Simon Glassffbfeac2025-02-07 11:30:44 -070030static int fdt_overlay_init(struct unit_test_state *uts)
31{
32 void *fdt_base = &__dtb_test_fdt_base_begin;
33 void *fdt_overlay = &__dtbo_test_fdt_overlay_begin;
34 void *fdt_overlay_stacked = &__dtbo_test_fdt_overlay_stacked_begin;
35 void *fdt_overlay_copy, *fdt_overlay_stacked_copy;
36
37 ut_assertok(fdt_check_header(fdt_base));
38 ut_assertok(fdt_check_header(fdt_overlay));
39
40 fdt = malloc(FDT_COPY_SIZE);
41 fdt_overlay_copy = malloc(FDT_COPY_SIZE);
42 fdt_overlay_stacked_copy = malloc(FDT_COPY_SIZE);
43 ut_assertnonnull(fdt);
44 ut_assertnonnull(fdt_overlay_copy);
45 ut_assertnonnull(fdt_overlay_stacked_copy);
46
47 /*
48 * Resize the FDT to 4k so that we have room to operate on
49 *
50 * (and relocate it since the memory might be mapped
51 * read-only)
52 */
53 ut_assertok(fdt_open_into(fdt_base, fdt, FDT_COPY_SIZE));
54
55 /*
56 * Resize the overlay to 4k so that we have room to operate on
57 *
58 * (and relocate it since the memory might be mapped
59 * read-only)
60 */
61 ut_assertok(fdt_open_into(fdt_overlay, fdt_overlay_copy,
62 FDT_COPY_SIZE));
63
64 /*
65 * Resize the stacked overlay to 4k so that we have room to operate on
66 *
67 * (and relocate it since the memory might be mapped
68 * read-only)
69 */
70 ut_assertok(fdt_open_into(fdt_overlay_stacked, fdt_overlay_stacked_copy,
71 FDT_COPY_SIZE));
72
73 /* Apply the overlay */
74 ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_copy));
75
76 /* Apply the stacked overlay */
77 ut_assertok(fdt_overlay_apply(fdt, fdt_overlay_stacked_copy));
78
79 free(fdt_overlay_stacked_copy);
80 free(fdt_overlay_copy);
81
82 return 0;
83}
Simon Glass14a96a52025-02-07 11:30:46 -070084FDT_OVERLAY_TEST_INIT(fdt_overlay_init, 0);
Simon Glassffbfeac2025-02-07 11:30:44 -070085
Maxime Ripard0e31a112016-07-05 10:26:46 +020086static int fdt_getprop_str(void *fdt, const char *path, const char *name,
87 const char **out)
88{
89 int node_off;
Simon Glassb0ea7402016-10-02 17:59:28 -060090 int len;
Maxime Ripard0e31a112016-07-05 10:26:46 +020091
92 node_off = fdt_path_offset(fdt, path);
93 if (node_off < 0)
94 return node_off;
95
Simon Glassb0ea7402016-10-02 17:59:28 -060096 *out = fdt_stringlist_get(fdt, node_off, name, 0, &len);
97
98 return len < 0 ? len : 0;
Maxime Ripard0e31a112016-07-05 10:26:46 +020099}
100
Simon Glass04e76d52025-02-07 11:30:39 -0700101static int fdt_overlay_test_change_int_property(struct unit_test_state *uts)
Maxime Ripard0e31a112016-07-05 10:26:46 +0200102{
Simon Glass2bac0992025-02-07 11:30:45 -0700103 int off;
Maxime Ripard0e31a112016-07-05 10:26:46 +0200104
Simon Glass2bac0992025-02-07 11:30:45 -0700105 off = fdt_path_offset(fdt, "/test-node");
106 ut_assert(off >= 0);
107
108 ut_asserteq(43, fdtdec_get_uint(fdt, off, "test-int-property", 0));
Maxime Ripard0e31a112016-07-05 10:26:46 +0200109
110 return CMD_RET_SUCCESS;
111}
Simon Glass04e76d52025-02-07 11:30:39 -0700112FDT_OVERLAY_TEST(fdt_overlay_test_change_int_property, 0);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200113
Simon Glass04e76d52025-02-07 11:30:39 -0700114static int fdt_overlay_test_change_str_property(struct unit_test_state *uts)
Maxime Ripard0e31a112016-07-05 10:26:46 +0200115{
Maxime Ripard0e31a112016-07-05 10:26:46 +0200116 const char *val = NULL;
117
118 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property",
119 &val));
120 ut_asserteq_str("foobar", val);
121
122 return CMD_RET_SUCCESS;
123}
Simon Glass04e76d52025-02-07 11:30:39 -0700124FDT_OVERLAY_TEST(fdt_overlay_test_change_str_property, 0);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200125
Simon Glass04e76d52025-02-07 11:30:39 -0700126static int fdt_overlay_test_add_str_property(struct unit_test_state *uts)
Maxime Ripard0e31a112016-07-05 10:26:46 +0200127{
Maxime Ripard0e31a112016-07-05 10:26:46 +0200128 const char *val = NULL;
129
130 ut_assertok(fdt_getprop_str(fdt, "/test-node", "test-str-property-2",
131 &val));
132 ut_asserteq_str("foobar2", val);
133
134 return CMD_RET_SUCCESS;
135}
Simon Glass04e76d52025-02-07 11:30:39 -0700136FDT_OVERLAY_TEST(fdt_overlay_test_add_str_property, 0);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200137
Simon Glass04e76d52025-02-07 11:30:39 -0700138static int fdt_overlay_test_add_node_by_phandle(struct unit_test_state *uts)
Maxime Ripard0e31a112016-07-05 10:26:46 +0200139{
Maxime Ripard0e31a112016-07-05 10:26:46 +0200140 int off;
141
142 off = fdt_path_offset(fdt, "/test-node/new-node");
143 ut_assert(off >= 0);
144
145 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
146
147 return CMD_RET_SUCCESS;
148}
Simon Glass04e76d52025-02-07 11:30:39 -0700149FDT_OVERLAY_TEST(fdt_overlay_test_add_node_by_phandle, 0);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200150
Simon Glass04e76d52025-02-07 11:30:39 -0700151static int fdt_overlay_test_add_node_by_path(struct unit_test_state *uts)
Maxime Ripard0e31a112016-07-05 10:26:46 +0200152{
Maxime Ripard0e31a112016-07-05 10:26:46 +0200153 int off;
154
155 off = fdt_path_offset(fdt, "/new-node");
156 ut_assert(off >= 0);
157
158 ut_assertnonnull(fdt_getprop(fdt, off, "new-property", NULL));
159
160 return CMD_RET_SUCCESS;
161}
Simon Glass04e76d52025-02-07 11:30:39 -0700162FDT_OVERLAY_TEST(fdt_overlay_test_add_node_by_path, 0);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200163
Simon Glass04e76d52025-02-07 11:30:39 -0700164static int fdt_overlay_test_add_subnode_property(struct unit_test_state *uts)
Maxime Ripard0e31a112016-07-05 10:26:46 +0200165{
Maxime Ripard0e31a112016-07-05 10:26:46 +0200166 int off;
167
168 off = fdt_path_offset(fdt, "/test-node/sub-test-node");
169 ut_assert(off >= 0);
170
171 ut_assertnonnull(fdt_getprop(fdt, off, "sub-test-property", NULL));
172 ut_assertnonnull(fdt_getprop(fdt, off, "new-sub-test-property", NULL));
173
174 return CMD_RET_SUCCESS;
175}
Simon Glass04e76d52025-02-07 11:30:39 -0700176FDT_OVERLAY_TEST(fdt_overlay_test_add_subnode_property, 0);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200177
Simon Glass04e76d52025-02-07 11:30:39 -0700178static int fdt_overlay_test_local_phandle(struct unit_test_state *uts)
Maxime Ripard0e31a112016-07-05 10:26:46 +0200179{
180 uint32_t local_phandle;
Simon Glass2bac0992025-02-07 11:30:45 -0700181 u32 val[2];
Maxime Ripard0e31a112016-07-05 10:26:46 +0200182 int off;
183
184 off = fdt_path_offset(fdt, "/new-local-node");
185 ut_assert(off >= 0);
186
187 local_phandle = fdt_get_phandle(fdt, off);
188 ut_assert(local_phandle);
189
Simon Glass2bac0992025-02-07 11:30:45 -0700190 ut_assertok(fdtdec_get_int_array(fdt, 0, "test-several-phandle", val,
191 ARRAY_SIZE(val)));
192 ut_asserteq(local_phandle, val[0]);
193 ut_asserteq(local_phandle, val[1]);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200194
195 return CMD_RET_SUCCESS;
196}
Simon Glass04e76d52025-02-07 11:30:39 -0700197FDT_OVERLAY_TEST(fdt_overlay_test_local_phandle, 0);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200198
Simon Glass04e76d52025-02-07 11:30:39 -0700199static int fdt_overlay_test_local_phandles(struct unit_test_state *uts)
Maxime Ripard0e31a112016-07-05 10:26:46 +0200200{
201 uint32_t local_phandle, test_phandle;
Simon Glass2bac0992025-02-07 11:30:45 -0700202 u32 val[2];
Maxime Ripard0e31a112016-07-05 10:26:46 +0200203 int off;
204
205 off = fdt_path_offset(fdt, "/new-local-node");
206 ut_assert(off >= 0);
207
208 local_phandle = fdt_get_phandle(fdt, off);
209 ut_assert(local_phandle);
210
211 off = fdt_path_offset(fdt, "/test-node");
212 ut_assert(off >= 0);
213
214 test_phandle = fdt_get_phandle(fdt, off);
215 ut_assert(test_phandle);
216
Simon Glass2bac0992025-02-07 11:30:45 -0700217 ut_assertok(fdtdec_get_int_array(fdt, 0, "test-phandle", val,
218 ARRAY_SIZE(val)));
219 ut_asserteq(test_phandle, val[0]);
220 ut_asserteq(local_phandle, val[1]);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200221
222 return CMD_RET_SUCCESS;
223}
Simon Glass04e76d52025-02-07 11:30:39 -0700224FDT_OVERLAY_TEST(fdt_overlay_test_local_phandles, 0);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200225
Simon Glass04e76d52025-02-07 11:30:39 -0700226static int fdt_overlay_test_stacked(struct unit_test_state *uts)
Pantelis Antoniouccdc28b2017-09-04 23:12:23 +0300227{
Simon Glass2bac0992025-02-07 11:30:45 -0700228 int off;
229
230 off = fdt_path_offset(fdt, "/new-local-node");
231 ut_assert(off > 0);
Pantelis Antoniouccdc28b2017-09-04 23:12:23 +0300232
Simon Glass2bac0992025-02-07 11:30:45 -0700233 ut_asserteq(43,
234 fdtdec_get_uint(fdt, off, "stacked-test-int-property", 0));
Pantelis Antoniouccdc28b2017-09-04 23:12:23 +0300235
236 return CMD_RET_SUCCESS;
237}
Simon Glass04e76d52025-02-07 11:30:39 -0700238FDT_OVERLAY_TEST(fdt_overlay_test_stacked, 0);
Pantelis Antoniouccdc28b2017-09-04 23:12:23 +0300239
Simon Glass04e76d52025-02-07 11:30:39 -0700240int do_ut_fdt_overlay(struct unit_test_state *uts, struct cmd_tbl *cmdtp,
241 int flag, int argc, char *const argv[])
Maxime Ripard0e31a112016-07-05 10:26:46 +0200242{
Simon Glass04e76d52025-02-07 11:30:39 -0700243 struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_overlay);
244 const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_overlay);
Tom Riniaaa968b2017-09-26 12:43:12 -0400245 int ret = -ENOMEM;
Maxime Ripard0e31a112016-07-05 10:26:46 +0200246
Simon Glass04e76d52025-02-07 11:30:39 -0700247 ret = cmd_ut_category(uts, "fdt_overlay", "fdt_overlay_test_", tests,
248 n_ents, argc, argv);
Maxime Ripard0e31a112016-07-05 10:26:46 +0200249
Heinrich Schuchardta4189002018-12-08 09:53:05 +0100250 free(fdt);
Simon Glassffbfeac2025-02-07 11:30:44 -0700251
Tom Riniaaa968b2017-09-26 12:43:12 -0400252 return ret;
Maxime Ripard0e31a112016-07-05 10:26:46 +0200253}