143e00a9kevans#ifndef LIBFDT_ENV_H
243e00a9kevans#define LIBFDT_ENV_H
3c4b53c8imp/*
4c4b53c8imp * libfdt - Flat Device Tree manipulation
5c4b53c8imp * Copyright (C) 2006 David Gibson, IBM Corporation.
6c4b53c8imp * Copyright 2012 Kim Phillips, Freescale Semiconductor.
7c4b53c8imp *
8c4b53c8imp * libfdt is dual licensed: you can use it either under the terms of
9c4b53c8imp * the GPL, or the BSD license, at your option.
10c4b53c8imp *
11c4b53c8imp *  a) This library is free software; you can redistribute it and/or
12c4b53c8imp *     modify it under the terms of the GNU General Public License as
13c4b53c8imp *     published by the Free Software Foundation; either version 2 of the
14c4b53c8imp *     License, or (at your option) any later version.
15c4b53c8imp *
16c4b53c8imp *     This library is distributed in the hope that it will be useful,
17c4b53c8imp *     but WITHOUT ANY WARRANTY; without even the implied warranty of
18c4b53c8imp *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19c4b53c8imp *     GNU General Public License for more details.
20c4b53c8imp *
21c4b53c8imp *     You should have received a copy of the GNU General Public
22c4b53c8imp *     License along with this library; if not, write to the Free
23c4b53c8imp *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24c4b53c8imp *     MA 02110-1301 USA
25c4b53c8imp *
26c4b53c8imp * Alternatively,
27c4b53c8imp *
28c4b53c8imp *  b) Redistribution and use in source and binary forms, with or
29c4b53c8imp *     without modification, are permitted provided that the following
30c4b53c8imp *     conditions are met:
31c4b53c8imp *
32c4b53c8imp *     1. Redistributions of source code must retain the above
33c4b53c8imp *        copyright notice, this list of conditions and the following
34c4b53c8imp *        disclaimer.
35c4b53c8imp *     2. Redistributions in binary form must reproduce the above
36c4b53c8imp *        copyright notice, this list of conditions and the following
37c4b53c8imp *        disclaimer in the documentation and/or other materials
38c4b53c8imp *        provided with the distribution.
39c4b53c8imp *
40c4b53c8imp *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
41c4b53c8imp *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
42c4b53c8imp *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43c4b53c8imp *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44c4b53c8imp *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
45c4b53c8imp *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46c4b53c8imp *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47c4b53c8imp *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48c4b53c8imp *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49c4b53c8imp *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50c4b53c8imp *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
51c4b53c8imp *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
52c4b53c8imp *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53c4b53c8imp */
54220c940raj
55425b046raj#ifdef _KERNEL
56425b046raj#include <sys/cdefs.h>
57425b046raj#include <sys/param.h>
58425b046raj#include <sys/types.h>
59425b046raj#include <sys/systm.h>
60425b046raj#include <sys/stdint.h>
61425b046raj#else
62220c940raj#include <stddef.h>
63220c940raj#include <stdint.h>
64d857593gonzo#include <stdlib.h>
65220c940raj#include <string.h>
66425b046raj#endif
67220c940raj
68c4b53c8imp#ifdef __CHECKER__
6943e00a9kevans#define FDT_FORCE __attribute__((force))
7043e00a9kevans#define FDT_BITWISE __attribute__((bitwise))
71c4b53c8imp#else
7243e00a9kevans#define FDT_FORCE
7343e00a9kevans#define FDT_BITWISE
74c4b53c8imp#endif
75c4b53c8imp
7643e00a9kevanstypedef uint16_t FDT_BITWISE fdt16_t;
7743e00a9kevanstypedef uint32_t FDT_BITWISE fdt32_t;
7843e00a9kevanstypedef uint64_t FDT_BITWISE fdt64_t;
79c4b53c8imp
80c4b53c8imp#define EXTRACT_BYTE(x, n)	((unsigned long long)((uint8_t *)&x)[n])
81c4b53c8imp#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
82c4b53c8imp#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
83c4b53c8imp			 (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
84c4b53c8imp#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
85c4b53c8imp			 (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
86c4b53c8imp			 (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
87c4b53c8imp			 (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
88c4b53c8imp
89c4b53c8impstatic inline uint16_t fdt16_to_cpu(fdt16_t x)
90bcb4f0dimp{
9143e00a9kevans	return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
92c4b53c8imp}
93c4b53c8impstatic inline fdt16_t cpu_to_fdt16(uint16_t x)
94bcb4f0dimp{
9543e00a9kevans	return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
96bcb4f0dimp}
97bcb4f0dimp
98c4b53c8impstatic inline uint32_t fdt32_to_cpu(fdt32_t x)
99220c940raj{
10043e00a9kevans	return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
101c4b53c8imp}
102c4b53c8impstatic inline fdt32_t cpu_to_fdt32(uint32_t x)
103c4b53c8imp{
10443e00a9kevans	return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
105220c940raj}
106220c940raj
107c4b53c8impstatic inline uint64_t fdt64_to_cpu(fdt64_t x)
108220c940raj{
10943e00a9kevans	return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
110220c940raj}
111c4b53c8impstatic inline fdt64_t cpu_to_fdt64(uint64_t x)
112220c940raj{
11343e00a9kevans	return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
114220c940raj}
115c4b53c8imp#undef CPU_TO_FDT64
116c4b53c8imp#undef CPU_TO_FDT32
117c4b53c8imp#undef CPU_TO_FDT16
118bcb4f0dimp#undef EXTRACT_BYTE
119220c940raj
12043e00a9kevans#ifdef __APPLE__
12143e00a9kevans#include <AvailabilityMacros.h>
12243e00a9kevans
12343e00a9kevans/* strnlen() is not available on Mac OS < 10.7 */
12443e00a9kevans# if !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < \
12543e00a9kevans                                         MAC_OS_X_VERSION_10_7)
12643e00a9kevans
12743e00a9kevans#define strnlen fdt_strnlen
12843e00a9kevans
12943e00a9kevans/*
13043e00a9kevans * fdt_strnlen: returns the length of a string or max_count - which ever is
13143e00a9kevans * smallest.
13243e00a9kevans * Input 1 string: the string whose size is to be determined
13343e00a9kevans * Input 2 max_count: the maximum value returned by this function
13443e00a9kevans * Output: length of the string or max_count (the smallest of the two)
13543e00a9kevans */
13643e00a9kevansstatic inline size_t fdt_strnlen(const char *string, size_t max_count)
13743e00a9kevans{
13843e00a9kevans    const char *p = memchr(string, 0, max_count);
13943e00a9kevans    return p ? p - string : max_count;
14043e00a9kevans}
14143e00a9kevans
14243e00a9kevans#endif /* !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED <
14343e00a9kevans          MAC_OS_X_VERSION_10_7) */
14443e00a9kevans
14543e00a9kevans#endif /* __APPLE__ */
14643e00a9kevans
14743e00a9kevans#endif /* LIBFDT_ENV_H */
148