From 3c7ad93d1c9b0aa9111f05dad8b62ee868214aa7 Mon Sep 17 00:00:00 2001 From: windows-server-2003 Date: Tue, 27 Feb 2018 16:53:45 +0900 Subject: [PATCH] Forbid writing to SD if write locked --- arm9/source/filesys/fsperm.c | 7 +++++++ arm9/source/filesys/fsutil.c | 6 ++++++ arm9/source/nand/sdmmc.h | 2 ++ 3 files changed, 15 insertions(+) diff --git a/arm9/source/filesys/fsperm.c b/arm9/source/filesys/fsperm.c index dad27a8..3f20ae8 100644 --- a/arm9/source/filesys/fsperm.c +++ b/arm9/source/filesys/fsperm.c @@ -4,6 +4,7 @@ #include "image.h" #include "unittype.h" #include "ui.h" +#include "sdmmc.h" #define PATH_SYS_LVL1 "S:/twln.bin", "S:/twlp.bin" #define PATH_SYS_LVL2 "1:/rw/sys/LocalFriendCodeSeed_B", "1:/rw/sys/LocalFriendCodeSeed_A", \ @@ -26,6 +27,12 @@ bool CheckWritePermissions(const char* path) { if ((drvtype & DRV_IMAGE) && !CheckWritePermissions(GetMountPath())) return false; // endless loop when mounted file inside image, but not possible + // SD card write protection check + if ((drvtype & (DRV_SDCARD | DRV_EMUNAND | DRV_ALIAS)) && SD_WRITE_PROTECTED) { + ShowPrompt(false, "SD card is write protected!\nCan't continue."); + return false; + } + // check drive type, get permission type if (drvtype & DRV_SYSNAND) { u32 perms[] = { PERM_SYS_LVL0, PERM_SYS_LVL1, PERM_SYS_LVL2, PERM_SYS_LVL3 }; diff --git a/arm9/source/filesys/fsutil.c b/arm9/source/filesys/fsutil.c index e75537e..9296449 100644 --- a/arm9/source/filesys/fsutil.c +++ b/arm9/source/filesys/fsutil.c @@ -49,6 +49,12 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) { return false; } + // Write protection check + if (SD_WRITE_PROTECTED) { + ShowPrompt(false, "SD card is write protected!\nCan't continue."); + return false; + } + // build the MBR memcpy(mbrdata + 0x08, &fat_sector, 4); memcpy(mbrdata + 0x0C, &fat_size, 4); diff --git a/arm9/source/nand/sdmmc.h b/arm9/source/nand/sdmmc.h index 5195cc0..3f62092 100644 --- a/arm9/source/nand/sdmmc.h +++ b/arm9/source/nand/sdmmc.h @@ -74,6 +74,8 @@ #define TMIO_MASK_READOP (TMIO_STAT1_RXRDY | TMIO_STAT1_DATAEND) #define TMIO_MASK_WRITEOP (TMIO_STAT1_TXRQ | TMIO_STAT1_DATAEND) +#define SD_WRITE_PROTECTED (!((*(volatile u16*) (SDMMC_BASE + 0x1C)) & (0x1 << 7))) + typedef struct mmcdevice { u8 *rData; const u8 *tData;