/scripts/dtc/libfdt/fdt_ro.c

http://github.com/mirrors/linux · C · 857 lines · 649 code · 168 blank · 40 comment · 202 complexity · 19f89409575ebea2659fd2e345d68fed MD5 · raw file

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