1*3e6106c4SLoGin /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
2*3e6106c4SLoGin #ifndef LIBFDT_ENV_H
3*3e6106c4SLoGin #define LIBFDT_ENV_H
4*3e6106c4SLoGin /*
5*3e6106c4SLoGin * libfdt - Flat Device Tree manipulation
6*3e6106c4SLoGin * Copyright (C) 2006 David Gibson, IBM Corporation.
7*3e6106c4SLoGin * Copyright 2012 Kim Phillips, Freescale Semiconductor.
8*3e6106c4SLoGin */
9*3e6106c4SLoGin
10*3e6106c4SLoGin // #include <stdbool.h>
11*3e6106c4SLoGin // #include <stddef.h>
12*3e6106c4SLoGin // #include <stdint.h>
13*3e6106c4SLoGin // #include <stdlib.h>
14*3e6106c4SLoGin // #include <string.h>
15*3e6106c4SLoGin // #include <limits.h>
16*3e6106c4SLoGin #include <dragonstub/types.h>
17*3e6106c4SLoGin #include <dragonstub/limits.h>
18*3e6106c4SLoGin
19*3e6106c4SLoGin #ifdef __CHECKER__
20*3e6106c4SLoGin #define FDT_FORCE __attribute__((force))
21*3e6106c4SLoGin #define FDT_BITWISE __attribute__((bitwise))
22*3e6106c4SLoGin #else
23*3e6106c4SLoGin #define FDT_FORCE
24*3e6106c4SLoGin #define FDT_BITWISE
25*3e6106c4SLoGin #endif
26*3e6106c4SLoGin
27*3e6106c4SLoGin typedef uint16_t FDT_BITWISE fdt16_t;
28*3e6106c4SLoGin typedef uint32_t FDT_BITWISE fdt32_t;
29*3e6106c4SLoGin typedef uint64_t FDT_BITWISE fdt64_t;
30*3e6106c4SLoGin
31*3e6106c4SLoGin #define EXTRACT_BYTE(x, n) ((unsigned long long)((uint8_t *)&x)[n])
32*3e6106c4SLoGin #define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
33*3e6106c4SLoGin #define CPU_TO_FDT32(x) \
34*3e6106c4SLoGin ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
35*3e6106c4SLoGin (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
36*3e6106c4SLoGin #define CPU_TO_FDT64(x) \
37*3e6106c4SLoGin ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
38*3e6106c4SLoGin (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
39*3e6106c4SLoGin (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
40*3e6106c4SLoGin (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
41*3e6106c4SLoGin
fdt16_to_cpu(fdt16_t x)42*3e6106c4SLoGin static inline uint16_t fdt16_to_cpu(fdt16_t x)
43*3e6106c4SLoGin {
44*3e6106c4SLoGin return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
45*3e6106c4SLoGin }
cpu_to_fdt16(uint16_t x)46*3e6106c4SLoGin static inline fdt16_t cpu_to_fdt16(uint16_t x)
47*3e6106c4SLoGin {
48*3e6106c4SLoGin return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
49*3e6106c4SLoGin }
50*3e6106c4SLoGin
fdt32_to_cpu(fdt32_t x)51*3e6106c4SLoGin static inline uint32_t fdt32_to_cpu(fdt32_t x)
52*3e6106c4SLoGin {
53*3e6106c4SLoGin return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
54*3e6106c4SLoGin }
cpu_to_fdt32(uint32_t x)55*3e6106c4SLoGin static inline fdt32_t cpu_to_fdt32(uint32_t x)
56*3e6106c4SLoGin {
57*3e6106c4SLoGin return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
58*3e6106c4SLoGin }
59*3e6106c4SLoGin
fdt64_to_cpu(fdt64_t x)60*3e6106c4SLoGin static inline uint64_t fdt64_to_cpu(fdt64_t x)
61*3e6106c4SLoGin {
62*3e6106c4SLoGin return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
63*3e6106c4SLoGin }
cpu_to_fdt64(uint64_t x)64*3e6106c4SLoGin static inline fdt64_t cpu_to_fdt64(uint64_t x)
65*3e6106c4SLoGin {
66*3e6106c4SLoGin return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
67*3e6106c4SLoGin }
68*3e6106c4SLoGin #undef CPU_TO_FDT64
69*3e6106c4SLoGin #undef CPU_TO_FDT32
70*3e6106c4SLoGin #undef CPU_TO_FDT16
71*3e6106c4SLoGin #undef EXTRACT_BYTE
72*3e6106c4SLoGin
73*3e6106c4SLoGin #ifdef __APPLE__
74*3e6106c4SLoGin #include <AvailabilityMacros.h>
75*3e6106c4SLoGin
76*3e6106c4SLoGin /* strnlen() is not available on Mac OS < 10.7 */
77*3e6106c4SLoGin #if !defined(MAC_OS_X_VERSION_10_7) || \
78*3e6106c4SLoGin (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7)
79*3e6106c4SLoGin
80*3e6106c4SLoGin #define strnlen fdt_strnlen
81*3e6106c4SLoGin
82*3e6106c4SLoGin /*
83*3e6106c4SLoGin * fdt_strnlen: returns the length of a string or max_count - which ever is
84*3e6106c4SLoGin * smallest.
85*3e6106c4SLoGin * Input 1 string: the string whose size is to be determined
86*3e6106c4SLoGin * Input 2 max_count: the maximum value returned by this function
87*3e6106c4SLoGin * Output: length of the string or max_count (the smallest of the two)
88*3e6106c4SLoGin */
fdt_strnlen(const char * string,size_t max_count)89*3e6106c4SLoGin static inline size_t fdt_strnlen(const char *string, size_t max_count)
90*3e6106c4SLoGin {
91*3e6106c4SLoGin const char *p = memchr(string, 0, max_count);
92*3e6106c4SLoGin return p ? p - string : max_count;
93*3e6106c4SLoGin }
94*3e6106c4SLoGin
95*3e6106c4SLoGin #endif /* !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED <
96*3e6106c4SLoGin MAC_OS_X_VERSION_10_7) */
97*3e6106c4SLoGin
98*3e6106c4SLoGin #endif /* __APPLE__ */
99*3e6106c4SLoGin
100*3e6106c4SLoGin #endif /* LIBFDT_ENV_H */
101