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