fdt_ro.c 11 KB

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