2019-06-01 02:31:10 +02:00
|
|
|
using LibHac.Fs;
|
|
|
|
using LibHac.Fs.NcaUtils;
|
2018-10-17 19:15:50 +02:00
|
|
|
using Ryujinx.Common.Logging;
|
2019-01-05 01:41:49 +01:00
|
|
|
using Ryujinx.HLE.FileSystem;
|
2018-08-17 01:47:36 +02:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
using Ryujinx.HLE.HOS.SystemState;
|
2018-04-26 02:52:22 +02:00
|
|
|
using System;
|
2018-03-01 21:23:55 +01:00
|
|
|
using System.Collections.Generic;
|
2018-04-22 01:04:43 +02:00
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
2018-03-01 21:23:55 +01:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Set
|
2018-03-01 21:23:55 +01:00
|
|
|
{
|
2018-04-06 06:01:52 +02:00
|
|
|
class ISystemSettingsServer : IpcService
|
2018-03-01 21:23:55 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
private Dictionary<int, ServiceProcessRequest> _commands;
|
2018-03-01 21:23:55 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
|
2018-03-01 21:23:55 +01:00
|
|
|
|
2018-04-06 06:01:52 +02:00
|
|
|
public ISystemSettingsServer()
|
2018-03-01 21:23:55 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_commands = new Dictionary<int, ServiceProcessRequest>
|
2018-03-01 21:23:55 +01:00
|
|
|
{
|
2018-11-18 20:37:41 +01:00
|
|
|
{ 3, GetFirmwareVersion },
|
2018-04-26 02:52:22 +02:00
|
|
|
{ 4, GetFirmwareVersion2 },
|
|
|
|
{ 23, GetColorSetId },
|
|
|
|
{ 24, SetColorSetId },
|
|
|
|
{ 38, GetSettingsItemValue }
|
2018-03-01 21:23:55 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-18 20:37:41 +01:00
|
|
|
// GetFirmwareVersion() -> buffer<nn::settings::system::FirmwareVersion, 0x1a, 0x100>
|
2018-12-06 12:16:24 +01:00
|
|
|
public static long GetFirmwareVersion(ServiceCtx context)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return GetFirmwareVersion2(context);
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetFirmwareVersion2() -> buffer<nn::settings::system::FirmwareVersion, 0x1a, 0x100>
|
2018-12-06 12:16:24 +01:00
|
|
|
public static long GetFirmwareVersion2(ServiceCtx context)
|
2018-04-22 01:04:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
long replyPos = context.Request.RecvListBuff[0].Position;
|
|
|
|
long replySize = context.Request.RecvListBuff[0].Size;
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
byte[] firmwareData = GetFirmwareData(context.Device);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (firmwareData != null)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
context.Memory.WriteBytes(replyPos, firmwareData);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
const byte majorFwVersion = 0x03;
|
|
|
|
const byte minorFwVersion = 0x00;
|
|
|
|
const byte microFwVersion = 0x00;
|
|
|
|
const byte unknown = 0x00; //Build?
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
const int revisionNumber = 0x0A;
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
const string platform = "NX";
|
|
|
|
const string unknownHex = "7fbde2b0bba4d14107bf836e4643043d9f6c8e47";
|
|
|
|
const string version = "3.0.0";
|
|
|
|
const string build = "NintendoSDK Firmware for NX 3.0.0-10.0";
|
2018-04-22 01:04:43 +02:00
|
|
|
|
|
|
|
//http://switchbrew.org/index.php?title=System_Version_Title
|
2018-12-06 12:16:24 +01:00
|
|
|
using (MemoryStream ms = new MemoryStream(0x100))
|
2018-04-22 01:04:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
BinaryWriter writer = new BinaryWriter(ms);
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(majorFwVersion);
|
|
|
|
writer.Write(minorFwVersion);
|
|
|
|
writer.Write(microFwVersion);
|
|
|
|
writer.Write(unknown);
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(revisionNumber);
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(Encoding.ASCII.GetBytes(platform));
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ms.Seek(0x28, SeekOrigin.Begin);
|
2018-06-09 18:05:41 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(Encoding.ASCII.GetBytes(unknownHex));
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ms.Seek(0x68, SeekOrigin.Begin);
|
2018-06-09 18:05:41 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(Encoding.ASCII.GetBytes(version));
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ms.Seek(0x80, SeekOrigin.Begin);
|
2018-06-09 18:05:41 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
writer.Write(Encoding.ASCII.GetBytes(build));
|
2018-04-22 01:04:43 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
context.Memory.WriteBytes(replyPos, ms.ToArray());
|
2018-04-22 01:04:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-18 20:37:41 +01:00
|
|
|
// GetColorSetId() -> i32
|
2018-12-06 12:16:24 +01:00
|
|
|
public static long GetColorSetId(ServiceCtx context)
|
2018-03-01 21:23:55 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
context.ResponseData.Write((int)context.Device.System.State.ThemeColor);
|
2018-03-03 18:04:58 +01:00
|
|
|
|
2018-03-01 21:23:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-18 20:37:41 +01:00
|
|
|
// GetColorSetId() -> i32
|
2018-12-06 12:16:24 +01:00
|
|
|
public static long SetColorSetId(ServiceCtx context)
|
2018-04-06 06:01:52 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int colorSetId = context.RequestData.ReadInt32();
|
2018-04-17 02:24:42 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
context.Device.System.State.ThemeColor = (ColorSet)colorSetId;
|
2018-08-15 00:02:42 +02:00
|
|
|
|
2018-03-01 21:23:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-09 18:05:41 +02:00
|
|
|
|
2018-11-18 20:37:41 +01:00
|
|
|
// GetSettingsItemValue(buffer<nn::settings::SettingsName, 0x19, 0x48>, buffer<nn::settings::SettingsItemKey, 0x19, 0x48>) -> (u64, buffer<unknown, 6, 0>)
|
2018-12-06 12:16:24 +01:00
|
|
|
public static long GetSettingsItemValue(ServiceCtx context)
|
2018-04-26 02:52:22 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
long classPos = context.Request.PtrBuff[0].Position;
|
|
|
|
long classSize = context.Request.PtrBuff[0].Size;
|
2018-04-26 02:52:22 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
long namePos = context.Request.PtrBuff[1].Position;
|
|
|
|
long nameSize = context.Request.PtrBuff[1].Size;
|
2018-04-26 02:52:22 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
long replyPos = context.Request.ReceiveBuff[0].Position;
|
|
|
|
long replySize = context.Request.ReceiveBuff[0].Size;
|
2018-04-26 02:52:22 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
byte[] Class = context.Memory.ReadBytes(classPos, classSize);
|
|
|
|
byte[] name = context.Memory.ReadBytes(namePos, nameSize);
|
2018-04-26 02:52:22 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
string askedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(name).Trim('\0');
|
2018-04-26 02:52:22 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting);
|
2018-04-26 02:52:22 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (nxSetting != null)
|
2018-04-26 02:52:22 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
byte[] settingBuffer = new byte[replySize];
|
2018-04-26 02:52:22 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (nxSetting is string stringValue)
|
2018-04-26 02:52:22 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (stringValue.Length + 1 > replySize)
|
2018-06-09 18:05:41 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Logger.PrintError(LogClass.ServiceSet, $"{askedSetting} String value size is too big!");
|
2018-04-26 02:52:22 +02:00
|
|
|
}
|
|
|
|
else
|
2018-06-09 18:05:41 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
settingBuffer = Encoding.ASCII.GetBytes(stringValue + "\0");
|
2018-04-26 02:52:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 00:02:42 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (nxSetting is int intValue)
|
2018-04-26 02:52:22 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
settingBuffer = BitConverter.GetBytes(intValue);
|
2018-04-26 02:52:22 +02:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (nxSetting is bool boolValue)
|
2018-04-26 02:52:22 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
settingBuffer[0] = boolValue ? (byte)1 : (byte)0;
|
2018-04-26 02:52:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
throw new NotImplementedException(nxSetting.GetType().Name);
|
2018-04-26 02:52:22 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
context.Memory.WriteBytes(replyPos, settingBuffer);
|
2018-04-26 02:52:22 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
Logger.PrintDebug(LogClass.ServiceSet, $"{askedSetting} set value: {nxSetting} as {nxSetting.GetType()}");
|
2018-04-26 02:52:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Logger.PrintError(LogClass.ServiceSet, $"{askedSetting} not found!");
|
2018-04-26 02:52:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public static byte[] GetFirmwareData(Switch device)
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
long titleId = 0x0100000000000809;
|
|
|
|
string contentPath = device.System.ContentManager.GetInstalledContentPath(titleId, StorageId.NandSystem, ContentType.Data);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if(string.IsNullOrWhiteSpace(contentPath))
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-05 01:41:49 +01:00
|
|
|
string firmwareTitlePath = device.FileSystem.SwitchPathToSystemPath(contentPath);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
using(IStorage firmwareStorage = new LocalStorage(firmwareTitlePath, FileAccess.Read))
|
|
|
|
{
|
|
|
|
Nca firmwareContent = new Nca(device.System.KeySet, firmwareStorage);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
if (!firmwareContent.CanOpenSection(NcaSectionType.Data))
|
2018-11-18 20:37:41 +01:00
|
|
|
{
|
2019-01-05 01:41:49 +01:00
|
|
|
return null;
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
IFileSystem firmwareRomFs = firmwareContent.OpenFileSystem(NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
IFile firmwareFile = firmwareRomFs.OpenFile("/file", OpenMode.Read);
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
byte[] data = new byte[firmwareFile.GetSize()];
|
2019-01-05 01:41:49 +01:00
|
|
|
|
|
|
|
firmwareFile.Read(data, 0);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2018-11-18 20:37:41 +01:00
|
|
|
}
|
2018-03-01 21:23:55 +01:00
|
|
|
}
|
2018-04-26 02:52:22 +02:00
|
|
|
}
|