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