|
1 /* |
|
2 * libfdt - Flat Device Tree manipulation |
|
3 * Testcase common utility functions |
|
4 * Copyright (C) 2006 David Gibson, IBM Corporation. |
|
5 * |
|
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. |
|
10 * |
|
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. |
|
15 * |
|
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 |
|
19 */ |
|
20 |
|
21 #define _GNU_SOURCE /* for strsignal() in glibc. FreeBSD has it either way */ |
|
22 |
|
23 #include <stdio.h> |
|
24 #include <stdlib.h> |
|
25 #include <stdint.h> |
|
26 #include <limits.h> |
|
27 #include <string.h> |
|
28 #include <errno.h> |
|
29 #include <signal.h> |
|
30 #include <unistd.h> |
|
31 #include <fcntl.h> |
|
32 |
|
33 #include <libfdt.h> |
|
34 |
|
35 #include "tests.h" |
|
36 |
|
37 int verbose_test = 1; |
|
38 char *test_name; |
|
39 |
|
40 void __attribute__((weak)) cleanup(void) |
|
41 { |
|
42 } |
|
43 |
|
44 static void sigint_handler(int signum, siginfo_t *si, void *uc) |
|
45 { |
|
46 cleanup(); |
|
47 fprintf(stderr, "%s: %s (pid=%d)\n", test_name, |
|
48 strsignal(signum), getpid()); |
|
49 exit(RC_BUG); |
|
50 } |
|
51 |
|
52 void test_init(int argc, char *argv[]) |
|
53 { |
|
54 int err; |
|
55 struct sigaction sa_int = { |
|
56 .sa_sigaction = sigint_handler, |
|
57 }; |
|
58 |
|
59 test_name = argv[0]; |
|
60 |
|
61 err = sigaction(SIGINT, &sa_int, NULL); |
|
62 if (err) |
|
63 FAIL("Can't install SIGINT handler"); |
|
64 |
|
65 if (getenv("QUIET_TEST")) |
|
66 verbose_test = 0; |
|
67 |
|
68 verbose_printf("Starting testcase \"%s\", pid %d\n", |
|
69 test_name, getpid()); |
|
70 } |
|
71 |
|
72 void check_mem_rsv(void *fdt, int n, uint64_t addr, uint64_t size) |
|
73 { |
|
74 int err; |
|
75 uint64_t addr_v, size_v; |
|
76 |
|
77 err = fdt_get_mem_rsv(fdt, n, &addr_v, &size_v); |
|
78 if (err < 0) |
|
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); |
|
85 } |
|
86 |
|
87 void check_property(void *fdt, int nodeoffset, const char *name, |
|
88 int len, const void *val) |
|
89 { |
|
90 const struct fdt_property *prop; |
|
91 int retlen; |
|
92 uint32_t tag, nameoff, proplen; |
|
93 const char *propname; |
|
94 |
|
95 verbose_printf("Checking property \"%s\"...", name); |
|
96 prop = fdt_get_property(fdt, nodeoffset, name, &retlen); |
|
97 verbose_printf("pointer %p\n", prop); |
|
98 if (! prop) |
|
99 FAIL("Error retreiving \"%s\" pointer: %s", name, |
|
100 fdt_strerror(retlen)); |
|
101 |
|
102 tag = fdt32_to_cpu(prop->tag); |
|
103 nameoff = fdt32_to_cpu(prop->nameoff); |
|
104 proplen = fdt32_to_cpu(prop->len); |
|
105 |
|
106 if (tag != FDT_PROP) |
|
107 FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name); |
|
108 |
|
109 propname = fdt_string(fdt, nameoff); |
|
110 if (!propname || !streq(propname, name)) |
|
111 FAIL("Property name mismatch \"%s\" instead of \"%s\"", |
|
112 propname, name); |
|
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); |
|
117 if (proplen != len) |
|
118 FAIL("Size mismatch on property \"%s\": %d insead of %d", |
|
119 name, proplen, len); |
|
120 if (memcmp(val, prop->data, len) != 0) |
|
121 FAIL("Data mismatch on property \"%s\"", name); |
|
122 } |
|
123 |
|
124 const void *check_getprop(void *fdt, int nodeoffset, const char *name, |
|
125 int len, const void *val) |
|
126 { |
|
127 const void *propval; |
|
128 int proplen; |
|
129 |
|
130 propval = fdt_getprop(fdt, nodeoffset, name, &proplen); |
|
131 if (! propval) |
|
132 FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen)); |
|
133 |
|
134 if (proplen != len) |
|
135 FAIL("Size mismatch on property \"%s\": %d insead of %d", |
|
136 name, proplen, len); |
|
137 if (memcmp(val, propval, len) != 0) |
|
138 FAIL("Data mismatch on property \"%s\"", name); |
|
139 |
|
140 return propval; |
|
141 } |
|
142 |
|
143 int nodename_eq(const char *s1, const char *s2) |
|
144 { |
|
145 int len = strlen(s2); |
|
146 |
|
147 len = strlen(s2); |
|
148 if (strncmp(s1, s2, len) != 0) |
|
149 return 0; |
|
150 if (s1[len] == '\0') |
|
151 return 1; |
|
152 else if (!memchr(s2, '@', len) && (s1[len] == '@')) |
|
153 return 1; |
|
154 else |
|
155 return 0; |
|
156 } |
|
157 |
|
158 #define CHUNKSIZE 128 |
|
159 |
|
160 void *load_blob(const char *filename) |
|
161 { |
|
162 int fd; |
|
163 int offset = 0; |
|
164 int bufsize = 1024; |
|
165 char *p = NULL; |
|
166 int ret; |
|
167 |
|
168 fd = open(filename, O_RDONLY); |
|
169 if (fd < 0) |
|
170 CONFIG("Couldn't open blob from \"%s\": %s", filename, |
|
171 strerror(errno)); |
|
172 |
|
173 p = xmalloc(bufsize); |
|
174 do { |
|
175 if (offset == bufsize) { |
|
176 bufsize *= 2; |
|
177 p = xrealloc(p, bufsize); |
|
178 } |
|
179 |
|
180 ret = read(fd, &p[offset], bufsize - offset); |
|
181 if (ret < 0) |
|
182 CONFIG("Couldn't read from \"%s\": %s", filename, |
|
183 strerror(errno)); |
|
184 |
|
185 offset += ret; |
|
186 } while (ret != 0); |
|
187 |
|
188 return p; |
|
189 } |
|
190 |
|
191 void *load_blob_arg(int argc, char *argv[]) |
|
192 { |
|
193 if (argc != 2) |
|
194 CONFIG("Usage: %s <dtb file>", argv[0]); |
|
195 return load_blob(argv[1]); |
|
196 } |
|
197 |
|
198 void save_blob(const char *filename, void *fdt) |
|
199 { |
|
200 int fd; |
|
201 int totalsize; |
|
202 int offset; |
|
203 char *p; |
|
204 int ret; |
|
205 |
|
206 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
|
207 if (fd < 0) |
|
208 CONFIG("Couldn't open \"%s\" to write blob: %s", filename, |
|
209 strerror(errno)); |
|
210 |
|
211 totalsize = fdt_totalsize(fdt); |
|
212 offset = 0; |
|
213 p = fdt; |
|
214 |
|
215 while (offset < totalsize) { |
|
216 ret = write(fd, p + offset, totalsize - offset); |
|
217 if (ret < 0) |
|
218 CONFIG("Couldn't write to \"%s\": %s", filename, |
|
219 strerror(errno)); |
|
220 offset += ret; |
|
221 } |
|
222 } |
|
223 |
|
224 void *open_blob_rw(void *blob) |
|
225 { |
|
226 int err; |
|
227 void *buf = blob; |
|
228 |
|
229 err = fdt_open_into(blob, buf, fdt_totalsize(blob)); |
|
230 if (err == -FDT_ERR_NOSPACE) { |
|
231 /* Ran out of space converting to v17 */ |
|
232 int newsize = fdt_totalsize(blob) + 8; |
|
233 |
|
234 buf = xmalloc(newsize); |
|
235 err = fdt_open_into(blob, buf, newsize); |
|
236 } |
|
237 if (err) |
|
238 FAIL("fdt_open_into(): %s", fdt_strerror(err)); |
|
239 return buf; |
|
240 } |