xref: /rk3399_ARM-atf/lib/libfdt/libfdt_internal.h (revision 9900d4eb06605c6ff79801c4371445d245b7b2fb)
1243ce5d5SMadhukar Pappireddy /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
2630b011fSAntonio Nino Diaz #ifndef LIBFDT_INTERNAL_H
3630b011fSAntonio Nino Diaz #define LIBFDT_INTERNAL_H
4630b011fSAntonio Nino Diaz /*
5630b011fSAntonio Nino Diaz  * libfdt - Flat Device Tree manipulation
6630b011fSAntonio Nino Diaz  * Copyright (C) 2006 David Gibson, IBM Corporation.
7630b011fSAntonio Nino Diaz  */
8630b011fSAntonio Nino Diaz #include <fdt.h>
9630b011fSAntonio Nino Diaz 
10630b011fSAntonio Nino Diaz #define FDT_ALIGN(x, a)		(((x) + (a) - 1) & ~((a) - 1))
11630b011fSAntonio Nino Diaz #define FDT_TAGALIGN(x)		(FDT_ALIGN((x), FDT_TAGSIZE))
12630b011fSAntonio Nino Diaz 
13243ce5d5SMadhukar Pappireddy int32_t fdt_ro_probe_(const void *fdt);
14243ce5d5SMadhukar Pappireddy #define FDT_RO_PROBE(fdt)					\
15630b011fSAntonio Nino Diaz 	{							\
16243ce5d5SMadhukar Pappireddy 		int32_t totalsize_;				\
17243ce5d5SMadhukar Pappireddy 		if ((totalsize_ = fdt_ro_probe_(fdt)) < 0)	\
18243ce5d5SMadhukar Pappireddy 			return totalsize_;			\
19630b011fSAntonio Nino Diaz 	}
20630b011fSAntonio Nino Diaz 
21630b011fSAntonio Nino Diaz int fdt_check_node_offset_(const void *fdt, int offset);
22630b011fSAntonio Nino Diaz int fdt_check_prop_offset_(const void *fdt, int offset);
23630b011fSAntonio Nino Diaz const char *fdt_find_string_(const char *strtab, int tabsize, const char *s);
24630b011fSAntonio Nino Diaz int fdt_node_end_offset_(void *fdt, int nodeoffset);
25630b011fSAntonio Nino Diaz 
fdt_offset_ptr_(const void * fdt,int offset)26630b011fSAntonio Nino Diaz static inline const void *fdt_offset_ptr_(const void *fdt, int offset)
27630b011fSAntonio Nino Diaz {
28630b011fSAntonio Nino Diaz 	return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
29630b011fSAntonio Nino Diaz }
30630b011fSAntonio Nino Diaz 
fdt_offset_ptr_w_(void * fdt,int offset)31630b011fSAntonio Nino Diaz static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
32630b011fSAntonio Nino Diaz {
33630b011fSAntonio Nino Diaz 	return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
34630b011fSAntonio Nino Diaz }
35630b011fSAntonio Nino Diaz 
fdt_mem_rsv_(const void * fdt,int n)36630b011fSAntonio Nino Diaz static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n)
37630b011fSAntonio Nino Diaz {
38630b011fSAntonio Nino Diaz 	const struct fdt_reserve_entry *rsv_table =
39630b011fSAntonio Nino Diaz 		(const struct fdt_reserve_entry *)
40630b011fSAntonio Nino Diaz 		((const char *)fdt + fdt_off_mem_rsvmap(fdt));
41630b011fSAntonio Nino Diaz 
42630b011fSAntonio Nino Diaz 	return rsv_table + n;
43630b011fSAntonio Nino Diaz }
fdt_mem_rsv_w_(void * fdt,int n)44630b011fSAntonio Nino Diaz static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
45630b011fSAntonio Nino Diaz {
46630b011fSAntonio Nino Diaz 	return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n);
47630b011fSAntonio Nino Diaz }
48630b011fSAntonio Nino Diaz 
49*94b2f94bSDaniel Boulby /*
50*94b2f94bSDaniel Boulby  * Internal helpers to access tructural elements of the device tree
51*94b2f94bSDaniel Boulby  * blob (rather than for exaple reading integers from within property
52*94b2f94bSDaniel Boulby  * values).  We assume that we are either given a naturally aligned
53*94b2f94bSDaniel Boulby  * address for the platform or if we are not, we are on a platform
54*94b2f94bSDaniel Boulby  * where unaligned memory reads will be handled in a graceful manner.
55*94b2f94bSDaniel Boulby  * If not the external helpers fdtXX_ld() from libfdt.h can be used
56*94b2f94bSDaniel Boulby  * instead.
57*94b2f94bSDaniel Boulby  */
fdt32_ld_(const fdt32_t * p)58*94b2f94bSDaniel Boulby static inline uint32_t fdt32_ld_(const fdt32_t *p)
59*94b2f94bSDaniel Boulby {
60*94b2f94bSDaniel Boulby 	return fdt32_to_cpu(*p);
61*94b2f94bSDaniel Boulby }
62*94b2f94bSDaniel Boulby 
fdt64_ld_(const fdt64_t * p)63*94b2f94bSDaniel Boulby static inline uint64_t fdt64_ld_(const fdt64_t *p)
64*94b2f94bSDaniel Boulby {
65*94b2f94bSDaniel Boulby 	return fdt64_to_cpu(*p);
66*94b2f94bSDaniel Boulby }
67*94b2f94bSDaniel Boulby 
68630b011fSAntonio Nino Diaz #define FDT_SW_MAGIC		(~FDT_MAGIC)
69630b011fSAntonio Nino Diaz 
70243ce5d5SMadhukar Pappireddy /**********************************************************************/
71243ce5d5SMadhukar Pappireddy /* Checking controls                                                  */
72243ce5d5SMadhukar Pappireddy /**********************************************************************/
73243ce5d5SMadhukar Pappireddy 
74243ce5d5SMadhukar Pappireddy #ifndef FDT_ASSUME_MASK
75243ce5d5SMadhukar Pappireddy #define FDT_ASSUME_MASK 0
76243ce5d5SMadhukar Pappireddy #endif
77243ce5d5SMadhukar Pappireddy 
78243ce5d5SMadhukar Pappireddy /*
79243ce5d5SMadhukar Pappireddy  * Defines assumptions which can be enabled. Each of these can be enabled
80243ce5d5SMadhukar Pappireddy  * individually. For maximum safety, don't enable any assumptions!
81243ce5d5SMadhukar Pappireddy  *
82243ce5d5SMadhukar Pappireddy  * For minimal code size and no safety, use ASSUME_PERFECT at your own risk.
83243ce5d5SMadhukar Pappireddy  * You should have another method of validating the device tree, such as a
84243ce5d5SMadhukar Pappireddy  * signature or hash check before using libfdt.
85243ce5d5SMadhukar Pappireddy  *
86243ce5d5SMadhukar Pappireddy  * For situations where security is not a concern it may be safe to enable
87243ce5d5SMadhukar Pappireddy  * ASSUME_SANE.
88243ce5d5SMadhukar Pappireddy  */
89243ce5d5SMadhukar Pappireddy enum {
90243ce5d5SMadhukar Pappireddy 	/*
91243ce5d5SMadhukar Pappireddy 	 * This does essentially no checks. Only the latest device-tree
92243ce5d5SMadhukar Pappireddy 	 * version is correctly handled. Inconsistencies or errors in the device
93243ce5d5SMadhukar Pappireddy 	 * tree may cause undefined behaviour or crashes. Invalid parameters
94243ce5d5SMadhukar Pappireddy 	 * passed to libfdt may do the same.
95243ce5d5SMadhukar Pappireddy 	 *
96243ce5d5SMadhukar Pappireddy 	 * If an error occurs when modifying the tree it may leave the tree in
97243ce5d5SMadhukar Pappireddy 	 * an intermediate (but valid) state. As an example, adding a property
98243ce5d5SMadhukar Pappireddy 	 * where there is insufficient space may result in the property name
99243ce5d5SMadhukar Pappireddy 	 * being added to the string table even though the property itself is
100243ce5d5SMadhukar Pappireddy 	 * not added to the struct section.
101243ce5d5SMadhukar Pappireddy 	 *
102243ce5d5SMadhukar Pappireddy 	 * Only use this if you have a fully validated device tree with
103243ce5d5SMadhukar Pappireddy 	 * the latest supported version and wish to minimise code size.
104243ce5d5SMadhukar Pappireddy 	 */
105243ce5d5SMadhukar Pappireddy 	ASSUME_PERFECT		= 0xff,
106243ce5d5SMadhukar Pappireddy 
107243ce5d5SMadhukar Pappireddy 	/*
108243ce5d5SMadhukar Pappireddy 	 * This assumes that the device tree is sane. i.e. header metadata
109243ce5d5SMadhukar Pappireddy 	 * and basic hierarchy are correct.
110243ce5d5SMadhukar Pappireddy 	 *
111243ce5d5SMadhukar Pappireddy 	 * With this assumption enabled, normal device trees produced by libfdt
112243ce5d5SMadhukar Pappireddy 	 * and the compiler should be handled safely. Malicious device trees and
113243ce5d5SMadhukar Pappireddy 	 * complete garbage may cause libfdt to behave badly or crash. Truncated
114243ce5d5SMadhukar Pappireddy 	 * device trees (e.g. those only partially loaded) can also cause
115243ce5d5SMadhukar Pappireddy 	 * problems.
116243ce5d5SMadhukar Pappireddy 	 *
117243ce5d5SMadhukar Pappireddy 	 * Note: Only checks that relate exclusively to the device tree itself
118243ce5d5SMadhukar Pappireddy 	 * (not the parameters passed to libfdt) are disabled by this
119243ce5d5SMadhukar Pappireddy 	 * assumption. This includes checking headers, tags and the like.
120243ce5d5SMadhukar Pappireddy 	 */
121243ce5d5SMadhukar Pappireddy 	ASSUME_VALID_DTB	= 1 << 0,
122243ce5d5SMadhukar Pappireddy 
123243ce5d5SMadhukar Pappireddy 	/*
124243ce5d5SMadhukar Pappireddy 	 * This builds on ASSUME_VALID_DTB and further assumes that libfdt
125243ce5d5SMadhukar Pappireddy 	 * functions are called with valid parameters, i.e. not trigger
126243ce5d5SMadhukar Pappireddy 	 * FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any
127243ce5d5SMadhukar Pappireddy 	 * extensive checking of parameters and the device tree, making various
128243ce5d5SMadhukar Pappireddy 	 * assumptions about correctness.
129243ce5d5SMadhukar Pappireddy 	 *
130243ce5d5SMadhukar Pappireddy 	 * It doesn't make sense to enable this assumption unless
131243ce5d5SMadhukar Pappireddy 	 * ASSUME_VALID_DTB is also enabled.
132243ce5d5SMadhukar Pappireddy 	 */
133243ce5d5SMadhukar Pappireddy 	ASSUME_VALID_INPUT	= 1 << 1,
134243ce5d5SMadhukar Pappireddy 
135243ce5d5SMadhukar Pappireddy 	/*
136243ce5d5SMadhukar Pappireddy 	 * This disables checks for device-tree version and removes all code
137243ce5d5SMadhukar Pappireddy 	 * which handles older versions.
138243ce5d5SMadhukar Pappireddy 	 *
139243ce5d5SMadhukar Pappireddy 	 * Only enable this if you know you have a device tree with the latest
140243ce5d5SMadhukar Pappireddy 	 * version.
141243ce5d5SMadhukar Pappireddy 	 */
142243ce5d5SMadhukar Pappireddy 	ASSUME_LATEST		= 1 << 2,
143243ce5d5SMadhukar Pappireddy 
144243ce5d5SMadhukar Pappireddy 	/*
145243ce5d5SMadhukar Pappireddy 	 * This assumes that it is OK for a failed addition to the device tree,
146243ce5d5SMadhukar Pappireddy 	 * due to lack of space or some other problem, to skip any rollback
147243ce5d5SMadhukar Pappireddy 	 * steps (such as dropping the property name from the string table).
148243ce5d5SMadhukar Pappireddy 	 * This is safe to enable in most circumstances, even though it may
149243ce5d5SMadhukar Pappireddy 	 * leave the tree in a sub-optimal state.
150243ce5d5SMadhukar Pappireddy 	 */
151243ce5d5SMadhukar Pappireddy 	ASSUME_NO_ROLLBACK	= 1 << 3,
152243ce5d5SMadhukar Pappireddy 
153243ce5d5SMadhukar Pappireddy 	/*
154243ce5d5SMadhukar Pappireddy 	 * This assumes that the device tree components appear in a 'convenient'
155243ce5d5SMadhukar Pappireddy 	 * order, i.e. the memory reservation block first, then the structure
156243ce5d5SMadhukar Pappireddy 	 * block and finally the string block.
157243ce5d5SMadhukar Pappireddy 	 *
158243ce5d5SMadhukar Pappireddy 	 * This order is not specified by the device-tree specification,
159243ce5d5SMadhukar Pappireddy 	 * but is expected by libfdt. The device-tree compiler always created
160243ce5d5SMadhukar Pappireddy 	 * device trees with this order.
161243ce5d5SMadhukar Pappireddy 	 *
162243ce5d5SMadhukar Pappireddy 	 * This assumption disables a check in fdt_open_into() and removes the
163243ce5d5SMadhukar Pappireddy 	 * ability to fix the problem there. This is safe if you know that the
164243ce5d5SMadhukar Pappireddy 	 * device tree is correctly ordered. See fdt_blocks_misordered_().
165243ce5d5SMadhukar Pappireddy 	 */
166243ce5d5SMadhukar Pappireddy 	ASSUME_LIBFDT_ORDER	= 1 << 4,
167243ce5d5SMadhukar Pappireddy 
168243ce5d5SMadhukar Pappireddy 	/*
169243ce5d5SMadhukar Pappireddy 	 * This assumes that libfdt itself does not have any internal bugs. It
170243ce5d5SMadhukar Pappireddy 	 * drops certain checks that should never be needed unless libfdt has an
171243ce5d5SMadhukar Pappireddy 	 * undiscovered bug.
172243ce5d5SMadhukar Pappireddy 	 *
173243ce5d5SMadhukar Pappireddy 	 * This can generally be considered safe to enable.
174243ce5d5SMadhukar Pappireddy 	 */
175243ce5d5SMadhukar Pappireddy 	ASSUME_LIBFDT_FLAWLESS	= 1 << 5,
176243ce5d5SMadhukar Pappireddy };
177243ce5d5SMadhukar Pappireddy 
178243ce5d5SMadhukar Pappireddy /**
179243ce5d5SMadhukar Pappireddy  * can_assume_() - check if a particular assumption is enabled
180243ce5d5SMadhukar Pappireddy  *
181243ce5d5SMadhukar Pappireddy  * @mask: Mask to check (ASSUME_...)
182243ce5d5SMadhukar Pappireddy  * @return true if that assumption is enabled, else false
183243ce5d5SMadhukar Pappireddy  */
can_assume_(int mask)184243ce5d5SMadhukar Pappireddy static inline bool can_assume_(int mask)
185243ce5d5SMadhukar Pappireddy {
186243ce5d5SMadhukar Pappireddy 	return FDT_ASSUME_MASK & mask;
187243ce5d5SMadhukar Pappireddy }
188243ce5d5SMadhukar Pappireddy 
189243ce5d5SMadhukar Pappireddy /** helper macros for checking assumptions */
190243ce5d5SMadhukar Pappireddy #define can_assume(_assume)	can_assume_(ASSUME_ ## _assume)
191243ce5d5SMadhukar Pappireddy 
192630b011fSAntonio Nino Diaz #endif /* LIBFDT_INTERNAL_H */
193