Home | History | Annotate | Line # | Download | only in dist
libfdt_env.h revision 1.1.1.3
      1 /*	$NetBSD: libfdt_env.h,v 1.1.1.3 2019/12/22 12:30:37 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 #include <stdbool.h>
     13 #include <stddef.h>
     14 #include <stdint.h>
     15 #include <stdlib.h>
     16 #include <string.h>
     17 #include <limits.h>
     18 
     19 #ifdef __CHECKER__
     20 #define FDT_FORCE __attribute__((force))
     21 #define FDT_BITWISE __attribute__((bitwise))
     22 #else
     23 #define FDT_FORCE
     24 #define FDT_BITWISE
     25 #endif
     26 
     27 typedef uint16_t FDT_BITWISE fdt16_t;
     28 typedef uint32_t FDT_BITWISE fdt32_t;
     29 typedef uint64_t FDT_BITWISE fdt64_t;
     30 
     31 #define EXTRACT_BYTE(x, n)	((unsigned long long)((uint8_t *)&x)[n])
     32 #define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
     33 #define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
     34 			 (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
     35 #define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
     36 			 (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
     37 			 (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
     38 			 (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
     39 
     40 static inline uint16_t fdt16_to_cpu(fdt16_t x)
     41 {
     42 	return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
     43 }
     44 static inline fdt16_t cpu_to_fdt16(uint16_t x)
     45 {
     46 	return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
     47 }
     48 
     49 static inline uint32_t fdt32_to_cpu(fdt32_t x)
     50 {
     51 	return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
     52 }
     53 static inline fdt32_t cpu_to_fdt32(uint32_t x)
     54 {
     55 	return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
     56 }
     57 
     58 static inline uint64_t fdt64_to_cpu(fdt64_t x)
     59 {
     60 	return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
     61 }
     62 static inline fdt64_t cpu_to_fdt64(uint64_t x)
     63 {
     64 	return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
     65 }
     66 #undef CPU_TO_FDT64
     67 #undef CPU_TO_FDT32
     68 #undef CPU_TO_FDT16
     69 #undef EXTRACT_BYTE
     70 
     71 #ifdef __APPLE__
     72 #include <AvailabilityMacros.h>
     73 
     74 /* strnlen() is not available on Mac OS < 10.7 */
     75 # if !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < \
     76                                          MAC_OS_X_VERSION_10_7)
     77 
     78 #define strnlen fdt_strnlen
     79 
     80 /*
     81  * fdt_strnlen: returns the length of a string or max_count - which ever is
     82  * smallest.
     83  * Input 1 string: the string whose size is to be determined
     84  * Input 2 max_count: the maximum value returned by this function
     85  * Output: length of the string or max_count (the smallest of the two)
     86  */
     87 static inline size_t fdt_strnlen(const char *string, size_t max_count)
     88 {
     89     const char *p = memchr(string, 0, max_count);
     90     return p ? p - string : max_count;
     91 }
     92 
     93 #endif /* !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED <
     94           MAC_OS_X_VERSION_10_7) */
     95 
     96 #endif /* __APPLE__ */
     97 
     98 #endif /* LIBFDT_ENV_H */
     99