fdt_ro.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. uint32_t fdt_get_max_phandle(const void *fdt)
  82. {
  83. uint32_t max_phandle = 0;
  84. int offset;
  85. for (offset = fdt_next_node(fdt, -1, NULL);;
  86. offset = fdt_next_node(fdt, offset, NULL)) {
  87. uint32_t phandle;
  88. if (offset == -FDT_ERR_NOTFOUND)
  89. return max_phandle;
  90. if (offset < 0)
  91. return (uint32_t)-1;
  92. phandle = fdt_get_phandle(fdt, offset);
  93. if (phandle == (uint32_t)-1)
  94. continue;
  95. if (phandle > max_phandle)
  96. max_phandle = phandle;
  97. }
  98. return 0;
  99. }
  100. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
  101. {
  102. FDT_CHECK_HEADER(fdt);
  103. *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
  104. *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
  105. return 0;
  106. }
  107. int fdt_num_mem_rsv(const void *fdt)
  108. {
  109. int i = 0;
  110. while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
  111. i++;
  112. return i;
  113. }
  114. static int _nextprop(const void *fdt, int offset)
  115. {
  116. uint32_t tag;
  117. int nextoffset;
  118. do {
  119. tag = fdt_next_tag(fdt, offset, &nextoffset);
  120. switch (tag) {
  121. case FDT_END:
  122. if (nextoffset >= 0)
  123. return -FDT_ERR_BADSTRUCTURE;
  124. else
  125. return nextoffset;
  126. case FDT_PROP:
  127. return offset;
  128. }
  129. offset = nextoffset;
  130. } while (tag == FDT_NOP);
  131. return -FDT_ERR_NOTFOUND;
  132. }
  133. int fdt_subnode_offset_namelen(const void *fdt, int offset,
  134. const char *name, int namelen)
  135. {
  136. int depth;
  137. FDT_CHECK_HEADER(fdt);
  138. for (depth = 0;
  139. (offset >= 0) && (depth >= 0);
  140. offset = fdt_next_node(fdt, offset, &depth))
  141. if ((depth == 1)
  142. && _fdt_nodename_eq(fdt, offset, name, namelen))
  143. return offset;
  144. if (depth < 0)
  145. return -FDT_ERR_NOTFOUND;
  146. return offset; /* error */
  147. }
  148. int fdt_subnode_offset(const void *fdt, int parentoffset,
  149. const char *name)
  150. {
  151. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  152. }
  153. int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
  154. {
  155. const char *end = path + namelen;
  156. const char *p = path;
  157. int offset = 0;
  158. FDT_CHECK_HEADER(fdt);
  159. /* see if we have an alias */
  160. if (*path != '/') {
  161. const char *q = memchr(path, '/', end - p);
  162. if (!q)
  163. q = end;
  164. p = fdt_get_alias_namelen(fdt, p, q - p);
  165. if (!p)
  166. return -FDT_ERR_BADPATH;
  167. offset = fdt_path_offset(fdt, p);
  168. p = q;
  169. }
  170. while (p < end) {
  171. const char *q;
  172. while (*p == '/') {
  173. p++;
  174. if (p == end)
  175. return offset;
  176. }
  177. q = memchr(p, '/', end - p);
  178. if (! q)
  179. q = end;
  180. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  181. if (offset < 0)
  182. return offset;
  183. p = q;
  184. }
  185. return offset;
  186. }
  187. int fdt_path_offset(const void *fdt, const char *path)
  188. {
  189. return fdt_path_offset_namelen(fdt, path, strlen(path));
  190. }
  191. const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
  192. {
  193. const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
  194. int err;
  195. if (((err = fdt_check_header(fdt)) != 0)
  196. || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
  197. goto fail;
  198. if (len)
  199. *len = strlen(nh->name);
  200. return nh->name;
  201. fail:
  202. if (len)
  203. *len = err;
  204. return NULL;
  205. }
  206. int fdt_first_property_offset(const void *fdt, int nodeoffset)
  207. {
  208. int offset;
  209. if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
  210. return offset;
  211. return _nextprop(fdt, offset);
  212. }
  213. int fdt_next_property_offset(const void *fdt, int offset)
  214. {
  215. if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
  216. return offset;
  217. return _nextprop(fdt, offset);
  218. }
  219. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  220. int offset,
  221. int *lenp)
  222. {
  223. int err;
  224. const struct fdt_property *prop;
  225. if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
  226. if (lenp)
  227. *lenp = err;
  228. return NULL;
  229. }
  230. prop = _fdt_offset_ptr(fdt, offset);
  231. if (lenp)
  232. *lenp = fdt32_to_cpu(prop->len);
  233. return prop;
  234. }
  235. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  236. int offset,
  237. const char *name,
  238. int namelen, int *lenp)
  239. {
  240. for (offset = fdt_first_property_offset(fdt, offset);
  241. (offset >= 0);
  242. (offset = fdt_next_property_offset(fdt, offset))) {
  243. const struct fdt_property *prop;
  244. if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
  245. offset = -FDT_ERR_INTERNAL;
  246. break;
  247. }
  248. if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
  249. name, namelen))
  250. return prop;
  251. }
  252. if (lenp)
  253. *lenp = offset;
  254. return NULL;
  255. }
  256. const struct fdt_property *fdt_get_property(const void *fdt,
  257. int nodeoffset,
  258. const char *name, int *lenp)
  259. {
  260. return fdt_get_property_namelen(fdt, nodeoffset, name,
  261. strlen(name), lenp);
  262. }
  263. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  264. const char *name, int namelen, int *lenp)
  265. {
  266. const struct fdt_property *prop;
  267. prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
  268. if (! prop)
  269. return NULL;
  270. return prop->data;
  271. }
  272. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  273. const char **namep, int *lenp)
  274. {
  275. const struct fdt_property *prop;
  276. prop = fdt_get_property_by_offset(fdt, offset, lenp);
  277. if (!prop)
  278. return NULL;
  279. if (namep)
  280. *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  281. return prop->data;
  282. }
  283. const void *fdt_getprop(const void *fdt, int nodeoffset,
  284. const char *name, int *lenp)
  285. {
  286. return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
  287. }
  288. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
  289. {
  290. const fdt32_t *php;
  291. int len;
  292. /* FIXME: This is a bit sub-optimal, since we potentially scan
  293. * over all the properties twice. */
  294. php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
  295. if (!php || (len != sizeof(*php))) {
  296. php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
  297. if (!php || (len != sizeof(*php)))
  298. return 0;
  299. }
  300. return fdt32_to_cpu(*php);
  301. }
  302. const char *fdt_get_alias_namelen(const void *fdt,
  303. const char *name, int namelen)
  304. {
  305. int aliasoffset;
  306. aliasoffset = fdt_path_offset(fdt, "/aliases");
  307. if (aliasoffset < 0)
  308. return NULL;
  309. return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
  310. }
  311. const char *fdt_get_alias(const void *fdt, const char *name)
  312. {
  313. return fdt_get_alias_namelen(fdt, name, strlen(name));
  314. }
  315. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
  316. {
  317. int pdepth = 0, p = 0;
  318. int offset, depth, namelen;
  319. const char *name;
  320. FDT_CHECK_HEADER(fdt);
  321. if (buflen < 2)
  322. return -FDT_ERR_NOSPACE;
  323. for (offset = 0, depth = 0;
  324. (offset >= 0) && (offset <= nodeoffset);
  325. offset = fdt_next_node(fdt, offset, &depth)) {
  326. while (pdepth > depth) {
  327. do {
  328. p--;
  329. } while (buf[p-1] != '/');
  330. pdepth--;
  331. }
  332. if (pdepth >= depth) {
  333. name = fdt_get_name(fdt, offset, &namelen);
  334. if (!name)
  335. return namelen;
  336. if ((p + namelen + 1) <= buflen) {
  337. memcpy(buf + p, name, namelen);
  338. p += namelen;
  339. buf[p++] = '/';
  340. pdepth++;
  341. }
  342. }
  343. if (offset == nodeoffset) {
  344. if (pdepth < (depth + 1))
  345. return -FDT_ERR_NOSPACE;
  346. if (p > 1) /* special case so that root path is "/", not "" */
  347. p--;
  348. buf[p] = '\0';
  349. return 0;
  350. }
  351. }
  352. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  353. return -FDT_ERR_BADOFFSET;
  354. else if (offset == -FDT_ERR_BADOFFSET)
  355. return -FDT_ERR_BADSTRUCTURE;
  356. return offset; /* error from fdt_next_node() */
  357. }
  358. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  359. int supernodedepth, int *nodedepth)
  360. {
  361. int offset, depth;
  362. int supernodeoffset = -FDT_ERR_INTERNAL;
  363. FDT_CHECK_HEADER(fdt);
  364. if (supernodedepth < 0)
  365. return -FDT_ERR_NOTFOUND;
  366. for (offset = 0, depth = 0;
  367. (offset >= 0) && (offset <= nodeoffset);
  368. offset = fdt_next_node(fdt, offset, &depth)) {
  369. if (depth == supernodedepth)
  370. supernodeoffset = offset;
  371. if (offset == nodeoffset) {
  372. if (nodedepth)
  373. *nodedepth = depth;
  374. if (supernodedepth > depth)
  375. return -FDT_ERR_NOTFOUND;
  376. else
  377. return supernodeoffset;
  378. }
  379. }
  380. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  381. return -FDT_ERR_BADOFFSET;
  382. else if (offset == -FDT_ERR_BADOFFSET)
  383. return -FDT_ERR_BADSTRUCTURE;
  384. return offset; /* error from fdt_next_node() */
  385. }
  386. int fdt_node_depth(const void *fdt, int nodeoffset)
  387. {
  388. int nodedepth;
  389. int err;
  390. err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
  391. if (err)
  392. return (err < 0) ? err : -FDT_ERR_INTERNAL;
  393. return nodedepth;
  394. }
  395. int fdt_parent_offset(const void *fdt, int nodeoffset)
  396. {
  397. int nodedepth = fdt_node_depth(fdt, nodeoffset);
  398. if (nodedepth < 0)
  399. return nodedepth;
  400. return fdt_supernode_atdepth_offset(fdt, nodeoffset,
  401. nodedepth - 1, NULL);
  402. }
  403. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  404. const char *propname,
  405. const void *propval, int proplen)
  406. {
  407. int offset;
  408. const void *val;
  409. int len;
  410. FDT_CHECK_HEADER(fdt);
  411. /* FIXME: The algorithm here is pretty horrible: we scan each
  412. * property of a node in fdt_getprop(), then if that didn't
  413. * find what we want, we scan over them again making our way
  414. * to the next node. Still it's the easiest to implement
  415. * approach; performance can come later. */
  416. for (offset = fdt_next_node(fdt, startoffset, NULL);
  417. offset >= 0;
  418. offset = fdt_next_node(fdt, offset, NULL)) {
  419. val = fdt_getprop(fdt, offset, propname, &len);
  420. if (val && (len == proplen)
  421. && (memcmp(val, propval, len) == 0))
  422. return offset;
  423. }
  424. return offset; /* error from fdt_next_node() */
  425. }
  426. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
  427. {
  428. int offset;
  429. if ((phandle == 0) || (phandle == -1))
  430. return -FDT_ERR_BADPHANDLE;
  431. FDT_CHECK_HEADER(fdt);
  432. /* FIXME: The algorithm here is pretty horrible: we
  433. * potentially scan each property of a node in
  434. * fdt_get_phandle(), then if that didn't find what
  435. * we want, we scan over them again making our way to the next
  436. * node. Still it's the easiest to implement approach;
  437. * performance can come later. */
  438. for (offset = fdt_next_node(fdt, -1, NULL);
  439. offset >= 0;
  440. offset = fdt_next_node(fdt, offset, NULL)) {
  441. if (fdt_get_phandle(fdt, offset) == phandle)
  442. return offset;
  443. }
  444. return offset; /* error from fdt_next_node() */
  445. }
  446. int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
  447. {
  448. int len = strlen(str);
  449. const char *p;
  450. while (listlen >= len) {
  451. if (memcmp(str, strlist, len+1) == 0)
  452. return 1;
  453. p = memchr(strlist, '\0', listlen);
  454. if (!p)
  455. return 0; /* malformed strlist.. */
  456. listlen -= (p-strlist) + 1;
  457. strlist = p + 1;
  458. }
  459. return 0;
  460. }
  461. int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
  462. {
  463. const char *list, *end;
  464. int length, count = 0;
  465. list = fdt_getprop(fdt, nodeoffset, property, &length);
  466. if (!list)
  467. return length;
  468. end = list + length;
  469. while (list < end) {
  470. length = strnlen(list, end - list) + 1;
  471. /* Abort if the last string isn't properly NUL-terminated. */
  472. if (list + length > end)
  473. return -FDT_ERR_BADVALUE;
  474. list += length;
  475. count++;
  476. }
  477. return count;
  478. }
  479. int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
  480. const char *string)
  481. {
  482. int length, len, idx = 0;
  483. const char *list, *end;
  484. list = fdt_getprop(fdt, nodeoffset, property, &length);
  485. if (!list)
  486. return length;
  487. len = strlen(string) + 1;
  488. end = list + length;
  489. while (list < end) {
  490. length = strnlen(list, end - list) + 1;
  491. /* Abort if the last string isn't properly NUL-terminated. */
  492. if (list + length > end)
  493. return -FDT_ERR_BADVALUE;
  494. if (length == len && memcmp(list, string, length) == 0)
  495. return idx;
  496. list += length;
  497. idx++;
  498. }
  499. return -FDT_ERR_NOTFOUND;
  500. }
  501. const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
  502. const char *property, int idx,
  503. int *lenp)
  504. {
  505. const char *list, *end;
  506. int length;
  507. list = fdt_getprop(fdt, nodeoffset, property, &length);
  508. if (!list) {
  509. if (lenp)
  510. *lenp = length;
  511. return NULL;
  512. }
  513. end = list + length;
  514. while (list < end) {
  515. length = strnlen(list, end - list) + 1;
  516. /* Abort if the last string isn't properly NUL-terminated. */
  517. if (list + length > end) {
  518. if (lenp)
  519. *lenp = -FDT_ERR_BADVALUE;
  520. return NULL;
  521. }
  522. if (idx == 0) {
  523. if (lenp)
  524. *lenp = length - 1;
  525. return list;
  526. }
  527. list += length;
  528. idx--;
  529. }
  530. if (lenp)
  531. *lenp = -FDT_ERR_NOTFOUND;
  532. return NULL;
  533. }
  534. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  535. const char *compatible)
  536. {
  537. const void *prop;
  538. int len;
  539. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  540. if (!prop)
  541. return len;
  542. return !fdt_stringlist_contains(prop, len, compatible);
  543. }
  544. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  545. const char *compatible)
  546. {
  547. int offset, err;
  548. FDT_CHECK_HEADER(fdt);
  549. /* FIXME: The algorithm here is pretty horrible: we scan each
  550. * property of a node in fdt_node_check_compatible(), then if
  551. * that didn't find what we want, we scan over them again
  552. * making our way to the next node. Still it's the easiest to
  553. * implement approach; performance can come later. */
  554. for (offset = fdt_next_node(fdt, startoffset, NULL);
  555. offset >= 0;
  556. offset = fdt_next_node(fdt, offset, NULL)) {
  557. err = fdt_node_check_compatible(fdt, offset, compatible);
  558. if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
  559. return err;
  560. else if (err == 0)
  561. return offset;
  562. }
  563. return offset; /* error from fdt_next_node() */
  564. }