Linux Audio

Check our new training course

Loading...
v6.9.4
  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
 10#define FDT_ALIGN(x, a)		(((x) + (a) - 1) & ~((a) - 1))
 11#define FDT_TAGALIGN(x)		(FDT_ALIGN((x), FDT_TAGSIZE))
 12
 13int32_t fdt_ro_probe_(const void *fdt);
 14#define FDT_RO_PROBE(fdt)					\
 15	{							\
 16		int32_t totalsize_;				\
 17		if ((totalsize_ = fdt_ro_probe_(fdt)) < 0)	\
 18			return totalsize_;			\
 19	}
 20
 21int fdt_check_node_offset_(const void *fdt, int offset);
 22int fdt_check_prop_offset_(const void *fdt, int offset);
 23const char *fdt_find_string_(const char *strtab, int tabsize, const char *s);
 24int fdt_node_end_offset_(void *fdt, int nodeoffset);
 25
 26static inline const void *fdt_offset_ptr_(const void *fdt, int offset)
 27{
 28	return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
 29}
 30
 31static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
 32{
 33	return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
 34}
 35
 36static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n)
 37{
 38	const struct fdt_reserve_entry *rsv_table =
 39		(const struct fdt_reserve_entry *)
 40		((const char *)fdt + fdt_off_mem_rsvmap(fdt));
 41
 42	return rsv_table + n;
 43}
 44static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
 45{
 46	return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n);
 47}
 48
 49/*
 50 * Internal helpers to access tructural elements of the device tree
 51 * blob (rather than for exaple reading integers from within property
 52 * values).  We assume that we are either given a naturally aligned
 53 * address for the platform or if we are not, we are on a platform
 54 * where unaligned memory reads will be handled in a graceful manner.
 55 * If not the external helpers fdtXX_ld() from libfdt.h can be used
 56 * instead.
 57 */
 58static inline uint32_t fdt32_ld_(const fdt32_t *p)
 59{
 60	return fdt32_to_cpu(*p);
 61}
 62
 63static inline uint64_t fdt64_ld_(const fdt64_t *p)
 64{
 65	return fdt64_to_cpu(*p);
 66}
 67
 68#define FDT_SW_MAGIC		(~FDT_MAGIC)
 69
 70/**********************************************************************/
 71/* Checking controls                                                  */
 72/**********************************************************************/
 73
 74#ifndef FDT_ASSUME_MASK
 75#define FDT_ASSUME_MASK 0
 76#endif
 77
 78/*
 79 * Defines assumptions which can be enabled. Each of these can be enabled
 80 * individually. For maximum safety, don't enable any assumptions!
 81 *
 82 * For minimal code size and no safety, use ASSUME_PERFECT at your own risk.
 83 * You should have another method of validating the device tree, such as a
 84 * signature or hash check before using libfdt.
 85 *
 86 * For situations where security is not a concern it may be safe to enable
 87 * ASSUME_SANE.
 88 */
 89enum {
 90	/*
 91	 * This does essentially no checks. Only the latest device-tree
 92	 * version is correctly handled. Inconsistencies or errors in the device
 93	 * tree may cause undefined behaviour or crashes. Invalid parameters
 94	 * passed to libfdt may do the same.
 95	 *
 96	 * If an error occurs when modifying the tree it may leave the tree in
 97	 * an intermediate (but valid) state. As an example, adding a property
 98	 * where there is insufficient space may result in the property name
 99	 * being added to the string table even though the property itself is
100	 * not added to the struct section.
101	 *
102	 * Only use this if you have a fully validated device tree with
103	 * the latest supported version and wish to minimise code size.
104	 */
105	ASSUME_PERFECT		= 0xff,
106
107	/*
108	 * This assumes that the device tree is sane. i.e. header metadata
109	 * and basic hierarchy are correct.
110	 *
111	 * With this assumption enabled, normal device trees produced by libfdt
112	 * and the compiler should be handled safely. Malicious device trees and
113	 * complete garbage may cause libfdt to behave badly or crash. Truncated
114	 * device trees (e.g. those only partially loaded) can also cause
115	 * problems.
116	 *
117	 * Note: Only checks that relate exclusively to the device tree itself
118	 * (not the parameters passed to libfdt) are disabled by this
119	 * assumption. This includes checking headers, tags and the like.
120	 */
121	ASSUME_VALID_DTB	= 1 << 0,
122
123	/*
124	 * This builds on ASSUME_VALID_DTB and further assumes that libfdt
125	 * functions are called with valid parameters, i.e. not trigger
126	 * FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any
127	 * extensive checking of parameters and the device tree, making various
128	 * assumptions about correctness.
129	 *
130	 * It doesn't make sense to enable this assumption unless
131	 * ASSUME_VALID_DTB is also enabled.
132	 */
133	ASSUME_VALID_INPUT	= 1 << 1,
134
135	/*
136	 * This disables checks for device-tree version and removes all code
137	 * which handles older versions.
138	 *
139	 * Only enable this if you know you have a device tree with the latest
140	 * version.
141	 */
142	ASSUME_LATEST		= 1 << 2,
143
144	/*
145	 * This assumes that it is OK for a failed addition to the device tree,
146	 * due to lack of space or some other problem, to skip any rollback
147	 * steps (such as dropping the property name from the string table).
148	 * This is safe to enable in most circumstances, even though it may
149	 * leave the tree in a sub-optimal state.
150	 */
151	ASSUME_NO_ROLLBACK	= 1 << 3,
152
153	/*
154	 * This assumes that the device tree components appear in a 'convenient'
155	 * order, i.e. the memory reservation block first, then the structure
156	 * block and finally the string block.
157	 *
158	 * This order is not specified by the device-tree specification,
159	 * but is expected by libfdt. The device-tree compiler always created
160	 * device trees with this order.
161	 *
162	 * This assumption disables a check in fdt_open_into() and removes the
163	 * ability to fix the problem there. This is safe if you know that the
164	 * device tree is correctly ordered. See fdt_blocks_misordered_().
165	 */
166	ASSUME_LIBFDT_ORDER	= 1 << 4,
167
168	/*
169	 * This assumes that libfdt itself does not have any internal bugs. It
170	 * drops certain checks that should never be needed unless libfdt has an
171	 * undiscovered bug.
172	 *
173	 * This can generally be considered safe to enable.
174	 */
175	ASSUME_LIBFDT_FLAWLESS	= 1 << 5,
176};
177
178/**
179 * can_assume_() - check if a particular assumption is enabled
180 *
181 * @mask: Mask to check (ASSUME_...)
182 * @return true if that assumption is enabled, else false
183 */
184static inline bool can_assume_(int mask)
185{
186	return FDT_ASSUME_MASK & mask;
187}
188
189/** helper macros for checking assumptions */
190#define can_assume(_assume)	can_assume_(ASSUME_ ## _assume)
191
192#endif /* LIBFDT_INTERNAL_H */
v3.15
 1#ifndef _LIBFDT_INTERNAL_H
 2#define _LIBFDT_INTERNAL_H
 
 3/*
 4 * libfdt - Flat Device Tree manipulation
 5 * Copyright (C) 2006 David Gibson, IBM Corporation.
 6 *
 7 * libfdt is dual licensed: you can use it either under the terms of
 8 * the GPL, or the BSD license, at your option.
 9 *
10 *  a) This library is free software; you can redistribute it and/or
11 *     modify it under the terms of the GNU General Public License as
12 *     published by the Free Software Foundation; either version 2 of the
13 *     License, or (at your option) any later version.
14 *
15 *     This library is distributed in the hope that it will be useful,
16 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *     GNU General Public License for more details.
19 *
20 *     You should have received a copy of the GNU General Public
21 *     License along with this library; if not, write to the Free
22 *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 *     MA 02110-1301 USA
24 *
25 * Alternatively,
26 *
27 *  b) Redistribution and use in source and binary forms, with or
28 *     without modification, are permitted provided that the following
29 *     conditions are met:
30 *
31 *     1. Redistributions of source code must retain the above
32 *        copyright notice, this list of conditions and the following
33 *        disclaimer.
34 *     2. Redistributions in binary form must reproduce the above
35 *        copyright notice, this list of conditions and the following
36 *        disclaimer in the documentation and/or other materials
37 *        provided with the distribution.
38 *
39 *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
40 *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
41 *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42 *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
44 *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
50 *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
51 *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53#include <fdt.h>
54
55#define FDT_ALIGN(x, a)		(((x) + (a) - 1) & ~((a) - 1))
56#define FDT_TAGALIGN(x)		(FDT_ALIGN((x), FDT_TAGSIZE))
57
58#define FDT_CHECK_HEADER(fdt) \
59	{ \
60		int err; \
61		if ((err = fdt_check_header(fdt)) != 0) \
62			return err; \
 
63	}
64
65int _fdt_check_node_offset(const void *fdt, int offset);
66int _fdt_check_prop_offset(const void *fdt, int offset);
67const char *_fdt_find_string(const char *strtab, int tabsize, const char *s);
68int _fdt_node_end_offset(void *fdt, int nodeoffset);
69
70static inline const void *_fdt_offset_ptr(const void *fdt, int offset)
71{
72	return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
73}
74
75static inline void *_fdt_offset_ptr_w(void *fdt, int offset)
76{
77	return (void *)(uintptr_t)_fdt_offset_ptr(fdt, offset);
78}
79
80static inline const struct fdt_reserve_entry *_fdt_mem_rsv(const void *fdt, int n)
81{
82	const struct fdt_reserve_entry *rsv_table =
83		(const struct fdt_reserve_entry *)
84		((const char *)fdt + fdt_off_mem_rsvmap(fdt));
85
86	return rsv_table + n;
87}
88static inline struct fdt_reserve_entry *_fdt_mem_rsv_w(void *fdt, int n)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89{
90	return (void *)(uintptr_t)_fdt_mem_rsv(fdt, n);
91}
92
93#define FDT_SW_MAGIC		(~FDT_MAGIC)
94
95#endif /* _LIBFDT_INTERNAL_H */