162306a36Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */ 262306a36Sopenharmony_ci#ifndef LIBFDT_INTERNAL_H 362306a36Sopenharmony_ci#define LIBFDT_INTERNAL_H 462306a36Sopenharmony_ci/* 562306a36Sopenharmony_ci * libfdt - Flat Device Tree manipulation 662306a36Sopenharmony_ci * Copyright (C) 2006 David Gibson, IBM Corporation. 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci#include <fdt.h> 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 1162306a36Sopenharmony_ci#define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE)) 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ciint32_t fdt_ro_probe_(const void *fdt); 1462306a36Sopenharmony_ci#define FDT_RO_PROBE(fdt) \ 1562306a36Sopenharmony_ci { \ 1662306a36Sopenharmony_ci int32_t totalsize_; \ 1762306a36Sopenharmony_ci if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \ 1862306a36Sopenharmony_ci return totalsize_; \ 1962306a36Sopenharmony_ci } 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciint fdt_check_node_offset_(const void *fdt, int offset); 2262306a36Sopenharmony_ciint fdt_check_prop_offset_(const void *fdt, int offset); 2362306a36Sopenharmony_ciconst char *fdt_find_string_(const char *strtab, int tabsize, const char *s); 2462306a36Sopenharmony_ciint fdt_node_end_offset_(void *fdt, int nodeoffset); 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_cistatic inline const void *fdt_offset_ptr_(const void *fdt, int offset) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci return (const char *)fdt + fdt_off_dt_struct(fdt) + offset; 2962306a36Sopenharmony_ci} 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_cistatic inline void *fdt_offset_ptr_w_(void *fdt, int offset) 3262306a36Sopenharmony_ci{ 3362306a36Sopenharmony_ci return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset); 3462306a36Sopenharmony_ci} 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_cistatic inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci const struct fdt_reserve_entry *rsv_table = 3962306a36Sopenharmony_ci (const struct fdt_reserve_entry *) 4062306a36Sopenharmony_ci ((const char *)fdt + fdt_off_mem_rsvmap(fdt)); 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci return rsv_table + n; 4362306a36Sopenharmony_ci} 4462306a36Sopenharmony_cistatic inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n) 4562306a36Sopenharmony_ci{ 4662306a36Sopenharmony_ci return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n); 4762306a36Sopenharmony_ci} 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci/* 5062306a36Sopenharmony_ci * Internal helpers to access tructural elements of the device tree 5162306a36Sopenharmony_ci * blob (rather than for exaple reading integers from within property 5262306a36Sopenharmony_ci * values). We assume that we are either given a naturally aligned 5362306a36Sopenharmony_ci * address for the platform or if we are not, we are on a platform 5462306a36Sopenharmony_ci * where unaligned memory reads will be handled in a graceful manner. 5562306a36Sopenharmony_ci * If not the external helpers fdtXX_ld() from libfdt.h can be used 5662306a36Sopenharmony_ci * instead. 5762306a36Sopenharmony_ci */ 5862306a36Sopenharmony_cistatic inline uint32_t fdt32_ld_(const fdt32_t *p) 5962306a36Sopenharmony_ci{ 6062306a36Sopenharmony_ci return fdt32_to_cpu(*p); 6162306a36Sopenharmony_ci} 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_cistatic inline uint64_t fdt64_ld_(const fdt64_t *p) 6462306a36Sopenharmony_ci{ 6562306a36Sopenharmony_ci return fdt64_to_cpu(*p); 6662306a36Sopenharmony_ci} 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci#define FDT_SW_MAGIC (~FDT_MAGIC) 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci/**********************************************************************/ 7162306a36Sopenharmony_ci/* Checking controls */ 7262306a36Sopenharmony_ci/**********************************************************************/ 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci#ifndef FDT_ASSUME_MASK 7562306a36Sopenharmony_ci#define FDT_ASSUME_MASK 0 7662306a36Sopenharmony_ci#endif 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci/* 7962306a36Sopenharmony_ci * Defines assumptions which can be enabled. Each of these can be enabled 8062306a36Sopenharmony_ci * individually. For maximum safety, don't enable any assumptions! 8162306a36Sopenharmony_ci * 8262306a36Sopenharmony_ci * For minimal code size and no safety, use ASSUME_PERFECT at your own risk. 8362306a36Sopenharmony_ci * You should have another method of validating the device tree, such as a 8462306a36Sopenharmony_ci * signature or hash check before using libfdt. 8562306a36Sopenharmony_ci * 8662306a36Sopenharmony_ci * For situations where security is not a concern it may be safe to enable 8762306a36Sopenharmony_ci * ASSUME_SANE. 8862306a36Sopenharmony_ci */ 8962306a36Sopenharmony_cienum { 9062306a36Sopenharmony_ci /* 9162306a36Sopenharmony_ci * This does essentially no checks. Only the latest device-tree 9262306a36Sopenharmony_ci * version is correctly handled. Inconsistencies or errors in the device 9362306a36Sopenharmony_ci * tree may cause undefined behaviour or crashes. Invalid parameters 9462306a36Sopenharmony_ci * passed to libfdt may do the same. 9562306a36Sopenharmony_ci * 9662306a36Sopenharmony_ci * If an error occurs when modifying the tree it may leave the tree in 9762306a36Sopenharmony_ci * an intermediate (but valid) state. As an example, adding a property 9862306a36Sopenharmony_ci * where there is insufficient space may result in the property name 9962306a36Sopenharmony_ci * being added to the string table even though the property itself is 10062306a36Sopenharmony_ci * not added to the struct section. 10162306a36Sopenharmony_ci * 10262306a36Sopenharmony_ci * Only use this if you have a fully validated device tree with 10362306a36Sopenharmony_ci * the latest supported version and wish to minimise code size. 10462306a36Sopenharmony_ci */ 10562306a36Sopenharmony_ci ASSUME_PERFECT = 0xff, 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci /* 10862306a36Sopenharmony_ci * This assumes that the device tree is sane. i.e. header metadata 10962306a36Sopenharmony_ci * and basic hierarchy are correct. 11062306a36Sopenharmony_ci * 11162306a36Sopenharmony_ci * With this assumption enabled, normal device trees produced by libfdt 11262306a36Sopenharmony_ci * and the compiler should be handled safely. Malicious device trees and 11362306a36Sopenharmony_ci * complete garbage may cause libfdt to behave badly or crash. Truncated 11462306a36Sopenharmony_ci * device trees (e.g. those only partially loaded) can also cause 11562306a36Sopenharmony_ci * problems. 11662306a36Sopenharmony_ci * 11762306a36Sopenharmony_ci * Note: Only checks that relate exclusively to the device tree itself 11862306a36Sopenharmony_ci * (not the parameters passed to libfdt) are disabled by this 11962306a36Sopenharmony_ci * assumption. This includes checking headers, tags and the like. 12062306a36Sopenharmony_ci */ 12162306a36Sopenharmony_ci ASSUME_VALID_DTB = 1 << 0, 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci /* 12462306a36Sopenharmony_ci * This builds on ASSUME_VALID_DTB and further assumes that libfdt 12562306a36Sopenharmony_ci * functions are called with valid parameters, i.e. not trigger 12662306a36Sopenharmony_ci * FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any 12762306a36Sopenharmony_ci * extensive checking of parameters and the device tree, making various 12862306a36Sopenharmony_ci * assumptions about correctness. 12962306a36Sopenharmony_ci * 13062306a36Sopenharmony_ci * It doesn't make sense to enable this assumption unless 13162306a36Sopenharmony_ci * ASSUME_VALID_DTB is also enabled. 13262306a36Sopenharmony_ci */ 13362306a36Sopenharmony_ci ASSUME_VALID_INPUT = 1 << 1, 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci /* 13662306a36Sopenharmony_ci * This disables checks for device-tree version and removes all code 13762306a36Sopenharmony_ci * which handles older versions. 13862306a36Sopenharmony_ci * 13962306a36Sopenharmony_ci * Only enable this if you know you have a device tree with the latest 14062306a36Sopenharmony_ci * version. 14162306a36Sopenharmony_ci */ 14262306a36Sopenharmony_ci ASSUME_LATEST = 1 << 2, 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci /* 14562306a36Sopenharmony_ci * This assumes that it is OK for a failed addition to the device tree, 14662306a36Sopenharmony_ci * due to lack of space or some other problem, to skip any rollback 14762306a36Sopenharmony_ci * steps (such as dropping the property name from the string table). 14862306a36Sopenharmony_ci * This is safe to enable in most circumstances, even though it may 14962306a36Sopenharmony_ci * leave the tree in a sub-optimal state. 15062306a36Sopenharmony_ci */ 15162306a36Sopenharmony_ci ASSUME_NO_ROLLBACK = 1 << 3, 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci /* 15462306a36Sopenharmony_ci * This assumes that the device tree components appear in a 'convenient' 15562306a36Sopenharmony_ci * order, i.e. the memory reservation block first, then the structure 15662306a36Sopenharmony_ci * block and finally the string block. 15762306a36Sopenharmony_ci * 15862306a36Sopenharmony_ci * This order is not specified by the device-tree specification, 15962306a36Sopenharmony_ci * but is expected by libfdt. The device-tree compiler always created 16062306a36Sopenharmony_ci * device trees with this order. 16162306a36Sopenharmony_ci * 16262306a36Sopenharmony_ci * This assumption disables a check in fdt_open_into() and removes the 16362306a36Sopenharmony_ci * ability to fix the problem there. This is safe if you know that the 16462306a36Sopenharmony_ci * device tree is correctly ordered. See fdt_blocks_misordered_(). 16562306a36Sopenharmony_ci */ 16662306a36Sopenharmony_ci ASSUME_LIBFDT_ORDER = 1 << 4, 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci /* 16962306a36Sopenharmony_ci * This assumes that libfdt itself does not have any internal bugs. It 17062306a36Sopenharmony_ci * drops certain checks that should never be needed unless libfdt has an 17162306a36Sopenharmony_ci * undiscovered bug. 17262306a36Sopenharmony_ci * 17362306a36Sopenharmony_ci * This can generally be considered safe to enable. 17462306a36Sopenharmony_ci */ 17562306a36Sopenharmony_ci ASSUME_LIBFDT_FLAWLESS = 1 << 5, 17662306a36Sopenharmony_ci}; 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci/** 17962306a36Sopenharmony_ci * can_assume_() - check if a particular assumption is enabled 18062306a36Sopenharmony_ci * 18162306a36Sopenharmony_ci * @mask: Mask to check (ASSUME_...) 18262306a36Sopenharmony_ci * @return true if that assumption is enabled, else false 18362306a36Sopenharmony_ci */ 18462306a36Sopenharmony_cistatic inline bool can_assume_(int mask) 18562306a36Sopenharmony_ci{ 18662306a36Sopenharmony_ci return FDT_ASSUME_MASK & mask; 18762306a36Sopenharmony_ci} 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci/** helper macros for checking assumptions */ 19062306a36Sopenharmony_ci#define can_assume(_assume) can_assume_(ASSUME_ ## _assume) 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci#endif /* LIBFDT_INTERNAL_H */ 193