fdt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
  2. /*
  3. * libfdt - Flat Device Tree manipulation
  4. * Copyright (C) 2006 David Gibson, IBM Corporation.
  5. */
  6. #include "libfdt_env.h"
  7. #include <fdt.h>
  8. #include <libfdt.h>
  9. #include <lib.h>
  10. #include <dragonstub/dragonstub.h>
  11. #include "libfdt_internal.h"
  12. /*
  13. * Minimal sanity check for a read-only tree. fdt_ro_probe_() checks
  14. * that the given buffer contains what appears to be a flattened
  15. * device tree with sane information in its header.
  16. */
  17. int32_t fdt_ro_probe_(const void *fdt)
  18. {
  19. uint32_t totalsize = fdt_totalsize(fdt);
  20. if (can_assume(VALID_DTB))
  21. return totalsize;
  22. /* The device tree must be at an 8-byte aligned address */
  23. if ((uintptr_t)fdt & 7)
  24. return -FDT_ERR_ALIGNMENT;
  25. if (fdt_magic(fdt) == FDT_MAGIC) {
  26. /* Complete tree */
  27. if (!can_assume(LATEST)) {
  28. if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
  29. return -FDT_ERR_BADVERSION;
  30. if (fdt_last_comp_version(fdt) >
  31. FDT_LAST_SUPPORTED_VERSION)
  32. return -FDT_ERR_BADVERSION;
  33. }
  34. } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
  35. /* Unfinished sequential-write blob */
  36. if (!can_assume(VALID_INPUT) && fdt_size_dt_struct(fdt) == 0)
  37. return -FDT_ERR_BADSTATE;
  38. } else {
  39. return -FDT_ERR_BADMAGIC;
  40. }
  41. if (totalsize < INT32_MAX)
  42. return totalsize;
  43. else
  44. return -FDT_ERR_TRUNCATED;
  45. }
  46. static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off)
  47. {
  48. return (off >= hdrsize) && (off <= totalsize);
  49. }
  50. static int check_block_(uint32_t hdrsize, uint32_t totalsize,
  51. uint32_t base, uint32_t size)
  52. {
  53. if (!check_off_(hdrsize, totalsize, base))
  54. return 0; /* block start out of bounds */
  55. if ((base + size) < base)
  56. return 0; /* overflow */
  57. if (!check_off_(hdrsize, totalsize, base + size))
  58. return 0; /* block end out of bounds */
  59. return 1;
  60. }
  61. size_t fdt_header_size_(uint32_t version)
  62. {
  63. if (version <= 1)
  64. return FDT_V1_SIZE;
  65. else if (version <= 2)
  66. return FDT_V2_SIZE;
  67. else if (version <= 3)
  68. return FDT_V3_SIZE;
  69. else if (version <= 16)
  70. return FDT_V16_SIZE;
  71. else
  72. return FDT_V17_SIZE;
  73. }
  74. size_t fdt_header_size(const void *fdt)
  75. {
  76. return can_assume(LATEST) ? FDT_V17_SIZE :
  77. fdt_header_size_(fdt_version(fdt));
  78. }
  79. int fdt_check_header(const void *fdt)
  80. {
  81. size_t hdrsize;
  82. /* The device tree must be at an 8-byte aligned address */
  83. if ((uintptr_t)fdt & 7)
  84. return -FDT_ERR_ALIGNMENT;
  85. if (fdt_magic(fdt) != FDT_MAGIC)
  86. return -FDT_ERR_BADMAGIC;
  87. if (!can_assume(LATEST)) {
  88. if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
  89. || (fdt_last_comp_version(fdt) >
  90. FDT_LAST_SUPPORTED_VERSION))
  91. return -FDT_ERR_BADVERSION;
  92. if (fdt_version(fdt) < fdt_last_comp_version(fdt))
  93. return -FDT_ERR_BADVERSION;
  94. }
  95. hdrsize = fdt_header_size(fdt);
  96. if (!can_assume(VALID_DTB)) {
  97. if ((fdt_totalsize(fdt) < hdrsize)
  98. || (fdt_totalsize(fdt) > INT_MAX))
  99. return -FDT_ERR_TRUNCATED;
  100. /* Bounds check memrsv block */
  101. if (!check_off_(hdrsize, fdt_totalsize(fdt),
  102. fdt_off_mem_rsvmap(fdt)))
  103. return -FDT_ERR_TRUNCATED;
  104. /* Bounds check structure block */
  105. if (!can_assume(LATEST) && fdt_version(fdt) < 17) {
  106. if (!check_off_(hdrsize, fdt_totalsize(fdt),
  107. fdt_off_dt_struct(fdt)))
  108. return -FDT_ERR_TRUNCATED;
  109. } else {
  110. if (!check_block_(hdrsize, fdt_totalsize(fdt),
  111. fdt_off_dt_struct(fdt),
  112. fdt_size_dt_struct(fdt)))
  113. return -FDT_ERR_TRUNCATED;
  114. }
  115. /* Bounds check strings block */
  116. if (!check_block_(hdrsize, fdt_totalsize(fdt),
  117. fdt_off_dt_strings(fdt),
  118. fdt_size_dt_strings(fdt)))
  119. return -FDT_ERR_TRUNCATED;
  120. }
  121. return 0;
  122. }
  123. const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
  124. {
  125. unsigned int uoffset = offset;
  126. unsigned int absoffset = offset + fdt_off_dt_struct(fdt);
  127. if (offset < 0)
  128. return NULL;
  129. if (!can_assume(VALID_INPUT))
  130. if ((absoffset < uoffset)
  131. || ((absoffset + len) < absoffset)
  132. || (absoffset + len) > fdt_totalsize(fdt))
  133. return NULL;
  134. if (can_assume(LATEST) || fdt_version(fdt) >= 0x11)
  135. if (((uoffset + len) < uoffset)
  136. || ((offset + len) > fdt_size_dt_struct(fdt)))
  137. return NULL;
  138. return fdt_offset_ptr_(fdt, offset);
  139. }
  140. uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
  141. {
  142. const fdt32_t *tagp, *lenp;
  143. uint32_t tag, len, sum;
  144. int offset = startoffset;
  145. const char *p;
  146. *nextoffset = -FDT_ERR_TRUNCATED;
  147. tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
  148. if (!can_assume(VALID_DTB) && !tagp)
  149. return FDT_END; /* premature end */
  150. tag = fdt32_to_cpu(*tagp);
  151. offset += FDT_TAGSIZE;
  152. *nextoffset = -FDT_ERR_BADSTRUCTURE;
  153. switch (tag) {
  154. case FDT_BEGIN_NODE:
  155. /* skip name */
  156. do {
  157. p = fdt_offset_ptr(fdt, offset++, 1);
  158. } while (p && (*p != '\0'));
  159. if (!can_assume(VALID_DTB) && !p)
  160. return FDT_END; /* premature end */
  161. break;
  162. case FDT_PROP:
  163. lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
  164. if (!can_assume(VALID_DTB) && !lenp)
  165. return FDT_END; /* premature end */
  166. len = fdt32_to_cpu(*lenp);
  167. sum = len + offset;
  168. if (!can_assume(VALID_DTB) &&
  169. (INT_MAX <= sum || sum < (uint32_t) offset))
  170. return FDT_END; /* premature end */
  171. /* skip-name offset, length and value */
  172. offset += sizeof(struct fdt_property) - FDT_TAGSIZE + len;
  173. if (!can_assume(LATEST) &&
  174. fdt_version(fdt) < 0x10 && len >= 8 &&
  175. ((offset - len) % 8) != 0)
  176. offset += 4;
  177. break;
  178. case FDT_END:
  179. case FDT_END_NODE:
  180. case FDT_NOP:
  181. break;
  182. default:
  183. return FDT_END;
  184. }
  185. if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
  186. return FDT_END; /* premature end */
  187. *nextoffset = FDT_TAGALIGN(offset);
  188. return tag;
  189. }
  190. int fdt_check_node_offset_(const void *fdt, int offset)
  191. {
  192. if (!can_assume(VALID_INPUT)
  193. && ((offset < 0) || (offset % FDT_TAGSIZE)))
  194. return -FDT_ERR_BADOFFSET;
  195. if (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)
  196. return -FDT_ERR_BADOFFSET;
  197. return offset;
  198. }
  199. int fdt_check_prop_offset_(const void *fdt, int offset)
  200. {
  201. if (!can_assume(VALID_INPUT)
  202. && ((offset < 0) || (offset % FDT_TAGSIZE)))
  203. return -FDT_ERR_BADOFFSET;
  204. if (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)
  205. return -FDT_ERR_BADOFFSET;
  206. return offset;
  207. }
  208. int fdt_next_node(const void *fdt, int offset, int *depth)
  209. {
  210. int nextoffset = 0;
  211. uint32_t tag;
  212. if (offset >= 0)
  213. if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
  214. return nextoffset;
  215. do {
  216. offset = nextoffset;
  217. tag = fdt_next_tag(fdt, offset, &nextoffset);
  218. switch (tag) {
  219. case FDT_PROP:
  220. case FDT_NOP:
  221. break;
  222. case FDT_BEGIN_NODE:
  223. if (depth)
  224. (*depth)++;
  225. break;
  226. case FDT_END_NODE:
  227. if (depth && ((--(*depth)) < 0))
  228. return nextoffset;
  229. break;
  230. case FDT_END:
  231. if ((nextoffset >= 0)
  232. || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
  233. return -FDT_ERR_NOTFOUND;
  234. else
  235. return nextoffset;
  236. }
  237. } while (tag != FDT_BEGIN_NODE);
  238. return offset;
  239. }
  240. int fdt_first_subnode(const void *fdt, int offset)
  241. {
  242. int depth = 0;
  243. offset = fdt_next_node(fdt, offset, &depth);
  244. if (offset < 0 || depth != 1)
  245. return -FDT_ERR_NOTFOUND;
  246. return offset;
  247. }
  248. int fdt_next_subnode(const void *fdt, int offset)
  249. {
  250. int depth = 1;
  251. /*
  252. * With respect to the parent, the depth of the next subnode will be
  253. * the same as the last.
  254. */
  255. do {
  256. offset = fdt_next_node(fdt, offset, &depth);
  257. if (offset < 0 || depth < 1)
  258. return -FDT_ERR_NOTFOUND;
  259. } while (depth > 1);
  260. return offset;
  261. }
  262. const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)
  263. {
  264. int len = strlen(s) + 1;
  265. const char *last = strtab + tabsize - len;
  266. const char *p;
  267. for (p = strtab; p <= last; p++)
  268. if (memcmp(p, s, len) == 0)
  269. return p;
  270. return NULL;
  271. }
  272. int fdt_move(const void *fdt, void *buf, int bufsize)
  273. {
  274. if (!can_assume(VALID_INPUT) && bufsize < 0)
  275. return -FDT_ERR_NOSPACE;
  276. FDT_RO_PROBE(fdt);
  277. if (fdt_totalsize(fdt) > (unsigned int)bufsize)
  278. return -FDT_ERR_NOSPACE;
  279. memmove(buf, fdt, fdt_totalsize(fdt));
  280. return 0;
  281. }