162306a36Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */ 262306a36Sopenharmony_ci#ifndef LIBFDT_ENV_H 362306a36Sopenharmony_ci#define LIBFDT_ENV_H 462306a36Sopenharmony_ci/* 562306a36Sopenharmony_ci * libfdt - Flat Device Tree manipulation 662306a36Sopenharmony_ci * Copyright (C) 2006 David Gibson, IBM Corporation. 762306a36Sopenharmony_ci * Copyright 2012 Kim Phillips, Freescale Semiconductor. 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <stdbool.h> 1162306a36Sopenharmony_ci#include <stddef.h> 1262306a36Sopenharmony_ci#include <stdint.h> 1362306a36Sopenharmony_ci#include <stdlib.h> 1462306a36Sopenharmony_ci#include <string.h> 1562306a36Sopenharmony_ci#include <limits.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#ifdef __CHECKER__ 1862306a36Sopenharmony_ci#define FDT_FORCE __attribute__((force)) 1962306a36Sopenharmony_ci#define FDT_BITWISE __attribute__((bitwise)) 2062306a36Sopenharmony_ci#else 2162306a36Sopenharmony_ci#define FDT_FORCE 2262306a36Sopenharmony_ci#define FDT_BITWISE 2362306a36Sopenharmony_ci#endif 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_citypedef uint16_t FDT_BITWISE fdt16_t; 2662306a36Sopenharmony_citypedef uint32_t FDT_BITWISE fdt32_t; 2762306a36Sopenharmony_citypedef uint64_t FDT_BITWISE fdt64_t; 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#define EXTRACT_BYTE(x, n) ((unsigned long long)((uint8_t *)&x)[n]) 3062306a36Sopenharmony_ci#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1)) 3162306a36Sopenharmony_ci#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \ 3262306a36Sopenharmony_ci (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3)) 3362306a36Sopenharmony_ci#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \ 3462306a36Sopenharmony_ci (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \ 3562306a36Sopenharmony_ci (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \ 3662306a36Sopenharmony_ci (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7)) 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_cistatic inline uint16_t fdt16_to_cpu(fdt16_t x) 3962306a36Sopenharmony_ci{ 4062306a36Sopenharmony_ci return (FDT_FORCE uint16_t)CPU_TO_FDT16(x); 4162306a36Sopenharmony_ci} 4262306a36Sopenharmony_cistatic inline fdt16_t cpu_to_fdt16(uint16_t x) 4362306a36Sopenharmony_ci{ 4462306a36Sopenharmony_ci return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x); 4562306a36Sopenharmony_ci} 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_cistatic inline uint32_t fdt32_to_cpu(fdt32_t x) 4862306a36Sopenharmony_ci{ 4962306a36Sopenharmony_ci return (FDT_FORCE uint32_t)CPU_TO_FDT32(x); 5062306a36Sopenharmony_ci} 5162306a36Sopenharmony_cistatic inline fdt32_t cpu_to_fdt32(uint32_t x) 5262306a36Sopenharmony_ci{ 5362306a36Sopenharmony_ci return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x); 5462306a36Sopenharmony_ci} 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_cistatic inline uint64_t fdt64_to_cpu(fdt64_t x) 5762306a36Sopenharmony_ci{ 5862306a36Sopenharmony_ci return (FDT_FORCE uint64_t)CPU_TO_FDT64(x); 5962306a36Sopenharmony_ci} 6062306a36Sopenharmony_cistatic inline fdt64_t cpu_to_fdt64(uint64_t x) 6162306a36Sopenharmony_ci{ 6262306a36Sopenharmony_ci return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x); 6362306a36Sopenharmony_ci} 6462306a36Sopenharmony_ci#undef CPU_TO_FDT64 6562306a36Sopenharmony_ci#undef CPU_TO_FDT32 6662306a36Sopenharmony_ci#undef CPU_TO_FDT16 6762306a36Sopenharmony_ci#undef EXTRACT_BYTE 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci#ifdef __APPLE__ 7062306a36Sopenharmony_ci#include <AvailabilityMacros.h> 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci/* strnlen() is not available on Mac OS < 10.7 */ 7362306a36Sopenharmony_ci# if !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < \ 7462306a36Sopenharmony_ci MAC_OS_X_VERSION_10_7) 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci#define strnlen fdt_strnlen 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci/* 7962306a36Sopenharmony_ci * fdt_strnlen: returns the length of a string or max_count - which ever is 8062306a36Sopenharmony_ci * smallest. 8162306a36Sopenharmony_ci * Input 1 string: the string whose size is to be determined 8262306a36Sopenharmony_ci * Input 2 max_count: the maximum value returned by this function 8362306a36Sopenharmony_ci * Output: length of the string or max_count (the smallest of the two) 8462306a36Sopenharmony_ci */ 8562306a36Sopenharmony_cistatic inline size_t fdt_strnlen(const char *string, size_t max_count) 8662306a36Sopenharmony_ci{ 8762306a36Sopenharmony_ci const char *p = memchr(string, 0, max_count); 8862306a36Sopenharmony_ci return p ? p - string : max_count; 8962306a36Sopenharmony_ci} 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci#endif /* !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < 9262306a36Sopenharmony_ci MAC_OS_X_VERSION_10_7) */ 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci#endif /* __APPLE__ */ 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci#endif /* LIBFDT_ENV_H */ 97