From 94221ef0731c5490292a8e901aa2d6dc400488d3 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Fri, 6 Jan 2023 14:48:24 -0700 Subject: [PATCH] Add interfaces for the nn::gc APIs --- .../EmulatedStorageDeviceManagerFactory.cs | 5 +-- src/LibHac/Gc/GameCardEmulated.cs | 5 ++- src/LibHac/Gc/IGcApi.cs | 38 +++++++++++++++++++ src/LibHac/Gc/Writer/IGcWriterApi.cs | 19 ++++++++++ .../GcSrv/GameCardDetectionEventManager.cs | 4 +- src/LibHac/GcSrv/GameCardDeviceOperator.cs | 4 +- src/LibHac/GcSrv/GameCardManager.cs | 6 +-- src/LibHac/GcSrv/GameCardStorage.cs | 8 ++-- src/LibHac/GcSrv/GameCardStorageDevice.cs | 16 ++++---- 9 files changed, 81 insertions(+), 24 deletions(-) create mode 100644 src/LibHac/Gc/IGcApi.cs create mode 100644 src/LibHac/Gc/Writer/IGcWriterApi.cs diff --git a/src/LibHac/FsSrv/Storage/EmulatedStorageDeviceManagerFactory.cs b/src/LibHac/FsSrv/Storage/EmulatedStorageDeviceManagerFactory.cs index 94ffcdfe..8ced2ede 100644 --- a/src/LibHac/FsSrv/Storage/EmulatedStorageDeviceManagerFactory.cs +++ b/src/LibHac/FsSrv/Storage/EmulatedStorageDeviceManagerFactory.cs @@ -25,10 +25,9 @@ public class EmulatedStorageDeviceManagerFactory : IStorageDeviceManagerFactory private readonly FileSystemServer _fsServer; private readonly SdmmcApi _sdmmc; - private readonly GameCardEmulated _gc; + private readonly IGcApi _gc; - public EmulatedStorageDeviceManagerFactory(FileSystemServer fsServer, SdmmcApi sdmmc, GameCardEmulated gc, - bool hasGameCard) + public EmulatedStorageDeviceManagerFactory(FileSystemServer fsServer, SdmmcApi sdmmc, IGcApi gc, bool hasGameCard) { _fsServer = fsServer; _sdmmc = sdmmc; diff --git a/src/LibHac/Gc/GameCardEmulated.cs b/src/LibHac/Gc/GameCardEmulated.cs index 5db1c734..27c82038 100644 --- a/src/LibHac/Gc/GameCardEmulated.cs +++ b/src/LibHac/Gc/GameCardEmulated.cs @@ -10,7 +10,7 @@ using static LibHac.Gc.Values; namespace LibHac.Gc; -public class GameCardEmulated +public sealed class GameCardEmulated : IGcApi { private static ReadOnlySpan CardHeaderKey => new byte[] { 0x01, 0xC5, 0x8F, 0xE7, 0x00, 0x2D, 0x13, 0x5A, 0xB2, 0x9A, 0x3F, 0x69, 0x33, 0x95, 0x74, 0xB1 }; @@ -29,6 +29,7 @@ public class GameCardEmulated private Array32 _imageHash; public GameCardWriter Writer => new GameCardWriter(this); + IGcWriterApi IGcApi.Writer => Writer; private Result CheckCardReady() { @@ -126,7 +127,7 @@ public class GameCardEmulated return _cardStorage.Get.Read(baseStorageOffset, destination).Ret(); } - public readonly struct GameCardWriter + public readonly struct GameCardWriter : IGcWriterApi { private readonly GameCardEmulated _card; diff --git a/src/LibHac/Gc/IGcApi.cs b/src/LibHac/Gc/IGcApi.cs new file mode 100644 index 00000000..29c7d474 --- /dev/null +++ b/src/LibHac/Gc/IGcApi.cs @@ -0,0 +1,38 @@ +using System; +using LibHac.Common; +using LibHac.Fs; +using LibHac.Gc.Writer; + +namespace LibHac.Gc; + +public interface IGcApi +{ + IGcWriterApi Writer { get; } + void InsertGameCard(in SharedRef storage); + void RemoveGameCard(); + void PresetInternalKeys(ReadOnlySpan gameCardKey, ReadOnlySpan gameCardCertificate); + void Initialize(Memory workBuffer, ulong deviceBufferAddress); + void FinalizeGc(); + void PowerOffGameCard(); + void RegisterDeviceVirtualAddress(Memory buffer, ulong deviceBufferAddress); + void UnregisterDeviceVirtualAddress(Memory buffer, ulong deviceBufferAddress); + Result GetInitializationResult(); + Result Activate(); + void Deactivate(); + Result SetCardToSecureMode(); + Result Read(Span destination, uint pageAddress, uint pageCount); + void PutToSleep(); + void Awaken(); + bool IsCardInserted(); + bool IsCardActivationValid(); + Result GetCardStatus(out GameCardStatus outStatus); + Result GetCardDeviceId(Span destBuffer); + Result GetCardDeviceCertificate(Span destBuffer); + Result ChallengeCardExistence(Span responseBuffer, ReadOnlySpan challengeSeedBuffer, ReadOnlySpan challengeValueBuffer); + Result GetCardImageHash(Span destBuffer); + Result GetGameCardIdSet(out GameCardIdSet outGcIdSet); + void RegisterDetectionEventCallback(Action function, object args); + void UnregisterDetectionEventCallback(); + Result GetCardHeader(Span destBuffer); + Result GetErrorInfo(out GameCardErrorReportInfo outErrorReportInfo); +} \ No newline at end of file diff --git a/src/LibHac/Gc/Writer/IGcWriterApi.cs b/src/LibHac/Gc/Writer/IGcWriterApi.cs new file mode 100644 index 00000000..72a55f0c --- /dev/null +++ b/src/LibHac/Gc/Writer/IGcWriterApi.cs @@ -0,0 +1,19 @@ +using System; +using LibHac.Gc.Impl; + +namespace LibHac.Gc.Writer; + +public interface IGcWriterApi +{ + void ChangeMode(AsicMode mode); + Result ActivateForWriter(); + Result EraseAndWriteParameter(MemorySize size, uint romAreaStartPageIndex); + Result Write(ReadOnlySpan source, uint pageIndex, uint pageCount); + Result GetCardAvailableRawSize(out long outSize); + void SetVerifyEnableFlag(bool isEnabled); + void SetUserAsicFirmwareBuffer(ReadOnlySpan firmwareBuffer); + Result GetRmaInformation(out RmaInformation outRmaInformation); + Result WriteDevCardParam(in DevCardParameter devCardParam); + Result ReadDevCardParam(out DevCardParameter outDevCardParam); + Result ForceErase(); +} \ No newline at end of file diff --git a/src/LibHac/GcSrv/GameCardDetectionEventManager.cs b/src/LibHac/GcSrv/GameCardDetectionEventManager.cs index 0a927714..1c4be90c 100644 --- a/src/LibHac/GcSrv/GameCardDetectionEventManager.cs +++ b/src/LibHac/GcSrv/GameCardDetectionEventManager.cs @@ -9,9 +9,9 @@ namespace LibHac.GcSrv; /// Based on nnSdk 14.3.0 (FS 14.1.0) internal class GameCardDetectionEventManager : CardDeviceDetectionEventManager { - private GameCardEmulated _gc; + private IGcApi _gc; - public GameCardDetectionEventManager(GameCardEmulated gc) + public GameCardDetectionEventManager(IGcApi gc) { _gc = gc; diff --git a/src/LibHac/GcSrv/GameCardDeviceOperator.cs b/src/LibHac/GcSrv/GameCardDeviceOperator.cs index 797f1f75..4229aa61 100644 --- a/src/LibHac/GcSrv/GameCardDeviceOperator.cs +++ b/src/LibHac/GcSrv/GameCardDeviceOperator.cs @@ -15,14 +15,14 @@ internal class GameCardDeviceOperator : IStorageDeviceOperator private SharedRef _storageDevice; // LibHac additions - private readonly GameCardEmulated _gc; + private readonly IGcApi _gc; public static uint BytesToPages(long byteCount) { return (uint)((ulong)byteCount / GcPageSize); } - public GameCardDeviceOperator(ref SharedRef storageDevice, GameCardEmulated gc) + public GameCardDeviceOperator(ref SharedRef storageDevice, IGcApi gc) { _storageDevice = SharedRef.CreateMove(ref storageDevice); _gc = gc; diff --git a/src/LibHac/GcSrv/GameCardManager.cs b/src/LibHac/GcSrv/GameCardManager.cs index 75aa5cdd..d0dcfbfa 100644 --- a/src/LibHac/GcSrv/GameCardManager.cs +++ b/src/LibHac/GcSrv/GameCardManager.cs @@ -39,9 +39,9 @@ public class GameCardManager : IStorageDeviceManager, IStorageDeviceOperator, IG // LibHac additions private WeakRef _selfReference; private readonly FileSystemServer _fsServer; - private readonly GameCardEmulated _gc; + private readonly IGcApi _gc; - private GameCardManager(GameCardEmulated gc, FileSystemServer fsServer) + private GameCardManager(IGcApi gc, FileSystemServer fsServer) { _rwLock = new ReaderWriterLock(fsServer.Hos.Os); @@ -49,7 +49,7 @@ public class GameCardManager : IStorageDeviceManager, IStorageDeviceOperator, IG _gc = gc; } - public static SharedRef CreateShared(GameCardEmulated gc, FileSystemServer fsServer) + public static SharedRef CreateShared(IGcApi gc, FileSystemServer fsServer) { var manager = new GameCardManager(gc, fsServer); diff --git a/src/LibHac/GcSrv/GameCardStorage.cs b/src/LibHac/GcSrv/GameCardStorage.cs index 0af56a7f..1bd934d2 100644 --- a/src/LibHac/GcSrv/GameCardStorage.cs +++ b/src/LibHac/GcSrv/GameCardStorage.cs @@ -20,9 +20,9 @@ internal class ReadOnlyGameCardStorage : IStorage private SharedRef _deviceManager; // LibHac additions - private readonly GameCardEmulated _gc; + private readonly IGcApi _gc; - public ReadOnlyGameCardStorage(ref SharedRef deviceManger, GameCardEmulated gc) + public ReadOnlyGameCardStorage(ref SharedRef deviceManger, IGcApi gc) { _deviceManager = SharedRef.CreateMove(ref deviceManger); _gc = gc; @@ -103,9 +103,9 @@ internal class WriteOnlyGameCardStorage : IStorage private SharedRef _deviceManager; // LibHac additions - private readonly GameCardEmulated _gc; + private readonly IGcApi _gc; - public WriteOnlyGameCardStorage(ref SharedRef deviceManger, GameCardEmulated gc) + public WriteOnlyGameCardStorage(ref SharedRef deviceManger, IGcApi gc) { _deviceManager = SharedRef.CreateMove(ref deviceManger); _gc = gc; diff --git a/src/LibHac/GcSrv/GameCardStorageDevice.cs b/src/LibHac/GcSrv/GameCardStorageDevice.cs index 16c35927..fbf1310a 100644 --- a/src/LibHac/GcSrv/GameCardStorageDevice.cs +++ b/src/LibHac/GcSrv/GameCardStorageDevice.cs @@ -20,9 +20,9 @@ internal class GameCardStorageDevice : GameCardStorageInterfaceAdapter, IStorage // LibHac additions private WeakRef _selfReference; - private readonly GameCardEmulated _gc; + private readonly IGcApi _gc; - private GameCardStorageDevice(GameCardEmulated gc, ref SharedRef manager, + private GameCardStorageDevice(IGcApi gc, ref SharedRef manager, in SharedRef baseStorage, GameCardHandle handle) : base(in baseStorage) { _manager = SharedRef.CreateMove(ref manager); @@ -32,7 +32,7 @@ internal class GameCardStorageDevice : GameCardStorageInterfaceAdapter, IStorage _gc = gc; } - private GameCardStorageDevice(GameCardEmulated gc, ref SharedRef manager, + private GameCardStorageDevice(IGcApi gc, ref SharedRef manager, in SharedRef baseStorage, GameCardHandle handle, bool isSecure, ReadOnlySpan cardDeviceId, ReadOnlySpan cardImageHash) : base(in baseStorage) @@ -50,8 +50,8 @@ internal class GameCardStorageDevice : GameCardStorageInterfaceAdapter, IStorage _gc = gc; } - public static SharedRef CreateShared(GameCardEmulated gc, - ref SharedRef manager, in SharedRef baseStorage, GameCardHandle handle) + public static SharedRef CreateShared(IGcApi gc, ref SharedRef manager, + in SharedRef baseStorage, GameCardHandle handle) { var storageDevice = new GameCardStorageDevice(gc, ref manager, in baseStorage, handle); @@ -61,9 +61,9 @@ internal class GameCardStorageDevice : GameCardStorageInterfaceAdapter, IStorage return SharedRef.CreateMove(ref sharedStorageDevice.Ref); } - public static SharedRef CreateShared(GameCardEmulated gc, - ref SharedRef manager, in SharedRef baseStorage, GameCardHandle handle, - bool isSecure, ReadOnlySpan cardDeviceId, ReadOnlySpan cardImageHash) + public static SharedRef CreateShared(IGcApi gc, ref SharedRef manager, + in SharedRef baseStorage, GameCardHandle handle, bool isSecure, ReadOnlySpan cardDeviceId, + ReadOnlySpan cardImageHash) { var storageDevice = new GameCardStorageDevice(gc, ref manager, in baseStorage, handle, isSecure, cardDeviceId, cardImageHash);