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