ISystemSettingsServer.cs 8.7 KB

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