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