diff --git a/exosphere/mariko_fatal/source/fatal_main.cpp b/exosphere/mariko_fatal/source/fatal_main.cpp index 76dee3e8c..406aa4fae 100644 --- a/exosphere/mariko_fatal/source/fatal_main.cpp +++ b/exosphere/mariko_fatal/source/fatal_main.cpp @@ -18,6 +18,12 @@ namespace ams::secmon::fatal { + namespace { + + constinit u8 g_test_buffer[sdmmc::SectorSize * 2]; + + } + void Main() { /* Set library register addresses. */ actmon::SetRegisterAddress(MemoryRegionVirtualDeviceActivityMonitor.GetAddress()); @@ -43,6 +49,20 @@ namespace ams::secmon::fatal { Result result = InitializeSdCard(); AMS_SECMON_LOG("InitializeSdCard: %08x\n", result.GetValue()); + /* Get the connection status. */ + sdmmc::SpeedMode speed_mode; + sdmmc::BusWidth bus_width; + result = CheckSdCardConnection(std::addressof(speed_mode), std::addressof(bus_width)); + AMS_SECMON_LOG("CheckSdCardConnection: %08x\n", result.GetValue()); + AMS_SECMON_LOG(" Speed Mode: %u\n", static_cast(speed_mode)); + AMS_SECMON_LOG(" Bus Width: %u\n", static_cast(bus_width)); + + /* Read the first two sectors from the SD Card. */ + std::memset(g_test_buffer, 0xCC, sizeof(g_test_buffer)); + result = ReadSdCard(g_test_buffer, sizeof(g_test_buffer), 0, sizeof(g_test_buffer) / sdmmc::SectorSize); + AMS_SECMON_LOG("ReadSdCard: %08x\n", result.GetValue()); + AMS_DUMP(g_test_buffer, sizeof(g_test_buffer)); + /* TODO */ AMS_INFINITE_LOOP(); } diff --git a/exosphere/mariko_fatal/source/fatal_sdmmc.cpp b/exosphere/mariko_fatal/source/fatal_sdmmc.cpp index 47293bea2..b46b329b8 100644 --- a/exosphere/mariko_fatal/source/fatal_sdmmc.cpp +++ b/exosphere/mariko_fatal/source/fatal_sdmmc.cpp @@ -26,23 +26,55 @@ namespace ams::secmon::fatal { return MemoryRegionVirtualDramSdmmcMappedData.GetPointer() + MemoryRegionVirtualDramSdmmcMappedData.GetSize() - mmu::PageSize; } + ALWAYS_INLINE u8 *GetSdCardDmaBuffer() { + return MemoryRegionVirtualDramSdmmcMappedData.GetPointer(); + } + + constexpr inline size_t SdCardDmaBufferSize = MemoryRegionVirtualDramSdmmcMappedData.GetSize() - mmu::PageSize; + constexpr inline size_t SdCardDmaBufferSectors = SdCardDmaBufferSize / sdmmc::SectorSize; + static_assert(util::IsAligned(SdCardDmaBufferSize, sdmmc::SectorSize)); + } Result InitializeSdCard() { /* Map main memory for the sdmmc device. */ - AMS_SECMON_LOG("%s\n", "Initializing Device Page Table."); InitializeDevicePageTableForSdmmc1(); - AMS_SECMON_LOG("%s\n", "Initialized Device Page Table."); /* Initialize sdmmc library. */ sdmmc::Initialize(Port); - AMS_SECMON_LOG("%s\n", "Initialized Sdmmc Port."); sdmmc::SetSdCardWorkBuffer(Port, GetSdCardWorkBuffer(), sdmmc::SdCardWorkBufferSize); - AMS_SECMON_LOG("%s\n", "Set SD Card Work Buffer."); R_TRY(sdmmc::Activate(Port)); - AMS_SECMON_LOG("%s\n", "Activated."); + + return ResultSuccess(); + } + + Result CheckSdCardConnection(sdmmc::SpeedMode *out_sm, sdmmc::BusWidth *out_bw) { + return sdmmc::CheckSdCardConnection(out_sm, out_bw, Port); + } + + 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); + + /* Repeatedly read sectors. */ + u8 *dst_u8 = static_cast(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; + } return ResultSuccess(); } diff --git a/exosphere/mariko_fatal/source/fatal_sdmmc.hpp b/exosphere/mariko_fatal/source/fatal_sdmmc.hpp index bdd7ec89d..a8a7dd7c5 100644 --- a/exosphere/mariko_fatal/source/fatal_sdmmc.hpp +++ b/exosphere/mariko_fatal/source/fatal_sdmmc.hpp @@ -19,5 +19,7 @@ namespace ams::secmon::fatal { Result InitializeSdCard(); + Result CheckSdCardConnection(sdmmc::SpeedMode *out_sm, sdmmc::BusWidth *out_bw); + Result ReadSdCard(void *dst, size_t size, size_t sector_index, size_t sector_count); } diff --git a/exosphere/program/source/secmon_exception_handler.cpp b/exosphere/program/source/secmon_exception_handler.cpp index 6625ee946..dbf84ebd6 100644 --- a/exosphere/program/source/secmon_exception_handler.cpp +++ b/exosphere/program/source/secmon_exception_handler.cpp @@ -69,7 +69,7 @@ namespace ams::secmon { } /* Acquire exclusive access to exception handling logic. */ - if (g_is_locked.exchange(true)) { + if (!g_is_locked.exchange(true)) { /* Invoke the exception handler impl. */ ExceptionHandlerImpl(lr, sp); diff --git a/libraries/libvapours/source/dd/dd_io_mapping.cpp b/libraries/libvapours/source/dd/dd_io_mapping.cpp index 966de2fb2..581d479ce 100644 --- a/libraries/libvapours/source/dd/dd_io_mapping.cpp +++ b/libraries/libvapours/source/dd/dd_io_mapping.cpp @@ -37,6 +37,12 @@ namespace ams::dd { return secmon::MemoryRegionVirtualDeviceApbMisc.GetAddress() + phys_addr - secmon::MemoryRegionPhysicalDeviceApbMisc.GetAddress(); } else if (secmon::MemoryRegionPhysicalDeviceSdmmc.Contains(phys_addr, size)) { return secmon::MemoryRegionVirtualDeviceSdmmc.GetAddress() + phys_addr - secmon::MemoryRegionPhysicalDeviceSdmmc.GetAddress(); + } else if (secmon::MemoryRegionPhysicalDevicePmc.Contains(phys_addr, size)) { + return secmon::MemoryRegionVirtualDevicePmc.GetAddress() + phys_addr - secmon::MemoryRegionPhysicalDevicePmc.GetAddress(); + } else if (secmon::MemoryRegionPhysicalDeviceI2c5.Contains(phys_addr, size)) { + return secmon::MemoryRegionVirtualDeviceI2c5.GetAddress() + phys_addr - secmon::MemoryRegionPhysicalDeviceI2c5.GetAddress(); + } else if (secmon::MemoryRegionPhysicalDeviceI2c1.Contains(phys_addr, size)) { + return secmon::MemoryRegionVirtualDeviceI2c1.GetAddress() + phys_addr - secmon::MemoryRegionPhysicalDeviceI2c1.GetAddress(); } else { AMS_UNUSED(size); return static_cast(phys_addr); diff --git a/libraries/libvapours/source/sdmmc/impl/sdmmc_sd_host_standard_controller.cpp b/libraries/libvapours/source/sdmmc/impl/sdmmc_sd_host_standard_controller.cpp index 5cb9af01a..f03d15399 100644 --- a/libraries/libvapours/source/sdmmc/impl/sdmmc_sd_host_standard_controller.cpp +++ b/libraries/libvapours/source/sdmmc/impl/sdmmc_sd_host_standard_controller.cpp @@ -747,13 +747,7 @@ namespace ams::sdmmc::impl { SdHostStandardController::SdHostStandardController(dd::PhysicalAddress registers_phys_addr, size_t registers_size) { /* Translate the physical address to a address. */ - #if defined(ATMOSPHERE_IS_STRATOSPHERE) - const uintptr_t registers_addr = dd::QueryIoMapping(registers_phys_addr, registers_size); - #else - /* TODO: Discriminate between bpmp, exosphere address? */ - AMS_UNUSED(registers_size); - const uintptr_t registers_addr = static_cast(registers_phys_addr); - #endif + const uintptr_t registers_addr = dd::QueryIoMapping(registers_phys_addr, registers_size); /* Set registers. */ AMS_ABORT_UNLESS(registers_addr != 0);