fdt_ro.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. * Return a pointer to the string at the given string offset.
  45. */
  46. char *fdt_string(const void *fdt, int stroffset)
  47. {
  48. return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
  49. }
  50. /*
  51. * Return the node offset of the node specified by:
  52. * parentoffset - starting place (0 to start at the root)
  53. * name - name being searched for
  54. * namelen - length of the name: typically strlen(name)
  55. *
  56. * Notes:
  57. * If the start node has subnodes, the subnodes are _not_ searched for the
  58. * requested name.
  59. */
  60. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  61. const char *name, int namelen)
  62. {
  63. int level = 0;
  64. uint32_t tag;
  65. int offset, nextoffset;
  66. CHECK_HEADER(fdt);
  67. tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
  68. if (tag != FDT_BEGIN_NODE)
  69. return -FDT_ERR_BADOFFSET;
  70. do {
  71. offset = nextoffset;
  72. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  73. switch (tag) {
  74. case FDT_END:
  75. return -FDT_ERR_TRUNCATED;
  76. case FDT_BEGIN_NODE:
  77. level++;
  78. /*
  79. * If we are nested down levels, ignore the strings
  80. * until we get back to the proper level.
  81. */
  82. if (level != 1)
  83. continue;
  84. /* Return the offset if this is "our" string. */
  85. if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
  86. return offset;
  87. break;
  88. case FDT_END_NODE:
  89. level--;
  90. break;
  91. case FDT_PROP:
  92. case FDT_NOP:
  93. break;
  94. default:
  95. return -FDT_ERR_BADSTRUCTURE;
  96. }
  97. } while (level >= 0);
  98. return -FDT_ERR_NOTFOUND;
  99. }
  100. /*
  101. * See fdt_subnode_offset_namelen()
  102. */
  103. int fdt_subnode_offset(const void *fdt, int parentoffset,
  104. const char *name)
  105. {
  106. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  107. }
  108. /*
  109. * Searches for the node corresponding to the given path and returns the
  110. * offset of that node.
  111. */
  112. int fdt_path_offset(const void *fdt, const char *path)
  113. {
  114. const char *end = path + strlen(path);
  115. const char *p = path;
  116. int offset = 0;
  117. CHECK_HEADER(fdt);
  118. /* Paths must be absolute */
  119. if (*path != '/')
  120. return -FDT_ERR_BADPATH;
  121. while (*p) {
  122. const char *q;
  123. /* Skip path separator(s) */
  124. while (*p == '/')
  125. p++;
  126. if (! *p)
  127. return -FDT_ERR_BADPATH;
  128. /*
  129. * Find the next path separator. The characters between
  130. * p and q are the next segment of the the path to find.
  131. */
  132. q = strchr(p, '/');
  133. if (! q)
  134. q = end;
  135. /*
  136. * Find the offset corresponding to the this path segment.
  137. */
  138. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  139. /* Oops, error, abort abort abort */
  140. if (offset < 0)
  141. return offset;
  142. p = q;
  143. }
  144. return offset;
  145. }
  146. /*
  147. * Given the offset of a node and a name of a property in that node, return
  148. * a pointer to the property struct.
  149. */
  150. struct fdt_property *fdt_get_property(const void *fdt,
  151. int nodeoffset,
  152. const char *name, int *lenp)
  153. {
  154. int level = 0;
  155. uint32_t tag;
  156. struct fdt_property *prop;
  157. int namestroff;
  158. int offset, nextoffset;
  159. int err;
  160. if ((err = fdt_check_header(fdt)) != 0)
  161. goto fail;
  162. err = -FDT_ERR_BADOFFSET;
  163. if (nodeoffset % FDT_TAGSIZE)
  164. goto fail;
  165. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
  166. if (tag != FDT_BEGIN_NODE)
  167. goto fail;
  168. do {
  169. offset = nextoffset;
  170. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  171. switch (tag) {
  172. case FDT_END:
  173. err = -FDT_ERR_TRUNCATED;
  174. goto fail;
  175. case FDT_BEGIN_NODE:
  176. level++;
  177. break;
  178. case FDT_END_NODE:
  179. level--;
  180. break;
  181. case FDT_PROP:
  182. /*
  183. * If we are nested down levels, ignore the strings
  184. * until we get back to the proper level.
  185. */
  186. if (level != 0)
  187. continue;
  188. err = -FDT_ERR_BADSTRUCTURE;
  189. prop = fdt_offset_ptr_typed(fdt, offset, prop);
  190. if (! prop)
  191. goto fail;
  192. namestroff = fdt32_to_cpu(prop->nameoff);
  193. if (streq(fdt_string(fdt, namestroff), name)) {
  194. /* Found it! */
  195. int len = fdt32_to_cpu(prop->len);
  196. prop = fdt_offset_ptr(fdt, offset,
  197. sizeof(*prop)+len);
  198. if (! prop)
  199. goto fail;
  200. if (lenp)
  201. *lenp = len;
  202. return prop;
  203. }
  204. break;
  205. case FDT_NOP:
  206. break;
  207. default:
  208. err = -FDT_ERR_BADSTRUCTURE;
  209. goto fail;
  210. }
  211. } while (level >= 0);
  212. err = -FDT_ERR_NOTFOUND;
  213. fail:
  214. if (lenp)
  215. *lenp = err;
  216. return NULL;
  217. }
  218. /*
  219. * Given the offset of a node and a name of a property in that node, return
  220. * a pointer to the property data (ONLY).
  221. */
  222. void *fdt_getprop(const void *fdt, int nodeoffset,
  223. const char *name, int *lenp)
  224. {
  225. const struct fdt_property *prop;
  226. prop = fdt_get_property(fdt, nodeoffset, name, lenp);
  227. if (! prop)
  228. return NULL;
  229. return (void *)prop->data;
  230. }
  231. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
  232. {
  233. const uint32_t *tagp, *lenp;
  234. uint32_t tag;
  235. const char *p;
  236. if (offset % FDT_TAGSIZE)
  237. return -1;
  238. tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
  239. if (! tagp)
  240. return FDT_END; /* premature end */
  241. tag = fdt32_to_cpu(*tagp);
  242. offset += FDT_TAGSIZE;
  243. switch (tag) {
  244. case FDT_BEGIN_NODE:
  245. if(namep)
  246. *namep = fdt_offset_ptr(fdt, offset, 1);
  247. /* skip name */
  248. do {
  249. p = fdt_offset_ptr(fdt, offset++, 1);
  250. } while (p && (*p != '\0'));
  251. if (! p)
  252. return FDT_END;
  253. break;
  254. case FDT_PROP:
  255. lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
  256. if (! lenp)
  257. return FDT_END;
  258. /*
  259. * Get the property and set the namep to the name.
  260. */
  261. if(namep) {
  262. struct fdt_property *prop;
  263. prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
  264. if (! prop)
  265. return -FDT_ERR_BADSTRUCTURE;
  266. *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  267. }
  268. /* skip name offset, length and value */
  269. offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
  270. break;
  271. }
  272. if (nextoffset)
  273. *nextoffset = ALIGN(offset, FDT_TAGSIZE);
  274. return tag;
  275. }
  276. /*
  277. * Return the number of used reserve map entries and total slots available.
  278. */
  279. int fdt_num_reservemap(void *fdt, int *used, int *total)
  280. {
  281. struct fdt_reserve_entry *re;
  282. int start;
  283. int end;
  284. int err = fdt_check_header(fdt);
  285. if (err != 0)
  286. return err;
  287. start = fdt_off_mem_rsvmap(fdt);
  288. /*
  289. * Convention is that the reserve map is before the dt_struct,
  290. * but it does not have to be.
  291. */
  292. end = fdt_totalsize(fdt);
  293. if (end > fdt_off_dt_struct(fdt))
  294. end = fdt_off_dt_struct(fdt);
  295. if (end > fdt_off_dt_strings(fdt))
  296. end = fdt_off_dt_strings(fdt);
  297. /*
  298. * Since the reserved area list is zero terminated, you get one fewer.
  299. */
  300. if (total)
  301. *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
  302. if (used) {
  303. *used = 0;
  304. while (start < end) {
  305. re = (struct fdt_reserve_entry *)(fdt + start);
  306. if (re->size == 0)
  307. return 0; /* zero size terminates the list */
  308. *used += 1;
  309. start += sizeof(struct fdt_reserve_entry);
  310. }
  311. /*
  312. * If we get here, there was no zero size termination.
  313. */
  314. return -FDT_ERR_BADLAYOUT;
  315. }
  316. return 0;
  317. }
  318. /*
  319. * Return the nth reserve map entry.
  320. */
  321. int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
  322. {
  323. int used;
  324. int total;
  325. int err;
  326. err = fdt_num_reservemap(fdt, &used, &total);
  327. if (err != 0)
  328. return err;
  329. if (n >= total)
  330. return -FDT_ERR_NOSPACE;
  331. if (re) {
  332. *re = *(struct fdt_reserve_entry *)
  333. _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));
  334. }
  335. return 0;
  336. }
  337. #endif /* CONFIG_OF_LIBFDT */