mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2024-11-10 14:11:43 +00:00
f66b41c027
exo2: Implement uncompressor stub and boot code up to Main(). exo2: implement some more init (uart/gic) exo2: implement more of init exo2: improve reg api, add keyslot flag setters exo2: implement se aes decryption/enc exo2: fix bugs in loader stub/mmu mappings exo2: start skeletoning bootconfig/global context types arch: fix makefile flags exo2: implement through master key derivation exo2: implement device master keygen exo2: more init through start of SetupSocSecurity exo2: implement pmc secure scratch management se: implement sticky bit validation libexosphere: fix building for arm32 libexo: fix makefile flags libexo: support building for arm64/arm sc7fw: skeleton binary sc7fw: skeleton a little more sc7fw: implement all non-dram functionality exo2: fix DivideUp error sc7fw: implement more dram code, fix reg library errors sc7fw: complete sc7fw impl. exo2: skeleton the rest of SetupSocSecurity exo2: implement fiq interrupt handler exo2: implement all exception handlers exo2: skeleton the entire smc api, implement the svc invoker exo2: implement rest of SetupSocSecurity exo2: correct slave security errors exo2: fix register definition exo2: minor fixes
157 lines
5.5 KiB
C++
157 lines
5.5 KiB
C++
/*
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms and conditions of the GNU General Public License,
|
|
* version 2, as published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#pragma once
|
|
#include <vapours/common.hpp>
|
|
#include <vapours/assert.hpp>
|
|
|
|
namespace ams::util {
|
|
|
|
constexpr bool IsLittleEndian() {
|
|
return std::endian::native == std::endian::little;
|
|
}
|
|
|
|
constexpr bool IsBigEndian() {
|
|
return std::endian::native == std::endian::big;
|
|
}
|
|
|
|
static_assert(IsLittleEndian() ^ IsBigEndian());
|
|
|
|
template<typename U> requires std::unsigned_integral<U>
|
|
constexpr ALWAYS_INLINE U SwapBytes(const U u) {
|
|
static_assert(BITSIZEOF(u8) == 8);
|
|
constexpr U ByteMask = 0xFFu;
|
|
if constexpr (std::is_same<U, u64>::value) {
|
|
return ((u & (ByteMask << 56)) >> 56) |
|
|
((u & (ByteMask << 48)) >> 40) |
|
|
((u & (ByteMask << 40)) >> 24) |
|
|
((u & (ByteMask << 32)) >> 8) |
|
|
((u & (ByteMask << 24)) << 8) |
|
|
((u & (ByteMask << 16)) << 24) |
|
|
((u & (ByteMask << 8)) << 40) |
|
|
((u & (ByteMask << 0)) << 56);
|
|
|
|
} else if constexpr (std::is_same<U, u32>::value) {
|
|
return ((u & (ByteMask << 24)) >> 24) |
|
|
((u & (ByteMask << 16)) >> 8) |
|
|
((u & (ByteMask << 8)) << 8) |
|
|
((u & (ByteMask << 0)) << 24);
|
|
|
|
} else if constexpr (std::is_same<U, u16>::value) {
|
|
return ((u & (ByteMask << 8)) >> 8) |
|
|
((u & (ByteMask << 0)) << 8);
|
|
|
|
} else if constexpr (std::is_same<U, u8>::value) {
|
|
AMS_UNUSED(ByteMask);
|
|
return u;
|
|
} else {
|
|
static_assert(!std::is_same<U, U>::value);
|
|
}
|
|
}
|
|
|
|
constexpr ALWAYS_INLINE u64 SwapBytes48(const u64 u) {
|
|
using U = u64;
|
|
static_assert(BITSIZEOF(u8) == 8);
|
|
constexpr U ByteMask = 0xFFu;
|
|
AMS_ASSERT((u & UINT64_C(0xFFFF000000000000)) == 0);
|
|
return ((u & (ByteMask << 40)) >> 40) |
|
|
((u & (ByteMask << 32)) >> 24) |
|
|
((u & (ByteMask << 24)) >> 8) |
|
|
((u & (ByteMask << 16)) << 8) |
|
|
((u & (ByteMask << 8)) << 24) |
|
|
((u & (ByteMask << 0)) << 40);
|
|
}
|
|
|
|
template<typename T> requires std::integral<T>
|
|
constexpr ALWAYS_INLINE void SwapBytes(T *ptr) {
|
|
using U = typename std::make_unsigned<T>::type;
|
|
|
|
*ptr = static_cast<T>(SwapBytes(static_cast<U>(*ptr)));
|
|
}
|
|
|
|
template<typename T> requires std::integral<T>
|
|
constexpr ALWAYS_INLINE T ConvertToBigEndian(const T val) {
|
|
using U = typename std::make_unsigned<T>::type;
|
|
|
|
if constexpr (IsBigEndian()) {
|
|
return static_cast<T>(static_cast<U>(val));
|
|
} else {
|
|
static_assert(IsLittleEndian());
|
|
return static_cast<T>(SwapBytes(static_cast<U>(val)));
|
|
}
|
|
}
|
|
|
|
template<typename T> requires std::integral<T>
|
|
constexpr ALWAYS_INLINE T ConvertToLittleEndian(const T val) {
|
|
using U = typename std::make_unsigned<T>::type;
|
|
|
|
if constexpr (IsBigEndian()) {
|
|
return static_cast<T>(SwapBytes(static_cast<U>(val)));
|
|
} else {
|
|
static_assert(IsLittleEndian());
|
|
return static_cast<T>(static_cast<U>(val));
|
|
}
|
|
}
|
|
|
|
template<typename T> requires std::integral<T>
|
|
constexpr ALWAYS_INLINE T ConvertToBigEndian48(const T val) {
|
|
using U = typename std::make_unsigned<T>::type;
|
|
static_assert(sizeof(T) == sizeof(u64));
|
|
|
|
if constexpr (IsBigEndian()) {
|
|
AMS_ASSERT((static_cast<U>(val) & UINT64_C(0xFFFF000000000000)) == 0);
|
|
return static_cast<T>(static_cast<U>(val));
|
|
} else {
|
|
static_assert(IsLittleEndian());
|
|
return static_cast<T>(SwapBytes48(static_cast<U>(val)));
|
|
}
|
|
}
|
|
|
|
template<typename T> requires std::integral<T>
|
|
constexpr ALWAYS_INLINE T ConvertToLittleEndian48(const T val) {
|
|
using U = typename std::make_unsigned<T>::type;
|
|
static_assert(sizeof(T) == sizeof(u64));
|
|
|
|
if constexpr (IsBigEndian()) {
|
|
return static_cast<T>(SwapBytes48(static_cast<U>(val)));
|
|
} else {
|
|
static_assert(IsLittleEndian());
|
|
AMS_ASSERT((static_cast<U>(val) & UINT64_C(0xFFFF000000000000)) == 0);
|
|
return static_cast<T>(static_cast<U>(val));
|
|
}
|
|
}
|
|
|
|
template<typename T> requires std::integral<T>
|
|
constexpr ALWAYS_INLINE T LoadBigEndian(const T *ptr) {
|
|
return ConvertToBigEndian(*ptr);
|
|
}
|
|
|
|
template<typename T> requires std::integral<T>
|
|
constexpr ALWAYS_INLINE T LoadLittleEndian(const T *ptr) {
|
|
return ConvertToLittleEndian(*ptr);
|
|
}
|
|
|
|
template<typename T> requires std::integral<T>
|
|
constexpr ALWAYS_INLINE void StoreBigEndian(T *ptr, T val) {
|
|
*ptr = ConvertToBigEndian(val);
|
|
}
|
|
|
|
template<typename T> requires std::integral<T>
|
|
constexpr ALWAYS_INLINE void StoreLittleEndian(T *ptr, T val) {
|
|
*ptr = ConvertToLittleEndian(val);
|
|
}
|
|
|
|
}
|