2 * libfdt - Flat Device Tree manipulation
3 * Testcase common utility functions
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #define _GNU_SOURCE /* for strsignal() in glibc. FreeBSD has it either way */
40 void __attribute__((weak)) cleanup(void)
44 static void sigint_handler(int signum, siginfo_t *si, void *uc)
47 fprintf(stderr, "%s: %s (pid=%d)\n", test_name,
48 strsignal(signum), getpid());
52 void test_init(int argc, char *argv[])
55 struct sigaction sa_int = {
56 .sa_sigaction = sigint_handler,
61 err = sigaction(SIGINT, &sa_int, NULL);
63 FAIL("Can't install SIGINT handler");
65 if (getenv("QUIET_TEST"))
68 verbose_printf("Starting testcase \"%s\", pid %d\n",
72 void check_mem_rsv(void *fdt, int n, uint64_t addr, uint64_t size)
75 uint64_t addr_v, size_v;
77 err = fdt_get_mem_rsv(fdt, n, &addr_v, &size_v);
79 FAIL("fdt_get_mem_rsv(%d): %s", n, fdt_strerror(err));
80 if ((addr_v != addr) || (size_v != size))
81 FAIL("fdt_get_mem_rsv() returned (0x%llx,0x%llx) "
82 "instead of (0x%llx,0x%llx)",
83 (unsigned long long)addr_v, (unsigned long long)size_v,
84 (unsigned long long)addr, (unsigned long long)size);
87 void check_property(void *fdt, int nodeoffset, const char *name,
88 int len, const void *val)
90 const struct fdt_property *prop;
92 uint32_t tag, nameoff, proplen;
95 verbose_printf("Checking property \"%s\"...", name);
96 prop = fdt_get_property(fdt, nodeoffset, name, &retlen);
97 verbose_printf("pointer %p\n", prop);
99 FAIL("Error retreiving \"%s\" pointer: %s", name,
100 fdt_strerror(retlen));
102 tag = fdt32_to_cpu(prop->tag);
103 nameoff = fdt32_to_cpu(prop->nameoff);
104 proplen = fdt32_to_cpu(prop->len);
107 FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
109 propname = fdt_string(fdt, nameoff);
110 if (!propname || !streq(propname, name))
111 FAIL("Property name mismatch \"%s\" instead of \"%s\"",
113 if (proplen != retlen)
114 FAIL("Length retrieved for \"%s\" by fdt_get_property()"
115 " differs from stored length (%d != %d)",
116 name, retlen, proplen);
118 FAIL("Size mismatch on property \"%s\": %d insead of %d",
120 if (memcmp(val, prop->data, len) != 0)
121 FAIL("Data mismatch on property \"%s\"", name);
124 const void *check_getprop(void *fdt, int nodeoffset, const char *name,
125 int len, const void *val)
130 propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
132 FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
135 FAIL("Size mismatch on property \"%s\": %d insead of %d",
137 if (memcmp(val, propval, len) != 0)
138 FAIL("Data mismatch on property \"%s\"", name);
143 int nodename_eq(const char *s1, const char *s2)
145 int len = strlen(s2);
148 if (strncmp(s1, s2, len) != 0)
152 else if (!memchr(s2, '@', len) && (s1[len] == '@'))
158 #define CHUNKSIZE 128
160 void *load_blob(const char *filename)
163 int ret = utilfdt_read_err(filename, &blob);
166 CONFIG("Couldn't open blob from \"%s\": %s", filename,
171 void *load_blob_arg(int argc, char *argv[])
174 CONFIG("Usage: %s <dtb file>", argv[0]);
175 return load_blob(argv[1]);
178 void save_blob(const char *filename, void *fdt)
180 int ret = utilfdt_write_err(filename, fdt);
183 CONFIG("Couldn't write blob to \"%s\": %s", filename,
187 void *open_blob_rw(void *blob)
192 err = fdt_open_into(blob, buf, fdt_totalsize(blob));
193 if (err == -FDT_ERR_NOSPACE) {
194 /* Ran out of space converting to v17 */
195 int newsize = fdt_totalsize(blob) + 8;
197 buf = xmalloc(newsize);
198 err = fdt_open_into(blob, buf, newsize);
201 FAIL("fdt_open_into(): %s", fdt_strerror(err));