Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
0002 #ifndef LIBFDT_INTERNAL_H
0003 #define LIBFDT_INTERNAL_H
0004 /*
0005  * libfdt - Flat Device Tree manipulation
0006  * Copyright (C) 2006 David Gibson, IBM Corporation.
0007  */
0008 #include <fdt.h>
0009 
0010 #define FDT_ALIGN(x, a)     (((x) + (a) - 1) & ~((a) - 1))
0011 #define FDT_TAGALIGN(x)     (FDT_ALIGN((x), FDT_TAGSIZE))
0012 
0013 int32_t fdt_ro_probe_(const void *fdt);
0014 #define FDT_RO_PROBE(fdt)                   \
0015     {                           \
0016         int32_t totalsize_;             \
0017         if ((totalsize_ = fdt_ro_probe_(fdt)) < 0)  \
0018             return totalsize_;          \
0019     }
0020 
0021 int fdt_check_node_offset_(const void *fdt, int offset);
0022 int fdt_check_prop_offset_(const void *fdt, int offset);
0023 const char *fdt_find_string_(const char *strtab, int tabsize, const char *s);
0024 int fdt_node_end_offset_(void *fdt, int nodeoffset);
0025 
0026 static inline const void *fdt_offset_ptr_(const void *fdt, int offset)
0027 {
0028     return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
0029 }
0030 
0031 static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
0032 {
0033     return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
0034 }
0035 
0036 static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n)
0037 {
0038     const struct fdt_reserve_entry *rsv_table =
0039         (const struct fdt_reserve_entry *)
0040         ((const char *)fdt + fdt_off_mem_rsvmap(fdt));
0041 
0042     return rsv_table + n;
0043 }
0044 static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
0045 {
0046     return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n);
0047 }
0048 
0049 /*
0050  * Internal helpers to access tructural elements of the device tree
0051  * blob (rather than for exaple reading integers from within property
0052  * values).  We assume that we are either given a naturally aligned
0053  * address for the platform or if we are not, we are on a platform
0054  * where unaligned memory reads will be handled in a graceful manner.
0055  * If not the external helpers fdtXX_ld() from libfdt.h can be used
0056  * instead.
0057  */
0058 static inline uint32_t fdt32_ld_(const fdt32_t *p)
0059 {
0060     return fdt32_to_cpu(*p);
0061 }
0062 
0063 static inline uint64_t fdt64_ld_(const fdt64_t *p)
0064 {
0065     return fdt64_to_cpu(*p);
0066 }
0067 
0068 #define FDT_SW_MAGIC        (~FDT_MAGIC)
0069 
0070 /**********************************************************************/
0071 /* Checking controls                                                  */
0072 /**********************************************************************/
0073 
0074 #ifndef FDT_ASSUME_MASK
0075 #define FDT_ASSUME_MASK 0
0076 #endif
0077 
0078 /*
0079  * Defines assumptions which can be enabled. Each of these can be enabled
0080  * individually. For maximum safety, don't enable any assumptions!
0081  *
0082  * For minimal code size and no safety, use ASSUME_PERFECT at your own risk.
0083  * You should have another method of validating the device tree, such as a
0084  * signature or hash check before using libfdt.
0085  *
0086  * For situations where security is not a concern it may be safe to enable
0087  * ASSUME_SANE.
0088  */
0089 enum {
0090     /*
0091      * This does essentially no checks. Only the latest device-tree
0092      * version is correctly handled. Inconsistencies or errors in the device
0093      * tree may cause undefined behaviour or crashes. Invalid parameters
0094      * passed to libfdt may do the same.
0095      *
0096      * If an error occurs when modifying the tree it may leave the tree in
0097      * an intermediate (but valid) state. As an example, adding a property
0098      * where there is insufficient space may result in the property name
0099      * being added to the string table even though the property itself is
0100      * not added to the struct section.
0101      *
0102      * Only use this if you have a fully validated device tree with
0103      * the latest supported version and wish to minimise code size.
0104      */
0105     ASSUME_PERFECT      = 0xff,
0106 
0107     /*
0108      * This assumes that the device tree is sane. i.e. header metadata
0109      * and basic hierarchy are correct.
0110      *
0111      * With this assumption enabled, normal device trees produced by libfdt
0112      * and the compiler should be handled safely. Malicious device trees and
0113      * complete garbage may cause libfdt to behave badly or crash. Truncated
0114      * device trees (e.g. those only partially loaded) can also cause
0115      * problems.
0116      *
0117      * Note: Only checks that relate exclusively to the device tree itself
0118      * (not the parameters passed to libfdt) are disabled by this
0119      * assumption. This includes checking headers, tags and the like.
0120      */
0121     ASSUME_VALID_DTB    = 1 << 0,
0122 
0123     /*
0124      * This builds on ASSUME_VALID_DTB and further assumes that libfdt
0125      * functions are called with valid parameters, i.e. not trigger
0126      * FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any
0127      * extensive checking of parameters and the device tree, making various
0128      * assumptions about correctness.
0129      *
0130      * It doesn't make sense to enable this assumption unless
0131      * ASSUME_VALID_DTB is also enabled.
0132      */
0133     ASSUME_VALID_INPUT  = 1 << 1,
0134 
0135     /*
0136      * This disables checks for device-tree version and removes all code
0137      * which handles older versions.
0138      *
0139      * Only enable this if you know you have a device tree with the latest
0140      * version.
0141      */
0142     ASSUME_LATEST       = 1 << 2,
0143 
0144     /*
0145      * This assumes that it is OK for a failed addition to the device tree,
0146      * due to lack of space or some other problem, to skip any rollback
0147      * steps (such as dropping the property name from the string table).
0148      * This is safe to enable in most circumstances, even though it may
0149      * leave the tree in a sub-optimal state.
0150      */
0151     ASSUME_NO_ROLLBACK  = 1 << 3,
0152 
0153     /*
0154      * This assumes that the device tree components appear in a 'convenient'
0155      * order, i.e. the memory reservation block first, then the structure
0156      * block and finally the string block.
0157      *
0158      * This order is not specified by the device-tree specification,
0159      * but is expected by libfdt. The device-tree compiler always created
0160      * device trees with this order.
0161      *
0162      * This assumption disables a check in fdt_open_into() and removes the
0163      * ability to fix the problem there. This is safe if you know that the
0164      * device tree is correctly ordered. See fdt_blocks_misordered_().
0165      */
0166     ASSUME_LIBFDT_ORDER = 1 << 4,
0167 
0168     /*
0169      * This assumes that libfdt itself does not have any internal bugs. It
0170      * drops certain checks that should never be needed unless libfdt has an
0171      * undiscovered bug.
0172      *
0173      * This can generally be considered safe to enable.
0174      */
0175     ASSUME_LIBFDT_FLAWLESS  = 1 << 5,
0176 };
0177 
0178 /**
0179  * can_assume_() - check if a particular assumption is enabled
0180  *
0181  * @mask: Mask to check (ASSUME_...)
0182  * @return true if that assumption is enabled, else false
0183  */
0184 static inline bool can_assume_(int mask)
0185 {
0186     return FDT_ASSUME_MASK & mask;
0187 }
0188 
0189 /** helper macros for checking assumptions */
0190 #define can_assume(_assume) can_assume_(ASSUME_ ## _assume)
0191 
0192 #endif /* LIBFDT_INTERNAL_H */