ISystemSettingsServer.cs 11 KB

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