fdt_ro.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. /* see if we have an alias */
  208. if (*path != '/') {
  209. const char *q = memchr(path, '/', end - p);
  210. if (!q)
  211. q = end;
  212. p = fdt_get_alias_namelen(fdt, p, q - p);
  213. if (!p)
  214. return -FDT_ERR_BADPATH;
  215. offset = fdt_path_offset(fdt, p);
  216. p = q;
  217. }
  218. while (p < end) {
  219. const char *q;
  220. while (*p == '/') {
  221. p++;
  222. if (p == end)
  223. return offset;
  224. }
  225. q = memchr(p, '/', end - p);
  226. if (! q)
  227. q = end;
  228. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  229. if (offset < 0)
  230. return offset;
  231. p = q;
  232. }
  233. return offset;
  234. }
  235. int fdt_path_offset(const void *fdt, const char *path)
  236. {
  237. return fdt_path_offset_namelen(fdt, path, strlen(path));
  238. }
  239. const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
  240. {
  241. const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
  242. const char *nameptr;
  243. int err;
  244. if (((err = fdt_ro_probe_(fdt)) < 0)
  245. || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
  246. goto fail;
  247. nameptr = nh->name;
  248. if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
  249. /*
  250. * For old FDT versions, match the naming conventions of V16:
  251. * give only the leaf name (after all /). The actual tree
  252. * contents are loosely checked.
  253. */
  254. const char *leaf;
  255. leaf = strrchr(nameptr, '/');
  256. if (leaf == NULL) {
  257. err = -FDT_ERR_BADSTRUCTURE;
  258. goto fail;
  259. }
  260. nameptr = leaf+1;
  261. }
  262. if (len)
  263. *len = strlen(nameptr);
  264. return nameptr;
  265. fail:
  266. if (len)
  267. *len = err;
  268. return NULL;
  269. }
  270. int fdt_first_property_offset(const void *fdt, int nodeoffset)
  271. {
  272. int offset;
  273. if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
  274. return offset;
  275. return nextprop_(fdt, offset);
  276. }
  277. int fdt_next_property_offset(const void *fdt, int offset)
  278. {
  279. if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
  280. return offset;
  281. return nextprop_(fdt, offset);
  282. }
  283. static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
  284. int offset,
  285. int *lenp)
  286. {
  287. int err;
  288. const struct fdt_property *prop;
  289. if (!can_assume(VALID_INPUT) &&
  290. (err = fdt_check_prop_offset_(fdt, offset)) < 0) {
  291. if (lenp)
  292. *lenp = err;
  293. return NULL;
  294. }
  295. prop = fdt_offset_ptr_(fdt, offset);
  296. if (lenp)
  297. *lenp = fdt32_ld_(&prop->len);
  298. return prop;
  299. }
  300. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  301. int offset,
  302. int *lenp)
  303. {
  304. /* Prior to version 16, properties may need realignment
  305. * and this API does not work. fdt_getprop_*() will, however. */
  306. if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
  307. if (lenp)
  308. *lenp = -FDT_ERR_BADVERSION;
  309. return NULL;
  310. }
  311. return fdt_get_property_by_offset_(fdt, offset, lenp);
  312. }
  313. static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
  314. int offset,
  315. const char *name,
  316. int namelen,
  317. int *lenp,
  318. int *poffset)
  319. {
  320. for (offset = fdt_first_property_offset(fdt, offset);
  321. (offset >= 0);
  322. (offset = fdt_next_property_offset(fdt, offset))) {
  323. const struct fdt_property *prop;
  324. prop = fdt_get_property_by_offset_(fdt, offset, lenp);
  325. if (!can_assume(LIBFDT_FLAWLESS) && !prop) {
  326. offset = -FDT_ERR_INTERNAL;
  327. break;
  328. }
  329. if (fdt_string_eq_(fdt, fdt32_ld_(&prop->nameoff),
  330. name, namelen)) {
  331. if (poffset)
  332. *poffset = offset;
  333. return prop;
  334. }
  335. }
  336. if (lenp)
  337. *lenp = offset;
  338. return NULL;
  339. }
  340. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  341. int offset,
  342. const char *name,
  343. int namelen, int *lenp)
  344. {
  345. /* Prior to version 16, properties may need realignment
  346. * and this API does not work. fdt_getprop_*() will, however. */
  347. if (!can_assume(LATEST) && fdt_version(fdt) < 0x10) {
  348. if (lenp)
  349. *lenp = -FDT_ERR_BADVERSION;
  350. return NULL;
  351. }
  352. return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp,
  353. NULL);
  354. }
  355. const struct fdt_property *fdt_get_property(const void *fdt,
  356. int nodeoffset,
  357. const char *name, int *lenp)
  358. {
  359. return fdt_get_property_namelen(fdt, nodeoffset, name,
  360. strlen(name), lenp);
  361. }
  362. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  363. const char *name, int namelen, int *lenp)
  364. {
  365. int poffset;
  366. const struct fdt_property *prop;
  367. prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp,
  368. &poffset);
  369. if (!prop)
  370. return NULL;
  371. /* Handle realignment */
  372. if (!can_assume(LATEST) && fdt_version(fdt) < 0x10 &&
  373. (poffset + sizeof(*prop)) % 8 && fdt32_ld_(&prop->len) >= 8)
  374. return prop->data + 4;
  375. return prop->data;
  376. }
  377. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  378. const char **namep, int *lenp)
  379. {
  380. const struct fdt_property *prop;
  381. prop = fdt_get_property_by_offset_(fdt, offset, lenp);
  382. if (!prop)
  383. return NULL;
  384. if (namep) {
  385. const char *name;
  386. int namelen;
  387. if (!can_assume(VALID_INPUT)) {
  388. name = fdt_get_string(fdt, fdt32_ld_(&prop->nameoff),
  389. &namelen);
  390. if (!name) {
  391. if (lenp)
  392. *lenp = namelen;
  393. return NULL;
  394. }
  395. *namep = name;
  396. } else {
  397. *namep = fdt_string(fdt, fdt32_ld_(&prop->nameoff));
  398. }
  399. }
  400. /* Handle realignment */
  401. if (!can_assume(LATEST) && fdt_version(fdt) < 0x10 &&
  402. (offset + sizeof(*prop)) % 8 && fdt32_ld_(&prop->len) >= 8)
  403. return prop->data + 4;
  404. return prop->data;
  405. }
  406. const void *fdt_getprop(const void *fdt, int nodeoffset,
  407. const char *name, int *lenp)
  408. {
  409. return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
  410. }
  411. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
  412. {
  413. const fdt32_t *php;
  414. int len;
  415. /* FIXME: This is a bit sub-optimal, since we potentially scan
  416. * over all the properties twice. */
  417. php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
  418. if (!php || (len != sizeof(*php))) {
  419. php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
  420. if (!php || (len != sizeof(*php)))
  421. return 0;
  422. }
  423. return fdt32_ld_(php);
  424. }
  425. const char *fdt_get_alias_namelen(const void *fdt,
  426. const char *name, int namelen)
  427. {
  428. int aliasoffset;
  429. aliasoffset = fdt_path_offset(fdt, "/aliases");
  430. if (aliasoffset < 0)
  431. return NULL;
  432. return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
  433. }
  434. const char *fdt_get_alias(const void *fdt, const char *name)
  435. {
  436. return fdt_get_alias_namelen(fdt, name, strlen(name));
  437. }
  438. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
  439. {
  440. int pdepth = 0, p = 0;
  441. int offset, depth, namelen;
  442. const char *name;
  443. FDT_RO_PROBE(fdt);
  444. if (buflen < 2)
  445. return -FDT_ERR_NOSPACE;
  446. for (offset = 0, depth = 0;
  447. (offset >= 0) && (offset <= nodeoffset);
  448. offset = fdt_next_node(fdt, offset, &depth)) {
  449. while (pdepth > depth) {
  450. do {
  451. p--;
  452. } while (buf[p-1] != '/');
  453. pdepth--;
  454. }
  455. if (pdepth >= depth) {
  456. name = fdt_get_name(fdt, offset, &namelen);
  457. if (!name)
  458. return namelen;
  459. if ((p + namelen + 1) <= buflen) {
  460. memcpy(buf + p, name, namelen);
  461. p += namelen;
  462. buf[p++] = '/';
  463. pdepth++;
  464. }
  465. }
  466. if (offset == nodeoffset) {
  467. if (pdepth < (depth + 1))
  468. return -FDT_ERR_NOSPACE;
  469. if (p > 1) /* special case so that root path is "/", not "" */
  470. p--;
  471. buf[p] = '\0';
  472. return 0;
  473. }
  474. }
  475. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  476. return -FDT_ERR_BADOFFSET;
  477. else if (offset == -FDT_ERR_BADOFFSET)
  478. return -FDT_ERR_BADSTRUCTURE;
  479. return offset; /* error from fdt_next_node() */
  480. }
  481. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  482. int supernodedepth, int *nodedepth)
  483. {
  484. int offset, depth;
  485. int supernodeoffset = -FDT_ERR_INTERNAL;
  486. FDT_RO_PROBE(fdt);
  487. if (supernodedepth < 0)
  488. return -FDT_ERR_NOTFOUND;
  489. for (offset = 0, depth = 0;
  490. (offset >= 0) && (offset <= nodeoffset);
  491. offset = fdt_next_node(fdt, offset, &depth)) {
  492. if (depth == supernodedepth)
  493. supernodeoffset = offset;
  494. if (offset == nodeoffset) {
  495. if (nodedepth)
  496. *nodedepth = depth;
  497. if (supernodedepth > depth)
  498. return -FDT_ERR_NOTFOUND;
  499. else
  500. return supernodeoffset;
  501. }
  502. }
  503. if (!can_assume(VALID_INPUT)) {
  504. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  505. return -FDT_ERR_BADOFFSET;
  506. else if (offset == -FDT_ERR_BADOFFSET)
  507. return -FDT_ERR_BADSTRUCTURE;
  508. }
  509. return offset; /* error from fdt_next_node() */
  510. }
  511. int fdt_node_depth(const void *fdt, int nodeoffset)
  512. {
  513. int nodedepth;
  514. int err;
  515. err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
  516. if (err)
  517. return (can_assume(LIBFDT_FLAWLESS) || err < 0) ? err :
  518. -FDT_ERR_INTERNAL;
  519. return nodedepth;
  520. }
  521. int fdt_parent_offset(const void *fdt, int nodeoffset)
  522. {
  523. int nodedepth = fdt_node_depth(fdt, nodeoffset);
  524. if (nodedepth < 0)
  525. return nodedepth;
  526. return fdt_supernode_atdepth_offset(fdt, nodeoffset,
  527. nodedepth - 1, NULL);
  528. }
  529. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  530. const char *propname,
  531. const void *propval, int proplen)
  532. {
  533. int offset;
  534. const void *val;
  535. int len;
  536. FDT_RO_PROBE(fdt);
  537. /* FIXME: The algorithm here is pretty horrible: we scan each
  538. * property of a node in fdt_getprop(), then if that didn't
  539. * find what we want, we scan over them again making our way
  540. * to the next node. Still it's the easiest to implement
  541. * approach; performance can come later. */
  542. for (offset = fdt_next_node(fdt, startoffset, NULL);
  543. offset >= 0;
  544. offset = fdt_next_node(fdt, offset, NULL)) {
  545. val = fdt_getprop(fdt, offset, propname, &len);
  546. if (val && (len == proplen)
  547. && (memcmp(val, propval, len) == 0))
  548. return offset;
  549. }
  550. return offset; /* error from fdt_next_node() */
  551. }
  552. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
  553. {
  554. int offset;
  555. if ((phandle == 0) || (phandle == ~0U))
  556. return -FDT_ERR_BADPHANDLE;
  557. FDT_RO_PROBE(fdt);
  558. /* FIXME: The algorithm here is pretty horrible: we
  559. * potentially scan each property of a node in
  560. * fdt_get_phandle(), then if that didn't find what
  561. * we want, we scan over them again making our way to the next
  562. * node. Still it's the easiest to implement approach;
  563. * performance can come later. */
  564. for (offset = fdt_next_node(fdt, -1, NULL);
  565. offset >= 0;
  566. offset = fdt_next_node(fdt, offset, NULL)) {
  567. if (fdt_get_phandle(fdt, offset) == phandle)
  568. return offset;
  569. }
  570. return offset; /* error from fdt_next_node() */
  571. }
  572. int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
  573. {
  574. int len = strlen(str);
  575. const char *p;
  576. while (listlen >= len) {
  577. if (memcmp(str, strlist, len+1) == 0)
  578. return 1;
  579. p = memchr(strlist, '\0', listlen);
  580. if (!p)
  581. return 0; /* malformed strlist.. */
  582. listlen -= (p-strlist) + 1;
  583. strlist = p + 1;
  584. }
  585. return 0;
  586. }
  587. int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
  588. {
  589. const char *list, *end;
  590. int length, count = 0;
  591. list = fdt_getprop(fdt, nodeoffset, property, &length);
  592. if (!list)
  593. return length;
  594. end = list + length;
  595. while (list < end) {
  596. length = strnlen(list, end - list) + 1;
  597. /* Abort if the last string isn't properly NUL-terminated. */
  598. if (list + length > end)
  599. return -FDT_ERR_BADVALUE;
  600. list += length;
  601. count++;
  602. }
  603. return count;
  604. }
  605. int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
  606. const char *string)
  607. {
  608. int length, len, idx = 0;
  609. const char *list, *end;
  610. list = fdt_getprop(fdt, nodeoffset, property, &length);
  611. if (!list)
  612. return length;
  613. len = strlen(string) + 1;
  614. end = list + length;
  615. while (list < end) {
  616. length = strnlen(list, end - list) + 1;
  617. /* Abort if the last string isn't properly NUL-terminated. */
  618. if (list + length > end)
  619. return -FDT_ERR_BADVALUE;
  620. if (length == len && memcmp(list, string, length) == 0)
  621. return idx;
  622. list += length;
  623. idx++;
  624. }
  625. return -FDT_ERR_NOTFOUND;
  626. }
  627. const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
  628. const char *property, int idx,
  629. int *lenp)
  630. {
  631. const char *list, *end;
  632. int length;
  633. list = fdt_getprop(fdt, nodeoffset, property, &length);
  634. if (!list) {
  635. if (lenp)
  636. *lenp = length;
  637. return NULL;
  638. }
  639. end = list + length;
  640. while (list < end) {
  641. length = strnlen(list, end - list) + 1;
  642. /* Abort if the last string isn't properly NUL-terminated. */
  643. if (list + length > end) {
  644. if (lenp)
  645. *lenp = -FDT_ERR_BADVALUE;
  646. return NULL;
  647. }
  648. if (idx == 0) {
  649. if (lenp)
  650. *lenp = length - 1;
  651. return list;
  652. }
  653. list += length;
  654. idx--;
  655. }
  656. if (lenp)
  657. *lenp = -FDT_ERR_NOTFOUND;
  658. return NULL;
  659. }
  660. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  661. const char *compatible)
  662. {
  663. const void *prop;
  664. int len;
  665. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  666. if (!prop)
  667. return len;
  668. return !fdt_stringlist_contains(prop, len, compatible);
  669. }
  670. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  671. const char *compatible)
  672. {
  673. int offset, err;
  674. FDT_RO_PROBE(fdt);
  675. /* FIXME: The algorithm here is pretty horrible: we scan each
  676. * property of a node in fdt_node_check_compatible(), then if
  677. * that didn't find what we want, we scan over them again
  678. * making our way to the next node. Still it's the easiest to
  679. * implement approach; performance can come later. */
  680. for (offset = fdt_next_node(fdt, startoffset, NULL);
  681. offset >= 0;
  682. offset = fdt_next_node(fdt, offset, NULL)) {
  683. err = fdt_node_check_compatible(fdt, offset, compatible);
  684. if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
  685. return err;
  686. else if (err == 0)
  687. return offset;
  688. }
  689. return offset; /* error from fdt_next_node() */
  690. }