ISystemSettingsServer.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using LibHac.Fs;
  2. using LibHac.Fs.NcaUtils;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.HLE.FileSystem;
  5. using Ryujinx.HLE.HOS.SystemState;
  6. using System;
  7. using System.IO;
  8. using System.Text;
  9. namespace Ryujinx.HLE.HOS.Services.Set
  10. {
  11. [Service("set:sys")]
  12. class ISystemSettingsServer : IpcService
  13. {
  14. public ISystemSettingsServer(ServiceCtx context) { }
  15. [Command(3)]
  16. // GetFirmwareVersion() -> buffer<nn::settings::system::FirmwareVersion, 0x1a, 0x100>
  17. public ResultCode GetFirmwareVersion(ServiceCtx context)
  18. {
  19. return GetFirmwareVersion2(context);
  20. }
  21. [Command(4)]
  22. // GetFirmwareVersion2() -> buffer<nn::settings::system::FirmwareVersion, 0x1a, 0x100>
  23. public ResultCode GetFirmwareVersion2(ServiceCtx context)
  24. {
  25. long replyPos = context.Request.RecvListBuff[0].Position;
  26. long replySize = context.Request.RecvListBuff[0].Size;
  27. byte[] firmwareData = GetFirmwareData(context.Device);
  28. if (firmwareData != null)
  29. {
  30. context.Memory.WriteBytes(replyPos, firmwareData);
  31. return ResultCode.Success;
  32. }
  33. const byte majorFwVersion = 0x03;
  34. const byte minorFwVersion = 0x00;
  35. const byte microFwVersion = 0x00;
  36. const byte unknown = 0x00; //Build?
  37. const int revisionNumber = 0x0A;
  38. const string platform = "NX";
  39. const string unknownHex = "7fbde2b0bba4d14107bf836e4643043d9f6c8e47";
  40. const string version = "3.0.0";
  41. const string build = "NintendoSDK Firmware for NX 3.0.0-10.0";
  42. // http://switchbrew.org/index.php?title=System_Version_Title
  43. using (MemoryStream ms = new MemoryStream(0x100))
  44. {
  45. BinaryWriter writer = new BinaryWriter(ms);
  46. writer.Write(majorFwVersion);
  47. writer.Write(minorFwVersion);
  48. writer.Write(microFwVersion);
  49. writer.Write(unknown);
  50. writer.Write(revisionNumber);
  51. writer.Write(Encoding.ASCII.GetBytes(platform));
  52. ms.Seek(0x28, SeekOrigin.Begin);
  53. writer.Write(Encoding.ASCII.GetBytes(unknownHex));
  54. ms.Seek(0x68, SeekOrigin.Begin);
  55. writer.Write(Encoding.ASCII.GetBytes(version));
  56. ms.Seek(0x80, SeekOrigin.Begin);
  57. writer.Write(Encoding.ASCII.GetBytes(build));
  58. context.Memory.WriteBytes(replyPos, ms.ToArray());
  59. }
  60. return ResultCode.Success;
  61. }
  62. [Command(23)]
  63. // GetColorSetId() -> i32
  64. public ResultCode GetColorSetId(ServiceCtx context)
  65. {
  66. context.ResponseData.Write((int)context.Device.System.State.ThemeColor);
  67. return ResultCode.Success;
  68. }
  69. [Command(24)]
  70. // GetColorSetId() -> i32
  71. public ResultCode SetColorSetId(ServiceCtx context)
  72. {
  73. int colorSetId = context.RequestData.ReadInt32();
  74. context.Device.System.State.ThemeColor = (ColorSet)colorSetId;
  75. return ResultCode.Success;
  76. }
  77. [Command(38)]
  78. // GetSettingsItemValue(buffer<nn::settings::SettingsName, 0x19, 0x48>, buffer<nn::settings::SettingsItemKey, 0x19, 0x48>) -> (u64, buffer<unknown, 6, 0>)
  79. public ResultCode GetSettingsItemValue(ServiceCtx context)
  80. {
  81. long classPos = context.Request.PtrBuff[0].Position;
  82. long classSize = context.Request.PtrBuff[0].Size;
  83. long namePos = context.Request.PtrBuff[1].Position;
  84. long nameSize = context.Request.PtrBuff[1].Size;
  85. long replyPos = context.Request.ReceiveBuff[0].Position;
  86. long replySize = context.Request.ReceiveBuff[0].Size;
  87. byte[] Class = context.Memory.ReadBytes(classPos, classSize);
  88. byte[] name = context.Memory.ReadBytes(namePos, nameSize);
  89. string askedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(name).Trim('\0');
  90. NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting);
  91. if (nxSetting != null)
  92. {
  93. byte[] settingBuffer = new byte[replySize];
  94. if (nxSetting is string stringValue)
  95. {
  96. if (stringValue.Length + 1 > replySize)
  97. {
  98. Logger.PrintError(LogClass.ServiceSet, $"{askedSetting} String value size is too big!");
  99. }
  100. else
  101. {
  102. settingBuffer = Encoding.ASCII.GetBytes(stringValue + "\0");
  103. }
  104. }
  105. if (nxSetting is int intValue)
  106. {
  107. settingBuffer = BitConverter.GetBytes(intValue);
  108. }
  109. else if (nxSetting is bool boolValue)
  110. {
  111. settingBuffer[0] = boolValue ? (byte)1 : (byte)0;
  112. }
  113. else
  114. {
  115. throw new NotImplementedException(nxSetting.GetType().Name);
  116. }
  117. context.Memory.WriteBytes(replyPos, settingBuffer);
  118. Logger.PrintDebug(LogClass.ServiceSet, $"{askedSetting} set value: {nxSetting} as {nxSetting.GetType()}");
  119. }
  120. else
  121. {
  122. Logger.PrintError(LogClass.ServiceSet, $"{askedSetting} not found!");
  123. }
  124. return ResultCode.Success;
  125. }
  126. public byte[] GetFirmwareData(Switch device)
  127. {
  128. long titleId = 0x0100000000000809;
  129. string contentPath = device.System.ContentManager.GetInstalledContentPath(titleId, StorageId.NandSystem, ContentType.Data);
  130. if (string.IsNullOrWhiteSpace(contentPath))
  131. {
  132. return null;
  133. }
  134. string firmwareTitlePath = device.FileSystem.SwitchPathToSystemPath(contentPath);
  135. using(IStorage firmwareStorage = new LocalStorage(firmwareTitlePath, FileAccess.Read))
  136. {
  137. Nca firmwareContent = new Nca(device.System.KeySet, firmwareStorage);
  138. if (!firmwareContent.CanOpenSection(NcaSectionType.Data))
  139. {
  140. return null;
  141. }
  142. IFileSystem firmwareRomFs = firmwareContent.OpenFileSystem(NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
  143. IFile firmwareFile = firmwareRomFs.OpenFile("/file", OpenMode.Read);
  144. byte[] data = new byte[firmwareFile.GetSize()];
  145. firmwareFile.Read(data, 0);
  146. return data;
  147. }
  148. }
  149. }
  150. }