PageRenderTime 42ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/dtc/libfdt/fdt_ro.c

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