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