2020-11-15 20:58:13 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-11-15 20:58:13 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
#include <exosphere.hpp>
|
|
|
|
#include "fatal_device_page_table.hpp"
|
|
|
|
|
|
|
|
namespace ams::secmon::fatal {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr inline auto Port = sdmmc::Port_SdCard0;
|
|
|
|
|
|
|
|
ALWAYS_INLINE u8 *GetSdCardWorkBuffer() {
|
|
|
|
return MemoryRegionVirtualDramSdmmcMappedData.GetPointer<u8>() + MemoryRegionVirtualDramSdmmcMappedData.GetSize() - mmu::PageSize;
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:31:01 +00:00
|
|
|
ALWAYS_INLINE u8 *GetSdCardDmaBuffer() {
|
|
|
|
return MemoryRegionVirtualDramSdmmcMappedData.GetPointer<u8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr inline size_t SdCardDmaBufferSize = MemoryRegionVirtualDramSdmmcMappedData.GetSize() - mmu::PageSize;
|
|
|
|
constexpr inline size_t SdCardDmaBufferSectors = SdCardDmaBufferSize / sdmmc::SectorSize;
|
|
|
|
static_assert(util::IsAligned(SdCardDmaBufferSize, sdmmc::SectorSize));
|
|
|
|
|
2020-11-15 20:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result InitializeSdCard() {
|
|
|
|
/* Map main memory for the sdmmc device. */
|
|
|
|
InitializeDevicePageTableForSdmmc1();
|
|
|
|
|
|
|
|
/* Initialize sdmmc library. */
|
|
|
|
sdmmc::Initialize(Port);
|
|
|
|
|
|
|
|
sdmmc::SetSdCardWorkBuffer(Port, GetSdCardWorkBuffer(), sdmmc::SdCardWorkBufferSize);
|
|
|
|
|
2020-11-18 02:01:38 +00:00
|
|
|
//sdmmc::Deactivate(Port);
|
2020-11-15 20:58:13 +00:00
|
|
|
R_TRY(sdmmc::Activate(Port));
|
2020-11-15 21:31:01 +00:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-11-15 21:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CheckSdCardConnection(sdmmc::SpeedMode *out_sm, sdmmc::BusWidth *out_bw) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(sdmmc::CheckSdCardConnection(out_sm, out_bw, Port));
|
2020-11-15 21:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ReadSdCard(void *dst, size_t size, size_t sector_index, size_t sector_count) {
|
|
|
|
/* Validate that our buffer is valid. */
|
|
|
|
AMS_ASSERT(size >= sector_count * sdmmc::SectorSize);
|
2021-09-30 05:32:40 +01:00
|
|
|
AMS_UNUSED(size);
|
2020-11-15 21:31:01 +00:00
|
|
|
|
|
|
|
/* Repeatedly read sectors. */
|
|
|
|
u8 *dst_u8 = static_cast<u8 *>(dst);
|
|
|
|
void * const dma_buffer = GetSdCardDmaBuffer();
|
|
|
|
while (sector_count > 0) {
|
|
|
|
/* Read sectors into the DMA buffer. */
|
|
|
|
const size_t cur_sectors = std::min(sector_count, SdCardDmaBufferSectors);
|
|
|
|
const size_t cur_size = cur_sectors * sdmmc::SectorSize;
|
|
|
|
R_TRY(sdmmc::Read(dma_buffer, cur_size, Port, sector_index, cur_sectors));
|
|
|
|
|
|
|
|
/* Copy data from the DMA buffer to the output. */
|
|
|
|
std::memcpy(dst_u8, dma_buffer, cur_size);
|
|
|
|
|
|
|
|
/* Advance. */
|
|
|
|
dst_u8 += cur_size;
|
|
|
|
sector_index += cur_sectors;
|
|
|
|
sector_count -= cur_sectors;
|
|
|
|
}
|
2020-11-15 20:58:13 +00:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-11-15 20:58:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 04:35:47 +00:00
|
|
|
Result WriteSdCard(size_t sector_index, size_t sector_count, const void *src, size_t size) {
|
|
|
|
/* Validate that our buffer is valid. */
|
|
|
|
AMS_ASSERT(size >= sector_count * sdmmc::SectorSize);
|
2021-09-30 05:32:40 +01:00
|
|
|
AMS_UNUSED(size);
|
2020-11-17 04:35:47 +00:00
|
|
|
|
|
|
|
/* Repeatedly read sectors. */
|
|
|
|
const u8 *src_u8 = static_cast<const u8 *>(src);
|
|
|
|
void * const dma_buffer = GetSdCardDmaBuffer();
|
|
|
|
while (sector_count > 0) {
|
|
|
|
/* Copy sectors into the DMA buffer. */
|
|
|
|
const size_t cur_sectors = std::min(sector_count, SdCardDmaBufferSectors);
|
|
|
|
const size_t cur_size = cur_sectors * sdmmc::SectorSize;
|
|
|
|
std::memcpy(dma_buffer, src_u8, cur_size);
|
|
|
|
|
|
|
|
/* Write sectors to the sd card. */
|
|
|
|
R_TRY(sdmmc::Write(Port, sector_index, cur_sectors, dma_buffer, cur_size));
|
|
|
|
|
|
|
|
/* Advance. */
|
|
|
|
src_u8 += cur_size;
|
|
|
|
sector_index += cur_sectors;
|
|
|
|
sector_count -= cur_sectors;
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2020-11-17 04:35:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 20:58:13 +00:00
|
|
|
}
|