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