fdt_ro.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. #define CHECK_HEADER(fdt) \
  60. { \
  61. int err; \
  62. if ((err = fdt_check_header(fdt)) != 0) \
  63. return err; \
  64. }
  65. static int nodename_eq(const void *fdt, int offset,
  66. const char *s, int len)
  67. {
  68. const char *p = fdt_offset_ptr(fdt, offset, len+1);
  69. if (! p)
  70. /* short match */
  71. return 0;
  72. if (memcmp(p, s, len) != 0)
  73. return 0;
  74. if (p[len] == '\0')
  75. return 1;
  76. else if (!memchr(s, '@', len) && (p[len] == '@'))
  77. return 1;
  78. else
  79. return 0;
  80. }
  81. const char *fdt_string(const void *fdt, int stroffset)
  82. {
  83. return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
  84. }
  85. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
  86. {
  87. 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. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  100. const char *name, int namelen)
  101. {
  102. int level = 0;
  103. uint32_t tag;
  104. int offset, nextoffset;
  105. CHECK_HEADER(fdt);
  106. tag = fdt_next_tag(fdt, parentoffset, &nextoffset);
  107. if (tag != FDT_BEGIN_NODE)
  108. return -FDT_ERR_BADOFFSET;
  109. do {
  110. offset = nextoffset;
  111. tag = fdt_next_tag(fdt, offset, &nextoffset);
  112. switch (tag) {
  113. case FDT_END:
  114. return -FDT_ERR_TRUNCATED;
  115. case FDT_BEGIN_NODE:
  116. level++;
  117. if (level != 1)
  118. continue;
  119. if (nodename_eq(fdt, offset+FDT_TAGSIZE, name, namelen))
  120. /* Found it! */
  121. return offset;
  122. break;
  123. case FDT_END_NODE:
  124. level--;
  125. break;
  126. case FDT_PROP:
  127. case FDT_NOP:
  128. break;
  129. default:
  130. return -FDT_ERR_BADSTRUCTURE;
  131. }
  132. } while (level >= 0);
  133. return -FDT_ERR_NOTFOUND;
  134. }
  135. int fdt_subnode_offset(const void *fdt, int parentoffset,
  136. const char *name)
  137. {
  138. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  139. }
  140. int fdt_path_offset(const void *fdt, const char *path)
  141. {
  142. const char *end = path + strlen(path);
  143. const char *p = path;
  144. int offset = 0;
  145. CHECK_HEADER(fdt);
  146. if (*path != '/')
  147. return -FDT_ERR_BADPATH;
  148. while (*p) {
  149. const char *q;
  150. while (*p == '/')
  151. p++;
  152. if (! *p)
  153. return offset;
  154. q = strchr(p, '/');
  155. if (! q)
  156. q = end;
  157. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  158. if (offset < 0)
  159. return offset;
  160. p = q;
  161. }
  162. return offset;
  163. }
  164. const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
  165. {
  166. const struct fdt_node_header *nh;
  167. int err;
  168. if ((err = fdt_check_header(fdt)) != 0)
  169. goto fail;
  170. err = -FDT_ERR_BADOFFSET;
  171. nh = fdt_offset_ptr(fdt, nodeoffset, sizeof(*nh));
  172. if (!nh || (fdt32_to_cpu(nh->tag) != FDT_BEGIN_NODE))
  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. const struct fdt_property *fdt_get_property(const void *fdt,
  183. int nodeoffset,
  184. const char *name, int *lenp)
  185. {
  186. uint32_t tag;
  187. const struct fdt_property *prop;
  188. int namestroff;
  189. int offset, nextoffset;
  190. int err;
  191. if ((err = fdt_check_header(fdt)) != 0)
  192. goto fail;
  193. err = -FDT_ERR_BADOFFSET;
  194. if (nodeoffset % FDT_TAGSIZE)
  195. goto fail;
  196. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
  197. if (tag != FDT_BEGIN_NODE)
  198. goto fail;
  199. do {
  200. offset = nextoffset;
  201. tag = fdt_next_tag(fdt, offset, &nextoffset);
  202. switch (tag) {
  203. case FDT_END:
  204. err = -FDT_ERR_TRUNCATED;
  205. goto fail;
  206. case FDT_BEGIN_NODE:
  207. case FDT_END_NODE:
  208. case FDT_NOP:
  209. break;
  210. case FDT_PROP:
  211. err = -FDT_ERR_BADSTRUCTURE;
  212. prop = fdt_offset_ptr(fdt, offset, sizeof(*prop));
  213. if (! prop)
  214. goto fail;
  215. namestroff = fdt32_to_cpu(prop->nameoff);
  216. if (streq(fdt_string(fdt, namestroff), name)) {
  217. /* Found it! */
  218. int len = fdt32_to_cpu(prop->len);
  219. prop = fdt_offset_ptr(fdt, offset,
  220. sizeof(*prop)+len);
  221. if (! prop)
  222. goto fail;
  223. if (lenp)
  224. *lenp = len;
  225. return prop;
  226. }
  227. break;
  228. default:
  229. err = -FDT_ERR_BADSTRUCTURE;
  230. goto fail;
  231. }
  232. } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE));
  233. err = -FDT_ERR_NOTFOUND;
  234. fail:
  235. if (lenp)
  236. *lenp = err;
  237. return NULL;
  238. }
  239. const void *fdt_getprop(const void *fdt, int nodeoffset,
  240. const char *name, int *lenp)
  241. {
  242. const struct fdt_property *prop;
  243. prop = fdt_get_property(fdt, nodeoffset, name, lenp);
  244. if (! prop)
  245. return NULL;
  246. return prop->data;
  247. }
  248. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
  249. {
  250. const uint32_t *php;
  251. int len;
  252. php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
  253. if (!php || (len != sizeof(*php)))
  254. return 0;
  255. return fdt32_to_cpu(*php);
  256. }
  257. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
  258. {
  259. uint32_t tag;
  260. int p = 0, overflow = 0;
  261. int offset, nextoffset, namelen;
  262. const char *name;
  263. CHECK_HEADER(fdt);
  264. tag = fdt_next_tag(fdt, 0, &nextoffset);
  265. if (tag != FDT_BEGIN_NODE)
  266. return -FDT_ERR_BADSTRUCTURE;
  267. if (buflen < 2)
  268. return -FDT_ERR_NOSPACE;
  269. buf[0] = '/';
  270. p = 1;
  271. while (nextoffset <= nodeoffset) {
  272. offset = nextoffset;
  273. tag = fdt_next_tag(fdt, offset, &nextoffset);
  274. switch (tag) {
  275. case FDT_END:
  276. return -FDT_ERR_BADOFFSET;
  277. case FDT_BEGIN_NODE:
  278. name = fdt_get_name(fdt, offset, &namelen);
  279. if (!name)
  280. return namelen;
  281. if (overflow || ((p + namelen + 1) > buflen)) {
  282. overflow++;
  283. break;
  284. }
  285. memcpy(buf + p, name, namelen);
  286. p += namelen;
  287. buf[p++] = '/';
  288. break;
  289. case FDT_END_NODE:
  290. if (overflow) {
  291. overflow--;
  292. break;
  293. }
  294. do {
  295. p--;
  296. } while (buf[p-1] != '/');
  297. break;
  298. case FDT_PROP:
  299. case FDT_NOP:
  300. break;
  301. default:
  302. return -FDT_ERR_BADSTRUCTURE;
  303. }
  304. }
  305. if (overflow)
  306. return -FDT_ERR_NOSPACE;
  307. if (p > 1) /* special case so that root path is "/", not "" */
  308. p--;
  309. buf[p] = '\0';
  310. return p;
  311. }
  312. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  313. int supernodedepth, int *nodedepth)
  314. {
  315. int level = -1;
  316. uint32_t tag;
  317. int offset, nextoffset = 0;
  318. int supernodeoffset = -FDT_ERR_INTERNAL;
  319. CHECK_HEADER(fdt);
  320. if (supernodedepth < 0)
  321. return -FDT_ERR_NOTFOUND;
  322. do {
  323. offset = nextoffset;
  324. tag = fdt_next_tag(fdt, offset, &nextoffset);
  325. switch (tag) {
  326. case FDT_END:
  327. return -FDT_ERR_BADOFFSET;
  328. case FDT_BEGIN_NODE:
  329. level++;
  330. if (level == supernodedepth)
  331. supernodeoffset = offset;
  332. break;
  333. case FDT_END_NODE:
  334. level--;
  335. break;
  336. case FDT_PROP:
  337. case FDT_NOP:
  338. break;
  339. default:
  340. return -FDT_ERR_BADSTRUCTURE;
  341. }
  342. } while (offset < nodeoffset);
  343. if (nodedepth)
  344. *nodedepth = level;
  345. if (supernodedepth > level)
  346. return -FDT_ERR_NOTFOUND;
  347. return supernodeoffset;
  348. }
  349. int fdt_node_depth(const void *fdt, int nodeoffset)
  350. {
  351. int nodedepth;
  352. int err;
  353. err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
  354. if (err)
  355. return (err < 0) ? err : -FDT_ERR_INTERNAL;
  356. return nodedepth;
  357. }
  358. int fdt_parent_offset(const void *fdt, int nodeoffset)
  359. {
  360. int nodedepth = fdt_node_depth(fdt, nodeoffset);
  361. if (nodedepth < 0)
  362. return nodedepth;
  363. return fdt_supernode_atdepth_offset(fdt, nodeoffset,
  364. nodedepth - 1, NULL);
  365. }
  366. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  367. const char *propname,
  368. const void *propval, int proplen)
  369. {
  370. uint32_t tag;
  371. int offset, nextoffset;
  372. const void *val;
  373. int len;
  374. CHECK_HEADER(fdt);
  375. if (startoffset >= 0) {
  376. tag = fdt_next_tag(fdt, startoffset, &nextoffset);
  377. if (tag != FDT_BEGIN_NODE)
  378. return -FDT_ERR_BADOFFSET;
  379. } else {
  380. nextoffset = 0;
  381. }
  382. /* FIXME: The algorithm here is pretty horrible: we scan each
  383. * property of a node in fdt_getprop(), then if that didn't
  384. * find what we want, we scan over them again making our way
  385. * to the next node. Still it's the easiest to implement
  386. * approach; performance can come later. */
  387. do {
  388. offset = nextoffset;
  389. tag = fdt_next_tag(fdt, offset, &nextoffset);
  390. switch (tag) {
  391. case FDT_BEGIN_NODE:
  392. val = fdt_getprop(fdt, offset, propname, &len);
  393. if (val
  394. && (len == proplen)
  395. && (memcmp(val, propval, len) == 0))
  396. return offset;
  397. break;
  398. case FDT_PROP:
  399. case FDT_END:
  400. case FDT_END_NODE:
  401. case FDT_NOP:
  402. break;
  403. default:
  404. return -FDT_ERR_BADSTRUCTURE;
  405. }
  406. } while (tag != FDT_END);
  407. return -FDT_ERR_NOTFOUND;
  408. }
  409. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
  410. {
  411. if ((phandle == 0) || (phandle == -1))
  412. return -FDT_ERR_BADPHANDLE;
  413. phandle = cpu_to_fdt32(phandle);
  414. return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle",
  415. &phandle, sizeof(phandle));
  416. }
  417. int _stringlist_contains(const void *strlist, int listlen, const char *str)
  418. {
  419. int len = strlen(str);
  420. const void *p;
  421. while (listlen >= len) {
  422. if (memcmp(str, strlist, len+1) == 0)
  423. return 1;
  424. p = memchr(strlist, '\0', listlen);
  425. if (!p)
  426. return 0; /* malformed strlist.. */
  427. listlen -= (p-strlist) + 1;
  428. strlist = p + 1;
  429. }
  430. return 0;
  431. }
  432. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  433. const char *compatible)
  434. {
  435. const void *prop;
  436. int len;
  437. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  438. if (!prop)
  439. return len;
  440. if (_stringlist_contains(prop, len, compatible))
  441. return 0;
  442. else
  443. return 1;
  444. }
  445. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  446. const char *compatible)
  447. {
  448. uint32_t tag;
  449. int offset, nextoffset;
  450. int err;
  451. CHECK_HEADER(fdt);
  452. if (startoffset >= 0) {
  453. tag = fdt_next_tag(fdt, startoffset, &nextoffset);
  454. if (tag != FDT_BEGIN_NODE)
  455. return -FDT_ERR_BADOFFSET;
  456. } else {
  457. nextoffset = 0;
  458. }
  459. /* FIXME: The algorithm here is pretty horrible: we scan each
  460. * property of a node in fdt_node_check_compatible(), then if
  461. * that didn't find what we want, we scan over them again
  462. * making our way to the next node. Still it's the easiest to
  463. * implement approach; performance can come later. */
  464. do {
  465. offset = nextoffset;
  466. tag = fdt_next_tag(fdt, offset, &nextoffset);
  467. switch (tag) {
  468. case FDT_BEGIN_NODE:
  469. err = fdt_node_check_compatible(fdt, offset,
  470. compatible);
  471. if ((err < 0)
  472. && (err != -FDT_ERR_NOTFOUND))
  473. return err;
  474. else if (err == 0)
  475. return offset;
  476. break;
  477. case FDT_PROP:
  478. case FDT_END:
  479. case FDT_END_NODE:
  480. case FDT_NOP:
  481. break;
  482. default:
  483. return -FDT_ERR_BADSTRUCTURE;
  484. }
  485. } while (tag != FDT_END);
  486. return -FDT_ERR_NOTFOUND;
  487. }