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