diff --git a/src/LibHac/Fs/GameCard.cs b/src/LibHac/Fs/GameCard.cs index c4a22cf8..a10d301e 100644 --- a/src/LibHac/Fs/GameCard.cs +++ b/src/LibHac/Fs/GameCard.cs @@ -1,4 +1,6 @@ -using System; +global using GameCardHandle = System.UInt32; + +using System; using LibHac.Common.FixedArrays; namespace LibHac.Fs; @@ -81,20 +83,4 @@ public struct GameCardErrorReportInfo public uint ReadCountFromInsert; public uint ReadCountFromAwaken; public Array8 Reserved38; -} - -public readonly struct GameCardHandle : IEquatable -{ - public readonly int Value; - - public GameCardHandle(int value) - { - Value = value; - } - - public override bool Equals(object obj) => obj is GameCardHandle handle && Equals(handle); - public bool Equals(GameCardHandle other) => Value == other.Value; - public override int GetHashCode() => Value.GetHashCode(); - public static bool operator ==(GameCardHandle left, GameCardHandle right) => left.Equals(right); - public static bool operator !=(GameCardHandle left, GameCardHandle right) => !(left == right); } \ No newline at end of file diff --git a/src/LibHac/Fs/Shim/GameCard.cs b/src/LibHac/Fs/Shim/GameCard.cs index f51f98b4..0a38a02f 100644 --- a/src/LibHac/Fs/Shim/GameCard.cs +++ b/src/LibHac/Fs/Shim/GameCard.cs @@ -63,7 +63,7 @@ public static class GameCard var sb = new U8StringBuilder(nameBuffer); sb.Append(CommonMountNames.GameCardFileSystemMountName) .Append(GetGameCardMountNameSuffix(_partitionId)) - .AppendFormat(_handle.Value, 'x', (byte)handleDigitCount) + .AppendFormat(_handle, 'x', (byte)handleDigitCount) .Append(StringTraits.DriveSeparator); Assert.SdkEqual(sb.Length, requiredNameBufferSize - 1); @@ -83,11 +83,11 @@ public static class GameCard fs.Impl.AbortIfNeeded(rc); if (rc.IsFailure()) return rc; - rc = deviceOperator.Get.GetGameCardHandle(out uint handle); + rc = deviceOperator.Get.GetGameCardHandle(out GameCardHandle handle); fs.Impl.AbortIfNeeded(rc); if (rc.IsFailure()) return rc.Miss(); - outHandle = new GameCardHandle((int)handle); + outHandle = handle; return Result.Success; } @@ -107,7 +107,7 @@ public static class GameCard var sb = new U8StringBuilder(logBuffer, true); sb.Append(LogName).Append(mountName).Append(LogQuote) - .Append(LogGameCardHandle).AppendFormat(handle.Value) + .Append(LogGameCardHandle).AppendFormat(handle) .Append(LogGameCardPartition).Append(idString.ToString(partitionId)); fs.Impl.OutputAccessLog(rc, start, end, null, new U8Span(sb.Buffer)); diff --git a/src/LibHac/FsSrv/EmulatedDeviceOperator.cs b/src/LibHac/FsSrv/EmulatedDeviceOperator.cs index d6e6452c..e1793966 100644 --- a/src/LibHac/FsSrv/EmulatedDeviceOperator.cs +++ b/src/LibHac/FsSrv/EmulatedDeviceOperator.cs @@ -108,11 +108,6 @@ public class EmulatedDeviceOperator : IDeviceOperator throw new NotImplementedException(); } - public Result GetGameCardHandle(out uint outHandle) - { - throw new NotImplementedException(); - } - public Result GetGameCardUpdatePartitionInfo(out uint outCupVersion, out ulong outCupId, uint handle) { throw new NotImplementedException(); diff --git a/src/LibHac/FsSrv/EmulatedGameCard.cs b/src/LibHac/FsSrv/EmulatedGameCard.cs index 60bfbc83..bbb47c93 100644 --- a/src/LibHac/FsSrv/EmulatedGameCard.cs +++ b/src/LibHac/FsSrv/EmulatedGameCard.cs @@ -9,7 +9,7 @@ namespace LibHac.FsSrv; public class EmulatedGameCard { private IStorage CardImageStorage { get; set; } - private int Handle { get; set; } + private GameCardHandle Handle { get; set; } private XciHeader CardHeader { get; set; } private Xci CardImage { get; set; } private KeySet KeySet { get; set; } @@ -22,12 +22,12 @@ public class EmulatedGameCard } public GameCardHandle GetGameCardHandle() { - return new GameCardHandle(Handle); + return Handle; } public bool IsGameCardHandleInvalid(GameCardHandle handle) { - return Handle != handle.Value; + return Handle != handle; } public bool IsGameCardInserted() diff --git a/src/LibHac/FsSrv/Impl/DeviceOperator.cs b/src/LibHac/FsSrv/Impl/DeviceOperator.cs index ad05ebb9..ab9d1718 100644 --- a/src/LibHac/FsSrv/Impl/DeviceOperator.cs +++ b/src/LibHac/FsSrv/Impl/DeviceOperator.cs @@ -225,7 +225,7 @@ public class DeviceOperator : IDeviceOperator return _fsServer.Storage.EraseGameCard(gameCardSize, romAreaStartPageAddress).Ret(); } - public Result GetGameCardHandle(out uint outHandle) + public Result GetGameCardHandle(out GameCardHandle outHandle) { UnsafeHelpers.SkipParamInit(out outHandle); @@ -236,14 +236,14 @@ public class DeviceOperator : IDeviceOperator if (!isInserted) return ResultFs.GameCardFsGetHandleFailure.Log(); - rc = _fsServer.Storage.GetGameCardHandle(out uint handle); + rc = _fsServer.Storage.GetGameCardHandle(out GameCardHandle handle); if (rc.IsFailure()) return rc.Miss(); outHandle = handle; return Result.Success; } - public Result GetGameCardUpdatePartitionInfo(out uint outCupVersion, out ulong outCupId, uint handle) + public Result GetGameCardUpdatePartitionInfo(out uint outCupVersion, out ulong outCupId, GameCardHandle handle) { UnsafeHelpers.SkipParamInit(out outCupVersion, out outCupId); @@ -265,7 +265,7 @@ public class DeviceOperator : IDeviceOperator return Result.Success; } - public Result GetGameCardAttribute(out byte outAttribute, uint handle) + public Result GetGameCardAttribute(out byte outAttribute, GameCardHandle handle) { UnsafeHelpers.SkipParamInit(out outAttribute); @@ -276,7 +276,7 @@ public class DeviceOperator : IDeviceOperator return Result.Success; } - public Result GetGameCardCompatibilityType(out byte outCompatibilityType, uint handle) + public Result GetGameCardCompatibilityType(out byte outCompatibilityType, GameCardHandle handle) { UnsafeHelpers.SkipParamInit(out outCompatibilityType); @@ -287,7 +287,7 @@ public class DeviceOperator : IDeviceOperator return Result.Success; } - public Result GetGameCardDeviceCertificate(OutBuffer outBuffer, long outBufferSize, uint handle) + public Result GetGameCardDeviceCertificate(OutBuffer outBuffer, long outBufferSize, GameCardHandle handle) { if (!_accessControl.CanCall(OperationType.GetGameCardDeviceCertificate)) return ResultFs.PermissionDenied.Log(); @@ -299,7 +299,7 @@ public class DeviceOperator : IDeviceOperator } public Result ChallengeCardExistence(OutBuffer outResponseBuffer, InBuffer challengeSeedBuffer, - InBuffer challengeValueBuffer, uint handle) + InBuffer challengeValueBuffer, GameCardHandle handle) { if (!_accessControl.CanCall(OperationType.ChallengeCardExistence)) return ResultFs.PermissionDenied.Log(); @@ -370,7 +370,7 @@ public class DeviceOperator : IDeviceOperator return Result.Success; } - public Result GetGameCardImageHash(OutBuffer outBuffer, long outBufferSize, uint handle) + public Result GetGameCardImageHash(OutBuffer outBuffer, long outBufferSize, GameCardHandle handle) { if (outBuffer.Size < outBufferSize) return ResultFs.InvalidSize.Log(); diff --git a/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs b/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs index ba2131e3..478bbb87 100644 --- a/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs +++ b/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs @@ -104,8 +104,7 @@ public class NcaFileSystemServiceImpl if (type == FileSystemProxyType.Logo && mountNameInfo.IsGameCard) { - rc = _config.BaseFsService.OpenGameCardFileSystem(ref outFileSystem, - new GameCardHandle(mountNameInfo.GcHandle), + rc = _config.BaseFsService.OpenGameCardFileSystem(ref outFileSystem, (uint)mountNameInfo.GcHandle, GameCardPartition.Logo); if (rc.IsSuccess()) @@ -352,8 +351,7 @@ public class NcaFileSystemServiceImpl path = path.Slice(8); - Result rc = _config.BaseFsService.OpenGameCardFileSystem(ref outFileSystem, new GameCardHandle(handle), - partition); + Result rc = _config.BaseFsService.OpenGameCardFileSystem(ref outFileSystem, (uint)handle, partition); if (rc.IsFailure()) return rc; info.GcHandle = handle; diff --git a/src/LibHac/FsSrv/Sf/IDeviceOperator.cs b/src/LibHac/FsSrv/Sf/IDeviceOperator.cs index df0dfc1e..e60aabe3 100644 --- a/src/LibHac/FsSrv/Sf/IDeviceOperator.cs +++ b/src/LibHac/FsSrv/Sf/IDeviceOperator.cs @@ -23,16 +23,16 @@ public interface IDeviceOperator : IDisposable Result ResumeMmcPatrol(); Result IsGameCardInserted(out bool outIsInserted); Result EraseGameCard(uint gameCardSize, ulong romAreaStartPageAddress); - Result GetGameCardHandle(out uint outHandle); - Result GetGameCardUpdatePartitionInfo(out uint outCupVersion, out ulong outCupId, uint handle); + Result GetGameCardHandle(out GameCardHandle outHandle); + Result GetGameCardUpdatePartitionInfo(out uint outCupVersion, out ulong outCupId, GameCardHandle handle); Result FinalizeGameCardDriver(); - Result GetGameCardAttribute(out byte outAttribute, uint handle); - Result GetGameCardDeviceCertificate(OutBuffer outBuffer, long outBufferSize, uint handle); + Result GetGameCardAttribute(out byte outAttribute, GameCardHandle handle); + Result GetGameCardDeviceCertificate(OutBuffer outBuffer, long outBufferSize, GameCardHandle handle); Result GetGameCardAsicInfo(OutBuffer outRmaInfoBuffer, long rmaInfoBufferSize, InBuffer asicFirmwareBuffer, long asicFirmwareBufferSize); Result GetGameCardIdSet(OutBuffer outBuffer, long outBufferSize); Result WriteToGameCardDirectly(long offset, OutBuffer buffer, long bufferSize); Result SetVerifyWriteEnableFlag(bool isEnabled); - Result GetGameCardImageHash(OutBuffer outBuffer, long outBufferSize, uint handle); + Result GetGameCardImageHash(OutBuffer outBuffer, long outBufferSize, GameCardHandle handle); Result GetGameCardDeviceIdForProdCard(OutBuffer outBuffer, long outBufferSize, InBuffer devHeaderBuffer, long devHeaderBufferSize); Result EraseAndWriteParamDirectly(InBuffer inBuffer, long inBufferSize); Result ReadParamDirectly(OutBuffer outBuffer, long outBufferSize); @@ -40,8 +40,8 @@ public interface IDeviceOperator : IDisposable Result GetGameCardErrorInfo(out GameCardErrorInfo outErrorInfo); Result GetGameCardErrorReportInfo(out GameCardErrorReportInfo outErrorInfo); Result GetGameCardDeviceId(OutBuffer outBuffer, long outBufferSize); - Result ChallengeCardExistence(OutBuffer outResponseBuffer, InBuffer challengeSeedBuffer, InBuffer challengeValueBuffer, uint handle); - Result GetGameCardCompatibilityType(out byte outCompatibilityType, uint handle); + Result ChallengeCardExistence(OutBuffer outResponseBuffer, InBuffer challengeSeedBuffer, InBuffer challengeValueBuffer, GameCardHandle handle); + Result GetGameCardCompatibilityType(out byte outCompatibilityType, GameCardHandle handle); Result SetSpeedEmulationMode(int mode); Result GetSpeedEmulationMode(out int outMode); Result SuspendSdmmcControl(); diff --git a/src/LibHac/FsSrv/Storage/GameCardService.cs b/src/LibHac/FsSrv/Storage/GameCardService.cs index f2503958..36ded37a 100644 --- a/src/LibHac/FsSrv/Storage/GameCardService.cs +++ b/src/LibHac/FsSrv/Storage/GameCardService.cs @@ -106,7 +106,7 @@ internal static class GameCardService } public static Result OpenGameCardStorage(this StorageService service, ref SharedRef outStorage, - OpenGameCardAttribute attribute, uint handle) + OpenGameCardAttribute attribute, GameCardHandle handle) { using var gameCardStorageDevice = new SharedRef(); @@ -153,10 +153,10 @@ internal static class GameCardService { if (g.CachedStorageDevice.HasValue) { - Result rc = g.CachedStorageDevice.Get.GetHandle(out uint handleValue); + Result rc = g.CachedStorageDevice.Get.GetHandle(out GameCardHandle handle); if (rc.IsFailure()) return rc.Miss(); - outHandle = new StorageDeviceHandle(handleValue, StorageDevicePortId.GameCard); + outHandle = new StorageDeviceHandle(handle, StorageDevicePortId.GameCard); return Result.Success; } } @@ -167,7 +167,7 @@ internal static class GameCardService OpenGameCardAttribute.ReadOnly); if (rc.IsFailure()) return rc.Miss(); - rc = gameCardStorageDevice.Get.GetHandle(out uint handleValue); + rc = gameCardStorageDevice.Get.GetHandle(out GameCardHandle handleValue); if (rc.IsFailure()) return rc.Miss(); outHandle = new StorageDeviceHandle(handleValue, StorageDevicePortId.GameCard); @@ -228,7 +228,7 @@ internal static class GameCardService } public static Result GetGameCardStatus(this StorageService service, out GameCardStatus outGameCardStatus, - uint handle) + GameCardHandle handle) { UnsafeHelpers.SkipParamInit(out outGameCardStatus); @@ -267,7 +267,8 @@ internal static class GameCardService return gcOperator.Get.Operate(operationId); } - public static Result GetGameCardDeviceCertificate(this StorageService service, Span outBuffer, uint handle) + public static Result GetGameCardDeviceCertificate(this StorageService service, Span outBuffer, + GameCardHandle handle) { using var gcOperator = new SharedRef(); Result rc = service.GetGameCardOperator(ref gcOperator.Ref()); @@ -294,7 +295,7 @@ internal static class GameCardService } public static Result ChallengeCardExistence(this StorageService service, Span outResponseBuffer, - ReadOnlySpan challengeSeed, ReadOnlySpan challengeValue, uint handle) + ReadOnlySpan challengeSeed, ReadOnlySpan challengeValue, GameCardHandle handle) { using var gcOperator = new SharedRef(); Result rc = service.GetGameCardOperator(ref gcOperator.Ref()); @@ -323,16 +324,16 @@ internal static class GameCardService return Result.Success; } - public static Result GetGameCardHandle(this StorageService service, out uint handle) + public static Result GetGameCardHandle(this StorageService service, out GameCardHandle outHandle) { - UnsafeHelpers.SkipParamInit(out handle); + UnsafeHelpers.SkipParamInit(out outHandle); using var gcOperator = new SharedRef(); Result rc = service.GetGameCardManagerOperator(ref gcOperator.Ref()); if (rc.IsFailure()) return rc.Miss(); // Get the current handle. - OutBuffer handleOutBuffer = OutBuffer.FromStruct(ref handle); + OutBuffer handleOutBuffer = OutBuffer.FromStruct(ref outHandle); int operationId = MakeOperationId(GameCardManagerOperationIdValue.GetHandle); rc = gcOperator.Get.OperateOut(out long bytesWritten, handleOutBuffer, operationId); @@ -346,7 +347,7 @@ internal static class GameCardService if (g.CachedStorageDevice.HasValue) { - g.CachedStorageDevice.Get.GetHandle(out uint handleValue); + g.CachedStorageDevice.Get.GetHandle(out GameCardHandle handleValue); if (rc.IsFailure()) return rc.Miss(); var currentHandle = new StorageDeviceHandle(handleValue, StorageDevicePortId.GameCard); @@ -432,7 +433,7 @@ internal static class GameCardService return gcOperator.Get.OperateIn(inIsEnabledBuffer, offset: 0, size: 0, operationId); } - public static Result GetGameCardImageHash(this StorageService service, Span outBuffer, uint handle) + public static Result GetGameCardImageHash(this StorageService service, Span outBuffer, GameCardHandle handle) { using var gcOperator = new SharedRef(); Result rc = service.GetGameCardOperator(ref gcOperator.Ref()); @@ -573,7 +574,7 @@ internal static class GameCardService return Result.Success; } - public static bool IsGameCardActivationValid(this StorageService service, uint handle) + public static bool IsGameCardActivationValid(this StorageService service, GameCardHandle handle) { using var gcOperator = new SharedRef(); Result rc = service.GetGameCardManagerOperator(ref gcOperator.Ref()); diff --git a/tests/LibHac.Tests/Fs/TypeLayoutTests.cs b/tests/LibHac.Tests/Fs/TypeLayoutTests.cs index 0ef83b6e..0c20f71d 100644 --- a/tests/LibHac.Tests/Fs/TypeLayoutTests.cs +++ b/tests/LibHac.Tests/Fs/TypeLayoutTests.cs @@ -492,16 +492,6 @@ public class TypeLayoutTests Assert.Equal(0x38, GetOffset(in s, in s.Reserved38)); } - [Fact] - public static void GameCardHandle_Layout() - { - var s = new GameCardHandle(); - - Assert.Equal(4, Unsafe.SizeOf()); - - Assert.Equal(0, GetOffset(in s, in s.Value)); - } - [Fact] public static void Int64_Layout() {