ISystemSettingsServer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using LibHac;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.Fs.Fsa;
  5. using LibHac.FsSystem;
  6. using LibHac.Ncm;
  7. using LibHac.Tools.FsSystem.NcaUtils;
  8. using Ryujinx.Common;
  9. using Ryujinx.Common.Logging;
  10. using Ryujinx.HLE.HOS.SystemState;
  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. [CommandCmif(3)]
  21. // GetFirmwareVersion() -> buffer<nn::settings::system::FirmwareVersion, 0x1a, 0x100>
  22. public ResultCode GetFirmwareVersion(ServiceCtx context)
  23. {
  24. return GetFirmwareVersion2(context);
  25. }
  26. [CommandCmif(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(0x100);
  49. BinaryWriter writer = new(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. return ResultCode.Success;
  64. }
  65. [CommandCmif(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. [CommandCmif(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. [CommandCmif(37)]
  81. // GetSettingsItemValueSize(buffer<nn::settings::SettingsName, 0x19>, buffer<nn::settings::SettingsItemKey, 0x19>) -> u64
  82. public ResultCode GetSettingsItemValueSize(ServiceCtx context)
  83. {
  84. ulong classPos = context.Request.PtrBuff[0].Position;
  85. ulong classSize = context.Request.PtrBuff[0].Size;
  86. ulong namePos = context.Request.PtrBuff[1].Position;
  87. ulong nameSize = context.Request.PtrBuff[1].Size;
  88. byte[] classBuffer = new byte[classSize];
  89. context.Memory.Read(classPos, classBuffer);
  90. byte[] nameBuffer = new byte[nameSize];
  91. context.Memory.Read(namePos, nameBuffer);
  92. string askedSetting = Encoding.ASCII.GetString(classBuffer).Trim('\0') + "!" + Encoding.ASCII.GetString(nameBuffer).Trim('\0');
  93. NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting);
  94. if (nxSetting != null)
  95. {
  96. ulong settingSize;
  97. if (nxSetting is string stringValue)
  98. {
  99. settingSize = (ulong)stringValue.Length + 1;
  100. }
  101. else if (nxSetting is int)
  102. {
  103. settingSize = sizeof(int);
  104. }
  105. else if (nxSetting is bool)
  106. {
  107. settingSize = 1;
  108. }
  109. else
  110. {
  111. throw new NotImplementedException(nxSetting.GetType().Name);
  112. }
  113. context.ResponseData.Write(settingSize);
  114. }
  115. return ResultCode.Success;
  116. }
  117. [CommandCmif(38)]
  118. // GetSettingsItemValue(buffer<nn::settings::SettingsName, 0x19, 0x48>, buffer<nn::settings::SettingsItemKey, 0x19, 0x48>) -> (u64, buffer<unknown, 6, 0>)
  119. public ResultCode GetSettingsItemValue(ServiceCtx context)
  120. {
  121. ulong classPos = context.Request.PtrBuff[0].Position;
  122. ulong classSize = context.Request.PtrBuff[0].Size;
  123. ulong namePos = context.Request.PtrBuff[1].Position;
  124. ulong nameSize = context.Request.PtrBuff[1].Size;
  125. ulong replyPos = context.Request.ReceiveBuff[0].Position;
  126. ulong replySize = context.Request.ReceiveBuff[0].Size;
  127. byte[] classBuffer = new byte[classSize];
  128. context.Memory.Read(classPos, classBuffer);
  129. byte[] nameBuffer = new byte[nameSize];
  130. context.Memory.Read(namePos, nameBuffer);
  131. string askedSetting = Encoding.ASCII.GetString(classBuffer).Trim('\0') + "!" + Encoding.ASCII.GetString(nameBuffer).Trim('\0');
  132. NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting);
  133. if (nxSetting != null)
  134. {
  135. byte[] settingBuffer = new byte[replySize];
  136. if (nxSetting is string stringValue)
  137. {
  138. if ((ulong)(stringValue.Length + 1) > replySize)
  139. {
  140. Logger.Error?.Print(LogClass.ServiceSet, $"{askedSetting} String value size is too big!");
  141. }
  142. else
  143. {
  144. settingBuffer = Encoding.ASCII.GetBytes(stringValue + "\0");
  145. }
  146. }
  147. if (nxSetting is int intValue)
  148. {
  149. settingBuffer = BitConverter.GetBytes(intValue);
  150. }
  151. else if (nxSetting is bool boolValue)
  152. {
  153. settingBuffer[0] = boolValue ? (byte)1 : (byte)0;
  154. }
  155. else
  156. {
  157. throw new NotImplementedException(nxSetting.GetType().Name);
  158. }
  159. context.Memory.Write(replyPos, settingBuffer);
  160. Logger.Debug?.Print(LogClass.ServiceSet, $"{askedSetting} set value: {nxSetting} as {nxSetting.GetType()}");
  161. }
  162. else
  163. {
  164. Logger.Error?.Print(LogClass.ServiceSet, $"{askedSetting} not found!");
  165. }
  166. return ResultCode.Success;
  167. }
  168. [CommandCmif(60)]
  169. // IsUserSystemClockAutomaticCorrectionEnabled() -> bool
  170. public ResultCode IsUserSystemClockAutomaticCorrectionEnabled(ServiceCtx context)
  171. {
  172. // NOTE: When set to true, is automatically synced with the internet.
  173. context.ResponseData.Write(true);
  174. Logger.Stub?.PrintStub(LogClass.ServiceSet);
  175. return ResultCode.Success;
  176. }
  177. [CommandCmif(62)]
  178. // GetDebugModeFlag() -> bool
  179. public ResultCode GetDebugModeFlag(ServiceCtx context)
  180. {
  181. context.ResponseData.Write(false);
  182. Logger.Stub?.PrintStub(LogClass.ServiceSet);
  183. return ResultCode.Success;
  184. }
  185. [CommandCmif(68)]
  186. // GetSerialNumber() -> buffer<nn::settings::system::SerialNumber, 0x16>
  187. public ResultCode GetSerialNumber(ServiceCtx context)
  188. {
  189. context.ResponseData.Write(Encoding.ASCII.GetBytes("RYU00000000000"));
  190. return ResultCode.Success;
  191. }
  192. [CommandCmif(77)]
  193. // GetDeviceNickName() -> buffer<nn::settings::system::DeviceNickName, 0x16>
  194. public ResultCode GetDeviceNickName(ServiceCtx context)
  195. {
  196. ulong deviceNickNameBufferPosition = context.Request.ReceiveBuff[0].Position;
  197. ulong deviceNickNameBufferSize = context.Request.ReceiveBuff[0].Size;
  198. if (deviceNickNameBufferPosition == 0)
  199. {
  200. return ResultCode.NullDeviceNicknameBuffer;
  201. }
  202. if (deviceNickNameBufferSize != 0x80)
  203. {
  204. Logger.Warning?.Print(LogClass.ServiceSet, "Wrong buffer size");
  205. }
  206. context.Memory.Write(deviceNickNameBufferPosition, Encoding.ASCII.GetBytes(context.Device.System.State.DeviceNickName + '\0'));
  207. return ResultCode.Success;
  208. }
  209. [CommandCmif(78)]
  210. // SetDeviceNickName(buffer<nn::settings::system::DeviceNickName, 0x15>)
  211. public ResultCode SetDeviceNickName(ServiceCtx context)
  212. {
  213. ulong deviceNickNameBufferPosition = context.Request.SendBuff[0].Position;
  214. ulong deviceNickNameBufferSize = context.Request.SendBuff[0].Size;
  215. byte[] deviceNickNameBuffer = new byte[deviceNickNameBufferSize];
  216. context.Memory.Read(deviceNickNameBufferPosition, deviceNickNameBuffer);
  217. context.Device.System.State.DeviceNickName = Encoding.ASCII.GetString(deviceNickNameBuffer);
  218. return ResultCode.Success;
  219. }
  220. [CommandCmif(90)]
  221. // GetMiiAuthorId() -> nn::util::Uuid
  222. public ResultCode GetMiiAuthorId(ServiceCtx context)
  223. {
  224. // NOTE: If miiAuthorId is null ResultCode.NullMiiAuthorIdBuffer is returned.
  225. // Doesn't occur in our case.
  226. context.ResponseData.Write(Mii.Helper.GetDeviceId());
  227. return ResultCode.Success;
  228. }
  229. public byte[] GetFirmwareData(Switch device)
  230. {
  231. const ulong SystemVersionTitleId = 0x0100000000000809;
  232. string contentPath = device.System.ContentManager.GetInstalledContentPath(SystemVersionTitleId, StorageId.BuiltInSystem, NcaContentType.Data);
  233. if (string.IsNullOrWhiteSpace(contentPath))
  234. {
  235. return null;
  236. }
  237. string firmwareTitlePath = FileSystem.VirtualFileSystem.SwitchPathToSystemPath(contentPath);
  238. using IStorage firmwareStorage = new LocalStorage(firmwareTitlePath, FileAccess.Read);
  239. Nca firmwareContent = new(device.System.KeySet, firmwareStorage);
  240. if (!firmwareContent.CanOpenSection(NcaSectionType.Data))
  241. {
  242. return null;
  243. }
  244. IFileSystem firmwareRomFs = firmwareContent.OpenFileSystem(NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
  245. using UniqueRef<IFile> firmwareFile = new UniqueRef<IFile>();
  246. Result result = firmwareRomFs.OpenFile(ref firmwareFile.Ref, "/file".ToU8Span(), OpenMode.Read);
  247. if (result.IsFailure())
  248. {
  249. return null;
  250. }
  251. result = firmwareFile.Get.GetSize(out long fileSize);
  252. if (result.IsFailure())
  253. {
  254. return null;
  255. }
  256. byte[] data = new byte[fileSize];
  257. result = firmwareFile.Get.Read(out _, 0, data);
  258. if (result.IsFailure())
  259. {
  260. return null;
  261. }
  262. return data;
  263. }
  264. }
  265. }