fdt_ro.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. int olen;
  59. const char *p = fdt_get_name(fdt, offset, &olen);
  60. if (!p || olen < len)
  61. /* short match */
  62. return 0;
  63. if (memcmp(p, s, len) != 0)
  64. return 0;
  65. if (p[len] == '\0')
  66. return 1;
  67. else if (!memchr(s, '@', len) && (p[len] == '@'))
  68. return 1;
  69. else
  70. return 0;
  71. }
  72. const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
  73. {
  74. uint32_t absoffset = stroffset + fdt_off_dt_strings(fdt);
  75. size_t len;
  76. int err;
  77. const char *s, *n;
  78. err = fdt_ro_probe_(fdt);
  79. if (err != 0)
  80. goto fail;
  81. err = -FDT_ERR_BADOFFSET;
  82. if (absoffset >= fdt_totalsize(fdt))
  83. goto fail;
  84. len = fdt_totalsize(fdt) - absoffset;
  85. if (fdt_magic(fdt) == FDT_MAGIC) {
  86. if (stroffset < 0)
  87. goto fail;
  88. if (fdt_version(fdt) >= 17) {
  89. if (stroffset >= fdt_size_dt_strings(fdt))
  90. goto fail;
  91. if ((fdt_size_dt_strings(fdt) - stroffset) < len)
  92. len = fdt_size_dt_strings(fdt) - stroffset;
  93. }
  94. } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
  95. if ((stroffset >= 0)
  96. || (stroffset < -fdt_size_dt_strings(fdt)))
  97. goto fail;
  98. if ((-stroffset) < len)
  99. len = -stroffset;
  100. } else {
  101. err = -FDT_ERR_INTERNAL;
  102. goto fail;
  103. }
  104. s = (const char *)fdt + absoffset;
  105. n = memchr(s, '\0', len);
  106. if (!n) {
  107. /* missing terminating NULL */
  108. err = -FDT_ERR_TRUNCATED;
  109. goto fail;
  110. }
  111. if (lenp)
  112. *lenp = n - s;
  113. return s;
  114. fail:
  115. if (lenp)
  116. *lenp = err;
  117. return NULL;
  118. }
  119. const char *fdt_string(const void *fdt, int stroffset)
  120. {
  121. return fdt_get_string(fdt, stroffset, NULL);
  122. }
  123. static int fdt_string_eq_(const void *fdt, int stroffset,
  124. const char *s, int len)
  125. {
  126. int slen;
  127. const char *p = fdt_get_string(fdt, stroffset, &slen);
  128. return p && (slen == len) && (memcmp(p, s, len) == 0);
  129. }
  130. uint32_t fdt_get_max_phandle(const void *fdt)
  131. {
  132. uint32_t max_phandle = 0;
  133. int offset;
  134. for (offset = fdt_next_node(fdt, -1, NULL);;
  135. offset = fdt_next_node(fdt, offset, NULL)) {
  136. uint32_t phandle;
  137. if (offset == -FDT_ERR_NOTFOUND)
  138. return max_phandle;
  139. if (offset < 0)
  140. return (uint32_t)-1;
  141. phandle = fdt_get_phandle(fdt, offset);
  142. if (phandle == (uint32_t)-1)
  143. continue;
  144. if (phandle > max_phandle)
  145. max_phandle = phandle;
  146. }
  147. return 0;
  148. }
  149. static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
  150. {
  151. int offset = n * sizeof(struct fdt_reserve_entry);
  152. int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
  153. if (absoffset < fdt_off_mem_rsvmap(fdt))
  154. return NULL;
  155. if (absoffset > fdt_totalsize(fdt) - sizeof(struct fdt_reserve_entry))
  156. return NULL;
  157. return fdt_mem_rsv_(fdt, n);
  158. }
  159. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
  160. {
  161. const struct fdt_reserve_entry *re;
  162. FDT_RO_PROBE(fdt);
  163. re = fdt_mem_rsv(fdt, n);
  164. if (!re)
  165. return -FDT_ERR_BADOFFSET;
  166. *address = fdt64_ld(&re->address);
  167. *size = fdt64_ld(&re->size);
  168. return 0;
  169. }
  170. int fdt_num_mem_rsv(const void *fdt)
  171. {
  172. int i;
  173. const struct fdt_reserve_entry *re;
  174. for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
  175. if (fdt64_ld(&re->size) == 0)
  176. return i;
  177. }
  178. return -FDT_ERR_TRUNCATED;
  179. }
  180. static int nextprop_(const void *fdt, int offset)
  181. {
  182. uint32_t tag;
  183. int nextoffset;
  184. do {
  185. tag = fdt_next_tag(fdt, offset, &nextoffset);
  186. switch (tag) {
  187. case FDT_END:
  188. if (nextoffset >= 0)
  189. return -FDT_ERR_BADSTRUCTURE;
  190. else
  191. return nextoffset;
  192. case FDT_PROP:
  193. return offset;
  194. }
  195. offset = nextoffset;
  196. } while (tag == FDT_NOP);
  197. return -FDT_ERR_NOTFOUND;
  198. }
  199. int fdt_subnode_offset_namelen(const void *fdt, int offset,
  200. const char *name, int namelen)
  201. {
  202. int depth;
  203. FDT_RO_PROBE(fdt);
  204. for (depth = 0;
  205. (offset >= 0) && (depth >= 0);
  206. offset = fdt_next_node(fdt, offset, &depth))
  207. if ((depth == 1)
  208. && fdt_nodename_eq_(fdt, offset, name, namelen))
  209. return offset;
  210. if (depth < 0)
  211. return -FDT_ERR_NOTFOUND;
  212. return offset; /* error */
  213. }
  214. int fdt_subnode_offset(const void *fdt, int parentoffset,
  215. const char *name)
  216. {
  217. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  218. }
  219. int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
  220. {
  221. const char *end = path + namelen;
  222. const char *p = path;
  223. int offset = 0;
  224. FDT_RO_PROBE(fdt);
  225. /* see if we have an alias */
  226. if (*path != '/') {
  227. const char *q = memchr(path, '/', end - p);
  228. if (!q)
  229. q = end;
  230. p = fdt_get_alias_namelen(fdt, p, q - p);
  231. if (!p)
  232. return -FDT_ERR_BADPATH;
  233. offset = fdt_path_offset(fdt, p);
  234. p = q;
  235. }
  236. while (p < end) {
  237. const char *q;
  238. while (*p == '/') {
  239. p++;
  240. if (p == end)
  241. return offset;
  242. }
  243. q = memchr(p, '/', end - p);
  244. if (! q)
  245. q = end;
  246. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  247. if (offset < 0)
  248. return offset;
  249. p = q;
  250. }
  251. return offset;
  252. }
  253. int fdt_path_offset(const void *fdt, const char *path)
  254. {
  255. return fdt_path_offset_namelen(fdt, path, strlen(path));
  256. }
  257. const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
  258. {
  259. const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
  260. const char *nameptr;
  261. int err;
  262. if (((err = fdt_ro_probe_(fdt)) != 0)
  263. || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
  264. goto fail;
  265. nameptr = nh->name;
  266. if (fdt_version(fdt) < 0x10) {
  267. /*
  268. * For old FDT versions, match the naming conventions of V16:
  269. * give only the leaf name (after all /). The actual tree
  270. * contents are loosely checked.
  271. */
  272. const char *leaf;
  273. leaf = strrchr(nameptr, '/');
  274. if (leaf == NULL) {
  275. err = -FDT_ERR_BADSTRUCTURE;
  276. goto fail;
  277. }
  278. nameptr = leaf+1;
  279. }
  280. if (len)
  281. *len = strlen(nameptr);
  282. return nameptr;
  283. fail:
  284. if (len)
  285. *len = err;
  286. return NULL;
  287. }
  288. int fdt_first_property_offset(const void *fdt, int nodeoffset)
  289. {
  290. int offset;
  291. if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
  292. return offset;
  293. return nextprop_(fdt, offset);
  294. }
  295. int fdt_next_property_offset(const void *fdt, int offset)
  296. {
  297. if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
  298. return offset;
  299. return nextprop_(fdt, offset);
  300. }
  301. static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
  302. int offset,
  303. int *lenp)
  304. {
  305. int err;
  306. const struct fdt_property *prop;
  307. if ((err = fdt_check_prop_offset_(fdt, offset)) < 0) {
  308. if (lenp)
  309. *lenp = err;
  310. return NULL;
  311. }
  312. prop = fdt_offset_ptr_(fdt, offset);
  313. if (lenp)
  314. *lenp = fdt32_ld(&prop->len);
  315. return prop;
  316. }
  317. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  318. int offset,
  319. int *lenp)
  320. {
  321. /* Prior to version 16, properties may need realignment
  322. * and this API does not work. fdt_getprop_*() will, however. */
  323. if (fdt_version(fdt) < 0x10) {
  324. if (lenp)
  325. *lenp = -FDT_ERR_BADVERSION;
  326. return NULL;
  327. }
  328. return fdt_get_property_by_offset_(fdt, offset, lenp);
  329. }
  330. static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
  331. int offset,
  332. const char *name,
  333. int namelen,
  334. int *lenp,
  335. int *poffset)
  336. {
  337. for (offset = fdt_first_property_offset(fdt, offset);
  338. (offset >= 0);
  339. (offset = fdt_next_property_offset(fdt, offset))) {
  340. const struct fdt_property *prop;
  341. if (!(prop = fdt_get_property_by_offset_(fdt, offset, lenp))) {
  342. offset = -FDT_ERR_INTERNAL;
  343. break;
  344. }
  345. if (fdt_string_eq_(fdt, fdt32_ld(&prop->nameoff),
  346. name, namelen)) {
  347. if (poffset)
  348. *poffset = offset;
  349. return prop;
  350. }
  351. }
  352. if (lenp)
  353. *lenp = offset;
  354. return NULL;
  355. }
  356. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  357. int offset,
  358. const char *name,
  359. int namelen, int *lenp)
  360. {
  361. /* Prior to version 16, properties may need realignment
  362. * and this API does not work. fdt_getprop_*() will, however. */
  363. if (fdt_version(fdt) < 0x10) {
  364. if (lenp)
  365. *lenp = -FDT_ERR_BADVERSION;
  366. return NULL;
  367. }
  368. return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp,
  369. NULL);
  370. }
  371. const struct fdt_property *fdt_get_property(const void *fdt,
  372. int nodeoffset,
  373. const char *name, int *lenp)
  374. {
  375. return fdt_get_property_namelen(fdt, nodeoffset, name,
  376. strlen(name), lenp);
  377. }
  378. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  379. const char *name, int namelen, int *lenp)
  380. {
  381. int poffset;
  382. const struct fdt_property *prop;
  383. prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp,
  384. &poffset);
  385. if (!prop)
  386. return NULL;
  387. /* Handle realignment */
  388. if (fdt_version(fdt) < 0x10 && (poffset + sizeof(*prop)) % 8 &&
  389. fdt32_ld(&prop->len) >= 8)
  390. return prop->data + 4;
  391. return prop->data;
  392. }
  393. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  394. const char **namep, int *lenp)
  395. {
  396. const struct fdt_property *prop;
  397. prop = fdt_get_property_by_offset_(fdt, offset, lenp);
  398. if (!prop)
  399. return NULL;
  400. if (namep) {
  401. const char *name;
  402. int namelen;
  403. name = fdt_get_string(fdt, fdt32_ld(&prop->nameoff),
  404. &namelen);
  405. if (!name) {
  406. if (lenp)
  407. *lenp = namelen;
  408. return NULL;
  409. }
  410. *namep = name;
  411. }
  412. /* Handle realignment */
  413. if (fdt_version(fdt) < 0x10 && (offset + sizeof(*prop)) % 8 &&
  414. fdt32_ld(&prop->len) >= 8)
  415. return prop->data + 4;
  416. return prop->data;
  417. }
  418. const void *fdt_getprop(const void *fdt, int nodeoffset,
  419. const char *name, int *lenp)
  420. {
  421. return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
  422. }
  423. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
  424. {
  425. const fdt32_t *php;
  426. int len;
  427. /* FIXME: This is a bit sub-optimal, since we potentially scan
  428. * over all the properties twice. */
  429. php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
  430. if (!php || (len != sizeof(*php))) {
  431. php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
  432. if (!php || (len != sizeof(*php)))
  433. return 0;
  434. }
  435. return fdt32_ld(php);
  436. }
  437. const char *fdt_get_alias_namelen(const void *fdt,
  438. const char *name, int namelen)
  439. {
  440. int aliasoffset;
  441. aliasoffset = fdt_path_offset(fdt, "/aliases");
  442. if (aliasoffset < 0)
  443. return NULL;
  444. return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
  445. }
  446. const char *fdt_get_alias(const void *fdt, const char *name)
  447. {
  448. return fdt_get_alias_namelen(fdt, name, strlen(name));
  449. }
  450. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
  451. {
  452. int pdepth = 0, p = 0;
  453. int offset, depth, namelen;
  454. const char *name;
  455. FDT_RO_PROBE(fdt);
  456. if (buflen < 2)
  457. return -FDT_ERR_NOSPACE;
  458. for (offset = 0, depth = 0;
  459. (offset >= 0) && (offset <= nodeoffset);
  460. offset = fdt_next_node(fdt, offset, &depth)) {
  461. while (pdepth > depth) {
  462. do {
  463. p--;
  464. } while (buf[p-1] != '/');
  465. pdepth--;
  466. }
  467. if (pdepth >= depth) {
  468. name = fdt_get_name(fdt, offset, &namelen);
  469. if (!name)
  470. return namelen;
  471. if ((p + namelen + 1) <= buflen) {
  472. memcpy(buf + p, name, namelen);
  473. p += namelen;
  474. buf[p++] = '/';
  475. pdepth++;
  476. }
  477. }
  478. if (offset == nodeoffset) {
  479. if (pdepth < (depth + 1))
  480. return -FDT_ERR_NOSPACE;
  481. if (p > 1) /* special case so that root path is "/", not "" */
  482. p--;
  483. buf[p] = '\0';
  484. return 0;
  485. }
  486. }
  487. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  488. return -FDT_ERR_BADOFFSET;
  489. else if (offset == -FDT_ERR_BADOFFSET)
  490. return -FDT_ERR_BADSTRUCTURE;
  491. return offset; /* error from fdt_next_node() */
  492. }
  493. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  494. int supernodedepth, int *nodedepth)
  495. {
  496. int offset, depth;
  497. int supernodeoffset = -FDT_ERR_INTERNAL;
  498. FDT_RO_PROBE(fdt);
  499. if (supernodedepth < 0)
  500. return -FDT_ERR_NOTFOUND;
  501. for (offset = 0, depth = 0;
  502. (offset >= 0) && (offset <= nodeoffset);
  503. offset = fdt_next_node(fdt, offset, &depth)) {
  504. if (depth == supernodedepth)
  505. supernodeoffset = offset;
  506. if (offset == nodeoffset) {
  507. if (nodedepth)
  508. *nodedepth = depth;
  509. if (supernodedepth > depth)
  510. return -FDT_ERR_NOTFOUND;
  511. else
  512. return supernodeoffset;
  513. }
  514. }
  515. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  516. return -FDT_ERR_BADOFFSET;
  517. else if (offset == -FDT_ERR_BADOFFSET)
  518. return -FDT_ERR_BADSTRUCTURE;
  519. return offset; /* error from fdt_next_node() */
  520. }
  521. int fdt_node_depth(const void *fdt, int nodeoffset)
  522. {
  523. int nodedepth;
  524. int err;
  525. err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
  526. if (err)
  527. return (err < 0) ? err : -FDT_ERR_INTERNAL;
  528. return nodedepth;
  529. }
  530. int fdt_parent_offset(const void *fdt, int nodeoffset)
  531. {
  532. int nodedepth = fdt_node_depth(fdt, nodeoffset);
  533. if (nodedepth < 0)
  534. return nodedepth;
  535. return fdt_supernode_atdepth_offset(fdt, nodeoffset,
  536. nodedepth - 1, NULL);
  537. }
  538. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  539. const char *propname,
  540. const void *propval, int proplen)
  541. {
  542. int offset;
  543. const void *val;
  544. int len;
  545. FDT_RO_PROBE(fdt);
  546. /* FIXME: The algorithm here is pretty horrible: we scan each
  547. * property of a node in fdt_getprop(), then if that didn't
  548. * find what we want, we scan over them again making our way
  549. * to the next node. Still it's the easiest to implement
  550. * approach; performance can come later. */
  551. for (offset = fdt_next_node(fdt, startoffset, NULL);
  552. offset >= 0;
  553. offset = fdt_next_node(fdt, offset, NULL)) {
  554. val = fdt_getprop(fdt, offset, propname, &len);
  555. if (val && (len == proplen)
  556. && (memcmp(val, propval, len) == 0))
  557. return offset;
  558. }
  559. return offset; /* error from fdt_next_node() */
  560. }
  561. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
  562. {
  563. int offset;
  564. if ((phandle == 0) || (phandle == -1))
  565. return -FDT_ERR_BADPHANDLE;
  566. FDT_RO_PROBE(fdt);
  567. /* FIXME: The algorithm here is pretty horrible: we
  568. * potentially scan each property of a node in
  569. * fdt_get_phandle(), then if that didn't find what
  570. * we want, we scan over them again making our way to the next
  571. * node. Still it's the easiest to implement approach;
  572. * performance can come later. */
  573. for (offset = fdt_next_node(fdt, -1, NULL);
  574. offset >= 0;
  575. offset = fdt_next_node(fdt, offset, NULL)) {
  576. if (fdt_get_phandle(fdt, offset) == phandle)
  577. return offset;
  578. }
  579. return offset; /* error from fdt_next_node() */
  580. }
  581. int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
  582. {
  583. int len = strlen(str);
  584. const char *p;
  585. while (listlen >= len) {
  586. if (memcmp(str, strlist, len+1) == 0)
  587. return 1;
  588. p = memchr(strlist, '\0', listlen);
  589. if (!p)
  590. return 0; /* malformed strlist.. */
  591. listlen -= (p-strlist) + 1;
  592. strlist = p + 1;
  593. }
  594. return 0;
  595. }
  596. int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
  597. {
  598. const char *list, *end;
  599. int length, count = 0;
  600. list = fdt_getprop(fdt, nodeoffset, property, &length);
  601. if (!list)
  602. return length;
  603. end = list + length;
  604. while (list < end) {
  605. length = strnlen(list, end - list) + 1;
  606. /* Abort if the last string isn't properly NUL-terminated. */
  607. if (list + length > end)
  608. return -FDT_ERR_BADVALUE;
  609. list += length;
  610. count++;
  611. }
  612. return count;
  613. }
  614. int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
  615. const char *string)
  616. {
  617. int length, len, idx = 0;
  618. const char *list, *end;
  619. list = fdt_getprop(fdt, nodeoffset, property, &length);
  620. if (!list)
  621. return length;
  622. len = strlen(string) + 1;
  623. end = list + length;
  624. while (list < end) {
  625. length = strnlen(list, end - list) + 1;
  626. /* Abort if the last string isn't properly NUL-terminated. */
  627. if (list + length > end)
  628. return -FDT_ERR_BADVALUE;
  629. if (length == len && memcmp(list, string, length) == 0)
  630. return idx;
  631. list += length;
  632. idx++;
  633. }
  634. return -FDT_ERR_NOTFOUND;
  635. }
  636. const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
  637. const char *property, int idx,
  638. int *lenp)
  639. {
  640. const char *list, *end;
  641. int length;
  642. list = fdt_getprop(fdt, nodeoffset, property, &length);
  643. if (!list) {
  644. if (lenp)
  645. *lenp = length;
  646. return NULL;
  647. }
  648. end = list + length;
  649. while (list < end) {
  650. length = strnlen(list, end - list) + 1;
  651. /* Abort if the last string isn't properly NUL-terminated. */
  652. if (list + length > end) {
  653. if (lenp)
  654. *lenp = -FDT_ERR_BADVALUE;
  655. return NULL;
  656. }
  657. if (idx == 0) {
  658. if (lenp)
  659. *lenp = length - 1;
  660. return list;
  661. }
  662. list += length;
  663. idx--;
  664. }
  665. if (lenp)
  666. *lenp = -FDT_ERR_NOTFOUND;
  667. return NULL;
  668. }
  669. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  670. const char *compatible)
  671. {
  672. const void *prop;
  673. int len;
  674. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  675. if (!prop)
  676. return len;
  677. return !fdt_stringlist_contains(prop, len, compatible);
  678. }
  679. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  680. const char *compatible)
  681. {
  682. int offset, err;
  683. FDT_RO_PROBE(fdt);
  684. /* FIXME: The algorithm here is pretty horrible: we scan each
  685. * property of a node in fdt_node_check_compatible(), then if
  686. * that didn't find what we want, we scan over them again
  687. * making our way to the next node. Still it's the easiest to
  688. * implement approach; performance can come later. */
  689. for (offset = fdt_next_node(fdt, startoffset, NULL);
  690. offset >= 0;
  691. offset = fdt_next_node(fdt, offset, NULL)) {
  692. err = fdt_node_check_compatible(fdt, offset, compatible);
  693. if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
  694. return err;
  695. else if (err == 0)
  696. return offset;
  697. }
  698. return offset; /* error from fdt_next_node() */
  699. }
  700. int fdt_check_full(const void *fdt, size_t bufsize)
  701. {
  702. int err;
  703. int num_memrsv;
  704. int offset, nextoffset = 0;
  705. uint32_t tag;
  706. unsigned depth = 0;
  707. const void *prop;
  708. const char *propname;
  709. if (bufsize < FDT_V1_SIZE)
  710. return -FDT_ERR_TRUNCATED;
  711. err = fdt_check_header(fdt);
  712. if (err != 0)
  713. return err;
  714. if (bufsize < fdt_totalsize(fdt))
  715. return -FDT_ERR_TRUNCATED;
  716. num_memrsv = fdt_num_mem_rsv(fdt);
  717. if (num_memrsv < 0)
  718. return num_memrsv;
  719. while (1) {
  720. offset = nextoffset;
  721. tag = fdt_next_tag(fdt, offset, &nextoffset);
  722. if (nextoffset < 0)
  723. return nextoffset;
  724. switch (tag) {
  725. case FDT_NOP:
  726. break;
  727. case FDT_END:
  728. if (depth != 0)
  729. return -FDT_ERR_BADSTRUCTURE;
  730. return 0;
  731. case FDT_BEGIN_NODE:
  732. depth++;
  733. if (depth > INT_MAX)
  734. return -FDT_ERR_BADSTRUCTURE;
  735. break;
  736. case FDT_END_NODE:
  737. if (depth == 0)
  738. return -FDT_ERR_BADSTRUCTURE;
  739. depth--;
  740. break;
  741. case FDT_PROP:
  742. prop = fdt_getprop_by_offset(fdt, offset, &propname,
  743. &err);
  744. if (!prop)
  745. return err;
  746. break;
  747. default:
  748. return -FDT_ERR_INTERNAL;
  749. }
  750. }
  751. }