fdt_ro.c 19 KB

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