1#ifndef LIBFDT_ENV_H
2#define LIBFDT_ENV_H
3/*
4 * libfdt - Flat Device Tree manipulation
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 * Copyright 2012 Kim Phillips, Freescale Semiconductor.
7 *
8 * libfdt is dual licensed: you can use it either under the terms of
9 * the GPL, or the BSD license, at your option.
10 *
11 *  a) This library is free software; you can redistribute it and/or
12 *     modify it under the terms of the GNU General Public License as
13 *     published by the Free Software Foundation; either version 2 of the
14 *     License, or (at your option) any later version.
15 *
16 *     This library is distributed in the hope that it will be useful,
17 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *     GNU General Public License for more details.
20 *
21 *     You should have received a copy of the GNU General Public
22 *     License along with this library; if not, write to the Free
23 *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24 *     MA 02110-1301 USA
25 *
26 * Alternatively,
27 *
28 *  b) Redistribution and use in source and binary forms, with or
29 *     without modification, are permitted provided that the following
30 *     conditions are met:
31 *
32 *     1. Redistributions of source code must retain the above
33 *        copyright notice, this list of conditions and the following
34 *        disclaimer.
35 *     2. Redistributions in binary form must reproduce the above
36 *        copyright notice, this list of conditions and the following
37 *        disclaimer in the documentation and/or other materials
38 *        provided with the distribution.
39 *
40 *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
41 *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
42 *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43 *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
45 *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
51 *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
52 *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 */
54
55#ifdef _KERNEL
56#include <sys/cdefs.h>
57#include <sys/param.h>
58#include <sys/types.h>
59#include <sys/systm.h>
60#include <sys/stdint.h>
61#else
62#include <stddef.h>
63#include <stdint.h>
64#include <stdlib.h>
65#include <string.h>
66#endif
67
68#ifdef __CHECKER__
69#define FDT_FORCE __attribute__((force))
70#define FDT_BITWISE __attribute__((bitwise))
71#else
72#define FDT_FORCE
73#define FDT_BITWISE
74#endif
75
76typedef uint16_t FDT_BITWISE fdt16_t;
77typedef uint32_t FDT_BITWISE fdt32_t;
78typedef uint64_t FDT_BITWISE fdt64_t;
79
80#define EXTRACT_BYTE(x, n)	((unsigned long long)((uint8_t *)&x)[n])
81#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
82#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
83			 (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
84#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
85			 (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
86			 (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
87			 (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
88
89static inline uint16_t fdt16_to_cpu(fdt16_t x)
90{
91	return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
92}
93static inline fdt16_t cpu_to_fdt16(uint16_t x)
94{
95	return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
96}
97
98static inline uint32_t fdt32_to_cpu(fdt32_t x)
99{
100	return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
101}
102static inline fdt32_t cpu_to_fdt32(uint32_t x)
103{
104	return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
105}
106
107static inline uint64_t fdt64_to_cpu(fdt64_t x)
108{
109	return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
110}
111static inline fdt64_t cpu_to_fdt64(uint64_t x)
112{
113	return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
114}
115#undef CPU_TO_FDT64
116#undef CPU_TO_FDT32
117#undef CPU_TO_FDT16
118#undef EXTRACT_BYTE
119
120#ifdef __APPLE__
121#include <AvailabilityMacros.h>
122
123/* strnlen() is not available on Mac OS < 10.7 */
124# if !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < \
125                                         MAC_OS_X_VERSION_10_7)
126
127#define strnlen fdt_strnlen
128
129/*
130 * fdt_strnlen: returns the length of a string or max_count - which ever is
131 * smallest.
132 * Input 1 string: the string whose size is to be determined
133 * Input 2 max_count: the maximum value returned by this function
134 * Output: length of the string or max_count (the smallest of the two)
135 */
136static inline size_t fdt_strnlen(const char *string, size_t max_count)
137{
138    const char *p = memchr(string, 0, max_count);
139    return p ? p - string : max_count;
140}
141
142#endif /* !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED <
143          MAC_OS_X_VERSION_10_7) */
144
145#endif /* __APPLE__ */
146
147#endif /* LIBFDT_ENV_H */
148