ISystemSettingsServer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. [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(62)]
  180. // GetDebugModeFlag() -> bool
  181. public ResultCode GetDebugModeFlag(ServiceCtx context)
  182. {
  183. context.ResponseData.Write(false);
  184. Logger.Stub?.PrintStub(LogClass.ServiceSet);
  185. return ResultCode.Success;
  186. }
  187. [CommandHipc(77)]
  188. // GetDeviceNickName() -> buffer<nn::settings::system::DeviceNickName, 0x16>
  189. public ResultCode GetDeviceNickName(ServiceCtx context)
  190. {
  191. ulong deviceNickNameBufferPosition = context.Request.ReceiveBuff[0].Position;
  192. ulong deviceNickNameBufferSize = context.Request.ReceiveBuff[0].Size;
  193. if (deviceNickNameBufferPosition == 0)
  194. {
  195. return ResultCode.NullDeviceNicknameBuffer;
  196. }
  197. if (deviceNickNameBufferSize != 0x80)
  198. {
  199. Logger.Warning?.Print(LogClass.ServiceSet, "Wrong buffer size");
  200. }
  201. context.Memory.Write(deviceNickNameBufferPosition, Encoding.ASCII.GetBytes(context.Device.System.State.DeviceNickName + '\0'));
  202. return ResultCode.Success;
  203. }
  204. [CommandHipc(78)]
  205. // SetDeviceNickName(buffer<nn::settings::system::DeviceNickName, 0x15>)
  206. public ResultCode SetDeviceNickName(ServiceCtx context)
  207. {
  208. ulong deviceNickNameBufferPosition = context.Request.SendBuff[0].Position;
  209. ulong deviceNickNameBufferSize = context.Request.SendBuff[0].Size;
  210. byte[] deviceNickNameBuffer = new byte[deviceNickNameBufferSize];
  211. context.Memory.Read(deviceNickNameBufferPosition, deviceNickNameBuffer);
  212. context.Device.System.State.DeviceNickName = Encoding.ASCII.GetString(deviceNickNameBuffer);
  213. return ResultCode.Success;
  214. }
  215. [CommandHipc(90)]
  216. // GetMiiAuthorId() -> nn::util::Uuid
  217. public ResultCode GetMiiAuthorId(ServiceCtx context)
  218. {
  219. // NOTE: If miiAuthorId is null ResultCode.NullMiiAuthorIdBuffer is returned.
  220. // Doesn't occur in our case.
  221. context.ResponseData.Write(Mii.Helper.GetDeviceId());
  222. return ResultCode.Success;
  223. }
  224. public byte[] GetFirmwareData(Switch device)
  225. {
  226. const ulong SystemVersionTitleId = 0x0100000000000809;
  227. string contentPath = device.System.ContentManager.GetInstalledContentPath(SystemVersionTitleId, StorageId.BuiltInSystem, NcaContentType.Data);
  228. if (string.IsNullOrWhiteSpace(contentPath))
  229. {
  230. return null;
  231. }
  232. string firmwareTitlePath = device.FileSystem.SwitchPathToSystemPath(contentPath);
  233. using(IStorage firmwareStorage = new LocalStorage(firmwareTitlePath, FileAccess.Read))
  234. {
  235. Nca firmwareContent = new Nca(device.System.KeySet, firmwareStorage);
  236. if (!firmwareContent.CanOpenSection(NcaSectionType.Data))
  237. {
  238. return null;
  239. }
  240. IFileSystem firmwareRomFs = firmwareContent.OpenFileSystem(NcaSectionType.Data, device.System.FsIntegrityCheckLevel);
  241. using var firmwareFile = new UniqueRef<IFile>();
  242. Result result = firmwareRomFs.OpenFile(ref firmwareFile.Ref(), "/file".ToU8Span(), OpenMode.Read);
  243. if (result.IsFailure())
  244. {
  245. return null;
  246. }
  247. result = firmwareFile.Get.GetSize(out long fileSize);
  248. if (result.IsFailure())
  249. {
  250. return null;
  251. }
  252. byte[] data = new byte[fileSize];
  253. result = firmwareFile.Get.Read(out _, 0, data);
  254. if (result.IsFailure())
  255. {
  256. return null;
  257. }
  258. return data;
  259. }
  260. }
  261. }
  262. }