fdt_ro.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
  18. if (!p)
  19. /* short match */
  20. return 0;
  21. if (memcmp(p, s, len) != 0)
  22. return 0;
  23. if (p[len] == '\0')
  24. return 1;
  25. else if (!memchr(s, '@', len) && (p[len] == '@'))
  26. return 1;
  27. else
  28. return 0;
  29. }
  30. const char *fdt_string(const void *fdt, int stroffset)
  31. {
  32. return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
  33. }
  34. static int _fdt_string_eq(const void *fdt, int stroffset,
  35. const char *s, int len)
  36. {
  37. const char *p = fdt_string(fdt, stroffset);
  38. return (strnlen(p, len + 1) == len) && (memcmp(p, s, len) == 0);
  39. }
  40. uint32_t fdt_get_max_phandle(const void *fdt)
  41. {
  42. uint32_t max_phandle = 0;
  43. int offset;
  44. for (offset = fdt_next_node(fdt, -1, NULL);;
  45. offset = fdt_next_node(fdt, offset, NULL)) {
  46. uint32_t phandle;
  47. if (offset == -FDT_ERR_NOTFOUND)
  48. return max_phandle;
  49. if (offset < 0)
  50. return (uint32_t)-1;
  51. phandle = fdt_get_phandle(fdt, offset);
  52. if (phandle == (uint32_t)-1)
  53. continue;
  54. if (phandle > max_phandle)
  55. max_phandle = phandle;
  56. }
  57. return 0;
  58. }
  59. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
  60. {
  61. FDT_CHECK_HEADER(fdt);
  62. *address = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->address);
  63. *size = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->size);
  64. return 0;
  65. }
  66. int fdt_num_mem_rsv(const void *fdt)
  67. {
  68. int i = 0;
  69. while (fdt64_to_cpu(fdt_mem_rsv_(fdt, i)->size) != 0)
  70. i++;
  71. return i;
  72. }
  73. static int _nextprop(const void *fdt, int offset)
  74. {
  75. uint32_t tag;
  76. int nextoffset;
  77. do {
  78. tag = fdt_next_tag(fdt, offset, &nextoffset);
  79. switch (tag) {
  80. case FDT_END:
  81. if (nextoffset >= 0)
  82. return -FDT_ERR_BADSTRUCTURE;
  83. else
  84. return nextoffset;
  85. case FDT_PROP:
  86. return offset;
  87. }
  88. offset = nextoffset;
  89. } while (tag == FDT_NOP);
  90. return -FDT_ERR_NOTFOUND;
  91. }
  92. int fdt_subnode_offset_namelen(const void *fdt, int offset,
  93. const char *name, int namelen)
  94. {
  95. int depth;
  96. FDT_CHECK_HEADER(fdt);
  97. for (depth = 0;
  98. (offset >= 0) && (depth >= 0);
  99. offset = fdt_next_node(fdt, offset, &depth))
  100. if ((depth == 1)
  101. && _fdt_nodename_eq(fdt, offset, name, namelen))
  102. return offset;
  103. if (depth < 0)
  104. return -FDT_ERR_NOTFOUND;
  105. return offset; /* error */
  106. }
  107. int fdt_subnode_offset(const void *fdt, int parentoffset,
  108. const char *name)
  109. {
  110. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  111. }
  112. /*
  113. * Find the next of path separator, note we need to search for both '/' and ':'
  114. * and then take the first one so that we do the right thing for e.g.
  115. * "foo/bar:option" and "bar:option/otheroption", both of which happen, so
  116. * first searching for either ':' or '/' does not work.
  117. */
  118. static const char *fdt_path_next_separator(const char *path, int len)
  119. {
  120. const void *sep1 = memchr(path, '/', len);
  121. const void *sep2 = memchr(path, ':', len);
  122. if (sep1 && sep2)
  123. return (sep1 < sep2) ? sep1 : sep2;
  124. else if (sep1)
  125. return sep1;
  126. else
  127. return sep2;
  128. }
  129. int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
  130. {
  131. const char *end = path + namelen;
  132. const char *p = path;
  133. int offset = 0;
  134. FDT_CHECK_HEADER(fdt);
  135. /* see if we have an alias */
  136. if (*path != '/') {
  137. const char *q = fdt_path_next_separator(path, namelen);
  138. if (!q)
  139. q = end;
  140. p = fdt_get_alias_namelen(fdt, p, q - p);
  141. if (!p)
  142. return -FDT_ERR_BADPATH;
  143. offset = fdt_path_offset(fdt, p);
  144. p = q;
  145. }
  146. while (*p && (p < end)) {
  147. const char *q;
  148. while (*p == '/')
  149. p++;
  150. if (*p == '\0' || *p == ':')
  151. return offset;
  152. q = fdt_path_next_separator(p, end - p);
  153. if (!q)
  154. q = end;
  155. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  156. if (offset < 0)
  157. return offset;
  158. p = q;
  159. }
  160. return offset;
  161. }
  162. int fdt_path_offset(const void *fdt, const char *path)
  163. {
  164. return fdt_path_offset_namelen(fdt, path, strlen(path));
  165. }
  166. const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
  167. {
  168. const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset);
  169. int err;
  170. if (((err = fdt_check_header(fdt)) != 0)
  171. || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))
  172. goto fail;
  173. if (len)
  174. *len = strlen(nh->name);
  175. return nh->name;
  176. fail:
  177. if (len)
  178. *len = err;
  179. return NULL;
  180. }
  181. int fdt_first_property_offset(const void *fdt, int nodeoffset)
  182. {
  183. int offset;
  184. if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
  185. return offset;
  186. return _nextprop(fdt, offset);
  187. }
  188. int fdt_next_property_offset(const void *fdt, int offset)
  189. {
  190. if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0)
  191. return offset;
  192. return _nextprop(fdt, offset);
  193. }
  194. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  195. int offset,
  196. int *lenp)
  197. {
  198. int err;
  199. const struct fdt_property *prop;
  200. if ((err = fdt_check_prop_offset_(fdt, offset)) < 0) {
  201. if (lenp)
  202. *lenp = err;
  203. return NULL;
  204. }
  205. prop = fdt_offset_ptr_(fdt, offset);
  206. if (lenp)
  207. *lenp = fdt32_to_cpu(prop->len);
  208. return prop;
  209. }
  210. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  211. int offset,
  212. const char *name,
  213. int namelen, int *lenp)
  214. {
  215. for (offset = fdt_first_property_offset(fdt, offset);
  216. (offset >= 0);
  217. (offset = fdt_next_property_offset(fdt, offset))) {
  218. const struct fdt_property *prop;
  219. if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
  220. offset = -FDT_ERR_INTERNAL;
  221. break;
  222. }
  223. if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
  224. name, namelen))
  225. return prop;
  226. }
  227. if (lenp)
  228. *lenp = offset;
  229. return NULL;
  230. }
  231. const struct fdt_property *fdt_get_property(const void *fdt,
  232. int nodeoffset,
  233. const char *name, int *lenp)
  234. {
  235. return fdt_get_property_namelen(fdt, nodeoffset, name,
  236. strlen(name), lenp);
  237. }
  238. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  239. const char *name, int namelen, int *lenp)
  240. {
  241. const struct fdt_property *prop;
  242. prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
  243. if (!prop)
  244. return NULL;
  245. return prop->data;
  246. }
  247. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  248. const char **namep, int *lenp)
  249. {
  250. const struct fdt_property *prop;
  251. prop = fdt_get_property_by_offset(fdt, offset, lenp);
  252. if (!prop)
  253. return NULL;
  254. if (namep)
  255. *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  256. return prop->data;
  257. }
  258. const void *fdt_getprop(const void *fdt, int nodeoffset,
  259. const char *name, int *lenp)
  260. {
  261. return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
  262. }
  263. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
  264. {
  265. const fdt32_t *php;
  266. int len;
  267. /* FIXME: This is a bit sub-optimal, since we potentially scan
  268. * over all the properties twice. */
  269. php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
  270. if (!php || (len != sizeof(*php))) {
  271. php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
  272. if (!php || (len != sizeof(*php)))
  273. return 0;
  274. }
  275. return fdt32_to_cpu(*php);
  276. }
  277. const char *fdt_get_alias_namelen(const void *fdt,
  278. const char *name, int namelen)
  279. {
  280. int aliasoffset;
  281. aliasoffset = fdt_path_offset(fdt, "/aliases");
  282. if (aliasoffset < 0)
  283. return NULL;
  284. return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
  285. }
  286. const char *fdt_get_alias(const void *fdt, const char *name)
  287. {
  288. return fdt_get_alias_namelen(fdt, name, strlen(name));
  289. }
  290. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
  291. {
  292. int pdepth = 0, p = 0;
  293. int offset, depth, namelen;
  294. const char *name;
  295. FDT_CHECK_HEADER(fdt);
  296. if (buflen < 2)
  297. return -FDT_ERR_NOSPACE;
  298. for (offset = 0, depth = 0;
  299. (offset >= 0) && (offset <= nodeoffset);
  300. offset = fdt_next_node(fdt, offset, &depth)) {
  301. while (pdepth > depth) {
  302. do {
  303. p--;
  304. } while (buf[p-1] != '/');
  305. pdepth--;
  306. }
  307. if (pdepth >= depth) {
  308. name = fdt_get_name(fdt, offset, &namelen);
  309. if (!name)
  310. return namelen;
  311. if ((p + namelen + 1) <= buflen) {
  312. memcpy(buf + p, name, namelen);
  313. p += namelen;
  314. buf[p++] = '/';
  315. pdepth++;
  316. }
  317. }
  318. if (offset == nodeoffset) {
  319. if (pdepth < (depth + 1))
  320. return -FDT_ERR_NOSPACE;
  321. if (p > 1) /* special case so that root path is "/", not "" */
  322. p--;
  323. buf[p] = '\0';
  324. return 0;
  325. }
  326. }
  327. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  328. return -FDT_ERR_BADOFFSET;
  329. else if (offset == -FDT_ERR_BADOFFSET)
  330. return -FDT_ERR_BADSTRUCTURE;
  331. return offset; /* error from fdt_next_node() */
  332. }
  333. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  334. int supernodedepth, int *nodedepth)
  335. {
  336. int offset, depth;
  337. int supernodeoffset = -FDT_ERR_INTERNAL;
  338. FDT_CHECK_HEADER(fdt);
  339. if (supernodedepth < 0)
  340. return -FDT_ERR_NOTFOUND;
  341. for (offset = 0, depth = 0;
  342. (offset >= 0) && (offset <= nodeoffset);
  343. offset = fdt_next_node(fdt, offset, &depth)) {
  344. if (depth == supernodedepth)
  345. supernodeoffset = offset;
  346. if (offset == nodeoffset) {
  347. if (nodedepth)
  348. *nodedepth = depth;
  349. if (supernodedepth > depth)
  350. return -FDT_ERR_NOTFOUND;
  351. else
  352. return supernodeoffset;
  353. }
  354. }
  355. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  356. return -FDT_ERR_BADOFFSET;
  357. else if (offset == -FDT_ERR_BADOFFSET)
  358. return -FDT_ERR_BADSTRUCTURE;
  359. return offset; /* error from fdt_next_node() */
  360. }
  361. int fdt_node_depth(const void *fdt, int nodeoffset)
  362. {
  363. int nodedepth;
  364. int err;
  365. err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
  366. if (err)
  367. return (err < 0) ? err : -FDT_ERR_INTERNAL;
  368. return nodedepth;
  369. }
  370. int fdt_parent_offset(const void *fdt, int nodeoffset)
  371. {
  372. int nodedepth = fdt_node_depth(fdt, nodeoffset);
  373. if (nodedepth < 0)
  374. return nodedepth;
  375. return fdt_supernode_atdepth_offset(fdt, nodeoffset,
  376. nodedepth - 1, NULL);
  377. }
  378. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  379. const char *propname,
  380. const void *propval, int proplen)
  381. {
  382. int offset;
  383. const void *val;
  384. int len;
  385. FDT_CHECK_HEADER(fdt);
  386. /* FIXME: The algorithm here is pretty horrible: we scan each
  387. * property of a node in fdt_getprop(), then if that didn't
  388. * find what we want, we scan over them again making our way
  389. * to the next node. Still it's the easiest to implement
  390. * approach; performance can come later. */
  391. for (offset = fdt_next_node(fdt, startoffset, NULL);
  392. offset >= 0;
  393. offset = fdt_next_node(fdt, offset, NULL)) {
  394. val = fdt_getprop(fdt, offset, propname, &len);
  395. if (val && (len == proplen)
  396. && (memcmp(val, propval, len) == 0))
  397. return offset;
  398. }
  399. return offset; /* error from fdt_next_node() */
  400. }
  401. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
  402. {
  403. int offset;
  404. if ((phandle == 0) || (phandle == -1))
  405. return -FDT_ERR_BADPHANDLE;
  406. FDT_CHECK_HEADER(fdt);
  407. /* FIXME: The algorithm here is pretty horrible: we
  408. * potentially scan each property of a node in
  409. * fdt_get_phandle(), then if that didn't find what
  410. * we want, we scan over them again making our way to the next
  411. * node. Still it's the easiest to implement approach;
  412. * performance can come later. */
  413. for (offset = fdt_next_node(fdt, -1, NULL);
  414. offset >= 0;
  415. offset = fdt_next_node(fdt, offset, NULL)) {
  416. if (fdt_get_phandle(fdt, offset) == phandle)
  417. return offset;
  418. }
  419. return offset; /* error from fdt_next_node() */
  420. }
  421. int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
  422. {
  423. int len = strlen(str);
  424. const char *p;
  425. while (listlen >= len) {
  426. if (memcmp(str, strlist, len+1) == 0)
  427. return 1;
  428. p = memchr(strlist, '\0', listlen);
  429. if (!p)
  430. return 0; /* malformed strlist.. */
  431. listlen -= (p-strlist) + 1;
  432. strlist = p + 1;
  433. }
  434. return 0;
  435. }
  436. int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property)
  437. {
  438. const char *list, *end;
  439. int length, count = 0;
  440. list = fdt_getprop(fdt, nodeoffset, property, &length);
  441. if (!list)
  442. return length;
  443. end = list + length;
  444. while (list < end) {
  445. length = strnlen(list, end - list) + 1;
  446. /* Abort if the last string isn't properly NUL-terminated. */
  447. if (list + length > end)
  448. return -FDT_ERR_BADVALUE;
  449. list += length;
  450. count++;
  451. }
  452. return count;
  453. }
  454. int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
  455. const char *string)
  456. {
  457. int length, len, idx = 0;
  458. const char *list, *end;
  459. list = fdt_getprop(fdt, nodeoffset, property, &length);
  460. if (!list)
  461. return length;
  462. len = strlen(string) + 1;
  463. end = list + length;
  464. while (list < end) {
  465. length = strnlen(list, end - list) + 1;
  466. /* Abort if the last string isn't properly NUL-terminated. */
  467. if (list + length > end)
  468. return -FDT_ERR_BADVALUE;
  469. if (length == len && memcmp(list, string, length) == 0)
  470. return idx;
  471. list += length;
  472. idx++;
  473. }
  474. return -FDT_ERR_NOTFOUND;
  475. }
  476. const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
  477. const char *property, int idx,
  478. int *lenp)
  479. {
  480. const char *list, *end;
  481. int length;
  482. list = fdt_getprop(fdt, nodeoffset, property, &length);
  483. if (!list) {
  484. if (lenp)
  485. *lenp = length;
  486. return NULL;
  487. }
  488. end = list + length;
  489. while (list < end) {
  490. length = strnlen(list, end - list) + 1;
  491. /* Abort if the last string isn't properly NUL-terminated. */
  492. if (list + length > end) {
  493. if (lenp)
  494. *lenp = -FDT_ERR_BADVALUE;
  495. return NULL;
  496. }
  497. if (idx == 0) {
  498. if (lenp)
  499. *lenp = length - 1;
  500. return list;
  501. }
  502. list += length;
  503. idx--;
  504. }
  505. if (lenp)
  506. *lenp = -FDT_ERR_NOTFOUND;
  507. return NULL;
  508. }
  509. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  510. const char *compatible)
  511. {
  512. const void *prop;
  513. int len;
  514. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  515. if (!prop)
  516. return len;
  517. return !fdt_stringlist_contains(prop, len, compatible);
  518. }
  519. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  520. const char *compatible)
  521. {
  522. int offset, err;
  523. FDT_CHECK_HEADER(fdt);
  524. /* FIXME: The algorithm here is pretty horrible: we scan each
  525. * property of a node in fdt_node_check_compatible(), then if
  526. * that didn't find what we want, we scan over them again
  527. * making our way to the next node. Still it's the easiest to
  528. * implement approach; performance can come later. */
  529. for (offset = fdt_next_node(fdt, startoffset, NULL);
  530. offset >= 0;
  531. offset = fdt_next_node(fdt, offset, NULL)) {
  532. err = fdt_node_check_compatible(fdt, offset, compatible);
  533. if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
  534. return err;
  535. else if (err == 0)
  536. return offset;
  537. }
  538. return offset; /* error from fdt_next_node() */
  539. }