ISystemSettingsServer.cs 11 KB

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