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