fdt_ro.c 19 KB

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