diff --git a/libexosphere/include/exosphere/pkg2.hpp b/libexosphere/include/exosphere/pkg2.hpp index 8df2bc65..9fa9425d 100644 --- a/libexosphere/include/exosphere/pkg2.hpp +++ b/libexosphere/include/exosphere/pkg2.hpp @@ -19,9 +19,9 @@ namespace ams::pkg2 { constexpr inline size_t Package2SizeMax = 8_MB - 16_KB; - constexpr inline size_t SegmentAlignment = 4; + constexpr inline size_t PayloadAlignment = 4; - constexpr inline int SegmentCount = 3; + constexpr inline int PayloadCount = 3; constexpr inline int MinimumValidDataVersion = 0; /* We allow older package2 to load; this value is currently 0x10 in Nintendo's code. */ constexpr inline int CurrentBootloaderVersion = 0xD; @@ -32,7 +32,7 @@ namespace ams::pkg2 { u32 package2_size; u8 key_generation; u8 header_iv_remainder[11]; - u8 segment_iv[SegmentCount][0x10]; + u8 payload_ivs[PayloadCount][0x10]; u8 padding_40[0x10]; u8 magic[4]; u32 entrypoint; @@ -40,11 +40,11 @@ namespace ams::pkg2 { u8 package2_version; u8 bootloader_version; u8 padding_5E[2]; - u32 segment_sizes[SegmentCount]; + u32 payload_sizes[PayloadCount]; u8 padding_6C[4]; - u32 segment_offsets[SegmentCount]; + u32 payload_offsets[PayloadCount]; u8 padding_7C[4]; - u8 segment_hashes[SegmentCount][crypto::Sha256Generator::HashSize]; + u8 payload_hashes[PayloadCount][crypto::Sha256Generator::HashSize]; u8 padding_E0[0x20]; private: @@ -53,7 +53,7 @@ namespace ams::pkg2 { } public: ALWAYS_INLINE u8 GetKeyGeneration() const { - return std::min(0, (this->key_generation ^ this->header_iv_remainder[1] ^ this->header_iv_remainder[2]) - 1); + return static_cast(std::max(0, static_cast(this->key_generation ^ this->header_iv_remainder[1] ^ this->header_iv_remainder[2]) - 1)); } ALWAYS_INLINE u32 GetSize() const { diff --git a/libexosphere/include/exosphere/secmon/secmon_memory_layout.hpp b/libexosphere/include/exosphere/secmon/secmon_memory_layout.hpp index 34e4be52..ffe9625e 100644 --- a/libexosphere/include/exosphere/secmon/secmon_memory_layout.hpp +++ b/libexosphere/include/exosphere/secmon/secmon_memory_layout.hpp @@ -80,6 +80,9 @@ namespace ams::secmon { constexpr inline const MemoryRegion MemoryRegionDramDefaultKernelCarveout = MemoryRegion(UINT64_C(0x80060000), UINT64_C(0x1FFE0000)); static_assert(MemoryRegionDram.Contains(MemoryRegionDramDefaultKernelCarveout)); + constexpr inline const MemoryRegion MemoryRegionDramPackage2Payloads = MemoryRegion(MemoryRegionDram.GetAddress(), 8_MB); + static_assert(MemoryRegionDram.Contains(MemoryRegionDramPackage2Payloads)); + constexpr inline const MemoryRegion MemoryRegionDramPackage2 = MemoryRegion(UINT64_C(0xA9800000), UINT64_C(0x07FC0000)); static_assert(MemoryRegionDram.Contains(MemoryRegionDramPackage2)); diff --git a/libvapours/include/vapours/util.hpp b/libvapours/include/vapours/util.hpp index c10fa5d3..0d60a3ea 100644 --- a/libvapours/include/vapours/util.hpp +++ b/libvapours/include/vapours/util.hpp @@ -36,5 +36,6 @@ #include #include #include +#include #include #include diff --git a/libvapours/include/vapours/util/util_overlap.hpp b/libvapours/include/vapours/util/util_overlap.hpp new file mode 100644 index 00000000..a1bf7d2d --- /dev/null +++ b/libvapours/include/vapours/util/util_overlap.hpp @@ -0,0 +1,35 @@ +/* + * 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 . + */ + +#pragma once +#include +#include + +namespace ams::util { + + constexpr inline bool HasOverlap(uintptr_t addr0, size_t size0, uintptr_t addr1, size_t size1) { + if (addr0 <= addr1) { + return addr1 < addr0 + size0; + } else { + return addr0 < addr1 + size1; + } + } + + constexpr inline bool Contains(uintptr_t addr, size_t size, uintptr_t ptr) { + return (addr <= ptr) && (ptr < addr + size); + } + +}