fdt_ro.c 19 KB

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