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