1 /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
2 #ifndef LIBFDT_INTERNAL_H
3 #define LIBFDT_INTERNAL_H
5 * libfdt - Flat Device Tree manipulation
6 * Copyright (C) 2006 David Gibson, IBM Corporation.
10 #define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
11 #define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE))
13 int fdt_ro_probe_(const void *fdt);
14 #define FDT_RO_PROBE(fdt) \
17 if (fdt_chk_basic()) { \
18 totalsize_ = fdt_ro_probe_(fdt); \
24 int fdt_check_node_offset_(const void *fdt, int offset);
25 int fdt_check_prop_offset_(const void *fdt, int offset);
26 const char *fdt_find_string_(const char *strtab, int tabsize, const char *s);
27 int fdt_node_end_offset_(void *fdt, int nodeoffset);
29 static inline const void *fdt_offset_ptr_(const void *fdt, int offset)
31 return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
34 static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
36 return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
39 static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n)
41 const struct fdt_reserve_entry *rsv_table =
42 (const struct fdt_reserve_entry *)
43 ((const char *)fdt + fdt_off_mem_rsvmap(fdt));
47 static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
49 return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n);
52 #define FDT_SW_MAGIC (~FDT_MAGIC)
54 /**********************************************************************/
55 /* Checking controls */
56 /**********************************************************************/
58 #ifndef FDT_ASSUME_MASK
59 #define FDT_ASSUME_MASK 0
63 * Defines assumptions which can be enabled. Each of these can be enabled
64 * individually. For maximum saftey, don't enable any assumptions!
66 * For minimal code size and no safety, use FDT_ASSUME_PERFECT at your own risk.
67 * You should have another method of validating the device tree, such as a
68 * signature or hash check before using libfdt.
70 * For situations where security is not a concern it may be safe to enable
71 * FDT_ASSUME_FRIENDLY.
75 * This does essentially no checks. Only the latest device-tree
76 * version is correctly handled. Incosistencies or errors in the device
77 * tree may cause undefined behaviour or crashes.
79 * If an error occurs when modifying the tree it may leave the tree in
80 * an intermediate (but valid) state. As an example, adding a property
81 * where there is insufficient space may result in the property name
82 * being added to the string table even though the property itself is
83 * not added to the struct section.
85 * Only use this if you have a fully validated device tree with
86 * the latest supported version and wish to minimise code size.
88 FDT_ASSUME_PERFECT = 0xff,
91 * This assumes that the device tree is sane. i.e. header metadata
92 * and basic hierarchy are correct.
94 * These checks will be sufficient if you have a valid device tree with
95 * no internal inconsistencies. With this assumption, libfdt will
96 * generally not return -FDT_ERR_INTERNAL, -FDT_ERR_BADLAYOUT, etc.
98 FDT_ASSUME_SANE = 1 << 0,
101 * This disables checks for device-tree version and removes all code
102 * which handles older versions.
104 * Only enable this if you know you have a device tree with the latest
107 FDT_ASSUME_LATEST = 1 << 1,
110 * This disables any extensive checking of parameters and the device
111 * tree, making various assumptions about correctness. Normal device
112 * trees produced by libfdt and the compiler should be handled safely.
113 * Malicious device trees and complete garbage may cause libfdt to
114 * behave badly or crash.
116 FDT_ASSUME_FRIENDLY = 1 << 2,
119 /** fdt_chk_basic() - see if basic checking of params and DT data is enabled */
120 static inline bool fdt_chk_basic(void)
122 return !(FDT_ASSUME_MASK & FDT_ASSUME_SANE);
125 /** fdt_chk_version() - see if we need to handle old versions of the DT */
126 static inline bool fdt_chk_version(void)
128 return !(FDT_ASSUME_MASK & FDT_ASSUME_LATEST);
131 /** fdt_chk_extra() - see if extra checking is enabled */
132 static inline bool fdt_chk_extra(void)
134 return !(FDT_ASSUME_MASK & FDT_ASSUME_FRIENDLY);
137 #endif /* LIBFDT_INTERNAL_H */