fdt_ro.c revision 1.1 1 1.1 macallan /*
2 1.1 macallan * libfdt - Flat Device Tree manipulation
3 1.1 macallan * Copyright (C) 2006 David Gibson, IBM Corporation.
4 1.1 macallan *
5 1.1 macallan * libfdt is dual licensed: you can use it either under the terms of
6 1.1 macallan * the GPL, or the BSD license, at your option.
7 1.1 macallan *
8 1.1 macallan * a) This library is free software; you can redistribute it and/or
9 1.1 macallan * modify it under the terms of the GNU General Public License as
10 1.1 macallan * published by the Free Software Foundation; either version 2 of the
11 1.1 macallan * License, or (at your option) any later version.
12 1.1 macallan *
13 1.1 macallan * This library is distributed in the hope that it will be useful,
14 1.1 macallan * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 1.1 macallan * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 1.1 macallan * GNU General Public License for more details.
17 1.1 macallan *
18 1.1 macallan * You should have received a copy of the GNU General Public
19 1.1 macallan * License along with this library; if not, write to the Free
20 1.1 macallan * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 1.1 macallan * MA 02110-1301 USA
22 1.1 macallan *
23 1.1 macallan * Alternatively,
24 1.1 macallan *
25 1.1 macallan * b) Redistribution and use in source and binary forms, with or
26 1.1 macallan * without modification, are permitted provided that the following
27 1.1 macallan * conditions are met:
28 1.1 macallan *
29 1.1 macallan * 1. Redistributions of source code must retain the above
30 1.1 macallan * copyright notice, this list of conditions and the following
31 1.1 macallan * disclaimer.
32 1.1 macallan * 2. Redistributions in binary form must reproduce the above
33 1.1 macallan * copyright notice, this list of conditions and the following
34 1.1 macallan * disclaimer in the documentation and/or other materials
35 1.1 macallan * provided with the distribution.
36 1.1 macallan *
37 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38 1.1 macallan * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39 1.1 macallan * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 1.1 macallan * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 1.1 macallan * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42 1.1 macallan * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 1.1 macallan * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 1.1 macallan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 1.1 macallan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 1.1 macallan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 1.1 macallan * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49 1.1 macallan * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 1.1 macallan */
51 1.1 macallan #include "libfdt_env.h"
52 1.1 macallan
53 1.1 macallan #include <fdt.h>
54 1.1 macallan #include <libfdt.h>
55 1.1 macallan
56 1.1 macallan #include "libfdt_internal.h"
57 1.1 macallan
58 1.1 macallan static int _fdt_nodename_eq(const void *fdt, int offset,
59 1.1 macallan const char *s, int len)
60 1.1 macallan {
61 1.1 macallan const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
62 1.1 macallan
63 1.1 macallan if (! p)
64 1.1 macallan /* short match */
65 1.1 macallan return 0;
66 1.1 macallan
67 1.1 macallan if (memcmp(p, s, len) != 0)
68 1.1 macallan return 0;
69 1.1 macallan
70 1.1 macallan if (p[len] == '\0')
71 1.1 macallan return 1;
72 1.1 macallan else if (!memchr(s, '@', len) && (p[len] == '@'))
73 1.1 macallan return 1;
74 1.1 macallan else
75 1.1 macallan return 0;
76 1.1 macallan }
77 1.1 macallan
78 1.1 macallan const char *fdt_string(const void *fdt, int stroffset)
79 1.1 macallan {
80 1.1 macallan return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
81 1.1 macallan }
82 1.1 macallan
83 1.1 macallan static int _fdt_string_eq(const void *fdt, int stroffset,
84 1.1 macallan const char *s, int len)
85 1.1 macallan {
86 1.1 macallan const char *p = fdt_string(fdt, stroffset);
87 1.1 macallan
88 1.1 macallan return (strlen(p) == len) && (memcmp(p, s, len) == 0);
89 1.1 macallan }
90 1.1 macallan
91 1.1 macallan int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
92 1.1 macallan {
93 1.1 macallan FDT_CHECK_HEADER(fdt);
94 1.1 macallan *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
95 1.1 macallan *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
96 1.1 macallan return 0;
97 1.1 macallan }
98 1.1 macallan
99 1.1 macallan int fdt_num_mem_rsv(const void *fdt)
100 1.1 macallan {
101 1.1 macallan int i = 0;
102 1.1 macallan
103 1.1 macallan while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
104 1.1 macallan i++;
105 1.1 macallan return i;
106 1.1 macallan }
107 1.1 macallan
108 1.1 macallan static int _nextprop(const void *fdt, int offset)
109 1.1 macallan {
110 1.1 macallan uint32_t tag;
111 1.1 macallan int nextoffset;
112 1.1 macallan
113 1.1 macallan do {
114 1.1 macallan tag = fdt_next_tag(fdt, offset, &nextoffset);
115 1.1 macallan
116 1.1 macallan switch (tag) {
117 1.1 macallan case FDT_END:
118 1.1 macallan if (nextoffset >= 0)
119 1.1 macallan return -FDT_ERR_BADSTRUCTURE;
120 1.1 macallan else
121 1.1 macallan return nextoffset;
122 1.1 macallan
123 1.1 macallan case FDT_PROP:
124 1.1 macallan return offset;
125 1.1 macallan }
126 1.1 macallan offset = nextoffset;
127 1.1 macallan } while (tag == FDT_NOP);
128 1.1 macallan
129 1.1 macallan return -FDT_ERR_NOTFOUND;
130 1.1 macallan }
131 1.1 macallan
132 1.1 macallan int fdt_subnode_offset_namelen(const void *fdt, int offset,
133 1.1 macallan const char *name, int namelen)
134 1.1 macallan {
135 1.1 macallan int depth;
136 1.1 macallan
137 1.1 macallan FDT_CHECK_HEADER(fdt);
138 1.1 macallan
139 1.1 macallan for (depth = 0;
140 1.1 macallan (offset >= 0) && (depth >= 0);
141 1.1 macallan offset = fdt_next_node(fdt, offset, &depth))
142 1.1 macallan if ((depth == 1)
143 1.1 macallan && _fdt_nodename_eq(fdt, offset, name, namelen))
144 1.1 macallan return offset;
145 1.1 macallan
146 1.1 macallan if (depth < 0)
147 1.1 macallan return -FDT_ERR_NOTFOUND;
148 1.1 macallan return offset; /* error */
149 1.1 macallan }
150 1.1 macallan
151 1.1 macallan int fdt_subnode_offset(const void *fdt, int parentoffset,
152 1.1 macallan const char *name)
153 1.1 macallan {
154 1.1 macallan return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
155 1.1 macallan }
156 1.1 macallan
157 1.1 macallan int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
158 1.1 macallan {
159 1.1 macallan const char *end = path + namelen;
160 1.1 macallan const char *p = path;
161 1.1 macallan int offset = 0;
162 1.1 macallan
163 1.1 macallan FDT_CHECK_HEADER(fdt);
164 1.1 macallan
165 1.1 macallan /* see if we have an alias */
166 1.1 macallan if (*path != '/') {
167 1.1 macallan const char *q = memchr(path, '/', end - p);
168 1.1 macallan
169 1.1 macallan if (!q)
170 1.1 macallan q = end;
171 1.1 macallan
172 1.1 macallan p = fdt_get_alias_namelen(fdt, p, q - p);
173 1.1 macallan if (!p)
174 1.1 macallan return -FDT_ERR_BADPATH;
175 1.1 macallan offset = fdt_path_offset(fdt, p);
176 1.1 macallan
177 1.1 macallan p = q;
178 1.1 macallan }
179 1.1 macallan
180 1.1 macallan while (p < end) {
181 1.1 macallan const char *q;
182 1.1 macallan
183 1.1 macallan while (*p == '/') {
184 1.1 macallan p++;
185 1.1 macallan if (p == end)
186 1.1 macallan return offset;
187 1.1 macallan }
188 1.1 macallan q = memchr(p, '/', end - p);
189 1.1 macallan if (! q)
190 1.1 macallan q = end;
191 1.1 macallan
192 1.1 macallan offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
193 1.1 macallan if (offset < 0)
194 1.1 macallan return offset;
195 1.1 macallan
196 1.1 macallan p = q;
197 1.1 macallan }
198 1.1 macallan
199 1.1 macallan return offset;
200 1.1 macallan }
201 1.1 macallan
202 1.1 macallan int fdt_path_offset(const void *fdt, const char *path)
203 1.1 macallan {
204 1.1 macallan return fdt_path_offset_namelen(fdt, path, strlen(path));
205 1.1 macallan }
206 1.1 macallan
207 1.1 macallan const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
208 1.1 macallan {
209 1.1 macallan const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
210 1.1 macallan int err;
211 1.1 macallan
212 1.1 macallan if (((err = fdt_check_header(fdt)) != 0)
213 1.1 macallan || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
214 1.1 macallan goto fail;
215 1.1 macallan
216 1.1 macallan if (len)
217 1.1 macallan *len = strlen(nh->name);
218 1.1 macallan
219 1.1 macallan return nh->name;
220 1.1 macallan
221 1.1 macallan fail:
222 1.1 macallan if (len)
223 1.1 macallan *len = err;
224 1.1 macallan return NULL;
225 1.1 macallan }
226 1.1 macallan
227 1.1 macallan int fdt_first_property_offset(const void *fdt, int nodeoffset)
228 1.1 macallan {
229 1.1 macallan int offset;
230 1.1 macallan
231 1.1 macallan if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
232 1.1 macallan return offset;
233 1.1 macallan
234 1.1 macallan return _nextprop(fdt, offset);
235 1.1 macallan }
236 1.1 macallan
237 1.1 macallan int fdt_next_property_offset(const void *fdt, int offset)
238 1.1 macallan {
239 1.1 macallan if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
240 1.1 macallan return offset;
241 1.1 macallan
242 1.1 macallan return _nextprop(fdt, offset);
243 1.1 macallan }
244 1.1 macallan
245 1.1 macallan const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
246 1.1 macallan int offset,
247 1.1 macallan int *lenp)
248 1.1 macallan {
249 1.1 macallan int err;
250 1.1 macallan const struct fdt_property *prop;
251 1.1 macallan
252 1.1 macallan if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
253 1.1 macallan if (lenp)
254 1.1 macallan *lenp = err;
255 1.1 macallan return NULL;
256 1.1 macallan }
257 1.1 macallan
258 1.1 macallan prop = _fdt_offset_ptr(fdt, offset);
259 1.1 macallan
260 1.1 macallan if (lenp)
261 1.1 macallan *lenp = fdt32_to_cpu(prop->len);
262 1.1 macallan
263 1.1 macallan return prop;
264 1.1 macallan }
265 1.1 macallan
266 1.1 macallan const struct fdt_property *fdt_get_property_namelen(const void *fdt,
267 1.1 macallan int offset,
268 1.1 macallan const char *name,
269 1.1 macallan int namelen, int *lenp)
270 1.1 macallan {
271 1.1 macallan for (offset = fdt_first_property_offset(fdt, offset);
272 1.1 macallan (offset >= 0);
273 1.1 macallan (offset = fdt_next_property_offset(fdt, offset))) {
274 1.1 macallan const struct fdt_property *prop;
275 1.1 macallan
276 1.1 macallan if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
277 1.1 macallan offset = -FDT_ERR_INTERNAL;
278 1.1 macallan break;
279 1.1 macallan }
280 1.1 macallan if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
281 1.1 macallan name, namelen))
282 1.1 macallan return prop;
283 1.1 macallan }
284 1.1 macallan
285 1.1 macallan if (lenp)
286 1.1 macallan *lenp = offset;
287 1.1 macallan return NULL;
288 1.1 macallan }
289 1.1 macallan
290 1.1 macallan const struct fdt_property *fdt_get_property(const void *fdt,
291 1.1 macallan int nodeoffset,
292 1.1 macallan const char *name, int *lenp)
293 1.1 macallan {
294 1.1 macallan return fdt_get_property_namelen(fdt, nodeoffset, name,
295 1.1 macallan strlen(name), lenp);
296 1.1 macallan }
297 1.1 macallan
298 1.1 macallan const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
299 1.1 macallan const char *name, int namelen, int *lenp)
300 1.1 macallan {
301 1.1 macallan const struct fdt_property *prop;
302 1.1 macallan
303 1.1 macallan prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
304 1.1 macallan if (! prop)
305 1.1 macallan return NULL;
306 1.1 macallan
307 1.1 macallan return prop->data;
308 1.1 macallan }
309 1.1 macallan
310 1.1 macallan const void *fdt_getprop_by_offset(const void *fdt, int offset,
311 1.1 macallan const char **namep, int *lenp)
312 1.1 macallan {
313 1.1 macallan const struct fdt_property *prop;
314 1.1 macallan
315 1.1 macallan prop = fdt_get_property_by_offset(fdt, offset, lenp);
316 1.1 macallan if (!prop)
317 1.1 macallan return NULL;
318 1.1 macallan if (namep)
319 1.1 macallan *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
320 1.1 macallan return prop->data;
321 1.1 macallan }
322 1.1 macallan
323 1.1 macallan const void *fdt_getprop(const void *fdt, int nodeoffset,
324 1.1 macallan const char *name, int *lenp)
325 1.1 macallan {
326 1.1 macallan return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
327 1.1 macallan }
328 1.1 macallan
329 1.1 macallan uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
330 1.1 macallan {
331 1.1 macallan const fdt32_t *php;
332 1.1 macallan int len;
333 1.1 macallan
334 1.1 macallan /* FIXME: This is a bit sub-optimal, since we potentially scan
335 1.1 macallan * over all the properties twice. */
336 1.1 macallan php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
337 1.1 macallan if (!php || (len != sizeof(*php))) {
338 1.1 macallan php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
339 1.1 macallan if (!php || (len != sizeof(*php)))
340 1.1 macallan return 0;
341 1.1 macallan }
342 1.1 macallan
343 1.1 macallan return fdt32_to_cpu(*php);
344 1.1 macallan }
345 1.1 macallan
346 1.1 macallan const char *fdt_get_alias_namelen(const void *fdt,
347 1.1 macallan const char *name, int namelen)
348 1.1 macallan {
349 1.1 macallan int aliasoffset;
350 1.1 macallan
351 1.1 macallan aliasoffset = fdt_path_offset(fdt, "/aliases");
352 1.1 macallan if (aliasoffset < 0)
353 1.1 macallan return NULL;
354 1.1 macallan
355 1.1 macallan return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
356 1.1 macallan }
357 1.1 macallan
358 1.1 macallan const char *fdt_get_alias(const void *fdt, const char *name)
359 1.1 macallan {
360 1.1 macallan return fdt_get_alias_namelen(fdt, name, strlen(name));
361 1.1 macallan }
362 1.1 macallan
363 1.1 macallan int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
364 1.1 macallan {
365 1.1 macallan int pdepth = 0, p = 0;
366 1.1 macallan int offset, depth, namelen;
367 1.1 macallan const char *name;
368 1.1 macallan
369 1.1 macallan FDT_CHECK_HEADER(fdt);
370 1.1 macallan
371 1.1 macallan if (buflen < 2)
372 1.1 macallan return -FDT_ERR_NOSPACE;
373 1.1 macallan
374 1.1 macallan for (offset = 0, depth = 0;
375 1.1 macallan (offset >= 0) && (offset <= nodeoffset);
376 1.1 macallan offset = fdt_next_node(fdt, offset, &depth)) {
377 1.1 macallan while (pdepth > depth) {
378 1.1 macallan do {
379 1.1 macallan p--;
380 1.1 macallan } while (buf[p-1] != '/');
381 1.1 macallan pdepth--;
382 1.1 macallan }
383 1.1 macallan
384 1.1 macallan if (pdepth >= depth) {
385 1.1 macallan name = fdt_get_name(fdt, offset, &namelen);
386 1.1 macallan if (!name)
387 1.1 macallan return namelen;
388 1.1 macallan if ((p + namelen + 1) <= buflen) {
389 1.1 macallan memcpy(buf + p, name, namelen);
390 1.1 macallan p += namelen;
391 1.1 macallan buf[p++] = '/';
392 1.1 macallan pdepth++;
393 1.1 macallan }
394 1.1 macallan }
395 1.1 macallan
396 1.1 macallan if (offset == nodeoffset) {
397 1.1 macallan if (pdepth < (depth + 1))
398 1.1 macallan return -FDT_ERR_NOSPACE;
399 1.1 macallan
400 1.1 macallan if (p > 1) /* special case so that root path is "/", not "" */
401 1.1 macallan p--;
402 1.1 macallan buf[p] = '\0';
403 1.1 macallan return 0;
404 1.1 macallan }
405 1.1 macallan }
406 1.1 macallan
407 1.1 macallan if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
408 1.1 macallan return -FDT_ERR_BADOFFSET;
409 1.1 macallan else if (offset == -FDT_ERR_BADOFFSET)
410 1.1 macallan return -FDT_ERR_BADSTRUCTURE;
411 1.1 macallan
412 1.1 macallan return offset; /* error from fdt_next_node() */
413 1.1 macallan }
414 1.1 macallan
415 1.1 macallan int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
416 1.1 macallan int supernodedepth, int *nodedepth)
417 1.1 macallan {
418 1.1 macallan int offset, depth;
419 1.1 macallan int supernodeoffset = -FDT_ERR_INTERNAL;
420 1.1 macallan
421 1.1 macallan FDT_CHECK_HEADER(fdt);
422 1.1 macallan
423 1.1 macallan if (supernodedepth < 0)
424 1.1 macallan return -FDT_ERR_NOTFOUND;
425 1.1 macallan
426 1.1 macallan for (offset = 0, depth = 0;
427 1.1 macallan (offset >= 0) && (offset <= nodeoffset);
428 1.1 macallan offset = fdt_next_node(fdt, offset, &depth)) {
429 1.1 macallan if (depth == supernodedepth)
430 1.1 macallan supernodeoffset = offset;
431 1.1 macallan
432 1.1 macallan if (offset == nodeoffset) {
433 1.1 macallan if (nodedepth)
434 1.1 macallan *nodedepth = depth;
435 1.1 macallan
436 1.1 macallan if (supernodedepth > depth)
437 1.1 macallan return -FDT_ERR_NOTFOUND;
438 1.1 macallan else
439 1.1 macallan return supernodeoffset;
440 1.1 macallan }
441 1.1 macallan }
442 1.1 macallan
443 1.1 macallan if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
444 1.1 macallan return -FDT_ERR_BADOFFSET;
445 1.1 macallan else if (offset == -FDT_ERR_BADOFFSET)
446 1.1 macallan return -FDT_ERR_BADSTRUCTURE;
447 1.1 macallan
448 1.1 macallan return offset; /* error from fdt_next_node() */
449 1.1 macallan }
450 1.1 macallan
451 1.1 macallan int fdt_node_depth(const void *fdt, int nodeoffset)
452 1.1 macallan {
453 1.1 macallan int nodedepth;
454 1.1 macallan int err;
455 1.1 macallan
456 1.1 macallan err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
457 1.1 macallan if (err)
458 1.1 macallan return (err < 0) ? err : -FDT_ERR_INTERNAL;
459 1.1 macallan return nodedepth;
460 1.1 macallan }
461 1.1 macallan
462 1.1 macallan int fdt_parent_offset(const void *fdt, int nodeoffset)
463 1.1 macallan {
464 1.1 macallan int nodedepth = fdt_node_depth(fdt, nodeoffset);
465 1.1 macallan
466 1.1 macallan if (nodedepth < 0)
467 1.1 macallan return nodedepth;
468 1.1 macallan return fdt_supernode_atdepth_offset(fdt, nodeoffset,
469 1.1 macallan nodedepth - 1, NULL);
470 1.1 macallan }
471 1.1 macallan
472 1.1 macallan int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
473 1.1 macallan const char *propname,
474 1.1 macallan const void *propval, int proplen)
475 1.1 macallan {
476 1.1 macallan int offset;
477 1.1 macallan const void *val;
478 1.1 macallan int len;
479 1.1 macallan
480 1.1 macallan FDT_CHECK_HEADER(fdt);
481 1.1 macallan
482 1.1 macallan /* FIXME: The algorithm here is pretty horrible: we scan each
483 1.1 macallan * property of a node in fdt_getprop(), then if that didn't
484 1.1 macallan * find what we want, we scan over them again making our way
485 1.1 macallan * to the next node. Still it's the easiest to implement
486 1.1 macallan * approach; performance can come later. */
487 1.1 macallan for (offset = fdt_next_node(fdt, startoffset, NULL);
488 1.1 macallan offset >= 0;
489 1.1 macallan offset = fdt_next_node(fdt, offset, NULL)) {
490 1.1 macallan val = fdt_getprop(fdt, offset, propname, &len);
491 1.1 macallan if (val && (len == proplen)
492 1.1 macallan && (memcmp(val, propval, len) == 0))
493 1.1 macallan return offset;
494 1.1 macallan }
495 1.1 macallan
496 1.1 macallan return offset; /* error from fdt_next_node() */
497 1.1 macallan }
498 1.1 macallan
499 1.1 macallan int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
500 1.1 macallan {
501 1.1 macallan int offset;
502 1.1 macallan
503 1.1 macallan if ((phandle == 0) || (phandle == -1))
504 1.1 macallan return -FDT_ERR_BADPHANDLE;
505 1.1 macallan
506 1.1 macallan FDT_CHECK_HEADER(fdt);
507 1.1 macallan
508 1.1 macallan /* FIXME: The algorithm here is pretty horrible: we
509 1.1 macallan * potentially scan each property of a node in
510 1.1 macallan * fdt_get_phandle(), then if that didn't find what
511 1.1 macallan * we want, we scan over them again making our way to the next
512 1.1 macallan * node. Still it's the easiest to implement approach;
513 1.1 macallan * performance can come later. */
514 1.1 macallan for (offset = fdt_next_node(fdt, -1, NULL);
515 1.1 macallan offset >= 0;
516 1.1 macallan offset = fdt_next_node(fdt, offset, NULL)) {
517 1.1 macallan if (fdt_get_phandle(fdt, offset) == phandle)
518 1.1 macallan return offset;
519 1.1 macallan }
520 1.1 macallan
521 1.1 macallan return offset; /* error from fdt_next_node() */
522 1.1 macallan }
523 1.1 macallan
524 1.1 macallan int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
525 1.1 macallan {
526 1.1 macallan int len = strlen(str);
527 1.1 macallan const char *p;
528 1.1 macallan
529 1.1 macallan while (listlen >= len) {
530 1.1 macallan if (memcmp(str, strlist, len+1) == 0)
531 1.1 macallan return 1;
532 1.1 macallan p = memchr(strlist, '\0', listlen);
533 1.1 macallan if (!p)
534 1.1 macallan return 0; /* malformed strlist.. */
535 1.1 macallan listlen -= (p-strlist) + 1;
536 1.1 macallan strlist = p + 1;
537 1.1 macallan }
538 1.1 macallan return 0;
539 1.1 macallan }
540 1.1 macallan
541 1.1 macallan int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
542 1.1 macallan {
543 1.1 macallan const char *list, *end;
544 1.1 macallan int length, count = 0;
545 1.1 macallan
546 1.1 macallan list = fdt_getprop(fdt, nodeoffset, property, &length);
547 1.1 macallan if (!list)
548 1.1 macallan return -length;
549 1.1 macallan
550 1.1 macallan end = list + length;
551 1.1 macallan
552 1.1 macallan while (list < end) {
553 1.1 macallan length = strnlen(list, end - list) + 1;
554 1.1 macallan
555 1.1 macallan /* Abort if the last string isn't properly NUL-terminated. */
556 1.1 macallan if (list + length > end)
557 1.1 macallan return -FDT_ERR_BADVALUE;
558 1.1 macallan
559 1.1 macallan list += length;
560 1.1 macallan count++;
561 1.1 macallan }
562 1.1 macallan
563 1.1 macallan return count;
564 1.1 macallan }
565 1.1 macallan
566 1.1 macallan int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
567 1.1 macallan const char *string)
568 1.1 macallan {
569 1.1 macallan int length, len, idx = 0;
570 1.1 macallan const char *list, *end;
571 1.1 macallan
572 1.1 macallan list = fdt_getprop(fdt, nodeoffset, property, &length);
573 1.1 macallan if (!list)
574 1.1 macallan return -length;
575 1.1 macallan
576 1.1 macallan len = strlen(string) + 1;
577 1.1 macallan end = list + length;
578 1.1 macallan
579 1.1 macallan while (list < end) {
580 1.1 macallan length = strnlen(list, end - list) + 1;
581 1.1 macallan
582 1.1 macallan /* Abort if the last string isn't properly NUL-terminated. */
583 1.1 macallan if (list + length > end)
584 1.1 macallan return -FDT_ERR_BADVALUE;
585 1.1 macallan
586 1.1 macallan if (length == len && memcmp(list, string, length) == 0)
587 1.1 macallan return idx;
588 1.1 macallan
589 1.1 macallan list += length;
590 1.1 macallan idx++;
591 1.1 macallan }
592 1.1 macallan
593 1.1 macallan return -FDT_ERR_NOTFOUND;
594 1.1 macallan }
595 1.1 macallan
596 1.1 macallan const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
597 1.1 macallan const char *property, int idx,
598 1.1 macallan int *lenp)
599 1.1 macallan {
600 1.1 macallan const char *list, *end;
601 1.1 macallan int length;
602 1.1 macallan
603 1.1 macallan list = fdt_getprop(fdt, nodeoffset, property, &length);
604 1.1 macallan if (!list) {
605 1.1 macallan if (lenp)
606 1.1 macallan *lenp = length;
607 1.1 macallan
608 1.1 macallan return NULL;
609 1.1 macallan }
610 1.1 macallan
611 1.1 macallan end = list + length;
612 1.1 macallan
613 1.1 macallan while (list < end) {
614 1.1 macallan length = strnlen(list, end - list) + 1;
615 1.1 macallan
616 1.1 macallan /* Abort if the last string isn't properly NUL-terminated. */
617 1.1 macallan if (list + length > end) {
618 1.1 macallan if (lenp)
619 1.1 macallan *lenp = -FDT_ERR_BADVALUE;
620 1.1 macallan
621 1.1 macallan return NULL;
622 1.1 macallan }
623 1.1 macallan
624 1.1 macallan if (idx == 0) {
625 1.1 macallan if (lenp)
626 1.1 macallan *lenp = length - 1;
627 1.1 macallan
628 1.1 macallan return list;
629 1.1 macallan }
630 1.1 macallan
631 1.1 macallan list += length;
632 1.1 macallan idx--;
633 1.1 macallan }
634 1.1 macallan
635 1.1 macallan if (lenp)
636 1.1 macallan *lenp = -FDT_ERR_NOTFOUND;
637 1.1 macallan
638 1.1 macallan return NULL;
639 1.1 macallan }
640 1.1 macallan
641 1.1 macallan int fdt_node_check_compatible(const void *fdt, int nodeoffset,
642 1.1 macallan const char *compatible)
643 1.1 macallan {
644 1.1 macallan const void *prop;
645 1.1 macallan int len;
646 1.1 macallan
647 1.1 macallan prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
648 1.1 macallan if (!prop)
649 1.1 macallan return len;
650 1.1 macallan if (fdt_stringlist_contains(prop, len, compatible))
651 1.1 macallan return 0;
652 1.1 macallan else
653 1.1 macallan return 1;
654 1.1 macallan }
655 1.1 macallan
656 1.1 macallan int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
657 1.1 macallan const char *compatible)
658 1.1 macallan {
659 1.1 macallan int offset, err;
660 1.1 macallan
661 1.1 macallan FDT_CHECK_HEADER(fdt);
662 1.1 macallan
663 1.1 macallan /* FIXME: The algorithm here is pretty horrible: we scan each
664 1.1 macallan * property of a node in fdt_node_check_compatible(), then if
665 1.1 macallan * that didn't find what we want, we scan over them again
666 1.1 macallan * making our way to the next node. Still it's the easiest to
667 1.1 macallan * implement approach; performance can come later. */
668 1.1 macallan for (offset = fdt_next_node(fdt, startoffset, NULL);
669 1.1 macallan offset >= 0;
670 1.1 macallan offset = fdt_next_node(fdt, offset, NULL)) {
671 1.1 macallan err = fdt_node_check_compatible(fdt, offset, compatible);
672 1.1 macallan if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
673 1.1 macallan return err;
674 1.1 macallan else if (err == 0)
675 1.1 macallan return offset;
676 1.1 macallan }
677 1.1 macallan
678 1.1 macallan return offset; /* error from fdt_next_node() */
679 1.1 macallan }
680