fdt.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * libfdt - Flat Device Tree manipulation
  3. * Copyright (C) 2006 David Gibson, IBM Corporation.
  4. *
  5. * libfdt is dual licensed: you can use it either under the terms of
  6. * the GPL, or the BSD license, at your option.
  7. *
  8. * a) This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this library; if not, write to the Free
  20. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  21. * MA 02110-1301 USA
  22. *
  23. * Alternatively,
  24. *
  25. * b) Redistribution and use in source and binary forms, with or
  26. * without modification, are permitted provided that the following
  27. * conditions are met:
  28. *
  29. * 1. Redistributions of source code must retain the above
  30. * copyright notice, this list of conditions and the following
  31. * disclaimer.
  32. * 2. Redistributions in binary form must reproduce the above
  33. * copyright notice, this list of conditions and the following
  34. * disclaimer in the documentation and/or other materials
  35. * provided with the distribution.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  38. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  39. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  40. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  42. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  48. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  49. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50. */
  51. #include "libfdt_env.h"
  52. #include <fdt.h>
  53. #include <libfdt.h>
  54. #include "libfdt_internal.h"
  55. /*
  56. * Minimal sanity check for a read-only tree. fdt_ro_probe_() checks
  57. * that the given buffer contains what appears to be a flattened
  58. * device tree with sane information in its header.
  59. */
  60. int fdt_ro_probe_(const void *fdt)
  61. {
  62. if (fdt_magic(fdt) == FDT_MAGIC) {
  63. /* Complete tree */
  64. if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
  65. return -FDT_ERR_BADVERSION;
  66. if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
  67. return -FDT_ERR_BADVERSION;
  68. } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
  69. /* Unfinished sequential-write blob */
  70. if (fdt_size_dt_struct(fdt) == 0)
  71. return -FDT_ERR_BADSTATE;
  72. } else {
  73. return -FDT_ERR_BADMAGIC;
  74. }
  75. return 0;
  76. }
  77. static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off)
  78. {
  79. return (off >= hdrsize) && (off <= totalsize);
  80. }
  81. static int check_block_(uint32_t hdrsize, uint32_t totalsize,
  82. uint32_t base, uint32_t size)
  83. {
  84. if (!check_off_(hdrsize, totalsize, base))
  85. return 0; /* block start out of bounds */
  86. if ((base + size) < base)
  87. return 0; /* overflow */
  88. if (!check_off_(hdrsize, totalsize, base + size))
  89. return 0; /* block end out of bounds */
  90. return 1;
  91. }
  92. size_t fdt_header_size_(uint32_t version)
  93. {
  94. if (version <= 1)
  95. return FDT_V1_SIZE;
  96. else if (version <= 2)
  97. return FDT_V2_SIZE;
  98. else if (version <= 3)
  99. return FDT_V3_SIZE;
  100. else if (version <= 16)
  101. return FDT_V16_SIZE;
  102. else
  103. return FDT_V17_SIZE;
  104. }
  105. int fdt_check_header(const void *fdt)
  106. {
  107. size_t hdrsize;
  108. if (fdt_magic(fdt) != FDT_MAGIC)
  109. return -FDT_ERR_BADMAGIC;
  110. hdrsize = fdt_header_size(fdt);
  111. if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
  112. || (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION))
  113. return -FDT_ERR_BADVERSION;
  114. if (fdt_version(fdt) < fdt_last_comp_version(fdt))
  115. return -FDT_ERR_BADVERSION;
  116. if ((fdt_totalsize(fdt) < hdrsize)
  117. || (fdt_totalsize(fdt) > INT_MAX))
  118. return -FDT_ERR_TRUNCATED;
  119. /* Bounds check memrsv block */
  120. if (!check_off_(hdrsize, fdt_totalsize(fdt), fdt_off_mem_rsvmap(fdt)))
  121. return -FDT_ERR_TRUNCATED;
  122. /* Bounds check structure block */
  123. if (fdt_version(fdt) < 17) {
  124. if (!check_off_(hdrsize, fdt_totalsize(fdt),
  125. fdt_off_dt_struct(fdt)))
  126. return -FDT_ERR_TRUNCATED;
  127. } else {
  128. if (!check_block_(hdrsize, fdt_totalsize(fdt),
  129. fdt_off_dt_struct(fdt),
  130. fdt_size_dt_struct(fdt)))
  131. return -FDT_ERR_TRUNCATED;
  132. }
  133. /* Bounds check strings block */
  134. if (!check_block_(hdrsize, fdt_totalsize(fdt),
  135. fdt_off_dt_strings(fdt), fdt_size_dt_strings(fdt)))
  136. return -FDT_ERR_TRUNCATED;
  137. return 0;
  138. }
  139. const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
  140. {
  141. unsigned absoffset = offset + fdt_off_dt_struct(fdt);
  142. if ((absoffset < offset)
  143. || ((absoffset + len) < absoffset)
  144. || (absoffset + len) > fdt_totalsize(fdt))
  145. return NULL;
  146. if (fdt_version(fdt) >= 0x11)
  147. if (((offset + len) < offset)
  148. || ((offset + len) > fdt_size_dt_struct(fdt)))
  149. return NULL;
  150. return fdt_offset_ptr_(fdt, offset);
  151. }
  152. uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
  153. {
  154. const fdt32_t *tagp, *lenp;
  155. uint32_t tag;
  156. int offset = startoffset;
  157. const char *p;
  158. *nextoffset = -FDT_ERR_TRUNCATED;
  159. tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
  160. if (!tagp)
  161. return FDT_END; /* premature end */
  162. tag = fdt32_to_cpu(*tagp);
  163. offset += FDT_TAGSIZE;
  164. *nextoffset = -FDT_ERR_BADSTRUCTURE;
  165. switch (tag) {
  166. case FDT_BEGIN_NODE:
  167. /* skip name */
  168. do {
  169. p = fdt_offset_ptr(fdt, offset++, 1);
  170. } while (p && (*p != '\0'));
  171. if (!p)
  172. return FDT_END; /* premature end */
  173. break;
  174. case FDT_PROP:
  175. lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
  176. if (!lenp)
  177. return FDT_END; /* premature end */
  178. /* skip-name offset, length and value */
  179. offset += sizeof(struct fdt_property) - FDT_TAGSIZE
  180. + fdt32_to_cpu(*lenp);
  181. if (fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 &&
  182. ((offset - fdt32_to_cpu(*lenp)) % 8) != 0)
  183. offset += 4;
  184. break;
  185. case FDT_END:
  186. case FDT_END_NODE:
  187. case FDT_NOP:
  188. break;
  189. default:
  190. return FDT_END;
  191. }
  192. if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
  193. return FDT_END; /* premature end */
  194. *nextoffset = FDT_TAGALIGN(offset);
  195. return tag;
  196. }
  197. int fdt_check_node_offset_(const void *fdt, int offset)
  198. {
  199. if ((offset < 0) || (offset % FDT_TAGSIZE)
  200. || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
  201. return -FDT_ERR_BADOFFSET;
  202. return offset;
  203. }
  204. int fdt_check_prop_offset_(const void *fdt, int offset)
  205. {
  206. if ((offset < 0) || (offset % FDT_TAGSIZE)
  207. || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
  208. return -FDT_ERR_BADOFFSET;
  209. return offset;
  210. }
  211. int fdt_next_node(const void *fdt, int offset, int *depth)
  212. {
  213. int nextoffset = 0;
  214. uint32_t tag;
  215. if (offset >= 0)
  216. if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
  217. return nextoffset;
  218. do {
  219. offset = nextoffset;
  220. tag = fdt_next_tag(fdt, offset, &nextoffset);
  221. switch (tag) {
  222. case FDT_PROP:
  223. case FDT_NOP:
  224. break;
  225. case FDT_BEGIN_NODE:
  226. if (depth)
  227. (*depth)++;
  228. break;
  229. case FDT_END_NODE:
  230. if (depth && ((--(*depth)) < 0))
  231. return nextoffset;
  232. break;
  233. case FDT_END:
  234. if ((nextoffset >= 0)
  235. || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
  236. return -FDT_ERR_NOTFOUND;
  237. else
  238. return nextoffset;
  239. }
  240. } while (tag != FDT_BEGIN_NODE);
  241. return offset;
  242. }
  243. int fdt_first_subnode(const void *fdt, int offset)
  244. {
  245. int depth = 0;
  246. offset = fdt_next_node(fdt, offset, &depth);
  247. if (offset < 0 || depth != 1)
  248. return -FDT_ERR_NOTFOUND;
  249. return offset;
  250. }
  251. int fdt_next_subnode(const void *fdt, int offset)
  252. {
  253. int depth = 1;
  254. /*
  255. * With respect to the parent, the depth of the next subnode will be
  256. * the same as the last.
  257. */
  258. do {
  259. offset = fdt_next_node(fdt, offset, &depth);
  260. if (offset < 0 || depth < 1)
  261. return -FDT_ERR_NOTFOUND;
  262. } while (depth > 1);
  263. return offset;
  264. }
  265. const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)
  266. {
  267. int len = strlen(s) + 1;
  268. const char *last = strtab + tabsize - len;
  269. const char *p;
  270. for (p = strtab; p <= last; p++)
  271. if (memcmp(p, s, len) == 0)
  272. return p;
  273. return NULL;
  274. }
  275. int fdt_move(const void *fdt, void *buf, int bufsize)
  276. {
  277. FDT_RO_PROBE(fdt);
  278. if (fdt_totalsize(fdt) > bufsize)
  279. return -FDT_ERR_NOSPACE;
  280. memmove(buf, fdt, fdt_totalsize(fdt));
  281. return 0;
  282. }