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