fdt_ro.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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, write to the Free
  20. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  21. * MA 02110-1301 USA
  22. *
  23. * Alternatively,
  24. *
  25. * b) Redistribution and use in source and binary forms, with or
  26. * without modification, are permitted provided that the following
  27. * conditions are met:
  28. *
  29. * 1. Redistributions of source code must retain the above
  30. * copyright notice, this list of conditions and the following
  31. * disclaimer.
  32. * 2. Redistributions in binary form must reproduce the above
  33. * copyright notice, this list of conditions and the following
  34. * disclaimer in the documentation and/or other materials
  35. * provided with the distribution.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  38. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  39. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  40. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  42. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  48. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  49. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50. */
  51. #include "libfdt_env.h"
  52. #ifndef USE_HOSTCC
  53. #include <fdt.h>
  54. #include <libfdt.h>
  55. #else
  56. #include "fdt_host.h"
  57. #endif
  58. #include "libfdt_internal.h"
  59. static int nodename_eq(const void *fdt, int offset,
  60. const char *s, int len)
  61. {
  62. const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
  63. if (! p)
  64. /* short match */
  65. return 0;
  66. if (memcmp(p, s, len) != 0)
  67. return 0;
  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. const char *fdt_string(const void *fdt, int stroffset)
  76. {
  77. return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
  78. }
  79. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
  80. {
  81. CHECK_HEADER(fdt);
  82. *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
  83. *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
  84. return 0;
  85. }
  86. int fdt_num_mem_rsv(const void *fdt)
  87. {
  88. int i = 0;
  89. while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
  90. i++;
  91. return i;
  92. }
  93. int fdt_subnode_offset_namelen(const void *fdt, int offset,
  94. const char *name, int namelen)
  95. {
  96. int depth;
  97. CHECK_HEADER(fdt);
  98. for (depth = 0;
  99. offset >= 0;
  100. offset = fdt_next_node(fdt, offset, &depth)) {
  101. if (depth < 0)
  102. return -FDT_ERR_NOTFOUND;
  103. else if ((depth == 1)
  104. && nodename_eq(fdt, offset, name, namelen))
  105. return offset;
  106. }
  107. return offset; /* error */
  108. }
  109. int fdt_subnode_offset(const void *fdt, int parentoffset,
  110. const char *name)
  111. {
  112. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  113. }
  114. int fdt_path_offset(const void *fdt, const char *path)
  115. {
  116. const char *end = path + strlen(path);
  117. const char *p = path;
  118. int offset = 0;
  119. CHECK_HEADER(fdt);
  120. if (*path != '/')
  121. return -FDT_ERR_BADPATH;
  122. while (*p) {
  123. const char *q;
  124. while (*p == '/')
  125. p++;
  126. if (! *p)
  127. return offset;
  128. q = strchr(p, '/');
  129. if (! q)
  130. q = end;
  131. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  132. if (offset < 0)
  133. return offset;
  134. p = q;
  135. }
  136. return offset;
  137. }
  138. const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
  139. {
  140. const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
  141. int err;
  142. if (((err = fdt_check_header(fdt)) != 0)
  143. || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
  144. goto fail;
  145. if (len)
  146. *len = strlen(nh->name);
  147. return nh->name;
  148. fail:
  149. if (len)
  150. *len = err;
  151. return NULL;
  152. }
  153. const struct fdt_property *fdt_get_property(const void *fdt,
  154. int nodeoffset,
  155. const char *name, int *lenp)
  156. {
  157. uint32_t tag;
  158. const struct fdt_property *prop;
  159. int namestroff;
  160. int offset, nextoffset;
  161. int err;
  162. if (((err = fdt_check_header(fdt)) != 0)
  163. || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
  164. goto fail;
  165. nextoffset = err;
  166. do {
  167. offset = nextoffset;
  168. tag = fdt_next_tag(fdt, offset, &nextoffset);
  169. switch (tag) {
  170. case FDT_END:
  171. err = -FDT_ERR_TRUNCATED;
  172. goto fail;
  173. case FDT_BEGIN_NODE:
  174. case FDT_END_NODE:
  175. case FDT_NOP:
  176. break;
  177. case FDT_PROP:
  178. err = -FDT_ERR_BADSTRUCTURE;
  179. prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
  180. if (! prop)
  181. goto fail;
  182. namestroff = fdt32_to_cpu(prop->nameoff);
  183. if (streq(fdt_string(fdt, namestroff), name)) {
  184. /* Found it! */
  185. int len = fdt32_to_cpu(prop->len);
  186. prop = fdt_offset_ptr(fdt, offset,
  187. sizeof(*prop)+len);
  188. if (! prop)
  189. goto fail;
  190. if (lenp)
  191. *lenp = len;
  192. return prop;
  193. }
  194. break;
  195. default:
  196. err = -FDT_ERR_BADSTRUCTURE;
  197. goto fail;
  198. }
  199. } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
  200. err = -FDT_ERR_NOTFOUND;
  201. fail:
  202. if (lenp)
  203. *lenp = err;
  204. return NULL;
  205. }
  206. const void *fdt_getprop(const void *fdt, int nodeoffset,
  207. const char *name, int *lenp)
  208. {
  209. const struct fdt_property *prop;
  210. prop = fdt_get_property(fdt, nodeoffset, name, lenp);
  211. if (! prop)
  212. return NULL;
  213. return prop->data;
  214. }
  215. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
  216. {
  217. const uint32_t *php;
  218. int len;
  219. php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
  220. if (!php || (len != sizeof(*php)))
  221. return 0;
  222. return fdt32_to_cpu(*php);
  223. }
  224. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
  225. {
  226. int pdepth = 0, p = 0;
  227. int offset, depth, namelen;
  228. const char *name;
  229. CHECK_HEADER(fdt);
  230. if (buflen < 2)
  231. return -FDT_ERR_NOSPACE;
  232. for (offset = 0, depth = 0;
  233. (offset >= 0) && (offset <= nodeoffset);
  234. offset = fdt_next_node(fdt, offset, &depth)) {
  235. if (pdepth < depth)
  236. continue; /* overflowed buffer */
  237. while (pdepth > depth) {
  238. do {
  239. p--;
  240. } while (buf[p-1] != '/');
  241. pdepth--;
  242. }
  243. name = fdt_get_name(fdt, offset, &namelen);
  244. if (!name)
  245. return namelen;
  246. if ((p + namelen + 1) <= buflen) {
  247. memcpy(buf + p, name, namelen);
  248. p += namelen;
  249. buf[p++] = '/';
  250. pdepth++;
  251. }
  252. if (offset == nodeoffset) {
  253. if (pdepth < (depth + 1))
  254. return -FDT_ERR_NOSPACE;
  255. if (p > 1) /* special case so that root path is "/", not "" */
  256. p--;
  257. buf[p] = '\0';
  258. return p;
  259. }
  260. }
  261. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  262. return -FDT_ERR_BADOFFSET;
  263. else if (offset == -FDT_ERR_BADOFFSET)
  264. return -FDT_ERR_BADSTRUCTURE;
  265. return offset; /* error from fdt_next_node() */
  266. }
  267. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  268. int supernodedepth, int *nodedepth)
  269. {
  270. int offset, depth;
  271. int supernodeoffset = -FDT_ERR_INTERNAL;
  272. CHECK_HEADER(fdt);
  273. if (supernodedepth < 0)
  274. return -FDT_ERR_NOTFOUND;
  275. for (offset = 0, depth = 0;
  276. (offset >= 0) && (offset <= nodeoffset);
  277. offset = fdt_next_node(fdt, offset, &depth)) {
  278. if (depth == supernodedepth)
  279. supernodeoffset = offset;
  280. if (offset == nodeoffset) {
  281. if (nodedepth)
  282. *nodedepth = depth;
  283. if (supernodedepth > depth)
  284. return -FDT_ERR_NOTFOUND;
  285. else
  286. return supernodeoffset;
  287. }
  288. }
  289. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  290. return -FDT_ERR_BADOFFSET;
  291. else if (offset == -FDT_ERR_BADOFFSET)
  292. return -FDT_ERR_BADSTRUCTURE;
  293. return offset; /* error from fdt_next_node() */
  294. }
  295. int fdt_node_depth(const void *fdt, int nodeoffset)
  296. {
  297. int nodedepth;
  298. int err;
  299. err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
  300. if (err)
  301. return (err < 0) ? err : -FDT_ERR_INTERNAL;
  302. return nodedepth;
  303. }
  304. int fdt_parent_offset(const void *fdt, int nodeoffset)
  305. {
  306. int nodedepth = fdt_node_depth(fdt, nodeoffset);
  307. if (nodedepth < 0)
  308. return nodedepth;
  309. return fdt_supernode_atdepth_offset(fdt, nodeoffset,
  310. nodedepth - 1, NULL);
  311. }
  312. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  313. const char *propname,
  314. const void *propval, int proplen)
  315. {
  316. int offset;
  317. const void *val;
  318. int len;
  319. CHECK_HEADER(fdt);
  320. /* FIXME: The algorithm here is pretty horrible: we scan each
  321. * property of a node in fdt_getprop(), then if that didn't
  322. * find what we want, we scan over them again making our way
  323. * to the next node. Still it's the easiest to implement
  324. * approach; performance can come later. */
  325. for (offset = fdt_next_node(fdt, startoffset, NULL);
  326. offset >= 0;
  327. offset = fdt_next_node(fdt, offset, NULL)) {
  328. val = fdt_getprop(fdt, offset, propname, &len);
  329. if (val && (len == proplen)
  330. && (memcmp(val, propval, len) == 0))
  331. return offset;
  332. }
  333. return offset; /* error from fdt_next_node() */
  334. }
  335. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
  336. {
  337. if ((phandle == 0) || (phandle == -1))
  338. return -FDT_ERR_BADPHANDLE;
  339. phandle = cpu_to_fdt32(phandle);
  340. return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle",
  341. &phandle, sizeof(phandle));
  342. }
  343. int _stringlist_contains(const void *strlist, int listlen, const char *str)
  344. {
  345. int len = strlen(str);
  346. const void *p;
  347. while (listlen >= len) {
  348. if (memcmp(str, strlist, len+1) == 0)
  349. return 1;
  350. p = memchr(strlist, '\0', listlen);
  351. if (!p)
  352. return 0; /* malformed strlist.. */
  353. listlen -= (p-strlist) + 1;
  354. strlist = p + 1;
  355. }
  356. return 0;
  357. }
  358. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  359. const char *compatible)
  360. {
  361. const void *prop;
  362. int len;
  363. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  364. if (!prop)
  365. return len;
  366. if (_stringlist_contains(prop, len, compatible))
  367. return 0;
  368. else
  369. return 1;
  370. }
  371. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  372. const char *compatible)
  373. {
  374. int offset, err;
  375. CHECK_HEADER(fdt);
  376. /* FIXME: The algorithm here is pretty horrible: we scan each
  377. * property of a node in fdt_node_check_compatible(), then if
  378. * that didn't find what we want, we scan over them again
  379. * making our way to the next node. Still it's the easiest to
  380. * implement approach; performance can come later. */
  381. for (offset = fdt_next_node(fdt, startoffset, NULL);
  382. offset >= 0;
  383. offset = fdt_next_node(fdt, offset, NULL)) {
  384. err = fdt_node_check_compatible(fdt, offset, compatible);
  385. if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
  386. return err;
  387. else if (err == 0)
  388. return offset;
  389. }
  390. return offset; /* error from fdt_next_node() */
  391. }