-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathutil.c
255 lines (214 loc) · 5.44 KB
/
util.c
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <assert.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
// We're actually going to cheat and cast the pointers, but define
// a structure to keep the compiler happy.
struct dt_subnode_iter
{
DIR *dh;
};
const char *dtpath;
static void *do_read_file(const char *fname, const char *mode, size_t *plen)
{
FILE *fp = fopen(fname, mode);
void *buf;
long len;
if (fp == NULL)
return NULL;
fseek(fp, 0, SEEK_END);
len = ftell(fp);
if (plen)
*plen = len;
buf = malloc(len);
fseek(fp, 0, SEEK_SET);
if (buf)
{
if (fread(buf, 1, len, fp) != (size_t)len)
{
free(buf);
buf = NULL;
}
}
fclose(fp);
return (char *)buf;
}
char *read_text_file(const char *fname, size_t *plen)
{
return do_read_file(fname, "rt", plen);
}
void *read_file(const char *fname, size_t *plen)
{
return do_read_file(fname, "rb", plen);
}
void dt_set_path(const char *path)
{
dtpath = path;
}
char *dt_read_prop(const char *node, const char *prop, size_t *plen)
{
char filename[FILENAME_MAX];
size_t len;
len = snprintf(filename, sizeof(filename), "%s%s/%s", dtpath, node, prop);
if (len >= sizeof(filename))
{
assert(0);
return NULL;
}
filename[sizeof(filename) - 1] = '\0';
return read_file(filename, plen);
}
uint32_t *dt_read_cells(const char *node, const char *prop, unsigned *num_cells)
{
uint8_t *buf;
size_t len, i;
buf = (uint8_t *)dt_read_prop(node, prop, &len);
if (buf)
{
for (i = 0; i + 3 < len; i += 4)
{
*(uint32_t *)(buf + i) = (buf[i] << 24) + (buf[i + 1] << 16) +
(buf[i + 2] << 8) + (buf[i + 3] << 0);
}
*num_cells = i >> 2;
}
return (uint32_t *)buf;
}
uint64_t dt_extract_num(const uint32_t *cells, int size)
{
uint64_t val = 0;
int i;
/* PCIe uses 3 cells for an address, but we can ignore the first cell.
* In this case, the big-endian representation makes it easy because
* the unwanted portion is shifted off the top.
*/
for (i = 0; i < size; i++)
{
val = (val << 32) | cells[i];
}
return val;
}
uint64_t dt_read_num(const char *node, const char *prop, size_t size)
{
unsigned num_cells;
uint32_t *cells = dt_read_cells(node, prop, &num_cells);
uint64_t val = 0;
if (cells)
{
if (size <= num_cells)
val = dt_extract_num(cells, size);
dt_free(cells);
}
return val;
}
uint32_t dt_read_u32(const char *node, const char *prop)
{
return dt_read_num(node, prop, 1);
}
static uint64_t dt_translate_addr(const uint32_t *ranges, unsigned ranges_cells,
int npa, int nps, int nca, uint64_t addr)
{
/* Entries in the ranges table take the form:
* <child addr> <parent addr> <size>
* where the elements are big-endian numbers of lengths nca, npa and nps
* respectively.
*/
unsigned pos = 0;
while (pos + npa + nps + nca <= ranges_cells)
{
uint64_t ca, pa, ps;
ca = dt_extract_num(ranges + pos, nca);
pa = dt_extract_num(ranges + pos + nca, npa);
ps = dt_extract_num(ranges + pos + nca + npa, nps);
if (addr >= ca && addr <= ca + ps)
{
addr -= ca;
addr += pa;
break;
}
pos += npa + nps + nca;
}
return addr;
}
uint64_t dt_parse_addr(const char *node)
{
char buf1[FILENAME_MAX], buf2[FILENAME_MAX];
char *parent, *nextparent;
uint32_t *ranges = NULL;
unsigned ranges_cells = 0;
uint64_t addr = INVALID_ADDRESS;
unsigned npa, nps, nca = 0;
parent = buf1;
nextparent = buf2;
while (1)
{
char *tmp, *p;
strcpy(parent, node);
p = strrchr(parent, '/');
if (!p)
return INVALID_ADDRESS;
if (p == parent)
p[1] = '\0';
else
p[0] = '\0';
npa = dt_read_u32(parent, "#address-cells");
nps = dt_read_u32(parent, "#size-cells");
if (!npa || !nps)
{
addr = INVALID_ADDRESS;
break;
}
if (addr == INVALID_ADDRESS)
{
addr = dt_read_num(node, "reg", npa);
}
else if (ranges)
{
addr = dt_translate_addr(ranges, ranges_cells, npa, nps, nca, addr);
dt_free(ranges);
ranges = NULL;
}
if (parent[1] == '\0')
break;
ranges = dt_read_cells(parent, "ranges", &ranges_cells);
nca = npa;
node = parent;
/* Swap parent and nextparent */
tmp = parent;
parent = nextparent;
nextparent = tmp;
}
dt_free(ranges);
return addr;
}
void dt_free(void *value)
{
free(value);
}
DT_SUBNODE_HANDLE dt_open_subnodes(const char *node)
{
char dirpath[FILENAME_MAX];
size_t len;
len = snprintf(dirpath, sizeof(dirpath), "%s%s", dtpath, node);
if (len >= sizeof(dirpath))
{
assert(0);
return NULL;
}
return (DT_SUBNODE_HANDLE)opendir(dirpath);
}
const char *dt_next_subnode(DT_SUBNODE_HANDLE handle)
{
DIR *dirh = (DIR *)handle;
struct dirent *dent;
dent = readdir(dirh);
return dent ? dent->d_name : NULL;
}
void dt_close_subnodes(DT_SUBNODE_HANDLE handle)
{
DIR *dirh = (DIR *)handle;
closedir(dirh);
}