PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/dtc/libfdt/fdt_ro.c

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