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

/dtc/libfdt/fdt_ro.c

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