fdt_ro.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 _fdt_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 (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
  78. }
  79. static int _fdt_string_eq(const void *fdt, int stroffset,
  80. const char *s, int len)
  81. {
  82. const char *p = fdt_string(fdt, stroffset);
  83. return (strlen(p) == len) && (memcmp(p, s, len) == 0);
  84. }
  85. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
  86. {
  87. FDT_CHECK_HEADER(fdt);
  88. *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
  89. *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
  90. return 0;
  91. }
  92. int fdt_num_mem_rsv(const void *fdt)
  93. {
  94. int i = 0;
  95. while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
  96. i++;
  97. return i;
  98. }
  99. int fdt_subnode_offset_namelen(const void *fdt, int offset,
  100. const char *name, int namelen)
  101. {
  102. int depth;
  103. FDT_CHECK_HEADER(fdt);
  104. for (depth = 0;
  105. (offset >= 0) && (depth >= 0);
  106. offset = fdt_next_node(fdt, offset, &depth))
  107. if ((depth == 1)
  108. && _fdt_nodename_eq(fdt, offset, name, namelen))
  109. return offset;
  110. if (depth < 0)
  111. return -FDT_ERR_NOTFOUND;
  112. return offset; /* error */
  113. }
  114. int fdt_subnode_offset(const void *fdt, int parentoffset,
  115. const char *name)
  116. {
  117. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  118. }
  119. int fdt_path_offset(const void *fdt, const char *path)
  120. {
  121. const char *end = path + strlen(path);
  122. const char *p = path;
  123. int offset = 0;
  124. FDT_CHECK_HEADER(fdt);
  125. /* see if we have an alias */
  126. if (*path != '/') {
  127. const char *q = strchr(path, '/');
  128. if (!q)
  129. q = end;
  130. p = fdt_get_alias_namelen(fdt, p, q - p);
  131. if (!p)
  132. return -FDT_ERR_BADPATH;
  133. offset = fdt_path_offset(fdt, p);
  134. p = q;
  135. }
  136. while (*p) {
  137. const char *q;
  138. while (*p == '/')
  139. p++;
  140. if (! *p)
  141. return offset;
  142. q = strchr(p, '/');
  143. if (! q)
  144. q = end;
  145. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  146. if (offset < 0)
  147. return offset;
  148. p = q;
  149. }
  150. return offset;
  151. }
  152. const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
  153. {
  154. const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
  155. int err;
  156. if (((err = fdt_check_header(fdt)) != 0)
  157. || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
  158. goto fail;
  159. if (len)
  160. *len = strlen(nh->name);
  161. return nh->name;
  162. fail:
  163. if (len)
  164. *len = err;
  165. return NULL;
  166. }
  167. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  168. int nodeoffset,
  169. const char *name,
  170. int namelen, int *lenp)
  171. {
  172. uint32_t tag;
  173. const struct fdt_property *prop;
  174. int offset, nextoffset;
  175. int err;
  176. if (((err = fdt_check_header(fdt)) != 0)
  177. || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
  178. goto fail;
  179. nextoffset = err;
  180. do {
  181. offset = nextoffset;
  182. tag = fdt_next_tag(fdt, offset, &nextoffset);
  183. switch (tag) {
  184. case FDT_END:
  185. if (nextoffset < 0)
  186. err = nextoffset;
  187. else
  188. /* FDT_END tag with unclosed nodes */
  189. err = -FDT_ERR_BADSTRUCTURE;
  190. goto fail;
  191. case FDT_PROP:
  192. prop = _fdt_offset_ptr(fdt, offset);
  193. if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
  194. name, namelen)) {
  195. /* Found it! */
  196. if (lenp)
  197. *lenp = fdt32_to_cpu(prop->len);
  198. return prop;
  199. }
  200. break;
  201. }
  202. } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
  203. err = -FDT_ERR_NOTFOUND;
  204. fail:
  205. if (lenp)
  206. *lenp = err;
  207. return NULL;
  208. }
  209. const struct fdt_property *fdt_get_property(const void *fdt,
  210. int nodeoffset,
  211. const char *name, int *lenp)
  212. {
  213. return fdt_get_property_namelen(fdt, nodeoffset, name,
  214. strlen(name), lenp);
  215. }
  216. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  217. const char *name, int namelen, int *lenp)
  218. {
  219. const struct fdt_property *prop;
  220. prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
  221. if (! prop)
  222. return NULL;
  223. return prop->data;
  224. }
  225. const void *fdt_getprop(const void *fdt, int nodeoffset,
  226. const char *name, int *lenp)
  227. {
  228. return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
  229. }
  230. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
  231. {
  232. const uint32_t *php;
  233. int len;
  234. /* FIXME: This is a bit sub-optimal, since we potentially scan
  235. * over all the properties twice. */
  236. php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
  237. if (!php || (len != sizeof(*php))) {
  238. php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
  239. if (!php || (len != sizeof(*php)))
  240. return 0;
  241. }
  242. return fdt32_to_cpu(*php);
  243. }
  244. const char *fdt_get_alias_namelen(const void *fdt,
  245. const char *name, int namelen)
  246. {
  247. int aliasoffset;
  248. aliasoffset = fdt_path_offset(fdt, "/aliases");
  249. if (aliasoffset < 0)
  250. return NULL;
  251. return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
  252. }
  253. const char *fdt_get_alias(const void *fdt, const char *name)
  254. {
  255. return fdt_get_alias_namelen(fdt, name, strlen(name));
  256. }
  257. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
  258. {
  259. int pdepth = 0, p = 0;
  260. int offset, depth, namelen;
  261. const char *name;
  262. FDT_CHECK_HEADER(fdt);
  263. if (buflen < 2)
  264. return -FDT_ERR_NOSPACE;
  265. for (offset = 0, depth = 0;
  266. (offset >= 0) && (offset <= nodeoffset);
  267. offset = fdt_next_node(fdt, offset, &depth)) {
  268. while (pdepth > depth) {
  269. do {
  270. p--;
  271. } while (buf[p-1] != '/');
  272. pdepth--;
  273. }
  274. if (pdepth >= depth) {
  275. name = fdt_get_name(fdt, offset, &namelen);
  276. if (!name)
  277. return namelen;
  278. if ((p + namelen + 1) <= buflen) {
  279. memcpy(buf + p, name, namelen);
  280. p += namelen;
  281. buf[p++] = '/';
  282. pdepth++;
  283. }
  284. }
  285. if (offset == nodeoffset) {
  286. if (pdepth < (depth + 1))
  287. return -FDT_ERR_NOSPACE;
  288. if (p > 1) /* special case so that root path is "/", not "" */
  289. p--;
  290. buf[p] = '\0';
  291. return 0;
  292. }
  293. }
  294. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  295. return -FDT_ERR_BADOFFSET;
  296. else if (offset == -FDT_ERR_BADOFFSET)
  297. return -FDT_ERR_BADSTRUCTURE;
  298. return offset; /* error from fdt_next_node() */
  299. }
  300. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  301. int supernodedepth, int *nodedepth)
  302. {
  303. int offset, depth;
  304. int supernodeoffset = -FDT_ERR_INTERNAL;
  305. FDT_CHECK_HEADER(fdt);
  306. if (supernodedepth < 0)
  307. return -FDT_ERR_NOTFOUND;
  308. for (offset = 0, depth = 0;
  309. (offset >= 0) && (offset <= nodeoffset);
  310. offset = fdt_next_node(fdt, offset, &depth)) {
  311. if (depth == supernodedepth)
  312. supernodeoffset = offset;
  313. if (offset == nodeoffset) {
  314. if (nodedepth)
  315. *nodedepth = depth;
  316. if (supernodedepth > depth)
  317. return -FDT_ERR_NOTFOUND;
  318. else
  319. return supernodeoffset;
  320. }
  321. }
  322. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  323. return -FDT_ERR_BADOFFSET;
  324. else if (offset == -FDT_ERR_BADOFFSET)
  325. return -FDT_ERR_BADSTRUCTURE;
  326. return offset; /* error from fdt_next_node() */
  327. }
  328. int fdt_node_depth(const void *fdt, int nodeoffset)
  329. {
  330. int nodedepth;
  331. int err;
  332. err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
  333. if (err)
  334. return (err < 0) ? err : -FDT_ERR_INTERNAL;
  335. return nodedepth;
  336. }
  337. int fdt_parent_offset(const void *fdt, int nodeoffset)
  338. {
  339. int nodedepth = fdt_node_depth(fdt, nodeoffset);
  340. if (nodedepth < 0)
  341. return nodedepth;
  342. return fdt_supernode_atdepth_offset(fdt, nodeoffset,
  343. nodedepth - 1, NULL);
  344. }
  345. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  346. const char *propname,
  347. const void *propval, int proplen)
  348. {
  349. int offset;
  350. const void *val;
  351. int len;
  352. FDT_CHECK_HEADER(fdt);
  353. /* FIXME: The algorithm here is pretty horrible: we scan each
  354. * property of a node in fdt_getprop(), then if that didn't
  355. * find what we want, we scan over them again making our way
  356. * to the next node. Still it's the easiest to implement
  357. * approach; performance can come later. */
  358. for (offset = fdt_next_node(fdt, startoffset, NULL);
  359. offset >= 0;
  360. offset = fdt_next_node(fdt, offset, NULL)) {
  361. val = fdt_getprop(fdt, offset, propname, &len);
  362. if (val && (len == proplen)
  363. && (memcmp(val, propval, len) == 0))
  364. return offset;
  365. }
  366. return offset; /* error from fdt_next_node() */
  367. }
  368. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
  369. {
  370. int offset;
  371. if ((phandle == 0) || (phandle == -1))
  372. return -FDT_ERR_BADPHANDLE;
  373. FDT_CHECK_HEADER(fdt);
  374. /* FIXME: The algorithm here is pretty horrible: we
  375. * potentially scan each property of a node in
  376. * fdt_get_phandle(), then if that didn't find what
  377. * we want, we scan over them again making our way to the next
  378. * node. Still it's the easiest to implement approach;
  379. * performance can come later. */
  380. for (offset = fdt_next_node(fdt, -1, NULL);
  381. offset >= 0;
  382. offset = fdt_next_node(fdt, offset, NULL)) {
  383. if (fdt_get_phandle(fdt, offset) == phandle)
  384. return offset;
  385. }
  386. return offset; /* error from fdt_next_node() */
  387. }
  388. static int _fdt_stringlist_contains(const char *strlist, int listlen,
  389. const char *str)
  390. {
  391. int len = strlen(str);
  392. const char *p;
  393. while (listlen >= len) {
  394. if (memcmp(str, strlist, len+1) == 0)
  395. return 1;
  396. p = memchr(strlist, '\0', listlen);
  397. if (!p)
  398. return 0; /* malformed strlist.. */
  399. listlen -= (p-strlist) + 1;
  400. strlist = p + 1;
  401. }
  402. return 0;
  403. }
  404. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  405. const char *compatible)
  406. {
  407. const void *prop;
  408. int len;
  409. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  410. if (!prop)
  411. return len;
  412. if (_fdt_stringlist_contains(prop, len, compatible))
  413. return 0;
  414. else
  415. return 1;
  416. }
  417. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  418. const char *compatible)
  419. {
  420. int offset, err;
  421. FDT_CHECK_HEADER(fdt);
  422. /* FIXME: The algorithm here is pretty horrible: we scan each
  423. * property of a node in fdt_node_check_compatible(), then if
  424. * that didn't find what we want, we scan over them again
  425. * making our way to the next node. Still it's the easiest to
  426. * implement approach; performance can come later. */
  427. for (offset = fdt_next_node(fdt, startoffset, NULL);
  428. offset >= 0;
  429. offset = fdt_next_node(fdt, offset, NULL)) {
  430. err = fdt_node_check_compatible(fdt, offset, compatible);
  431. if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
  432. return err;
  433. else if (err == 0)
  434. return offset;
  435. }
  436. return offset; /* error from fdt_next_node() */
  437. }