fdt_ro.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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 || (fdt_chk_extra() && 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 (!fdt_chk_extra()) {
  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 (!fdt_chk_version() || 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 (fdt_chk_extra()) {
  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 (fdt_chk_extra() && !re)
  147. return -FDT_ERR_BADOFFSET;
  148. *address = fdt64_to_cpu(re->address);
  149. *size = fdt64_to_cpu(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_to_cpu(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 (fdt_chk_extra() &&
  245. (((err = fdt_ro_probe_(fdt)) < 0)
  246. || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0)))
  247. goto fail;
  248. nameptr = nh->name;
  249. if (fdt_chk_version() && fdt_version(fdt) < 0x10) {
  250. /*
  251. * For old FDT versions, match the naming conventions of V16:
  252. * give only the leaf name (after all /). The actual tree
  253. * contents are loosely checked.
  254. */
  255. const char *leaf;
  256. leaf = strrchr(nameptr, '/');
  257. if (leaf == NULL) {
  258. err = -FDT_ERR_BADSTRUCTURE;
  259. goto fail;
  260. }
  261. nameptr = leaf+1;
  262. }
  263. if (len)
  264. *len = strlen(nameptr);
  265. return nameptr;
  266. fail:
  267. if (len)
  268. *len = err;
  269. return NULL;
  270. }
  271. int fdt_first_property_offset(const void *fdt, int nodeoffset)
  272. {
  273. int offset;
  274. if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
  275. return offset;
  276. return nextprop_(fdt, offset);
  277. }
  278. int fdt_next_property_offset(const void *fdt, int offset)
  279. {
  280. if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
  281. return offset;
  282. return nextprop_(fdt, offset);
  283. }
  284. static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
  285. int offset,
  286. int *lenp)
  287. {
  288. int err;
  289. const struct fdt_property *prop;
  290. if (fdt_chk_basic() && (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_to_cpu(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 (fdt_chk_version() && 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 (fdt_chk_extra() && !prop) {
  326. offset = -FDT_ERR_INTERNAL;
  327. break;
  328. }
  329. if (fdt_string_eq_(fdt, fdt32_to_cpu(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 (fdt_chk_version() && 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 (fdt_chk_version() && fdt_version(fdt) < 0x10 &&
  373. (poffset + sizeof(*prop)) % 8 && fdt32_to_cpu(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 (fdt_chk_extra()) {
  388. name = fdt_get_string(fdt, fdt32_to_cpu(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_to_cpu(prop->nameoff));
  398. }
  399. }
  400. /* Handle realignment */
  401. if (fdt_chk_version() && fdt_version(fdt) < 0x10 &&
  402. (offset + sizeof(*prop)) % 8 && fdt32_to_cpu(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_to_cpu(*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 (fdt_chk_extra()) {
  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 (!fdt_chk_extra() || err < 0) ? err : -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 == ~0U))
  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. }
  690. #if !defined(FDT_ASSUME_MASK) || FDT_ASSUME_MASK != 0xff
  691. int fdt_check_full(const void *fdt, size_t bufsize)
  692. {
  693. int err;
  694. int num_memrsv;
  695. int offset, nextoffset = 0;
  696. uint32_t tag;
  697. unsigned depth = 0;
  698. const void *prop;
  699. const char *propname;
  700. if (bufsize < FDT_V1_SIZE)
  701. return -FDT_ERR_TRUNCATED;
  702. err = fdt_check_header(fdt);
  703. if (err != 0)
  704. return err;
  705. if (bufsize < fdt_totalsize(fdt))
  706. return -FDT_ERR_TRUNCATED;
  707. num_memrsv = fdt_num_mem_rsv(fdt);
  708. if (num_memrsv < 0)
  709. return num_memrsv;
  710. while (1) {
  711. offset = nextoffset;
  712. tag = fdt_next_tag(fdt, offset, &nextoffset);
  713. if (nextoffset < 0)
  714. return nextoffset;
  715. switch (tag) {
  716. case FDT_NOP:
  717. break;
  718. case FDT_END:
  719. if (depth != 0)
  720. return -FDT_ERR_BADSTRUCTURE;
  721. return 0;
  722. case FDT_BEGIN_NODE:
  723. depth++;
  724. if (depth > INT_MAX)
  725. return -FDT_ERR_BADSTRUCTURE;
  726. break;
  727. case FDT_END_NODE:
  728. if (depth == 0)
  729. return -FDT_ERR_BADSTRUCTURE;
  730. depth--;
  731. break;
  732. case FDT_PROP:
  733. prop = fdt_getprop_by_offset(fdt, offset, &propname,
  734. &err);
  735. if (!prop)
  736. return err;
  737. break;
  738. default:
  739. return -FDT_ERR_INTERNAL;
  740. }
  741. }
  742. }
  743. #endif