fdt_ro.c 13 KB

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