fdt_ro.c 12 KB

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