fdt_ro.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "libfdt_env.h"
  20. #include <fdt.h>
  21. #include <libfdt.h>
  22. #include "libfdt_internal.h"
  23. #define CHECK_HEADER(fdt) \
  24. { \
  25. int err; \
  26. if ((err = _fdt_check_header(fdt)) != 0) \
  27. return err; \
  28. }
  29. static int offset_streq(const void *fdt, int offset,
  30. const char *s, int len)
  31. {
  32. const char *p = fdt_offset_ptr(fdt, offset, len+1);
  33. if (! p)
  34. /* short match */
  35. return 0;
  36. if (memcmp(p, s, len) != 0)
  37. return 0;
  38. if (p[len] != '\0')
  39. return 0;
  40. return 1;
  41. }
  42. /*
  43. * Return a pointer to the string at the given string offset.
  44. */
  45. char *fdt_string(const void *fdt, int stroffset)
  46. {
  47. return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
  48. }
  49. /*
  50. * Return the node offset of the node specified by:
  51. * parentoffset - starting place (0 to start at the root)
  52. * name - name being searched for
  53. * namelen - length of the name: typically strlen(name)
  54. *
  55. * Notes:
  56. * If the start node has subnodes, the subnodes are _not_ searched for the
  57. * requested name.
  58. */
  59. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  60. const char *name, int namelen)
  61. {
  62. int level = 0;
  63. uint32_t tag;
  64. int offset, nextoffset;
  65. CHECK_HEADER(fdt);
  66. tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
  67. if (tag != FDT_BEGIN_NODE)
  68. return -FDT_ERR_BADOFFSET;
  69. do {
  70. offset = nextoffset;
  71. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  72. switch (tag) {
  73. case FDT_END:
  74. return -FDT_ERR_TRUNCATED;
  75. case FDT_BEGIN_NODE:
  76. level++;
  77. /*
  78. * If we are nested down levels, ignore the strings
  79. * until we get back to the proper level.
  80. */
  81. if (level != 1)
  82. continue;
  83. /* Return the offset if this is "our" string. */
  84. if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
  85. return offset;
  86. break;
  87. case FDT_END_NODE:
  88. level--;
  89. break;
  90. case FDT_PROP:
  91. case FDT_NOP:
  92. break;
  93. default:
  94. return -FDT_ERR_BADSTRUCTURE;
  95. }
  96. } while (level >= 0);
  97. return -FDT_ERR_NOTFOUND;
  98. }
  99. /*
  100. * See fdt_subnode_offset_namelen()
  101. */
  102. int fdt_subnode_offset(const void *fdt, int parentoffset,
  103. const char *name)
  104. {
  105. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  106. }
  107. /*
  108. * Searches for the node corresponding to the given path and returns the
  109. * offset of that node.
  110. */
  111. int fdt_path_offset(const void *fdt, const char *path)
  112. {
  113. const char *end = path + strlen(path);
  114. const char *p = path;
  115. int offset = 0;
  116. CHECK_HEADER(fdt);
  117. /* Paths must be absolute */
  118. if (*path != '/')
  119. return -FDT_ERR_BADPATH;
  120. while (*p) {
  121. const char *q;
  122. /* Skip path separator(s) */
  123. while (*p == '/')
  124. p++;
  125. if (! *p)
  126. return -FDT_ERR_BADPATH;
  127. /*
  128. * Find the next path separator. The characters between
  129. * p and q are the next segment of the the path to find.
  130. */
  131. q = strchr(p, '/');
  132. if (! q)
  133. q = end;
  134. /*
  135. * Find the offset corresponding to the this path segment.
  136. */
  137. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  138. /* Oops, error, abort abort abort */
  139. if (offset < 0)
  140. return offset;
  141. p = q;
  142. }
  143. return offset;
  144. }
  145. /*
  146. * Given the offset of a node and a name of a property in that node, return
  147. * a pointer to the property struct.
  148. */
  149. struct fdt_property *fdt_get_property(const void *fdt,
  150. int nodeoffset,
  151. const char *name, int *lenp)
  152. {
  153. int level = 0;
  154. uint32_t tag;
  155. struct fdt_property *prop;
  156. int namestroff;
  157. int offset, nextoffset;
  158. int err;
  159. if ((err = _fdt_check_header(fdt)) != 0)
  160. goto fail;
  161. err = -FDT_ERR_BADOFFSET;
  162. if (nodeoffset % FDT_TAGSIZE)
  163. goto fail;
  164. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
  165. if (tag != FDT_BEGIN_NODE)
  166. goto fail;
  167. do {
  168. offset = nextoffset;
  169. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  170. switch (tag) {
  171. case FDT_END:
  172. err = -FDT_ERR_TRUNCATED;
  173. goto fail;
  174. case FDT_BEGIN_NODE:
  175. level++;
  176. break;
  177. case FDT_END_NODE:
  178. level--;
  179. break;
  180. case FDT_PROP:
  181. /*
  182. * If we are nested down levels, ignore the strings
  183. * until we get back to the proper level.
  184. */
  185. if (level != 0)
  186. continue;
  187. err = -FDT_ERR_BADSTRUCTURE;
  188. prop = fdt_offset_ptr_typed(fdt, offset, prop);
  189. if (! prop)
  190. goto fail;
  191. namestroff = fdt32_to_cpu(prop->nameoff);
  192. if (streq(fdt_string(fdt, namestroff), name)) {
  193. /* Found it! */
  194. int len = fdt32_to_cpu(prop->len);
  195. prop = fdt_offset_ptr(fdt, offset,
  196. sizeof(*prop)+len);
  197. if (! prop)
  198. goto fail;
  199. if (lenp)
  200. *lenp = len;
  201. return prop;
  202. }
  203. break;
  204. case FDT_NOP:
  205. break;
  206. default:
  207. err = -FDT_ERR_BADSTRUCTURE;
  208. goto fail;
  209. }
  210. } while (level >= 0);
  211. err = -FDT_ERR_NOTFOUND;
  212. fail:
  213. if (lenp)
  214. *lenp = err;
  215. return NULL;
  216. }
  217. /*
  218. * Given the offset of a node and a name of a property in that node, return
  219. * a pointer to the property data (ONLY).
  220. */
  221. void *fdt_getprop(const void *fdt, int nodeoffset,
  222. const char *name, int *lenp)
  223. {
  224. const struct fdt_property *prop;
  225. prop = fdt_get_property(fdt, nodeoffset, name, lenp);
  226. if (! prop)
  227. return NULL;
  228. return (void *)prop->data;
  229. }
  230. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
  231. {
  232. const uint32_t *tagp, *lenp;
  233. uint32_t tag;
  234. const char *p;
  235. if (offset % FDT_TAGSIZE)
  236. return -1;
  237. tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
  238. if (! tagp)
  239. return FDT_END; /* premature end */
  240. tag = fdt32_to_cpu(*tagp);
  241. offset += FDT_TAGSIZE;
  242. switch (tag) {
  243. case FDT_BEGIN_NODE:
  244. if(namep)
  245. *namep = fdt_offset_ptr(fdt, offset, 1);
  246. /* skip name */
  247. do {
  248. p = fdt_offset_ptr(fdt, offset++, 1);
  249. } while (p && (*p != '\0'));
  250. if (! p)
  251. return FDT_END;
  252. break;
  253. case FDT_PROP:
  254. lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
  255. if (! lenp)
  256. return FDT_END;
  257. /*
  258. * Get the property and set the namep to the name.
  259. */
  260. if(namep) {
  261. struct fdt_property *prop;
  262. prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
  263. if (! prop)
  264. return -FDT_ERR_BADSTRUCTURE;
  265. *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  266. }
  267. /* skip name offset, length and value */
  268. offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
  269. break;
  270. }
  271. if (nextoffset)
  272. *nextoffset = ALIGN(offset, FDT_TAGSIZE);
  273. return tag;
  274. }