PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/libfdt/fdt_ro.c

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