1*cf74e51eSskrll /* $NetBSD: libfdt_env.h,v 1.5 2019/12/22 12:33:17 skrll Exp $ */
2e02cc31bSskrll
3*cf74e51eSskrll /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
4*cf74e51eSskrll #ifndef LIBFDT_ENV_H
5*cf74e51eSskrll #define LIBFDT_ENV_H
66233fbe7Smacallan /*
76233fbe7Smacallan * libfdt - Flat Device Tree manipulation
86233fbe7Smacallan * Copyright (C) 2006 David Gibson, IBM Corporation.
96233fbe7Smacallan * Copyright 2012 Kim Phillips, Freescale Semiconductor.
106233fbe7Smacallan */
116233fbe7Smacallan
1234a1573cSjmcneill #if defined(_KERNEL) || defined(_STANDALONE)
13bce64666Sjmcneill #include <sys/param.h>
14bce64666Sjmcneill #include <sys/types.h>
1534a1573cSjmcneill #include <lib/libkern/libkern.h>
16bce64666Sjmcneill #else
17*cf74e51eSskrll #include <stdbool.h>
186233fbe7Smacallan #include <stddef.h>
196233fbe7Smacallan #include <stdint.h>
20e02cc31bSskrll #include <stdlib.h>
216233fbe7Smacallan #include <string.h>
22*cf74e51eSskrll #include <limits.h>
23bce64666Sjmcneill #endif
246233fbe7Smacallan
256233fbe7Smacallan #ifdef __CHECKER__
26e02cc31bSskrll #define FDT_FORCE __attribute__((force))
27e02cc31bSskrll #define FDT_BITWISE __attribute__((bitwise))
286233fbe7Smacallan #else
29e02cc31bSskrll #define FDT_FORCE
30e02cc31bSskrll #define FDT_BITWISE
316233fbe7Smacallan #endif
326233fbe7Smacallan
33e02cc31bSskrll typedef uint16_t FDT_BITWISE fdt16_t;
34e02cc31bSskrll typedef uint32_t FDT_BITWISE fdt32_t;
35e02cc31bSskrll typedef uint64_t FDT_BITWISE fdt64_t;
366233fbe7Smacallan
376233fbe7Smacallan #define EXTRACT_BYTE(x, n) ((unsigned long long)((uint8_t *)&x)[n])
386233fbe7Smacallan #define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
396233fbe7Smacallan #define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
406233fbe7Smacallan (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
416233fbe7Smacallan #define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
426233fbe7Smacallan (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
436233fbe7Smacallan (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
446233fbe7Smacallan (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
456233fbe7Smacallan
fdt16_to_cpu(fdt16_t x)466233fbe7Smacallan static inline uint16_t fdt16_to_cpu(fdt16_t x)
476233fbe7Smacallan {
48e02cc31bSskrll return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
496233fbe7Smacallan }
cpu_to_fdt16(uint16_t x)506233fbe7Smacallan static inline fdt16_t cpu_to_fdt16(uint16_t x)
516233fbe7Smacallan {
52e02cc31bSskrll return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
536233fbe7Smacallan }
546233fbe7Smacallan
fdt32_to_cpu(fdt32_t x)556233fbe7Smacallan static inline uint32_t fdt32_to_cpu(fdt32_t x)
566233fbe7Smacallan {
57e02cc31bSskrll return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
586233fbe7Smacallan }
cpu_to_fdt32(uint32_t x)596233fbe7Smacallan static inline fdt32_t cpu_to_fdt32(uint32_t x)
606233fbe7Smacallan {
61e02cc31bSskrll return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
626233fbe7Smacallan }
636233fbe7Smacallan
fdt64_to_cpu(fdt64_t x)646233fbe7Smacallan static inline uint64_t fdt64_to_cpu(fdt64_t x)
656233fbe7Smacallan {
66e02cc31bSskrll return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
676233fbe7Smacallan }
cpu_to_fdt64(uint64_t x)686233fbe7Smacallan static inline fdt64_t cpu_to_fdt64(uint64_t x)
696233fbe7Smacallan {
70e02cc31bSskrll return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
716233fbe7Smacallan }
726233fbe7Smacallan #undef CPU_TO_FDT64
736233fbe7Smacallan #undef CPU_TO_FDT32
746233fbe7Smacallan #undef CPU_TO_FDT16
756233fbe7Smacallan #undef EXTRACT_BYTE
766233fbe7Smacallan
77*cf74e51eSskrll #ifdef __APPLE__
78*cf74e51eSskrll #include <AvailabilityMacros.h>
79*cf74e51eSskrll
80*cf74e51eSskrll /* strnlen() is not available on Mac OS < 10.7 */
81*cf74e51eSskrll # if !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < \
82*cf74e51eSskrll MAC_OS_X_VERSION_10_7)
83*cf74e51eSskrll
84*cf74e51eSskrll #define strnlen fdt_strnlen
85*cf74e51eSskrll
86*cf74e51eSskrll /*
87*cf74e51eSskrll * fdt_strnlen: returns the length of a string or max_count - which ever is
88*cf74e51eSskrll * smallest.
89*cf74e51eSskrll * Input 1 string: the string whose size is to be determined
90*cf74e51eSskrll * Input 2 max_count: the maximum value returned by this function
91*cf74e51eSskrll * Output: length of the string or max_count (the smallest of the two)
92*cf74e51eSskrll */
fdt_strnlen(const char * string,size_t max_count)93*cf74e51eSskrll static inline size_t fdt_strnlen(const char *string, size_t max_count)
94*cf74e51eSskrll {
95*cf74e51eSskrll const char *p = memchr(string, 0, max_count);
96*cf74e51eSskrll return p ? p - string : max_count;
97*cf74e51eSskrll }
98*cf74e51eSskrll
99*cf74e51eSskrll #endif /* !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED <
100*cf74e51eSskrll MAC_OS_X_VERSION_10_7) */
101*cf74e51eSskrll
102*cf74e51eSskrll #endif /* __APPLE__ */
103*cf74e51eSskrll
104*cf74e51eSskrll #endif /* LIBFDT_ENV_H */
105