libfdt_internal.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
  2. #ifndef LIBFDT_INTERNAL_H
  3. #define LIBFDT_INTERNAL_H
  4. /*
  5. * libfdt - Flat Device Tree manipulation
  6. * Copyright (C) 2006 David Gibson, IBM Corporation.
  7. */
  8. #include <fdt.h>
  9. #define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
  10. #define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE))
  11. int32_t fdt_ro_probe_(const void *fdt);
  12. #define FDT_RO_PROBE(fdt) \
  13. { \
  14. if (!can_assume(VALID_DTB)) { \
  15. int32_t totalsize_; \
  16. if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \
  17. return totalsize_; \
  18. } \
  19. }
  20. int fdt_check_node_offset_(const void *fdt, int offset);
  21. int fdt_check_prop_offset_(const void *fdt, int offset);
  22. const char *fdt_find_string_len_(const char *strtab, int tabsize, const char *s,
  23. int s_len);
  24. static inline const char *fdt_find_string_(const char *strtab, int tabsize,
  25. const char *s)
  26. {
  27. return fdt_find_string_len_(strtab, tabsize, s, strlen(s));
  28. }
  29. int fdt_node_end_offset_(void *fdt, int nodeoffset);
  30. static inline const void *fdt_offset_ptr_(const void *fdt, int offset)
  31. {
  32. return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
  33. }
  34. static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
  35. {
  36. return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
  37. }
  38. static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n)
  39. {
  40. const struct fdt_reserve_entry *rsv_table =
  41. (const struct fdt_reserve_entry *)
  42. ((const char *)fdt + fdt_off_mem_rsvmap(fdt));
  43. return rsv_table + n;
  44. }
  45. static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
  46. {
  47. return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n);
  48. }
  49. /*
  50. * Internal helpers to access structural elements of the device tree
  51. * blob (rather than for example reading integers from within property
  52. * values). We assume that we are either given a naturally aligned
  53. * address for the platform or if we are not, we are on a platform
  54. * where unaligned memory reads will be handled in a graceful manner.
  55. * If not the external helpers fdtXX_ld() from libfdt.h can be used
  56. * instead.
  57. */
  58. static inline uint32_t fdt32_ld_(const fdt32_t *p)
  59. {
  60. return fdt32_to_cpu(*p);
  61. }
  62. static inline uint64_t fdt64_ld_(const fdt64_t *p)
  63. {
  64. return fdt64_to_cpu(*p);
  65. }
  66. #define FDT_SW_MAGIC (~FDT_MAGIC)
  67. /**********************************************************************/
  68. /* Checking controls */
  69. /**********************************************************************/
  70. #ifndef FDT_ASSUME_MASK
  71. #define FDT_ASSUME_MASK 0
  72. #endif
  73. /*
  74. * Defines assumptions which can be enabled. Each of these can be enabled
  75. * individually. For maximum safety, don't enable any assumptions!
  76. *
  77. * For minimal code size and no safety, use ASSUME_PERFECT at your own risk.
  78. * You should have another method of validating the device tree, such as a
  79. * signature or hash check before using libfdt.
  80. *
  81. * For situations where security is not a concern it may be safe to enable
  82. * ASSUME_PERFECT.
  83. */
  84. enum {
  85. /*
  86. * This does essentially no checks. Only the latest device-tree
  87. * version is correctly handled. Inconsistencies or errors in the device
  88. * tree may cause undefined behaviour or crashes. Invalid parameters
  89. * passed to libfdt may do the same.
  90. *
  91. * If an error occurs when modifying the tree it may leave the tree in
  92. * an intermediate (but valid) state. As an example, adding a property
  93. * where there is insufficient space may result in the property name
  94. * being added to the string table even though the property itself is
  95. * not added to the struct section.
  96. *
  97. * Only use this if you have a fully validated device tree with
  98. * the latest supported version and wish to minimise code size.
  99. */
  100. ASSUME_PERFECT = 0xff,
  101. /*
  102. * This assumes that the device tree is sane. i.e. header metadata
  103. * and basic hierarchy are correct.
  104. *
  105. * With this assumption enabled, normal device trees produced by libfdt
  106. * and the compiler should be handled safely. Malicious device trees and
  107. * complete garbage may cause libfdt to behave badly or crash. Truncated
  108. * device trees (e.g. those only partially loaded) can also cause
  109. * problems.
  110. *
  111. * Note: Only checks that relate exclusively to the device tree itself
  112. * (not the parameters passed to libfdt) are disabled by this
  113. * assumption. This includes checking headers, tags and the like.
  114. */
  115. ASSUME_VALID_DTB = 1 << 0,
  116. /*
  117. * This builds on ASSUME_VALID_DTB and further assumes that libfdt
  118. * functions are called with valid parameters, i.e. not trigger
  119. * FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any
  120. * extensive checking of parameters and the device tree, making various
  121. * assumptions about correctness.
  122. *
  123. * It doesn't make sense to enable this assumption unless
  124. * ASSUME_VALID_DTB is also enabled.
  125. */
  126. ASSUME_VALID_INPUT = 1 << 1,
  127. /*
  128. * This disables checks for device-tree version and removes all code
  129. * which handles older versions.
  130. *
  131. * Only enable this if you know you have a device tree with the latest
  132. * version.
  133. */
  134. ASSUME_LATEST = 1 << 2,
  135. /*
  136. * This assumes that it is OK for a failed addition to the device tree,
  137. * due to lack of space or some other problem, to skip any rollback
  138. * steps (such as dropping the property name from the string table).
  139. * This is safe to enable in most circumstances, even though it may
  140. * leave the tree in a sub-optimal state.
  141. */
  142. ASSUME_NO_ROLLBACK = 1 << 3,
  143. /*
  144. * This assumes that the device tree components appear in a 'convenient'
  145. * order, i.e. the memory reservation block first, then the structure
  146. * block and finally the string block.
  147. *
  148. * This order is not specified by the device-tree specification,
  149. * but is expected by libfdt. The device-tree compiler always created
  150. * device trees with this order.
  151. *
  152. * This assumption disables a check in fdt_open_into() and removes the
  153. * ability to fix the problem there. This is safe if you know that the
  154. * device tree is correctly ordered. See fdt_blocks_misordered_().
  155. */
  156. ASSUME_LIBFDT_ORDER = 1 << 4,
  157. /*
  158. * This assumes that libfdt itself does not have any internal bugs. It
  159. * drops certain checks that should never be needed unless libfdt has an
  160. * undiscovered bug.
  161. *
  162. * This can generally be considered safe to enable.
  163. */
  164. ASSUME_LIBFDT_FLAWLESS = 1 << 5,
  165. };
  166. /**
  167. * can_assume_() - check if a particular assumption is enabled
  168. *
  169. * @mask: Mask to check (ASSUME_...)
  170. * @return true if that assumption is enabled, else false
  171. */
  172. static inline bool can_assume_(int mask)
  173. {
  174. return FDT_ASSUME_MASK & mask;
  175. }
  176. /** helper macros for checking assumptions */
  177. #define can_assume(_assume) can_assume_(ASSUME_ ## _assume)
  178. #endif /* LIBFDT_INTERNAL_H */