1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
* libfdt - Flat Device Tree manipulation
* Testcase common utility functions
* Copyright (C) 2006 David Gibson, IBM Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE /* for strsignal() */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <libfdt.h>
#include "tests.h"
int verbose_test = 1;
char *test_name;
void __attribute__((weak)) cleanup(void)
{
}
static void sigint_handler(int signum, siginfo_t *si, void *uc)
{
cleanup();
fprintf(stderr, "%s: %s (pid=%d)\n", test_name,
strsignal(signum), getpid());
exit(RC_BUG);
}
void test_init(int argc, char *argv[])
{
int err;
struct sigaction sa_int = {
.sa_sigaction = sigint_handler,
};
test_name = argv[0];
err = sigaction(SIGINT, &sa_int, NULL);
if (err)
FAIL("Can't install SIGINT handler");
if (getenv("QUIET_TEST"))
verbose_test = 0;
verbose_printf("Starting testcase \"%s\", pid %d\n",
test_name, getpid());
}
void check_property(void *fdt, int nodeoffset, const char *name,
int len, const void *val)
{
const struct fdt_property *prop;
int retlen;
uint32_t tag, nameoff, proplen;
const char *propname;
verbose_printf("Checking property \"%s\"...", name);
prop = fdt_get_property(fdt, nodeoffset, name, &retlen);
verbose_printf("pointer %p\n", prop);
if (! prop)
FAIL("Error retreiving \"%s\" pointer: %s", name,
fdt_strerror(retlen));
tag = fdt32_to_cpu(prop->tag);
nameoff = fdt32_to_cpu(prop->nameoff);
proplen = fdt32_to_cpu(prop->len);
if (tag != FDT_PROP)
FAIL("Incorrect tag 0x%08x on property \"%s\"", tag, name);
propname = fdt_string(fdt, nameoff);
if (!propname || !streq(propname, name))
FAIL("Property name mismatch \"%s\" instead of \"%s\"",
propname, name);
if (proplen != retlen)
FAIL("Length retrieved for \"%s\" by fdt_get_property()"
" differs from stored length (%d != %d)",
name, retlen, proplen);
if (proplen != len)
FAIL("Size mismatch on property \"%s\": %d insead of %d",
name, proplen, len);
if (memcmp(val, prop->data, len) != 0)
FAIL("Data mismatch on property \"%s\"", name);
}
const void *check_getprop(void *fdt, int nodeoffset, const char *name,
int len, const void *val)
{
const void *propval;
int proplen;
propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
if (! propval)
FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(proplen));
if (proplen != len)
FAIL("Size mismatch on property \"%s\": %d insead of %d",
name, proplen, len);
if (memcmp(val, propval, len) != 0)
FAIL("Data mismatch on property \"%s\"", name);
return propval;
}
#define CHUNKSIZE 128
void *load_blob(const char *filename)
{
int fd;
int offset = 0;
int bufsize = 1024;
char *p = NULL;
int ret;
fd = open(filename, O_RDONLY);
if (fd < 0)
CONFIG("Couldn't open blob from \"%s\": %s", filename,
strerror(errno));
p = xmalloc(bufsize);
do {
if (offset == bufsize) {
bufsize *= 2;
p = xrealloc(p, bufsize);
}
ret = read(fd, &p[offset], bufsize - offset);
if (ret < 0)
CONFIG("Couldn't read from \"%s\": %s", filename,
strerror(errno));
offset += ret;
} while (ret != 0);
return p;
}
void *load_blob_arg(int argc, char *argv[])
{
if (argc != 2)
CONFIG("Usage: %s <dtb file>", argv[0]);
return load_blob(argv[1]);
}
void save_blob(const char *filename, void *fdt)
{
int fd;
int totalsize;
int offset;
void *p;
int ret;
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
CONFIG("Couldn't open \"%s\" to write blob: %s", filename,
strerror(errno));
totalsize = fdt_totalsize(fdt);
offset = 0;
p = fdt;
while (offset < totalsize) {
ret = write(fd, p + offset, totalsize - offset);
if (ret < 0)
CONFIG("Couldn't write to \"%s\": %s", filename,
strerror(errno));
offset += ret;
}
}
|