PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/libfdt/fdt_ro.c

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