1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Testcase common utility functions
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 */
7
8 #define _GNU_SOURCE /* for strsignal() in glibc. FreeBSD has it either way */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <limits.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <signal.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19
20 #if NO_VALGRIND
VALGRIND_MAKE_MEM_UNDEFINED(void * p,size_t len)21 static inline void VALGRIND_MAKE_MEM_UNDEFINED(void *p, size_t len)
22 {
23 }
24
VALGRIND_MAKE_MEM_DEFINED(void * p,size_t len)25 static inline void VALGRIND_MAKE_MEM_DEFINED(void *p, size_t len)
26 {
27 }
28 #else
29 #include <valgrind/memcheck.h>
30 #endif
31
32 #include <libfdt.h>
33
34 #include "tests.h"
35 #include "testdata.h"
36
37 /* For FDT_SW_MAGIC */
38 #include "libfdt_internal.h"
39
40 int verbose_test = 1;
41 char *test_name;
42
cleanup(void)43 void __attribute__((weak)) cleanup(void)
44 {
45 }
46
sigint_handler(int signum,siginfo_t * si,void * uc)47 static void sigint_handler(int signum, siginfo_t *si, void *uc)
48 {
49 cleanup();
50 fprintf(stderr, "%s: %s (pid=%d)\n", test_name,
51 strsignal(signum), getpid());
52 exit(RC_BUG);
53 }
54
test_init(int argc,char * argv[])55 void test_init(int argc, char *argv[])
56 {
57 int err;
58 struct sigaction sa_int = {
59 .sa_sigaction = sigint_handler,
60 };
61
62 test_name = argv[0];
63
64 err = sigaction(SIGINT, &sa_int, NULL);
65 if (err)
66 FAIL("Can't install SIGINT handler");
67
68 if (getenv("QUIET_TEST"))
69 verbose_test = 0;
70
71 verbose_printf("Starting testcase \"%s\", pid %d\n",
72 test_name, getpid());
73 }
74
check_mem_rsv(void * fdt,int n,uint64_t addr,uint64_t size)75 void check_mem_rsv(void *fdt, int n, uint64_t addr, uint64_t size)
76 {
77 int err;
78 uint64_t addr_v, size_v;
79
80 err = fdt_get_mem_rsv(fdt, n, &addr_v, &size_v);
81 if (err < 0)
82 FAIL("fdt_get_mem_rsv(%d): %s", n, fdt_strerror(err));
83 if ((addr_v != addr) || (size_v != size))
84 FAIL("fdt_get_mem_rsv() returned (0x%llx,0x%llx) "
85 "instead of (0x%llx,0x%llx)",
86 (unsigned long long)addr_v, (unsigned long long)size_v,
87 (unsigned long long)addr, (unsigned long long)size);
88 }
89
check_property(void * fdt,int nodeoffset,const char * name,unsigned int len,const void * val)90 void check_property(void *fdt, int nodeoffset, const char *name,
91 unsigned int len, const void *val)
92 {
93 const struct fdt_property *prop;
94 int retlen, namelen;
95 uint32_t tag, nameoff, proplen;
96 const char *propname;
97
98 verbose_printf("Checking property \"%s\"...", name);
99 prop = fdt_get_property(fdt, nodeoffset, name, &retlen);
100 verbose_printf("pointer %p\n", prop);
101 if (! prop)
102 FAIL("Error retrieving \"%s\" pointer: %s", name,
103 fdt_strerror(retlen));
104 if (retlen < 0)
105 FAIL("negative name length (%d) for returned property\n",
106 retlen);
107
108 tag = fdt32_to_cpu(prop->tag);
109 nameoff = fdt32_to_cpu(prop->nameoff);
110 proplen = fdt32_to_cpu(prop->len);
111
112 if (tag != FDT_PROP)
113 FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
114
115 propname = fdt_get_string(fdt, nameoff, &namelen);
116 if (!propname)
117 FAIL("Couldn't get property name: %s", fdt_strerror(namelen));
118 if (namelen < 0)
119 FAIL("negative name length (%d) for returned string\n",
120 namelen);
121 if ((unsigned)namelen != strlen(propname))
122 FAIL("Incorrect prop name length: %d instead of %zd",
123 namelen, strlen(propname));
124 if (!streq(propname, name))
125 FAIL("Property name mismatch \"%s\" instead of \"%s\"",
126 propname, name);
127 if (proplen != (unsigned)retlen)
128 FAIL("Length retrieved for \"%s\" by fdt_get_property()"
129 " differs from stored length (%d != %d)",
130 name, retlen, proplen);
131 if (proplen != len)
132 FAIL("Size mismatch on property \"%s\": %d insead of %d",
133 name, proplen, len);
134 if (len && memcmp(val, prop->data, len) != 0)
135 FAIL("Data mismatch on property \"%s\"", name);
136 }
137
check_getprop(void * fdt,int nodeoffset,const char * name,int len,const void * val)138 const void *check_getprop(void *fdt, int nodeoffset, const char *name,
139 int len, const void *val)
140 {
141 const void *propval;
142 int proplen;
143
144 propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
145 if (! propval)
146 FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
147
148 if (proplen != len)
149 FAIL("Size mismatch on property \"%s\": %d insead of %d",
150 name, proplen, len);
151 if (len && memcmp(val, propval, len) != 0)
152 FAIL("Data mismatch on property \"%s\"", name);
153
154 return propval;
155 }
156
check_get_prop_offset(void * fdt,int poffset,const char * exp_name,int exp_len,const void * exp_val)157 const void *check_get_prop_offset(void *fdt, int poffset, const char *exp_name,
158 int exp_len, const void *exp_val)
159 {
160 const void *propval;
161 const char *name;
162 int proplen;
163
164 propval = fdt_getprop_by_offset(fdt, poffset, &name, &proplen);
165 if (!propval)
166 FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
167
168 /* Not testing for this field, so ignore */
169 if (strcmp(name, exp_name))
170 return NULL;
171
172 if (proplen != exp_len)
173 FAIL("Size mismatch on property \"%s\": %d insead of %d",
174 name, proplen, exp_len);
175 if (exp_len && memcmp(exp_val, propval, exp_len))
176 FAIL("Data mismatch on property \"%s\"", name);
177
178 return propval;
179 }
180
check_getprop_addrrange(void * fdt,int parent,int nodeoffset,const char * name,int num)181 const void *check_getprop_addrrange(void *fdt, int parent, int nodeoffset,
182 const char *name, int num)
183 {
184 const void *propval;
185 int xac, xsc, buf_size, cells, i;
186 char *buf, *p;
187 uint64_t addr, size;
188 fdt32_t val;
189
190 xac = fdt_address_cells(fdt, parent);
191 xsc = fdt_size_cells(fdt, parent);
192
193 if (xac <= 0)
194 FAIL("Couldn't identify #address-cells: %s",
195 fdt_strerror(xac));
196 if (xsc <= 0)
197 FAIL("Couldn't identify #size-cells: %s",
198 fdt_strerror(xsc));
199
200 buf_size = (xac + xsc) * sizeof(fdt32_t) * num;
201 buf = malloc(buf_size);
202 if (!buf)
203 FAIL("Couldn't allocate temporary buffer");
204
205 /* expected value */
206 addr = TEST_MEMREGION_ADDR;
207 if (xac > 1)
208 addr += TEST_MEMREGION_ADDR_HI;
209 size = TEST_MEMREGION_SIZE;
210 if (xsc > 1)
211 size += TEST_MEMREGION_SIZE_HI;
212 for (p = buf, i = 0; i < num; i++) {
213 cells = xac;
214 while (cells) {
215 val = cpu_to_fdt32(addr >> (32 * (--cells)));
216 memcpy(p, &val, sizeof(val));
217 p += sizeof(val);
218 }
219 cells = xsc;
220 while (cells) {
221 val = cpu_to_fdt32(size >> (32 * (--cells)));
222 memcpy(p, &val, sizeof(val));
223 p += sizeof(val);
224 }
225
226 addr += size;
227 size += TEST_MEMREGION_SIZE_INC;
228 }
229
230 /* check */
231 propval = check_getprop(fdt, nodeoffset, name, buf_size,
232 (const void *)buf);
233
234 free(buf);
235
236 return propval;
237 }
238
nodename_eq(const char * s1,const char * s2)239 int nodename_eq(const char *s1, const char *s2)
240 {
241 int len = strlen(s2);
242
243 if (strncmp(s1, s2, len) != 0)
244 return 0;
245 if (s1[len] == '\0')
246 return 1;
247 else if (!memchr(s2, '@', len) && (s1[len] == '@'))
248 return 1;
249 else
250 return 0;
251 }
252
vg_prepare_blob(void * fdt,size_t bufsize)253 void vg_prepare_blob(void *fdt, size_t bufsize)
254 {
255 char *blob = fdt;
256 int off_memrsv, off_strings, off_struct;
257 int num_memrsv;
258 size_t size_memrsv, size_strings, size_struct;
259
260 off_memrsv = fdt_off_mem_rsvmap(fdt);
261 num_memrsv = fdt_num_mem_rsv(fdt);
262 if (num_memrsv < 0)
263 size_memrsv = fdt_totalsize(fdt) - off_memrsv;
264 else
265 size_memrsv = (num_memrsv + 1)
266 * sizeof(struct fdt_reserve_entry);
267
268 VALGRIND_MAKE_MEM_UNDEFINED(blob, bufsize);
269 VALGRIND_MAKE_MEM_DEFINED(blob, FDT_V1_SIZE);
270 VALGRIND_MAKE_MEM_DEFINED(blob, fdt_header_size(fdt));
271
272 if (fdt_magic(fdt) == FDT_MAGIC) {
273 off_strings = fdt_off_dt_strings(fdt);
274 if (fdt_version(fdt) >= 3)
275 size_strings = fdt_size_dt_strings(fdt);
276 else
277 size_strings = fdt_totalsize(fdt) - off_strings;
278
279 off_struct = fdt_off_dt_struct(fdt);
280 if (fdt_version(fdt) >= 17)
281 size_struct = fdt_size_dt_struct(fdt);
282 else
283 size_struct = fdt_totalsize(fdt) - off_struct;
284 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
285 size_strings = fdt_size_dt_strings(fdt);
286 off_strings = fdt_off_dt_strings(fdt) - size_strings;
287
288 off_struct = fdt_off_dt_struct(fdt);
289 size_struct = fdt_size_dt_struct(fdt);
290 size_struct = fdt_totalsize(fdt) - off_struct;
291
292 } else {
293 CONFIG("Bad magic on vg_prepare_blob()");
294 }
295
296 VALGRIND_MAKE_MEM_DEFINED(blob + off_memrsv, size_memrsv);
297 VALGRIND_MAKE_MEM_DEFINED(blob + off_strings, size_strings);
298 VALGRIND_MAKE_MEM_DEFINED(blob + off_struct, size_struct);
299 }
300
load_blob(const char * filename)301 void *load_blob(const char *filename)
302 {
303 char *blob;
304 size_t len;
305 int ret = utilfdt_read_err(filename, &blob, &len);
306
307 if (ret)
308 CONFIG("Couldn't open blob from \"%s\": %s", filename,
309 strerror(ret));
310
311 vg_prepare_blob(blob, len);
312
313 return blob;
314 }
315
load_blob_arg(int argc,char * argv[])316 void *load_blob_arg(int argc, char *argv[])
317 {
318 if (argc != 2)
319 CONFIG("Usage: %s <dtb file>", argv[0]);
320 return load_blob(argv[1]);
321 }
322
save_blob(const char * filename,void * fdt)323 void save_blob(const char *filename, void *fdt)
324 {
325 size_t size = fdt_totalsize(fdt);
326 void *tmp;
327 int ret;
328
329 /* Make a temp copy of the blob so that valgrind won't check
330 * about uninitialized bits in the pieces between blocks */
331 tmp = xmalloc(size);
332 fdt_move(fdt, tmp, size);
333 VALGRIND_MAKE_MEM_DEFINED(tmp, size);
334 ret = utilfdt_write_err(filename, tmp);
335 if (ret)
336 CONFIG("Couldn't write blob to \"%s\": %s", filename,
337 strerror(ret));
338 free(tmp);
339 }
340
open_blob_rw(void * blob)341 void *open_blob_rw(void *blob)
342 {
343 int err;
344 void *buf = blob;
345
346 err = fdt_open_into(blob, buf, fdt_totalsize(blob));
347 if (err == -FDT_ERR_NOSPACE) {
348 /* Ran out of space converting to v17 */
349 int newsize = fdt_totalsize(blob) + 8;
350
351 buf = xmalloc(newsize);
352 err = fdt_open_into(blob, buf, newsize);
353 }
354 if (err)
355 FAIL("fdt_open_into(): %s", fdt_strerror(err));
356 return buf;
357 }
358