From 795bcf0960691acc908d40ae153c15eceb442313 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Wed, 19 Jan 2022 01:36:23 -0700 Subject: [PATCH] Add/skeleton SignedSystemPartition/SdmmcControl --- src/LibHac/Fs/FsEnums.cs | 40 +++++++++++--- src/LibHac/Fs/Fsa/IFileSystem.cs | 2 +- src/LibHac/Fs/Shim/SdmmcControl.cs | 26 ++++++++++ src/LibHac/Fs/Shim/SignedSystemPartition.cs | 52 +++++++++++++++++++ .../FsSrv/Impl/FileSystemInterfaceAdapter.cs | 2 +- src/LibHac/Sdmmc/SdmmcEnums.cs | 14 +++++ 6 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 src/LibHac/Fs/Shim/SdmmcControl.cs create mode 100644 src/LibHac/Fs/Shim/SignedSystemPartition.cs diff --git a/src/LibHac/Fs/FsEnums.cs b/src/LibHac/Fs/FsEnums.cs index 48f82d31..f5c3ceb4 100644 --- a/src/LibHac/Fs/FsEnums.cs +++ b/src/LibHac/Fs/FsEnums.cs @@ -189,13 +189,6 @@ public enum CommitOptionFlag SetRestoreFlag = 2 } -public enum SdmmcPort -{ - Mmc = 0, - SdCard = 1, - GcAsic = 2 -} - public enum CacheStorageTargetMedia { None = 0, @@ -261,4 +254,37 @@ public enum SdCardSpeedMode Sdr104 = 6, Ddr50 = 7, Unknown = 8 +} + +public enum SdmmcBusWidth +{ + Width1Bit = 0, + Width4Bit = 1, + Width8Bit = 2, +} + +public enum SdmmcSpeedMode +{ + MmcIdentification = 0, + MmcLegacySpeed = 1, + MmcHighSpeed = 2, + MmcHs200 = 3, + MmcHs400 = 4, + SdCardIdentification = 5, + SdCardDefaultSpeed = 6, + SdCardHighSpeed = 7, + SdCardSdr12 = 8, + SdCardSdr25 = 9, + SdCardSdr50 = 10, + SdCardSdr104 = 11, + SdCardDdr50 = 12, + GcAsicFpgaSpeed = 13, + GcAsicSpeed = 14 +} + +public enum SdmmcPort +{ + Mmc = 0, + SdCard = 1, + GcAsic = 2 } \ No newline at end of file diff --git a/src/LibHac/Fs/Fsa/IFileSystem.cs b/src/LibHac/Fs/Fsa/IFileSystem.cs index b405de69..5e5b624f 100644 --- a/src/LibHac/Fs/Fsa/IFileSystem.cs +++ b/src/LibHac/Fs/Fsa/IFileSystem.cs @@ -371,6 +371,6 @@ public enum QueryId /// SetConcatenationFileAttribute = 0, UpdateMac = 1, - IsSignedSystemPartitionOnSdCardValid = 2, + IsSignedSystemPartition = 2, QueryUnpreparedFileInformation = 3 } \ No newline at end of file diff --git a/src/LibHac/Fs/Shim/SdmmcControl.cs b/src/LibHac/Fs/Shim/SdmmcControl.cs new file mode 100644 index 00000000..a72625e9 --- /dev/null +++ b/src/LibHac/Fs/Shim/SdmmcControl.cs @@ -0,0 +1,26 @@ +using System; + +namespace LibHac.Fs.Shim; + +/// +/// Contains functions for suspending, resuming, and checking sdmmc status. +/// +/// Based on nnSdk 13.4.0 +public static class SdmmcControl +{ + public static Result GetSdmmcConnectionStatus(this FileSystemClient fs, out SdmmcSpeedMode speedMode, + out SdmmcBusWidth busWidth, SdmmcPort port) + { + throw new NotImplementedException(); + } + + public static Result SuspendSdmmcControl() + { + throw new NotImplementedException(); + } + + public static Result ResumeSdmmcControl() + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/src/LibHac/Fs/Shim/SignedSystemPartition.cs b/src/LibHac/Fs/Shim/SignedSystemPartition.cs new file mode 100644 index 00000000..5d662865 --- /dev/null +++ b/src/LibHac/Fs/Shim/SignedSystemPartition.cs @@ -0,0 +1,52 @@ +using System; +using LibHac.Common; +using LibHac.Diag; +using LibHac.Fs.Fsa; +using LibHac.Fs.Impl; + +namespace LibHac.Fs.Shim; + +/// +/// Contains functions for checking if a mounted file system was created from a signed system partition. +/// +/// Based on nnSdk 13.4.0 +public static class SignedSystemPartition +{ + public static bool IsValidSignedSystemPartitionOnSdCard(this FileSystemClient fs, U8Span path) + { + Result rc = fs.Impl.FindFileSystem(out FileSystemAccessor fileSystem, out U8Span _, path); + fs.Impl.LogResultErrorMessage(rc); + Abort.DoAbortUnlessSuccess(rc); + + bool isValid = false; + + rc = Operate(ref isValid, fileSystem); + fs.Impl.LogResultErrorMessage(rc); + Abort.DoAbortUnlessSuccess(rc); + + return isValid; + + static Result Operate(ref bool isValid, FileSystemAccessor fileSystem) + { + Result rc = fileSystem.QueryEntry(SpanHelpers.AsByteSpan(ref isValid), ReadOnlySpan.Empty, + QueryId.IsSignedSystemPartition, new U8Span(new[] { (byte)'/' })); + + if (rc.IsFailure()) + { + // Any IFileSystems other than a signed system partition IFileSystem should + // return an "UnsupportedOperation" result + if (ResultFs.UnsupportedOperation.Includes(rc)) + { + rc.Catch(); + isValid = false; + } + else + { + return rc.Miss(); + } + } + + return Result.Success; + } + } +} \ No newline at end of file diff --git a/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs b/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs index cd5dfe6e..f33caf1f 100644 --- a/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs +++ b/src/LibHac/FsSrv/Impl/FileSystemInterfaceAdapter.cs @@ -566,7 +566,7 @@ public class FileSystemInterfaceAdapter : IFileSystemSf static Result PermissionCheck(QueryId queryId, FileSystemInterfaceAdapter fsAdapter) { if (queryId == QueryId.SetConcatenationFileAttribute || - queryId == QueryId.IsSignedSystemPartitionOnSdCardValid || + queryId == QueryId.IsSignedSystemPartition || queryId == QueryId.QueryUnpreparedFileInformation) { return Result.Success; diff --git a/src/LibHac/Sdmmc/SdmmcEnums.cs b/src/LibHac/Sdmmc/SdmmcEnums.cs index e7eead68..113246d9 100644 --- a/src/LibHac/Sdmmc/SdmmcEnums.cs +++ b/src/LibHac/Sdmmc/SdmmcEnums.cs @@ -1,5 +1,12 @@ namespace LibHac.Sdmmc; +public enum BusWidth +{ + Width1Bit = 0, + Width4Bit = 1, + Width8Bit = 2, +} + public enum SpeedMode { MmcIdentification = 0, @@ -18,3 +25,10 @@ public enum SpeedMode GcAsicFpgaSpeed = 13, GcAsicSpeed = 14 } + +public enum Port +{ + Mmc0 = 0, + SdCard0 = 1, + GcAsic0 = 2 +} \ No newline at end of file