fdt_ro.c 12 KB

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