RTEMS  5.1
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 
10 #define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
11 #define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE))
12 
13 int32_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 
21 int fdt_check_node_offset_(const void *fdt, int offset);
22 int fdt_check_prop_offset_(const void *fdt, int offset);
23 const char *fdt_find_string_(const char *strtab, int tabsize, const char *s);
24 int fdt_node_end_offset_(void *fdt, int nodeoffset);
25 
26 static 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 
31 static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
32 {
33  return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
34 }
35 
36 static 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 }
44 static 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 #define FDT_SW_MAGIC (~FDT_MAGIC)
50 
51 /**********************************************************************/
52 /* Checking controls */
53 /**********************************************************************/
54 
55 #ifndef FDT_ASSUME_MASK
56 #define FDT_ASSUME_MASK 0
57 #endif
58 
59 /*
60  * Defines assumptions which can be enabled. Each of these can be enabled
61  * individually. For maximum saftey, don't enable any assumptions!
62  *
63  * For minimal code size and no safety, use ASSUME_PERFECT at your own risk.
64  * You should have another method of validating the device tree, such as a
65  * signature or hash check before using libfdt.
66  *
67  * For situations where security is not a concern it may be safe to enable
68  * ASSUME_SANE.
69  */
70 enum {
71  /*
72  * This does essentially no checks. Only the latest device-tree
73  * version is correctly handled. Inconsistencies or errors in the device
74  * tree may cause undefined behaviour or crashes. Invalid parameters
75  * passed to libfdt may do the same.
76  *
77  * If an error occurs when modifying the tree it may leave the tree in
78  * an intermediate (but valid) state. As an example, adding a property
79  * where there is insufficient space may result in the property name
80  * being added to the string table even though the property itself is
81  * not added to the struct section.
82  *
83  * Only use this if you have a fully validated device tree with
84  * the latest supported version and wish to minimise code size.
85  */
86  ASSUME_PERFECT = 0xff,
87 
88  /*
89  * This assumes that the device tree is sane. i.e. header metadata
90  * and basic hierarchy are correct.
91  *
92  * With this assumption enabled, normal device trees produced by libfdt
93  * and the compiler should be handled safely. Malicious device trees and
94  * complete garbage may cause libfdt to behave badly or crash.
95  *
96  * Note: Only checks that relate exclusively to the device tree itself
97  * (not the parameters passed to libfdt) are disabled by this
98  * assumption. This includes checking headers, tags and the like.
99  */
100  ASSUME_VALID_DTB = 1 << 0,
101 
102  /*
103  * This builds on ASSUME_VALID_DTB and further assumes that libfdt
104  * functions are called with valid parameters, i.e. not trigger
105  * FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any
106  * extensive checking of parameters and the device tree, making various
107  * assumptions about correctness.
108  *
109  * It doesn't make sense to enable this assumption unless
110  * ASSUME_VALID_DTB is also enabled.
111  */
112  ASSUME_VALID_INPUT = 1 << 1,
113 
114  /*
115  * This disables checks for device-tree version and removes all code
116  * which handles older versions.
117  *
118  * Only enable this if you know you have a device tree with the latest
119  * version.
120  */
121  ASSUME_LATEST = 1 << 2,
122 
123  /*
124  * This assume that it is OK for a failed additional to the device tree
125  * due to lack of space or some other problem can skip any rollback
126  * steps (such as dropping the property name from the string table).
127  * This is safe to enable in most circumstances, even though it may
128  * leave the tree in a sub-optimal state.
129  */
130  ASSUME_NO_ROLLBACK = 1 << 3,
131 
132  /*
133  * This assumes that the device tree components appear in the correct
134  * order. As such it disables a check in fdt_open_into() and removes the
135  * ability to fix the problem there. This is safe if you know that the
136  * device tree is correctly ordered. See fdt_blocks_misordered_().
137  */
138  ASSUME_LIBFDT_ORDER = 1 << 4,
139 };
140 
147 static inline bool can_assume_(int mask)
148 {
149  return FDT_ASSUME_MASK & mask;
150 }
151 
153 #define can_assume(_assume) can_assume_(ASSUME_ ## _assume)
154 
155 #endif /* LIBFDT_INTERNAL_H */
Definition: fdt.h:31