fdt_ro.c 18 KB

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