fdt_ro.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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_stringlist_count(const void *fdt, int nodeoffset, const char *property)
  443. {
  444. const char *list, *end;
  445. int length, count = 0;
  446. list = fdt_getprop(fdt, nodeoffset, property, &length);
  447. if (!list)
  448. return -length;
  449. end = list + length;
  450. while (list < end) {
  451. length = strnlen(list, end - list) + 1;
  452. /* Abort if the last string isn't properly NUL-terminated. */
  453. if (list + length > end)
  454. return -FDT_ERR_BADVALUE;
  455. list += length;
  456. count++;
  457. }
  458. return count;
  459. }
  460. int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
  461. const char *string)
  462. {
  463. int length, len, idx = 0;
  464. const char *list, *end;
  465. list = fdt_getprop(fdt, nodeoffset, property, &length);
  466. if (!list)
  467. return -length;
  468. len = strlen(string) + 1;
  469. end = list + length;
  470. while (list < end) {
  471. length = strnlen(list, end - list) + 1;
  472. /* Abort if the last string isn't properly NUL-terminated. */
  473. if (list + length > end)
  474. return -FDT_ERR_BADVALUE;
  475. if (length == len && memcmp(list, string, length) == 0)
  476. return idx;
  477. list += length;
  478. idx++;
  479. }
  480. return -FDT_ERR_NOTFOUND;
  481. }
  482. const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
  483. const char *property, int idx,
  484. int *lenp)
  485. {
  486. const char *list, *end;
  487. int length;
  488. list = fdt_getprop(fdt, nodeoffset, property, &length);
  489. if (!list) {
  490. if (lenp)
  491. *lenp = length;
  492. return NULL;
  493. }
  494. end = list + length;
  495. while (list < end) {
  496. length = strnlen(list, end - list) + 1;
  497. /* Abort if the last string isn't properly NUL-terminated. */
  498. if (list + length > end) {
  499. if (lenp)
  500. *lenp = -FDT_ERR_BADVALUE;
  501. return NULL;
  502. }
  503. if (idx == 0) {
  504. if (lenp)
  505. *lenp = length - 1;
  506. return list;
  507. }
  508. list += length;
  509. idx--;
  510. }
  511. if (lenp)
  512. *lenp = -FDT_ERR_NOTFOUND;
  513. return NULL;
  514. }
  515. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  516. const char *compatible)
  517. {
  518. const void *prop;
  519. int len;
  520. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  521. if (!prop)
  522. return len;
  523. return !fdt_stringlist_contains(prop, len, compatible);
  524. }
  525. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  526. const char *compatible)
  527. {
  528. int offset, err;
  529. FDT_CHECK_HEADER(fdt);
  530. /* FIXME: The algorithm here is pretty horrible: we scan each
  531. * property of a node in fdt_node_check_compatible(), then if
  532. * that didn't find what we want, we scan over them again
  533. * making our way to the next node. Still it's the easiest to
  534. * implement approach; performance can come later. */
  535. for (offset = fdt_next_node(fdt, startoffset, NULL);
  536. offset >= 0;
  537. offset = fdt_next_node(fdt, offset, NULL)) {
  538. err = fdt_node_check_compatible(fdt, offset, compatible);
  539. if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
  540. return err;
  541. else if (err == 0)
  542. return offset;
  543. }
  544. return offset; /* error from fdt_next_node() */
  545. }