fdt_ro.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * libfdt - Flat Device Tree manipulation
  3. * Copyright (C) 2006 David Gibson, IBM Corporation.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 2.1 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "config.h"
  20. #if CONFIG_OF_LIBFDT
  21. #include "libfdt_env.h"
  22. #include <fdt.h>
  23. #include <libfdt.h>
  24. #include "libfdt_internal.h"
  25. #define CHECK_HEADER(fdt) { \
  26. int err; \
  27. if ((err = fdt_check_header(fdt)) != 0) \
  28. return err; \
  29. }
  30. static int offset_streq(const void *fdt, int offset,
  31. const char *s, int len)
  32. {
  33. const char *p = fdt_offset_ptr(fdt, offset, len+1);
  34. if (! p)
  35. /* short match */
  36. return 0;
  37. if (memcmp(p, s, len) != 0)
  38. return 0;
  39. if (p[len] != '\0')
  40. return 0;
  41. return 1;
  42. }
  43. /*
  44. * Checks if the property name matches.
  45. */
  46. static int prop_name_eq(const void *fdt, int offset, const char *name,
  47. struct fdt_property **prop, int *lenp)
  48. {
  49. int namestroff, len;
  50. *prop = fdt_offset_ptr_typed(fdt, offset, *prop);
  51. if (! *prop)
  52. return -FDT_ERR_BADSTRUCTURE;
  53. namestroff = fdt32_to_cpu((*prop)->nameoff);
  54. if (streq(fdt_string(fdt, namestroff), name)) {
  55. len = fdt32_to_cpu((*prop)->len);
  56. *prop = fdt_offset_ptr(fdt, offset,
  57. sizeof(**prop) + len);
  58. if (*prop) {
  59. if (lenp)
  60. *lenp = len;
  61. return 1;
  62. } else
  63. return -FDT_ERR_BADSTRUCTURE;
  64. }
  65. return 0;
  66. }
  67. /*
  68. * Return a pointer to the string at the given string offset.
  69. */
  70. char *fdt_string(const void *fdt, int stroffset)
  71. {
  72. return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
  73. }
  74. /*
  75. * Check if the specified node is compatible by comparing the tokens
  76. * in its "compatible" property with the specified string:
  77. *
  78. * nodeoffset - starting place of the node
  79. * compat - the string to match to one of the tokens in the
  80. * "compatible" list.
  81. */
  82. int fdt_node_is_compatible(const void *fdt, int nodeoffset,
  83. const char *compat)
  84. {
  85. const char* cp;
  86. int cplen, len;
  87. cp = fdt_getprop(fdt, nodeoffset, "compatible", &cplen);
  88. if (cp == NULL)
  89. return 0;
  90. while (cplen > 0) {
  91. if (strncmp(cp, compat, strlen(compat)) == 0)
  92. return 1;
  93. len = strlen(cp) + 1;
  94. cp += len;
  95. cplen -= len;
  96. }
  97. return 0;
  98. }
  99. /*
  100. * Find a node by its device type property. On success, the offset of that
  101. * node is returned or an error code otherwise:
  102. *
  103. * nodeoffset - the node to start searching from or 0, the node you pass
  104. * will not be searched, only the next one will; typically,
  105. * you pass 0 to start the search and then what the previous
  106. * call returned.
  107. * type - the device type string to match against.
  108. */
  109. int fdt_find_node_by_type(const void *fdt, int nodeoffset, const char *type)
  110. {
  111. int offset, nextoffset;
  112. struct fdt_property *prop;
  113. uint32_t tag;
  114. int len, ret;
  115. CHECK_HEADER(fdt);
  116. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
  117. if (tag != FDT_BEGIN_NODE)
  118. return -FDT_ERR_BADOFFSET;
  119. if (nodeoffset)
  120. nodeoffset = 0; /* start searching with next node */
  121. while (1) {
  122. offset = nextoffset;
  123. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  124. switch (tag) {
  125. case FDT_BEGIN_NODE:
  126. nodeoffset = offset;
  127. break;
  128. case FDT_PROP:
  129. if (nodeoffset == 0)
  130. break;
  131. ret = prop_name_eq(fdt, offset, "device_type",
  132. &prop, &len);
  133. if (ret < 0)
  134. return ret;
  135. else if (ret > 0 &&
  136. strncmp(prop->data, type, len - 1) == 0)
  137. return nodeoffset;
  138. break;
  139. case FDT_END_NODE:
  140. case FDT_NOP:
  141. break;
  142. case FDT_END:
  143. return -FDT_ERR_NOTFOUND;
  144. default:
  145. return -FDT_ERR_BADSTRUCTURE;
  146. }
  147. }
  148. }
  149. /*
  150. * Find a node based on its device type and one of the tokens in its its
  151. * "compatible" property. On success, the offset of that node is returned
  152. * or an error code otherwise:
  153. *
  154. * nodeoffset - the node to start searching from or 0, the node you pass
  155. * will not be searched, only the next one will; typically,
  156. * you pass 0 to start the search and then what the previous
  157. * call returned.
  158. * type - the device type string to match against.
  159. * compat - the string to match to one of the tokens in the
  160. * "compatible" list.
  161. */
  162. int fdt_find_compatible_node(const void *fdt, int nodeoffset,
  163. const char *type, const char *compat)
  164. {
  165. int offset;
  166. offset = fdt_find_node_by_type(fdt, nodeoffset, type);
  167. if (offset < 0 || fdt_node_is_compatible(fdt, offset, compat))
  168. return offset;
  169. return -FDT_ERR_NOTFOUND;
  170. }
  171. /*
  172. * Return the node offset of the node specified by:
  173. * parentoffset - starting place (0 to start at the root)
  174. * name - name being searched for
  175. * namelen - length of the name: typically strlen(name)
  176. *
  177. * Notes:
  178. * If the start node has subnodes, the subnodes are _not_ searched for the
  179. * requested name.
  180. */
  181. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  182. const char *name, int namelen)
  183. {
  184. int level = 0;
  185. uint32_t tag;
  186. int offset, nextoffset;
  187. CHECK_HEADER(fdt);
  188. tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
  189. if (tag != FDT_BEGIN_NODE)
  190. return -FDT_ERR_BADOFFSET;
  191. do {
  192. offset = nextoffset;
  193. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  194. switch (tag) {
  195. case FDT_END:
  196. return -FDT_ERR_TRUNCATED;
  197. case FDT_BEGIN_NODE:
  198. level++;
  199. /*
  200. * If we are nested down levels, ignore the strings
  201. * until we get back to the proper level.
  202. */
  203. if (level != 1)
  204. continue;
  205. /* Return the offset if this is "our" string. */
  206. if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
  207. return offset;
  208. break;
  209. case FDT_END_NODE:
  210. level--;
  211. break;
  212. case FDT_PROP:
  213. case FDT_NOP:
  214. break;
  215. default:
  216. return -FDT_ERR_BADSTRUCTURE;
  217. }
  218. } while (level >= 0);
  219. return -FDT_ERR_NOTFOUND;
  220. }
  221. /*
  222. * See fdt_subnode_offset_namelen()
  223. */
  224. int fdt_subnode_offset(const void *fdt, int parentoffset,
  225. const char *name)
  226. {
  227. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  228. }
  229. /*
  230. * Searches for the node corresponding to the given path and returns the
  231. * offset of that node.
  232. */
  233. int fdt_find_node_by_path(const void *fdt, const char *path)
  234. {
  235. const char *end = path + strlen(path);
  236. const char *p = path;
  237. int offset = 0;
  238. CHECK_HEADER(fdt);
  239. /* Paths must be absolute */
  240. if (*path != '/')
  241. return -FDT_ERR_BADPATH;
  242. /* Handle the root path: root offset is 0 */
  243. if (strcmp(path, "/") == 0)
  244. return 0;
  245. while (*p) {
  246. const char *q;
  247. /* Skip path separator(s) */
  248. while (*p == '/')
  249. p++;
  250. if (! *p)
  251. return -FDT_ERR_BADPATH;
  252. /*
  253. * Find the next path separator. The characters between
  254. * p and q are the next segment of the the path to find.
  255. */
  256. q = strchr(p, '/');
  257. if (! q)
  258. q = end;
  259. /*
  260. * Find the offset corresponding to the this path segment.
  261. */
  262. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  263. /* Oops, error, abort abort abort */
  264. if (offset < 0)
  265. return offset;
  266. p = q;
  267. }
  268. return offset;
  269. }
  270. /*
  271. * Given the offset of a node and a name of a property in that node, return
  272. * a pointer to the property struct.
  273. */
  274. struct fdt_property *fdt_get_property(const void *fdt,
  275. int nodeoffset,
  276. const char *name, int *lenp)
  277. {
  278. int level = 0;
  279. uint32_t tag;
  280. struct fdt_property *prop;
  281. int offset, nextoffset;
  282. int err;
  283. if ((err = fdt_check_header(fdt)) != 0)
  284. goto fail;
  285. err = -FDT_ERR_BADOFFSET;
  286. if (nodeoffset % FDT_TAGSIZE)
  287. goto fail;
  288. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
  289. if (tag != FDT_BEGIN_NODE)
  290. goto fail;
  291. do {
  292. offset = nextoffset;
  293. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  294. switch (tag) {
  295. case FDT_END:
  296. err = -FDT_ERR_TRUNCATED;
  297. goto fail;
  298. case FDT_BEGIN_NODE:
  299. level++;
  300. break;
  301. case FDT_END_NODE:
  302. level--;
  303. break;
  304. case FDT_PROP:
  305. /*
  306. * If we are nested down levels, ignore the strings
  307. * until we get back to the proper level.
  308. */
  309. if (level != 0)
  310. continue;
  311. err = prop_name_eq(fdt, offset, name, &prop, lenp);
  312. if (err > 0)
  313. return prop;
  314. else if (err < 0)
  315. goto fail;
  316. break;
  317. case FDT_NOP:
  318. break;
  319. default:
  320. err = -FDT_ERR_BADSTRUCTURE;
  321. goto fail;
  322. }
  323. } while (level >= 0);
  324. err = -FDT_ERR_NOTFOUND;
  325. fail:
  326. if (lenp)
  327. *lenp = err;
  328. return NULL;
  329. }
  330. /*
  331. * Given the offset of a node and a name of a property in that node, return
  332. * a pointer to the property data (ONLY).
  333. */
  334. void *fdt_getprop(const void *fdt, int nodeoffset,
  335. const char *name, int *lenp)
  336. {
  337. const struct fdt_property *prop;
  338. prop = fdt_get_property(fdt, nodeoffset, name, lenp);
  339. if (! prop)
  340. return NULL;
  341. return (void *)prop->data;
  342. }
  343. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
  344. {
  345. const uint32_t *tagp, *lenp;
  346. uint32_t tag;
  347. const char *p;
  348. if (offset % FDT_TAGSIZE)
  349. return -1;
  350. tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
  351. if (! tagp)
  352. return FDT_END; /* premature end */
  353. tag = fdt32_to_cpu(*tagp);
  354. offset += FDT_TAGSIZE;
  355. switch (tag) {
  356. case FDT_BEGIN_NODE:
  357. if(namep)
  358. *namep = fdt_offset_ptr(fdt, offset, 1);
  359. /* skip name */
  360. do {
  361. p = fdt_offset_ptr(fdt, offset++, 1);
  362. } while (p && (*p != '\0'));
  363. if (! p)
  364. return FDT_END;
  365. break;
  366. case FDT_PROP:
  367. lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
  368. if (! lenp)
  369. return FDT_END;
  370. /*
  371. * Get the property and set the namep to the name.
  372. */
  373. if(namep) {
  374. struct fdt_property *prop;
  375. prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
  376. if (! prop)
  377. return -FDT_ERR_BADSTRUCTURE;
  378. *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  379. }
  380. /* skip name offset, length and value */
  381. offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
  382. break;
  383. }
  384. if (nextoffset)
  385. *nextoffset = ALIGN(offset, FDT_TAGSIZE);
  386. return tag;
  387. }
  388. /*
  389. * Return the number of used reserve map entries and total slots available.
  390. */
  391. int fdt_num_reservemap(void *fdt, int *used, int *total)
  392. {
  393. struct fdt_reserve_entry *re;
  394. int start;
  395. int end;
  396. int err = fdt_check_header(fdt);
  397. if (err != 0)
  398. return err;
  399. start = fdt_off_mem_rsvmap(fdt);
  400. /*
  401. * Convention is that the reserve map is before the dt_struct,
  402. * but it does not have to be.
  403. */
  404. end = fdt_totalsize(fdt);
  405. if (end > fdt_off_dt_struct(fdt))
  406. end = fdt_off_dt_struct(fdt);
  407. if (end > fdt_off_dt_strings(fdt))
  408. end = fdt_off_dt_strings(fdt);
  409. /*
  410. * Since the reserved area list is zero terminated, you get one fewer.
  411. */
  412. if (total)
  413. *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
  414. if (used) {
  415. *used = 0;
  416. while (start < end) {
  417. re = (struct fdt_reserve_entry *)(fdt + start);
  418. if (re->size == 0)
  419. return 0; /* zero size terminates the list */
  420. *used += 1;
  421. start += sizeof(struct fdt_reserve_entry);
  422. }
  423. /*
  424. * If we get here, there was no zero size termination.
  425. */
  426. return -FDT_ERR_BADLAYOUT;
  427. }
  428. return 0;
  429. }
  430. /*
  431. * Return the nth reserve map entry.
  432. */
  433. int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
  434. {
  435. int used;
  436. int total;
  437. int err;
  438. err = fdt_num_reservemap(fdt, &used, &total);
  439. if (err != 0)
  440. return err;
  441. if (n >= total)
  442. return -FDT_ERR_NOSPACE;
  443. if (re) {
  444. *re = *(struct fdt_reserve_entry *)
  445. _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));
  446. }
  447. return 0;
  448. }
  449. #endif /* CONFIG_OF_LIBFDT */