blob: 10129c093fef4626e71475a05467c4bcde7f7a22 [file] [log] [blame]
Rob Herringc4ffc052019-06-20 15:19:41 -06001// SPDX-License-Identifier: LGPL-2.1-or-later
David Gibson3da0f9a2006-11-27 16:21:28 +11002/*
3 * libfdt - Flat Device Tree manipulation
4 * Testcase common utility functions
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
David Gibson3da0f9a2006-11-27 16:21:28 +11006 */
7
David Gibsoncdcb4152008-06-26 11:03:49 +10008#define _GNU_SOURCE /* for strsignal() in glibc. FreeBSD has it either way */
David Gibson3da0f9a2006-11-27 16:21:28 +11009
10#include <stdio.h>
11#include <stdlib.h>
David Gibson857f54e2007-03-23 15:16:54 +110012#include <stdint.h>
David Gibson3da0f9a2006-11-27 16:21:28 +110013#include <limits.h>
14#include <string.h>
15#include <errno.h>
16#include <signal.h>
17#include <unistd.h>
David Gibson4e6221c2006-11-28 17:20:01 +110018#include <fcntl.h>
David Gibson3da0f9a2006-11-27 16:21:28 +110019
David Gibsonb94c0562018-09-10 16:46:59 +100020#if NO_VALGRIND
21static inline void VALGRIND_MAKE_MEM_UNDEFINED(void *p, size_t len)
22{
23}
24
25static inline void VALGRIND_MAKE_MEM_DEFINED(void *p, size_t len)
26{
27}
28#else
David Gibson5b67d2b2018-03-17 20:05:24 +110029#include <valgrind/memcheck.h>
David Gibsonb94c0562018-09-10 16:46:59 +100030#endif
David Gibson5b67d2b2018-03-17 20:05:24 +110031
David Gibson3da0f9a2006-11-27 16:21:28 +110032#include <libfdt.h>
33
34#include "tests.h"
AKASHI Takahiro7fcf8202019-03-27 15:15:52 +090035#include "testdata.h"
David Gibson3da0f9a2006-11-27 16:21:28 +110036
David Gibsonc14223f2018-03-17 19:02:58 +110037/* For FDT_SW_MAGIC */
38#include "libfdt_internal.h"
39
David Gibson3da0f9a2006-11-27 16:21:28 +110040int verbose_test = 1;
41char *test_name;
42
43void __attribute__((weak)) cleanup(void)
44{
45}
46
47static 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
55void 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
David Gibsonfd1bf3a2007-10-10 17:12:12 +100075void 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
David Gibson73d60922006-12-15 15:12:47 +110090void check_property(void *fdt, int nodeoffset, const char *name,
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +010091 unsigned int len, const void *val)
David Gibson3da0f9a2006-11-27 16:21:28 +110092{
David Gibson3da0f9a2006-11-27 16:21:28 +110093 const struct fdt_property *prop;
David Gibson70166d62017-11-14 22:45:56 +110094 int retlen, namelen;
David Gibson3da0f9a2006-11-27 16:21:28 +110095 uint32_t tag, nameoff, proplen;
96 const char *propname;
David Gibson3da0f9a2006-11-27 16:21:28 +110097
98 verbose_printf("Checking property \"%s\"...", name);
David Gibson5434fcc2007-02-23 14:40:10 +110099 prop = fdt_get_property(fdt, nodeoffset, name, &retlen);
David Gibson3da0f9a2006-11-27 16:21:28 +1100100 verbose_printf("pointer %p\n", prop);
101 if (! prop)
Thomas Huth825146d2019-05-20 10:12:09 +0200102 FAIL("Error retrieving \"%s\" pointer: %s", name,
David Gibson5434fcc2007-02-23 14:40:10 +1100103 fdt_strerror(retlen));
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100104 if (retlen < 0)
105 FAIL("negative name length (%d) for returned property\n",
106 retlen);
David Gibson3da0f9a2006-11-27 16:21:28 +1100107
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
David Gibson70166d62017-11-14 22:45:56 +1100115 propname = fdt_get_string(fdt, nameoff, &namelen);
116 if (!propname)
117 FAIL("Couldn't get property name: %s", fdt_strerror(namelen));
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100118 if (namelen < 0)
119 FAIL("negative name length (%d) for returned string\n",
120 namelen);
121 if ((unsigned)namelen != strlen(propname))
David Gibson70166d62017-11-14 22:45:56 +1100122 FAIL("Incorrect prop name length: %d instead of %zd",
123 namelen, strlen(propname));
124 if (!streq(propname, name))
David Gibson3da0f9a2006-11-27 16:21:28 +1100125 FAIL("Property name mismatch \"%s\" instead of \"%s\"",
126 propname, name);
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100127 if (proplen != (unsigned)retlen)
David Gibson5434fcc2007-02-23 14:40:10 +1100128 FAIL("Length retrieved for \"%s\" by fdt_get_property()"
129 " differs from stored length (%d != %d)",
130 name, retlen, proplen);
David Gibson3da0f9a2006-11-27 16:21:28 +1100131 if (proplen != len)
132 FAIL("Size mismatch on property \"%s\": %d insead of %d",
133 name, proplen, len);
David Gibson57f7f9e2018-07-20 14:35:12 +1000134 if (len && memcmp(val, prop->data, len) != 0)
David Gibson3da0f9a2006-11-27 16:21:28 +1100135 FAIL("Data mismatch on property \"%s\"", name);
David Gibson3da0f9a2006-11-27 16:21:28 +1100136}
137
David Gibsona6c76f92007-06-13 14:18:10 +1000138const void *check_getprop(void *fdt, int nodeoffset, const char *name,
139 int len, const void *val)
David Gibson3da0f9a2006-11-27 16:21:28 +1100140{
David Gibsona6c76f92007-06-13 14:18:10 +1000141 const void *propval;
David Gibson3da0f9a2006-11-27 16:21:28 +1100142 int proplen;
David Gibson3da0f9a2006-11-27 16:21:28 +1100143
144 propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
David Gibsona7ee95d2006-12-15 15:12:49 +1100145 if (! propval)
David Gibson5434fcc2007-02-23 14:40:10 +1100146 FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
David Gibson3da0f9a2006-11-27 16:21:28 +1100147
148 if (proplen != len)
149 FAIL("Size mismatch on property \"%s\": %d insead of %d",
150 name, proplen, len);
David Gibson57f7f9e2018-07-20 14:35:12 +1000151 if (len && memcmp(val, propval, len) != 0)
David Gibson3da0f9a2006-11-27 16:21:28 +1100152 FAIL("Data mismatch on property \"%s\"", name);
153
154 return propval;
155}
David Gibson4e6221c2006-11-28 17:20:01 +1100156
Simon Glass82a52ce2018-11-22 13:15:06 -0700157const 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
AKASHI Takahiro7fcf8202019-03-27 15:15:52 +0900181const 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}
Simon Glass82a52ce2018-11-22 13:15:06 -0700238
David Gibsond2a9da02007-09-28 15:51:04 +1000239int nodename_eq(const char *s1, const char *s2)
240{
241 int len = strlen(s2);
242
David Gibsond2a9da02007-09-28 15:51:04 +1000243 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
David Gibsonc14223f2018-03-17 19:02:58 +1100253void vg_prepare_blob(void *fdt, size_t bufsize)
254{
255 char *blob = fdt;
256 int off_memrsv, off_strings, off_struct;
David Gibson85bce8b2018-07-23 12:16:09 +1000257 int num_memrsv;
David Gibsonc14223f2018-03-17 19:02:58 +1100258 size_t size_memrsv, size_strings, size_struct;
259
David Gibson85bce8b2018-07-23 12:16:09 +1000260 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);
David Gibsonc14223f2018-03-17 19:02:58 +1100267
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) {
David Gibsonc14223f2018-03-17 19:02:58 +1100273 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) {
David Gibsonc14223f2018-03-17 19:02:58 +1100285 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
David Gibson4e6221c2006-11-28 17:20:01 +1100301void *load_blob(const char *filename)
302{
Simon Glass1c25c0d2011-09-22 10:11:03 -0700303 char *blob;
David Gibsonc14223f2018-03-17 19:02:58 +1100304 size_t len;
305 int ret = utilfdt_read_err(filename, &blob, &len);
David Gibson4e6221c2006-11-28 17:20:01 +1100306
Simon Glass1c25c0d2011-09-22 10:11:03 -0700307 if (ret)
David Gibson4e6221c2006-11-28 17:20:01 +1100308 CONFIG("Couldn't open blob from \"%s\": %s", filename,
Simon Glass1c25c0d2011-09-22 10:11:03 -0700309 strerror(ret));
David Gibsonc14223f2018-03-17 19:02:58 +1100310
311 vg_prepare_blob(blob, len);
312
Simon Glass1c25c0d2011-09-22 10:11:03 -0700313 return blob;
David Gibson4e6221c2006-11-28 17:20:01 +1100314}
315
316void *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}
David Gibson063693a2006-11-29 16:45:46 +1100322
David Gibson73d60922006-12-15 15:12:47 +1100323void save_blob(const char *filename, void *fdt)
David Gibson063693a2006-11-29 16:45:46 +1100324{
David Gibson5b67d2b2018-03-17 20:05:24 +1100325 size_t size = fdt_totalsize(fdt);
326 void *tmp;
327 int ret;
David Gibson063693a2006-11-29 16:45:46 +1100328
David Gibson5b67d2b2018-03-17 20:05:24 +1100329 /* 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);
Simon Glass1c25c0d2011-09-22 10:11:03 -0700335 if (ret)
336 CONFIG("Couldn't write blob to \"%s\": %s", filename,
337 strerror(ret));
David Gibson5b67d2b2018-03-17 20:05:24 +1100338 free(tmp);
David Gibson063693a2006-11-29 16:45:46 +1100339}
David Gibsona041dcd2007-11-01 11:37:31 +1100340
341void *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}