• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,int len,const void * val)90 void check_property(void *fdt, int nodeoffset, const char *name,
91 		    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 
105 	tag = fdt32_to_cpu(prop->tag);
106 	nameoff = fdt32_to_cpu(prop->nameoff);
107 	proplen = fdt32_to_cpu(prop->len);
108 
109 	if (tag != FDT_PROP)
110 		FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
111 
112 	propname = fdt_get_string(fdt, nameoff, &namelen);
113 	if (!propname)
114 		FAIL("Couldn't get property name: %s", fdt_strerror(namelen));
115 	if (namelen != strlen(propname))
116 		FAIL("Incorrect prop name length: %d instead of %zd",
117 		     namelen, strlen(propname));
118 	if (!streq(propname, name))
119 		FAIL("Property name mismatch \"%s\" instead of \"%s\"",
120 		     propname, name);
121 	if (proplen != retlen)
122 		FAIL("Length retrieved for \"%s\" by fdt_get_property()"
123 		     " differs from stored length (%d != %d)",
124 		     name, retlen, proplen);
125 	if (proplen != len)
126 		FAIL("Size mismatch on property \"%s\": %d insead of %d",
127 		     name, proplen, len);
128 	if (len && memcmp(val, prop->data, len) != 0)
129 		FAIL("Data mismatch on property \"%s\"", name);
130 }
131 
check_getprop(void * fdt,int nodeoffset,const char * name,int len,const void * val)132 const void *check_getprop(void *fdt, int nodeoffset, const char *name,
133 			  int len, const void *val)
134 {
135 	const void *propval;
136 	int proplen;
137 
138 	propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
139 	if (! propval)
140 		FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
141 
142 	if (proplen != len)
143 		FAIL("Size mismatch on property \"%s\": %d insead of %d",
144 		     name, proplen, len);
145 	if (len && memcmp(val, propval, len) != 0)
146 		FAIL("Data mismatch on property \"%s\"", name);
147 
148 	return propval;
149 }
150 
check_get_prop_offset(void * fdt,int poffset,const char * exp_name,int exp_len,const void * exp_val)151 const void *check_get_prop_offset(void *fdt, int poffset, const char *exp_name,
152 				  int exp_len, const void *exp_val)
153 {
154 	const void *propval;
155 	const char *name;
156 	int proplen;
157 
158 	propval = fdt_getprop_by_offset(fdt, poffset, &name, &proplen);
159 	if (!propval)
160 		FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
161 
162 	/* Not testing for this field, so ignore */
163 	if (strcmp(name, exp_name))
164 		return NULL;
165 
166 	if (proplen != exp_len)
167 		FAIL("Size mismatch on property \"%s\": %d insead of %d",
168 		     name, proplen, exp_len);
169 	if (exp_len && memcmp(exp_val, propval, exp_len))
170 		FAIL("Data mismatch on property \"%s\"", name);
171 
172 	return propval;
173 }
174 
check_getprop_addrrange(void * fdt,int parent,int nodeoffset,const char * name,int num)175 const void *check_getprop_addrrange(void *fdt, int parent, int nodeoffset,
176 				    const char *name, int num)
177 {
178 	const void *propval;
179 	int xac, xsc, buf_size, cells, i;
180 	char *buf, *p;
181 	uint64_t addr, size;
182 	fdt32_t val;
183 
184 	xac = fdt_address_cells(fdt, parent);
185 	xsc = fdt_size_cells(fdt, parent);
186 
187 	if (xac <= 0)
188 		FAIL("Couldn't identify #address-cells: %s",
189 		     fdt_strerror(xac));
190 	if (xsc <= 0)
191 		FAIL("Couldn't identify #size-cells: %s",
192 		     fdt_strerror(xsc));
193 
194 	buf_size = (xac + xsc) * sizeof(fdt32_t) * num;
195 	buf = malloc(buf_size);
196 	if (!buf)
197 		FAIL("Couldn't allocate temporary buffer");
198 
199 	/* expected value */
200 	addr = TEST_MEMREGION_ADDR;
201 	if (xac > 1)
202 		addr += TEST_MEMREGION_ADDR_HI;
203 	size = TEST_MEMREGION_SIZE;
204 	if (xsc > 1)
205 		size += TEST_MEMREGION_SIZE_HI;
206 	for (p = buf, i = 0; i < num; i++) {
207 		cells = xac;
208 		while (cells) {
209 			val = cpu_to_fdt32(addr >> (32 * (--cells)));
210 			memcpy(p, &val, sizeof(val));
211 			p += sizeof(val);
212 		}
213 		cells = xsc;
214 		while (cells) {
215 			val = cpu_to_fdt32(size >> (32 * (--cells)));
216 			memcpy(p, &val, sizeof(val));
217 			p += sizeof(val);
218 		}
219 
220 		addr += size;
221 		size += TEST_MEMREGION_SIZE_INC;
222 	}
223 
224 	/* check */
225 	propval = check_getprop(fdt, nodeoffset, name, buf_size,
226 				(const void *)buf);
227 
228 	free(buf);
229 
230 	return propval;
231 }
232 
nodename_eq(const char * s1,const char * s2)233 int nodename_eq(const char *s1, const char *s2)
234 {
235 	int len = strlen(s2);
236 
237 	if (strncmp(s1, s2, len) != 0)
238 		return 0;
239 	if (s1[len] == '\0')
240 		return 1;
241 	else if (!memchr(s2, '@', len) && (s1[len] == '@'))
242 		return 1;
243 	else
244 		return 0;
245 }
246 
vg_prepare_blob(void * fdt,size_t bufsize)247 void vg_prepare_blob(void *fdt, size_t bufsize)
248 {
249 	char *blob = fdt;
250 	int off_memrsv, off_strings, off_struct;
251 	int num_memrsv;
252 	size_t size_memrsv, size_strings, size_struct;
253 
254 	off_memrsv = fdt_off_mem_rsvmap(fdt);
255 	num_memrsv = fdt_num_mem_rsv(fdt);
256 	if (num_memrsv < 0)
257 		size_memrsv = fdt_totalsize(fdt) - off_memrsv;
258 	else
259 		size_memrsv = (num_memrsv + 1)
260 			* sizeof(struct fdt_reserve_entry);
261 
262 	VALGRIND_MAKE_MEM_UNDEFINED(blob, bufsize);
263 	VALGRIND_MAKE_MEM_DEFINED(blob, FDT_V1_SIZE);
264 	VALGRIND_MAKE_MEM_DEFINED(blob, fdt_header_size(fdt));
265 
266 	if (fdt_magic(fdt) == FDT_MAGIC) {
267 		off_strings = fdt_off_dt_strings(fdt);
268 		if (fdt_version(fdt) >= 3)
269 			size_strings = fdt_size_dt_strings(fdt);
270 		else
271 			size_strings = fdt_totalsize(fdt) - off_strings;
272 
273 		off_struct = fdt_off_dt_struct(fdt);
274 		if (fdt_version(fdt) >= 17)
275 			size_struct = fdt_size_dt_struct(fdt);
276 		else
277 			size_struct = fdt_totalsize(fdt) - off_struct;
278 	} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
279 		size_strings = fdt_size_dt_strings(fdt);
280 		off_strings = fdt_off_dt_strings(fdt) - size_strings;
281 
282 		off_struct = fdt_off_dt_struct(fdt);
283 		size_struct = fdt_size_dt_struct(fdt);
284 		size_struct = fdt_totalsize(fdt) - off_struct;
285 
286 	} else {
287 		CONFIG("Bad magic on vg_prepare_blob()");
288 	}
289 
290 	VALGRIND_MAKE_MEM_DEFINED(blob + off_memrsv, size_memrsv);
291 	VALGRIND_MAKE_MEM_DEFINED(blob + off_strings, size_strings);
292 	VALGRIND_MAKE_MEM_DEFINED(blob + off_struct, size_struct);
293 }
294 
load_blob(const char * filename)295 void *load_blob(const char *filename)
296 {
297 	char *blob;
298 	size_t len;
299 	int ret = utilfdt_read_err(filename, &blob, &len);
300 
301 	if (ret)
302 		CONFIG("Couldn't open blob from \"%s\": %s", filename,
303 		       strerror(ret));
304 
305 	vg_prepare_blob(blob, len);
306 
307 	return blob;
308 }
309 
load_blob_arg(int argc,char * argv[])310 void *load_blob_arg(int argc, char *argv[])
311 {
312 	if (argc != 2)
313 		CONFIG("Usage: %s <dtb file>", argv[0]);
314 	return load_blob(argv[1]);
315 }
316 
save_blob(const char * filename,void * fdt)317 void save_blob(const char *filename, void *fdt)
318 {
319 	size_t size = fdt_totalsize(fdt);
320 	void *tmp;
321 	int ret;
322 
323 	/* Make a temp copy of the blob so that valgrind won't check
324 	 * about uninitialized bits in the pieces between blocks */
325 	tmp = xmalloc(size);
326 	fdt_move(fdt, tmp, size);
327 	VALGRIND_MAKE_MEM_DEFINED(tmp, size);
328 	ret = utilfdt_write_err(filename, tmp);
329 	if (ret)
330 		CONFIG("Couldn't write blob to \"%s\": %s", filename,
331 		       strerror(ret));
332 	free(tmp);
333 }
334 
open_blob_rw(void * blob)335 void *open_blob_rw(void *blob)
336 {
337 	int err;
338 	void *buf = blob;
339 
340 	err = fdt_open_into(blob, buf, fdt_totalsize(blob));
341 	if (err == -FDT_ERR_NOSPACE) {
342 		/* Ran out of space converting to v17 */
343 		int newsize = fdt_totalsize(blob) + 8;
344 
345 		buf = xmalloc(newsize);
346 		err = fdt_open_into(blob, buf, newsize);
347 	}
348 	if (err)
349 		FAIL("fdt_open_into(): %s", fdt_strerror(err));
350 	return buf;
351 }
352