Add interfaces for the nn::gc APIs

This commit is contained in:
Alex Barney 2023-01-06 14:48:24 -07:00
parent fab5efc4de
commit 94221ef073
9 changed files with 81 additions and 24 deletions

View file

@ -25,10 +25,9 @@ public class EmulatedStorageDeviceManagerFactory : IStorageDeviceManagerFactory
private readonly FileSystemServer _fsServer; private readonly FileSystemServer _fsServer;
private readonly SdmmcApi _sdmmc; private readonly SdmmcApi _sdmmc;
private readonly GameCardEmulated _gc; private readonly IGcApi _gc;
public EmulatedStorageDeviceManagerFactory(FileSystemServer fsServer, SdmmcApi sdmmc, GameCardEmulated gc, public EmulatedStorageDeviceManagerFactory(FileSystemServer fsServer, SdmmcApi sdmmc, IGcApi gc, bool hasGameCard)
bool hasGameCard)
{ {
_fsServer = fsServer; _fsServer = fsServer;
_sdmmc = sdmmc; _sdmmc = sdmmc;

View file

@ -10,7 +10,7 @@ using static LibHac.Gc.Values;
namespace LibHac.Gc; namespace LibHac.Gc;
public class GameCardEmulated public sealed class GameCardEmulated : IGcApi
{ {
private static ReadOnlySpan<byte> CardHeaderKey => new byte[] private static ReadOnlySpan<byte> CardHeaderKey => new byte[]
{ 0x01, 0xC5, 0x8F, 0xE7, 0x00, 0x2D, 0x13, 0x5A, 0xB2, 0x9A, 0x3F, 0x69, 0x33, 0x95, 0x74, 0xB1 }; { 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<byte> _imageHash; private Array32<byte> _imageHash;
public GameCardWriter Writer => new GameCardWriter(this); public GameCardWriter Writer => new GameCardWriter(this);
IGcWriterApi IGcApi.Writer => Writer;
private Result CheckCardReady() private Result CheckCardReady()
{ {
@ -126,7 +127,7 @@ public class GameCardEmulated
return _cardStorage.Get.Read(baseStorageOffset, destination).Ret(); return _cardStorage.Get.Read(baseStorageOffset, destination).Ret();
} }
public readonly struct GameCardWriter public readonly struct GameCardWriter : IGcWriterApi
{ {
private readonly GameCardEmulated _card; private readonly GameCardEmulated _card;

38
src/LibHac/Gc/IGcApi.cs Normal file
View file

@ -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<IStorage> storage);
void RemoveGameCard();
void PresetInternalKeys(ReadOnlySpan<byte> gameCardKey, ReadOnlySpan<byte> gameCardCertificate);
void Initialize(Memory<byte> workBuffer, ulong deviceBufferAddress);
void FinalizeGc();
void PowerOffGameCard();
void RegisterDeviceVirtualAddress(Memory<byte> buffer, ulong deviceBufferAddress);
void UnregisterDeviceVirtualAddress(Memory<byte> buffer, ulong deviceBufferAddress);
Result GetInitializationResult();
Result Activate();
void Deactivate();
Result SetCardToSecureMode();
Result Read(Span<byte> destination, uint pageAddress, uint pageCount);
void PutToSleep();
void Awaken();
bool IsCardInserted();
bool IsCardActivationValid();
Result GetCardStatus(out GameCardStatus outStatus);
Result GetCardDeviceId(Span<byte> destBuffer);
Result GetCardDeviceCertificate(Span<byte> destBuffer);
Result ChallengeCardExistence(Span<byte> responseBuffer, ReadOnlySpan<byte> challengeSeedBuffer, ReadOnlySpan<byte> challengeValueBuffer);
Result GetCardImageHash(Span<byte> destBuffer);
Result GetGameCardIdSet(out GameCardIdSet outGcIdSet);
void RegisterDetectionEventCallback(Action<object> function, object args);
void UnregisterDetectionEventCallback();
Result GetCardHeader(Span<byte> destBuffer);
Result GetErrorInfo(out GameCardErrorReportInfo outErrorReportInfo);
}

View file

@ -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<byte> source, uint pageIndex, uint pageCount);
Result GetCardAvailableRawSize(out long outSize);
void SetVerifyEnableFlag(bool isEnabled);
void SetUserAsicFirmwareBuffer(ReadOnlySpan<byte> firmwareBuffer);
Result GetRmaInformation(out RmaInformation outRmaInformation);
Result WriteDevCardParam(in DevCardParameter devCardParam);
Result ReadDevCardParam(out DevCardParameter outDevCardParam);
Result ForceErase();
}

View file

@ -9,9 +9,9 @@ namespace LibHac.GcSrv;
/// <remarks>Based on nnSdk 14.3.0 (FS 14.1.0)</remarks> /// <remarks>Based on nnSdk 14.3.0 (FS 14.1.0)</remarks>
internal class GameCardDetectionEventManager : CardDeviceDetectionEventManager internal class GameCardDetectionEventManager : CardDeviceDetectionEventManager
{ {
private GameCardEmulated _gc; private IGcApi _gc;
public GameCardDetectionEventManager(GameCardEmulated gc) public GameCardDetectionEventManager(IGcApi gc)
{ {
_gc = gc; _gc = gc;

View file

@ -15,14 +15,14 @@ internal class GameCardDeviceOperator : IStorageDeviceOperator
private SharedRef<GameCardStorageDevice> _storageDevice; private SharedRef<GameCardStorageDevice> _storageDevice;
// LibHac additions // LibHac additions
private readonly GameCardEmulated _gc; private readonly IGcApi _gc;
public static uint BytesToPages(long byteCount) public static uint BytesToPages(long byteCount)
{ {
return (uint)((ulong)byteCount / GcPageSize); return (uint)((ulong)byteCount / GcPageSize);
} }
public GameCardDeviceOperator(ref SharedRef<GameCardStorageDevice> storageDevice, GameCardEmulated gc) public GameCardDeviceOperator(ref SharedRef<GameCardStorageDevice> storageDevice, IGcApi gc)
{ {
_storageDevice = SharedRef<GameCardStorageDevice>.CreateMove(ref storageDevice); _storageDevice = SharedRef<GameCardStorageDevice>.CreateMove(ref storageDevice);
_gc = gc; _gc = gc;

View file

@ -39,9 +39,9 @@ public class GameCardManager : IStorageDeviceManager, IStorageDeviceOperator, IG
// LibHac additions // LibHac additions
private WeakRef<GameCardManager> _selfReference; private WeakRef<GameCardManager> _selfReference;
private readonly FileSystemServer _fsServer; 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); _rwLock = new ReaderWriterLock(fsServer.Hos.Os);
@ -49,7 +49,7 @@ public class GameCardManager : IStorageDeviceManager, IStorageDeviceOperator, IG
_gc = gc; _gc = gc;
} }
public static SharedRef<GameCardManager> CreateShared(GameCardEmulated gc, FileSystemServer fsServer) public static SharedRef<GameCardManager> CreateShared(IGcApi gc, FileSystemServer fsServer)
{ {
var manager = new GameCardManager(gc, fsServer); var manager = new GameCardManager(gc, fsServer);

View file

@ -20,9 +20,9 @@ internal class ReadOnlyGameCardStorage : IStorage
private SharedRef<IGameCardManager> _deviceManager; private SharedRef<IGameCardManager> _deviceManager;
// LibHac additions // LibHac additions
private readonly GameCardEmulated _gc; private readonly IGcApi _gc;
public ReadOnlyGameCardStorage(ref SharedRef<IGameCardManager> deviceManger, GameCardEmulated gc) public ReadOnlyGameCardStorage(ref SharedRef<IGameCardManager> deviceManger, IGcApi gc)
{ {
_deviceManager = SharedRef<IGameCardManager>.CreateMove(ref deviceManger); _deviceManager = SharedRef<IGameCardManager>.CreateMove(ref deviceManger);
_gc = gc; _gc = gc;
@ -103,9 +103,9 @@ internal class WriteOnlyGameCardStorage : IStorage
private SharedRef<IGameCardManager> _deviceManager; private SharedRef<IGameCardManager> _deviceManager;
// LibHac additions // LibHac additions
private readonly GameCardEmulated _gc; private readonly IGcApi _gc;
public WriteOnlyGameCardStorage(ref SharedRef<IGameCardManager> deviceManger, GameCardEmulated gc) public WriteOnlyGameCardStorage(ref SharedRef<IGameCardManager> deviceManger, IGcApi gc)
{ {
_deviceManager = SharedRef<IGameCardManager>.CreateMove(ref deviceManger); _deviceManager = SharedRef<IGameCardManager>.CreateMove(ref deviceManger);
_gc = gc; _gc = gc;

View file

@ -20,9 +20,9 @@ internal class GameCardStorageDevice : GameCardStorageInterfaceAdapter, IStorage
// LibHac additions // LibHac additions
private WeakRef<GameCardStorageDevice> _selfReference; private WeakRef<GameCardStorageDevice> _selfReference;
private readonly GameCardEmulated _gc; private readonly IGcApi _gc;
private GameCardStorageDevice(GameCardEmulated gc, ref SharedRef<IGameCardManager> manager, private GameCardStorageDevice(IGcApi gc, ref SharedRef<IGameCardManager> manager,
in SharedRef<IStorage> baseStorage, GameCardHandle handle) : base(in baseStorage) in SharedRef<IStorage> baseStorage, GameCardHandle handle) : base(in baseStorage)
{ {
_manager = SharedRef<IGameCardManager>.CreateMove(ref manager); _manager = SharedRef<IGameCardManager>.CreateMove(ref manager);
@ -32,7 +32,7 @@ internal class GameCardStorageDevice : GameCardStorageInterfaceAdapter, IStorage
_gc = gc; _gc = gc;
} }
private GameCardStorageDevice(GameCardEmulated gc, ref SharedRef<IGameCardManager> manager, private GameCardStorageDevice(IGcApi gc, ref SharedRef<IGameCardManager> manager,
in SharedRef<IStorage> baseStorage, GameCardHandle handle, bool isSecure, ReadOnlySpan<byte> cardDeviceId, in SharedRef<IStorage> baseStorage, GameCardHandle handle, bool isSecure, ReadOnlySpan<byte> cardDeviceId,
ReadOnlySpan<byte> cardImageHash) ReadOnlySpan<byte> cardImageHash)
: base(in baseStorage) : base(in baseStorage)
@ -50,8 +50,8 @@ internal class GameCardStorageDevice : GameCardStorageInterfaceAdapter, IStorage
_gc = gc; _gc = gc;
} }
public static SharedRef<GameCardStorageDevice> CreateShared(GameCardEmulated gc, public static SharedRef<GameCardStorageDevice> CreateShared(IGcApi gc, ref SharedRef<IGameCardManager> manager,
ref SharedRef<IGameCardManager> manager, in SharedRef<IStorage> baseStorage, GameCardHandle handle) in SharedRef<IStorage> baseStorage, GameCardHandle handle)
{ {
var storageDevice = new GameCardStorageDevice(gc, ref manager, in baseStorage, handle); var storageDevice = new GameCardStorageDevice(gc, ref manager, in baseStorage, handle);
@ -61,9 +61,9 @@ internal class GameCardStorageDevice : GameCardStorageInterfaceAdapter, IStorage
return SharedRef<GameCardStorageDevice>.CreateMove(ref sharedStorageDevice.Ref); return SharedRef<GameCardStorageDevice>.CreateMove(ref sharedStorageDevice.Ref);
} }
public static SharedRef<GameCardStorageDevice> CreateShared(GameCardEmulated gc, public static SharedRef<GameCardStorageDevice> CreateShared(IGcApi gc, ref SharedRef<IGameCardManager> manager,
ref SharedRef<IGameCardManager> manager, in SharedRef<IStorage> baseStorage, GameCardHandle handle, in SharedRef<IStorage> baseStorage, GameCardHandle handle, bool isSecure, ReadOnlySpan<byte> cardDeviceId,
bool isSecure, ReadOnlySpan<byte> cardDeviceId, ReadOnlySpan<byte> cardImageHash) ReadOnlySpan<byte> cardImageHash)
{ {
var storageDevice = new GameCardStorageDevice(gc, ref manager, in baseStorage, handle, isSecure, cardDeviceId, var storageDevice = new GameCardStorageDevice(gc, ref manager, in baseStorage, handle, isSecure, cardDeviceId,
cardImageHash); cardImageHash);