ISystemSettingsServer.cs 8.7 KB

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