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