libfdt_internal.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. int fdt_ro_probe_(const void *fdt);
  12. #define FDT_RO_PROBE(fdt) \
  13. { \
  14. int totalsize_; \
  15. if (fdt_chk_basic()) { \
  16. totalsize_ = fdt_ro_probe_(fdt); \
  17. if (totalsize_ < 0) \
  18. return totalsize_; \
  19. } \
  20. }
  21. int fdt_check_node_offset_(const void *fdt, int offset);
  22. int fdt_check_prop_offset_(const void *fdt, int offset);
  23. const char *fdt_find_string_(const char *strtab, int tabsize, const char *s);
  24. int fdt_node_end_offset_(void *fdt, int nodeoffset);
  25. static inline const void *fdt_offset_ptr_(const void *fdt, int offset)
  26. {
  27. return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
  28. }
  29. static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
  30. {
  31. return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
  32. }
  33. static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n)
  34. {
  35. const struct fdt_reserve_entry *rsv_table =
  36. (const struct fdt_reserve_entry *)
  37. ((const char *)fdt + fdt_off_mem_rsvmap(fdt));
  38. return rsv_table + n;
  39. }
  40. static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
  41. {
  42. return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n);
  43. }
  44. #define FDT_SW_MAGIC (~FDT_MAGIC)
  45. /**********************************************************************/
  46. /* Checking controls */
  47. /**********************************************************************/
  48. #ifndef FDT_ASSUME_MASK
  49. #define FDT_ASSUME_MASK 0
  50. #endif
  51. /*
  52. * Defines assumptions which can be enabled. Each of these can be enabled
  53. * individually. For maximum saftey, don't enable any assumptions!
  54. *
  55. * For minimal code size and no safety, use FDT_ASSUME_PERFECT at your own risk.
  56. * You should have another method of validating the device tree, such as a
  57. * signature or hash check before using libfdt.
  58. *
  59. * For situations where security is not a concern it may be safe to enable
  60. * FDT_ASSUME_FRIENDLY.
  61. */
  62. enum {
  63. /*
  64. * This does essentially no checks. Only the latest device-tree
  65. * version is correctly handled. Incosistencies or errors in the device
  66. * tree may cause undefined behaviour or crashes.
  67. *
  68. * If an error occurs when modifying the tree it may leave the tree in
  69. * an intermediate (but valid) state. As an example, adding a property
  70. * where there is insufficient space may result in the property name
  71. * being added to the string table even though the property itself is
  72. * not added to the struct section.
  73. *
  74. * Only use this if you have a fully validated device tree with
  75. * the latest supported version and wish to minimise code size.
  76. */
  77. FDT_ASSUME_PERFECT = 0xff,
  78. /*
  79. * This assumes that the device tree is sane. i.e. header metadata
  80. * and basic hierarchy are correct.
  81. *
  82. * These checks will be sufficient if you have a valid device tree with
  83. * no internal inconsistencies. With this assumption, libfdt will
  84. * generally not return -FDT_ERR_INTERNAL, -FDT_ERR_BADLAYOUT, etc.
  85. */
  86. FDT_ASSUME_SANE = 1 << 0,
  87. /*
  88. * This disables checks for device-tree version and removes all code
  89. * which handles older versions.
  90. *
  91. * Only enable this if you know you have a device tree with the latest
  92. * version.
  93. */
  94. FDT_ASSUME_LATEST = 1 << 1,
  95. /*
  96. * This disables any extensive checking of parameters and the device
  97. * tree, making various assumptions about correctness. Normal device
  98. * trees produced by libfdt and the compiler should be handled safely.
  99. * Malicious device trees and complete garbage may cause libfdt to
  100. * behave badly or crash.
  101. */
  102. FDT_ASSUME_FRIENDLY = 1 << 2,
  103. };
  104. /** fdt_chk_basic() - see if basic checking of params and DT data is enabled */
  105. static inline bool fdt_chk_basic(void)
  106. {
  107. return !(FDT_ASSUME_MASK & FDT_ASSUME_SANE);
  108. }
  109. /** fdt_chk_version() - see if we need to handle old versions of the DT */
  110. static inline bool fdt_chk_version(void)
  111. {
  112. return !(FDT_ASSUME_MASK & FDT_ASSUME_LATEST);
  113. }
  114. /** fdt_chk_extra() - see if extra checking is enabled */
  115. static inline bool fdt_chk_extra(void)
  116. {
  117. return !(FDT_ASSUME_MASK & FDT_ASSUME_FRIENDLY);
  118. }
  119. #endif /* LIBFDT_INTERNAL_H */